TensorFlow自学笔记-07 基于官方文档 多层感知机
TensorFlow 支持自动求导,可以使用 TensorFlow 优化器来计算和使用梯度。它使用梯度自动更新用变量定义的张量。本节将使用 TensorFlow 的一些强大功能,如 Contrib(层)来定义神经网络层及使用 TensorFlow 自带的优化器来计算和使用梯度。通过前面的学习,我们已经知道如何使用 TensorFlow 的优化器。Contrib 可以用来添加各种层到神经网络模型,如添加构建块。
对于分类任务,最好使用交叉熵损失函数。隐藏层使用 ReLU 激活函数,输出层使用 softmax 函数。
import tensorflow as tf
import tensorflow.contrib.layers as layers
from tensorflow.python import debug as tf_debug
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
n_hidden = 30
n_input = 784
n_classes = 10
max_epochs = 10
eta = 0.001
batch_size = 200
def multilayer_perceptron(x):
fc1 = layers.fully_connected(x, n_hidden, activation_fn=tf.nn.relu, scope='fc1')
out