预处理
- tensorflow库内包含mnist,直接加载mnist数据并转为一维数组形式。直接加载的是.gz格式。
import tensorflow.examples.tutorials.mnist.input_data as input_data # 加载mnist数据
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) # one_hot为是否将标签转为一维数组形式
逻辑回归
- 加载数据
- 图片转为一维数组
- 建立模型:softmax回归模型
- w为可变n*784二维矩阵,b为10数组
- w、b变量初始化为0
- y=w*x+b
- 损失函数:交叉熵
- 训练模型
- 模型评估
# -*- coding: utf-8 -*-
# 读取数据图片,预处理
import tensorflow as tf
import tensorflow.examples.tutorials.mnist.input_data as input_data # 加载mnist数据
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) # one_hot为是否将标签转为一维数组形式
# 构建softmax回归模型
sess = tf.InteractiveSession() # 交互作用类(不需用在计算前构建整个图)
# 占位符(x和y_)
x = tf.placeholder("float", shape=[None, 784]) # 浮点数,二维数组(第一维大小不定,第二维是784)
y_ = tf.placeholder("float", shape=[None, 10]) # 用于代表对应某一MNIST图片的类别
# 变量(w权重和b偏置)
w = tf.Variable(tf.zeros([784, 10])) # 784*10的可变参数值二维矩阵
b = tf.Variable(tf.zeros([10])) # 10维的向量
sess.run(tf.initialize_all_variables()) # 初始化所有变量为0
# 类别预测与损失函数
y = tf.nn.softmax(tf.matmul(x, w) + b) # 计算每个分类的softmax概率值
cross_entropy = -tf.reduce_sum(y_ * tf.log(y)) # 损失函数(目标类别和预测类别之间的交叉熵)
# 训练模型
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy) # 最速下降法,步长为0.01
for i in range(1000):
batch = mnist.train.next_batch(50) # 每步加载50个样本
train_step.run(feed_dict={x: batch[0], y_: batch[1]}) # feed_dict被每次训练的数据替代
# 模型评估
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) # 检测预测值与实际值是否匹配
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float")) # 将布尔数组转化为正确率
print(accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels})) # 输出最终正确率
神经网络
- 加载数据集
- 定义函数:
- 初始化函数
- 神经元模型:mlp、逻辑回归、s函数
- 照片转一维数组、确定测试、训练的照片、标签
- 占位符:定义张量
- 调用神经元模型函数
- 计算代价函数、构造优化器、求行最值
- 初始化变量、迭代100次
- 打印正确率
# -*- coding: utf-8 -*-
# 神经网络
import tensorflow as tf
import numpy as np
from tensorflow.examples.tutorials.mnist import input_data # 加载数据
def init_weight(shape): # 初始化
return tf.Variable(tf.random_normal(shape, stddev=0.01)) # (stddev是标准差,random_normal是正态分布的随机输出值)张量的可变随机值
def model(X, w_h, w_o): # 神经元模型
h = tf.nn.sigmoid(tf.matmul(X, w_h)) # 这是一个基本的mlp,两个堆栈逻辑回归 # X和w_h矩阵相乘,s函数
return tf.matmul(h, w_o) # 在最后不使用softmax,因为代价函数 # 返回h和w_o的两矩阵之积
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) # 把数据转换为一位数组的格式
trX = mnist.train.images # 训练的图片
trY = mnist.train.labels # 训练的标签
teX = mnist.test.images # 测试的图片
teY = mnist.test.labels # 测试的标签
# 占位符
X = tf.placeholder("float", [None, 784]) # 第一维长度不定,第二维长度为784的二维矩阵 浮点数 784=28*28
Y = tf.placeholder("float", [None, 10]) # 输出的10种情况
w_h = init_weight([784, 625]) # 创建特征变量 # 调用上边自定义的函数,对矩阵进行初始化
w_o = init_weight([625, 10]) # 初始化
py_x = model(X, w_h, w_o) # 调用上边的自定义函数,
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=py_x, labels=Y)) # 计算代价
train_op = tf.train.GradientDescentOptimizer(0.05).minimize(cost) # 构造优化器 # 0.05的学习率使代价函数最小
predict_op = tf.argmax(py_x, 1) # 求行中最大值
# 在会话中启动图表
with tf.Session() as sess:
# 初始化所有变量
tf.global_variables_initializer().run() # 添加用于初始化变量的节点,
for i in range(100): # 迭代100次
for start, end in zip(range(0, len(trX)), range(128, len(trX)+1, 128)): # zip函数是将两个列表打包为元组的列表,元素个数与最短列表一致
sess.run(train_op, feed_dict={X: trX[start: end], Y: trY[start:end]}) # 跑
print(i, np.mean(np.argmax(teY, axis=1) ==
sess.run(predict_op, feed_dict={X: teX}))) # mean函数是求平均值,打印预测和实际相同时的概率
# for i in range(100):
# for start, end in zip(range(0, 1000), range(128, 1000+1, 128)):
# sess.run(train_op, feed_dict={X: trX[start: end], Y: trY[start:end]})
# print(i, np.mean(np.argmax(teY, axis=1) ==
# sess.run(predict_op, feed_dict={X: teX})))
卷积神经网络
- CNN
- 输入层
- 卷积层
- 激活函数
- 池化层
- 全连接层
卷积就是为了降维
池化就是数据压缩,特征压缩(提取主要特征)
- 加载数据
- 定义初始化函数、定义模型函数(relu、max_pool、dropout)
- 图片转一维数组
- 张量、初始化
- 调用模型函数
- 训练的下降率
- argmax
- 迭代10次
- 测试集是打乱的(np.random.shuffle)
- 打印准确率
# -*- coding: utf-8 -*-
#
#
# # # 读取数据图片,预处理
# # import tensorflow as tf
# # import tensorflow.examples.tutorials.mnist.input_data as input_data # 加载mnist数据
# # mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) # one_hot为是否将标签转为一维数组形式
#
#
# # 构建一个多层卷积神经网络
# import tensorflow as tf
#
#
# # 权重初始化
# def weight_variable(shape): # 权重
# initial = tf.truncated_normal(shape, stddev=0.01)
# return tf.Variable(initial)
#
#
# def bias_variable(shape): # 偏置
# initial = tf.constant(0.1, shape=shape)
# return tf.Variable(initial)
#
#
# # 卷积和池化
# def conv2d(x, W):
# return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME') # 1步长的卷积,0边距的模板(用0填充边界)
#
#
# def max_pool_2x2(x): # 池化使用2*2的模板
# return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
# strides=[1, 2, 2, 1], padding='SAME')
#
#
# # 第一层卷积
# W_conv1 = weight_variable([5, 5, 1, 32]) # 卷积在每个5*5的patch中算出32个特征
# b_conv1 = bias_variable([32]) # 每个输出的通道都有一个的对应的偏置量
#
# x_image = tf.reshape(x, [-1, 28, 28, 1]) # x变为一个4d向量,第2、3维是图片的宽、高,第4维是图片的颜色通道数
#
# h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) # 把x_image和权值向量进行卷积,加上偏置项,然后使用RELU函数
# h_pool1 = max_pool_2x2(h_conv1) # 最后进行max_pooling(四个像素点中选取最大的)
#
#
# # 第二层卷积
# W_conv2 = weight_variable([5, 5, 32, 64]) # 每个5*5的patch中算出64个特征
# b_conv2 = bias_variable([64])
#
# h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
# h_pool2 = max_pool_2x2(h_conv2)
#
#
# # 密集层网络
# W_fc1 = weight_variable([7*7*64, 1024]) # 图片尺寸减少到了7*7,加入一个有1024个神经元的全连接层
# b_fc1 = bias_variable([1024])
#
# h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64]) # 把池化层输出的张量reshape为一些向量
# h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1) # 乘上权重矩阵,加上偏置,然后使用RELU
#
#
# # Droput(减少过拟合)
# keep_prob = tf.placeholder("float") # 训练中启用dropout,测试中关闭的dropout
# h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
#
#
# # 输出层(添加一个softmax层)
# W_fc2 = weight_variable([1024, 10])
# b_fc2 = bias_variable([10])
#
# y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2) # softmax函数
#
#
# # 训练和模型评估(ADAM优化器做梯度下降,每100次迭代输出一次日志)
# cross_entropy = -tf.reduce_sum(y_ * tf.log(y_conv))
# train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
# correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))
# accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
# sess.run(tf.initialize_all_variables())
# for i in range(20000):
# batch = mnist.train.next_batch(50)
# if i % 100 == 0:
# train_accuracy = accuracy.eval(feed_dict={
# x: batch[0], y_: batch[1], keep_prob: 1.0
# })
# print('step %d, training accuracy %g' % i, train_accuracy)
# train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})
#
# print('test accuracy %g' % accuracy.eval(feed_dict={
# x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0
# }))
#
#
# ----------------------------------------------------------------------------------------------------------------------
#
# # # -*- coding: utf-8 -*-
#
#
# 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))
#
#
# def model(X, w, w2, w3, w4, w_o, p_keep_conv, p_keep_hidden):
# l1a = tf.nn.relu(tf.nn.conv2d(X, w,
# strides=[1, 1, 1, 1], padding='SAME'))
# l1 = tf.nn.max_pool(l1a, ksize=[1, 2, 2, 1],
# strides=[1, 2, 2, 1], padding='SAME')
# l1 = tf.nn.dropout(l1, p_keep_conv)
#
# l2a = tf.nn.relu(tf.nn.conv2d(l1, w2,
# strides=[1, 1, 1, 1], padding='SAME'))
# l2 = tf.nn.max_pool(l2a, ksize=[1, 2, 2, 1],
# strides=[1, 2, 2, 1], padding='SAME')
# l2 = tf.nn.dropout(l2, p_keep_conv)
#
# l3a = tf.nn.relu(tf.nn.conv2d(l2, w3,
# strides=[1, 1, 1, 1], padding='SAME'))
# l3 = tf.nn.max_pool(l3a, ksize=[1, 2, 2, 1],
# strides=[1, 2, 2, 1], padding='SAME')
# l3 = tf.reshape(l3, [-1, w4.get_shape().as_list()[0]])
# l3 = tf.nn.dropout(l3, p_keep_conv)
#
# l4 = tf.nn.relu(tf.matmul(l3, w4))
# l4 = tf.nn.dropout(l4, p_keep_conv)
#
# 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)
# teX = teX.reshape(-1, 28, 28, 1)
#
#
# X = tf.placeholder("float", [None, 28, 28, 1])
# Y = tf.placeholder("float", [None, 10])
#
#
# w = init_weights([3, 3, 1, 32])
# w2 = init_weights([3, 3, 32, 64])
# w3 = init_weights([3, 3, 64, 128])
# w4 = init_weights([128 * 4 * 4, 625])
# w_o = init_weights([625, 10])
#
#
# 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)
#
#
# #
# with tf.Session() as sess:
# #
# tf.global_variables_initializer().run()
#
# for i in range(10):
# train_batch = zip(range(0, len(trX), batch_size),
# range(batch_size, len(trX) + 1, batch_size))
# for start, end in train_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))
# 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: teY[test_indices],
# Y: teY[test_indices],
# p_keep_conv: 1.0,
# p_keep_hidden: 1.0})))
# ----------------------------------------------------------------------------------------------------------------------
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))
def model(X, w, w2, w3, w4, w_o, p_keep_conv, p_keep_hidden): # 定义的模型函数
l1a = tf.nn.relu(tf.nn.conv2d(X, w,
strides=[1, 1, 1, 1], padding='SAME')) # l1a shape=(?, 28, 28, 32)
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)
l2a = tf.nn.relu(tf.nn.conv2d(l1, w2,
strides=[1, 1, 1, 1], padding='SAME')) # l2a shape=(?, 14, 14, 64)
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)
l3a = tf.nn.relu(tf.nn.conv2d(l2, w3,
strides=[1, 1, 1, 1], padding='SAME')) # l3a shape=(?, 7, 7, 128)
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)
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]) # 张量 浮点数 4维矩阵
Y = tf.placeholder("float", [None, 10])
w = init_weights([3, 3, 1, 32]) # 3x3x1 conv, 32 outputs
w2 = init_weights([3, 3, 32, 64]) # 3x3x32 conv, 64 outputs
w3 = init_weights([3, 3, 64, 128]) # 3x3x32 conv, 128 outputs
w4 = init_weights([128 * 4 * 4, 625]) # FC 128 * 4 * 4 inputs, 625 outputs
w_o = init_weights([625, 10]) # FC 625 inputs, 10 outputs (labels)
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) # 求行中最大
#
with tf.Session() as sess:
# 初始化所有可变值
tf.global_variables_initializer().run() # 跑
for i in range(10): # 迭代10次
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)) # 得到一个测试批
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],
Y: teY[test_indices],
p_keep_conv: 1.0,
p_keep_hidden: 1.0}))) # 打印预准率