我在 tensorflow 中定义了一个无监督问题,我需要在每次迭代时更新我的 B 和我的 tfZ,但我不知道如何tfZ
使用 tensorflow 会话来更新我的。
tfY = tf.placeholder(shape=(15, 15), dtype=tf.float32)
with tf.variable_scope('test'):
B = tf.Variable(tf.zeros([]))
tfZ = tf.convert_to_tensor(Z, dtype=tf.float32)
def loss(tfY):
r = tf.reduce_sum(tfZ*tfZ, 1)
r = tf.reshape(r, [-1, 1])
D = tf.sqrt(r - 2*tf.matmul(tfZ, tf.transpose(tfZ)) + tf.transpose(r) + 1e-9)
return tf.reduce_sum(tfY*tf.log(tf.sigmoid(D+B))+(1-tfY)*tf.log(1-tf.sigmoid(D+B)))
LOSS = loss(Y)
GRADIENT = tf.gradients(LOSS, [B, tfZ])
sess = tf.Session()
sess.run(tf.global_variables_initializer())
tot_loss = sess.run(LOSS, feed_dict={tfY: Y})
loss_grad = sess.run(GRADIENT, feed_dict={tfY: Y})
learning_rate = 1e-4
for i in range(1000):
sess.run(B.assign(B - learning_rate * loss_grad[0]))
print(tfZ)
sess.run(tfZ.assign(tfZ - learning_rate * loss_grad[1]))
tot_loss = sess.run(LOSS, feed_dict={tfY: Y})
if i%10==0:
print(tot_loss)
此代码打印以下内容:
Tensor("test_18/Const:0", shape=(15, 2), dtype=float32)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-35-74ddafc0bf3a> in <module>()
25 sess.run(B.assign(B - learning_rate * loss_grad[0]))
26 print(tfZ)
---> 27 sess.run(tfZ.assign(tfZ - learning_rate * loss_grad[1]))
28
29 tot_loss = sess.run(LOSS, feed_dict={tfY: Y})
AttributeError: 'Tensor' object has no attribute 'assign'
张量对象正确地没有分配属性,但我找不到任何其他附加到对象的函数可以做到这一点。如何正确更新我的张量?