【发布时间】:2019-05-12 14:24:30
【问题描述】:
我尝试构建图表然后运行它,但我仍然得到
RuntimeError:会话图为空。向图中添加操作 在调用 run() 之前。
这个图内置的功能是
def init_network(self):
self.graph = tf.Graph()
with self.graph.as_default():
self.lstm = tf.contrib.rnn.LSTMCell(self.state_variables)
self.state = self.lstm.zero_state(1, dtype=tf.float32)
self.weights = tf.get_variable("Weights",
shape=[self.state_variables, 1],
dtype=tf.float32)
self.lstm_output_ph = tf.placeholder(shape=[1, self.state_variables], dtype=tf.float32)
inner_product = tf.linalg.matmul(self.lstm_output_ph, self.weights)
q_estimate = tf.nn.softmax(inner_product)
self.reward_ph = tf.placeholder(shape=[1], dtype=tf.float32)
self.previous_q_ph = tf.placeholder(shape=[1], dtype=tf.float32)
loss = tf.subtract(tf.add(self.reward_ph, tf.multiply(self.memory, self.previous_q_ph)), q_estimate)
optimizer = tf.train.GradientDescentOptimizer(
self.learning_rate,
use_locking=False,
name='SGD'
)
self.train_step = optimizer.minimize(loss)
self.state_ph = tf.placeholder(shape=[1, self.state_variables], dtype=tf.float32)
self.last_output, self.state = self.lstm(self.state_ph, self.state)
inner_product_predict = tf.linalg.matmul(self.last_output, self.weights)
q_estimate_init = tf.nn.softmax(inner_product_predict)
self.predict_step = q_estimate_init
然后我尝试使用此图运行会话,如下所示:
with tf.Session(graph=self.graph) as sess:
sess.run(self.train_step,
feed_dict={
self.lstm_output_ph: self.last_output,
self.reward_ph: reward,
self.previous_q_ph: previous_q
})
知道为什么我的图表仍然是空的吗?调试 init_network 方法表明,即使在方法结束时 self.graph 变量仍为空(其 _graph_key 变量仍设置为'grap-key-0/')。
【问题讨论】:
标签: python tensorflow