【问题标题】:tensorflow variable assign broadcastingtensorflow 变量分配广播
【发布时间】:2017-09-01 02:23:04
【问题描述】:

tensorflow中是否有任何方法可以实现对矩阵的广播分配(tf.Variable) 类似于以下代码....

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) 或者必须有更聪明的方法,谢谢!

【问题讨论】:

    标签: python tensorflow assign broadcasting


    【解决方案1】:

    不是通用解决方案(大量硬编码张量形状),但希望这可以为您的示例提供要点:

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-18
      • 2015-06-27
      • 1970-01-01
      相关资源
      最近更新 更多