MNIST 卷积神经网络。https://github.com/nlintz/TensorFlow-Tutorials/blob/master/05_convolutional_net.py 。
TensorFlow搭建卷积神经网络(CNN)模型,训练MNIST数据集。
构建模型。
定义输入数据,预处理数据。读取数据MNIST,得到训练集图片、标记矩阵,测试集图片标记矩阵。trX、trY、teX、teY 数据矩阵表现。trX、teX形状变为[-1,28,28,1],-1 不考虑输入图片数量,28x28 图片长、宽像素数,1 通道(channel)数量。MNIST 黑白图片,通道1。RGB彩色图像,通道3。
初始化权重,定义网络结构。卷积神经网络,3个卷积层、3个池化层、1个全连接层、1个输出层。
定义dropout占位符keep_conv,神经元保留比例。生成网络模型,得到预测值。
定义损失函数,tf.nn.softmax_cross_entropy_with_logits 比较预测值、真实值差异,做均值处理。
定义训练操作(train_op),RMSProp算法优化器tf.train.RMSPropOptimizer,学习率0.001,衰减值0.9,优化损失。
定义预测操作(predict_op)。
会话启动图,训练、评估。
#!/usr/bin/env python
import tensorflow as tf
import numpy as np
from tensorflow.examples.tutorials.mnist import input_data
batch_size = 128 # 训练批次大小
test_size = 256 # 评估批次大小
# 定义初始化权重函数
def init_weights(shape):
return tf.Variable(tf.random_normal(shape, stddev=0.01))
# 定义神经网络模型函数
# 入参:X 输入数据,w 每层权重,p_keep_conv、p_keep_hidden dropout保留神经元比例
def model(X, w, w2, w3, w4, w_o, p_keep_conv, p_keep_hidden):
# 第一组卷积层及池化层,dropout部分神经元
l1a = tf.nn.relu(tf.nn.conv2d(X, w, # l1a shape=(?, 28, 28, 32)
strides=[1, 1, 1, 1], padding='SAME'))
l1 = tf.nn.max_pool(l1a, ksize=[1, 2, 2, 1], # l1 shape=(?, 14, 14, 32)
strides=[1, 2, 2, 1], padding='SAME')
l1 = tf.nn.dropout(l1, p_keep_conv)
# 第二组卷积层及池化层,dropout部分神经元
l2a = tf.nn.relu(tf.nn.conv2d(l1, w2, # l2a shape=(?, 14, 14, 64)
strides=[1, 1, 1, 1], padding='SAME'))
l2 = tf.nn.max_pool(l2a, ksize=[1, 2, 2, 1], # l2 shape=(?, 7, 7, 64)
strides=[1, 2, 2, 1], padding='SAME')
l2 = tf.nn.dropout(l2, p_keep_conv)
# 第三组卷积层及池化层,dropout部分神经元
l3a = tf.nn.relu(tf.nn.conv2d(l2, w3, # l3a shape=(?, 7, 7, 128)
strides=[1, 1, 1, 1], padding='SAME'))
l3 = tf.nn.max_pool(l3a, ksize=[1, 2, 2, 1], # l3 shape=(?, 4, 4, 128)
strides=[1, 2, 2, 1], padding='SAME')
l3 = tf.reshape(l3, [-1, w4.get_shape().as_list()[0]]) # reshape to (?, 2048)
l3 = tf.nn.dropout(l3, p_keep_conv)
# 全连接层,dropout部分神经元
l4 = tf.nn.relu(tf.matmul(l3, w4))
l4 = tf.nn.dropout(l4, p_keep_hidden)
# 输出层
pyx = tf.matmul(l4, w_o)
return pyx # 返回预测值
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
trX, trY, teX, teY = mnist.train.images, mnist.train.labels, mnist.test.images, mnist.test.labels
# 数据预处理
trX = trX.reshape(-1, 28, 28, 1) # 28x28x1 input img
teX = teX.reshape(-1, 28, 28, 1) # 28x28x1 input img
X = tf.placeholder("float", [None, 28, 28, 1])
Y = tf.placeholder("float", [None, 10])
# 卷积核大小 3x3
# patch大小3x3,输入维度1,输出维度32
w = init_weights([3, 3, 1, 32]) # 3x3x1 conv, 32 outputs
# patch大小3x3,输入维度32,输出维度64
w2 = init_weights([3, 3, 32, 64]) # 3x3x32 conv, 64 outputs
# patch大小3x3,输入维度64,输出维度128
w3 = init_weights([3, 3, 64, 128]) # 3x3x32 conv, 128 outputs
# 全连接层,输入维度128*4*4 上层输数据三维转一维,输出维度625
w4 = init_weights([128 * 4 * 4, 625]) # FC 128 * 4 * 4 inputs, 625 outputs
# 输出层,输入维度625,输出维度10 代表10类(labels)
w_o = init_weights([625, 10]) # FC 625 inputs, 10 outputs (labels)
# 定义dropout占位符
p_keep_conv = tf.placeholder("float")
p_keep_hidden = tf.placeholder("float")
py_x = model(X, w, w2, w3, w4, w_o, p_keep_conv, p_keep_hidden) # 得到预测值
# 定义损失函数
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=py_x, labels=Y))
# 定义训练操作
train_op = tf.train.RMSPropOptimizer(0.001, 0.9).minimize(cost)
# 定义预测操作
predict_op = tf.argmax(py_x, 1)
# Launch the graph in a session
#会话启动图
with tf.Session() as sess:
# you need to initialize all variables
tf.global_variables_initializer().run()
for i in range(100):
# 训练模型
training_batch = zip(range(0, len(trX), batch_size),
range(batch_size, len(trX)+1, batch_size))
for start, end in training_batch:
sess.run(train_op, feed_dict={X: trX[start:end], Y: trY[start:end],
p_keep_conv: 0.8, p_keep_hidden: 0.5})
# 评估模型
test_indices = np.arange(len(teX)) # Get A Test Batch
np.random.shuffle(test_indices)
test_indices = test_indices[0:test_size]
print(i, np.mean(np.argmax(teY[test_indices], axis=1) ==
sess.run(predict_op, feed_dict={X: teX[test_indices],
p_keep_conv: 1.0,
p_keep_hidden: 1.0})))
MNIST 循环神经网络。 https://github.com/aymericdamien/TensorFlow-Examples/blob/master/examples/3_NeuralNetworks/recurrent_network.py 。
RNN 自然语言处理领域成功应用,机器翻译、语音识别、图像描述生成(图像特征生成描述)、语言模型与文本生成(生成模型预测下一单词概率)。Alex Graves《Supervised Sequence Labelling with Recurrent Neural Networks》 http://www.cs.toronto.edu/~graves/preprint.pdf 。
构建模型。设置训练超参数,设置学习率、训练次数、每轮训练数据大小。
RNN分类图片,每张图片行,像素序列(sequence)。MNIST图片大小28x28,28个元素序列 X 28行,每步输入序列长度28,输入步数28步。
定义输入数据、权重。
定义RNN模型。
定义损失函数、优化器(AdamOptimizer)。
定义模型预测结果、准确率计算方法。
会话启动图,开始训练,每20次输出1次准确率大小。
from __future__ import print_function
import tensorflow as tf
from tensorflow.contrib import rnn
# Import MNIST data
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)
# Training Parameters
# 设置训练超参数
learning_rate = 0.001
training_steps = 10000
batch_size = 128
display_step = 200
# Network Parameters
# 神经网络参数
num_input = 28 # MNIST data input (img shape: 28*28) 输入层
timesteps = 28 # timesteps 28 长度
num_hidden = 128 # hidden layer num of features 隐藏层神经元数
num_classes = 10 # MNIST total classes (0-9 digits) 输出数量,分类类别 0~9
# tf Graph input
# 输入数据占位符
X = tf.placeholder("float", [None, timesteps, num_input])
Y = tf.placeholder("float", [None, num_classes])
# Define weights
# 定义权重
weights = {
'out': tf.Variable(tf.random_normal([num_hidden, num_classes]))
}
biases = {
'out': tf.Variable(tf.random_normal([num_classes]))
}
# 定义RNN模型
def RNN(x, weights, biases):
# Unstack to get a list of 'timesteps' tensors of shape (batch_size, n_input)
# 输入x转换成(128 batch * 28 steps, 28 inputs)
x = tf.unstack(x, timesteps, 1)
# Define a lstm cell with tensorflow
# 基本LSTM循环网络单元 BasicLSTMCell
lstm_cell = rnn.BasicLSTMCell(num_hidden, forget_bias=1.0)
# Get lstm cell output
outputs, states = rnn.static_rnn(lstm_cell, x, dtype=tf.float32)
# Linear activation, using rnn inner loop last output
return tf.matmul(outputs[-1], weights['out']) + biases['out']
logits = RNN(X, weights, biases)
prediction = tf.nn.softmax(logits)
# Define loss and optimizer
# 定义损失函数
loss_op = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(
logits=logits, labels=Y))
# 定义优化器
optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)
train_op = optimizer.minimize(loss_op)
# Evaluate model (with test logits, for dropout to be disabled)
correct_pred = tf.equal(tf.argmax(prediction, 1), tf.argmax(Y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
# Initialize the variables (i.e. assign their default value)
init = tf.global_variables_initializer()
# Start training
with tf.Session() as sess:
# Run the initializer
sess.run(init)
for step in range(1, training_steps+1):
batch_x, batch_y = mnist.train.next_batch(batch_size)
# Reshape data to get 28 seq of 28 elements
batch_x = batch_x.reshape((batch_size, timesteps, num_input))
# Run optimization op (backprop)
sess.run(train_op, feed_dict={X: batch_x, Y: batch_y})
if step % display_step == 0 or step == 1:
# Calculate batch loss and accuracy
loss, acc = sess.run([loss_op, accuracy], feed_dict={X: batch_x,
Y: batch_y})
print("Step " + str(step) + ", Minibatch Loss= " + \
"{:.4f}".format(loss) + ", Training Accuracy= " + \
"{:.3f}".format(acc))
print("Optimization Finished!")
# Calculate accuracy for 128 mnist test images
test_len = 128
test_data = mnist.test.images[:test_len].reshape((-1, timesteps, num_input))
test_label = mnist.test.labels[:test_len]
print("Testing Accuracy:", \
sess.run(accuracy, feed_dict={X: test_data, Y: test_label}))
MNIST 无监督学习。自编码器(autoencoder)。
自编码网络。UFLDL http://ufldl.stanford.edu/wiki/index.php/Autoencoders_and_Sparsity 。
监督学习数据有标记。
自编码网络,输入样本压缩到隐藏层,解压,输出端重建样本。最终输出层神经元数量等于输入层神经元数据量。压缩,输入数据(图像、文本、声音)存在不同程度冗余信息,自动编码网络学习去掉冗余信息,有用特征输入到隐藏层。找到可以代表源数据的主要成分。激活函数不使用sigmoid等非线性函数,用线性函数,就是PCA模型。
主成分分析(principal components analysis, PCA),分析、简化数据集技术。减少数据集维数,保持数据集方差贡献最大特征。保留低阶主成分,忽略高阶主成分。最常用线性降维方法。
压缩过程,限制隐藏神经元数量,学习有意义特征。希望神经元大部分时间被抑制。神经元输出接近1为被激活,接近0为被抑制。部分神经元处于被抑制状态,稀疏性限制。
多个隐藏层,输入数据图像,第一层学习识别边,第二层学习组合边,构成轮廓、角,更高层学习组合更有意义特征。
TensorFlow自编码网络实现。 https://github.com/aymericdamien/TensorFlow-Examples/blob/master/examples/3_NeuralNetworks/autoencoder.py 。
构建模型。设置超参数,学习率、训练轮数(epoch)、每次训练数据多少、每隔多少轮显示一次训练结果。
定义输入数据,无监督学习只需要图片数据,不需要标记数据。
初始化权重,定义网络结构。2个隐藏层,第一个隐藏层神经元256个,第二个隐藏层神经元128个。包括压缩、解压过程。
构建损失函数、优化器。损失函数“最小二乘法”,原始数据集和输出数据集平方差取均值运算。优化器用RMSPropOptimizer。
训练数据、评估模型。对测试集应用训练好的自动编码网络。比较测试集原始图片和自动编码网络重建结果。
from __future__ import division, print_function, absolute_import
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
# Import MNIST data
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)
# Training Parameters
# 设置训练超参数
learning_rate = 0.01 # 学习率
num_steps = 30000 # 训练轮数
batch_size = 256 # 每次训练数据多少
display_step = 1000 # 每隔多少轮显示训练结果
examples_to_show = 10 # 测试集选10张图片验证自动编码器结果
# Network Parameters
# 网络参数
# 第一个隐藏层神经元个数,特征值个数
num_hidden_1 = 256 # 1st layer num features
# 第二个隐藏层神经元个数,特征值个数
num_hidden_2 = 128 # 2nd layer num features (the latent dim)
# 输入数据特征值个数 28x28=784
num_input = 784 # MNIST data input (img shape: 28*28)
# tf Graph input (only pictures)
# 定义输入数据,只需要图片,不要需要标记
X = tf.placeholder("float", [None, num_input])
# 初始化每层权重和偏置
weights = {
'encoder_h1': tf.Variable(tf.random_normal([num_input, num_hidden_1])),
'encoder_h2': tf.Variable(tf.random_normal([num_hidden_1, num_hidden_2])),
'decoder_h1': tf.Variable(tf.random_normal([num_hidden_2, num_hidden_1])),
'decoder_h2': tf.Variable(tf.random_normal([num_hidden_1, num_input])),
}
biases = {
'encoder_b1': tf.Variable(tf.random_normal([num_hidden_1])),
'encoder_b2': tf.Variable(tf.random_normal([num_hidden_2])),
'decoder_b1': tf.Variable(tf.random_normal([num_hidden_1])),
'decoder_b2': tf.Variable(tf.random_normal([num_input])),
}
# Building the encoder
# 定义压缩函数
def encoder(x):
# Encoder Hidden layer with sigmoid activation #1
layer_1 = tf.nn.sigmoid(tf.add(tf.matmul(x, weights['encoder_h1']),
biases['encoder_b1']))
# Encoder Hidden layer with sigmoid activation #2
layer_2 = tf.nn.sigmoid(tf.add(tf.matmul(layer_1, weights['encoder_h2']),
biases['encoder_b2']))
return layer_2
# Building the decoder
# 定义解压函数
def decoder(x):
# Decoder Hidden layer with sigmoid activation #1
layer_1 = tf.nn.sigmoid(tf.add(tf.matmul(x, weights['decoder_h1']),
biases['decoder_b1']))
# Decoder Hidden layer with sigmoid activation #2
layer_2 = tf.nn.sigmoid(tf.add(tf.matmul(layer_1, weights['decoder_h2']),
biases['decoder_b2']))
return layer_2
# Construct model
# 构建模型
encoder_op = encoder(X)
decoder_op = decoder(encoder_op)
# Prediction
# 得出预测值
y_pred = decoder_op
# Targets (Labels) are the input data.
# 得出真实值,即输入值
y_true = X
# Define loss and optimizer, minimize the squared error
# 定义损失函数、优化器
loss = tf.reduce_mean(tf.pow(y_true - y_pred, 2))
optimizer = tf.train.RMSPropOptimizer(learning_rate).minimize(loss)
# Initialize the variables (i.e. assign their default value)
init = tf.global_variables_initializer()
# Start Training
# Start a new TF session
with tf.Session() as sess:
# Run the initializer
sess.run(init)
# Training
# 开始训练
for i in range(1, num_steps+1):
# Prepare Data
# Get the next batch of MNIST data (only images are needed, not labels)
batch_x, _ = mnist.train.next_batch(batch_size)
# Run optimization op (backprop) and cost op (to get loss value)
_, l = sess.run([optimizer, loss], feed_dict={X: batch_x})
# Display logs per step
# 每一轮,打印出一次损失值
if i % display_step == 0 or i == 1:
print('Step %i: Minibatch Loss: %f' % (i, l))
# Testing
# Encode and decode images from test set and visualize their reconstruction.
n = 4
canvas_orig = np.empty((28 * n, 28 * n))
canvas_recon = np.empty((28 * n, 28 * n))
for i in range(n):
# MNIST test set
batch_x, _ = mnist.test.next_batch(n)
# Encode and decode the digit image
g = sess.run(decoder_op, feed_dict={X: batch_x})
# Display original images
for j in range(n):
# Draw the original digits
canvas_orig[i * 28:(i + 1) * 28, j * 28:(j + 1) * 28] = \
batch_x[j].reshape([28, 28])
# Display reconstructed images
for j in range(n):
# Draw the reconstructed digits
canvas_recon[i * 28:(i + 1) * 28, j * 28:(j + 1) * 28] = \
g[j].reshape([28, 28])
print("Original Images")
plt.figure(figsize=(n, n))
plt.imshow(canvas_orig, origin="upper", cmap="gray")
plt.show()
print("Reconstructed Images")
plt.figure(figsize=(n, n))
plt.imshow(canvas_recon, origin="upper", cmap="gray")
plt.show()
参考资料:
《TensorFlow技术解析与实战》
欢迎推荐上海机器学习工作机会,我的微信:qingxingfengzi