Skip to content

Commit 0f0dd40

Browse files
committed
training and scoring
1 parent b1d326c commit 0f0dd40

File tree

7 files changed

+43
-4
lines changed

7 files changed

+43
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
MNIST handwritten digits with a convolutional neural network.

data/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .data import dataset

data/data.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import numpy as np
2+
from tensorflow.keras import datasets
3+
from tensorflow.keras.utils import to_categorical
4+
5+
def dataset():
6+
(x_train, y_train), (x_test, y_test) = datasets.mnist.load_data()
7+
x_train = x_train.astype("float32") / 255
8+
x_test = x_test.astype("float32") / 255
9+
x_train = np.expand_dims(x_train, -1)
10+
x_test = np.expand_dims(x_test, -1)
11+
y_train = to_categorical(y_train, 10)
12+
y_test = to_categorical(y_test, 10)
13+
def result(set):
14+
return (x_train, y_train) if set == 'train' else (x_test, y_test)
15+
return result

main.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
11
from model import model_factory
2+
from data import dataset
3+
from trainer import trainer
24

35
model = model_factory()
4-
model.summary()
6+
print(model.summary())
7+
data = dataset()
8+
9+
train = trainer(model, data)
10+
model = train(epochs=10, batch_size=128)
11+
12+
(x, y) = data('test')
13+
score = model.evaluate(x, y, verbose=0)
14+
print("Test loss:", score[0])
15+
print("Test accuracy:", score[1])

model/layers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
def Conv(filters, kernel_size, block_name):
1111
def result(input):
1212
conv = Conv2D(filters, kernel_size, padding="same", name=block_name)(input)
13-
relu = Activation('relu')(conv)
13+
relu = Activation('relu')(conv)
1414
pool = MaxPooling2D(pool_size=(2, 2))(relu)
1515
return pool
1616
return result
@@ -20,8 +20,8 @@ def result(input):
2020
flatten = Flatten(name='flat')(input)
2121
dropout = Dropout(0.5, name='drop')(flatten)
2222
dense = Dense(units=num_classes)(dropout)
23-
relu = Activation('relu', name='relu_x')(dense)
24-
return relu
23+
softmax = Activation('softmax', name='softmax_x')(dense)
24+
return softmax
2525
return result
2626

2727
def Inputs():

trainer/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .trainer import trainer

trainer/trainer.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
3+
def trainer(model, data):
4+
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
5+
(x, y) = data('train')
6+
def result(epochs, batch_size):
7+
model.fit(x, y, batch_size=batch_size, epochs=epochs, validation_split=0.1)
8+
return model
9+
return result
10+

0 commit comments

Comments
 (0)