我有以下代码段。我从矩阵中每行采样一个项目。此项应为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'对象不支持项目分配'