Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
1,276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.15 KB | None | 0 0
  1. from tensorflow.keras.layers import Input, Dense, LSTM, Bidirectional, Conv1D
  2. from tensorflow.keras.layers import Flatten, Dropout
  3. from tensorflow.keras.models import Model
  4. from tensorflow.keras.optimizers import Adam
  5. import numpy as np
  6. from time import time
  7.  
  8. def timeit(func, iterations, *args):
  9.     t0 = time()
  10.     for _ in range(iterations):
  11.         print('.')
  12.         func(*args)
  13.     print("%.4f sec" % (time() - t0))
  14.  
  15. batch_size = 32
  16. batch_shape = (batch_size, 400, 16)
  17. ipt   = Input(batch_shape=batch_shape)
  18. x     = Bidirectional(LSTM(512, activation='relu', return_sequences=True))(ipt)
  19. x     = LSTM(512, activation='relu', return_sequences=True)(ipt)
  20. x     = Conv1D(128, 400, strides=1, padding='same')(x)
  21. x     = Flatten()(x)
  22. x     = Dense(256, activation='relu')(x)
  23. x     = Dropout(0.5)(x)
  24. x     = Dense(128, activation='relu')(x)
  25. x     = Dense(64,  activation='relu')(x)
  26. out   = Dense(1,  activation='sigmoid')(x)
  27. model = Model(ipt, out)
  28.  
  29. X = np.random.randn(*batch_shape)
  30. Y = np.random.randint(0, 2, (batch_size, 1))
  31.  
  32. model.compile(Adam(lr=1e-4), 'binary_crossentropy')
  33.  
  34. model.train_on_batch(X, Y)
  35. timeit(model.train_on_batch, 4, X, Y)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement