Tai*_*ian 5 python mnist tensorflow
我是机器学习的新手,我正在关注tensorflow的教程,创建一些简单的神经网络来学习MNIST数据.
我建立了一个单层网络(遵循tutotial),准确度约为0.92,这对我来说还可以.但后来又添加了一层,精度降低到0.113,这非常糟糕.
以下是2层之间的关系:
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])
Run Code Online (Sandbox Code Playgroud)
我的结构好吗?导致它表现如此糟糕的原因是什么?我该如何修改我的网络?
第二层的输入是softmax第一层的输出.你不想那样做.
你强迫这些值之和为1.如果某个值tf.matmul(x, W1) + b1约为0(有些肯定是),则softmax操作将此值降低为0.结果:你正在消除梯度,没有任何东西可以流过低谷这些神经元.
如果你删除图层之间的softmax(但是如果你想将值视为概率,则将它输出到输出图层上的softmax)你的网络将正常工作.
TL;博士:
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])
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8583 次 |
| 最近记录: |