用值填充张量中的特定索引 [英] Fill a specific index in tensor with a value
本文介绍了用值填充张量中的特定索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是tensorflow的初学者. 我创建了这个张量
I'm beginner with tensorflow. I created this tensor
z = tf.zeros([20,2], tf.float32)
,我想将索引z[2,1]
和z[2,2]
的值更改为1.0
,而不是零.
我该怎么办?
and I want to change the value of index z[2,1]
and z[2,2]
to 1.0
instead of zeros.
How can I do that?
推荐答案
您确切地询问的内容是不可能的,原因有两个:
What you exactly ask is not possible for two reasons:
-
z
是一个恒定张量,无法更改. - 没有
z[2,2]
,只有z[2,0]
和z[2,1]
.
z
is a constant tensor, it can't be changed.- There is no
z[2,2]
, onlyz[2,0]
andz[2,1]
.
但是假设您想将z
更改为变量并修复索引,可以通过以下方式完成:
But assuming you want to change z
to a variable and fix the indices, it can be done this way:
z = tf.Variable(tf.zeros([20,2], tf.float32)) # a variable, not a const
assign21 = tf.assign(z[2, 0], 1.0) # an op to update z
assign22 = tf.assign(z[2, 1], 1.0) # an op to update z
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print(sess.run(z)) # prints all zeros
sess.run([assign21, assign22])
print(sess.run(z)) # prints 1.0 in the 3d row
这篇关于用值填充张量中的特定索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文