关于张量流量的ptb_word_lm.py中函数run_epoch的一些问题

时间:2017-10-04 07:54:11

标签: python tensorflow rnn

tensorflow rnn ptb language model tuorial https://github.com/tensorflow/models/tree/master/tutorials/rnn/ptb

我对ptb_word_lm.py中的 run_epoch 函数有两个问题,只有cpu device

for step in range(model.input.epoch_size):
feed_dict = {}
for i, (c, h) in enumerate(model.initial_state):
  feed_dict[c] = state[i].c
  feed_dict[h] = state[i].h

vals = session.run(fetches, feed_dict)
cost = vals["cost"]
state = vals["final_state"]

Qustion1:为什么这里需要创建一个feed_dict来sess,我认为在类PTBmodel中,它已经为lstm网络创建了初始状态

cell = tf.contrib.rnn.MultiRNNCell(
    [cell for _ in range(config.num_layers)], state_is_tuple=True)
self._initial_state = cell.zero_state(config.batch_size, data_type())
state = self._initial_state

outputs = []
with tf.variable_scope("RNN"):
  for time_step in range(self.num_steps):
    if time_step > 0: tf.get_variable_scope().reuse_variables()
    (cell_output, state) = cell(inputs[:, time_step, :], state)
    outputs.append(cell_output)

Qustion2:为什么sessrion.run(fetches,feed_dict)可以在这里返回值,但是,我在测试代码中尝试这个,它返回None

import tensorflow as tf

# Model parameters
W = tf.Variable([.3], dtype=tf.float32)
b = tf.Variable([-.3], dtype=tf.float32)
# Model input and output
x = tf.placeholder(tf.float32)
linear_model = W * x + b
y = tf.placeholder(tf.float32)

# loss
loss = tf.reduce_sum(tf.square(linear_model - y)) # sum of the squares
# optimizer
optimizer = tf.train.GradientDescentOptimizer(0.01)
# train = optimizer.minimize(loss)

# training data
x_train = [1, 2, 3, 4]
y_train = [0, -1, -2, -3]
# training loop
init = tf.global_variables_initializer()

tvars = tf.trainable_variables()
grads = tf.gradients(loss, tvars)
train = optimizer.apply_gradients(
            zip(grads, tvars))
sess = tf.Session()
sess.run(init) # reset values to wrong
for i in range(100):
  print sess.run(train, {x: x_train, y: y_train})

它只是打印

None
None
None
..
..
谢谢你!

0 个答案:

没有答案