caicloud / tensorflow-tutorial

Example TensorFlow codes and Caicloud TensorFlow as a Service dev environment.
2.93k stars 2.08k forks source link

感觉Chapter03/3. 完整神经网络样例程序.ipynb的Loss函数定义有问题 #88

Closed ABadCandy closed 6 years ago

ABadCandy commented 6 years ago

原程序为二分类,类别标签为y_= tf.placeholder(tf.float32, shape=(None, 1), name='y-input')即标量, 而程序中定义cross_entropy = -tf.reducemean(y tf.log(tf.clip_by_value(y, 1e-10, 1.0))) 个人认为根据公式定义,二分类交叉熵应为binary_cross_entropy = -tf.reducesum(ytf.log(y) + (1 - y_) * tf.log(1 - y)) (此处省略了clip函数),请问这里是否有问题呢?

perhapszzy commented 6 years ago

这是个好问题,作为二分类问题确实应该有后面那一项。感谢为我们指出这个问题,我们会尽快修改。

KiXiang commented 6 years ago

@ABadCandy @perhapszzy cross_entropy = -tf.reducemean(y tf.log(tf.clip_byvalue(y,1e-10,1.0)) + (1 - y) tf.log(1 - tf.log(1 - tf.clip_by_value(y,1e-10,0.9999999))) 请问这样修改可否,训练结果不正确,这个例程该如何修改并根据W1和W2分类呢?,谢谢

ABadCandy commented 6 years ago

根据logistic regression的交叉熵损失函数的公式来看应该是改成: binary_cross_entropy = -tf.reducemean(y tf.log(tf.clip_byvalue(y,1e-10,1.0)) + (1 - y) tf.log(1 - tf.clip_by_value(y,1e-10,1.0)) 吧,其中y非0即1;而如果把标签改成二分类的one-hot编码(即y=(0,1)或y_=(1,0))则用书中的这个代码应该就是对的了,个人意见。 或者用tf自带的softmaxloss函数验证下 发送自 Windows 10 版邮件应用

发件人: 南望山下 发送时间: 2018年1月3日 13:58 收件人: caicloud/tensorflow-tutorial 抄送: Hao Tang; Mention 主题: Re: [caicloud/tensorflow-tutorial] 感觉Chapter03/3. 完整神经网络样例程序.ipynb的Loss函数定义有问题 (#88)

@ABadCandy@perhapszzy cross_entropy = -tf.reducemean(y tf.log(tf.clip_byvalue(y,1e-10,1.0)) + (1 - y) tf.log(1 - tf.log(1 - tf.clip_by_value(y,1e-10,0.9999999))) 请问这样修改可否,训练结果不正确,这个例程该如何修改并根据W1和W2分类呢?,谢谢 — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

KiXiang commented 6 years ago

@ABadCandy 感谢你回复,标签改成二分类的one-hot编码(即y=(0,1)或y=(1,0))则用书中的代码Y = [[int(x1+x2 < 1),1 - (int(x1+x2 < 1))] for (x1,x2) in X],我预测sess.run(y, feed_dict={x: [[0.5, 0.5],[0.6, 0.5],[0.7, 0.9], [0.5, 0.4], [0.5, 0.8]]}),输出[[ 0.43270305],[ 0.47653222],[ 0.69120681],[ 0.38999173],[ 0.56083697]],请问一下是根据大于0.5去判断属于哪一类吗,如果是大于0.5是属于int(x1+x2 < 1)还是其它类别,谢谢

ABadCandy commented 6 years ago

还得把代码里的w2权重改成两维的,意思就是最后相当于有两个节点的输出,再经过softmax函数进行概率归一化后哪个节点输出值更大就是属于哪一类,具体解释建议看看ufldl或cs229n/231n

KiXiang commented 6 years ago

@ABadCandy 非常感谢你的回复,如果是w2是2维的,通过softmax进行分类,这个我了解,但是我好奇的是如果是书中的网络结构,按常理一个输出单元是可以实现二分类的,比较不明白一个输出单元是如何操作。

ABadCandy commented 6 years ago

@nanwangshanxia 嗯书中最后的预测值y也没有经过一个sigmoid函数将其转为0-1之间的概率值,因此其输出可能为任意实数。 而程序中定义的0为负样本,1为正样本,所以对于该程序而言我觉得一个输出单元最后预测时可能y越接近于0则判断为负样本,越接近1则判定为正样本吧。至于你说的0.5是因为sigmoid函数将输出压缩到0-1之间了,而原程序感觉更像是线性回归,最后决策的阈值可能还得人为指定。

perhapszzy commented 6 years ago

fixed by https://github.com/caicloud/tensorflow-tutorial/pull/95