[英]ValueError: Dimensions must be equal, but are 784 and 500 for 'Mul' (op: 'Mul') with input shapes: [?,784], [784,500]
[英]ValueError: Dimensions must be equal, but are 4096 and 9 for 'mul'. Why no broadcasting here?
我有一個非常簡單的示例:
import tensorflow as tf
import pdb
number_features = tf.random_uniform((4096,22))
probs = number_features
probs_L = probs[:,:3]
probs_S1 = probs[:,3:12]
probs_S2 = probs[:,12:22]
confidence_no_digits = probs_L[:,0]
confidence_single_digit = probs_L[:,1] * probs_S1
with tf.Session() as sess:
result = sess.run([confidence_single_digit])
但這給出了:
ValueError:尺寸必須相等,但對於輸入形狀為[4096],[4096,9]的“ mul”(操作:“ Mul”),尺寸必須為4096和9。
為什么不能將元素大小為[4096]的向量和大小為[4096,9]的矩陣相乘。 為什么廣播在這里不起作用?
張量流中的廣播遵循與NumPy廣播相同的模式。 當對兩個數組進行運算時,它會從最后一個維度開始逐個元素地比較它們的形狀,然后一直到第一個維度。 在以下情況下,兩個維度兼容:
在這種情況下,根據上述規則,從最后一個尺寸開始,尺寸4096(第一個數組的最后一個尺寸)和9(第二個數組的最后一個尺寸)不兼容,因此會出現錯誤。
為了修復它以獲得所需的廣播效果,可以將第一個數組轉換為具有兼容的形狀:
confidence_single_digit = tf.expand_dims(probs_L[:,1],1) * probs_S1
因此形狀分別為(4096,1)和(4096,9)。
這樣您能得到理想的結果嗎?
confidence_single_digit = tf.expand_dims(probs_L[:,1],1) * probs_S1
現在的形狀是這些。
<bound method Tensor.get_shape of <tf.Tensor 'ExpandDims_1:0'
shape=(4096, 1) dtype=float32>>
<bound method Tensor.get_shape of <tf.Tensor 'strided_slice_2:0'
shape=(4096, 9) dtype=float32>>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.