from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
利用placeholder(占位符)构图
# datamnist = input_data.read_data_sets('/tmp/tensorflow/mnist/input_data',one_hot=True)
# placeholder for x cause image=28*28x= tf.placeholder(tf.float32,[None,784])
# predict yW = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))
y = tf.nn.softmax(tf.matmul(x,W)+b)
# correct yy_ = tf.placeholder(tf.float32,[None, 10])
计算交叉熵,并用梯度下降进行训练
# calculate cross_entropy
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))
# use GradientDescent for training
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)