简体   繁体   English

如果模型 output 层有多个神经元并且只有一个值要预测,那么 Tensorflow model 如何计算损失?

[英]How does a Tensorflow model calculate loss, if the models output layer has multiple neurons and there is only one value to predict?

I recently noticed, that I can have and use a Tensorflow model, that has any number of outputs even if the predicted value is just a single real value.我最近注意到,我可以拥有并使用Tensorflow model,即使预测值只是一个实数值,它也有任意数量的输出。 For example, I can have 16 outputs from the model and only 1 real target value, however, the model does still train normally with no errors.例如,我可以从 model 获得 16 个输出,只有 1 个实际目标值,但是,model 仍然可以正常训练,没有错误。

My question is how does Tensorflow handle the incorrect size of the output when comparing the output to the target value.我的问题是,在将 output 与目标值进行比较时,Tensorflow 如何处理 output 的不正确大小。 How can it still calculate loss?怎么还能计算损失呢? Does it try to get every output as close to the target value as possible, or does it do some kind of averaging?它是尝试使每个 output 尽可能接近目标值,还是进行某种平均?

We use Categorical cross-entropy loss for a model which has any number of outputs and a single prediction value.我们对 model 使用分类交叉熵损失,它具有任意数量的输出和单个预测值。

Categorical cross-entropy loss = Softmax Activation + Cross-entropy loss分类交叉熵损失 = Softmax 激活 + 交叉熵损失

在此处输入图像描述

Lets assume we have 4 classes in our model, the softmax activation function will compute the probabilities for 4 classes.假设我们的 model 中有 4 个类别,softmax 激活 function 将计算 4 个类别的概率。 The Cross-entropy loss is calculated on these probabilities for the true label class.交叉熵损失是根据真实 label class 的这些概率计算的。

Consider the following Example:考虑以下示例:

The 4 output nodes are Rabbit, Cat, Dog, Squirrel. 4个output节点分别是Rabbit, Cat, Dog, Squirrel。

True Label: Rabbit真 Label:兔子

Predictions: Rabbit = 8, Cat = 1, Dog = 4, Squirrel = 2预测:兔子 = 8,猫 = 1,狗 = 4,松鼠 = 2

Softmax Calculation: R = 2980.95/3045.63, C = 2.71/3045.63, D = 54.59/3045.63, S = 7.38/3045.63 (Here, 3045.63 = 2980.95 + 2.71 + 54.59 + 7.38 = 3045.63) SoftMax计算:R = 2980.95/3045.63,C = 2.71/3045.63,d = 54.59/3045.63,s = 7.38/3045.63

Cross- entropy loss: -(1 * ln(R) + 0 * ln(C) + 0 * ln(D) + 0 * ln(S)) = -(-0.0214 + 0 + 0 + 0) = 0.0214交叉熵损失:-(1 * ln(R) + 0 * ln(C) + 0 * ln(D) + 0 * ln(S)) = -(-0.0214 + 0 + 0 + 0) = 0.0214

This is how the model handles the outputs and calculate the loss.这就是 model 处理输出和计算损失的方式。 Thanks!谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为 tf.keras 模型的隐藏层选择输出神经元的值? - How can I choose the value of output neurons for the hidden layer of tf.keras model? Tensorflow 仅 CNN 1 output 预测值 - Tensorflow CNN only 1 output predict value 如何 model.predict 内部损失 function? (张量流,Keras) - How to model.predict inside loss function? (Tensorflow, Keras) 如何在 Keras 中使用多个变量作为具有一个输出的模型的损失? - How to use multiple variables as loss for a model with one output in Keras? 如何仅预测 tensorflow 中的一类 - how to predict only one class in tensorflow 为什么keras model.predict只返回一个概率?如何输出所有类别的所有概率? - why does keras model.predict only return one probability?How can I output all probabilities of all the classes? 如何在tensorflow 2中获得损失梯度wrt内层输出? - How to get loss gradient wrt internal layer output in tensorflow 2? scikit-learn中多层Perceprton分类器中神经元的输出 - Output of neurons in Multiple Layer Perceprton Classifier in scikit-learn 如何制作 tensorflow.keras.model.predict() Z78E6221F6393D1466681DB398 - How to make tensorflow.keras.model.predict() output a generator? 如何计算密集层输出参数值 - How to calculate dense layer output parameter value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM