<jupyter_start><jupyter_text>Get MNIST data<jupyter_code>from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)<jupyter_output>Extracting MNIST_data/train-images-idx3-ubyte.gz
Extracting MNIST_data/train-labels-idx1-ubyte.gz
Extracting MNIST_data/t10k-images-idx3-ubyte.gz
Extracting MNIST_data/t10k-labels-idx1-ubyte.gz<jupyter_text>Check dataset<jupyter_code>mnist.train.images.shape# training dataset, 28*28 image
mnist.train.labels.shape# training label, one-hot
mnist.validation.images.shape, mnist.test.images.shape
mnist.train.labels[0]
%matplotlib inline

import matplotlib
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from numpy import *

img = mnist.train.images[0]
#list to array
array_img = array(img)
#array to matrix
m_img = array_img.reshape(28,28)


plt.imshow(m_img, cmap=cm.binary)
plt.show()<jupyter_output><empty_output><jupyter_text>SoftMax Regression<jupyter_code>import tensorflow as tf

# Model
x = tf.placeholder(tf.float32, [None, 784]) # None means that a dimension can be of any length.
W = tf.Variable(tf.zeros([784, 10])) # multiply the 784-dimensional image vectors by it to produce 10-dimensional vectors of evidence for the difference classes
b = tf.Variable(tf.zeros([10]))
y = tf.nn.softmax(tf.matmul(x, W) + b)


# Train: cost function optimization
y_ = tf.placeholder(tf.float32, [None, 10]) # y_true
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1])) # define cost-function
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)# init learn_rate=0.5
sess = tf.InteractiveSession()
tf.global_variables_initializer().run()
for _ in range(1000):# training times
    batch_xs, batch_ys = mnist.train.next_batch(100)# batch size
    sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})# y_: y_true
    
    
# evaluation
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))# y: y_pred, y_:y_true
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))
batch_xs.shape<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1240238860817639, 'fraction_numerical': 0.025264124942581533, 'mean_word_length': 6.2842809364548495, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '143131', 'license': 'MIT', 'n_tokens_mistral': 860, 'n_tokens_neox': 813, 'n_words': 158, 'path': 'tensorflow/tf_basic_mnist_softmax.ipynb', 'repo_name': 'ybdesire/machinelearning', 'size': '2177'}