-
-
Save ox1111/5b63f87cfad1a3b732616e97ceb1de1d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import tensorflow as tf | |
import numpy as np | |
import matplotlib.pyplot as plt | |
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.fashion_mnist.load_data() | |
print("x_train shape:", x_train.shape, "y_train shape:", y_train.shape) | |
plt.imshow(x_train[1]) | |
x_train = x_train.astype('float32') / 255 | |
x_test = x_test.astype('float32') / 255 | |
model = tf.keras.Sequential() | |
# Must define the input shape in the first layer of the neural network | |
model.add(tf.keras.layers.Conv2D(filters=64, kernel_size=2, padding='same', activation='relu', input_shape=(28,28,1))) | |
model.add(tf.keras.layers.MaxPooling2D(pool_size=2)) | |
model.add(tf.keras.layers.Dropout(0.3)) | |
model.add(tf.keras.layers.Conv2D(filters=32, kernel_size=2, padding='same', activation='relu')) | |
model.add(tf.keras.layers.MaxPooling2D(pool_size=2)) | |
model.add(tf.keras.layers.Dropout(0.3)) | |
model.add(tf.keras.layers.Flatten()) | |
model.add(tf.keras.layers.Dense(256, activation='relu')) | |
model.add(tf.keras.layers.Dropout(0.5)) | |
model.add(tf.keras.layers.Dense(10, activation='softmax')) | |
# Take a look at the model summary | |
model.summary() | |
model.compile(loss='categorical_crossentropy', | |
optimizer='adam', | |
metrics=['accuracy']) | |
model.fit(x_train, | |
y_train, | |
batch_size=64, | |
epochs=10, | |
validation_data=(x_train, y_train) | |
) | |
score = model.evaluate(x_test, y_test, verbose=0) | |
print('\n', 'Test accuracy:', score[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment