了解不同顺序的喀拉斯语中的lstm输入形状 [英] Understanding lstm input shape in keras with different sequence

查看:74
本文介绍了了解不同顺序的喀拉斯语中的lstm输入形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对keras和python都是新手. 我有一个具有不同序列长度的时间序列数据集(例如,第一个序列是484000x128,第二个序列是563110x128,依此类推) 我已将序列放入3D数组中.

I'm very new to keras and also to python. I have a time series dataset with different sequence lengths (for example 1st sequence is 484000x128, 2nd sequence is 563110x128, etc) I've put the sequences in 3D array.

我的问题是如何定义输入形状,因为我很困惑.我使用的是DL4J,但是在定义网络配置方面概念有所不同.

My question is how to define the input shape, because I'm confused. I was using DL4J but the concept is different in defining the network configuration.

这是我的第一个试用代码:

Here is my first trial code:

import numpy as np
from keras.models import Sequential
from keras.layers import Embedding,LSTM,Dense,Dropout


## Loading dummy data
sequences = np.array([[[1,2,3],[1,2,3]], [[4,5,6],[4,5,6],[4,5,6]]])
y = np.array([[[0],[0]], [[1],[1],[1]]])
x_test=np.array([[2,3,2],[4,6,7],[1,2,1]])
y_test=np.array([0,1,1])

n_epochs=40

# model configration
model = Sequential()
model.add(LSTM(100, input_shape=(3,1), activation='tanh', recurrent_activation='hard_sigmoid')) # 100 num of LSTM units
model.add(LSTM(100, activation='tanh', recurrent_activation='hard_sigmoid'))
model.add(Dense(1, activation='softmax'))
model.compile(loss='binary_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])

print(model.summary())

## training with batches of size 1 (each batch is a sequence)
for epoch in range(n_epochs):
    for seq, label in zip(sequences, y):
        model.train(np.array([seq]), [label]) # train a batch at a time..
        scores=model.evaluate(x_test, y_test) # evaluate batch at a time..

推荐答案

以下是有关LSTM输入形状的文档:

Here is the docs on input shapes for LSTMs:

输入形状

具有形状(batch_size,timesteps,input_dim)的3D张量,(可选)2D 形状为(batch_size,output_dim)的张量.

3D tensor with shape (batch_size, timesteps, input_dim), (Optional) 2D tensors with shape (batch_size, output_dim).

这意味着您将需要每个批次的大小不变的时间步长.

Which implies that you you're going to need timesteps with a constant size for each batch.

典型的做法是使用 keras的填充实用程序

然后您可以尝试:

# let say timestep you choose: is 700000 and dimension of the vectors are 128

timestep = 700000
dims = 128 

model.add(LSTM(100, input_shape=(timestep, dim),
         activation='tanh', recurrent_activation='hard_sigmoid'))

我编辑了答案,删除了batch_size参数.通过此设置,批次大小未指定,您可以在拟合模型时进行设置(在model.fit()中).

I edited the answer to remove the batch_size argument. With this setup the batch size is unspecified, you could set that when you fitting the model (in model.fit()).

这篇关于了解不同顺序的喀拉斯语中的lstm输入形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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