如何在张量流中分配张量的元素

How to assign an element of an tensor in tensorflow

我想了解以下代码的错误信息

M = K.eye(2)
K.assign(M[0,1],1.0)

我收到的消息是"Tried to convert 'input' to a tensor and failed. Error: None values not supported."

您可以在tensorflow中为变量赋值。这是一个例子。 (我在我安装的tensorflow版本中没有发现K.assign这个操作,btw)

import tensorflow as tf
import keras.backend as K

M = tf.Variable(K.eye(2), tf.float32)
assign_op = tf.assign(M[0,1], 1.0)

with tf.Session() as sess:
  sess.run(tf.global_variables_initializer())
  print(sess.run(M))
  sess.run(assign_op)
  print(sess.run(M))

#[[1. 0.]
# [0. 1.]]

#[[1. 1.]
# [0. 1.]]