前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >TensorFlow入门:MNIST数据的单层逻辑回归代码单层回归代码输出结果

TensorFlow入门:MNIST数据的单层逻辑回归代码单层回归代码输出结果

作者头像
致Great
发布2018-06-07 11:05:36
6740
发布2018-06-07 11:05:36
举报
文章被收录于专栏:程序生活

单层回归代码

代码语言:javascript
复制
import tensorflow as tf
import tensorflow.examples.tutorials.mnist.input_data as input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)

# 初始化变量
X = tf.placeholder(tf.float32, [None, 784], name='image')
Y = tf.placeholder(tf.float32, [None, 10], name='label')

w = tf.get_variable(name="weights", shape=(784, 10), initializer=tf.random_normal_initializer())
b = tf.get_variable(name="bias", shape=(1, 10), initializer=tf.zeros_initializer())

# 定义计算
logits = tf.matmul(X, w) + b

# 定义损失函数
entropy = tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=Y, name="loss")
loss = tf.reduce_mean(entropy)

# 定义optimizer
optimizer = tf.train.AdamOptimizer(learning_rate=0.01).minimize(loss)

# 计算机准确率
preds = tf.nn.softmax(logits)
correct_preds = tf.equal(tf.argmax(preds, 1), tf.argmax(Y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_preds, tf.float32))

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    for i in range(10000):
        batch = mnist.train.next_batch(50)
        _, batch_loss = sess.run([optimizer, loss], feed_dict={X: batch[0], Y: batch[1]})
        print("epochs:{0}:loss:{1}".format(i, batch_loss))
    test_accuracy = sess.run(accuracy, feed_dict={X: mnist.test.images, Y: mnist.test.labels})
    print(test_accuracy)

输出结果

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018.06.06 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 单层回归代码
  • 输出结果
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档