【问题标题】:Update a slice or column in tensor更新张量中的切片或列
【发布时间】:2019-03-13 22:03:27
【问题描述】:

无论张量的形状如何,我都在尝试更新一整列,列的索引也可以是形状范围内的任何内容。

 tensor = tf.Variable(tf.ones((5,5)))

我正在尝试实现这个 numpy 操作:

 tensor[:,2] = 0

 [ 1 1 0 1 1 ]
 [ 1 1 0 1 1 ]
 [ 1 1 0 1 1 ]
 [ 1 1 0 1 1 ]
 [ 1 1 0 1 1 ]

我尝试使用 tf.scatter_update 但没有成功。

【问题讨论】:

    标签: python numpy tensorflow


    【解决方案1】:

    如果你不介意创建一个新的张量,你可以尝试:

    import tensorflow as tf
    
    t = tf.Variable(tf.ones((5,5)))
    s0, s1, s2 = tf.split(t, [2, 1, 2], axis=1)
    s = tf.concat([s0,tf.zeros((5,1)),s2], axis=1)
    
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        y = sess.run(s)
        print(y)
    

    【讨论】:

    • 我考虑过这种方式,但问题正如我所说,我希望它适用于任何形状。
    • 并选择任何列。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-01
    • 2018-03-15
    • 2016-01-19
    • 1970-01-01
    • 2019-10-21
    • 1970-01-01
    • 2016-07-04
    相关资源
    最近更新 更多