Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- np.random.seed(1337)
- import keras
- from keras.models import Sequential
- from keras.layers import Dense, LSTM, TimeDistributed
- from keras.utils import np_utils
- from tqdm import tqdm
- import random
- random.seed(1)
- def generate():
- X = np.array([[random.choice([0, 1]) for _ in range(n)]
- for _ in range(batch)])
- Y = np.concatenate(([[0] * n], X[:-1]))
- X = X.reshape(batch, 1, n)
- Y = Y.reshape(batch, 1, n)
- return X, Y
- n = 1
- model = Sequential()
- model.add(LSTM(n + max(n, 5), input_shape=(1, n), return_sequences=True))
- model.add(TimeDistributed(Dense(n, activation='sigmoid')))
- model.compile(loss='binary_crossentropy', optimizer='adam',
- metrics=['binary_accuracy'])
- for i in tqdm(range(20)):
- batch = 30
- X, Y = generate()
- model.fit(X, Y, nb_epoch=30, batch_size=batch, shuffle=False, verbose=0)
- for i in range(5):
- batch = 20
- X, Y = generate()
- print(model.evaluate(X, Y, batch_size=batch, verbose=0)[1])
Add Comment
Please, Sign In to add comment