From f29c55c0e1e92c2b3410f262227fd35ffceb13a9 Mon Sep 17 00:00:00 2001 From: jakubknczny Date: Fri, 14 May 2021 21:52:14 +0200 Subject: [PATCH] add scripts for 6 --- lab5/create/Jenkinsfile | 37 +++++++++++++++++++++++++++++++++++ lab5/create/create_dataset.py | 1 + lab5/eval/eval.py | 22 ++++++++------------- lab5/train/train.py | 27 ++++++++++++------------- 4 files changed, 59 insertions(+), 28 deletions(-) create mode 100644 lab5/create/Jenkinsfile diff --git a/lab5/create/Jenkinsfile b/lab5/create/Jenkinsfile new file mode 100644 index 0000000..5c103cf --- /dev/null +++ b/lab5/create/Jenkinsfile @@ -0,0 +1,37 @@ +pipeline { + agent none + stages { + stage('copy files') { + agent any + steps { + sh ''' + cp ./lab5/script.sh . + cp ./lab5/create_dataset.py . + cp ./lab5/Dockerfile . + cp ./lab5/requirements.txt . + ''' + } + } + stage('docker') { + agent { + dockerfile true + } + stages { + stage('script') { + steps { + sh ''' + chmod +x script.sh + ./script.sh''' + } + } + stage('archive artifact') { + steps { + archiveArtifacts 'train.csv' + archiveArtifacts 'test.csv' + archiveArtifacts 'valid.csv' + } + } + } + } + } +} diff --git a/lab5/create/create_dataset.py b/lab5/create/create_dataset.py index e600bcd..45d451c 100644 --- a/lab5/create/create_dataset.py +++ b/lab5/create/create_dataset.py @@ -33,3 +33,4 @@ valid = pd.concat([X_valid, Y_valid], axis=1) train.to_csv('train.cs', index_col = False) test.to_csv('test.csv', index_col = False) valid.to_csv('valid.csv', index_col = False) + diff --git a/lab5/eval/eval.py b/lab5/eval/eval.py index 57a1545..5a20969 100644 --- a/lab5/eval/eval.py +++ b/lab5/eval/eval.py @@ -1,23 +1,17 @@ +import csv import pandas as pd -import numpy as np import tensorflow as tf -def onezero(label): - return 0 if label == 'unstable' else 1 - X_test = pd.read_csv('test.csv') + Y_test = X_test.pop('stabf') +Y_test = pd.get_dummies(Y_test) -Y_test_one_zero = [onezero(x) for x in Y_test] -Y_test_onehot = np.eye(2)[Y_test_one_zero] +model = tf.keras.models.load_model('grid-stability-dense.h5') +results = model.evaluate(X_test, Y_test, batch_size=64) -model = tf.keras.models.load_model('grid_stability.h5') - -results = model.evaluate(X_test, Y_test_onehot, batch_size=64) - -f = open('eval.csv', 'a+') - -f.write(results[0], ',') -f.write(results[1], ',') +with open('eval.csv', 'a', newline='') as fp: + wr = csv.writer(fp, dialect='excel') + wr.writerow(results) diff --git a/lab5/train/train.py b/lab5/train/train.py index 686b0bf..408a7db 100644 --- a/lab5/train/train.py +++ b/lab5/train/train.py @@ -1,36 +1,35 @@ -import numpy as np import pandas as pd import tensorflow as tf from tensorflow.keras import layers -def onezero(label): - return 0 if label == 'unstable' else 1 - - X_train = pd.read_csv('train.csv') X_test = pd.read_csv('test.csv') +X_valid = pd.read_csv('valid.csv') Y_train = X_train.pop('stabf') +Y_train = pd.get_dummies(Y_train) + Y_test = X_test.pop('stabf') +Y_test = pd.get_dummies(Y_test) -Y_train_one_zero = [onezero(x) for x in Y_train] -Y_train_onehot = np.eye(2)[Y_train_one_zero] - -Y_test_one_zero = [onezero(x) for x in Y_test] -Y_test_onehot = np.eye(2)[Y_test_one_zero] +Y_valid = X_valid.pop('stabf') +Y_valid = pd.get_dummies(Y_valid) model = tf.keras.Sequential([ layers.Input(shape=(12,)), layers.Dense(32), layers.Dense(16), - layers.Dense(2, activation='softmax')]) + layers.Dense(2, activation='softmax') +]) model.compile( loss=tf.losses.BinaryCrossentropy(), optimizer=tf.optimizers.Adam(), metrics=[tf.keras.metrics.BinaryAccuracy()]) -history = model.fit(tf.convert_to_tensor(X_train, np.float32), - Y_train_onehot, epochs=5) -model.save('grid_stability.h5') +history = model.fit(tf.convert_to_tensor(X_train, np.float32), + Y_train, epochs=2, validation_data=(X_valid, Y_valid)) + +model.save('grid-stability-dense.h5') +