相关疑难解决方法(0)

在Tensorflow中,变量和张量之间有什么区别?

Tensorflow文档指出a Variable可以在任何可以使用的地方Tensor使用,它们似乎是可以互换的.例如,如果v是a Variable,则x = 1.0 + v变为a Tensor.

两者之间有什么区别,何时我会使用另一个?

python tensorflow

9
推荐指数
1
解决办法
3032
查看次数

“张量”对象没有属性“assign_add”

当我尝试使用assign_add 或assign_sub 函数时,我遇到了错误“Tensor”对象没有属性“assign_add”。代码如下所示:

我定义了两个张量 t1 和 t2,它们具有相同的形状和相同的数据类型。

>>> t1 = tf.Variable(tf.ones([2,3,4],tf.int32))
>>> t2 = tf.Variable(tf.zeros([2,3,4],tf.int32))
>>> t1
<tf.Variable 'Variable_4:0' shape=(2, 3, 4) dtype=int32_ref>
>>> t2
<tf.Variable 'Variable_5:0' shape=(2, 3, 4) dtype=int32_ref>
Run Code Online (Sandbox Code Playgroud)

然后我在 t1 和 t2 上使用 assign_add 来创建 t3

>>> t3 = tf.assign_add(t1,t2)
>>> t3
<tf.Tensor 'AssignAdd_4:0' shape=(2, 3, 4) dtype=int32_ref>
Run Code Online (Sandbox Code Playgroud)

然后我尝试使用 t1[1] 和 t2[1] 创建一个新的张量 t4,它们是具有相同形状和相同数据类型的张量。

>>> t1[1]   
<tf.Tensor 'strided_slice_23:0' shape=(3, 4) dtype=int32>
>>> t2[1]
<tf.Tensor 'strided_slice_24:0' shape=(3, 4) dtype=int32>
>>> t4 = tf.assign_add(t1[1],t2[1])
Run Code Online (Sandbox Code Playgroud)

但有错误,

Traceback (most recent call …
Run Code Online (Sandbox Code Playgroud)

attributes object tensorflow tensor

6
推荐指数
2
解决办法
8617
查看次数

Updating a tensor in tensorflow

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

python variable-assignment tensorflow tensorflow-gradient

1
推荐指数
1
解决办法
2347
查看次数