-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathlesson 14. CNN.py
43 lines (31 loc) · 1.37 KB
/
lesson 14. CNN.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
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.keras.datasets import mnist # библиотека базы выборок Mnist
from tensorflow import keras
from tensorflow.keras.layers import Dense, Flatten, Dropout, Conv2D, MaxPooling2D
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# стандартизация входных данных
x_train = x_train / 255
x_test = x_test / 255
y_train_cat = keras.utils.to_categorical(y_train, 10)
y_test_cat = keras.utils.to_categorical(y_test, 10)
x_train = np.expand_dims(x_train, axis=3)
x_test = np.expand_dims(x_test, axis=3)
print( x_train.shape )
model = keras.Sequential([
Conv2D(32, (3,3), padding='same', activation='relu', input_shape=(28, 28, 1)),
MaxPooling2D((2, 2), strides=2),
Conv2D(64, (3,3), padding='same', activation='relu'),
MaxPooling2D((2, 2), strides=2),
Flatten(),
Dense(128, activation='relu'),
Dense(10, activation='softmax')
])
# print(model.summary()) # вывод структуры НС в консоль
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
his = model.fit(x_train, y_train_cat, batch_size=32, epochs=5, validation_split=0.2)
model.evaluate(x_test, y_test_cat)