将分类网络更改为回归网络的损失函数

Loss function for changing a classification network to a regression one

我正在尝试将一个将点云分类为 40 种不同 类 的神经网络更改为一个预测三个点 (x,y,z) 中特定 属性 点云的回归网络.对于损失函数,我已经替换了这个损失值:

loss = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=pred, labels=label)  
classify_loss = tf.reduce_mean(loss)   
tf.summary.scalar('classify loss', classify_loss)

这个:

mse = tf.keras.losses.MeanSquaredError()
regression_loss=mse(pred, label)
regression_loss = tf.cast(regression_loss, tf.float32)   
tf.summary.scalar('regression loss', regression_loss)

我只将最后一层输出从 40 更改为 3,并为网络实际开始训练做了一些小改动。 我使用 tf.cast 是因为在网络中间计算了另一个损失乘以 0.001 并添加到这个损失中得到平均损失,在此之前,它一直在提高 x(regression_loss) 的错误int 和 y(middle_loss) 是浮点数,不能相加,这也很奇怪。 'labels' 的值在零附近很小,但除了随机时间外,损失几乎一直保持在零。如果有人可以帮助我找到问题或替换损失函数,我将不胜感激。 日志如下所示:

**** EPOCH 000 ****
----0-----
mean loss: 0.000000
----1-----
mean loss: 0.000000
----2-----
mean loss: 0.000000
----3-----
mean loss: 0.000000
----4-----
mean loss: 0.000000
----0-----
----1-----
eval mean loss: 0.000000
Model saved in file: log/model.ckpt
**** EPOCH 001 ****
----0-----
mean loss: 0.000000
----1-----
mean loss: 0.000000
----2-----
mean loss: 0.000000
----3-----
mean loss: 0.000000
----4-----
mean loss: 0.000000
----0-----
----1-----
eval mean loss: 0.000000
**** EPOCH 002 ****
----0-----
mean loss: 0.000000
----1-----
mean loss: 0.019608
----2-----
mean loss: 0.000000
----3-----
mean loss: 0.015625
----4-----
mean loss: 0.000000
----0-----
----1-----
eval mean loss: 0.000000
**** EPOCH 003 ****
----0-----
mean loss: 0.000000
----1-----
mean loss: 0.000000
----2-----
mean loss: 0.000000
----3-----
mean loss: 0.000000
----4-----
mean loss: 0.000000
----0-----
----1-----
eval mean loss: 0.000000
**** EPOCH 004 ****
----0-----
mean loss: 0.000000
----1-----
mean loss: 0.000000
----2-----
mean loss: 0.000000
----3-----
mean loss: 0.000000
----4-----
mean loss: 0.000000
----0-----
----1-----
eval mean loss: 0.000000
**** EPOCH 005 ****
----0-----
mean loss: 0.019608
----1-----
mean loss: 0.000000
----2-----
mean loss: 0.000000
----3-----
mean loss: 0.000000
----4-----
mean loss: 0.000000
----0-----
----1-----
eval mean loss: 0.000000
**** EPOCH 006 ****
----0-----
mean loss: 0.000000
----1-----
mean loss: 0.000000
----2-----
mean loss: 0.000000
----3-----
mean loss: 0.000000
----4-----
mean loss: 0.000000
----0-----
----1-----
eval mean loss: 0.000000
**** EPOCH 007 ****
----0-----
mean loss: 0.000000
----1-----
mean loss: 0.000000
----2-----
mean loss: 0.015625
----3-----
mean loss: 0.015625
----4-----
mean loss: 0.000000
----0-----
----1-----
eval mean loss: 0.000000
**** EPOCH 008 ****
----0-----
mean loss: 0.000000
----1-----
mean loss: 0.000000
----2-----
mean loss: 0.000000
----3-----
mean loss: 0.000000
----4-----
mean loss: 0.000000
----0-----
----1-----
eval mean loss: 0.000000
**** EPOCH 009 ****
----0-----
mean loss: 0.015625
----1-----
mean loss: 0.000000
----2-----
mean loss: 0.000000
----3-----
mean loss: 0.000000
----4-----
mean loss: 0.000000
----0-----
----1-----
eval mean loss: 0.000000
**** EPOCH 010 ****
----0-----
mean loss: 0.000000
----1-----
mean loss: 0.015625
----2-----
mean loss: 0.000000
----3-----
mean loss: 0.000000
----4-----
mean loss: 0.000000
----0-----
----1-----
eval mean loss: 0.000000
Model saved in file: log/model.ckpt
**** EPOCH 011 ****
----0-----
mean loss: 0.000000
----1-----
mean loss: 0.000000
----2-----
mean loss: 0.000000
----3-----
mean loss: 0.000000
----4-----
mean loss: 0.000000
----0-----
----1-----
eval mean loss: 0.000000
**** EPOCH 012 ****
----0-----
mean loss: 0.000000
----1-----
mean loss: 0.000000
----2-----
mean loss: 0.000000
----3-----
mean loss: 0.000000
----4-----
mean loss: 0.000000
----0-----
----1-----
eval mean loss: 0.000000
**** EPOCH 013 ****
----0-----
mean loss: 0.000000
----1-----
mean loss: 0.000000
----2-----
mean loss: 0.000000
----3-----
mean loss: 0.000000
----4-----
mean loss: 0.000000
----0-----
----1-----
eval mean loss: 0.000000
**** EPOCH 014 ****
----0-----
mean loss: 0.000000
----1-----
mean loss: 0.015625
----2-----

我考虑过将 regression_loss 乘以 100,但我认为这不会有太大区别。

我当天就找出了原因:) 如果有人遇到这种问题,这就是我做错的地方:

对于标签占位符,我忘记将 tf.int32 更改为 tf.float32 所以标签都转换为 int32 并且它会不断计算损失为零!

labels_pl = tf.placeholder(tf.float32, shape=(batch_size,3))