-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathneuralnetwork.py
executable file
·85 lines (71 loc) · 2.85 KB
/
neuralnetwork.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
import numpy as np
import os.path
import tensorflow as tf
from tensorflow import keras
def build_model():
model = keras.Sequential([
keras.layers.Dense(64, activation=tf.nn.relu,
input_shape=(23,)),
keras.layers.Dense(64, activation=tf.nn.relu),
keras.layers.Dense(64, activation=tf.nn.relu),
keras.layers.Dense(23)
])
#optimizer = tf.train.RMSPropOptimizer(0.001)
optimizer = tf.train.AdamOptimizer()
model.compile(loss='mse',
optimizer=optimizer,
metrics=['mae'])
return model
def main():
original_data=np.loadtxt("./log/state_transition.log",delimiter=',')
data=original_data[:900000]
train_input=data[::2]
train_output=data[1::2]
print(data.shape)
print(train_input.shape,train_output.shape)
order = np.argsort(np.random.random(train_output.shape[0]))
train_input=train_input[order]
train_output=train_output[order]
print(train_input.shape,train_output.shape)
test_data=original_data[900000:]
test_input=test_data[::2]
test_output=test_data[1::2]
print(test_input.shape,test_output.shape)
model = build_model()
model.summary()
class PrintDot(keras.callbacks.Callback):
def on_epoch_end(self,epoch,logs):
if epoch % 100 == 0: print('')
print('.', end='')
EPOCHS = 2000#500
early_stop = keras.callbacks.EarlyStopping(monitor='val_loss', patience=200)
# Store training stats
history = model.fit(train_input, train_output, epochs=EPOCHS,
validation_split=0.2, verbose=0,
callbacks=[early_stop,PrintDot()])
import matplotlib.pyplot as plt
def plot_history(history):
plt.figure()
plt.xlabel('Epoch')
plt.ylabel('Mean Abs Error ')
plt.plot(history.epoch, np.array(history.history['mean_absolute_error']),
label='Train Loss')
plt.plot(history.epoch, np.array(history.history['val_mean_absolute_error']),
label = 'Val loss')
plt.legend()
plt.ylim([0,5])
plt.show()
[loss, mae] = model.evaluate(train_input, train_output, verbose=0)
print("Testing set Mean Abs Error: {:7.2f}".format(mae))
test_predictions = model.predict(test_input)
print(np.mean(abs(test_predictions-test_output)))
#print(sum(abs(test_predictions-test_output))/len(test_input))
plot_history(history)
f_model = './model'
json_string=model.to_json()
open(os.path.join(f_model,'battle_state_model_v2.json'),'w').write(json_string)
yaml_string=model.to_yaml()
open(os.path.join(f_model,'battle_state_model_v2.yaml'),'w').write(yaml_string)
model.save_weights(os.path.join(f_model,'battle_state_model_v2.hdf5'))
if __name__ == "__main__":
main()