通过交叉熵来判断和目标值的差距,越大,越不准确
1、简单的神经网络实现手写数字图片识别
'
import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data Flags = tf.app.flags.FLAGS tf.app.flags.DEFINE_integer("is_train",0, '0,1') def full_connected(): # 获取真实数据 mnist = input_data.read_data_sets("./data/mnist/input_data/", one_hot=True) # 建立数据的占位符 with tf.variable_scope("data"): x = tf.placeholder(tf.float32, [None, 784]) y_true = tf.placeholder(tf.int32, [None, 10]) # 建立一个全连接层的神经网络 with tf.variable_scope("fc_model"): # 随机初始化权重和偏置 weight = tf.Variable(tf.random_normal([784, 10], mean=0.0, stddev=1.0), name='fds') bias = tf.Variable(tf.constant(0.0, shape=[10])) # 预测None个样本的输出结果 matrix [NOne, 784] * [784, 10]+[10]=[NOne, 10] y_predict = tf.matmul(x, weight) + bias # 求出所有样本的损失 with tf.variable_scope("soft_cross"): # 求平均交叉熵损失 loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_true,logits=y_predict)) # 梯度下降求出损失 with tf.variable_scope("optimizer"): train_op = tf.train.GradientDescentOptimizer(0.1).minimize(loss) # 计算准确率 with tf.variable_scope("acc"): equal_list = tf.equal(tf.argmax(y_true, 1), tf.argmax(y_predict, 1)) accurary = tf.reduce_mean(tf.cast(equal_list, tf.float32)) # 收集变量, 单个数字值收集 tf.summary.scalar('losses', loss) tf.summary.scalar('acc', accurary) # 高纬变量收集 tf.summary.histogram('weighs', weight) tf.summary.histogram('bias', bias) # 定义一个初始化变量的op init_op = tf.global_variables_initializer() # 合并变量 merged = tf.summary.merge_all() # saver = tf.train.Saver() # 开启会话 with tf.Session() as sess: # 初始化变量 sess.run(init_op) #建立events文件 filewrite = tf.summary.FileWriter('./tmp/summary/test/', graph=sess.graph) #迭代步数去训练, 更新参数预测 if Flags.is_train == 1: for i in range(200): # 取出真实存在的特征值和目标值 minis_x, mnist_y = mnist.train.next_batch(50) # 运行train_op训练 sess.run(train_op, feed_dict={x:minis_x, y_true:mnist_y}) # 写入每步训练的值 summary = sess.run(merged, feed_dict={x:minis_x, y_true:mnist_y}) filewrite.add_summary(summary, i) print('训练第%d步, 准确率:%f'%(i, sess.run(accurary, feed_dict={x:minis_x, y_true:mnist_y}))) saver.save(sess, './tmp/ckpt/fc_model') else: # 加载模型 saver.restore(sess, './tmp/ckpt/fc_model') # 预测 for i in range(100): # 每次测试一张图片 [1,0,0,0,0] x_test, y_test = mnist.test.next_batch(1) # 求最大值 print('第{0}张图片,手写数字是{1},' '预测结果{2}'.format( i, tf.argmax(y_test,1).eval(), tf.argmax(sess.run(y_predict, feed_dict={x:x_test, y_true:y_test}), 1).eval() )) return None full_connected()