This repository has been archived by the owner on Jan 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NN_unsupbyconf.py
113 lines (77 loc) · 3.33 KB
/
NN_unsupbyconf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# -*- coding: utf-8 -*-
"""
Created on Thu Apr 29 17:14:38 2021
@author: jochem
"""
import datetime
import matplotlib.pyplot as plt
import numpy as np
#import pandas_datareader as pdr
from functions.functions import *
import timeit
import sys, os
import pickle
import copy
np.random.seed(1)
def out_dirnamer(Tk, epochs, steps, train_dirname):
output_dirname = str(Tk)+'_'+str(epochs)+'_'+str(steps)+'_'+str(train_dirname)
return output_dirname
if __name__ == "__main__":
dirname_train = "./Phase_Trans_ML/train_data"
dirname_test = "./Phase_Trans_ML/test_data"
train_dirname = sys.argv[1]
print("using: ", train_dirname, " as training input directory")
test_dirname = sys.argv[2]
print("using: ", test_dirname, " as test input directory")
epochs = int(sys.argv[3])
steps = int(sys.argv[4])
dims = int(sys.argv[5])
n = int(sys.argv[6])
Tk_dict = {2:2.27, 3:4.5, 4:6.68}
Tk = Tk_dict[dims]
out_dirname = os.path.join(out_dirnamer(Tk, epochs, steps, train_dirname))
os.mkdir(out_dirname)
number_of_training_data, train_data = unpickle_dir(os.path.join(dirname_train, train_dirname))
train_totdata = np.concatenate(train_data)
number_of_test_data, test_data = unpickle_dir(os.path.join(dirname_test, test_dirname))
test_totdata = np.concatenate(test_data)
size = n^dims
shape = [size,40,2]
Tks = list(np.linspace(0.001,2*Tk, steps))
trained_accuracies = []
test_accuracies = []
weights = [np.random.uniform(-0.1,0.1,(shape[i],shape[i+1])) for i in range(len(shape)-1)]
bias = [np.random.uniform(-1,1,(shape[i+1])) for i in range(len(shape)-1)]
for i in range(len(Tks)):
nn = NeuralNetwork(shape, copy.deepcopy(weights), copy.deepcopy(bias), train_totdata, number_of_training_data, Tks[i])
nn.Desired_Out()
foutmarge_ongeziene_data = []
weight_aanpas_groote = []
nfactor_lijst = []
nfactor = -3
for k in range(epochs):
if k%10 == 0:
foutmarge_ongezien = nn.test_ongeziene_data()
foutmarge_ongeziene_data.append(foutmarge_ongezien)
nfactor = learning_rate_function(foutmarge_ongezien)
nfactor_lijst.append(nfactor)
if k %100 == 0:
print("k = " + str(k))
print(nfactor)
print('error',nn.test_ongeziene_data())
print("Tk = " + str(Tks[i]) + " i = " + str(i))
nn.feedforward(normaal = 0)
nn.backprop(10 ** nfactor)
trained_accuracies.append(nn.test_ongeziene_data())
trained_w = nn.weight
trained_b = nn.bias
nn = NeuralNetwork(shape, trained_w, trained_b, test_totdata, number_of_training_data, Tks[i])
nn.Desired_Out()
test_accuracies.append(nn.test_ongeziene_data())
plt.scatter(Tks,trained_accuracies)
plt.savefig(os.path.join(out_dirname, 'trained_accuracies.png'))
plt.scatter(Tks,test_accuracies)
plt.savefig(os.path.join(out_dirname, 'test_accuracies.png'))
np.save(os.path.join(out_dirname, 'Tks'), Tks)
np.save(os.path.join(out_dirname, 'trained_accuracies'), trained_accuracies)
np.save(os.path.join(out_dirname, 'test_accuracies'), test_accuracies)