【问题标题】:The Session graph is empty after building会话图在构建后为空
【发布时间】: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


    【解决方案1】:

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

    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()
    

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

    【讨论】:

      猜你喜欢
      • 2017-07-24
      • 2013-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-14
      • 1970-01-01
      • 2021-02-22
      • 1970-01-01
      相关资源
      最近更新 更多