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()