# compile model model.compile(loss='mse', optimizer='sgd') example_input = np.array([[1, 2, 3, 4, 5]]) if args.train: example_target = np.array([[1, 2]]) model.fit(example_input, example_target, epochs=5) keras_predictions = model.predict(example_input)[0] # save the weights model.save(weights_file) # convert h5 file to txt h5_to_txt(weights_file_name=weights_file, output_file_name=None) txt_to_h5(weights_file_name=weights_file.replace('.h5', '.txt'), output_file_name=None) cmd = ['../../build/bin/./test_keras', weights_file.replace('.h5', '.txt')] print('Keras predictions: ', keras_predictions) if os.path.exists(weights_file.replace('.h5', '_converted.h5')): model2 = load_model(weights_file.replace('.h5', '_converted.h5')) keras_predictions2 = model2.predict(example_input)[0] print('Other Keras predictions:', keras_predictions2) # run result = subprocess.check_output(cmd, stderr=subprocess.STDOUT) fortran_predictions = np.array([float(num) for num in result.strip().split()],
model_path = args.model_dir + file_name output_path = args.model_dir + file_name.replace('.h5', '.txt') print('Model path:', model_path) print('Output path:', output_path) # load keras model from h5 try: model = load_model(model_path) except: print('Failed...') continue keras_predictions = model.predict(sample_input)[0] # convert h5 file to txt h5_to_txt(weights_file_name=model_path, output_file_name=output_path) cmd = ['../../build/bin/./test_bulk', output_path] # print('Keras predictions:', keras_predictions) # run result = subprocess.check_output(cmd, stderr=subprocess.STDOUT) fortran_predictions = np.array( [float(num) for num in result.strip().split()], dtype=np.float32) # print('Fortran predictions:', fortran_predictions) # keras predictions must match neural fortran predictions assert np.allclose(keras_predictions, fortran_predictions, atol=1e-3)
output2 = Dense(1)(x) output3 = Dense(1)(x) # CREATE THE MODEL multi_output_model = Model(input,outputs=[output1, output2, output3]) multi_output_model.compile( loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy'] ) # SAVE TO FILE FOR PARSING multi_output_model.save('multi_output_model.h5') # CONVERT TO TXT convert_weights.h5_to_txt('multi_output_model.h5', 'single_output_model.txt') # CONVERT TO H5 convert_weights.txt_to_h5('single_output_model.txt', 'single_output_model.h5') single_output_model = load_model('single_output_model.h5') # GRAPHIC PLOT OF MODEL plot_model(multi_output_model, to_file='../../Figures/multi_output_model.png', show_shapes=True, show_layer_names=True) plot_model(single_output_model, to_file='../../Figures/single_output_model.png', show_shapes=True, show_layer_names=True) # TEST INPUT input = np.array( [[1,2,3,4,5]] ) # COMPARE PREDICTIONS FROM MULTI OUTPUT AND SINGLE OUTPUT MODELS
# plt.plot(t['pred'].tolist(), label='prediction') # plt.ylabel('output') # plt.legend() # plt.tight_layout() # plt.savefig("adim_regression.pdf", dpi=150) # plt.show() # plt.close() # ## Plot the chart #chart_regression(pred.flatten(), y_test) #model.save('model.sav') dump(model, 'model.sav') # model_name_h5 = 'NN.h5' model_name_txt = model_name_h5.replace('h5', 'txt') model.save(model_name_h5, include_optimizer=False) h5_to_txt(model_name_h5, model_name_txt) #plot_model(model, to_file="./model.png", show_shapes=True, show_layer_names=True) #tf.keras.utils.plot_model(model, to_file='model.png', show_shapes=True, show_layer_names=True) #new_model = tf.keras.models.load_model('model.sav') #new_model.summary() #keras_file = 'linear.h5' #keras.models.save_model(model, keras_file) # #converter = lite.TocoConverter.from_keras_model_file(keras_file) #tflite_model = converter.convert() #open('linear.tflite', 'wb').write(tflite_model)
y_test = keras.utils.to_categorical(y_test, num_classes) # SEQUENTIAL MODEL model = Sequential() model.add(Dense(512, activation='relu', input_shape=(784, ))) model.add(Dense(512, activation='relu')) model.add(Dense(num_classes, activation='softmax')) # FUNCTIONAL MODEL # input = x = Input(shape=(784,)) # x = Dense(512, activation='relu')(x) # x = Dense(512, activation='relu')(x) # x = Dense(num_classes, activation='softmax')(x) # # model = Model(inputs=input, outputs=x) model.compile(loss='categorical_crossentropy', optimizer=RMSprop(), metrics=['accuracy']) history = model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=1, validation_data=(x_test, y_test)) model.save(weights_file_name) h5_to_txt(weights_file_name, txt_file_name)