-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodeltweak.py
91 lines (70 loc) · 2.46 KB
/
modeltweak.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
# -*- coding: utf-8 -*-
"""modeltweak.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1ae5IlVNbI9p-xi_z-WqC4hyS2VRiAxqg
"""
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.utils.np_utils import to_categorical
from keras.layers import Dense
from keras.optimizers import Adam
from keras.backend import clear_session
import numpy
# Train model function
def train_model(neurons , model , epochs , test) :
print("\n" , " *** Summary *** " , "\n", "Iteration : ", test , "\n" , " Number of Neurons : ", neurons , "\n" , " Number of Epochs : ", epochs)
model.add(Dense(units = neurons , input_dim = 28*28 , activation = 'relu'))
model.add(Dense(units=200 , input_dim = 28*28 , activation = 'relu'))
model.add(Dense(units=60 , input_dim = 28*28 , activation = 'relu'))
model.add(Dense(units=10 , input_dim = 28*28 , activation = 'softmax'))
model.compile( optimizer= "Adam" , loss='categorical_crossentropy',
metrics=['accuracy'] )
return model
def validate(fit_model, epochs):
text = fit_model.history
accuracy = text['accuracy'][epochs-1] * 100
accuracy = int(accuracy)
f= open("accuracy.txt","w+")
f.write(str(accuracy))
f.close()
print(" Accuracy for this Iteration is : " , accuracy ,"%")
return accuracy
# Load Model
(train_X , train_y), (test_X , test_y) = mnist.load_data("mymnist.data")
# Reshape data and change type
test_X = test_X.reshape(-1 , 28*28)
train_X = train_X.reshape(-1 , 28*28)
test_X = test_X.astype("float32")
train_X = train_X.astype("float32")
# One hot encoding
test_y = to_categorical(test_y)
train_y = to_categorical(train_y)
#Initials
neurons = 10
accuracy = 0
epochs = 1
test = 1
flag = 0
while int(accuracy) < 95 :
if flag == 1 :
model = keras.backend.clear_session()
neurons = neurons+10
epochs = epochs+1
test = test + 1
#model=reset_weights(model)
model = Sequential()
model = train_model(neurons , model , epochs , test)
print(" calculating accuracy . . .")
fit_model = model.fit(train_X , train_y , epochs = epochs , verbose = False)
accuracy=validate(fit_model , epochs)
flag = 1
#sending mail
import smtplib
s = smtplib.SMTP('smtp.gmail.com', 587)
s.starttls()
s.login("messiisahot@gmail.com", "anuragp0010")
s.sendmail("messiisahot@gmail.com", "anuragp0010@gmail.com", "The Machine Learning model has achieved the accuracy of " + str(accuracy))
# terminating the session
quit()