|
|
|
"""Untitled3.ipynb |
|
|
|
Automatically generated by Colaboratory. |
|
|
|
Original file is located at |
|
https://colab.research.google.com/drive/1gWAA1NHcuSs1JrZSG9sQIrcozcWOaaZL |
|
""" |
|
|
|
|
|
|
|
import tensorflow as tf |
|
from tensorflow.keras.datasets import mnist |
|
(x_train, y_train), (x_test, y_test) = mnist.load_data() |
|
|
|
x_train = x_train / 255 |
|
x_test = x_test / 255 |
|
y_train = tf.keras.utils.to_categorical(y_train, num_classes=10) |
|
y_test = tf.keras.utils.to_categorical(y_test, num_classes=10) |
|
|
|
from tensorflow.keras.models import Sequential |
|
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense |
|
model = Sequential() |
|
|
|
model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1))) |
|
model.add(MaxPooling2D((2, 2))) |
|
model.add(Flatten()) |
|
model.add(Dense(64, activation='relu')) |
|
model.add(Dense(10, activation='softmax')) |
|
model.summary() |
|
|
|
train_size = x_train.shape[0] |
|
val_size = int(train_size * 0.1) |
|
test_size = x_test.shape[0] |
|
print("Размер тренировочного датасета:", train_size) |
|
print("Размер валидационного датасета:", val_size) |
|
print("Размер тестового датасета:", test_size) |
|
tf.keras.utils.plot_model(model, show_shapes= True, show_layer_names= True, show_layer_activations= True) |
|
|
|
model.save('my_model') |
|
|
|
from google.colab import drive |
|
drive.mount('/content/drive') |
|
|
|
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) |
|
model.fit(x_train, y_train, batch_size=32, epochs=10, validation_data=(x_test, y_test)) |
|
|
|
loss, accuracy = model.evaluate(x_test, y_test) |
|
print(f'Loss: {loss}, Accuracy: {accuracy}') |
|
|
|
import numpy as np |
|
index = np.random.randint(len(x_test)) |
|
image = x_test[index] |
|
image = np.expand_dims(image, axis=0) |
|
prediction = model.predict(image) |
|
predicted_digit = np.argmax(prediction) |
|
remainder = predicted_digit % 2 |
|
print(f'Predicted Digit: {predicted_digit}, Remainder: {remainder}') |