程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

如何使用TensorFlow和python在MNIST数据上创建2层神经网络

发布于2019-10-25 18:58     阅读(1002)     评论(0)     点赞(29)     收藏(4)


我是机器学习的新手,我正在按照tensorflow的教程创建一些简单的神经网络,以学习MNIST数据。

我已经建立了一个单层网络(遵循tutotial),精度约为0.92,对我来说还可以。但是后来我又增加了一层,精度降低到0.113,这是非常糟糕的。

以下是两层之间的关系:

import tensorflow as tf
x = tf.placeholder(tf.float32, [None, 784])

#layer 1
W1 = tf.Variable(tf.zeros([784, 100]))
b1 = tf.Variable(tf.zeros([100]))
y1 = tf.nn.softmax(tf.matmul(x, W1) + b1)

#layer 2
W2 = tf.Variable(tf.zeros([100, 10]))
b2 = tf.Variable(tf.zeros([10]))
y2 = tf.nn.softmax(tf.matmul(y1, W2) + b2)

#output
y = y2
y_ = tf.placeholder(tf.float32, [None, 10])

我的结构还好吗?是什么原因使其性能如此差?我应该如何修改我的网络?


解决方案


第二层的输入是softmax第一层的输出。你不想那样做。

您将这些值的总和强制为1。如果某值的tf.matmul(x, W1) + b1大约为0(某些确实是),则softmax操作会将其降低为0。结果:您正在终止梯度,没有任何东西可以流过槽这些神经元。

如果您删除了各层之间的softmax(但如果您想将这些值视为概率,则将其保留在输出层上),则网络将正常工作。

Tl; dr:

import tensorflow as tf
x = tf.placeholder(tf.float32, [None, 784])

#layer 1
W1 = tf.Variable(tf.zeros([784, 100]))
b1 = tf.Variable(tf.zeros([100]))
y1 = tf.matmul(x, W1) + b1 #remove softmax

#layer 2
W2 = tf.Variable(tf.zeros([100, 10]))
b2 = tf.Variable(tf.zeros([10]))
y2 = tf.nn.softmax(tf.matmul(y1, W2) + b2)

#output
y = y2
y_ = tf.placeholder(tf.float32, [None, 10])


所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接:https://www.pythonheidong.com/blog/article/145699/17bafa8c9e1f5ce71127/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

29 0
收藏该文
已收藏

评论内容:(最多支持255个字符)