Tensorflow动态RNN(LSTM):如何格式化输入? [英] Tensorflow dynamic RNN (LSTM): how to format input?

查看:351
本文介绍了Tensorflow动态RNN(LSTM):如何格式化输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了一些这种格式的数据以及以下详细信息:

I have been given some data of this format and the following details:

person1, day1, feature1, feature2, ..., featureN, label
person1, day2, feature1, feature2, ..., featureN, label
...
person1, dayN, feature1, feature2, ..., featureN, label
person2, day1, feature1, feature2, ..., featureN, label
person2, day2, feature1, feature2, ..., featureN, label
...
person2, dayN, feature1, feature2, ..., featureN, label
...

  • 总是有相同数量的特征,但是每个特征可能是0,什么都不代表
  • 每个人都有不同的天数,例如person1有20天的数据,person2有50天的数据

    • there is always the same number of features but each feature might be a 0 representing nothing
    • there is a varying amount of days available for each person, e.g. person1 has 20 days of data, person2 has 50
    • 目标是为了预测第二天该人的标签,因此第N + 1天的标签是基于每个人还是整体(对于每个人,我).我可以自由地重新格式化数据(它不大).基于以上内容,经过阅读后,我认为动态RNN(LSTM)可能效果最好:

      The goal is to predict the label of the person the following day, so the label for dayN+1, either on a per-person basis, or overall (per-person makes more sense to me). I can freely reformat the data (it is not large). Based on the above after some reading I thought a dynamic RNN (LSTM) could work best:

      • 递归神经网络:因为第二天依赖于前一天
      • lstm:因为模型是每天建立的
      • 动态:因为并非每天都有所有功能

      如果对于我拥有的数据没有意义,请在此处停止. 问题是:

      If it does not make sense for the data I have, please stop me here. The question is then:

      如何为tensorflow/tflearn提供/格式化此数据?

      我使用tflearn查看了示例但我不了解其输入格式,因此无法对其进行镜像"操作.同样,在非常相似的问题上找到了帖子,但看起来像是海报彼此之间没有关联,因为它们在我的手中.我对tensorflow的经验仅限于其入门页面.

      I have looked at this example using tflearn but I do not understand its input format so that I can 'mirror' it to mine. Similarly, have found this post on a very similar question yet it seems like the samples the poster has are not related between each-other as they are in mine. My experience with tensorflow is limited to its get started page.

      推荐答案

      动态:因为并非每天都有所有功能

      dynamic: because not all features are present each day

      这里您对动态的概念有误. Tensorflow中的动态RNN 表示该图是在执行期间动态创建的,但是输入始终是相同的大小(0表示缺少功能应该可以).

      You've got the wrong concept of dynamic here. Dynamic RNN in Tensorflow means the graph is dynamically created during execution, but the inputs are always the same size (0 as the lack of a feature should work ok).

      无论如何,您在这里得到的是特征向量(feature1 ... featureN)的长度不同(day1 ... day?)的序列.首先,您需要一个 LSTM单元格

      Anyways, what you've got here are sequences of varying length (day1 ... day?) of feature vectors (feature1 ... featureN). First, you need a LSTM cell

      cell = tf.contrib.rnn.LSTMcell(size)
      

      ,因此您可以使用 tf.nn.dynamic_rnn创建动态展开的rnn图.从文档中:

      so you can then create a dynamically unrolled rnn graph using tf.nn.dynamic_rnn. From the docs:

      输入:RNN输入.

      inputs: The RNN inputs.

      如果time_major == False(默认值),则它必须是以下形状的张量:[batch_size,max_time,...]或此类元素的嵌套元组.

      If time_major == False (default), this must be a Tensor of shape: [batch_size, max_time, ...], or a nested tuple of such elements.

      其中max_time指输入序列长度.因为我们使用的是dynamic_rnn,所以在编译时不需要定义序列长度,因此您的输入占位符可以是:

      where max_time refers to the input sequence length. Because we're using dynamic_rnn, the sequence length doesn't need to be defined during compile time, so your input placeholder could be:

      x = tf.placeholder(tf.float32, shape=(batch_size, None, N))
      

      然后像这样馈入rnn

      Which is then fed into the rnn like

      outputs, state = tf.nn.dynamic_rnn(cell, x)
      

      表示输入数据的形状应为(batch_size, seq_length, N).如果一批示例的长度不同,则应将0矢量填充到最大长度,并将适当的sequence_length参数传递给dynamic_rnn

      Meaning your input data should have the shape (batch_size, seq_length, N). If examples in one batch have varying length, you should pad them with 0-vectors to the max length and pass the appropriate sequence_length parameter to dynamic_rnn

      显然,我已经跳过了很多细节,因此,要完全理解RNN,您应该阅读许多优秀的RNN教程之一,例如

      Obviously I've skipped a lot of details, so to fully understand RNNs you should probably read one of the many excellent RNN tutorials, like this one for example.

      这篇关于Tensorflow动态RNN(LSTM):如何格式化输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆