parent
53c9df6124
commit
9c479b9e84
@ -1,60 +0,0 @@
|
|||||||
import os
|
|
||||||
import tensorflow as tf
|
|
||||||
import matplotlib.pyplot as plt
|
|
||||||
from tensorflow import keras
|
|
||||||
import numpy as np
|
|
||||||
config = tf.compat.v1.ConfigProto(gpu_options=tf.compat.v1.GPUOptions(allow_growth=True))
|
|
||||||
sess = tf.compat.v1.Session(config=config)
|
|
||||||
|
|
||||||
num_mnist = keras.datasets.mnist
|
|
||||||
(train_images, train_labels), (test_images, test_labels) = num_mnist.load_data()
|
|
||||||
|
|
||||||
print(train_images.shape)
|
|
||||||
print(train_labels.shape)
|
|
||||||
print(test_images.shape)
|
|
||||||
print(test_labels.shape)
|
|
||||||
|
|
||||||
model = keras.Sequential()
|
|
||||||
model.add(keras.layers.Conv2D(8, (3,3), activation = 'relu', input_shape = (28,28,1)))
|
|
||||||
model.add(keras.layers.MaxPooling2D(2,2))
|
|
||||||
model.add(keras.layers.Conv2D(8, (3,3), activation = 'relu'))
|
|
||||||
model.add(keras.layers.MaxPooling2D(2,2))
|
|
||||||
|
|
||||||
model.add(keras.layers.Flatten())
|
|
||||||
model.add(keras.layers.Dense(128, activation = tf.nn.relu))
|
|
||||||
model.add(keras.layers.Dense(10, activation = tf.nn.softmax))
|
|
||||||
|
|
||||||
checkpoint_path = "training_1/cp.ckpt"
|
|
||||||
checkpoint_dir = os.path.dirname(checkpoint_path)
|
|
||||||
|
|
||||||
cp_callback = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_path,
|
|
||||||
save_weights_only=True,
|
|
||||||
verbose=1)
|
|
||||||
|
|
||||||
train_images_scaled = train_images/255
|
|
||||||
model.compile(optimizer = 'adam', loss = tf.losses.sparse_categorical_crossentropy, metrics = ['accuracy'])
|
|
||||||
|
|
||||||
history = model.fit(
|
|
||||||
train_images_scaled.reshape(-1, 28, 28 ,1),
|
|
||||||
train_labels,
|
|
||||||
epochs = 8,
|
|
||||||
validation_data=(test_images.reshape(-1, 28, 28 ,1), test_labels),
|
|
||||||
callbacks=[cp_callback]
|
|
||||||
)
|
|
||||||
|
|
||||||
results = model.evaluate(test_images.reshape(-1, 28, 28 ,1), test_labels)
|
|
||||||
|
|
||||||
testShow = test_labels[:100]
|
|
||||||
|
|
||||||
pred = model.predict(test_images.reshape(-1, 28, 28 ,1))
|
|
||||||
predict = []
|
|
||||||
for item in pred:
|
|
||||||
predict.append(np.argmax(item))
|
|
||||||
|
|
||||||
plt.figure()
|
|
||||||
plt.title('Conv Predict')
|
|
||||||
plt.ylabel('number')
|
|
||||||
plt.plot( range( testShow.size ), predict[:100], label='predict')
|
|
||||||
plt.plot( range( testShow.size ), testShow, label='result')
|
|
||||||
plt.legend()
|
|
||||||
plt.show()
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in new issue