# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: TensorFlow-GPU
#     language: python
#     name: tf-gpu
# ---

# +
# From the first example in Deep Learning with Python ()

from keras.datasets import mnist
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()

# -

print("Shape of the training data: ")
print(train_images.shape)
print("Shape of the testing data: ")
print(test_images.shape)

digit = train_images[4]
import matplotlib.pyplot as plt
plt.imshow(digit, cmap=plt.cm.binary)
plt.show()

# +
# Create a simple 2-layer neural network model
from keras import models
from keras import layers

network = models.Sequential()
network.add(layers.Dense(512, activation='relu', input_shape=(28 * 28, )))
network.add(layers.Dense(10, activation='softmax'))

# -

# Prepare the network by picking a loss function, optimizer, and metric to monitor during training.
network.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])


# +
# Prepare the data by reshaping to match inputs and transforming data range to 0-1.0
# original data was uint8 with interval [0, 255]
train_images = train_images.reshape((60000, 28 * 28))
train_images = train_images.astype('float32') / 255

test_images = test_images.reshape((10000, 28 * 28))
test_images = test_images.astype('float32') / 255


# +
# Prepare the labels as categorical data
from keras.utils import to_categorical

train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)

# +
# Now we can train the models with Keras
import tensorflow as tf

# Matt - gtx970 has some issues with memory, try to pull less.
#gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.7)
#sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))

network.fit(train_images, train_labels, epochs=5, batch_size=128)
# -

# The model is trained to 98.94% accuracy, lets see how it does with the test set.
test_loss, test_acc = network.evaluate(test_images, test_labels)
print('test_acc:', test_acc)



StarcoderdataJupyterScriptsDedupFiltered