Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from __future__ import absolute_import, division, print_function
- from tensorflow.keras.datasets import mnist
- from tensorflow.keras import Model, layers
- import tensorflow as tf
- import numpy as np
- import matplotlib.pyplot as plt
- num_classes = 10
- num_features = 784
- learning_rate = 0.5
- training_steps = 4000
- batch_size = 256
- n_hidden_1 = 256
- n_hidden_2 = 512
- (x_train, y_train), (x_test, y_test) = mnist.load_data()
- x_train, x_test = np.array(x_train, np.float32), np.array(x_test, np.float32)
- x_train, x_test = x_train.reshape([-1, num_features]), x_test.reshape([-1, num_features])
- x_train, x_test = x_train / 255., x_test / 255.
- train_data = tf.data.Dataset.from_tensor_slices((x_train, y_train))
- train_data = train_data.repeat().shuffle(5000).batch(batch_size).prefetch(1)
- class NeuralNet(Model):
- def __init__(self):
- super(NeuralNet, self).__init__()
- self.fc1 = layers.Dense(n_hidden_1, activation=tf.nn.relu)
- self.fc2 = layers.Dense(n_hidden_2, activation=tf.nn.relu)
- self.out = layers.Dense(num_classes, activation=tf.nn.softmax)
- def call(self, x, is_training=False):
- x = self.fc1(x)
- x = self.out(x)
- if not is_training:
- x = tf.nn.softmax(x)
- return x
- neural_net = NeuralNet()
- def cross_entropy_loss(x, y):
- y = tf.cast(y, tf.int64)
- loss = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=y, logits=x)
- return tf.reduce_mean(loss)
- def accuracy(y_pred, y_true):
- correct_prediction = tf.equal(tf.argmax(y_pred, 1), tf.cast(y_true, tf.int64))
- return tf.reduce_mean(tf.cast(correct_prediction, tf.float32), axis=-1)
- optimizer = tf.optimizers.SGD(learning_rate)
- def run_optimization(x, y):
- with tf.GradientTape() as g:
- pred = neural_net(x, is_training=True)
- loss = cross_entropy_loss(pred, y)
- trainable_variables = neural_net.trainable_variables
- gradients = g.gradient(loss, trainable_variables)
- optimizer.apply_gradients(zip(gradients, trainable_variables))
- for step, (batch_x, batch_y) in enumerate(train_data.take(training_steps), 1):
- run_optimization(batch_x, batch_y)
- if step % display_step == 0:
- pred = neural_net(batch_x, is_training=True)
- loss = cross_entropy_loss(pred, batch_y)
- acc = accuracy(pred, batch_y)
- print("step: %i, loss: %f, accuracy: %f" % (step, loss, acc))
- pred = neural_net(x_test, is_training=False)
- print("Test Accuracy: %f" % accuracy(pred, y_test))
- n_images = 10
- test_images = x_test[:n_images]
- predictions = neural_net(test_images)
- for i in range(n_images):
- plt.imshow(np.reshape(test_images[i], [28, 28]), cmap='gray')
- plt.show()
- print("Model prediction: %i" % np.argmax(predictions.numpy()[i]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement