[英]InvalidArgumentError: cannot compute MatMul as input #0(zero-based) was expected to be a float tensor but is a double tensor [Op:MatMul]
[英]Tensorflow cannot compute Addv2 as input #1(zero-based) was expected to be a double tensor but it is a float tensor [Op:Addv]
錯誤信息:
tensorflow.python.framework.errors_impl.InvalidArgumentError:無法計算 AddV2 作為輸入 #1(從零開始)應該是雙張量,但它是浮點張量 [Op:AddV2]
在我的代碼中,我創建了一個 tensorflow 分布 MixtureSameFamily object 並使用我的網絡的 output 作為參數。 但是,當我嘗試計算一系列值的概率以生成概率密度 function 時,我收到此錯誤。
我的代碼:
gm = tfd.MixtureSameFamily(
mixture_distribution=tfd.Categorical(probs=alphas),
components_distribution=tfd.Normal(
loc=mus,
scale=sigmas
)
)
x = np.linspace(-2,2,int(1000), dtype=np.double)
print(x.dtype)
pyx = gm.prob(x)
print(x.dtype)
的結果是 "dtype: 'float'"
據我所知,tensorflow根據文檔不支持浮點數據類型。
為此,我特別困惑。 任何幫助將不勝感激。
似乎是最新的 tensorflow-probability 模塊中的一個錯誤。 它僅適用於float32
。
解決方法
將參數顯式轉換為float32
gm = tfd.MixtureSameFamily(
mixture_distribution=tfd.Categorical(probs=alphas.astype('float32')),
components_distribution=tfd.Normal(
loc=mus.astype('float32'),
scale=sigmas.astype('float32')
)
)
x = np.linspace(-2,2,int(1000), dtype='float32')
pyx = gm.prob(x)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.