应用TensorFlow高级api构建全连接神经网络
import numpy as np
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("/tmp/data/", one_hot=False)
# 参数
learning_rate = 0.1
num_steps = 1000
batch_size = 128
display_step = 100
# 网络参数
num_input = 784
n_hidden_1 = 256
n_hidden_2 = 256
num_classes = 10
# 定义训练的输入函数
input_fn = tf.estimator.inputs.numpy_input_fn(
x={'images': mnist.train.images}, y=mnist.train.labels,
batch_size=batch_size, num_epochs=None, shuffle=True)
# 定义神经网络
def neural_net(x_dict):
x = x_dict['images']
#kernel_initializer:可选,默认为 None,即权重的初始化方法,如果为 None,则使用默认的 Xavier 初始化方法。
layer_1 = tf.layers.dense(x, n_hidden_1)
layer_2 = tf.layers.dense(layer_1, n_hidden_2)
out_layer = tf.layers.dense(layer_2, num_classes)
return out_layer
# 定义模型函数
def model_fn(features, labels, mode):
logits = neural_net(features)
# 返回的是logits中的最大值的索引号
pred_classes = tf.argmax(logits, axis=1)
pred_probas = tf.nn.softmax(logits)
# 预测模式
if mode == tf.estimator.ModeKeys.PREDICT:
return tf.estimator.EstimatorSpec(mode, predictions=pred_classes)
# 训练模式
# 损失函数
loss_op = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(
logits=logits, label=tf.cast(labels, dtype=tf.int32)))
# 优化器
optimizer = tf.train.GradientDescentOptimizer(learning_rate = learning_rate)
train_op = optimizer.minimize(loss_op, global_step=tf.train.get_global_step())
# 计算准确率
acc_op = tf.metrics.accuracy(labels=labels, predictions=pred_classes)
estim_specs = tf.estimator.EstimatorSpec(
mode=mode,
predictions=pred_classes,
loss=loss_op,
train_op=train_op,
eval_metric_ops={'accuracy':acc_op})
return estim_specs
# 构建评价器
model = tf.estimator.Estimator(model_fn)
# 训练模型
model.train(input_fn, steps=num_steps)
# 评价模型
input_fn = tf.estimator.inputs.numpy_input_fn(
x={'images': mnist.test.images}, y=mnist.test.labels,
batch_size=batch_size, shuffle=False)
model.evaluate(input_fn)
# 预测
n_images = 4
test_images = mnist.test.images[:n_images]
input_fn = tf.estimator.inputs.numpy_input_fn(
x={'images': test_images}, shuffle=False)
preds = list(model.predict(input_fn))
for i in range(n_images):
print("Model prediction:", preds[i])
高级api解释见下节。