fork(1) download
  1. import numpy as np
  2. np.random.seed(123)
  3. from keras.datasets import imdb
  4. from keras.preprocessing.sequence import pad_sequences
  5. from keras.layers import *
  6. from keras.models import Sequential, Model
  7. from keras.callbacks import EarlyStopping
  8. from keras import optimizers
  9. import matplotlib.pyplot as plot
  10. import time
  11.  
  12.  
  13. MAXLEN = 500
  14. BATCHSIZE = 16
  15. EMBSIZE = 50
  16. HIDDENSIZE = 50
  17. KERNELSIZE = 5
  18. VOCABSIZE = 10000
  19.  
  20. MAXEPOCHS = 20
  21.  
  22.  
  23.  
  24. (x_train, y_train), (x_test, y_test) = imdb.load_data(path = "imdb.npz",
  25. num_words = VOCABSIZE,
  26. skip_top = 0,
  27. maxlen = MAXLEN,
  28. start_char = 1,
  29. oov_char = 2,
  30. index_from = 3)
  31.  
  32. y_train = np.expand_dims(y_train, -1)
  33. y_test = np.expand_dims(y_test, -1)
  34.  
  35.  
  36.  
  37. x_test = x_test[:1000]
  38. y_test = y_test[:1000]
  39.  
  40. print("# training samples: {}; # validation samples: {}".format(len(x_train), len(x_test)))
  41.  
  42. BATCHES_PER_EPOCH = len(x_train) // BATCHSIZE
  43.  
  44. def generator(x, y, return_positions = False):
  45. while True:
  46. for i in range(0, len(x), BATCHSIZE):
  47. x_batch = x[i:i+BATCHSIZE] # DONE
  48. print(x_batch)
  49. y_batch =y[i:i+BATCHSIZE] # DONE
  50.  
  51. yield(pad_sequences(x_batch), y_batch)
  52.  
  53. def build_gru_model():
  54. embedding_L = Embedding(input_dim = VOCABSIZE, output_dim = EMBSIZE, mask_zero = True)
  55. gru_L = Bidirectional(GRU(units = HIDDENSIZE // 2))
  56. output_L = Dense(units = 1, activation = "sigmoid")
  57. return Sequential([embedding_L, dropout_L, gru_L, dropout_L, output_L])
  58.  
  59. def build_cnn_model():
  60. #DONE
  61. embedding_L = Embedding(input_dim = VOCABSIZE, output_dim = EMBSIZE, mask_zero = False)
  62. dropout_L = Dropout(0.25)
  63. #Posmotret
  64. conv_L = Convolution1D(convolution_num_filters, convolution_filter_length, input_length=EMBSIZE,
  65. input_dim=EMBSIZE, border_mode='same', activation='tanh')(inp)
  66. maxpool_L = MaxPooling1D(pool_length=sequence_length)(conv)
  67. dropout_L = Dropout(0.25)
  68. output_L = Dense(units = 1, activation = "sigmoid")
  69. return Sequential([embedding_L, dropout_L, conv_L, maxpool_L, dropout_L, output_L])
  70.  
  71.  
  72. def build_emb_model():
  73. embedding_L = Embedding(input_dim = VOCABSIZE, output_dim = EMBSIZE, mask_zero = False)
  74. dropout_L = Dropout(0.25)
  75. avaragepool_L = AveragePooling1D(pool_length=2, stride=None, border_mode='valid')
  76. dropout_L = Dropout(0.25)
  77. output_L = Dense(units = 1, activation = "sigmoid")
  78. return Sequential([embedding_L, dropout_L, avaragepool_L , dropout_L, output_L])
  79. # Done
  80.  
  81. def train_model(model, x_train, y_train, x_test, y_test):
  82. sgd = optimizers.SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
  83. model.compile(loss='categorical_crossentropy', optimizer="sgd", metrics=['accuracy']) # Done
  84.  
  85. earlystop = EarlyStopping(monitor = "val_acc", patience = 7)
  86.  
  87. history = model.fit_generator(generator(x_train, y_train),
  88. steps_per_epoch = BATCHES_PER_EPOCH,
  89. validation_data = (pad_sequences(x_test), y_test),
  90. epochs = MAXEPOCHS, callbacks = [earlystop])
  91.  
  92. return history.history
  93.  
  94. models = {}
  95.  
  96. #uncomment all models that you have implemented:
  97. #models["gru+attn"] = build_gru_attn_model()
  98. models["gru"] = build_gru_model()
  99. #models["cnn"] = build_cnn_model()
  100. #models["emb"] = build_emb_model()
  101.  
  102. histories = {}
  103. traintimes = {}
  104.  
  105. for name in sorted(models.keys()):
  106. print("Training", name)
  107. before = time.time()
  108. histories[name] = train_model(models[name], x_train, y_train, x_test, y_test)
  109. duration = time.time() - before
  110. traintimes[name] = duration / len(histories[name]["loss"]) / BATCHES_PER_EPOCH
Runtime error #stdin #stdout #stderr 0.08s 92224KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "./prog.py", line 3, in <module>
ImportError: No module named 'keras'