【发布时间】:2017-08-04 08:54:24
【问题描述】:
我在 tensorflow 中有一个数组,我想根据 for 循环中的另一个数组更新它的值。代码如下:
def get_weights(labels, class_ratio=0.5):
weights = tf.ones_like(labels, dtype=tf.float64))
pos_num = class_ratio * 100
neg_num = 100 - class_ratio * 100
for i in range(labels.shape[0]):
if labels[i] == 0:
weights[i].assign(pos_num/neg_num)
else:
weights[i].assign(neg_num)
return weights
然后我有这段代码来调用上面的函数:
with tf.Graph().as_default():
labels = tf.placeholder(tf.int32, (5,))
example_weights = get_weights(labels, class_ratio=0.1)
with tf.Session() as sess:
np_labels = np.random.randint(0, 2, 5)
np_weights = sess.run(example_weights, feed_dict={labels: np_labels})
print("Labels: %r" % (np_labels,))
print("Weights: %r" % (np_weights,))
但是当我运行它时,它给了我这个错误:
ValueError: Sliced assignment is only supported for variables
如何在 tensorflow 中分配/更新数组的值?
【问题讨论】:
标签: python tensorflow