TensorFlow多层感知机实现MNIST数据分类

Tensorflow中专门的函数可以帮助我们轻松的定义神经网络的连接层,例如我们定义一个包含一个隐层的神经网络

import tensorflow.contrib.layers as layers
def multilayer_perceptron(x):
    fc1 = layers.fully_connected(x, n_hidden, activation_fn = tf.nn.relu, biases_initializer = tf.zeros_initializer(), scope = 'fc1')
    #fc2 = layers.fully_connected(fc1, 256, activation_fn = tf.nn.relu, biases_initializer = tf.zeros_initializer(), scope = 'fc2')
    out = layers.fully_connected(fc1, n_classes, activation_fn = None,biases_initializer = tf.zeros_initializer(), scope = 'out')
    return out

 其中隐层节点数,输入层节点数和输出层节点数:

#Network Parameters
n_hidden = 30 #隐层节点数为30个
n_classes = 10 #类别数,也就是输出层节点数
n_input = 784 #输入层节点数

如果定义一个包含两个隐层(隐层节点数分别为n_hidden1,n_hidden2)的神经网络:

def multilayer_perceptron(x):
    fc1 = layers.fully_connected(x, n_hidden1, activation_fn = tf.nn.relu, biases_initializer = tf.zeros_initializer(), scope = 'fc1')
    fc2 = layers.fully_connected(fc1, n_hidden2, activation_fn = tf.nn.relu, biases_initializer = tf.zeros_initializer(), scope = 'fc2')
    out = layers.fully_connected(fc2, n_classes, activation_fn = None,biases_initializer = tf.zeros_initializer(), scope = 'out')
    return out

下面给出TensorFlow多层感知机实现MNIST数据分类的代码

#2021.10.16 HIT ATCI LZH
#TensorFlow多层感知机实现MNIST数据分类
import tensorflow as tf
import tensorflow.contrib.layers as layers
from tensorflow.python import debug as tf_debug
import matplotlib.pyplot as plt
from tensorflow.python.ops.gen_linalg_ops import batch_cholesky
from tensorflow.python.training.input import batch
#Network Parameters
n_hidden = 30 #隐层节点数为30个
n_classes = 10 #类别数,也就是输出层节点数
n_input = 784 #输入层节点数
#Hyperparameters
batch_size = 200 #批训练样本数为200
eta = 0.001
max_epoch = 1000 #最大训练次数
#导入数据集
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data/', one_hot = True) #如果当前文件夹下没有 MNIST_data,会首先创建该文件夹,然后下载mnist数据集,有的话直接打开
#定义神经网络的各层
def multilayer_perceptron(x):
    fc1 = layers.fully_connected(x, n_hidden, activation_fn = tf.nn.relu, biases_initializer = tf.zeros_initializer(), scope = 'fc1')
    #fc2 = layers.fully_connected(fc1, 256, activation_fn = tf.nn.relu, biases_initializer = tf.zeros_initializer(), scope = 'fc2')
    out = layers.fully_connected(fc1, n_classes, activation_fn = None,biases_initializer = tf.zeros_initializer(), scope = 'out')
    return out
#定义占位符
x = tf.placeholder(tf.float32, [None, n_input], name = 'placeholder_x')
y = tf.placeholder(tf.float32, [None, n_classes], name = 'placeholer_y')
y_hat = multilayer_perceptron(x)
#分类问题,最好使用交叉熵损失函数
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits = y_hat, labels =  y))
#优化器
train = tf.train.AdadeltaOptimizer(learning_rate=eta).minimize(loss)
#变量初始化
init = tf.global_variables_initializer()
Loss_epoch = []
with tf.Session() as  sess:
    sess.run(init)
    for epoch in range(max_epoch):
        epoch_loss = 0.
        batch_steps = int(mnist.train.num_examples / batch_size)
        for i in range (batch_steps):
            batch_x, batch_y = mnist.train.next_batch(batch_size)
            _, c = sess.run([train, loss], feed_dict = {x: batch_x, y: batch_y})
            epoch_loss += c / batch_steps
        Loss_epoch.append(epoch_loss)
        print("Epoch %02d, Loss = %.6f" %(epoch, epoch_loss))
    #计算模型对测试集的准确率
    correct_prediction = tf.equal(tf.argmax(y_hat, 1), tf.argmax(y, 1))#tf.equal返回Bool型变量
    acc = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    print("Accuracy: %",100*acc.eval({x: mnist.test.images, y: mnist.test.labels}))#tf.cast用于转换数据类型
plt.figure(1)
plt.title('loss')
plt.plot(Loss_epoch)
plt.show()

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

nwsuaf_huasir

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值