-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtraining_with_tensorflow.py
158 lines (131 loc) · 5.15 KB
/
training_with_tensorflow.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
from tensorflow.keras.models import Sequential
from tensorflow.keras import layers
import numpy as np
def genarate(symbols, max_number, max_sample, max_random_data_length):
return Data_generation(symbols, max_number, max_sample
, max_random_data_length).process()
def return_format():
return Data_generation.Random_data
def test_preprocess(x, symbols):
int_data = dict((i, j) for i, j in enumerate(symbols))
temp = []
for value in x:
string = int_data[np.argmax(value)]
temp.append(string)
return ''.join(temp)
def max_length(max_number):
return len(2 * str(max_number)) + 1
class Data_generation():
Random_data = None
def __init__(self, symbols, max_number, max_sample, max_random_data_length):
self.symbols = symbols
self.max_number = max_number
self.max_sample = max_sample
self.max_random_data_length = max_random_data_length
def random_generate(self):
random_labels = []
random_data = []
for _ in range(max_sample):
num1 = np.random.randint(1, max_number)
num2 = np.random.randint(1, max_number)
sum = str(num1 + num2)
if len(sum) < len(str(max_number)):
sum += ''.join(' ' for _ in range(len(str(max_number)) - len(sum)))
data_gen = str(num1) + "+" + str(num2)
if len(data_gen) < max_random_data_length:
data_gen += ''.join(' ' for _ in range(max_random_data_length - len(data_gen)))
random_data.append(data_gen)
random_labels.append(sum)
Data_generation.Random_data = random_data
return random_data, random_labels
def encode(self, x, y):
encoded_data = []
encoded_labels = []
for operations in x:
int_data = [symbols.index(value) for value in operations]
encoded_data.append(int_data)
for number in y:
int_label = [symbols.index(value) for value in number]
encoded_labels.append(int_label)
return encoded_data, encoded_labels
def one_hot(self, x, y):
one_hot_label = []
one_hot_data = []
for value in x:
temp = []
for i, j in enumerate(value):
zeros = np.zeros(len(self.symbols))
zeros[j] = 1
temp.append(zeros)
one_hot_data.append(temp)
for value in y:
temp = []
for i, j in enumerate(value):
zeros = np.zeros(len(self.symbols))
zeros[j] = 1.
temp.append(zeros)
one_hot_label.append(temp)
x = np.array(one_hot_data)
y = np.array(one_hot_label)
return x, y
def process(self):
x, y = self.random_generate()
x, y = self.encode(x, y)
x, y = self.one_hot(x, y)
return x, y
symbols = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ' ', '+']
max_number = 100
max_sample = 40000
max_random_data_length = max_length(max_number)
n_times_repeat_vector = int(np.log10(max_number) + 1)
model = Sequential()
model.add(layers.LSTM(100, input_shape=(max_random_data_length, len(symbols))))
model.add(layers.RepeatVector(n_times_repeat_vector))
model.add(layers.LSTM(50, return_sequences=True))
model.add(layers.TimeDistributed(layers.Dense(len(symbols), activation='softmax')))
model.compile(
loss="categorical_crossentropy",
optimizer="adam",
metrics=["acc"]
)
epochs = 25
batch_size = 100
for i in range(epochs):
x, y = genarate(symbols, max_number, max_sample, max_random_data_length)
model.fit(
x,
y,
epochs=1,
batch_size=batch_size
)
if i % 4 == 0 and i != 0:
test_x, _ = genarate(symbols, max_number, max_sample, max_random_data_length)
test_format = return_format()
result = model.predict(test_x, batch_size=batch_size)
predict = [test_preprocess(x, symbols) for x in result]
temp = 0
for data in test_format:
data1, data2 = data.split("+")
if int(data1) + int(data2) == int(predict[temp]):
print("{}= {}".format(test_format[temp], predict[temp]), "(correct)")
else:
print("{}= {}".format(test_format[temp], predict[temp]), "(incorrect)",
"(correct) = {}".format(int(data1) + int(data2)))
temp += 1
if temp == 4:
break
x, _ = genarate(symbols, max_number, max_sample, max_random_data_length)
test_format = return_format()
result = model.predict(x, batch_size=batch_size)
predict = [test_preprocess(x, symbols) for x in result]
i = 0
for data in test_format:
data1, data2 = data.split("+")
if int(data1) + int(data2) == int(predict[i]):
print("{}= {}".format(test_format[i], predict[i]), "(correct)")
else:
print("{}= {}".format(test_format[i], predict[i]), "(incorrect)",
"(correct) = {}".format(int(data1) + int(data2)))
i += 1
if i == 10:
break