打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
tensorflow 20 13-10 iter:0 ,test_acc:0.098 iter:10 ,test_acc:0.098 iter:20 ,test_acc:0.098
iter:0  ,test_acc:0.098
iter:10 ,test_acc:0.098
iter:20 ,test_acc:0.098
import
tensorboard
import tensorflow as tf
import time
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("/home/mj/MINIST_data", one_hot=True)

with tf.name_scope("input"):
with tf.name_scope("x_input"):
x=tf.placeholder(tf.float32,[None,784],name="x_input")
with tf.name_scope("y_input"):
y=tf.placeholder(tf.float32,[None,10],name="y_input")

#将图像reshape后,送入第一层卷积层 卷积核为5x5,1通道图像卷积为6通道
with tf.name_scope("conv1"):
x_image=tf.reshape(x,[-1,28,28,1])
conv1_filter1=tf.Variable(tf.truncated_normal([5,5,1,6]))
conv1_bias1=tf.Variable(tf.truncated_normal([6]))
conv1=tf.nn.conv2d(x_image,conv1_filter1,strides=[1,1,1,1],padding="SAME")
h_cv1=tf.nn.relu(conv1+conv1_bias1)
h_cv1_maxpool=tf.nn.max_pool(h_cv1,ksize=[1,2,2,1],strides=[1,2,2,1],padding="SAME")

#送入第二层卷积层 卷积核为5x5,6通道图像卷积为16通道
with tf.name_scope("conv2"):
conv2_filter2=tf.Variable(tf.truncated_normal([5,5,6,16]))
conv2_bias2=tf.Variable(tf.truncated_normal([16]))
conv2=tf.nn.conv2d(h_cv1_maxpool,conv2_filter2,strides=[1,1,1,1],padding="SAME")
h_cv2=tf.nn.relu(conv2+conv2_bias2)
h_cv2_maxpool=tf.nn.max_pool(h_cv2,ksize=[1,2,2,1],strides=[1,2,2,1],padding="SAME")

# 送入第三层卷积层 卷积核为5x5,16通道图像卷积为120通道
with tf.name_scope("conv3"):
conv3_filter3 = tf.Variable(tf.truncated_normal([5, 5, 16, 120]))
conv3_bias3 = tf.Variable(tf.truncated_normal([120]))
conv3 = tf.nn.conv2d(h_cv2_maxpool, conv3_filter3, strides=[1, 1, 1, 1], padding="SAME")
h_cv3 = tf.nn.relu(conv3 + conv3_bias3)


#第一个全连接层
with tf.name_scope("w_fc1"):
w_fc1=tf.Variable(tf.truncated_normal([7*7*120,80]))
b_fc1=tf.Variable(tf.truncated_normal([80]))
f_flat=tf.reshape(h_cv3,[-1,7*7*120])
h_fc1=tf.nn.relu(tf.matmul(f_flat,w_fc1)+b_fc1)

#第二个全连接层 输出层
with tf.name_scope("classfier"):
w_fc2=tf.Variable(tf.truncated_normal([80,10]))
b_fc2=tf.Variable(tf.truncated_normal([10]))
h_fc2=tf.nn.relu(tf.matmul(h_fc1,w_fc2)+b_fc2)
#损失函数
with tf.name_scope("loss_function"):
loss=tf.reduce_sum(y*tf.log(h_fc2))
tf.summary.scalar("loss",loss)
#优化器
with tf.name_scope("optimizer"):
train_step=tf.train.AdamOptimizer(0.01).minimize(loss)
#求精确du
with tf.name_scope("accuracy"):
correct=tf.equal(tf.argmax(y,1),tf.argmax(h_fc2,1))
accuracy=tf.reduce_mean(tf.cast(correct,tf.float32))
tf.summary.scalar("accuracy",accuracy)
merged=tf.summary.merge_all()

#启动会话

with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
train_writer=tf.summary.FileWriter("los1",sess.graph)
for i in range(10001):
batch_x,battch_y=mnist.train.next_batch(batch_size=200)
sess.run(train_step,feed_dict={x:batch_x,y:battch_y})
summary=sess.run(merged,feed_dict={x:batch_x,y:battch_y})
train_writer.add_summary(summary,i)
if i%10==0:
test_acc=sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})
print("iter:"+str(i)," ,test_acc:"+str(test_acc))
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
从小白开始入门python+tensorflow+cnn做人脸性别识别(一)
tf.nn.conv3d_transpose
CNN卷积神经网络原理讲解 图片识别应用(附源码)
【tensorflow速成】Tensorflow图像分类从模型自定义到测试
PyTorch下的可视化工具(网络结构/训练过程可视化)
深度学习之PyTorch实战(3)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服