import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import os
def crete_weights(shape):
# return tf.Variable(initial_value=tf.random_normal(shape=shape))
return tf.Variable(initial_value=tf.random_normal(shape=shape, stddev = 0.01))# 初始化权重的 标准差
def crete_model(x):
"""
构建卷积神经网络
:return:
"""
# 1 第一个卷积层
with tf.variable_scope("conv1"):
# 卷积层
# 将x[None, 784]进行形状上的修改
input_x = tf.reshape(x, shape = [-1, 28,28,1])
# 定义32 个filter和偏置
conv1_weights = crete_weights(shape=[5,5,1,32])
conv1_bias = crete_weights(shape=[32])
conv1_x = tf.nn.conv2d(input=input_x, filter=conv1_weights, strides=[1,1,1,1], padding="SAME") + conv1_bias
# 激活层
relu1_x = tf.nn.relu(conv1_x)
# 池化层
pool1_x = tf.nn.max_pool(value=relu1_x, ksize=[1,2,2,1], strides=[1,2,2,1], padding="SAME")
# 1 第2个卷积层
with tf.variable_scope("conv2"):
# 卷积层
# 定义32 个filter和偏置
conv2_weights = crete_weights(shape=[5, 5, 32, 64])
conv2_bias = crete_weights(shape=[64])
conv2_x = tf.nn.conv2d(input=pool1_x, filter=conv2_weights, strides=[1, 1, 1, 1],
padding="SAME") + conv2_bias
# 激活层
relu2_x = tf.nn.relu(conv2_x)
# 池化层
pool2_x = tf.nn.max_pool(value=relu2_x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="SAME")
# 全连接层
with tf.variable_scope("full_connection"):
x_fc = tf.reshape(pool2_x, shape=[-1,7*7*64])
weights_fc = crete_weights(shape=[7*7*64,10])
bias_fc = crete_weights(shape=[10])
y_predict = tf.matmul(x_fc, weights_fc) + bias_fc
# 调参 设置学习率 修改初始化权重的值 batch_normalization dropout(防止过拟合)
return y_predict
def full_connection():
"""
用全连接对手写是数字进行识别
:return:
"""
# 1 准备数据
mnist = input_data.read_data_sets('mnist_data', one_hot=True)
with tf.variable_scope("mnist_data"):
x = tf.placeholder(dtype=tf.float32, shape=(None,784))
y_true = tf.placeholder(dtype=tf.float32, shape=(None, 10))
# with tf.variable_scope("fc_model"):
# weights = tf.Variable(initial_value=tf.random_normal(shape=[784,10]))
# bias = tf.Variable(initial_value=tf.random_normal(shape=[10]))
# y_predict = tf.matmul(x,weights) + bias
y_predict = crete_model(x)
# 第一个卷积大层:
# 卷积层 32 filter
# 激活层 relu
# 赤化 大小 2 步长2
# 第二同第一
# 输入形状 [None, 28,28,1]
# weights = tf.Variable(initial_value=tf.random_normal(shape=[5,5,1,32]))
# bias = tf.Variable(initial_value=tf.random_normal(shape=[32]))
# initial_value = random_normal(shape = [F,F,3,32])
# 输出形状 [None, 28,28,32]
# 3构造损失函数
error = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_true, logits=y_predict))
# 4 优化损失
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01).minimize(error)# 一般是最多的
# optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(error)
# 5 准确率计算
equal_list = tf.equal(tf.argmax(y_true, 1),
tf.argmax(y_predict,1))# 返回的是bool型
accuracy = tf.reduce_mean(tf.cast(equal_list, tf.float32))
# 初始化变量
init = tf.global_variables_initializer()
# 开启会话
with tf.Session() as sess:
sess.run(init)
image, label = mnist.train.next_batch(100)
print("训练之前,损失为:%f" % sess.run(error, feed_dict={x:image, y_true:label}))
# 开始训练
for i in range(500):
_, loss, accuracy_value = sess.run([optimizer, error, accuracy], feed_dict={x:image, y_true:label})
print("第%d次训练的损失为%f, 准确率为% f" %(i+1, loss, accuracy_value))
return None
# 准确率 比较输出的结果最大值与真实值最大值所在的位置
# print(mnist.train.next_batch(1))
# print(mnist.train.images[0]) # 查看特征值
# print(mnist.train.labels[0]) # 目标值
# one - hot 0 1 ...9
if __name__ == "__main__":
full_connection()
# crete_model()
mnist 识别
最新推荐文章于 2024-09-27 10:11:28 发布