温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

tensorflow mnist模型怎么实现

发布时间:2021-12-08 14:10:07 来源:亿速云 阅读:192 作者:iii 栏目:大数据

这篇文章主要介绍“tensorflow mnist模型怎么实现”,在日常操作中,相信很多人在tensorflow mnist模型怎么实现问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”tensorflow mnist模型怎么实现”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

所有的ML模型或者DL 模型 都是下面这四个固定套路的步骤

1.获取到所需数据

2.开始搭建模型

3.计算采用何种loss函数

4.选择batch,epoch,feed数据 

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

mnist = input_data.read_data_sets('./tmp/tensorflow/mnist/input_data',one_hot=True) # 下载数据
x = tf.placeholder(tf.float32,[None,784]) # 输入占位符
yresult = tf.placeholder(tf.float32,[None,10]) #输入数据真实的label

w = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))
y = tf.nn.softmax(tf.matmul(x,w) + b) # 用不用激励函数 都可以的其实
cross_entropy = -tf.reduce_sum(yresult * tf.log(y)) # loss  值
train_setp = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy) #梯度下降法
init  = tf.initialize_all_variables()

with tf.Session() as sess:
    sess.run(init)
    for i in range(1000):
        batch_xs, batch_ys = mnist.train.next_batch(100)
        argv1,loss = sess.run([train_setp,cross_entropy],feed_dict={x:batch_xs,yresult:batch_ys}) #如果想知道corss_entropy试试变化值 加入就好。
        if i % 200 == 0:
            print (loss)

    current_prediction = tf.equal(tf.argmax(y,1),tf.argmax(yresult,1)) # compare real and calculate
    accuracy = tf.reduce_mean(tf.cast(current_prediction,tf.float32))  # 数据类型转换 然后求匹配上的概率
    result = sess.run(accuracy,feed_dict={x:mnist.test.images,yresult:mnist.test.labels}) # test数据入口
    print(str(result * 100) + '%')

到此,关于“tensorflow mnist模型怎么实现”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI