Updating a tensor in tensorflow

Nic*_*aiF 1 python variable-assignment tensorflow tensorflow-gradient

I have defined an unsupervised problem in tensorflow, I need to update my B and my tfZ with every iteration, but I don't know how to update my tfZ using the tensorflow session.

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)
Run Code Online (Sandbox Code Playgroud)

This code prints the following:

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'
Run Code Online (Sandbox Code Playgroud)

A tensor object correctly has no assign attribute, but I cannot find any other function attached to the object that could do just that. How do I update my tensor correctly?

Max*_*xim 6

不像tf.Variabletf.Tensor没有提供assign方法;如果张量是可变的,则必须tf.assign显式调用函数:

tf.assign(tfZ, tfZ - learning_rate * loss_grad[1])
Run Code Online (Sandbox Code Playgroud)

更新:并非所有张量都是可变的,例如您tfZ不是。截至目前,可变张量仅是与该答案中说明的变量相对应的张量(至少在tensorflow 1.x中,以后可以扩展)。普通张量是运算结果的句柄,即它们绑定到该操作及其输入。要更改一个不变的张量值,必须更改源张量(占位符或变量)。在您的特定情况下,使tfZ变量变容易。

顺便说一句,tf.Variable.assign()它只是一个包装器tf.assign,必须在会话中运行结果op才能实际执行分配。

请注意,在两种情况下,都会在图形中创建一个新节点。如果您以循环方式(如代码段中)调用它,则该图将膨胀一千个节点。在实际的生产代码中这样做不是一个好习惯,因为它很容易导致OOM。