-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathexperimentNetTF.py
More file actions
129 lines (83 loc) · 3.36 KB
/
experimentNetTF.py
File metadata and controls
129 lines (83 loc) · 3.36 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import tensorflow as tf
import numpy as np
import os
import shutil
from functools import wraps
def callOnce(inputFunc):
attribute = "_cache_" + inputFunc.__name__
@property
@wraps(inputFunc)
def checkAttribute(self):
if not hasattr(self, attribute):
setattr(self, attribute, inputFunc(self))
return getattr(self, attribute)
return checkAttribute
class ExperimentNetTF:
def __init__(self, shape, learningRate):
self.shape = shape
self.x = tf.placeholder(tf.float32, shape=[None, self.shape[0]], name="InputData")
self.y = tf.placeholder(tf.float32, shape=[None, self.shape[-1]], name="LabelData")
self.weights = self._getInitWeights()
self.logDir = "./log/experiment"
shutil.rmtree(self.logDir)
os.makedirs(self.logDir)
self.learningRate = learningRate
self.summaryWriter = tf.summary.FileWriter(self.logDir, graph=tf.get_default_graph())
self.sess = tf.Session()
self.sess.run(tf.global_variables_initializer())
self.output
self.optimizer
self.loss
tf.summary.scalar("loss", self.loss)
self.mergedSummary = tf.summary.merge_all()
def _getInitWeights(self):
return [tf.Variable(tf.truncated_normal([fromLayer, toLayer], stddev=0.1, name="Weight{}".format(i))) for i, (fromLayer, toLayer) in enumerate(zip(self.shape[:-1], self.shape[1:]))]
def train(self, datasetPath, epochs=1):
costMeanList = []
for epoch in range(epochs):
print(f"Epoch {epoch + 1}")
with open(datasetPath, "r") as ds:
costList = []
for i, line in enumerate(ds):
lineEntities = np.array([float(i) for i in line.split(",")], dtype=np.float128)
inputs = np.reshape(lineEntities[:3], (1, 3))
labels = np.reshape(np.divide(lineEntities[3:], 25), (1, 1))
# inputs = np.reshape(lineEntities[:2], (1, 2))
# labels = np.reshape(lineEntities[2:], (1, 1))
_, loss, summary = self.sess.run([self.optimizer, self.loss, self.mergedSummary], {self.x: inputs, self.y: labels})
costList.append(loss)
self.summaryWriter.add_summary(summary, epoch * 1000 + epoch + i)
tempList = np.array(costList)
costMeanList.append(np.mean(tempList))
addListSummary = tf.Summary()
addListSummary.value.add(tag="MeanLoss", simple_value=np.mean(tempList))
self.summaryWriter.add_summary(addListSummary, epoch)
self.summaryWriter.flush()
self.saveTrainingData("./experimentSave/test.txt", costMeanList)
def getPrediction(self, xData):
return self.sess.run(self.output, {self.x: xData})
@callOnce
def output(self):
layerInput = self.x
for weight in self.weights:
layerInput = tf.math.tanh(tf.matmul(layerInput, weight))
return layerInput
@callOnce
def loss(self):
return tf.reduce_mean(tf.square(self.y - self.output))
# return tf.square(self.y - self.output)
@callOnce
def optimizer(self):
return tf.train.GradientDescentOptimizer(self.learningRate).minimize(self.loss)
def saveTrainingData(self, filePath, lossList):
file = open(filePath, "a")
for loss in lossList:
file.write(str(loss) + "\n")
file.close()
def doSave(self, step):
savePath = self.saver.save(self.sess, os.path.join(self.savePath, "model"), global_step = step)
print("Saved current model to {}".format(savePath))
if __name__ == '__main__':
net = ExperimentNetTF([3, 10, 1], learningRate=0.0005)
net.train("simulation/dataset/trackMaster1k.txt", epochs=10)
# net.train("simulation/dataset/testAnd.txt", epochs=100)