Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. X_train.shape = (243*100*4) # Samples * Time steps * Features
  2. Y_train.shape = (243,) # either 0 or 1 for each samples
  3.  
  4. X_validate.shape : (31, 100, 4) # Samples * Time steps * Features
  5. Y_validate.shape : (31,) # either 0 or 1 for each samples
  6.  
  7. X_test.shape : (28, 100, 4) # Samples * Time steps * Features
  8. Y_test.shape : (28,) # either 0 or 1 for each samples
  9.  
  10. 1. Train the model with random time length batches
  11. 2. Predict the class, if random time length batches provided as input to the model
  12.  
  13. input_ = Input(shape=(None,4))
  14. x = LSTM(16, return_sequences=True)(input_)
  15. x = LSTM(8, return_sequences=True)(x)
  16. output = TimeDistributed(Dense(2, activation='sigmoid'))(x)
  17.  
  18. # Model
  19. model = Model(inputs=input_, outputs=output)
  20.  
  21. print(model.summary())
  22.  
  23. # Compile
  24. model.compile(
  25. loss='binary_crossentropy',
  26. optimizer=Adam(lr=1e-4),
  27. metrics=['accuracy']
  28. )
  29.  
  30.  
  31. def common_generator(X, Y):
  32. while True:
  33. sequence_length = random.randrange(60,100,5)
  34. # I want my model to be trained with random time length b/w 50 to 100 with multiples of 5
  35.  
  36. x_train = X[:, :sequence_length, :]
  37.  
  38. y = to_categorical(Y)
  39. y_train = np.repeat(y[:, np.newaxis], sequence_length, axis=1)
  40.  
  41. # For my convenience i changed my Y_train shape from (243,) to (243, sequence_length, 2)
  42. Refer picture below for better understanding
  43.  
  44. yield (x_train, y_train)
  45.  
  46. trainGen = common_generator(X_train,Y_train)
  47. ValGen = common_generator(X_validate, Y_validate)
  48.  
  49. H = model.fit_generator(trainGen, steps_per_epoch=25, validation_data=ValGen, validation_steps=3, epochs=150)
  50.  
  51. Epoch 150/150
  52. 25/25 [==============================] - 5s 187ms/step - loss: 0.3706 - acc: 0.8574 - val_loss: 0.3254 - val_acc: 0.8733
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement