SHARE
TWEET

Untitled

a guest Jun 19th, 2017 40 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from keras import layers as L
  2. from keras import backend as K
  3. from keras.models import Model, Sequential
  4. import numpy as np
  5.  
  6. n_samples = 20
  7. n_rows = 10
  8. n_columns = 15
  9. n_outputs = 13
  10.  
  11. X = np.zeros((1000, n_samples, n_rows, n_columns))
  12. y = np.zeros((1000, n_samples * n_outputs))
  13.  
  14. ####################
  15. # sequential version
  16. input_ = L.Input(shape=(n_samples, n_rows, n_columns), dtype='float32')
  17.  
  18. sequential_model = Sequential([
  19.     L.Dense(input_shape=(n_rows, n_columns), units=100),
  20.     #vvvvvvvvvvvvvvvvvvvvvv
  21.     L.BatchNormalization(), # TODO: Commenting out this line will make it work
  22.     #^^^^^^^^^^^^^^^^^^^^^^
  23.     L.Flatten(),
  24.     L.Dense(units=n_outputs)])
  25.  
  26. distributed = L.TimeDistributed(sequential_model)(input_)
  27. distributed = L.Reshape((-1,))(distributed)
  28.  
  29. # compile
  30. model = Model(inputs=[input_], outputs=[distributed])
  31. model.compile(optimizer='adagrad', loss='mean_squared_error')
  32.  
  33. #fit
  34. model.fit([X], [y], batch_size=200, epochs=7)
  35.  
  36. ####################
  37. # functional version
  38. input_ = L.Input(shape=(n_samples, n_rows, n_columns), dtype='float32')
  39. simple_input = L.Input(shape=(n_rows, n_columns), dtype='float32')
  40.  
  41. x = L.Dense(input_shape=(n_rows, n_columns), units=100)(simple_input)
  42. #vvvvvvvvvvvvvvvvvvvvvv
  43. x = L.BatchNormalization()(x) # TODO: Commenting out this line will make it work
  44. #^^^^^^^^^^^^^^^^^^^^^^
  45. x = L.Flatten()(x)
  46. output = L.Dense(units=n_outputs)(x)
  47.  
  48. functional_model = Model(simple_input, output)
  49.  
  50. distributed = L.TimeDistributed(functional_model)(input_)
  51. distributed = L.Reshape((-1,))(distributed)
  52.  
  53. # compile
  54. model = Model(inputs=[input_], outputs=[distributed])
  55. model.compile(optimizer='adagrad', loss='mean_squared_error')
  56.  
  57. #fit
  58. model.fit([X], [y], batch_size=200, epochs=7)
RAW Paste Data
Top