Understanding the lstm input form in kerams with different sequences

I am very new to keras as well as python. I have a set of time series with different sequence lengths (for example, the 1st sequence is 484000x128, the second is 563110x128, etc.). I put the sequences in a 3D array.

My question is how to determine the input form because I am confused. I used DL4J, but the concept is different than defining a 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..
+4
source share
1 answer

Here are the docs for LSTM input forms:

Input forms

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

, .

- , - keras padding

:

# 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()).

+3

Source: https://habr.com/ru/post/1674104/


All Articles