-
Notifications
You must be signed in to change notification settings - Fork 36
/
codeGeneration.py
196 lines (128 loc) · 4.19 KB
/
codeGeneration.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# coding: utf-8
# ### This notebook reads openssl .c source code and generates new source code
# #### References
# ##### https://www.tensorflow.org/tutorials/sequences/recurrent
# ##### https://machinelearningmastery.com/text-generation-lstm-recurrent-neural-networks-python-keras/
#
# ### Prerequisites install tensorflow and keras
# #### pip install tensorflow
# #### pip install keras
# In[1]:
import numpy
import os
import sys
import time
from keras.models import Sequential
from keras.layers import Activation
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import LSTM
from keras.utils import np_utils
# ### Tune these parameters
# In[2]:
SEQ_LENGTH = 100
EPOCHS = 10
BATCH_SIZE = 128
# ### Preprocessing
# #### read all files and concatenate into a single file train.txt
# #### Add tags "<start"> and "< eof >"
# In[3]:
PWD=os.getcwd()
print("PWD=" + str(PWD))
train_filename = PWD+"./train.txt"
readSize = 0
if (os.path.isfile(train_filename)):
os.remove(train_filename)
# read every 3rd file otherwise getting out of memory errors
i = 0
with open(train_filename, "w") as a:
for path, subdirs, files in os.walk(PWD+"/openssl-master"):
for filename in files:
if (filename.endswith(".c") and (i%3 == 0)):
#print("Reading "+ filename)
f = os.path.join(path, filename)
readSize += os.path.getsize(f)
currfile = open(f).read()
a.write("<start>")
a.write(currfile)
a.write("<eof>")
i=i+1
print("readSize="+str(readSize))
# ### Open "train.txt" convert into lower case and sort them
#
# In[4]:
raw_text = open(train_filename).read().lower()
INPUT_FILE_LEN = len(raw_text)
chars = sorted(list(set(raw_text)))
VOCAB_LENGTH = len(chars)
print ("Length of file: "+ str(INPUT_FILE_LEN))
print ("Total Vocab length (unique chars in input) : "+ str(VOCAB_LENGTH))
# ### create mapping of unique chars to integers, and a reverse mapping
#
# In[5]:
char_to_int = dict((c, i) for i, c in enumerate(chars))
int_to_char = dict((i, c) for i, c in enumerate(chars))
print(char_to_int)
print(int_to_char)
# In[6]:
dataX = []
dataY = []
for i in range(0, INPUT_FILE_LEN - SEQ_LENGTH, 1):
seq_in = raw_text[i:i + SEQ_LENGTH]
seq_out = raw_text[i + SEQ_LENGTH]
dataX.append([char_to_int[char] for char in seq_in])
dataY.append(char_to_int[seq_out])
samples = len(dataX)
print( "Total samples: "+ str(samples))
# reshape X to be [samples, time steps, features]
X = numpy.reshape(dataX, (samples, SEQ_LENGTH, 1))
# normalize
X = X / float(VOCAB_LENGTH)
# one hot encode the output variable
y = np_utils.to_categorical(dataY)
print("X.shape=" + str(X.shape))
print("y.shape=" + str(y.shape))
# ### create model with 2 LSTM Layers with dropout 0.2, 1 Dense layer with softmax
#
# In[7]:
model = Sequential()
model.add(LSTM(256, input_shape=(X.shape[1], X.shape[2]), return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(256))
model.add(Dropout(0.2)) # 0.5
model.add(Dense(y.shape[1], activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam')
# ### Visualize the model
# In[8]:
print(model.summary())
# ### Fit the model
# In[9]:
histroy = model.fit(X, y, epochs=EPOCHS, batch_size=BATCH_SIZE, verbose=1)
# ### Plot Model Loss
# In[10]:
import matplotlib.pyplot as plt
# list all data in history
print(histroy.history.keys())
# summarize history for loss
plt.plot(histroy.history['loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.show()
# ### Generate the code
# In[19]:
start = numpy.random.randint(0, len(dataX)-1)
pattern = dataX[start]
print ("input code starts with: [", ''.join([int_to_char[value] for value in pattern]), "]")
# generate characters
for i in range(500):
x = numpy.reshape(pattern, (1, len(pattern), 1))
x = x / float(VOCAB_LENGTH)
prediction = model.predict(x, verbose=0)
index = numpy.argmax(prediction)
result = int_to_char[index]
seq_in = [int_to_char[value] for value in pattern]
sys.stdout.write(result)
pattern.append(index)
pattern = pattern[1:len(pattern)]
print("\n#####.")