我试图调和我对LSTM的理解,并在Keras实施的this post by Christopher Olah篇文章中指出.我正在学习Keras教程的blog written by Jason Brownlee.我最困惑的是,

  1. 将数据系列reshape 为[samples, time steps, features]和,
  2. 有状态的LSTM

让我们参考下面粘贴的代码,集中讨论上述两个问题:

# reshape into X=t and Y=t+1
look_back = 3
trainX, trainY = create_dataset(train, look_back)
testX, testY = create_dataset(test, look_back)

# reshape input to be [samples, time steps, features]
trainX = numpy.reshape(trainX, (trainX.shape[0], look_back, 1))
testX = numpy.reshape(testX, (testX.shape[0], look_back, 1))
########################
# The IMPORTANT BIT
##########################
# create and fit the LSTM network
batch_size = 1
model = Sequential()
model.add(LSTM(4, batch_input_shape=(batch_size, look_back, 1), stateful=True))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
for i in range(100):
    model.fit(trainX, trainY, nb_epoch=1, batch_size=batch_size, verbose=2, shuffle=False)
    model.reset_states()

注意:create_dataset接受长度为N的序列,并返回一个N-look_back数组,其中每个元素都是长度为look_back的序列.

What is Time Steps and Features?

As can be seen TrainX is a 3-D array with Time_steps and Feature being the last two dimensions respectively (3 and 1 in this particular code). With respect to the image below, does this mean that we are considering the many to one case, where the number of pink boxes are 3? Or does it literally mean the chain length is 3 (i.e. only 3 green boxes considered). enter image description here

当我们考虑多元序列时,特征论据会变得相关吗?例如,同时为两只金融股建模?

Stateful LSTMs

有状态LSTM是否意味着在批处理运行之间保存单元内存值?如果是这样的话,batch_size是一,并且在两次训练之间内存被重置,那么说它是有状态的又有什么意义呢.我猜这与训练数据没有被洗牌有关,但我不确定如何洗牌.

有什么 idea 吗?

编辑1:

A bit confused about @van's comment about the red and green boxes being equal. So just to confirm, does the following API calls correspond to the unrolled diagrams? Especially noting the second diagram (batch_size was arbitrarily chosen.): enter image description here enter image description here

编辑2:

对于那些已经完成Udacity深度学习课程但仍然对时间步长争论感到困惑的人,请看以下讨论:https://discussions.udacity.com/t/rnn-lstm-use-implementation/163169

更新:

原来model.add(TimeDistributed(Dense(vocab_len)))就是我要找的.这里有一个例子:https://github.com/sachinruk/ShakespeareBot

更新2:

我在这里总结了我对LSTM的大部分理解:https://www.youtube.com/watch?v=ywinX5wgdEU

推荐答案

首先,你 Select 优秀的教程(12)开始.

What Time-step means:Time-steps==3在X.shape(描述数据形状)中表示有三个粉色框.由于在Keras中,每个步骤都需要输入,因此绿色框的数量通常应等于红色框的数量.除非你破解这个 struct .

many to many vs. many to one:在keras中,初始化LSTMGRUSimpleRNN时有一个return_sequences参数.当return_sequencesFalse时(默认情况下),则为many to one,如图所示.它的返回形状是(batch_size, hidden_unit_length),代表最后一个状态.当return_sequencesTrue,那么它就是many to many.它的返回形状是(batch_size, time_step, hidden_unit_length)

Does the features argument become relevant:特征参数的意思是"How big is your red box"或每个步骤的输入维度是多少.如果你想根据8种市场信息进行预测,那么你可以用feature==8种数据生成数据.

Stateful:你可以查到the source code.初始化状态时,如果为stateful==True,则将上次训练的状态用作初始状态,否则将生成新状态.我还没有打开stateful.但是,我不同意batch_sizestateful==True的时候只能是1的说法.

目前,您使用收集的数据生成数据.想象一下,你的股票信息是以流的形式出现的,而不是等待一天来收集所有连续的数据,你希望在使用网络进行训练/预测时生成输入数据online.如果你有400只股票共享同一个网络,那么你可以设置batch_size==400只.

Python相关问答推荐

将行从一个DF添加到另一个DF

使用多个性能指标执行循环特征消除

如何用symy更新分段函数

无法使用equals_html从网址获取全文

如何在具有重复数据的pandas中对groupby进行总和,同时保留其他列

_repr_html_实现自定义__getattr_时未显示

如何使用根据其他值相似的列从列表中获取的中间值填充空NaN数据

Godot:需要碰撞的对象的AdditionerBody2D或Area2D以及queue_free?

将输入聚合到统一词典中

使用Python更新字典中的值

joblib:无法从父目录的另一个子文件夹加载转储模型

可以bcrypts AES—256 GCM加密损坏ZIP文件吗?

使用BeautifulSoup抓取所有链接

处理具有多个独立头的CSV文件

Odoo16:模板中使用的docs变量在哪里定义?

使用类型提示进行类型转换

将CSS链接到HTML文件的问题

使用python playwright从 Select 子菜单中 Select 值

Python:从目录内的文件导入目录

在round函数中使用列值