from keras.models import Sequential
from keras.layers import Dense, Masking, LSTM, TimeDistributed, Activation
import numpy as np

#RNNs untuk single output
#sering digunakan untuk klasifikasi sequence data

X = np.array([[[1.,1.,0.,0.],[1.,1.,0.,0.],[1.,1.,0.,0.],[1.,1.,0.,0.]],\
              [[1.,1.,0.,0.],[1.,1.,0.,0.],[1.,1.,0.,0.],[1.,1.,0.,0.]],\
              [[1.,1.,0.,0.],[1.,1.,0.,0.],[1.,1.,0.,0.],[1.,1.,0.,0.]],\
              [[0.,0.,1.,1.],[0.,0.,1.,1.],[0.,0.,1.,1.],[0.,0.,1.,1.]],\
              [[0.,0.,1.,1.],[0.,0.,1.,1.],[0.,0.,1.,1.],[0.,0.,1.,1.]],\
              [[0.,0.,1.,1.],[0.,0.,1.,1.],[0.,0.,1.,1.],[0.,0.,1.,1.]],\
              [[1.,1.,0.,0.],[1.,1.,0.,0.],[1.,1.,0.,0.],[1.,1.,0.,0.]],\
              [[1.,1.,0.,0.],[1.,1.,0.,0.],[1.,1.,0.,0.],[1.,1.,0.,0.]],\
              [[0.,0.,1.,1.],[0.,0.,1.,1.],[0.,0.,1.,1.],[0.,0.,1.,1.]],\
              [[0.,0.,1.,1.],[0.,0.,1.,1.],[0.,0.,1.,1.],[0.,0.,1.,1.]]])

y = np.array([[1.,0.],
              [1.,0.],
              [1.,0.],
              [0.,1.],
              [0.,1.],
              [0.,1.],
              [1.,0.],
              [1.,0.],
              [0.,1.],
              [0.,1.]])

x_train, x_test = (X[:6], X[6:])
y_train, y_test = (y[:6], y[6:])

model = Sequential()

timesteps = 4
features = 4

# LSTM layer 1
# input_dim = features
# input_length = timesteps
model.add(LSTM(output_dim=3, 
               input_shape=(timesteps, features), 
               return_sequences=True))

# LSTM layer 2
# otomatis, input_shape
model.add(LSTM(output_dim=3,
               return_sequences=False))

# put Dense layer after LSTM's output
# otomatis, input_dim = output_dim LSTM = 3
# output shape = (num_sample, 2)
model.add(Dense(output_dim=2))
model.add(Activation('softmax'))

model.compile(loss='categorical_crossentropy', 
              optimizer='rmsprop', 
              metrics=['accuracy'])

model.fit(x_train, y_train, nb_epoch=200, batch_size=4)

score = model.evaluate(x_test, y_test, batch_size=4)

#predict classes given unseen data
classes = model.predict_classes(x_test, batch_size=4)
print(classes)
#most likely
#[0 0 1 1]

#show the probability scores
probs = model.predict_proba(x_test, batch_size=4)
print(probs)
#di salah satu kasus running
#[[ 0.82198185  0.17801815]
# [ 0.82198185  0.17801815]
# [ 0.18739352  0.81260651]
# [ 0.18739352  0.81260651]]
