Tensorflow卷积神经网络

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<button id="btn0" onclick="view_code('pre0');">view code</button>'''
##卷积神经网络,两个卷积层:32和64个特征平面,两个全连接层1024和10个神经元
'''
#加载数据,设定batch
mnist = input_data.read_data_sets('MNIST_data/',one_hot=True)
batch_size = 100
n_batch = mnist.train.num_examples // batch_size
 
#初始化权值
def weight_var(shape):
    return tf.Variable(tf.truncated_normal(shape,stddev=0.1))
 
#初始化偏移值
def bias_var(shape):
    return tf.Variable(tf.constant(0.1,shape=shape))
 
#卷积操作
def conv2d(x,W):
    return tf.nn.conv2d(x,W,strides=[1,1,1,1],padding='SAME')
 
#池化操作
def max_pool_2x2(x):
    return tf.nn.max_pool(x,strides=[1,2,2,1],ksize=[1,2,2,1],padding='SAME')
 
#定义三个占位符,数据,标签和dropout
x = tf.placeholder(tf.float32,shape=[None,784])
y = tf.placeholder(tf.float32,shape=[None,10])
keep_prob = tf.placeholder(tf.float32)
 
#把x变成一个4d向量,其第2、第3维对应图片的宽、高,最后一维代表图片的颜色通道数
x_image = tf.reshape(x,[-1,28,28,1])
 
#卷积层1,32个特征平面,卷积操作后[-1,28,28,32],池化操作后[-1,14,14,32]
W_conv1 = weight_var([5,5,1,32])
b_conv1 = bias_var([32])
h_conv1 = tf.nn.relu(conv2d(x_image,W_conv1)+b_conv1)
h_pool1 = max_pool_2x2(h_conv1)
 
#卷积层2,64个特征平面,卷积操作后[-1,14,14,64],池化操作后[-1,7,7,64]
W_conv2 = weight_var([5,5,32,64])
b_conv2 = bias_var([64])
h_conv2 = tf.nn.relu(conv2d(h_pool1,W_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2)
 
#全连接层1,1024个神经元,先将卷积层2的输出扁平化处理
h_pool2_flat = tf.reshape(h_pool2,[-1,7*7*64])
W_fc1 = weight_var([7*7*64,1024])
b_fc1 = bias_var([1024])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat,W_fc1) + b_fc1)
h_fc1 = tf.nn.dropout(h_fc1,keep_prob)
 
#全连接层2,10个神经元
W_fc2 = weight_var([1024,10])
b_fc2 = bias_var([10])
y_ = tf.nn.softmax(tf.matmul(h_fc1,W_fc2)+b_fc2)
 
#交叉熵损失函数,Adam优化器
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=y_))
train = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
 
#准确率
correct = tf.equal(tf.argmax(y,1),tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct,tf.float32))
 
#变量初始化
init = tf.global_variables_initializer()
 
with tf.Session() as sess:
    sess.run(init)
    for iteration in range(21):
        for batch in range(n_batch):
            train_xs,train_ys = mnist.train.next_batch(batch_size)
            sess.run(train,feed_dict={x:train_xs,y:train_ys,keep_prob:0.5})
             
        print('iter: ',iteration,'accuracy: ',sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels,keep_prob:1}))

 

posted @   hou永胜  阅读(145)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示