def test_nested_sequential(in_tmpdir): (x_train, y_train), (x_test, y_test) = _get_test_data() inner = Sequential() inner.add(Dense(num_hidden, input_shape=(input_dim,))) inner.add(Activation('relu')) inner.add(Dense(num_class)) middle = Sequential() middle.add(inner) model = Sequential() model.add(middle) model.add(Activation('softmax')) model.compile(loss='categorical_crossentropy', optimizer='rmsprop') model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=1, validation_data=(x_test, y_test)) model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=2, validation_split=0.1) model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=0) model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=1, shuffle=False) model.train_on_batch(x_train[:32], y_train[:32]) loss = model.evaluate(x_test, y_test, verbose=0) model.predict(x_test, verbose=0) model.predict_classes(x_test, verbose=0) model.predict_proba(x_test, verbose=0) fname = 'test_nested_sequential_temp.h5' model.save_weights(fname, overwrite=True) inner = Sequential() inner.add(Dense(num_hidden, input_shape=(input_dim,))) inner.add(Activation('relu')) inner.add(Dense(num_class)) middle = Sequential() middle.add(inner) model = Sequential() model.add(middle) model.add(Activation('softmax')) model.compile(loss='categorical_crossentropy', optimizer='rmsprop') model.load_weights(fname) os.remove(fname) nloss = model.evaluate(x_test, y_test, verbose=0) assert(loss == nloss) # test serialization config = model.get_config() Sequential.from_config(config) model.summary() json_str = model.to_json() model_from_json(json_str) yaml_str = model.to_yaml() model_from_yaml(yaml_str)
def test_nested_sequential(): (X_train, y_train), (X_test, y_test) = _get_test_data() inner = Sequential() inner.add(Dense(nb_hidden, input_shape=(input_dim,))) inner.add(Activation("relu")) inner.add(Dense(nb_class)) middle = Sequential() middle.add(inner) model = Sequential() model.add(middle) model.add(Activation("softmax")) model.compile(loss="categorical_crossentropy", optimizer="rmsprop") model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=1, validation_data=(X_test, y_test)) model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=2, validation_split=0.1) model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=0) model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=1, shuffle=False) model.train_on_batch(X_train[:32], y_train[:32]) loss = model.evaluate(X_test, y_test, verbose=0) model.predict(X_test, verbose=0) model.predict_classes(X_test, verbose=0) model.predict_proba(X_test, verbose=0) fname = "test_nested_sequential_temp.h5" model.save_weights(fname, overwrite=True) inner = Sequential() inner.add(Dense(nb_hidden, input_shape=(input_dim,))) inner.add(Activation("relu")) inner.add(Dense(nb_class)) middle = Sequential() middle.add(inner) model = Sequential() model.add(middle) model.add(Activation("softmax")) model.compile(loss="categorical_crossentropy", optimizer="rmsprop") model.load_weights(fname) os.remove(fname) nloss = model.evaluate(X_test, y_test, verbose=0) assert loss == nloss # test serialization config = model.get_config() new_model = Sequential.from_config(config) model.summary() json_str = model.to_json() new_model = model_from_json(json_str) yaml_str = model.to_yaml() new_model = model_from_yaml(yaml_str)
def test_recursive(): # test layer-like API graph = Graph() graph.add_input(name='input1', input_shape=(32,)) graph.add_node(Dense(16), name='dense1', input='input1') graph.add_node(Dense(4), name='dense2', input='input1') graph.add_node(Dense(4), name='dense3', input='dense1') graph.add_output(name='output1', inputs=['dense2', 'dense3'], merge_mode='sum') seq = Sequential() seq.add(Dense(32, input_shape=(32,))) seq.add(graph) seq.add(Dense(4)) seq.compile('rmsprop', 'mse') seq.fit(X_train_graph, y_train_graph, batch_size=10, nb_epoch=10) loss = seq.evaluate(X_test_graph, y_test_graph) # test serialization config = seq.get_config() new_graph = Sequential.from_config(config) seq.summary() json_str = seq.to_json() new_graph = model_from_json(json_str) yaml_str = seq.to_yaml() new_graph = model_from_yaml(yaml_str)
def train_lstm(n_symbols,embedding_weights,x_train,y_train,x_test,y_test): print 'Defining a Simple Keras Model...' model = Sequential() # or Graph or whatever model.add(Embedding(output_dim=vocab_dim, input_dim=n_symbols, mask_zero=True, weights=[embedding_weights], input_length=input_length)) # Adding Input Length model.add(LSTM(output_dim=50, activation='sigmoid', inner_activation='hard_sigmoid')) model.add(Dropout(0.5)) model.add(Dense(1)) model.add(Activation('sigmoid')) print 'Compiling the Model...' model.compile(loss='binary_crossentropy', optimizer='adam',metrics=['accuracy']) print "Train..." model.fit(x_train, y_train, batch_size=batch_size, nb_epoch=n_epoch,verbose=1, validation_data=(x_test, y_test),show_accuracy=True) print "Evaluate..." score = model.evaluate(x_test, y_test, batch_size=batch_size) yaml_string = model.to_yaml() with open('lstm_data/lstm.yml', 'w') as outfile: outfile.write( yaml.dump(yaml_string, default_flow_style=True) ) model.save_weights('lstm_data/lstm.h5') print 'Test score:', score
def test_merge_sum(): (X_train, y_train), (X_test, y_test) = _get_test_data() left = Sequential() left.add(Dense(nb_hidden, input_shape=(input_dim,))) left.add(Activation('relu')) right = Sequential() right.add(Dense(nb_hidden, input_shape=(input_dim,))) right.add(Activation('relu')) model = Sequential() model.add(Merge([left, right], mode='sum')) model.add(Dense(nb_class)) model.add(Activation('softmax')) model.compile(loss='categorical_crossentropy', optimizer='rmsprop') model.fit([X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=0, validation_data=([X_test, X_test], y_test)) model.fit([X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=0, validation_split=0.1) model.fit([X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=0) model.fit([X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=0, shuffle=False) loss = model.evaluate([X_test, X_test], y_test, verbose=0) model.predict([X_test, X_test], verbose=0) model.predict_classes([X_test, X_test], verbose=0) model.predict_proba([X_test, X_test], verbose=0) # test weight saving fname = 'test_merge_sum_temp.h5' model.save_weights(fname, overwrite=True) left = Sequential() left.add(Dense(nb_hidden, input_shape=(input_dim,))) left.add(Activation('relu')) right = Sequential() right.add(Dense(nb_hidden, input_shape=(input_dim,))) right.add(Activation('relu')) model = Sequential() model.add(Merge([left, right], mode='sum')) model.add(Dense(nb_class)) model.add(Activation('softmax')) model.load_weights(fname) os.remove(fname) model.compile(loss='categorical_crossentropy', optimizer='rmsprop') nloss = model.evaluate([X_test, X_test], y_test, verbose=0) assert(loss == nloss) # test serialization config = model.get_config() Sequential.from_config(config) model.summary() json_str = model.to_json() model_from_json(json_str) yaml_str = model.to_yaml() model_from_yaml(yaml_str)
def test_constant_initializer_with_numpy(): model = Sequential() model.add(Dense(2, input_shape=(3,), kernel_initializer=Constant(np.ones((3, 2))))) model.add(Dense(3)) model.compile(loss='mse', optimizer='sgd', metrics=['acc']) json_str = model.to_json() model_from_json(json_str).summary() yaml_str = model.to_yaml() model_from_yaml(yaml_str).summary()
def test_sequential(self): print('Test sequential') model = Sequential() model.add(Dense(nb_hidden, input_shape=(input_dim,))) model.add(Activation('relu')) model.add(Dense(nb_class)) model.add(Activation('softmax')) model.compile(loss='categorical_crossentropy', optimizer='rmsprop') model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=True, verbose=1, validation_data=(X_test, y_test)) model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=False, verbose=2, validation_data=(X_test, y_test)) model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=True, verbose=2, validation_split=0.1) model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=False, verbose=1, validation_split=0.1) model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=0) model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=1, shuffle=False) model.train_on_batch(X_train[:32], y_train[:32]) loss = model.evaluate(X_train, y_train, verbose=0) print('loss:', loss) if loss > 0.7: raise Exception('Score too low, learning issue.') model.predict(X_test, verbose=0) model.predict_classes(X_test, verbose=0) model.predict_proba(X_test, verbose=0) model.get_config(verbose=0) print('test weight saving') fname = 'test_sequential_temp.h5' model.save_weights(fname, overwrite=True) model = Sequential() model.add(Dense(nb_hidden, input_shape=(input_dim,))) model.add(Activation('relu')) model.add(Dense(nb_class)) model.add(Activation('softmax')) model.compile(loss='categorical_crossentropy', optimizer='rmsprop') model.load_weights(fname) os.remove(fname) nloss = model.evaluate(X_train, y_train, verbose=0) assert(loss == nloss) # test json serialization json_data = model.to_json() model = model_from_json(json_data) # test yaml serialization yaml_data = model.to_yaml() model = model_from_yaml(yaml_data)
def test_merge_overlap(): (X_train, y_train), (X_test, y_test) = _get_test_data() left = Sequential() left.add(Dense(nb_hidden, input_shape=(input_dim,))) left.add(Activation('relu')) model = Sequential() model.add(Merge([left, left], mode='sum')) model.add(Dense(nb_class)) model.add(Activation('softmax')) model.compile(loss='categorical_crossentropy', optimizer='rmsprop') model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=1, validation_data=(X_test, y_test)) model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=2, validation_split=0.1) model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=0) model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=1, shuffle=False) model.train_on_batch(X_train[:32], y_train[:32]) loss = model.evaluate(X_test, y_test, verbose=0) model.predict(X_test, verbose=0) model.predict_classes(X_test, verbose=0) model.predict_proba(X_test, verbose=0) fname = 'test_merge_overlap_temp.h5' print(model.layers) model.save_weights(fname, overwrite=True) print(model.trainable_weights) model.load_weights(fname) os.remove(fname) nloss = model.evaluate(X_test, y_test, verbose=0) assert(loss == nloss) # test serialization config = model.get_config() new_model = Sequential.from_config(config) model.summary() json_str = model.to_json() new_model = model_from_json(json_str) yaml_str = model.to_yaml() new_model = model_from_yaml(yaml_str)
def train_mlp(self, input, output): self.in_real = input.data['real'] self.in_imag = input.data['imag'] self.out_real = output.data['real'] self.out_imag = output.data['imag'] (i_dim_x, i_dim_y, i_dim_z) = self.in_real.shape in_dim = i_dim_x*i_dim_y*i_dim_z input_data = self.in_real.reshape(in_dim, 1) (o_dim_x, o_dim_y, o_dim_z) = self.out_real.shape out_dim = o_dim_x*o_dim_y*o_dim_z output_data = self.out_real.reshape(out_dim, 1) model = Sequential() model.add(Dense(200, input_dim=in_dim, init='uniform')) model.add(Activation('relu')) # model.add(Dropout(0.25)) model.add(Dense(200))#, init='uniform')) model.add(Activation('relu')) # model.add(Dropout(0.25)) model.add(Dense(out_dim))#, init='uniform')) model.add(Activation('softmax')) model.compile(loss='categorical_crossentropy', optimizer='sgd',\ metrics=['accuracy']) early_stop = EarlyStopping(monitor='val_loss', patience=2) hist = model.fit(input_data, output_data, nb_epoch=50, \ batch_size=64, validation_split=0.2, \ shuffle=True, callbacks=[early_stop]) print(hist.history) #TODO: batch train model.train_on_batch() # Save model model_to_save_json = model.to_json() open('model_architecture.json', 'w').write(model_to_save_json) model_to_save_yaml = model.to_yaml() open('model_architecture.yaml', 'w').write(model_to_save_yaml) model.save_weights('weights.h5')
def model(X_train, Y_train, X_test, Y_test): ''' Model providing function: Create Keras model with double curly brackets dropped-in as needed. Return value has to be a valid python dictionary with two customary keys: - loss: Specify a numeric evaluation metric to be minimized - status: Just use STATUS_OK and see hyperopt documentation if not feasible The last one is optional, though recommended, namely: - model: specify the model just created so that we can later use it again. ''' from keras.models import Sequential from keras.layers.core import Dense, Dropout, Activation from keras.optimizers import RMSprop model = Sequential() model.add(Dense(512, input_shape=(784,))) model.add(Activation('relu')) model.add(Dropout({{uniform(0, 1)}})) model.add(Dense({{choice([256, 512, 1024])}})) model.add(Activation('relu')) model.add(Dropout({{uniform(0, 1)}})) model.add(Dense(10)) model.add(Activation('softmax')) rms = RMSprop() model.compile(loss='categorical_crossentropy', optimizer=rms) model.fit(X_train, Y_train, batch_size={{choice([64, 128])}}, nb_epoch=1, show_accuracy=True, verbose=2, validation_data=(X_test, Y_test)) score, acc = model.evaluate(X_test, Y_test, show_accuracy=True, verbose=0) print('Test accuracy:', acc) return {'loss': -acc, 'status': STATUS_OK, 'model': model.to_yaml(), 'weights': pickle.dumps(model.get_weights())}
vertical_flip=False) # randomly flip images # compute quantities required for featurewise normalization # (std, mean, and principal components if ZCA whitening is applied) datagen.fit(X_train) print('Predicting train labels...') (X_train2,y_train2)=[(X,y) for X,y in datagen.flow(X_train,y_train,batch_size=X_train.shape[0])][0] y_pred_train=model.predict_classes(X_train2) from sklearn.metrics import accuracy_score print('Train accuracy:',accuracy_score(y_train,y_pred_train)) print('Predicting test labels...') (X_test2,y_test2)=[(X,y) for X,y in datagen.flow(X_test,y_test,batch_size=X_test.shape[0])][0] y_pred_test=model.predict_classes(X_test2) print('Test accuracy:',accuracy_score(y_test,y_pred_test)) #Save the model: yml_model=model.to_yaml() ff=open('model_architecture.yaml','w') ff.write(yml_model) ff.close() # Save the predictions to HDF5 File: f=h5py.File('model_predictions_50epoch.h5','w') f.create_dataset('X_train',data=X_train2) f.create_dataset('y_train',data=y_train2) f.create_dataset('y_pred_train',data=y_pred_train) f.create_dataset('X_test',data=X_test2) f.create_dataset('y_test',data=y_test2) f.create_dataset('y_pred_test',data=y_pred_test)
def test_sequential(self): print('Test sequential') model = Sequential() model.add(Dense(nb_hidden, input_shape=(input_dim, ))) model.add(Activation('relu')) model.add(Dense(nb_class)) model.add(Activation('softmax')) model.compile(loss='categorical_crossentropy', optimizer='rmsprop') model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=True, verbose=1, validation_data=(X_test, y_test)) model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=False, verbose=2, validation_data=(X_test, y_test)) model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=True, verbose=2, validation_split=0.1) model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=False, verbose=1, validation_split=0.1) model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=0) model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=1, shuffle=False) model.train_on_batch(X_train[:32], y_train[:32]) loss = model.evaluate(X_train, y_train, verbose=0) print('loss:', loss) if loss > 0.6: raise Exception('Score too low, learning issue.') preds = model.predict(X_test, verbose=0) classes = model.predict_classes(X_test, verbose=0) probas = model.predict_proba(X_test, verbose=0) print(model.get_config(verbose=1)) print('test weight saving') model.save_weights('temp.h5', overwrite=True) model = Sequential() model.add(Dense(nb_hidden, input_shape=(input_dim, ))) model.add(Activation('relu')) model.add(Dense(nb_class)) model.add(Activation('softmax')) model.compile(loss='categorical_crossentropy', optimizer='rmsprop') model.load_weights('temp.h5') nloss = model.evaluate(X_train, y_train, verbose=0) print(nloss) assert (loss == nloss) # test json serialization json_data = model.to_json() model = model_from_json(json_data) # test yaml serialization yaml_data = model.to_yaml() model = model_from_yaml(yaml_data)
def test_nested_sequential(): (X_train, y_train), (X_test, y_test) = _get_test_data() inner = Sequential() inner.add(Dense(nb_hidden, input_shape=(input_dim,))) inner.add(Activation('relu')) inner.add(Dense(nb_class)) middle = Sequential() middle.add(inner) model = Sequential() model.add(middle) model.add(Activation('softmax')) model.compile(loss='categorical_crossentropy', optimizer='rmsprop') model.summary() model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=True, verbose=1, validation_data=(X_test, y_test)) model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=False, verbose=2, validation_data=(X_test, y_test)) model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=True, verbose=2, validation_split=0.1) model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=False, verbose=1, validation_split=0.1) model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=0) model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=1, shuffle=False) model.train_on_batch(X_train[:32], y_train[:32]) loss = model.evaluate(X_test, y_test, verbose=0) assert(loss < 0.8) model.predict(X_test, verbose=0) model.predict_classes(X_test, verbose=0) model.predict_proba(X_test, verbose=0) model.get_config(verbose=0) fname = 'test_nested_sequential_temp.h5' model.save_weights(fname, overwrite=True) inner = Sequential() inner.add(Dense(nb_hidden, input_shape=(input_dim,))) inner.add(Activation('relu')) inner.add(Dense(nb_class)) middle = Sequential() middle.add(inner) model = Sequential() model.add(middle) model.add(Activation('softmax')) model.compile(loss='categorical_crossentropy', optimizer='rmsprop') model.load_weights(fname) os.remove(fname) nloss = model.evaluate(X_test, y_test, verbose=0) assert(loss == nloss) # test json serialization json_data = model.to_json() model = model_from_json(json_data) # test yaml serialization yaml_data = model.to_yaml() model = model_from_yaml(yaml_data)
class ImageToChar: # Hyperparameters nb_filters = 32 # number of convolutional filters to use pool_size = (2, 2) # size of pooling area for max pooling kernel_size = (3, 3) # convolution kernel size def build(self, nb_classes, input_shape=(28, 28, 1), verbose=False): self.model = Sequential() self.model.add( Conv2D(self.nb_filters, self.kernel_size, padding='valid', input_shape=input_shape, activation='relu')) self.model.add( Conv2D(self.nb_filters, self.kernel_size, activation='relu')) self.model.add(MaxPooling2D(pool_size=self.pool_size)) self.model.add(Dropout(0.25)) self.model.add(Flatten()) self.model.add(Dense(512, activation='relu')) self.model.add(Dropout(0.5)) self.model.add(Dense(nb_classes, activation='softmax')) self.model.compile(loss='categorical_crossentropy', optimizer='adadelta', metrics=['accuracy']) if verbose == True: print(self.model.summary()) return self.model def fit(self, X_train, Y_train, batch_size, nb_epoch): self.model.fit(X_train, Y_train, batch_size=batch_size, epochs=nb_epoch, verbose=1) def score(self, X_test, Y_test): score = self.model.evaluate(X_test, Y_test, verbose=1) print('Test score:', score[0]) print('Test accuracy:', score[1]) return score def predict(self, X_test): # Predict the label for X_test return self.model.predict_classes(X_test) def save(self): # Offload model to file model_yaml = self.model.to_yaml() with open("bin/model.yaml", "w") as yaml_file: yaml_file.write(model_yaml) save_model(self.model, 'bin/model.h5') def load(self): self.model = load_model('bin/model.h5')
print 'compiling...' dl.compile(loss='binary_crossentropy', optimizer='adam', class_mode='binary') print 'training!' h = dl.fit(X[train_ix], y[train_ix], batch_size=32, nb_epoch=50, show_accuracy=True, validation_data=(X[test_ix], y[test_ix]), callbacks = [ EarlyStopping(verbose=True, patience=10, monitor='val_loss'), ModelCheckpoint('./trainings/final-slac-maxout-hypercube-unnormalized-logloss-cvFold{}.h5'.format(foldN), monitor='val_loss', verbose=True, save_best_only=True), ROCModelCheckpoint('./trainings/final-slac-maxout-hypercube-unnormalized-roc-cvFold{}.h5'.format(foldN), X[test_ix], y[test_ix], cube_weights[test_ix], verbose=True) ], sample_weight=cube_weights[train_ix] ) foldN += 1 # sample_weight=np.power(weights, 0.7)) except KeyboardInterrupt: print 'ended early!' # yhat = dl.predict(X, verbose=True).ravel() # np.save('./yhat-cube.npy', yhat.astype('float32')) with open('./trainings/final-slac-maxout-unnormalizedphypercube.yaml', 'wb') as f: f.write(dl.to_yaml())
def test_sequential(self): print("Test sequential") model = Sequential() model.add(Dense(input_dim, nb_hidden)) model.add(Activation("relu")) model.add(Dense(nb_hidden, nb_class)) model.add(Activation("softmax")) model.compile(loss="categorical_crossentropy", optimizer="rmsprop") model.fit( X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=True, verbose=1, validation_data=(X_test, y_test), ) model.fit( X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=False, verbose=2, validation_data=(X_test, y_test), ) model.fit( X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=True, verbose=2, validation_split=0.1, ) model.fit( X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=False, verbose=1, validation_split=0.1, ) model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=0) model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=1, shuffle=False) model.train_on_batch(X_train[:32], y_train[:32]) loss = model.evaluate(X_train, y_train, verbose=0) print("loss:", loss) if loss > 0.6: raise Exception("Score too low, learning issue.") preds = model.predict(X_test, verbose=0) classes = model.predict_classes(X_test, verbose=0) probas = model.predict_proba(X_test, verbose=0) print(model.get_config(verbose=1)) print("test weight saving") model.save_weights("temp.h5", overwrite=True) model = Sequential() model.add(Dense(input_dim, nb_hidden)) model.add(Activation("relu")) model.add(Dense(nb_hidden, nb_class)) model.add(Activation("softmax")) model.compile(loss="categorical_crossentropy", optimizer="rmsprop") model.load_weights("temp.h5") nloss = model.evaluate(X_train, y_train, verbose=0) print(nloss) assert loss == nloss # test json serialization json_data = model.to_json() model = model_from_json(json_data) # test yaml serialization yaml_data = model.to_yaml() model = model_from_yaml(yaml_data)
model.add(Flatten()) #сглаживание model.add(Dense(128, activation='relu')) #полностью связанный слой model.add(Dropout(0.5)) model.add(Dense(DIGITS_COUNT, activation='softmax')) model.compile(loss=keras.losses.categorical_crossentropy, optimizer=keras.optimizers.Adadelta(), metrics=['accuracy']) model.fit( x_train, y_train, # batch_size=batch_size, epochs=2, verbose=1, validation_data=(x_test, y_test)) score = model.evaluate(x_test, y_test, verbose=0) print("Результат", score) fs = open( r"C:\MY_DOC_HP\BMSTU\2019_2\Pattern-recognition\lab4_neural_network\model.pkl", "wb") pickle.dump(model.to_yaml(), fs) fs.close() # pred=model.predict(x_test[5:6]) # print(pred) # plt.subplot(x_test[5]) # plt.subplot(x_test[6]) # plt.show()
def run(gParameters): print('Params:', gParameters) file_train = gParameters['train_data'] file_test = gParameters['test_data'] url = gParameters['data_url'] train_file = data_utils.get_file(file_train, url + file_train, cache_subdir='Pilot1') test_file = data_utils.get_file(file_test, url + file_test, cache_subdir='Pilot1') X_train, Y_train, X_test, Y_test = load_data(train_file, test_file, gParameters) print('X_train shape:', X_train.shape) print('X_test shape:', X_test.shape) print('Y_train shape:', Y_train.shape) print('Y_test shape:', Y_test.shape) x_train_len = X_train.shape[1] # this reshaping is critical for the Conv1D to work X_train = np.expand_dims(X_train, axis=2) X_test = np.expand_dims(X_test, axis=2) print('X_train shape:', X_train.shape) print('X_test shape:', X_test.shape) model = Sequential() layer_list = list(range(0, len(gParameters['conv']), 3)) for l, i in enumerate(layer_list): filters = gParameters['conv'][i] filter_len = gParameters['conv'][i + 1] stride = gParameters['conv'][i + 2] print(int(i / 3), filters, filter_len, stride) if gParameters['pool']: pool_list = gParameters['pool'] if type(pool_list) != list: pool_list = list(pool_list) if filters <= 0 or filter_len <= 0 or stride <= 0: break if 'locally_connected' in gParameters: model.add( LocallyConnected1D(filters, filter_len, strides=stride, padding='valid', input_shape=(x_train_len, 1))) else: #input layer if i == 0: model.add( Conv1D(filters=filters, kernel_size=filter_len, strides=stride, padding='valid', input_shape=(x_train_len, 1))) else: model.add( Conv1D(filters=filters, kernel_size=filter_len, strides=stride, padding='valid')) model.add(Activation(gParameters['activation'])) if gParameters['pool']: model.add(MaxPooling1D(pool_size=pool_list[int(i / 3)])) model.add(Flatten()) for layer in gParameters['dense']: if layer: model.add(Dense(layer)) model.add(Activation(gParameters['activation'])) # This has to be disabled for tensorrt otherwise I am getting an error if False and gParameters['drop']: model.add(Dropout(gParameters['drop'])) #model.add(Dense(gParameters['classes'])) #model.add(Activation(gParameters['out_act']), name='activation_5') model.add( Dense(gParameters['classes'], activation=gParameters['out_act'], name='activation_5')) #Reference case #model.add(Conv1D(filters=128, kernel_size=20, strides=1, padding='valid', input_shape=(P, 1))) #model.add(Activation('relu')) #model.add(MaxPooling1D(pool_size=1)) #model.add(Conv1D(filters=128, kernel_size=10, strides=1, padding='valid')) #model.add(Activation('relu')) #model.add(MaxPooling1D(pool_size=10)) #model.add(Flatten()) #model.add(Dense(200)) #model.add(Activation('relu')) #model.add(Dropout(0.1)) #model.add(Dense(20)) #model.add(Activation('relu')) #model.add(Dropout(0.1)) #model.add(Dense(CLASSES)) #model.add(Activation('softmax')) kerasDefaults = p1_common.keras_default_config() # Define optimizer optimizer = p1_common_keras.build_optimizer(gParameters['optimizer'], gParameters['learning_rate'], kerasDefaults) model.summary() for layer in model.layers: print(layer.name) print([x.op.name for x in model.outputs]) model.compile(loss=gParameters['loss'], optimizer=optimizer, metrics=[gParameters['metrics']]) output_dir = gParameters['save'] if not os.path.exists(output_dir): os.makedirs(output_dir) # calculate trainable and non-trainable params gParameters.update(compute_trainable_params(model)) # set up a bunch of callbacks to do work during model training.. model_name = gParameters['model_name'] path = '{}/{}.autosave.model.h5'.format(output_dir, model_name) # checkpointer = ModelCheckpoint(filepath=path, verbose=1, save_weights_only=False, save_best_only=True) csv_logger = CSVLogger('{}/training.log'.format(output_dir)) reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=10, verbose=1, mode='auto', epsilon=0.0001, cooldown=0, min_lr=0) candleRemoteMonitor = CandleRemoteMonitor(params=gParameters) timeoutMonitor = TerminateOnTimeOut(TIMEOUT) history = model.fit( X_train, Y_train, batch_size=gParameters['batch_size'], epochs=2, #gParameters['epochs'], verbose=1, validation_data=(X_test, Y_test), callbacks=[csv_logger, reduce_lr, candleRemoteMonitor, timeoutMonitor]) score = model.evaluate(X_test, Y_test, verbose=0) #Begin tensorrt code config = { # Where to save models (Tensorflow + TensorRT) "graphdef_file": "/gpfs/jlse-fs0/users/pbalapra/tensorrt/Benchmarks/Pilot1/NT3/nt3.pb", "frozen_model_file": "/gpfs/jlse-fs0/users/pbalapra/tensorrt/Benchmarks/Pilot1/NT3/nt3_frozen_model.pb", "snapshot_dir": "/gpfs/jlse-fs0/users/pbalapra/tensorrt/Benchmarks/Pilot1/NT3/snapshot", "engine_save_dir": "/gpfs/jlse-fs0/users/pbalapra/tensorrt/Benchmarks/Pilot1/NT3", # Needed for TensorRT "inference_batch_size": 1, # inference batch size "input_layer": "conv1d_1", # name of the input tensor in the TF computational graph "out_layer": "activation_5/Softmax", # name of the output tensorf in the TF conputational graph "output_size": 2, # number of classes in output (5) "precision": "fp32" # desired precision (fp32, fp16) "test_image_path" : "/home/data/val/roses" } # Now, let's use the Tensorflow backend to get the TF graphdef and frozen graph K.set_learning_phase(0) sess = K.get_session() saver = saver_lib.Saver(write_version=saver_pb2.SaverDef.V2) # save model weights in TF checkpoint checkpoint_path = saver.save(sess, config['snapshot_dir'], global_step=0, latest_filename='checkpoint_state') # remove nodes not needed for inference from graph def train_graph = sess.graph inference_graph = tf.graph_util.remove_training_nodes( train_graph.as_graph_def()) #print(len([n.name for n in tf.get_default_graph().as_graph_def().node])) # write the graph definition to a file. # You can view this file to see your network structure and # to determine the names of your network's input/output layers. graph_io.write_graph(inference_graph, '.', config['graphdef_file']) # specify which layer is the output layer for your graph. # In this case, we want to specify the softmax layer after our # last dense (fully connected) layer. out_names = config['out_layer'] # freeze your inference graph and save it for later! (Tensorflow) freeze_graph.freeze_graph(config['graphdef_file'], '', False, checkpoint_path, out_names, "save/restore_all", "save/Const:0", config['frozen_model_file'], False, "") if False: print('Test score:', score[0]) print('Test accuracy:', score[1]) # serialize model to JSON model_json = model.to_json() with open("{}/{}.model.json".format(output_dir, model_name), "w") as json_file: json_file.write(model_json) # serialize model to YAML model_yaml = model.to_yaml() with open("{}/{}.model.yaml".format(output_dir, model_name), "w") as yaml_file: yaml_file.write(model_yaml) # serialize weights to HDF5 model.save_weights("{}/{}.weights.h5".format(output_dir, model_name)) print("Saved model to disk") # load json and create model json_file = open('{}/{}.model.json'.format(output_dir, model_name), 'r') loaded_model_json = json_file.read() json_file.close() loaded_model_json = model_from_json(loaded_model_json) # load yaml and create model yaml_file = open('{}/{}.model.yaml'.format(output_dir, model_name), 'r') loaded_model_yaml = yaml_file.read() yaml_file.close() loaded_model_yaml = model_from_yaml(loaded_model_yaml) # load weights into new model loaded_model_json.load_weights('{}/{}.weights.h5'.format( output_dir, model_name)) print("Loaded json model from disk") # evaluate json loaded model on test data loaded_model_json.compile(loss=gParameters['loss'], optimizer=gParameters['optimizer'], metrics=[gParameters['metrics']]) score_json = loaded_model_json.evaluate(X_test, Y_test, verbose=0) print('json Test score:', score_json[0]) print('json Test accuracy:', score_json[1]) print("json %s: %.2f%%" % (loaded_model_json.metrics_names[1], score_json[1] * 100)) # load weights into new model loaded_model_yaml.load_weights('{}/{}.weights.h5'.format( output_dir, model_name)) print("Loaded yaml model from disk") # evaluate loaded model on test data loaded_model_yaml.compile(loss=gParameters['loss'], optimizer=gParameters['optimizer'], metrics=[gParameters['metrics']]) score_yaml = loaded_model_yaml.evaluate(X_test, Y_test, verbose=0) print('yaml Test score:', score_yaml[0]) print('yaml Test accuracy:', score_yaml[1]) print("yaml %s: %.2f%%" % (loaded_model_yaml.metrics_names[1], score_yaml[1] * 100)) return history
class TripletModel: def __init__(self, deep_id_dim=3000, aux_weight=0.1, nb_epoch=20, nb_classes=5043, model_name='my_model1'): self.batch_size = 100 self.nb_epoch = nb_epoch self.vision_model = Sequential() self.model = None self.hash_len = deep_id_dim self.aux_weight = aux_weight self.nb_classes = nb_classes self.model_name = model_name self.build_model2() def build_model2(self): def euclidean_distance(vecs): x, y = vecs return K.sum(K.square(x - y), axis=1, keepdims=True) def euclidean_dist_output_shape(shapes): shape1, _ = shapes return shape1[0], 1 def triplet_loss(y_true, y_pred): # Use y_true as alpha mse0, mse1 = y_pred[:, 0], y_pred[:, 1] return K.maximum(0.0, mse0 - mse1 + y_true[:, 0]) # input image dimensions img_rows, img_cols, img_channel = 100, 100, 3 # number of convolutional filters to use nb_filters = 20 # size of pooling area for max pooling nb_pool = 2 # convolution kernel size nb_conv = 5 # build a vision model self.vision_model.add(Convolution2D(nb_filters, nb_conv, nb_conv, activation='relu', input_shape=(img_channel, img_rows, img_cols))) self.vision_model.add(Convolution2D(nb_filters, nb_conv, nb_conv, activation='relu')) self.vision_model.add(MaxPooling2D(pool_size=(nb_pool, nb_pool))) self.vision_model.add(Dropout(0.25)) self.vision_model.add(Convolution2D(nb_filters, nb_conv, nb_conv, activation='relu')) self.vision_model.add(Convolution2D(nb_filters, nb_conv, nb_conv, activation='relu')) self.vision_model.add(MaxPooling2D(pool_size=(nb_pool, nb_pool))) self.vision_model.add(Flatten()) self.vision_model.add(Dense(self.hash_len)) # TODO: tunable! img1 = Input(shape=(img_channel, img_rows, img_cols), name='X1') img2 = Input(shape=(img_channel, img_rows, img_cols), name='X2') img3 = Input(shape=(img_channel, img_rows, img_cols), name='X3') hash1, hash2 = self.vision_model(img1), self.vision_model(img2) hash3 = self.vision_model(img3) vid = Dense(self.nb_classes, activation='softmax', name='aux_output')(hash1) distance_layer = Lambda(euclidean_distance, output_shape=euclidean_dist_output_shape) dist12 = distance_layer([hash1, hash2]) dist13 = distance_layer([hash1, hash3]) merged_out = merge([dist12, dist13], mode='concat', name='main_output') self.model = Model(input=[img1, img2, img3], output=[merged_out, vid]) self.model.summary() print(self.model.output_shape) print('DeepID dim:', self.hash_len) self.model.compile(optimizer='adadelta', loss={'main_output': triplet_loss, 'aux_output': 'categorical_crossentropy'}, loss_weights={'main_output': 1., 'aux_output': self.aux_weight}) def fit(self, X_trains, y_train): X_train1, X_train2, X_train3 = X_trains main_target, X1_vid = y_train early_stopping = EarlyStopping(monitor='val_loss', patience=2) print(X_train1.shape) print(X1_vid.shape) print(main_target.shape) self.model.fit({'X1': X_train1, 'X2': X_train2, 'X3': X_train3}, {'main_output': main_target, 'aux_output': X1_vid}, batch_size=self.batch_size, nb_epoch=self.nb_epoch, verbose=1, validation_data=([X_train1, X_train2, X_train3], y_train), callbacks=[early_stopping]) y_target = np.argmax(X1_vid, axis=1) y_predict = np.argmax(self.vision_model.predict(X_train1, verbose=0), axis=1) conf_mat = confusion_matrix(y_target, y_predict) print('Test accuracy:') n_correct = np.sum(np.diag(conf_mat)) print('# correct:', n_correct, 'out of', len(y_target), ', acc=', float(n_correct) / len(y_target)) def save_model(self, overwrite=True): model_path = '../model/' if not os.path.exists(model_path): os.mkdir(model_path) # save the wrapper distance model yaml_string = self.model.to_yaml() open(os.path.join(model_path, self.model_name + '_arch.yaml'), 'w').write(yaml_string) self.model.save_weights(os.path.join(model_path, self.model_name + '_weights.h5'), overwrite) # save the inner vision model model_name = self.model_name + '_vision' yaml_string = self.vision_model.to_yaml() open(os.path.join(model_path, model_name + '_arch.yaml'), 'w').write(yaml_string) self.vision_model.save_weights(os.path.join(model_path, model_name + '_weights.h5'), overwrite) def load_model(self): model_path = '../model/' self.model = model_from_yaml(open(os.path.join(model_path, self.model_name + '_arch.yaml')).read()) self.model.load_weights(os.path.join(model_path, self.model_name + '_weights.h5')) model_name = self.model_name + '_vision' self.vision_model = model_from_yaml(open(os.path.join(model_path, model_name + '_arch.yaml')).read()) self.vision_model.load_weights(os.path.join(model_path, model_name + '_weights.h5')) def get_deep_id(self, cars): return self.vision_model.predict(cars)
class ResNet: def __init__(self, batch, epochs, inputDim): self.__batch = batch self.__epochs = epochs self.__input_dim = inputDim self.__model = None self.__history = None def __del__(self): del self.__batch del self.__epochs del self.__input_dim del self.__model del self.__history #def build_resnet(self, input_shape, n_feature_maps, nb_classes): def build_resnet(self, input_shape, n_feature_maps): x = Input(shape=(input_shape)) conv_x = keras.layers.normalization.BatchNormalization()(x) conv_x = keras.layers.Conv2D(n_feature_maps, (8, 1), padding='same')(conv_x) conv_x = keras.layers.normalization.BatchNormalization()(conv_x) conv_x = Activation('relu')(conv_x) conv_y = keras.layers.Conv2D(n_feature_maps, (5, 1), padding='same')(conv_x) conv_y = keras.layers.normalization.BatchNormalization()(conv_y) conv_y = Activation('relu')(conv_y) conv_z = keras.layers.Conv2D(n_feature_maps, (3, 1), padding='same')(conv_y) conv_z = keras.layers.normalization.BatchNormalization()(conv_z) is_expand_channels = not (input_shape[-1] == n_feature_maps) if is_expand_channels: shortcut_y = keras.layers.Conv2D(n_feature_maps, (1, 1), padding='same')(x) shortcut_y = keras.layers.normalization.BatchNormalization()( shortcut_y) else: shortcut_y = keras.layers.normalization.BatchNormalization()(x) y = Add()([shortcut_y, conv_z]) y = Activation('relu')(y) x1 = y conv_x = keras.layers.Conv2D(n_feature_maps * 2, (8, 1), padding='same')(x1) conv_x = keras.layers.normalization.BatchNormalization()(conv_x) conv_x = Activation('relu')(conv_x) conv_y = keras.layers.Conv2D(n_feature_maps * 2, (5, 1), padding='same')(conv_x) conv_y = keras.layers.normalization.BatchNormalization()(conv_y) conv_y = Activation('relu')(conv_y) conv_z = keras.layers.Conv2D(n_feature_maps * 2, (3, 1), padding='same')(conv_y) conv_z = keras.layers.normalization.BatchNormalization()(conv_z) is_expand_channels = not (input_shape[-1] == n_feature_maps * 2) if is_expand_channels: shortcut_y = keras.layers.Conv2D(n_feature_maps * 2, (1, 1), padding='same')(x1) shortcut_y = keras.layers.normalization.BatchNormalization()( shortcut_y) else: shortcut_y = keras.layers.normalization.BatchNormalization()(x1) y = Add()([shortcut_y, conv_z]) y = Activation('relu')(y) x1 = y conv_x = keras.layers.Conv2D(n_feature_maps * 2, (8, 1), padding='same')(x1) conv_x = keras.layers.normalization.BatchNormalization()(conv_x) conv_x = Activation('relu')(conv_x) conv_y = keras.layers.Conv2D(n_feature_maps * 2, (5, 1), padding='same')(conv_x) conv_y = keras.layers.normalization.BatchNormalization()(conv_y) conv_y = Activation('relu')(conv_y) conv_z = keras.layers.Conv2D(n_feature_maps * 2, (3, 1), padding='same')(conv_y) conv_z = keras.layers.normalization.BatchNormalization()(conv_z) is_expand_channels = not (input_shape[-1] == n_feature_maps * 2) if is_expand_channels: shortcut_y = keras.layers.Conv2D(n_feature_maps * 2, (1, 1), padding='same')(x1) shortcut_y = keras.layers.normalization.BatchNormalization()( shortcut_y) else: shortcut_y = keras.layers.normalization.BatchNormalization()(x1) y = Add()([shortcut_y, conv_z]) y = Activation('relu')(y) full = keras.layers.pooling.GlobalAveragePooling2D()(y) #out = Dense(nb_classes, activation='softmax')(full) out = Dense(1, activation='linear')(full) self.__model = Model(inputs=x, outputs=out) def build_model(self): self.__model = Sequential() self.__model.add(Dense(12, kernel_initializer='normal', bias_initializer='zeros', \ input_dim=self.__input_dim, activation='sigmoid')) self.__model.add(BatchNormalization()) self.__model.add(Conv2D(n_feature_maps, 8, 1, border_mode='same')) self.__model.add(BatchNormalization()) self.__model.add(Activation('relu')) self.__model.add(Conv2D(n_feature_maps, 5, 1, border_mode='same')) self.__model.add(BatchNormalization()) self.__model.add(Activation('relu')) self.__model.add(Conv2D(n_feature_maps, 3, 1, border_mode='same')) self.__model.add(BatchNormalization()) is_expand_channels = not (input_shape[-1] == n_feature_maps) if is_expand_channels: self.__model.add(Conv2D(n_feature_maps, 1, 1, border_mode='same')) self.__model.add(BatchNormalization()) else: self.__model.add(BatchNormalization()) print('Merging skip connection') #y = merge([shortcut_y, conv_z], mode='sum') self.__model.add(Add()) #y = Add()([shortcut_y, conv_z]) #y = Activation('relu')(y) self.__model.add(Activation('relu')) full = keras.layers.pooling.GlobalAveragePooling2D()(y) out = Dense(nb_classes, activation='softmax')(full) def load_nasa_challenge_data(self, train_file, test_file, train_gap=20, dev_file=''): columns = ['device_id', 'cycles', 'setting1', 'setting2', 'setting3'] sensors = ['sensor' + str(i + 1) for i in np.arange(0, 23)] columns += sensors df_train = pd.read_csv(train_file, sep=' ', header=None) df_train.columns = columns df_train.dropna(axis=1, inplace=True) df_train['rank'] = df_train.groupby('device_id')['cycles'].rank( ascending=False) df_train['Y'] = np.zeros(df_train.shape[0]) df_train.loc[df_train['rank'] <= train_gap, ['Y']] = 1 df_train['RUL'] = np.zeros(df_train.shape[0]) df_test = pd.read_csv(test_file, sep=' ', header=None) df_test.columns = columns df_test.dropna(axis=1, inplace=True) df_test['rank'] = df_test.groupby('device_id')['cycles'].rank( ascending=False) df_test['Y'] = np.zeros(df_test.shape[0]) df_test.loc[df_test['rank'] <= train_gap, ['Y']] = 1 df_test['RUL'] = np.zeros(df_test.shape[0]) if len(dev_file) > 0: df_dev = pd.read_csv(dev_file, sep=' ', header=None) df_dev.columns = columns df_dev.dropna(axis=1, inplace=True) df_dev['rank'] = df_dev.groupby('device_id')['cycles'].rank( ascending=False) df_dev['Y'] = np.zeros(df_dev.shape[0]) df_dev.loc[df_dev['rank'] <= train_gap, ['Y']] = 1 df_dev['RUL'] = np.zeros(df_dev.shape[0]) train = [] for dev_id in df_train['device_id'].unique(): data = df_train[df_train['device_id'] == dev_id] data['RUL'] = data['cycles'].max() - data['cycles'] train.append(data) df_train = pd.concat(train, axis=0) test = [] for dev_id in df_test['device_id'].unique(): data = df_test[df_test['device_id'] == dev_id] data['RUL'] = data['cycles'].max() - data['cycles'] test.append(data) df_test = pd.concat(test, axis=0) if len(dev_file) > 0: dev = [] for dev_id in df_dev['device_id'].unique(): data = df_dev[df_dev['device_id'] == dev_id] data['RUL'] = data['cycles'].max() - data['cycles'] dev.append(data) df_dev = pd.concat(dev, axis=0) parameters = ['cycles', 'setting1', 'setting2', 'setting3'] #parameters = ['setting1', 'setting2', 'setting3'] sensors = ['sensor' + str(i + 1) for i in np.arange(0, 21)] parameters += sensors df_train.sort_values(by=['cycles'], ascending=True, inplace=True) df_test.sort_values(by=['cycles'], ascending=True, inplace=True) if len(dev_file) > 0: df_dev.sort_values(by=['cycles'], ascending=True, inplace=True) d = {} d['device_id'] = [] d['cycles'] = [] for dev_id in df_train['device_id'].unique(): d['device_id'].append(dev_id) d['cycles'].append( df_train[df_train['device_id'] == dev_id]['cycles'].max()) df_train_events = pd.DataFrame(data=d) df_train_events.sort_values(by=['cycles'], ascending=True, inplace=True) d = {} d['device_id'] = [] d['cycles'] = [] for dev_id in df_test['device_id'].unique(): d['device_id'].append(dev_id) d['cycles'].append( df_test[df_test['device_id'] == dev_id]['cycles'].max()) df_test_events = pd.DataFrame(data=d) df_test_events.sort_values(by=['cycles'], ascending=True, inplace=True) if len(dev_file) > 0: d = {} d['device_id'] = [] d['cycles'] = [] for dev_id in df_dev['device_id'].unique(): d['device_id'].append(dev_id) d['cycles'].append( df_dev[df_dev['device_id'] == dev_id]['cycles'].max()) df_dev_events = pd.DataFrame(data=d) df_dev_events.sort_values(by=['cycles'], ascending=True, inplace=True) #scaler = StandardScaler() scaler = MinMaxScaler(feature_range=(0, 1)) X_train = scaler.fit_transform(df_train[parameters].values) y_train = np.asarray(df_train['RUL']).ravel() #X_test = scaler.fit_transform(df_test[parameters].values) X_test = scaler.transform(df_test[parameters].values) y_test = np.asarray(df_test['RUL']).ravel() if len(dev_file) > 0: X_dev = scaler.transform(df_dev[parameters].values) y_dev = np.asarray(df_dev['RUL']).ravel() X_dev = X_dev.reshape(X_dev.shape + ( 1, 1, )) X_train = X_train.reshape(X_train.shape + ( 1, 1, )) X_test = X_test.reshape(X_test.shape + ( 1, 1, )) if len(dev_file) > 0: return X_train, y_train, X_test, y_test, df_train_events, df_test_events, X_dev, y_dev else: return X_train, y_train, X_test, y_test, df_train_events, df_test_events def fit(self, X, y, X_test, y_test, events, name='ResNet_NASA_Challenge', loss='mean_absolute_percentage_error'): self.build_resnet(X.shape[1:], 64) self.__model.summary() self.__model.compile(optimizer='rmsprop', loss=loss, metrics=['mae', 'acc']) #self.__model.compile(optimizer='sgd', loss=loss, metrics=['mae', 'acc']) #self.__model.compile(optimizer='rmsprop', loss=rul_loss(), metrics=['mae', 'acc']) #self.__model.compile(optimizer='sgd', loss=longitudinal_loss(events), metrics=['categorical_accuracy']) self.__history = self.__model.fit(X, y, \ batch_size=self.__batch, epochs=self.__epochs, \ verbose=1, validation_data=(X_test, y_test)) score = self.__model.evaluate(X_test, y_test, verbose=1) with open(DATA_DIR + 'model/' + name + '_history.pkl', 'wb') as handler: pickle.dump(self.__history.history, handler) handler.close() #metric = Metrics() #y_hat_test = self.__model.predict(X_test) y_hat_train = self.__model.predict(X) y_hat_test = self.__model.predict(X_test) def save(self, name='ResNet_NASA_Challenge'): json_string = self.__model.to_json() handler = open(DATA_DIR + 'model/' + name + '.json', 'w') handler.write(json_string) handler.close() yaml_string = self.__model.to_yaml() handler = open(DATA_DIR + 'model/' + name + '.yaml', 'w') handler.write(yaml_string) handler.close() self.__model.save_weights(DATA_DIR + 'model/' + name + '.h5') def test(self, X_test, y_test, name='ResNet_NASA_Challenge', loss='mean_absolute_percentage_error', test='test'): with open(DATA_DIR + 'model/' + name + '.json', 'r') as handler: json_string = handler.read() self.__model = model_from_json(json_string) handler.close() self.__model.load_weights(DATA_DIR + 'model/' + name + '.h5') self.__model.compile(optimizer='rmsprop', loss=loss, metrics=['mae', 'acc']) #self.__model.compile(optimizer='sgd', loss=loss, metrics=['mae', 'acc']) y_hat_test = self.__model.predict(X_test) d = {} d['y'] = y_test.ravel() d['y_hat'] = y_hat_test.ravel() df = pd.DataFrame(data=d) df.to_csv(DATA_DIR + 'model/' + name + '_y_' + test + '.csv', index=False)
MODEL.add(InputLayer(input_shape=IMG_SHAPE)) MODEL.add(Dense(512)) MODEL.add(Activation('selu')) MODEL.add(AlphaDropout(0.2)) MODEL.add(Dense(512)) MODEL.add(Activation('selu')) MODEL.add(AlphaDropout(0.2)) MODEL.add(Dense(N_CLASSES)) MODEL.add(Activation('softmax')) MODEL.summary() plot_model(MODEL, to_file='../bin/snn_fashion_mnist_2_hidden.png') with open('../bin/snn_fashion_mnist_2_hidden.yaml', 'w') as f: f.write(MODEL.to_yaml()) MODEL.compile(loss=categorical_crossentropy, optimizer=RMSprop(), metrics=['accuracy']) HIST_MODEL = MODEL.fit(X_TRAIN, Y_TRAIN, batch_size=BATCH, epochs=EPOCHS, verbose=True, validation_data=(X_TEST, Y_TEST)) MODEL.save('../bin/snn_fashion_mnist_2_hidden.h5') LOSS, ACCURACY = MODEL.evaluate(X_TEST, Y_TEST, verbose=False) print('Test Loss {0}'.format(LOSS))
regressor.add(LSTM(256, return_sequences=True)) regressor.add(Dropout(0.2)) regressor.add(LSTM(256, return_sequences=True)) regressor.add(Dropout(0.2)) regressor.add(LSTM(256)) regressor.add(Dropout(0.2)) regressor.add(Dense(1)) regressor.compile(algorithm, loss='mean_squared_error') # Save the model config with open(os.path.join(folder, 'model.yaml'), 'w') as f: f.write(regressor.to_yaml()) # Train the model and save progress regressor.fit(X_train, y_train, batch_size=batch_size, epochs=epochs) regressor.save_weights(os.path.join(folder, 'weights.hdf5')) # Predict the results predicted_stock = regressor.predict(X_test) predicted_stock = sc.inverse_transform(predicted_stock) predicted_stock = predicted_stock.flatten() real_stock = dataset.iloc[training_len:, 1].values # Visualize the prediction from utils import visualize_results
def cnn_model(path, model=None): #When the model started training start_date = dt.datetime.now().strftime("%Y-%m-%d-%H:%M") print ("Start Time: "+start_date) #y, names, X = utils.load_data(path) #num_of_classes = len(names) #Reshape input data to be fed to the network #[number, x, y, channels] #if (X.ndim == 4): # X = np.reshape(X, (X.shape[0], X.shape[1], X.shape[2], 3)) #else: # X = np.reshape(X, (X.shape[0], X.shape[1], X.shape[2], 1)) # Making sure your input shape is correct #assert(X.ndim == 4) #assert(y.ndim == 1) #Get image size #image_size = X.shape[1] #Split Data to chunks using dask #print "Splitting to chunks.." #k2 = da.from_array(X, chunks = 2) #k2.to_delayed() #PROBLEM HERE: THIS MODEL IS DIFFERENT THAN THE ONE IN KERAS #vgg_model = cnn_models.VGG_16("weights/vgg16_weights.h5") if (model == None): vgg_model = vgg16.VGG16(weights='imagenet') # model = cnn_models.custom_model(num_of_classes, image_size, weights_path=False) print "pretrained model loaded.." model = Sequential() for i in range(len(vgg_model.layers)-3): model.add(vgg_model.layers[i]) model.add(Dense(4096, kernel_initializer='uniform', activation='relu')) model.add(Dropout(0.5)) model.add(Dense(4096, kernel_initializer='uniform', activation='relu')) model.add(Dropout(0.5)) model.add(Dense(24, activation='softmax')) # set the first n layers to non-trainable (weights will not be updated) for layer in model.layers[:14]: layer.trainable = False #Remove last layer of the vgg model #model.pop() #model.outputs = [model.layers[-1].output] #model.layers[-1].outbound_nodes = [] #model = cnn_models.custom_model(num_of_classes, image_size, weights) # check the layers by name : same as using model.summary #for i,layer in enumerate(model.layers): # print(i,layer.name) # print(layer.get_output_at(0).get_shape().as_list()) #Print out the model structure print(model.summary()) #Convert class vectors to binary class matrices #y = keras_utils.to_categorical(np.ravel(y), num_of_classes) #Normalize data #print "Normalize data.." #X = X.astype('float16') #X /= 255 #Split data #print "Splitting data to train and test" #X_train, X_val, y_train, y_val = utils.train_test_split(X, y, test_size=0.65) #del X #del y #TRAIN MODEL WITH CHUNKS OF DATA #Data augmentation (shift + flip + rotation) train_datagen = ImageDataGenerator(width_shift_range=.2, height_shift_range=.2, horizontal_flip=True, rotation_range=25, rescale=1./255 ) # this is the augmentation configuration we will use for testing: # only rescaling test_datagen = ImageDataGenerator(rescale=1./255) train_generator = train_datagen.flow_from_directory( path+'/train', target_size=(224, 224), batch_size=32, class_mode='categorical', shuffle=True) # same es the train_generator validation_generator = test_datagen.flow_from_directory( path+'/validation', target_size=(224, 224), batch_size=32, class_mode='categorical', shuffle=True) #fit parameters from data #datagen.fit(X_train) sgd = optimizers.SGD(lr=0.0001, decay=1e-6, momentum=0.9, nesterov=True) model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy']) #Debug with tensorboard tensorboard = TensorBoard(log_dir="logs/{}".format(time())) #fits the model on batches with real-time data augmentation: #model.fit_generator(datagen.flow(X_train, y_train, batch_size=32), # steps_per_epoch=len(X_train) / 32, epochs=epochs, callbacks=[tensorboard]) # loads sequentially images and feeds them to the model. # the batch size is set in the constructor model.fit_generator( train_generator, samples_per_epoch=2000, nb_epoch=1, validation_data=validation_generator, nb_val_samples=800) #Evaluation of the model print "Evaluating the model with val data.." scores = model.evaluate(X_val, y_val, verbose=1, batch_size=64) print("Accuracy: %.2f%%" % (scores[1]*100)) #Optimizer #sgd = optimizers.SGD(lr=0.0001, decay=1e-6, momentum=0.9, nesterov=True) #model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy']) #Debug with tensorboard #tensorboard = TensorBoard(log_dir="logs/{}".format(time())) #model.fit(X_train, y_train, epochs=10, batch_size=32, verbose=1, shuffle=True, validation_data=(X_val, y_val), callbacks=[tensorboard]) #date of the model stoped Training end_date = dt.datetime.now().strftime("%Y-%m-%d-%H:%M") print ("Start Time: "+start_date) print("End Time: "+end_date) #Make a directory to save the weights try: os.makedirs('weights') except OSError as exception: if exception.errno != errno.EEXIST: raise #Make a directory to save the weights according to date try: os.makedirs('weights/'+end_date) except OSError as exception: if exception.errno != errno.EEXIST: raise #Serialize weights to HDF5 model.save_weights('weights/'+end_date+"/cnn_weights.h5") #Serialize model to YAML model_yaml = model.to_yaml() #Save model with open("weights/"+end_date+"/cnn_model.yaml", "w") as yaml_file: yaml_file.write(model_yaml) print("Saved CNN model with weights to disk") #Delete model to free GPU memory print "Deleting current model" del model limit_mem() return ('weights/'+end_date)
def test_sequential(): (X_train, y_train), (X_test, y_test) = _get_test_data() model = Sequential() model.add(Dense(nb_hidden, input_shape=(input_dim, ))) model.add(Activation('relu')) model.add(Dense(nb_class)) model.add(Activation('softmax')) model.compile(loss='categorical_crossentropy', optimizer='rmsprop') model.summary() model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=True, verbose=1, validation_data=(X_test, y_test)) model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=False, verbose=2, validation_data=(X_test, y_test)) model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=True, verbose=2, validation_split=0.1) model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=False, verbose=1, validation_split=0.1) model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=0) model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=1, shuffle=False) model.train_on_batch(X_train[:32], y_train[:32]) loss = model.evaluate(X_test, y_test, verbose=0) assert (loss < 0.8) model.predict(X_test, verbose=0) model.predict_classes(X_test, verbose=0) model.predict_proba(X_test, verbose=0) model.get_config(verbose=0) fname = 'test_sequential_temp.h5' model.save_weights(fname, overwrite=True) model = Sequential() model.add(Dense(nb_hidden, input_shape=(input_dim, ))) model.add(Activation('relu')) model.add(Dense(nb_class)) model.add(Activation('softmax')) model.compile(loss='categorical_crossentropy', optimizer='rmsprop') model.load_weights(fname) os.remove(fname) nloss = model.evaluate(X_test, y_test, verbose=0) assert (loss == nloss) # test json serialization json_data = model.to_json() model = model_from_json(json_data) # test yaml serialization yaml_data = model.to_yaml() model = model_from_yaml(yaml_data)
class_mode='categorical', color_mode='grayscale' # Since we are considering only B&W images ) # this is a similar generator, for validation data validation_generator = test_datagen.flow_from_directory( directory='../Letters/Val2', target_size=(28, 28), class_mode='categorical', color_mode='grayscale', batch_size=78) model.fit_generator( train_generator, # steps_per_epoch=5000 // batch_size, steps_per_epoch=260, epochs=10, validation_data=validation_generator, validation_steps=10) # """ # p = Augmentor.Pipeline("Letters") # p.random_distortion(probability=0.9, grid_width=4, grid_height=4, magnitude=8) # p.rotate(probability=0.3, max_left_rotation=5, max_right_rotation=5) # augmentGen = p.keras_generator(batch_size=batch_size) # model.fit_generator(augmentGen, steps_per_epoch=len(p.augmentor_images)/batch_size, epochs=5, verbose=1) model_yaml = model.to_yaml() with open("lenet.yaml", "w") as yaml_file: yaml_file.write(model_yaml) save_model(model, 'lenet.h5')
#proba = model.predict_proba(X_test, batch_size=3) #print('Test predict_proba:', proba) #plot(model, to_file='model.png') #predict = model.predict(X_test, batch_size=batch_size, verbose=0) #print( predict ) #print( 'sum', np.sum(y_test-predict) ) exit() ########### # Save ########### # save as JSON json_string = model.to_json() # save as YAML yaml_string = model.to_yaml() model.save_weights('my_model_weights.h5') ########### # Load ########### model = model_from_json(open('my_model_architecture.json').read()) model.load_weights('my_model_weights.h5') ########### # compile ########### model.compile(optimizer='adagrad', loss='mse') """ #使用data augmentation的方法
#w[0].shape = (100, 64) #w[1].shape = (64,) #w[2].shape = (64, 10) #w[3].shape = (10,) config = model1.get_config() #del model1 print(config) ''' model2 = Model.from_config(config) print(model2.summary()) print(model2.inputs) print(model2.outputs) model3 = Sequential.from_config(config) print(model3.summary()) print(model3.inputs) print(model3.outputs) ''' from keras.models import model_from_json json_string = model1.to_json() from keras.models import model_from_yaml yaml_string = model1.to_yaml() print(json_string) print(yaml_string)
def ann(self): #print self.company.X_train.shape[1] model = Sequential() model.add(Dense(input_dim=self.company.X_train.shape[1], output_dim=50, init="glorot_uniform")) #model.add(Activation('tanh')) model.add(Dropout(0.1)) model.add(Dense(input_dim=50, output_dim=10, init="uniform")) model.add(Activation('tanh')) #model.add(Dropout(0.5)) model.add(Dense(input_dim=10, output_dim=1, init="glorot_uniform")) model.add(Activation("tanh")) sgd = SGD(lr=0.3, decay=1e-6, momentum=0.9, nesterov=True) model.compile(loss='mean_squared_error', optimizer='rmsprop') early_stopping = EarlyStopping(monitor='val_loss', patience=110) model.fit(self.company.X_train, self.company.y_train, nb_epoch=1000, validation_split=.1, batch_size=16, verbose = 1, show_accuracy = True, shuffle = False, callbacks=[early_stopping]) self.ann_mse = model.evaluate(self.company.X_cv, self.company.y_cv, show_accuracy=True, batch_size=16) print self.ann_mse self.ann_preds = model.predict(self.company.X_test) yaml_string = model.to_yaml() with open(self.company.fin_dir + '/ann-models/' + self.company.experiment_version +'_ann_model.yml', 'w+') as outfile: outfile.write( yaml.dump(yaml_string, default_flow_style=True) ) #model.save_weights(self.company.fin_dir + '/ann-models/' + self.company.experiment_version +'_ann_weights') """ nb_features = self.company.X_train.shape[1] X_train = self.company.X_train.reshape(self.company.X_train.shape + (1, )) X_test = self.company.X_test.reshape(self.company.X_test.shape + (1, )) print X_train.shape model = Sequential() model.add(Convolution1D(nb_filter = 24, filter_length = 1, input_shape =(nb_features,1) )) model.add(Activation("tanh")) model.add(Dropout(0.2)) # some dropout to help w/ overfitting model.add(Convolution1D(nb_filter = 48, filter_length= 1, subsample_length= 1)) model.add(Activation("tanh")) model.add(Convolution1D(nb_filter = 96, filter_length= 1, subsample_length=1)) model.add(Activation("tanh")) model.add(Dropout(0.3)) model.add(Convolution1D(nb_filter = 192, filter_length= 1, subsample_length=1)) model.add(Activation("tanh")) model.add(Dropout(0.6)) model.add(MaxPooling1D(pool_length=2)) # flatten to add dense layers model.add(Flatten()) #model.add(Dense(input_dim=nb_features, output_dim=50)) model.add(Dense(nb_features * 2)) model.add(Activation("tanh")) #model.add(Dropout(0.5)) model.add(Dense(1)) model.add(Activation("linear")) sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True) model.compile(loss='mean_squared_error', optimizer='sgd') early_stopping = EarlyStopping(monitor='val_loss', patience=5) model.fit(X_train, self.company.y_train, nb_epoch=50, validation_split=0.25, verbose = 1, callbacks=[early_stopping]) self.ann_preds = model.predict(X_test) """ #print self.ann_preds #print "Trained ANN Score: %r" % score # visualize #plot(model, to_file= '/ann-training/' + self.company.fin_file_name + '.png') return
model.add(Dense(1024, activation = 'relu')) model.add(Dense(512, activation = 'relu')) model.add(Dense(128, activation = 'relu')) model.add(Dense(8 * opts["color_patch_size" * opts["color_patch_size"]], W_regularizer = l2(0.01), b_regularizer = l2(0.01))) model.add(Activation("relu")) model.add(Dropout(0.5)) model.add(Dense(2 * opts["color_patch_size"] * opts["color_patch_size"], W_regularizer = l2(0.01), b_regularizer = l2(0.01))) print("Compiling model") sgd = SGD(lr = 10e-4, momentum = 0.9, decay = 10e-4) rms = RMSprop() #sgd = SGD() model.compile(loss = 'mean_squared_error', optimizer = sgd) #texture_model.compile(loss = 'mean_squared_error', optimizer = sgd) yaml_model = texture_model.to_yaml() open(model_path + model_file, "w").write(yaml_model) #deal with command line parameters if (len(sys.argv) > 1): if (sys.argv[1] == "train"): opts["train_flag"] = True elif (sys.argv[1] == "test"): opts["train_flag"] = False else: print("Wrong parameter") sys.exit if (opts["train_flag"]): print("Get random patches") [train_x_patches, train_x_pixel_patches, train_y_patches] = rand_patches(train_x, train_y, opts) # [train_x_patches, train_x_pixel_patches, train_y_patches] = split_test_data(train_x, train_y, opts)
class DenoisingAutoencoder(object): """ Neural network which implements a denoising auto-encoder. Inputs are convoluted before being fed into an encoding layer. From the encoding layer we learn to recover the original signal. """ def __init__(self, window_size, model_path=None, weight_path=None): self.num_filters = 8 self.window_size = window_size self.size = (window_size - 3) * self.num_filters if model_path is not None: self.load_model(model_path) else: self.initialize_model() if weight_path is not None: self.load_weights(weight_path) def initialize_model(self): """Initialize the network model.""" self.model = Sequential() self.model.add(Convolution1D(self.num_filters, 4, 'uniform', 'linear', border_mode='valid', subsample_length=1, input_dim=1, input_length=self.window_size)) self.model.add(Flatten()) self.model.add(Dense(output_dim=self.size, init='uniform', activation='relu')) self.model.add(Dense(128, 'uniform', 'relu')) self.model.add(Dense(self.size, 'uniform', 'relu')) self.model.add(Reshape(dims=(self.window_size - 3, self.num_filters))) self.model.add(Convolution1D(1, 4, 'uniform', 'linear', border_mode='valid', subsample_length=1, input_dim=1, input_length=self.size)) self.model.compile(loss='mean_squared_error', optimizer='rmsprop') def train(self, aggregate_power, device_power): """Train the network given the aggregate and device powers.""" self.model.fit(aggregate_power, device_power, batch_size=10, nb_epoch=1) def save_model(self, path): """Save the network model to the given path as yaml.""" yaml_string = self.model.to_yaml() path = os.path.abspath(path) with open(path, 'w') as fd: fd.write(yaml_string) def save_weights(self, path): """Save the network weights to the given path in HDF5.""" path = os.path.abspath(path) self.model.save_weights(path, overwrite=True) def load_model(self, model_path): """ Load the network model from the given path.""" model_path = os.path.abspath(model_path) with open(model_path, 'r') as fd: self.model = model_from_yaml(fd.read()) def load_weights(self, weight_path): """Load the network weights from the given path.""" weight_path = os.path.abspath(weight_path) self.model.load_weights(weight_path)
def train_lstm(n_symbols, embedding_weights, x_train, y_train, x_test, y_test): print('定义一个简单的keras模型...') #定义基本网络结构(输入维度100,输出维度3) model = Sequential() # or Graph or whatever # #添加嵌入层,只能作为第一层,将输入转化为向量 model.add( Embedding( output_dim=vocab_dim, #大于0的整数,代表全连接嵌入的维度 input_dim=n_symbols, #大或等于0的整数,字典长度,即输入数据最大下标+1 mask_zero= True, #布尔值,确定是否将输入中的‘0’看作是应该被忽略的‘填充’(padding)值,该参数在使用递归层处理变长输入时有用。 weights=[embedding_weights], #初始化权值的numpy arrays组成的list input_length=input_length)) # 输入序列的长度 #添加LSTM层,内部投影的维数和最终输出的维数为50,激活函数tanh model.add(LSTM(units=50, activation='tanh')) #添加GRU层 ''' model.add(GRU(units=50, activation='tanh', unroll=True, )) ''' #添加RNN层 # model.add(SimpleRNN(units=50, # activation='tanh', # unroll=True, # )) #采用50%的dropout,模型训练时随机让网络某些隐含层节点50%的权重不工作,避免过拟合(训练和测试数据的结果差别大) model.add(Dropout(0.5)) #Dense=>全连接层,输出维度=3(三分类,输出维度为3,sigmoid) model.add(Dense(3)) #最后一层用激活函数softmax model.add(Activation('softmax')) #二分类与多分类在前面的结构上都没有问题,就是需要改一下最后的全连接层, #因为此时有3分类,所以需要Dense(3),同时激活函数是softmax,如果是二分类就是dense(2)+sigmoid(激活函数) print('编译模型...') #loss-目标函数(categorical_crossentropy-多分类,binary_crossentropy-二分类) #optimizer-指定模型训练的优化器 model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) print("训练...") # batch_size=32 #用于训练一个固定迭代次数的模型 #x_train-训练数据,y_train-标签,batch_size-每次训练和梯度更新块的大小,epochs-迭代次数 #verbose-进度表示方式,0=不显示,1=显示进度条,2=只显示一个数据 model.fit(x_train, y_train, batch_size=batch_size, epochs=n_epoch, verbose=1) print("评估...") #展示模型在验证数据上的效果 #x_test-训练数据,y_test-标签,batch_size-更新块大小 score, acc = model.evaluate(x_test, y_test, batch_size=batch_size) #以YAML字符串的形式返回模型,不包括权重,只包括结构 yaml_string = model.to_yaml() with open('./lstm.yml', 'w') as outfile: outfile.write(yaml.dump(yaml_string, default_flow_style=True)) #将所有层的权值保存为HDF5文件 model.save_weights('./lstm.h5') print('Test score:', score) print('Test accuracy:', acc)
X_test = sequence.pad_sequences(X_test, maxlen=maxlen) model = Sequential() model.add(Embedding(max_features, 128)) model.add(LSTM(128, 128)) model.add(Dropout(0.5)) model.add(Dense(128, 1, W_regularizer='identity', b_constraint='maxnorm')) model.add(Activation('sigmoid')) model.get_config(verbose=1) ##################################### # save model w/o parameters to yaml # ##################################### yaml_no_params = model.to_yaml() no_param_model = model_from_yaml(yaml_no_params) no_param_model.get_config(verbose=1) ###################################### # save multi-branch sequential model # ###################################### seq = Sequential() seq.add(Merge([model, model], mode='sum')) seq.get_config(verbose=1) merge_yaml = seq.to_yaml() merge_model = model_from_yaml(merge_yaml) large_model = Sequential()
sample_weight=np.asarray(sample_weight) X_sample_weight,X_sample_weight_rest=train_test_split(sample_weight,test_size=0.1,random_state=47) sample_weight_val,sample_weight_test=train_test_split(X_sample_weight_rest,test_size=0.5,random_state=47) #X_sample_weight=np.concatenate((X_sample_weight,sample_weight_val)) #model.add(Reshape((100,100),input_shape=(10000,))) model.add(LSTM(60,return_sequences=True,input_shape=(timesteps, data_dim))) model.add(BatchNormalization()) model.add(Activation('relu')) model.add(Dropout(0.2)) model.add(TimeDistributedDense(28, activation='softmax')) model.compile(loss='categorical_crossentropy', optimizer='adam',sample_weight_mode="temporal") totaltrain=np.asarray(totaltrain) totallabel=np.asarray(totallabel) print len(x_val) x_train=np.asarray(x_train) x_test=np.asarray(x_test) x_val=np.asarray(x_val) y_train=np.asarray(y_train) y_val=np.asarray(y_val) y_test=np.asarray(y_test) print y_train.shape x_train=x_train[:,:100,:] y_train=y_train[:,:100,:] print y_train.shape early_stop = EarlyStopping(monitor='val_loss', patience=100, verbose=1) model.fit(x_train,y_train, callbacks=[early_stop],nb_epoch=300, sample_weight=X_sample_weight,batch_size=100,show_accuracy=True, validation_split=0.1) with open('yaml','w') as f: f.write(model.to_yaml()) model.save_weights('NERmode_weights.h5',overwrite=True)
class DeepNeuroBrain: def __init__(self, step="test"): """ Input : step : 'train' or 'test', depending if you want to fit the neural network or not at each game.""" self.name = "neuroBrain" self.computingTimes = [] self.U = np.array([0]).reshape(1,1) # evaluation function, output of the NN # Neural Net constants self.model = None self.createNeuralNetwork() self.gamma = 0.9 # discount factor self.epsilon = 0.1 # epsilon-greedy algorithm self.normalReward = -1 # reward in normal games (not winning or losing) self.winningReward = 100 self.losingReward = -100 self.step = step self.verbose = 0 # if 0, only print the value of U at each game def createNeuralNetwork(self): """ Create and compile a convolutional neural network with Keras """ print("Create the neural network...") self.model = Sequential() self.model.add(Convolution2D(32, 4, 4, border_mode='same', input_shape=(1,8, 8))) self.model.add(Convolution2D(16, 4, 4, border_mode='same', input_shape=(1,8, 8))) self.model.add(Flatten()) self.model.add(Dense(25, activation="relu", init='lecun_uniform')) self.model.add(Dense(1, init='lecun_uniform')) rms = RMSprop() self.model.compile(loss='mse', optimizer=rms) yaml_string = self.model.to_yaml() with open("model.yaml", "w") as f: f.write(yaml_string) print("[+] Neural network created") def play(self, gameState, timeLimit): possibleMoves = gameState.getStateMoveDict() if self.verbose: print("Authorized moves : ") for m in possibleMoves.values(): print(m.toPDN()) string = "" try: if self.step == "train": string = self.nextMoveTrain(gameState) else: string = self.nextMoveTest(gameState) move = Move.fromPDN(string) choice = gameState.doMove(move, inplace = False) if str(choice) not in possibleMoves.keys(): raise Exception except Exception: print(string+' is an invalid move !') raise return choice def getMinUR(self, gameState): """ Given an ennemi game state, compute the best move for him, ie with the worst U, and return the couple (U,reward) corresponding """ if not gameState.getStateMoveDict(): reward = self.getReward(gameState) # either win or draw, because the ennemi (gameState) has no possible move return (reward,reward) possibleMoves = list(gameState.getStateMoveDict().values()) minU = INFINI for action in possibleMoves: newGameState = gameState.doMove(action) reward = self.getReward(newGameState) if not newGameState.getStateMoveDict(): return (reward,reward) # either lose or draw if reward + self.gamma * self.predict(newGameState) < minU: minU = reward + self.gamma * self.predict(newGameState) minReward = reward return (minU,self.normalReward) def getListNextUR(self, gameState, possibleMoves): """ Given our gameState and a list of possibleMoves, return a list of the U functions and rewards corresponding to all our moves. To compute the U function after a move, as the new board is ennemi, we consider the U of his best move (by calling the function getMinUR). It's a sort of deep-2 minMax. """ listU = [] listR = [] for action in possibleMoves: newGameState = gameState.doMove(action) newU, newR = self.getMinUR(newGameState) listU.append(newU) listR.append(newR) return (listU, listR) def nextMoveTrain(self, gameState): """ Reinforcement learning algorithm (TD(0)) with epsilon-greedy to chose the action Perform a sort of min-max with deep 2 to determine the best action (for each action, the eval function (called U) of the new game state is the eval function of the game state after the ennemi took his best move) The U function is the result of a neural network, and is updated after each move """ possibleMoves = list(gameState.getStateMoveDict().values()) U = self.predict(gameState) print ("U : " + str(U), end="") newU = [] if (random.random() < self.epsilon): #choose random action (epsilon-greedy part) print(" : random") action = possibleMoves[np.random.randint(0,len(possibleMoves))] newGameState = gameState.doMove(action) newU, reward = self.getMinUR(newGameState) # newU corresponds to the best move of the ennemi after we took the random action else: print("") newUR = self.getListNextUR(gameState, possibleMoves) # newUR = (listOfU, listOfReward) iBestMove = np.argmax(newUR[0]) # We take the best action (with the best U) if self.verbose: print("New UR : ", newUR) print("iBestMove : ", iBestMove) reward = newUR[1][iBestMove] newU = newUR[0][iBestMove] action = possibleMoves[iBestMove] if self.verbose: print("Action selected : " + str(action.toPDN())) if reward != self.normalReward: update = reward else: update = reward + self.gamma * newU # updated U, according to TD(0) algorithm y = np.array([update]).reshape(1,1) if self.verbose: print("Update : " + str(update)) print("Fitting...") self.fit(gameState, y) #time.sleep(0.04) return action.toPDN() def nextMoveTest(self, gameState): """ Same than nextMoveTrain, but without fitting with an update """ possibleMoves = list(gameState.getStateMoveDict().values()) U = self.predict(gameState) print ("U : " + str(U)) newUR = self.getListNextUR(gameState, possibleMoves) # newUR = (listOfU, listOfReward) iBestMove = np.argmax(newUR[0]) if self.verbose: print("New UR : ", newUR) print("iBestMove : ", iBestMove) reward = newUR[1][iBestMove] newU = newUR[0][iBestMove] action = possibleMoves[iBestMove] if self.verbose: print("Action selected : " + str(action.toPDN())) return action.toPDN() def getReward(self, gameState): if not gameState.getStateMoveDict(): hasWon = not gameState.isWhiteTurn if hasWon: return self.winningReward else: return -self.winningReward else: return self.normalReward def getInput(self,gameState): """ Turn the gameState into the format given to the input of the NN """ listCells = gameState.boardState.cells tInput = np.zeros((8,8)) nbrWhites, nbrBlacks, nbrKingsBlack, nbrKingsWhite = 0,0,0,0 iterCell = listCells.__iter__() for row in range(8): for col in range(8): if (row + col) % 2 == 1: cell = iterCell.__next__() if cell == Cell.empty: tInput[row,col] = 0 if cell == Cell.b: tInput[row,col] = -1 if cell == Cell.B: tInput[row,col] = -3 if cell == Cell.w: tInput[row,col] = 1 if cell == Cell.W: tInput[row,col] = 3 return tInput.reshape(1,1,8,8) def predict(self, gameState): return self.model.predict(self.getInput(gameState), batch_size=1) def fit(self, gameState, y): return self.model.fit(self.getInput(gameState), y, batch_size=1, nb_epoch=1, verbose=self.verbose) def saveWeights(self, filename='weights.h5'): self.model.save_weights(filename, overwrite=True) def loadWeights(self, filename='weights.h5'): self.model.load_weights(filename) def __str__(self): return self.name
train_freq = 10 #batch_size batch_size = 32 #Make the linear q network deep_q_net = DeepQNetworkDuellingDouble(q_network_online, q_network_target, preprocessor, memory, policy, gamma, target_update_freq, num_burn_in, train_freq, batch_size) #Define the loss function deep_q_net.compile(loss_func=mean_huber_loss) #Define the optimizer : Defaulted for now print("Created and compiled linear agent") #now run fit deep_q_net.fit(env, num_iterations=5000000, max_episode_length=1000) #save the models q_network_online_yaml = q_network_online.to_yaml() with open("q_network_online.yaml", "w") as yaml_file: yaml_file.write(q_network_online_yaml) q_network_target_yaml = q_network_target.to_yaml() with open("q_network_target.yaml", "w") as yaml_file: yaml_file.write(q_network_target_yaml) #save the weights q_network_online.save_weights("q_network_online.h5") q_network_target.save_weights("q_network_online.h5")
def test_sequential(): (X_train, y_train), (X_test, y_test) = _get_test_data() # TODO: factor out def data_generator(train): if train: max_batch_index = len(X_train) // batch_size else: max_batch_index = len(X_test) // batch_size i = 0 while 1: if train: yield (X_train[i * batch_size: (i + 1) * batch_size], y_train[i * batch_size: (i + 1) * batch_size]) else: yield (X_test[i * batch_size: (i + 1) * batch_size], y_test[i * batch_size: (i + 1) * batch_size]) i += 1 i = i % max_batch_index model = Sequential() model.add(Dense(nb_hidden, input_shape=(input_dim,))) model.add(Activation('relu')) model.add(Dense(nb_class)) model.add(Activation('softmax')) model.compile(loss='categorical_crossentropy', optimizer='rmsprop') model.summary() model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=True, verbose=1, validation_data=(X_test, y_test)) model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=False, verbose=2, validation_data=(X_test, y_test)) model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=True, verbose=2, validation_split=0.1) model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=False, verbose=1, validation_split=0.1) model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=0) model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=1, shuffle=False) model.train_on_batch(X_train[:32], y_train[:32]) gen_loss = model.evaluate_generator(data_generator(True), 256, verbose=0) assert(gen_loss < 0.8) loss = model.evaluate(X_test, y_test, verbose=0) assert(loss < 0.8) model.predict(X_test, verbose=0) model.predict_classes(X_test, verbose=0) model.predict_proba(X_test, verbose=0) model.get_config(verbose=0) fname = 'test_sequential_temp.h5' model.save_weights(fname, overwrite=True) model = Sequential() model.add(Dense(nb_hidden, input_shape=(input_dim,))) model.add(Activation('relu')) model.add(Dense(nb_class)) model.add(Activation('softmax')) model.compile(loss='categorical_crossentropy', optimizer='rmsprop') model.load_weights(fname) os.remove(fname) nloss = model.evaluate(X_test, y_test, verbose=0) assert(loss == nloss) # test json serialization json_data = model.to_json() model = model_from_json(json_data) # test yaml serialization yaml_data = model.to_yaml() model = model_from_yaml(yaml_data)
X.append(util.sentence2array(line, N_FEATURES)) y.append(label) return (X, y) if __name__ == '__main__': (x, y) = vectorize("negative.txt", 0) (xx, yy) = vectorize("positive.txt", 1) x += xx y += yy x = np.array(x) y = np.array(y) (X_train, X_test, y_train, y_test) = train_test_split(x, y, test_size=0.3) X_train = sequence.pad_sequences(X_train, maxlen=MAX_LEN) X_test = sequence.pad_sequences(X_test, maxlen=MAX_LEN) model = Sequential() model.add(Embedding(N_FEATURES, EMBEDDING_OUT_DIM, input_length=MAX_LEN)) model.add(LSTM(LSTM_UNITS)) model.add(Dropout(DROPOUT_RATE)) model.add(Dense(1)) model.add(Activation('sigmoid')) model.compile(loss='binary_crossentropy', optimizer='adam', class_mode='binary', metrics=['accuracy']) model.fit(X_train, y_train, batch_size=BATCH, nb_epoch=EPOCH, validation_data=(X_test, y_test)) model.evaluate(X_test, y_test, batch_size=BATCH) open('sentiment_model.yaml', 'w').write(model.to_yaml()) model.save_weights('sentiment_weights.hdf5')
model.add(Dense(512)) model.add(Activation('relu')) model.add(Dropout(0.5)) model.add(Dense(nb_classes)) model.add(Activation('softmax')) model.compile(loss='categorical_crossentropy', optimizer='adam') # Initialize Elephas Spark ML Estimator adagrad = elephas_optimizers.Adagrad() estimator = ElephasEstimator() estimator.setFeaturesCol("scaled_features") estimator.setLabelCol("index_category") estimator.set_keras_model_config(model.to_yaml()) estimator.set_optimizer_config(adagrad.get_config()) estimator.set_nb_epoch(10) estimator.set_batch_size(128) estimator.set_num_workers(4) estimator.set_verbosity(0) estimator.set_validation_split(0.15) estimator.set_categorical_labels(True) estimator.set_nb_classes(nb_classes) # Fitting a model returns a Transformer pipeline = Pipeline(stages=[string_indexer, scaler, estimator]) fitted_pipeline = pipeline.fit(train_df) from pyspark.mllib.evaluation import MulticlassMetrics # Evaluate Spark model
class Mlp(): def __init__(self): self.model = Sequential([ Dense(2, input_shape=(2, ), activation="relu", kernel_constraint=max_norm(1.)), Dense(6, activation="relu", kernel_constraint=max_norm(1.)), Dense(2, activation="softmax", kernel_constraint=max_norm(1.)) ]) #print(self.model.summary()) self.model.compile(Adam(lr=0.0001), loss='sparse_categorical_crossentropy', metrics=['accuracy']) #w = self.model.layers[0].get_weights() #print('------------w-----------------') #w = self.model.get_weights() #print(w) #print(w) def set_train(self, train, labels): print(train) scaler = MinMaxScaler(feature_range=(0, 1)) scaled_train = scaler.fit_transform((train)) self.scaled_train = scaled_train self.train_labels = labels def train(self): self.model.fit(self.scaled_train, self.train_labels, batch_size=1, epochs=1, shuffle=True, verbose=2) w = self.model.layers[0].get_weights() print(w) new_w = [] new_w.append(np.array([[1, 2], [3, 4]], float)) new_w.append(w[1]) self.model.layers[0].set_weights(new_w) w = self.model.layers[0].get_weights() print(w) def predict(self, test): #predictions = self.model.predict(self.scaled_train, batch_size=10, verbose=0) scaler = MinMaxScaler(feature_range=(0, 1)) test = np.array([test[0][0], test[0][1]], dtype=np.float) test = test.reshape(1, -1) test = np.vstack((test, [-500, -300])) test = np.vstack((test, [500, 300])) #print(test) scaled_test = scaler.fit_transform(test) #print('parameterssss scaled', scaled_test[0].reshape(1,-1)) rounded_predictions = self.model.predict_classes( scaled_test[0].reshape(1, -1), batch_size=1, verbose=0) #rounded_predictions = self.model.predict(scaled_test, batch_size=10, verbose=0) #print('rounded', rounded_predictions) #if(rounded_predictions[0][0] > rounded_predictions[0][1]): # rounded_predictions = [0] #else: # rounded_predictions = [1] return rounded_predictions def weight_mutation(self): number_of_layers = int(len(self.model.get_weights()) / 2) layer_len = [2] layer_len += [ len(self.model.layers[i].get_weights()[0][0]) for i in range(number_of_layers) ] #print(layer_len) #print(self.model.layers[0].get_weights()) #print(self.model.layers[0].get_weights()[0][0][0]) for layer in range(1, number_of_layers + 1): for i in range( int(layer_len[layer] * layer_len[layer - 1] * MUTATION_RANGE)): var = random.randint(1, 100) if (var <= 100 * MUTATION_CHANCE): new_weigth = self.model.layers[layer - 1].get_weights() index_i = random.randint(0, layer_len[layer - 1] - 1) index_j = random.randint(0, layer_len[layer] - 1) #print('layer', layer, layer_len[layer]-1, layer_len[layer-1]-1) value = random.uniform(-1, 1) #print(new_weigth) new_weigth[0][index_i][index_j] = value self.model.layers[layer - 1].set_weights(new_weigth) self.bias_mutation(layer - 1, layer_len[1:]) def bias_mutation(self, layer, layer_len): for i in range(int(layer_len[layer] * MUTATION_RANGE)): var = random.randint(1, 100) if (var <= 100 * MUTATION_BIAS_CHANCE): new_weigth = self.model.layers[layer].get_weights() index = random.randint(0, layer_len[layer] - 1) value = random.uniform(-1, 1) new_weigth[1][index] = value self.model.layers[layer].set_weights(new_weigth) #print('layer 2', self.model.layers[3].get_weights()) def crossover(self, mlp): number_of_layers = int(len(self.model.get_weights()) / 2) layer_len = [2] layer_len += [ len(self.model.layers[i].get_weights()[0][0]) for i in range(number_of_layers) ] for layer in range(1, number_of_layers + 1): for i in range( int(layer_len[layer] * layer_len[layer - 1] * CROSSOVER_RANGE)): var = random.randint(1, 100) if (var <= 100 * CROSSOVER_CHANCE): new_weigth = self.model.layers[layer - 1].get_weights() parent_weight = mlp.model.layers[layer - 1].get_weights() index_i = random.randint(0, layer_len[layer - 1] - 1) index_j = random.randint(0, layer_len[layer] - 1) new_weigth[0][index_i][index_j] = ( new_weigth[0][index_i][index_j] + parent_weight[0][index_i][index_j]) / 2 self.model.layers[layer - 1].set_weights(new_weigth) layerb = layer - 1 layer_lenb = layer_len[1:] for i in range(int(layer_lenb[layerb] * CROSSOVER_RANGE)): var = random.randint(1, 100) if (var <= 100 * CROSSOVER_BIAS_CHANCE): new_weigth = self.model.layers[layerb].get_weights() parent_weight = mlp.model.layers[layerb].get_weights() index = random.randint(0, layer_lenb[layerb] - 1) new_weigth[1][index] = (new_weigth[1][index] + parent_weight[1][index]) / 2 self.model.layers[layerb].set_weights(new_weigth) def save_mlp(self): # serialize model to YAML model_yaml = self.model.to_yaml() with open("model.yaml", "w") as yaml_file: yaml_file.write(model_yaml) # serialize weights to HDF5 self.model.save_weights("model.h5") def load_mlp(self): # load YAML and create model yaml_file = open('model.yaml', 'r') loaded_model_yaml = yaml_file.read() yaml_file.close() self.model = model_from_yaml(loaded_model_yaml) # load weights into new model self.model.load_weights("model.h5")
model.add(Activation('relu')) model.add(Dropout(0.2)) model.add(Dense(10)) model.add(Activation('softmax')) keras_loss = 'categorical_crossentropy' keras_optimizer = 'sgd' # Compile model #sgd = SGD(lr=0.01) model.compile(loss=keras_loss, optimizer=keras_optimizer, metrics=["accuracy"]) # Build RDD from numpy features and labels rdd = to_simple_rdd(sc, x_train, y_train) # Initialize SparkModel from Keras model and Spark context print(model.to_yaml()) adagrad = elephas_optimizers.Adagrad(lr=0.01) #sgd = elephas_optimizers.SGD(lr=1.0) spark_model = SparkModel(sc, model, keras_losss=keras_loss, keras_optimizer=keras_optimizer, optimizer=adagrad, frequency='batch', mode='asynchronous', num_workers=4) # Train Spark model spark_model.train(rdd, nb_epoch=nb_epoch, batch_size=batch_size, verbose=2, validation_split=0.1)
def train_cnn(self, input, output, test_input): # Add Distance Prior #input = add_dist_prior(input) print(input.shape) num_samples, num_channels, num_rows, num_cols = input.shape print(output.shape) output = output.reshape(1600,1,32,32) # Configurations batch_size = 100 num_epoch = 3000 model = Sequential() model.add(ZeroPadding2D((2,2), \ input_shape=(num_channels,num_rows,num_cols) )) model.add(Convolution2D(64,5,5, \ subsample=(1,1), \ activation='relu')) model.add(ZeroPadding2D((2,2))) model.add(Convolution2D(64,5,5, \ subsample=(1,1), \ activation='relu')) #model.add(MaxPooling2D((2,2))) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(64,4,4, \ subsample=(2,2), \ activation='relu')) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(128,3,3, \ subsample=(1,1), \ activation='relu')) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(128,3,3, \ subsample=(1,1), \ activation='relu')) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(128,3,3, \ subsample=(1,1), \ activation='relu')) #model.add(MaxPooling2D((2,2))) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(128,4,4, \ subsample=(2,2), \ activation='relu')) ''' model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(256,3,3, \ subsample=(1,1), \ activation='relu')) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(256,3,3, \ subsample=(1,1), \ activation='relu')) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(256,3,3, \ subsample=(1,1), \ activation='relu')) model.add(MaxPooling2D((4,4))) ''' ''' model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(1024,3,3, \ subsample=(1,1), \ activation='relu')) model.add(Flatten()) model.add(Dense(512, \ activation='relu')) model.add(Dropout(0.25)) model.add(Dense(1024*4*4, \ activation='relu')) model.add(Dropout(0.25)) model.add(Reshape((1024,4,4))) ''' model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(128,3,3, \ subsample=(1,1), \ activation='relu')) model.add(ZeroPadding2D((9,9))) model.add(Convolution2D(64,3,3, \ subsample=(1,1), \ activation='relu')) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(64,3,3, \ subsample=(1,1), \ activation='relu')) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(64,3,3, \ subsample=(1,1), \ activation='relu')) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(64,3,3, \ subsample=(1,1), \ activation='relu')) ''' model.add(UpSampling2D((2,2))) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(512,3,3, \ subsample=(1,1), \ activation='relu')) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(512,3,3, \ subsample=(1,1), \ activation='relu')) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(512,3,3, \ subsample=(1,1), \ activation='relu')) model.add(Dropout(0.3)) model.add(UpSampling2D((2,2))) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(256,3,3, \ subsample=(1,1), \ activation='relu')) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(256,3,3, \ subsample=(1,1), \ activation='relu')) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(256,3,3, \ subsample=(1,1), \ activation='relu')) model.add(Dropout(0.3)) model.add(UpSampling2D((2,2))) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(128,3,3, \ subsample=(1,1), \ activation='relu')) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(128,3,3, \ subsample=(1,1), \ activation='relu')) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(128,3,3, \ subsample=(1,1), \ activation='relu')) model.add(Dropout(0.3)) ''' model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(32,3,3, \ subsample=(1,1), \ activation='relu')) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(16,3,3, \ subsample=(1,1), \ activation='relu')) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(4,3,3, \ subsample=(1,1), \ activation='relu')) model.add(ZeroPadding2D((1,1))) model.add(Convolution2D(1,3,3, \ subsample=(1,1), \ activation='relu')) # Compile sgd = SGD(lr=0.01, decay=0.0005, momentum=0.9, nesterov=False) model.compile( optimizer='sgd', \ loss='mean_squared_error' ) #early_stop = EarlyStopping(monitor='val_loss', patience=2) #early_stop = EarlyStopping(monitor='loss', patience=4) hist = model.fit(input, output, \ batch_size=batch_size, nb_epoch=num_epoch, verbose=1, \ shuffle=True, \ validation_split=0.1) #callbacks=[early_stop]) # TODO: move Prediction to a seperated func # Prediction eval = model.evaluate(input, output, batch_size=batch_size) print('eval', eval) predict = model.predict(test_input, batch_size=batch_size) #predict = model.predict(input, batch_size=batch_size) print('predict', predict) # Save model model.save('model.h5') model_json = model.to_json() open('model_architecture.json', 'w').write(model_json) model_yaml = model.to_yaml() open('model_architecture.yaml', 'w').write(model_yaml) model.save_weights('weights.h5') # Visualization ''' I1 = input print("I1 shape: ", I1.shape) print('layer 0: ', model.layers[0].get_config()) print l1f = T.function([model.layers[0].input], \ model.layers[1].output, allow_input_downcast=True) l1o = np.array(l1f(I1)) print('layer 1: ', model.layers[1].get_config()) print("l1o shape: ", l1o.shape) l1w = np.squeeze(model.layers[1].W.get_value(borrow=True)) # W1 = model.layers[1].get_weights()[0] # 0 is W, 1 is b print("l1w shape: ", l1w.shape) print l2f = T.function([model.layers[1].input], \ act1.output, allow_input_downcast=True) l2o = np.array(l2f(I1)) print('layer 2: ', model.layers[2].get_config()) print("l2o shape: ", l2o.shape) print f = plt.figure() plt.title('I1') nice_show(f,I1[0]) f = plt.figure() plt.title('l1w') nice_show(f,l1w[0]) f = plt.figure() plt.title('l2o') nice_show(f,l2o[0]) plt.show() ''' return predict
# define model model = Sequential() model.add(Embedding(vocab_size, args.embedding, input_length=args.length - 1)) model.add(LSTM(args.unit, dropout=args.dropout)) model.add(Dense(vocab_size, activation='softmax')) optimizer = select_optimizer(args.optimizer.lower()) model.compile(loss='categorical_crossentropy', optimizer=optimizer(clipnorm=args.clipnorm), metrics=[perplexity]) # fit network train_generator = DataGenerator(args.train_path, args.batch, args.length, vocab_size) valid_generator = DataGenerator(args.dev_path, args.batch, args.length, vocab_size) train_data_size = calc_data_size(args.train_path) dev_data_size = calc_data_size(args.dev_path) model.fit_generator(generator=train_generator, validation_data=valid_generator, steps_per_epoch=int(np.ceil(train_data_size / args.batch)), validation_steps=int(np.ceil(dev_data_size / args.batch)), epochs=args.epoch, use_multiprocessing=False, verbose=1) # Save model to file open(os.path.join(args.out, 'rnnlm.yaml'), 'w').write(model.to_yaml()) model.save_weights(os.path.join(args.out, 'rnnlm.hdf5'))
class CNN: def __init__(self, current_id, learning_rate=0.01, state_size=80, position_num=2, action_size=2, hidden_size=80): self.state_size = state_size self.position_num = position_num self.hidden_size = hidden_size self.action_size = action_size self.model = Sequential() # 1st convolutional layer block self.model.add( Conv2D(self.hidden_size, (3, 3), padding='same', activation='relu', input_shape=(self.state_size, self.position_num, 1))) self.model.add(BatchNormalization(name='Batch1')) self.model.add(MaxPooling2D(pool_size=(2, 1))) # 2nd convolutional layer block self.model.add( Conv2D(self.hidden_size, (3, 3), padding='same', activation='relu')) self.model.add(BatchNormalization(name='Batch2')) self.model.add(MaxPooling2D(pool_size=(2, 1))) # connected layer self.model.add(Flatten()) self.model.add(Dense(self.hidden_size, activation='relu')) self.model.add(BatchNormalization(name='Batch3')) self.model.add(Dense(action_size, activation='linear')) self.optimizer = Adam(lr=learning_rate) # 誤差を減らす学習方法はAdam self.model.compile(loss='mean_squared_error', optimizer=self.optimizer) self.id = current_id def replay(self, memory, batch_num, gamma, targetQN): state_minibatch = np.zeros( (batch_num, self.state_size, self.position_num)) y_minibatch = np.zeros((batch_num, self.action_size)) batch = memory.sample(batch_num) for i in range(batch_num): #[ seq..., action, reward, seq_new] s_j = batch[i, 0:self.state_size, :] a_j = int(batch[i, self.state_size, 0]) r_j = batch[i, self.state_size + 1, 0] s_dash_j = batch[i, self.state_size + 2:self.state_size * 2 + 2, :] y_j = self.model.predict( s_j.reshape(-1, self.state_size, self.position_num, 1))[0] y_j[a_j] = r_j + gamma * np.max( targetQN.model.predict( s_dash_j.reshape(-1, self.state_size, self.position_num, 1))) state_minibatch[i, :, :] = s_j y_minibatch[i, :] = y_j state_minibatch = state_minibatch.reshape(batch_num, self.state_size, self.position_num, 1) history = self.model.fit(state_minibatch, y_minibatch, batch_size=batch_num, verbose=0) return history.history['loss'][0] def load_model(self, name_y, name_w): f_model = 'C:/Users/flabexp/Documents/DQN/Experiment/data/October14151109/trained_model' print('load model') json_string = open(os.path.join(f_model, name_y)).read() self.model = model_from_json(json_string) self.model.compile(loss='mean_squared_error', optimizer=self.optimizer) self.model.load_weights(os.path.join(f_model, name_w)) def save_model(self, num_episode): f_model = '../data/' + self.id + '/trained_model' name_j = 'model%d.json' % num_episode name_y = 'model%d.yaml' % num_episode name_w = 'weights%d.hdf5' % num_episode json_string = self.model.to_json() yaml_string = self.model.to_yaml() print('save the architecture of a model') open(os.path.join(f_model, name_j), 'w').write(json_string) open(os.path.join(f_model, name_y), 'w').write(yaml_string) print('save weights') self.model.save_weights(os.path.join(f_model, name_w))
Y = dataset[:,8] # create model model = Sequential() model.add(Dense(12, input_dim=8, init='uniform', activation='relu')) model.add(Dense(8, init='uniform', activation='relu')) model.add(Dense(1, init='uniform', activation='sigmoid')) # Compile model model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) # Fit the model model.fit(X, Y, nb_epoch=150, batch_size=10, verbose=0) # evaluate the model scores = model.evaluate(X, Y, verbose=0) print("%s: %.2f%%" % (model.metrics_names[1], scores[1]*100)) # serialize model to YAML model_yaml = model.to_yaml() with open("model.yaml", "w") as yaml_file: yaml_file.write(model_yaml) # serialize weights to HDF5 model.save_weights("model.h5") print("Saved model to disk") # later... # load YAML and create model yaml_file = open('model.yaml', 'r') loaded_model_yaml = yaml_file.read() yaml_file.close() loaded_model = model_from_yaml(loaded_model_yaml) # load weights into new model loaded_model.load_weights("model.h5")
def main(hypes_file, data_dir, override): """Orchestrate.""" with open(hypes_file, 'r') as f: hypes = json.load(f) if 'training' not in hypes: hypes['training'] = {} if 'make_equal' not in hypes['training']: hypes['training']['make_equal'] = False base = os.path.dirname(hypes_file) model_file_path = os.path.join(base, '%s.yaml' % hypes['model']['name']) model_file_path = os.path.abspath(model_file_path) weights_file_path = os.path.join(base, '%s.hdf5' % hypes['model']['name']) weights_file_path = os.path.abspath(weights_file_path) if not os.path.isfile(model_file_path) or override: if not os.path.isfile(model_file_path): logging.info("Did not find '%s'. Start training...", model_file_path) else: logging.info("Override '%s'. Start training...", model_file_path) # Get data # x_files, y_files = inputs(hypes, None, 'train', data_dir) x_files, y_files = get_file_list(hypes, 'train') x_files, y_files = sklearn.utils.shuffle(x_files, y_files, random_state=0) x_train, y_train = get_traindata_single_file(hypes, x_files[0], y_files[0]) nb_features = x_train[0].shape[0] logging.info("Input gets %i features", nb_features) # Make model model = Sequential() model.add(Dense(64, input_dim=nb_features, init='uniform', activation='sigmoid')) model.add(Dropout(0.5)) model.add(Dense(64, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(1, activation='sigmoid')) model.compile(loss='binary_crossentropy', optimizer='adagrad', # rmsprop metrics=['accuracy']) generator = generate_training_data(hypes, x_files, y_files) t0 = time.time() sep = hypes['solver']['samples_per_epoch'] if True: class_weight = get_class_weight(hypes) logging.info("class_weights = %s", class_weight) model.fit_generator(generator, samples_per_epoch=sep, nb_epoch=hypes['solver']['epochs'], verbose=1, validation_data=(x_train, y_train), class_weight=class_weight) else: logging.info("Fit with .fit") x_train, y_train = inputs(hypes, None, 'train', data_dir) model.fit(x_train, y_train, batch_size=128, nb_epoch=1) t1 = time.time() print("Training Time: %0.4f" % (t1 - t0)) # save as YAML yaml_string = model.to_yaml() with open(model_file_path, 'w') as f: f.write(yaml_string) model.save_weights(weights_file_path) # Evaluate data = get_file_list(hypes, 'test') logging.info("Start segmentation") analyze.evaluate(hypes, data, data_dir, model, elements=[0, 1], get_segmentation=get_segmentation) else: logging.info("## Found '%s'.", model_file_path) with open(model_file_path) as f: yaml_string = f.read() model = model_from_yaml(yaml_string) model.load_weights(weights_file_path) model.compile(optimizer='adagrad', loss='binary_crossentropy') data = get_file_list(hypes, 'test') analyze.evaluate(hypes, data, data_dir, model, elements=[0, 1], get_segmentation=get_segmentation)
def test_sequential(in_tmpdir): (x_train, y_train), (x_test, y_test) = _get_test_data() # TODO: factor out def data_generator(x, y, batch_size=50): index_array = np.arange(len(x)) while 1: batches = make_batches(len(x_test), batch_size) for batch_index, (batch_start, batch_end) in enumerate(batches): batch_ids = index_array[batch_start:batch_end] x_batch = x[batch_ids] y_batch = y[batch_ids] yield (x_batch, y_batch) model = Sequential() model.add(Dense(num_hidden, input_shape=(input_dim,))) model.add(Activation('relu')) model.add(Dense(num_classes)) model.add(Activation('softmax')) model.compile(loss='categorical_crossentropy', optimizer='rmsprop') model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=1, validation_data=(x_test, y_test)) model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=2, validation_split=0.1) model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=0) model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=1, shuffle=False) model.train_on_batch(x_train[:32], y_train[:32]) loss = model.evaluate(x_test, y_test) prediction = model.predict_generator(data_generator(x_test, y_test), 1, max_queue_size=2, verbose=1) gen_loss = model.evaluate_generator(data_generator(x_test, y_test, 50), 1, max_queue_size=2) pred_loss = K.eval(K.mean(losses.get(model.loss)(K.variable(y_test), K.variable(prediction)))) assert(np.isclose(pred_loss, loss)) assert(np.isclose(gen_loss, loss)) model.predict(x_test, verbose=0) model.predict_classes(x_test, verbose=0) model.predict_proba(x_test, verbose=0) fname = 'test_sequential_temp.h5' model.save_weights(fname, overwrite=True) model = Sequential() model.add(Dense(num_hidden, input_shape=(input_dim,))) model.add(Activation('relu')) model.add(Dense(num_classes)) model.add(Activation('softmax')) model.compile(loss='categorical_crossentropy', optimizer='rmsprop') model.load_weights(fname) os.remove(fname) nloss = model.evaluate(x_test, y_test, verbose=0) assert(loss == nloss) # Test serialization config = model.get_config() assert 'name' in config new_model = Sequential.from_config(config) assert new_model.weights # Model should be built. model.summary() json_str = model.to_json() model_from_json(json_str) yaml_str = model.to_yaml() model_from_yaml(yaml_str)
layer.trainable = False merged = Merge([ model1, model2a, model2b, model3, model4, model5, model6, model7, model8, model9, model10 ], mode='concat', name="merged") final_model = Sequential() final_model.add(merged) final_model.add(Dense(len(set(classes)), activation='softmax')) final_model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy']) start = time.time() finalHistory = final_model.fit([ trainDescription, trainDomain, trainTld, trainLocation, trainSource, trainTexts, trainUserName, trainTZ, trainUtc, trainUserLang, trainCreatedAt ], classes, nb_epoch=nb_epoch, batch_size=batch_size, verbose=1) end = time.time() print("final_model finished after " + str(end - start)) model_yaml = final_model.to_yaml() with open(modelPath + "finalmodel2.yaml", "w") as yaml_file: yaml_file.write(model_yaml) final_model.save_weights(modelPath + 'finalmodelWeight2.h5')