2017-09-01 97 views
1

有在tensorflow任何方法都可以實現廣播分配矩陣(tf.Variable) 類似下面的代碼....tensorflow變量分配廣播

a = tf.Variable(np.zeros([10,10,10,10], np.int32)) 

# creating a mask and trying to assign the 2nd, 3rd dimension of a 
mask = tf.ones([10,10]) 

# 1) which is work in this case, but only assign one block 
op = a[0,:,:,0].assign(mask) 

# 2) attempting to broadcasting while not work, size mismatch 
op = a[0].assign(mask) 

我目前的解決方案可能會遍歷所有其他尺寸但可能遭受嵌套循環,如1) 或者必須有一個更聰明的方法來做到這一點,謝謝!

回答

0

不是一般的解決方案(大量的硬編碼張量形狀的),但我希望這給你的要點爲你的例子:

a = tf.Variable(np.zeros([10,10,10,10], np.int32)) 
mask = tf.ones([10,10],dtype=tf.int32) 
mask_reshaped = tf.reshape(mask,[1,10,10,1]) # make the number of dims match 
mask_broadcast = tf.tile(mask_reshaped, [10, 1, 1, 10]) # do the actual broadcast 
op = a.assign(mask_broadcast) 
+0

什麼好的技巧!謝謝!它真的很整齊。 –