全连接神经网络学习(一)

代码:

#-*- coding:utf-8 -*-
#author : zhangwei

import os , math
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

#过滤警告
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
#mnist一共有10个分类
num_classes = 10
#将图像转换成784维度的矩阵
image_size = 28
image_pixels = image_size * image_size
#每个批次的样本数量为50
batch_size = 50
#定义隐藏层单元数20 ,10;
hidden1_units = 20
hidden2_units = 15
#定义学习率为0.1;
learning_rate = 0.1

#指定输入以及输入标签,输入是50*784.标签是50*10
image_placeholder = tf.placeholder(tf.float32 , shape=[batch_size , image_pixels])
labels_placeholder = tf.placeholder(tf.float32 , shape=[batch_size , num_classes])

#定义神经网络的隐藏层的大小以及参数设置
#输入图像是784,第一个隐藏层参数为hidden1_units,权值标准差为stddev,偏置初始化为0;
#第二个隐藏层单元是hidden2_units,标准差是stddev,偏置仍然采用初始化为0;
#定义线性层,网络连接是hidden2_units×num_classes,最后返回值是softmax线性表示,logits;
def inference(image , hidden1_units , hidden2_units):
    with tf.name_scope('hidden1'):
        weights1 = tf.Variable(tf.truncated_normal([image_pixels , hidden1_units] ,
                                                  stddev=1.0 / math.sqrt(image_pixels) ,
                                                  name='weights1'))
        biases1 = tf.Variable(tf.zeros([hidden1_units]) , name='biases1')
        hidden1 = tf.nn.relu(tf.matmul(image , weights1) + biases1)

    with tf.name_scope('hidden2'):
        weights2 = tf.Variable(tf.truncated_normal([hidden1_units , hidden2_units] ,
                                                  stddev=1.0 / math.sqrt(hidden1_units) ,
                                                  name='weights2'))
        biases2 = tf.Variable(tf.zeros([hidden2_units] , name='biases2'))
        hidden2 = tf.nn.relu(tf.matmul(hidden1 , weights2) + biases2)

    with tf.name_scope('softmax_linear'):
        weights3 = tf.Variable(tf.truncated_normal([hidden2_units , num_classes] ,
                                                   stddev=1.0/hidden2_units ,
                                                   name='weights3'))
        biases3 = tf.Variable(tf.zeros([num_classes]) + 0.1 , name='biases3')
        logits = tf.matmul(hidden2 , weights3) + biases3

    return logits

#定义损失函数,首先定义参数是预测值核标签,损失函数采用交叉熵,返回值是交叉熵的均值;
def loss(logits , labels):
    labels = tf.to_int64(labels)
    cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=labels , logits = logits , name='cross_entropy')
    return tf.reduce_mean(cross_entropy , name='entropy_mean')
#采用梯度下降进行训练,最后返回训练额结果
def training(loss , learning_rate):
    # tf.summary.scalar('loss' , loss)
    optimizer = tf.train.GradientDescentOptimizer(learning_rate)
    train_op = optimizer.minimize(loss)
    return train_op

def evaluation(logits , labels):
    correct = tf.nn.in_top_k(logits , labels , 1)#correct是bool类型,需要进行格式转化
    return tf.reduce_sum(tf.cast(correct , tf.int32))

logits = inference(image_placeholder , hidden1_units , hidden2_units)
batch_loss = loss(logits=logits , labels=labels_placeholder)#还未喂入数据
train_on_batch = training(loss=batch_loss , learning_rate=learning_rate)
correct_count = evaluation(logits=logits , labels=labels_placeholder)

writer = tf.summary.FileWriter(logdir='/home/zhangwei/tensorboard' , graph=tf.get_default_graph())
writer.close()

猜你喜欢

转载自blog.csdn.net/xwei1226/article/details/80416774
今日推荐