一、模型基本知识
-
卷积神经网络
-
卷积
-
采样
二、模型定义
本文以CNN卷积神经网络原型为示例:
-
初始化输入层,转换成统一尺寸大小:对于本例子中全部resize成28*28大小
代码块
Python
input_layer = tf.reshape(features["x"], [-1, 28, 28, 1])
-
定义第一层,卷积层,卷积核大小和数量:卷积核数量32个,每个卷积核大小5✖️5
代码块
Python
conv1 = tf.layers.conv2d(
inputs=input_layer,
filters=32,
kernel_size=[5, 5],
padding="same",
activation=tf.nn.relu)
-
定义第二层,池化层,所采用的池化方法为最大池化方法,池化的batch大小为:2✖️2,步长为2
代码块
Python
pool1 = tf.layers.max_pooling2d(inputs=conv1, pool_size=[2, 2], strides=2)
-
定义第三层,卷积层,卷积核大小和数量:卷积核数量64个,每个卷积核大小5✖️5
代码块
Python
conv2 = tf.layers.conv2d( inputs=pool1, filters=64, kernel_size=[5, 5], padding="same", activation=tf.nn.relu)
-
定义第四层,池化层,所采用的池化方法为最大池化方法,池化的batch大小为:2✖️2,步长为2
代码块
Python
pool2 = tf.layers.max_pooling2d(inputs=conv2, pool_size=[2, 2], strides=2)
-
定义第五层,全连接层,将第四层降采样之后的数据resize层7*7*64以为向量大小
代码块
Python
# Dense Layer pool2_flat = tf.reshape(pool2, [-1, 7 * 7 * 64]) dense = tf.layers.dense(inputs=pool2_flat, units=1024, activation=tf.nn.relu) dropout = tf.layers.dropout( inputs=dense, rate=0.4, training=mode == tf.estimator.ModeKeys.TRAIN) # Logits Layer logits = tf.layers.dense(inputs=dropout, units=10)
-
定义第五层,利用softmax算法计算预测每个类别的概率:
---训练样本,更新损失函数,更新梯度
---测试样本,返回预测结果值
三、训练数据代码
过程:读取数据-》构建结果评估模型-》训练样本-》预测样本
# -*- coding: UTF-8 -*-
import tensorflow as tf
import numpy as np
import cnn_mnist
import input_data
# Load training and eval data
#mnist = tf.contrib.learn.datasets.load_dataset("/Users/dingjie/PycharmProjects/baby-robot/minist/MNIST-data/")
#mnist = tf.contrib.learn.datasets.load_dataset("MNIST-data")
mnist = input_data.read_data_sets("MNIST_data/")
train_data = mnist.train.images # Returns np.array
train_labels = np.asarray(mnist.train.labels, dtype=np.int32)
eval_data = mnist.test.images # Returns np.array
eval_labels = np.asarray(mnist.test.labels, dtype=np.int32)
# Create the Estimator ---构建评估模型
mnist_classifier = tf.estimator.Estimator(
model_fn=cnn_mnist.cnn_model_fn, model_dir="model")
# Set up logging for predictions
tensors_to_log = {"probabilities": "softmax_tensor"}
logging_hook = tf.train.LoggingTensorHook(
tensors=tensors_to_log, every_n_iter=50)
# Train the model ---初始化训练数据
train_input_fn = tf.estimator.inputs.numpy_input_fn(
x={"x": train_data},
y=train_labels,
batch_size=100,
num_epochs=None,
shuffle=True)
#训练数据,得到训练模型
mnist_classifier.train(
input_fn=train_input_fn,
steps=20000,
hooks=[logging_hook])
# Evaluate the model and print results ----结果评估
eval_input_fn = tf.estimator.inputs.numpy_input_fn(
x={"x": eval_data},
y=eval_labels,
num_epochs=1,
shuffle=False)
eval_results = mnist_classifier.evaluate(input_fn=eval_input_fn)
print(eval_results)