深度学习神经网络 Tensorflow参数优化 之 损失函数2 一个重要误区的纠正

在学习了cross_entropy损失函数以后,我本想将其加入到程序代码中,测试一下二分类的效果:

#基于seed产生随机数
rdm = np.random.RandomState(seed)
#随机数返回32行2列的矩阵 表示32组 体积和重量 作为输入数据集
X = rdm.rand(32,2)/10.0+0.9  #范围在 0-1之间
for i in range(0,31):
    X[i, 0] = 0.3 - X[i,0]  #0.2——0.3
    X[i, 1] = 0.5 + X[i,1]  #0.5——0.6

#从X这个32行2列的矩阵中取出一行,判断如果和小于1,给Y赋值1,如果和不小于1,给Y赋值0
#作为输入数据集的标签(正确答案)
Y = rdm.rand(32,2)
for i in range(0,31):
    Y[i, 0] = X[i, 0] + X[i, 1] + 0.1 - rdm.rand()/5.0
    Y[i, 1] = X[i, 1] - X[i, 0] + 0.1 - rdm.rand()/5.0

首先生成了一组数据,这组数据一共32个样本,每个样本两个特征值。输出的Y也是两个数组成,一个比较大,是输入样本特征1和输入样本特征2的组合,一个比较小,使他们的差值。

之后我定义损失函数:

#2 定义损失函数及反向传播方法
ce = -tf.reduce_mean(y_*tf.log(tf.clip_by_value(y,1e-12,1.0)))
train_step = tf.train.GradientDescentOptimizer(0.001).minimize(ce)

该损失函数采用交叉熵,为了保证log运算数的意义,令y的值压缩在1e-12到1之间。

然后训练20000轮。发现参数根本没有变!损失函数的大小也没有变。

然后我进行了各种调试,发现原来自己忘记了使用激活函数。

有的时候想得太简单,总会忘记一些本质性的东西。在神经网络随机参数生成时,网络参数很可能会计算出负数的y,然后放进这个损失函数程序中,会变为1e-12,这就失去了意义。根源在于我们没有通过激活函数把y得值压缩在0到1之间。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
% Known encoding formats are the following FDSN codes: % 0: ASCII % 1: 16-bit integer % 2: 24-bit integer (untested) % 3: 32-bit integer % 4: IEEE float32 % 5: IEEE float64 % 10: Steim-1 % 11: Steim-2 % 12: GEOSCOPE 24-bit (untested) % 13: GEOSCOPE 16/3-bit gain ranged % 14: GEOSCOPE 16/4-bit gain ranged (untested) % 19: Steim-3 (alpha and untested) % % See also MKMSEED to export data in miniSEED format. % % % Author: Franois Beauducel % Institut de Physique du Globe de Paris % Created: 2010-09-17 % Updated: 2012-04-21 % % Acknowledgments: % Ljupco Jordanovski, Jean-Marie Saurel, Mohamed Boubacar, Jonathan Berger, % Shahid Ullah. % % References: % IRIS (2010), SEED Reference Manual: SEED Format Version 2.4, May 2010, % IFDSN/IRIS/USGS, http://www.iris.edu % Trabant C. (2010), libmseed: the Mini-SEED library, IRIS DMC. % Steim J.M. (1994), 'Steim' Compression, Quanterra Inc. % History: % [2012-04-21] % - Correct bug with Steim + little-endian coding % (thanks to Shahid Ullah) % [2012-03-21] % - Adds IDs for warning messages % [2011-11-10] % - Correct bug with multiple channel name length (thanks to % Jonathan Berger) % [2011-10-27] % - Add LocationIdentifier to X.ChannelFullName % [2011-10-24] % - Validation of IEEE double encoding (with PQL) % - Import/plot data even with file integrity problem (like PQL) % [2011-07-21] % - Validation of ASCII encoding format (logs) % - Blockettes are now stored in substructures below a single % field X.BLOCKETTES % - Add import of blockettes 500 and 2000 % - Accept multi-channel files with various data coding % [2010-10-16] % - Alpha-version of Steim-3 decoding... % - Extend output parameters with channel detection % - Add gaps and overlaps on plots % - Add possibility to force the plot % [2010-10-02] % - Add the input formats for GEOSCOPE multiplexed old data files % - Additional output argument with gap and overlap analysis % - Create a plot when no output argument are specified % - Optimize script coding (30 times faster STEIM decoding!) % % [2010-09-28] % - Correction of a problem with STEIM-1 nibble 3 decoding (one % 32-bit difference) % - Add reading of files without blockette 1000 with additional % input arguments (like Seismic Handler output files). % - Uses warning() function instead of fprintf().

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dezeming

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值