# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# + code_folding=[0]
# import  input_data and read the data set
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)


# + code_folding=[0]
# Define the tensor model

# x - the input variable - 28X28=784 vector
x = tf.placeholder(tf.float32, [None, 784])
# w - the weights to learn - inputting 784 and outputing 10 weights, one per each of the identified digits
W = tf.Variable(tf.zeros([784, 10]))
# b - the bias to learn - per 10 output digits
b = tf.Variable(tf.zeros([10]))
# y is the model we try to learn
y = tf.nn.softmax(tf.matmul(x, W) + b)
# y_ is the "real" target
y_ = tf.placeholder(tf.float32, [None, 10])

# Defining the loss function and attempting to minimize it
cross_entropy = tf.reduce_mean(
  tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))
# cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))

# Workign with the gradient descent algorithm and setting the learning rate (=.5)
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)

# + code_folding=[0]
# Train the model

# open a TF interactive session and initialize the variables
sess = tf.InteractiveSession()
tf.initialize_all_variables().run() # for older version of TF
# tf.global_variables_initializer().run() # for newer version of TF

# running the loop of training steps with batches from the input
for _ in range(10000):
  batch_xs, batch_ys = mnist.train.next_batch(100)
  sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})


# + code_folding=[0, 2]
# Check the accuracy of the model on the test set

# Compare the prediction with the real values
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))

# calculate the ratio of the correct predictions
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

# finally - run the trained model on the test set of images and labels and output the accuracy
print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))
# -

mnist

mnist.train

StarcoderdataJupyterScriptsDedupFiltered