Guest User

Pasties 2016-12-18 17:06:56 +0000

a guest
Dec 18th, 2016
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.98 KB | None | 0 0
  1. import numpy as np
  2. np.random.seed(1337)
  3.  
  4. import keras
  5. from keras.models import Sequential
  6. from keras.layers import Dense, LSTM, TimeDistributed
  7. from keras.utils import np_utils
  8. from tqdm import tqdm
  9.  
  10. import random
  11. random.seed(1)
  12.  
  13.  
  14. def generate():
  15.     X = np.array([[random.choice([0, 1]) for _ in range(n)]
  16.                   for _ in range(batch)])
  17.     Y = np.concatenate(([[0] * n], X[:-1]))
  18.     X = X.reshape(batch, 1, n)
  19.     Y = Y.reshape(batch, 1, n)
  20.     return X, Y
  21.  
  22.  
  23. n = 1
  24.  
  25. model = Sequential()
  26. model.add(LSTM(n + max(n, 5), input_shape=(1, n), return_sequences=True))
  27. model.add(TimeDistributed(Dense(n, activation='sigmoid')))
  28. model.compile(loss='binary_crossentropy', optimizer='adam',
  29.               metrics=['binary_accuracy'])
  30.  
  31.  
  32. for i in tqdm(range(20)):
  33.     batch = 30
  34.     X, Y = generate()
  35.     model.fit(X, Y, nb_epoch=30, batch_size=batch, shuffle=False, verbose=0)
  36.  
  37. for i in range(5):
  38.     batch = 20
  39.     X, Y = generate()
  40.     print(model.evaluate(X, Y, batch_size=batch, verbose=0)[1])
Add Comment
Please, Sign In to add comment