tensorflow static_rnn错误:输入必须是一个序列 [英] tensorflow static_rnn error: input must be a sequence

查看:77
本文介绍了tensorflow static_rnn错误:输入必须是一个序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将自己的3D数据馈送到LSTM.数据具有:高度= 365,宽度= 310,时间=未知/不一致,由0和1组成,每个产生输出的数据块都被分离到一个文件中.

I'm trying to feed my own 3D data to a LSTM. The data have: height = 365, width = 310, time = unknown / inconsistent, consist of 0 and 1, each block of data that produce an output are separated to a single file.

import tensorflow as tf
import os
from tensorflow.contrib import rnn

filename = "C:/Kuliah/EmotionRecognition/Train1/D2N2Sur.txt"

hm_epochs = 10
n_classes = 12
n_chunk = 443
n_hidden = 500

data = tf.placeholder(tf.bool, name='data')
cat = tf.placeholder("float", [None, n_classes])

weights = {
    'out': tf.Variable(tf.random_normal([n_hidden, n_classes]))
}
biases = {
    'out': tf.Variable(tf.random_normal([n_classes]))
}

def RNN(x, weights, biases):
    lstm_cell = rnn.BasicLSTMCell(n_hidden, forget_bias=1.0)
    outputs, states = rnn.static_rnn(lstm_cell, x, dtype=tf.float32)
    return tf.matmul(outputs[-1], weights['out']) + biases['out']

pred = RNN(data, weights, biases)

cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=cat))
optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost)

correct_pred = tf.equal(tf.argmax(pred, 1), tf.argmax(cat, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))

saver = tf.train.Saver()

temp = [[]]
d3 = [[]]
counter = 0
with tf.Session() as sess:
    #load
    #saver.restore(sess, "C:/Kuliah/EmotionRecognition/model.ckpt")
    sess.run(tf.global_variables_initializer())
    with open(filename) as inf:
        for line in inf:
            bla = list(line)
            bla.pop(len(bla) - 1)
            for index, item in enumerate(bla):
                if (item == '0'):
                    bla[index] = False
                else:
                    bla[index] = True
            temp.append(bla)
            counter += 1
            if counter%365==0: #height 365
                temp.pop(0)
                d3.append(temp)
                temp = [[]]
        temp.pop(0)
        d3.append(temp)

        batch_data = d3.reshape()
        sess.run(optimizer, feed_dict={data: d3, cat: 11})

        acc = sess.run(accuracy, feed_dict={data: d3, cat: 11})
        loss = sess.run(loss, feed_dict={data: d3, cat: 11})
        print(acc)
        print(loss)
        #save
        saver.save(sess, "C:/Kuliah/EmotionRecognition/model.ckpt")

这段代码给我一个错误:

this code throw me an error:

Traceback (most recent call last):
  File "C:/Kuliah/EmotionRecognition/Main", line 31, in <module>
    pred = RNN(data, weights, biases)
  File "C:/Kuliah/EmotionRecognition/Main", line 28, in RNN
    outputs, states = rnn.static_rnn(lstm_cell, x, dtype=tf.float32)
  File "C:\Users\Anonymous\AppData\Roaming\Python\Python35\site-packages\tensorflow\python\ops\rnn.py", line 1119, in static_rnn
    raise TypeError("inputs must be a sequence")
TypeError: inputs must be a sequence

推荐答案

调用pred = RNN(data, weights, biases)时,data参数的长度应为RNN的长度.但就您而言,它是data = tf.placeholder(tf.bool, name='data').

When you call pred = RNN(data, weights, biases), the data argument should be a sequence of length the length of your RNN. But in your case, it's a data = tf.placeholder(tf.bool, name='data').

您可以尝试pred = RNN([data], weights, biases).

请参见该方法的字符串文档:

See the string doc of the method:

inputs:长度为T的输入列表,每个输入为Tensor形状 [batch_size, input_size]或此类元素的嵌套元组.

inputs: A length T list of inputs, each a Tensor of shape [batch_size, input_size], or a nested tuple of such elements.

如果您的RNN长度未知,则应考虑使用 tf.nn.dynamic_rnn .

If the length of your RNN is unknow, you should consider use tf.nn.dynamic_rnn.

这篇关于tensorflow static_rnn错误:输入必须是一个序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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