我尝试构建图然后运行它,但是我仍然得到
RuntimeError:“会话”图为空。向图形添加操作 在调用run()之前。
此图形内置的功能是
W/System.err: org.json.JSONException:
然后,我尝试使用此图运行会话,如下所示:
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
知道我的图表为何仍为空吗?调试 init_network 方法表明,即使在方法结束时, self.graph 变量仍为空(其 _graph_key 变量仍设置为)。 'grap-key-0 /')。
答案 0 :(得分: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()
但是,我不知道确切的问题是什么。