相关疑难解决方法(0)

在张量流中操纵矩阵元素

如何在tensorflow中执行以下操作?

mat = [4,2,6,2,3] #
mat[2] = 0 # simple zero the 3rd element
Run Code Online (Sandbox Code Playgroud)

我不能使用[]括号,因为它只适用于常量而不适用于变量.我不能使用切片函数,因为它返回一个张量,你不能分配张量.

import tensorflow as tf
sess = tf.Session()
var1 = tf.Variable(initial_value=[2, 5, -4, 0])
assignZerosOP = (var1[2] = 0) # < ------ This is what I want to do

sess.run(tf.initialize_all_variables())

print sess.run(var1)
sess.run(assignZerosOP)
print sess.run(var1)
Run Code Online (Sandbox Code Playgroud)

会打印

[2, 5, -4, 0] 
[2, 5, 0, 0])
Run Code Online (Sandbox Code Playgroud)

indexing element matrix variable-assignment tensorflow

10
推荐指数
1
解决办法
1万
查看次数