问题描述
我很困惑如何精确地编码数据序列作为LSTM RNN的输入.
I am confused how exactly to encode a sequence of data as an input to an LSTM RNN.
在普通DNN中,每个标签都有一个输入.RNN中的输入"是什么?为了训练与标签相关的顺序事件,它不必是一组数据(或序列)吗?
In a vanilla DNN, there is an input for every label. What is the "input" in an RNN? Doesnt it have to be a set (or sequence) of data, in order to train sequential events associated with a label?
我对如何编码顺序信息感到困惑,因为似乎给定标签关联的输入应该不止一个.
Im confused how to encode sequential information, because it seems that there should be more than a single input associated with a given label.
推荐答案
让我们用代码绘制一个示例.
Let's draw up an example in code.
假设我们有一些句子,其中句子中的每个单词都被编码为向量(也许来自word2vec的向量).
Say we have some sentences where each word in the sentence is encoded as a vector (vectors from word2vec maybe).
假设我们要将每个句子分为两个类别(0、1)之一.我们可以像这样构建一个简单的分类器:
Suppose we want to classify each sentence into one of two class (0, 1). We might build a simple classifier like so:
import numpy as np
from keras.models import Sequential
from keras.layers import LSTM, Dense
# each example (of which we have a 100) is a sequence of 10 words and
# each words is encoded as 16 element vectors
X = np.random.rand(100, 10, 16)
y = np.random.choice(1, 100)
model = Sequential()
model.add(LSTM(128, input_shape=(10, 16)))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='sgd')
# fit model
model.fit(X, y, epochs=3, batch=16)
这篇关于RNN的输入数据格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!