张量流将执行哪些操作

时间:2019-06-29 14:01:48

标签: python tensorflow

我想将tf.variable与tf.placeholder一起使用。但是,我不知道应该在图中的何处使用feed_dict。

有时我会尝试。让我展示我的代码。

#!/usr/bin/env python
# -*- coding:utf-8 -*-

import tensorflow as tf

var = tf.get_variable('name', [1, 1], tf.float32)
pla = tf.placeholder(tf.float32, shape=var.get_shape())
new_var = var.assign(pla)
label = tf.placeholder(tf.float32, shape=(1, 1))
y = tf.reduce_sum(var, 0)
loss = tf.square(tf.subtract(label, y))

sess = tf.Session()
sess.run(tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()))

feeddict = {pla: [[1]], label:[[1]]}

# ok. will be 1 as expect
#sess.run(new_var, feed_dict=feeddict)
#print sess.run([y, loss], feed_dict=feeddict)[0]

# case 1. ok. will be 1 as expect
#print sess.run([y, loss, new_var], feed_dict=feeddict)[0]

# case 2. ok. will be a random value. why the assign op won't be executed?
#print sess.run([y, loss], feed_dict=feeddict)[0]

# case 3. ok. will be a random value
#print sess.run(y)

# case 4. fail. why it will fail?
#sess.run([y, loss])

# case 5. fail. why?
sess.run(new_var, feed_dict=feeddict)
sess.run([y, loss])

0 个答案:

没有答案