gpt4 book ai didi

python - 构建后 session 图为空

转载 作者:太空宇宙 更新时间:2023-11-03 20:54:40 24 4
gpt4 key购买 nike

我尝试构建一个图表然后运行它,但我仍然得到

RuntimeError: The Session graph is empty. Add operations to the graph before calling 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

然后我尝试使用此图运行 session ,如下所示:

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/')。

最佳答案

这不是一个实际的答案(如果有人发布更好的答案,我会删除它,或者如果我找到实际的解释,我会改进它)。我通过以下方式更改图形定义成功解决了这个问题:

def build_graph(self, init_state):
self.graph = tf.Graph()
with self.graph.as_default():
self.lstm = tf.keras.layers.CuDNNLSTM(units=4, stateful=True)
self.state = self.lstm.get_initial_state(inputs=init_state)
self.weights = tf.get_variable("Weights",
shape=[self.state_variables, 1],
dtype=tf.float32,
collections=[tf.GraphKeys.GLOBAL_VARIABLES,
tf.GraphKeys.TRAINABLE_VARIABLES])

self.input_ph = tf.placeholder(
name="input",
shape=[1, 1, self.state_variables],
dtype=tf.float32)

self.output_step = self.lstm(self.input_ph)

self.lstm_output_ph = tf.placeholder(
name="lstm_output",
shape=[1, self.state_variables],
dtype=tf.float32)

self.predict_step = tf.linalg.matmul(self.lstm_output_ph, tf.nn.softmax(self.weights))

self.reward_ph = tf.placeholder(
name="reward",
dtype=tf.float32)

self.previous_q_ph = tf.placeholder(
name="previous_q",
dtype=tf.float32)

self.loss = tf.losses.mean_squared_error(
labels=self.reward_ph + self.memory * self.previous_q_ph,
predictions=self.predict_step
)

optimizer = tf.train.GradientDescentOptimizer(
learning_rate=self.learning_rate,
use_locking=False,
name='SGD'
)

self.train_step = optimizer.minimize(
self.loss,
var_list=[self.weights],
gate_gradients=optimizer.GATE_OP,
aggregation_method=tf.AggregationMethod.DEFAULT,
colocate_gradients_with_ops=False,
name='GD_optimizer'
)

self.var_init = tf.global_variables_initializer()

但是,我不知道确切的问题是什么。

关于python - 构建后 session 图为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56100170/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com