Tensorflow:不支持项目分配,其他解决方案?

Tensorflow:不支持项目分配,其他解决方案?,tensorflow,Tensorflow,我有以下代码片段。我从矩阵中每行抽取一个项目。此项应为1,而其他项为零。TensorFlow中是否有一个优雅的解决方案 # Some example data weights = tf.random_uniform([10,3], minval=0, maxval=1) # Sample from weights id_x = tf.multinomial(tf.log(weights), 1) # Build corresponding indices ind_range = tf.exp

我有以下代码片段。我从矩阵中每行抽取一个项目。此项应为1,而其他项为零。TensorFlow中是否有一个优雅的解决方案

# Some example data
weights = tf.random_uniform([10,3], minval=0, maxval=1)

# Sample from weights
id_x = tf.multinomial(tf.log(weights), 1)

# Build corresponding indices
ind_range = tf.expand_dims(tf.range(tf.shape(id_x)[0]), 1)
ind = tf.concat(1, [ind_range, tf.cast(id_x, dtype=tf.int32)])
gripper = tf.zeros([tf.shape(id_x)[0], 3])
gripper[ind] = 1

with tf.Session() as sess:
  x,y = sess.run([id_x,ind])

print x
print y

此代码段导致“TypeError:‘Tensor’object不支持项分配”

该代码段对我有效,您应该包括更多信息,如完整堆栈跟踪,带夹持器[ind]的行已注释掉,现在已修复。请再试一次好的……所以张量是不可变的,所以你不能修改它们。一个解决方案是对gripper使用tf.Variable。这个代码片段对我来说很有用,你应该包括更多信息,比如完整的堆栈跟踪,带有gripper[ind]的行被注释掉了,现在已经修复。请再试一次好的……所以张量是不可变的,所以你不能修改它们。一种解决方案是使用tf.变量作为夹具。