更新张量流中的张量 [英] Updating a tensor in tensorflow

查看:25
本文介绍了更新张量流中的张量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 tensorflow 中定义了一个无监督问题,我需要在每次迭代时更新我的​​ B 和我的 tfZ,但我不知道如何使用 tensorflow 会话更新我的 tfZ.

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)

此代码打印以下内容:

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'

张量对象正确地没有分配属性,但我找不到任何其他附加到可以做到这一点的对象的函数.如何正确更新张量?

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?

推荐答案

不同于 tf.Variable, tf.Tensor 不提供 assign 方法;如果张量可变,你必须调用tf.assign 函数显式:

Unlike tf.Variable, tf.Tensor doesn't provide a assign method; if the tensor is mutable, you have to call tf.assign function explicitly:

tf.assign(tfZ, tfZ - learning_rate * loss_grad[1])

更新:并非所有张量都是可变的,例如你的 tfZ 不是.到目前为止,可变张量只是那些对应于这个答案中解释的变量的张量(至少在 tensorflow 1.x,这可以在未来扩展).普通张量是操作结果的句柄,即它们绑定到该操作及其输入.要更改不可变的张量值,必须更改源张量(占位符或变量).在您的特定情况下,将 tfZ 也设为变量会更容易.

Update: not all tensors are mutable, e.g. your tfZ isn't. As of now, mutable tensors are only those that correspond to variables as explained in this answer (at least in tensorflow 1.x, this can be expanded in the future). Ordinary tensors are handles to the result of an op, i.e. they're bound to that operation and it's inputs. To change an immutable tensor value, one has to change the source tensors (placeholders or variables). In your particular case, it'd be easier to make a tfZ a variable as well.

顺便说一下,tf.Variable.assign() 只是 tf.assign 的包装器,必须在会话中运行结果操作才能实际执行分配.

By the way, tf.Variable.assign() is just a wrapper over tf.assign and one has to run the result op in a session to actually perform an assignment.

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

Note in both cases a new node in the graph is created. If you call it in a loop (like in your snippet), the graph will by inflated by a thousand nodes. Doing so in real production code is a bad practice, because it can easily cause OOM.

这篇关于更新张量流中的张量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆