在训练RNN时,张量流损失是nan

时间:2016-12-30 01:17:37

标签: python tensorflow

使用单个GRU单元运行RNN,我遇到了下面的堆栈跟踪的情况

Traceback (most recent call last):
  File "language_model_test.py", line 15, in <module>
    test_model()
  File "language_model_test.py", line 12, in test_model
    model.train(random_data, s)
  File "/home/language_model/language_model.py", line 120, in train
    train_pp = self._run_epoch(data, sess, inputs, rnn_ouputs, loss, trainOp, verbose)
  File "/home/language_model/language_model.py", line 92, in _run_epoch
    loss, _= sess.run([loss, trainOp], feed_dict=feed)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 767, in run
    run_metadata_ptr)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 952, in _run
    fetch_handler = _FetchHandler(self._graph, fetches, feed_dict_string)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 408, in __init__
    self._fetch_mapper = _FetchMapper.for_fetch(fetches)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 230, in for_fetch
    return _ListFetchMapper(fetch)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 337, in __init__
    self._mappers = [_FetchMapper.for_fetch(fetch) for fetch in fetches]
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 238, in for_fetch
    return _ElementFetchMapper(fetches, contraction_fn)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 271, in __init__
    % (fetch, type(fetch), str(e)))
TypeError: Fetch argument nan has invalid type <type 'numpy.float32'>, must be a string or Tensor. (Can not convert a float32 into a Tensor or Operation.)

计算损失的步骤似乎是问题

def train(self,data, session=tf.Session(), verbose=10):

        print "initializing model"
        self._add_placeholders()
        inputs = self._add_embedding()
        rnn_ouputs, _ = self._run_rnn(inputs)
        outputs = self._projection_layer(rnn_ouputs)
        loss = self._compute_loss(outputs)
        trainOp = self._add_train_step(loss)
        start = tf.initialize_all_variables()
        saver = tf.train.Saver()

        with session as sess:
            sess.run(start)

            for epoch in xrange(self._max_epochs):
                train_pp = self._run_epoch(data, sess, inputs, rnn_ouputs, loss, trainOp, verbose)
                print "Training preplexity for batch {} - {}".format(epoch, train_pp)

以下是_run_epoch

的代码

任何有损失的地方都会返回nan

def _run_epoch(self, data, session, inputs, rnn_ouputs, loss, trainOp, verbose=10):
    with session.as_default() as sess:
        total_steps = sum(1 for x in data_iterator(data, self._batch_size, self._max_steps))
        train_loss = []
        for step, (x,y, l) in enumerate(data_iterator(data, self._batch_size, self._max_steps)):
            print "step - {0}".format(step)
            feed = {
                self.input_placeholder: x,
                self.label_placeholder: y,
                self.sequence_length: l,
                self._dropout_placeholder: self._dropout,
            }
            loss, _= sess.run([loss, trainOp], feed_dict=feed)
            print "loss - {0}".format(loss)
            train_loss.append(loss)
            if verbose and step % verbose == 0:
                sys.stdout.write('\r{} / {} : pp = {}'. format(step, total_steps, np.exp(np.mean(train_loss))))
                sys.stdout.flush()
            if verbose:
                sys.stdout.write('\r')

        return np.exp(np.mean(train_loss))

当我通过使用以下数据测试我的代码时,会出现这种情况 random_data = np.random.normal(0, 100, size=[42068, 46])旨在模仿使用单词ID作为输入传递。我的其余代码可以在以下gist

中找到

编辑以下是我在出现此问题时运行测试套件的方式:

def test_model():
    model = Language_model(vocab=range(0,101))
    s = tf.Session()
    #1 more than step size to acoomodate for the <eos> token at the end
    random_data = np.random.normal(0, 100, size=[42068, 46])
    # file = "./data/ptb.test.txt"
    print "Fitting started"
    model.train(random_data, s)

if __name__ == "__main__":
    test_model() 

如果我将random_data替换为其他语言模型,他们也会输出nan作为费用。我的理解是,通过传入feed-dict,tensorflow应该采用数值并检索与id对应的适当嵌入向量,我不明白为什么random_data导致{{1其他型号的其他型号。

1 个答案:

答案 0 :(得分:0)

上面的代码有几个问题

让我们从这一行开始

C = sum([A;B],'omitnan')

random_data = np.random.normal(0, 100, size=[42068, 46]) 不会产生不同的值,而是产生浮动值,让我们尝试上面的示例,但是具有可管理的大小。

np.random.normal(...)

机器学习算法无法学习那些,因为这些算法是嵌入模型的输入,并且我们已经得到负值和浮点值。

实际需要的是以下代码:

>>> np.random.normal(0, 100, size=[5])
array([-53.12407229,  39.57335574, -98.25406749,  90.81471139, -41.05069646])

检查其输出我们得到

random_data = np.random.randint(0, 101, size=...)

接下来,以下行实际上是在创建一个微妙的问题。

>>> np.random.randint(0, 100, size=[5])
array([27, 47, 33, 12, 24])

def _run_epoch(self, data, session, inputs, rnn_ouputs, loss, train, verbose=10): with session.as_default() as sess: total_steps = sum(1 for x in data_iterator(data, self._batch_size, self._max_steps)) train_loss = [] for step, (x,y, l) in enumerate(data_iterator(data, self._batch_size, self._max_steps)): print "step - {0}".format(step) feed = { self.input_placeholder: x, self.label_placeholder: y, self.sequence_length: l, self._dropout_placeholder: self._dropout, } loss, _= sess.run([loss, train], feed_dict=feed) print "loss - {0}".format(loss) train_loss.append(loss) if verbose and step % verbose == 0: sys.stdout.write('\r{} / {} : pp = {}'. format(step, total_steps, np.exp(np.mean(train_loss)))) sys.stdout.flush() if verbose: sys.stdout.write('\r') return np.exp(np.mean(train_loss)) 既是参数参数又是变量,因此第一次运行时,它将不再是张量,因此我们无法在会话中实际调用它。