python 专栏收录该内容
8 篇文章 0 订阅

# 非解释版算法

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

batch_size = 100

n_batch = mnist.train.num_examples // batch_size

x = tf.placeholder(tf.float32,[None,784])
y = tf.placeholder(tf.float32,[None,10])

W = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))
prediction = tf.nn.softmax(tf.matmul(x,W)+b)

loss = tf.reduce_mean(tf.square(y-prediction))

init = tf.global_variables_initializer()

correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))

accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))

with tf.Session() as sess:
sess.run(init)
for epoch in range(21):
for batch in range(n_batch):
batch_xs,batch_ys =  mnist.train.next_batch(batch_size)
sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys})

acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})
print("Iter " + str(epoch) + ",Testing Accuracy " + str(acc))



# 解释版算法

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

#载入数据集

#每个批次的大小
batch_size = 100
#计算一共有多少个批次
n_batch = mnist.train.num_examples // batch_size

#定义两个placeholder
x = tf.placeholder(tf.float32,[None,784])
y = tf.placeholder(tf.float32,[None,10])

#创建一个简单的神经网络
W = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))
prediction = tf.nn.softmax(tf.matmul(x,W)+b)

#二次代价函数
loss = tf.reduce_mean(tf.square(y-prediction))
#使用梯度下降法

#初始化变量
init = tf.global_variables_initializer()

#结果存放在一个布尔型列表中
#tf.equal返回是True或False
#argmax返回一维张量中最大的值所在的位置
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))
#求准确率，tf.cast(...)是将True和False转化为1和0
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))

with tf.Session() as sess:
sess.run(init)
for epoch in range(21):
for batch in range(n_batch):
batch_xs,batch_ys =  mnist.train.next_batch(batch_size)
sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys})

acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})
print("Iter " + str(epoch) + ",Testing Accuracy " + str(acc))



# 优化算法入口

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

#提升算法精确度的第一个入口,batch_size缩小10倍，acc越上升0.015
batch_size = 100
n_batch = mnist.train.num_examples//batch_size

x = tf.placeholder(tf.float32,[None,784])
y = tf.placeholder(tf.float32,[None,10])
#定义隐藏层是第二个入口
W = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))
prediction = tf.nn.softmax(tf.matmul(x,W)+b)

#学习率0.2是提升的第三个入口，变为0.5约上升0.008
loss = tf.reduce_mean(tf.square(y-prediction))

init = tf.compat.v1.global_variables_initializer()
corrent_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))

accuracy = tf.reduce_mean(tf.cast(corrent_prediction,tf.float32))

with tf.Session() as sess:
sess.run(init)
#循环epoch是第四个入口，21变为41约上升0.0056
for epoch in range(21):
for batch in range(n_batch):
batch_xs,batch_ys = mnist.train.next_batch(batch_size)
sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys})

acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})
print('Iter '+str(epoch)+',Testing Accuracy: '+str(acc))

• 0
点赞
• 0
评论
• 0
收藏
• 打赏
• 扫一扫，分享海报

01-07 362

11-08 3171
04-13 2827
07-30 532
07-30 658
04-12 5061
03-15 3053
04-29 1614
08-09 2406
09-27 104
©️2022 CSDN 皮肤主题：数字20 设计师：CSDN官方博客

¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。