Python LSTM单元上的Tensorflow LSTM矩阵乘法

Python LSTM单元上的Tensorflow LSTM矩阵乘法,python,tensorflow,lstm,Python,Tensorflow,Lstm,我正在用Tensorflow制作一个LSTM神经网络 输入张量大小为92 import tensorflow as tf from tensorflow.contrib import rnn import data test_x, train_x, test_y, train_y = data.get() # Parameters learning_rate = 0.001 epochs = 100 batch_size = 64 display_step = 10 # Network P

我正在用Tensorflow制作一个LSTM神经网络

输入张量大小为92

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

test_x, train_x, test_y, train_y = data.get()

# Parameters
learning_rate = 0.001
epochs = 100
batch_size = 64
display_step = 10

# Network Parameters
n_input = 28   # input size
n_hidden = 128 # number of hidden layers
n_classes = 20 # output size

# Placeholders
x = tf.placeholder(dtype=tf.float32, shape=[None, n_input])
y = tf.placeholder(dtype=tf.float32, shape=[None, n_classes])

# Network
def LSTM(x):
    W = tf.Variable(tf.random_normal([n_hidden, n_classes]), dtype=tf.float32) # weights
    b = tf.Variable(tf.random_normal([n_classes]), dtype=tf.float32) # biases

    x_shape = 92

    x = tf.transpose(x)
    x = tf.reshape(x, [-1, n_input])
    x = tf.split(x, x_shape)

    lstm = rnn.BasicLSTMCell(
        num_units=n_hidden,
        forget_bias=1.0
    )
    outputs, states = rnn.static_rnn(
        cell=lstm,
        inputs=x,
        dtype=tf.float32
    )

    output = tf.matmul( outputs[-1], W ) + b

    return output

# Train Network
def train(x):
    prediction = LSTM(x)

    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        output = sess.run(prediction, feed_dict={"x": train_x})
        print(output)

train(x)
我没有得到任何错误,但我输入了一个大小为92的输入张量,LSTM函数中的矩阵乘法返回一个包含一个结果向量的列表,当所需的量为92时,每个输入一个结果向量

问题是我只对输出数组中的最后一项进行矩阵相乘吗?像这样:

output = tf.matmul( outputs[-1], W ) + b
而不是:

output = tf.matmul( outputs, W ) + b
这是我在执行后一种操作时遇到的错误:

ValueError: Shape must be rank 2 but is rank 3 for 'MatMul' (op: 'MatMul') with input shapes: [92,?,128], [128,20].

静态神经网络,用于构造最简单的递归神经网络。因此,它的输入应该是张量序列。假设你想输入4个单词,分别叫“嗨”,“你好”,“是吗”,“你”。因此,您的输入占位符应该由对应于每个单词的四个n(每个输入向量的大小)维向量组成

我想你的占位符有问题。您应该使用RNN的输入数对其进行初始化。28是每个向量中的维数。我相信92是序列的长度。(更像92 lstm单元)

在输出列表中,您将获得一组向量,这些向量等于序列的长度,每个向量的大小等于隐藏单元的数量