Advertisement
jack06215

[keras] Variable time-series training by batches

Sep 26th, 2020
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.93 KB | None | 0 0
  1. from keras.models import Sequential
  2. from keras.layers import LSTM, Dense, TimeDistributed
  3. from keras.utils import to_categorical
  4. import numpy as np
  5.  
  6. model = Sequential()
  7.  
  8. model.add(LSTM(32, return_sequences=True, input_shape=(None, 5)))
  9. model.add(LSTM(8, return_sequences=True))
  10. model.add(TimeDistributed(Dense(2, activation='sigmoid')))
  11.  
  12. print(model.summary(90))
  13.  
  14. model.compile(loss='categorical_crossentropy',
  15.               optimizer='adam')
  16.  
  17. def train_generator():
  18.     while True:
  19.         sequence_length = np.random.randint(10, 100)
  20.         x_train = np.random.random((1000, sequence_length, 5))
  21.         # y_train will depend on past 5 timesteps of x
  22.         y_train = x_train[:, :, 0]
  23.         for i in range(1, 5):
  24.             y_train[:, i:] += x_train[:, :-i, i]
  25.         y_train = to_categorical(y_train > 2.5)
  26.         yield x_train, y_train
  27.  
  28. model.fit_generator(train_generator(), steps_per_epoch=30, epochs=10, verbose=1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement