<jupyter_start><jupyter_text>MNIST For ML Beginnershttps://www.tensorflow.org/versions/r0.12/tutorials/mnist/beginners/ The MNIST data is hosted on Yann LeCun's website(http://yann.lecun.com/exdb/mnist/). If you are copying and pasting in the code from this tutorial, start here with these two lines of code which will download and read in the data automatically:<jupyter_code># https://www.tensorflow.org/versions/r0.12/tutorials/mnist/beginners/
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>Implementing the Regression<jupyter_code>import tensorflow as tf

x = tf.placeholder(tf.float32, [None, 784])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))

# y == hypothesis
y = tf.nn.softmax(tf.matmul(x, W) + b)<jupyter_output><empty_output><jupyter_text>Training<jupyter_code># y_ == label
y_ = tf.placeholder(tf.float32, [None, 10])
# cross_entropy == cost/lost function
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)

# parameters
training_epochs = 10
batch_size = 100
total_batch = int(mnist.train.num_examples / batch_size)

sess = tf.Session()
sess.run(tf.global_variables_initializer())
for epoch in range(training_epochs):
    cost = 0
    for i in range(total_batch):
        batch_xs, batch_ys = mnist.train.next_batch(batch_size)
        c, _ = sess.run([cross_entropy, train_step],
                        feed_dict={x: batch_xs, y_: batch_ys})
        cost += c / total_batch
    print('Epoch:', '%04d' % (epoch + 1),
          'cost =', '{:.9f}'.format(cost))
print("Learning finished")<jupyter_output>Epoch: 0001 cost = 0.403418125
Epoch: 0002 cost = 0.310536775
Epoch: 0003 cost = 0.295193166
Epoch: 0004 cost = 0.286539707
Epoch: 0005 cost = 0.281588302
Epoch: 0006 cost = 0.277909960
Epoch: 0007 cost = 0.274699409
Epoch: 0008 cost = 0.271966946
Epoch: 0009 cost = 0.269758330
Epoch: 0010 cost = 0.267622066
Learning finished<jupyter_text>Evaluating Our Model<jupyter_code>correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))<jupyter_output>0.9233<jupyter_text>Deep MNIST for Expertshttps://www.tensorflow.org/versions/r0.12/tutorials/mnist/pros/ Shape<jupyter_code>sess = tf.InteractiveSession()

def show_(t):
    print('shape:', tf.shape(t).eval(), 'size:', tf.size(t).eval(), 'rank:', tf.rank(t).eval())
    print(t.eval())

c1 = tf.constant([1, 3, 5, 7, 9, 0, 2, 4, 6, 8, 3, 7])
c4 = tf.constant([[1, 2, 3], [7, 8, 9]])

print('-----------reshape------------')
show_(tf.reshape(c1, [2, -1]))    # [[1 3 5 7 9 0] [2 4 6 8 3 7]]
show_(tf.reshape(c1, [-1, 3]))    # [[1 3 5] [7 9 0] [2 4 6] [8 3 7]]
show_(tf.reshape(c4, [-1]))       # [1 2 3 7 8 9]

c2 = tf.reshape(c1, [2, 2, 1, 3])
c3 = tf.reshape(c1, [1, 4, 1, 3, 1])

# removes dimensions of size 1
print('-----------squeeze------------')
show_(c2)
# [          -> tensor
#  [         -> tf.reshape(c1, [2,
#    [       -> tf.reshape(c1, [2, 2,
#     [      -> tf.reshape(c1, [2, 2, 1,
#      1 3 5 -> tf.reshape(c1, [2, 2, 1, 3])
#     ]
#    ]
#    [
#     [7 9 0]
#    ]
#  ]
#  [[[2 4 6]] [[8 3 7]]]
# ]
show_(tf.squeeze(c2))    # [[[1 3 5] [7 9 0]]  [[2 4 6] [8 3 7]]]

# [[[[[1] [3] [5]]]  [[[7] [9] [0]]]  [[[2] [4] [6]]]  [[[8] [3] [7]]]]]
show_(c3)
show_(tf.squeeze(c3))    # [[1 3 5] [7 9 0] [2 4 6] [8 3 7]]

sess.close()<jupyter_output>-----------reshape------------
shape: [2 6] size: 12 rank: 2
[[1 3 5 7 9 0]
 [2 4 6 8 3 7]]
shape: [4 3] size: 12 rank: 2
[[1 3 5]
 [7 9 0]
 [2 4 6]
 [8 3 7]]
shape: [6] size: 6 rank: 1
[1 2 3 7 8 9]
-----------squeeze------------
shape: [2 2 1 3] size: 12 rank: 4
[[[[1 3 5]]

  [[7 9 0]]]


 [[[2 4 6]]

  [[8 3 7]]]]
shape: [2 2 3] size: 12 rank: 3
[[[1 3 5]
  [7 9 0]]

 [[2 4 6]
  [8 3 7]]]
shape: [1 4 1 3 1] size: 12 rank: 5
[[[[[1]
    [3]
    [5]]]


  [[[7]
    [9]
    [0]]]


  [[[2]
    [4]
    [6]]]


  [[[8]
    [3]
    [7]]]]]
shape: [4 3] size: 12 rank: 2
[[1 3 5]
 [7 9 0]
 [2 4 6]
 [8 3 7]]
StarcoderdataJupyterStructuredCleanDedup