Commit eaf7e727 authored by Steffen Schotthöfer's avatar Steffen Schotthöfer
Browse files

added training script and data, tidied python folder


Former-commit-id: 74987622
parent 6274c943
31d7c3dcfd02e4babdeafbb295d20dc026e872a8
\ No newline at end of file
## Stuff to get started
'''
https://www.tensorflow.org/tutorials/quickstart/beginner
'''
import os
#os.environ["CUDA_VISIBLE_DEVICES"]="-1"
import tensorflow as tf
# Check available devices
visible_devices = tf.config.get_visible_devices(
device_type=None
)
print(visible_devices)
# Tell tensorflow, that we run on CPU
#sess = tf.Session(config=tf.ConfigProto(device_count={'GPU': 0}))
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10)
])
# model execution
predictions = model(x_train[:1]).numpy()
print(predictions)
print(tf.nn.softmax(predictions).numpy())
# define loss
loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
print(loss_fn(y_train[:1], predictions).numpy())
# compile model
model.compile(optimizer='adam',
loss=loss_fn,
metrics=['accuracy'])
# Train
model.fit(x_train, y_train, epochs=5)
model.evaluate(x_test, y_test, verbose=2)
probability_model = tf.keras.Sequential([
model,
tf.keras.layers.Softmax()
])
probability_model(x_test[:5])
'''
Author: Steffen Schotthöfer
Description: This is the first prototype of a network to use in the entropy optimizer
https://www.tensorflow.org/tutorials/keras/save_and_load
'''
import numpy as np # advanced math library
import random # for generating random numbers
import tensorflow as tf
# Define the NN model
# Build the network:
def create_model():
model = tf.keras.models.Sequential([
keras.layers.Dense(512, activation='relu', input_shape=(4,)),
keras.layers.Dropout(0.2),
keras.layers.Dense(512, activation='relu', input_shape=(512,)),
keras.layers.Dropout(0.2),
keras.layers.Dense(4)
])
model.summary()
model.compile(optimizer='adam',
loss=tf.losses.MeanSquaredError(),
metrics=['accuracy'])
return model
#Prepare the Data
def prepData(Data):
divide
into
input and output
data
idx = 0
xDataList = list()
yDataList = list()
data = dataFrameInput.values # numpy array
## Somehow t = 0 has an odd number of elements, just cut it out
for row in data:
if (row[2] > 0):
if (idx % 2 == 0):
xDataList.append(row)
else:
yDataList.append(row)
idx = idx + 1
# merge the lists
DataList = list()
for rowX, rowY in zip(xDataList, yDataList):
DataList.append([rowX, rowY])
# Shuffle data
random.shuffle(DataList)
DataArray = np.asarray(DataList)
print(DataArray.shape)
# Strip off header information, i.e. the first 3 cols
DataArraySlim = DataArray[:, :, 3:]
print(DataArraySlim.shape)
# split in train and test data (ratio 2:1)
DataTrain = DataArraySlim[:2 * int(DataArraySlim.shape[0] / 3)]
DataTest = DataArraySlim[2 * int(DataArraySlim.shape[0] / 3):]
# Split in x (input) and y (output) data
xDataTrain = DataTrain[:, 0, :]
yDataTrain = DataTrain[:, 1, :]
xDataTest = DataTest[:, 0, :]
yDataTest = DataTest[:, 1, :]
print(yDataTrain.shape)
print(yDataTest.shape)
return (xDataTrain,yDataTrain,xDataTest,yDataTest)
'''
Author: Steffen Schotthöfer
Description: This file is used to explore load a saved pre-trained keras network in tf2.0 format and employ it
'''
# Read test data
# Load the Pandas libraries with alias 'pd'
import pandas as pd
import random
import numpy as np
import os
import tensorflow as tf
from tensorflow import keras
# get some data
dataFrameInput = pd.read_csv("trainNN.csv")
# Preview the first 5 lines of the loaded data
dataFrameInput.head()
# divide into input and output data
idx = 0
xDataList = list()
yDataList = list()
data = dataFrameInput.values # numpy array
## Somehow t = 0 has an odd number of elements, just cut it out
for row in data:
if (row[2] > 0):
if (idx % 2 == 0):
xDataList.append(row)
else:
yDataList.append(row)
idx = idx + 1
# merge the lists
DataList = list()
for rowX, rowY in zip(xDataList, yDataList):
DataList.append([rowX, rowY])
# Shuffle data
random.shuffle(DataList)
DataArray = np.asarray(DataList)
print(DataArray.shape)
# Strip off header information, i.e. the first 3 cols
DataArraySlim = DataArray[:, :, 3:]
print(DataArraySlim.shape)
# split in train and test data (ratio 2:1)
DataTrain = DataArraySlim[:2 * int(DataArraySlim.shape[0] / 3)]
DataTest = DataArraySlim[2 * int(DataArraySlim.shape[0] / 3):]
# Split in x (input) and y (output) data
xDataTrain = DataTrain[:, 0, :]
yDataTrain = DataTrain[:, 1, :]
xDataTest = DataTest[:, 0, :]
yDataTest = DataTest[:, 1, :]
print(yDataTrain.shape)
print(yDataTest.shape)
# Load model
new_model = tf.keras.models.load_model('saved_model/my_model')
# Check its architecture
new_model.summary()
# Evaluate the restored model
loss, acc = new_model.evaluate(xDataTest, yDataTest, verbose=2)
print('Restored model, accuracy: {:5.2f}%'.format(100*acc))
print(new_model.predict(xDataTest).shape)
'''
Author: Steffen Schotthöfer
Description: This file is used to explore ways to load a saved pre-trained keras network in tf2.0 format
https://www.tensorflow.org/tutorials/keras/save_and_load
'''
import os
import tensorflow as tf
from tensorflow import keras
print(tf.version.VERSION)
# get training data
(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.mnist.load_data()
train_labels = train_labels[:1000]
test_labels = test_labels[:1000]
train_images = train_images[:1000].reshape(-1, 28 * 28) / 255.0
test_images = test_images[:1000].reshape(-1, 28 * 28) / 255.0
# Define a simple sequential model
def create_model():
model = tf.keras.models.Sequential([
keras.layers.Dense(512, activation='relu', input_shape=(784,)),
keras.layers.Dropout(0.2),
keras.layers.Dense(10)
])
model.summary()
model.compile(optimizer='adam',
loss=tf.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
return model
# Save the entire model
# Create and train a new model instance.
model = create_model()
model.fit(train_images, train_labels, epochs=5)
# Save the entire model as a SavedModel.
model.save('saved_model/my_model')
# Load model
new_model = tf.keras.models.load_model('saved_model/my_model')
# Check its architecture
new_model.summary()
# Evaluate the restored model
loss, acc = new_model.evaluate(test_images, test_labels, verbose=2)
print('Restored model, accuracy: {:5.2f}%'.format(100*acc))
print(new_model.predict(test_images).shape)
'''
# Create a basic model instance
model = create_model()
# Display the model's architecture
model.summary()
checkpoint_path = "training_1/cp.ckpt"
checkpoint_dir = os.path.dirname(checkpoint_path)
# Create a callback that saves the model's weights
cp_callback = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_path,
save_weights_only=True,
verbose=1)
# Train the model with the new callback
model.fit(train_images,
train_labels,
epochs=10,
validation_data=(test_images,test_labels),
callbacks=[cp_callback]) # Pass callback to training
# This may generate warnings related to saving the state of the optimizer.
# These warnings (and similar warnings throughout this notebook)
# are in place to discourage outdated usage, and can be ignored.
# Create a basic model instance
model = create_model()
# Evaluate the model
loss, acc = model.evaluate(test_images, test_labels, verbose=2)
print("Untrained model, accuracy: {:5.2f}%".format(100*acc))
# Loads the weights
model.load_weights(checkpoint_path)
# Re-evaluate the model
loss,acc = model.evaluate(test_images, test_labels, verbose=2)
print("Restored model, accuracy: {:5.2f}%".format(100*acc))
'''
'''
import os
import tempfile
from matplotlib import pyplot as plt
import numpy as np
import tensorflow as tf
tmpdir = tempfile.mkdtemp()
# load saved model
pathToFile = os.path.join(tmpdir, "EntropyNN")
loaded = tf.saved_model.load(pathToFile)
print(list(loaded.signatures.keys())) # ["serving_default"]
'''
\ No newline at end of file
This diff is collapsed.
'''
Author: Steffen Schotthöfer
Description: This file contains the Training routine for the entropy NN
'''
# Imports
import pandas as pd
import random # for generating random numbers
import numpy as np
import tensorflow as tf
from tensorflow import keras
import matplotlib.pyplot as plt # MATLAB like plotting routines
def main():
(xDataTrain,yDataTrain,xDataTest,yDataTest) = prepare_data("trainNN.csv")
print(yDataTrain.shape)
print(yDataTest.shape)
model = create_model()
cb_list = create_callbacks()
# Do the training
history = model.fit(xDataTrain, yDataTrain, validation_split=0.33, epochs=500, batch_size=1000, verbose=1)
# plot training results
fig, axs = plt.subplots(2)
print(history.history.keys())
axs[0].plot(history.history['accuracy'])
axs[0].plot(history.history['val_accuracy'])
axs[0].set_title('model accuracy')
axs[0].set_ylabel('accuracy')
axs[0].set_xlabel('epoch')
axs[0].legend(['train_acc', 'val_acc'], loc='upper left')
#axs[0].show()
# summarize history for loss
axs[1].plot(history.history['loss'])
axs[1].plot(history.history['val_loss'])
axs[1].set_title('model loss')
axs[1].set_ylabel('loss')
axs[1].set_xlabel('epoch')
axs[1].legend(['train_loss', 'val_loss'], loc='upper left')
#axs[1].show()
plt.show()
# Evaluation tests
score = model.evaluate(xDataTest, yDataTest)
print('Test score:', score[0])
print('Test accuracy:', score[1])
# save model
model.save('model_M4')
return 0
# Build the network:
def create_model():
model = tf.keras.models.Sequential([
keras.layers.Dense(256, activation='relu', input_shape=(4,)),
keras.layers.Dropout(0.2),
keras.layers.Dense(512, activation='relu', input_shape=(64,)),
keras.layers.Dropout(0.2),
keras.layers.Dense(256, activation='relu', input_shape=(256,)),
keras.layers.Dropout(0.2),
keras.layers.Dense(128, activation='relu', input_shape=(128,)),
keras.layers.Dropout(0.2),
keras.layers.Dense(4,)
])
model.summary()
model.compile(loss=tf.keras.losses.MeanSquaredError(), optimizer='adam', metrics=['accuracy'])
return model
#Create Callbacks
def create_callbacks():
cb_list = [keras.callbacks.EarlyStopping(
monitor='val_loss', min_delta=0, patience=0, verbose=0, mode='auto',
baseline=None, restore_best_weights=False
)]
return cb_list
def prepare_data(filename):
# reading csv file
dataFrameInput = pd.read_csv(filename) #outputs a dataframe object
idx = 0
xDataList = list()
yDataList = list()
data = dataFrameInput.values # numpy array
## Somehow t = 0 has an odd number of elements, just cut it out
for row in data:
if (row[2] > 0):
if (idx % 2 == 0):
xDataList.append(row)
else:
yDataList.append(row)
idx = idx + 1
# merge the lists
DataList = list()
for rowX, rowY in zip(xDataList, yDataList):
DataList.append([rowX, rowY])
# Shuffle data
random.shuffle(DataList)
DataArray = np.asarray(DataList)
#print(DataArray.shape)
# Strip off header information, i.e. the first 3 cols
DataArraySlim = DataArray[:, :, 3:]
#print(DataArraySlim.shape)
# split in train and test data (ratio 4:1)
DataTrain = DataArraySlim[:4 * int(DataArraySlim.shape[0] / 5)]
DataTest = DataArraySlim[4 * int(DataArraySlim.shape[0] / 5):]
# Split in x (input) and y (output) data
xDataTrain = DataTrain[:, 0, :]
yDataTrain = DataTrain[:, 1, :]
xDataTest = DataTest[:, 0, :]
yDataTest = DataTest[:, 1, :]
return (xDataTrain,yDataTrain,xDataTest,yDataTest)
if __name__ == '__main__':
main()
......@@ -232,7 +232,7 @@ void MNSolver::Solve() {
if( rank == 0 ) log->info( "{:03.8f} {:01.5e} {:01.5e}", _energies[idx_energy], dFlux, mass );
Save( idx_energy );
WriteNNTrainingData( idx_energy );
// WriteNNTrainingData( idx_energy );
// Update Solution
_sol = psiNew;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment