forked from CleanPegasus/TensorDash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMNIST.py
88 lines (57 loc) · 2.21 KB
/
MNIST.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
import numpy as np
import matplotlib.pyplot as plt
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import Adam
from keras.layers import Flatten
from keras.layers import Conv2D
from keras.layers import MaxPooling2D
from keras.layers import Dropout
from keras.models import Model
from keras.utils import to_categorical
from keras.callbacks import LambdaCallback
import random
from tensordash import Tensordash
np.random.seed(0)
(X_train, y_train), (X_test, y_test) = mnist.load_data()
print(X_train.shape)
print(X_test.shape)
print(y_test.shape)
assert(X_train.shape[0] == y_train.shape[0]), "The number of images is not equal to the number of labels."
assert(X_test.shape[0] == y_test.shape[0]), "The number of images is not equal to the number of labels."
assert(X_train.shape[1:] == (28, 28)), "The dimensions of the images are not 28*28."
assert(X_test.shape[1:] == (28, 28)), "The dimensions of the images are not 28*28."
num_of_samples = []
cols = 5
num_classes = 10
y_train = to_categorical(y_train, 10)
y_test = to_categorical(y_test, 10)
print(X_train.shape)
X_train = X_train/255
X_test = X_test/255
#num_pixels = 784
X_train = X_train.reshape(60000, 28, 28, 1)
X_test = X_test.reshape(10000, 28, 28, 1)
def lenet_model():
model = Sequential()
model.add(Conv2D(30, (5,5), input_shape = (28, 28, 1), activation='relu'))
model.add(MaxPooling2D(pool_size = (2, 2)))
model.add(Conv2D(15, (3, 3), activation = 'relu'))
model.add(MaxPooling2D(pool_size = (2,2)))
model.add(Flatten())
model.add(Dense(500, activation = 'relu'))
# model.add(Dense(500, activation = 'relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation = 'softmax'))
model.compile(Adam(lr = 0.01), loss = 'categorical_crossentropy', metrics = ['accuracy'])
return model
model = lenet_model()
email = '[email protected]'
password = '12345678'
histories = Tensordash(email = email, password = password, ModelName = 'My new model')
try:
model.fit(X_train, y_train, epochs = 2, validation_split = 0.1, batch_size = 5000, verbose = 1, shuffle = True, callbacks = [histories])
except:
histories.sendCrash()