def test_model(model, sequence_length=None, verboose=False): input_dim = model.input_dim output_dim = model.output_dim batch_size = model.batch_size I, V, sw = next(get_sample(batch_size=batch_size, in_bits=input_dim, out_bits=output_dim, max_size=sequence_length, min_size=sequence_length)) Y = np.asarray(model.predict(I, batch_size=batch_size)) if not np.isnan(Y.sum()): #checks for a NaN anywhere Y = (Y > 0.5).astype('float64') x = V[:, -sequence_length:, :] == Y[:, -sequence_length:, :] acc = x.mean() * 100 if verboose: print("the overall accuracy for sequence_length {0} was: {1}".format(sequence_length, x.mean())) print("per bit") print(x.mean(axis=(0,1))) print("per timeslot") print(x.mean(axis=(0,2))) else: ntm = model.layers[0] weights = ntm.get_weights() import pudb; pu.db acc = 0 return acc
def train_model(model, epochs=10, min_size=5, max_size=20, callbacks=None, verboose=False): input_dim = model.input_dim output_dim = model.output_dim batch_size = model.batch_size sample_generator = get_sample(batch_size=batch_size, in_bits=input_dim, out_bits=output_dim, max_size=max_size, min_size=min_size) if verboose: for j in range(epochs): model.fit_generator(sample_generator, steps_per_epoch=10, epochs=j + 1, callbacks=callbacks, initial_epoch=j) print("currently at epoch {0}".format(j + 1)) for i in [5, 10, 20, 40]: test_model(model, sequence_length=i, verboose=True) else: model.fit_generator(sample_generator, steps_per_epoch=10, epochs=epochs, callbacks=callbacks) print("done training")