我的特征向量大小为 1x4098。每个特征向量对应一个浮点数(温度)。在训练中,我有 10.000 个样本。因此,我的训练集大小为 10000x4098,标签为 10000x1。我想使用线性回归模型从训练数据中预测温度。我正在使用具有 MSE 损失的 3 个隐藏层(512、128、32)。但是,我使用 tensorflow 只获得了 80% 的准确率。您能否向我建议其他损失函数以获得更好的性能?
来自 deeplearning.ai :
\n\n\n\n\n构建神经网络的一般方法是:
\n\n\n
\n- 定义神经网络结构(输入单元数、隐藏单元数等)。
\n- 初始化模型参数
\n- 循环:\n\n
\n\n
- 实现前向传播
\n- 计算损失
\n- 实现反向传播以获得梯度
\n- 更新参数(梯度下降)
\n
损失函数如何影响网络的学习方式?
\n\n例如,这是我对前向和反向传播的实现,我认为它是正确的,因为我可以使用以下代码训练模型以获得可接受的结果:
\n\n\n\nfor i in range(number_iterations):\n\n\n # forward propagation\n\n\n Z1 = np.dot(weight_layer_1, xtrain) + bias_1\n a_1 = sigmoid(Z1)\n\n Z2 = np.dot(weight_layer_2, a_1) + bias_2\n a_2 = sigmoid(Z2)\n\n mse_cost = np.sum(cost_all_examples)\n cost_cross_entropy = -(1.0/len(X_train) * (np.dot(np.log(a_2), Y_train.T) + np.dot(np.log(1-a_2), (1-Y_train).T)))\n\n# Back propagation and gradient descent\n d_Z2 = np.multiply((a_2 - xtrain), d_sigmoid(a_2))\n d_weight_2 = np.dot(d_Z2, …Run Code Online (Sandbox Code Playgroud) 为什么会出现这个错误。
我正在尝试编写一个自定义损失函数,它最终具有负对数可能性。
根据我的理解,NLL 是在两个概率值之间计算的?
>>> loss = F.nll_loss(sigm, trg_, ignore_index=250, weight=None, size_average=True)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home//lib/python3.5/site-packages/torch/nn/functional.py", line 1332, in nll_loss
return torch._C._nn.nll_loss(input, target, weight, size_average, ignore_index, reduce)
RuntimeError: Expected object of type torch.LongTensor but found type torch.FloatTensor for argument #2 'target'
Run Code Online (Sandbox Code Playgroud)
这里的输入如下:
>>> sigm.size()
torch.Size([151414, 80])
>>> sigm
tensor([[ 0.3283, 0.6472, 0.8278, ..., 0.6756, 0.2168, 0.5659],
[ 0.6603, 0.5957, 0.8375, ..., 0.2274, 0.4523, 0.4665],
[ 0.5262, 0.4223, 0.5009, ..., 0.5734, 0.3151, …Run Code Online (Sandbox Code Playgroud) 我要向那些犯了与我相同的错误的人发布此问题。尝试计算渐变时出现此错误:
criterion = torch.nn.CrossEntropyLoss()
loss = criterion(y_hat, y_truth)
loss.backwards()
Run Code Online (Sandbox Code Playgroud) 为什么在损失函数中使用均值而不是总和?
即有什么理由为什么这是首选
def mae_loss(y_true, y_pred):
loss = tf.reduce_mean(tf.abs(y_true-y_pred))
return loss
Run Code Online (Sandbox Code Playgroud)
对此
def mae_loss(y_true, y_pred):
loss = tf.reduce_sum(tf.abs(y_true-y_pred))
return loss
Run Code Online (Sandbox Code Playgroud)
在 Keras 源代码中,还使用了均值变体:
我正在 Keras 中编写两个联合解码器,具有一个公共输入、两个单独的输出以及一个将两个输出都考虑在内的损失函数。我遇到的问题是损失函数。
以下是可以重现错误的最小 Keras 代码:
import tensorflow as tf
from scat import *
from keras.layers import Input, Reshape, Permute, Lambda, Flatten
from keras.layers.core import Dense
from keras.layers.advanced_activations import LeakyReLU
from keras.models import Model
from keras import backend as K
def identity(x):
return K.identity(x)
# custom loss function
def custom_loss():
def my_loss(y_dummy, pred):
fcn_loss_1 = tf.nn.softmax_cross_entropy_with_logits(labels=y_dummy[0], logits=pred[0])
fcn_loss_2 = tf.nn.softmax_cross_entropy_with_logits(labels=y_dummy[1], logits=pred[1])
fcn_loss_2 = tf.matrix_band_part(fcn_loss_2, 0, -1) - tf.matrix_band_part(fcn_loss_2, 0, 0)
fcn_loss = tf.reduce_mean(fcn_loss_1) + 2 * tf.reduce_mean(fcn_loss_2)
return fcn_loss
return …Run Code Online (Sandbox Code Playgroud) 在我的实验中,我试图训练一个神经网络来检测患者是否表现出症状 A、B、C、D。我的数据由每位患者的不同角度照片以及他们是否表现出症状 A、B、C、D 组成。
现在,在 pytoch 中,我正在使用 MSELoss 并将测试误差计算为分类总数中正确分类的总数。我想这太天真了,甚至是不恰当的。
测试误差计算的示例如下:假设我们有 2 名患者,每人都有两张图像。那么总共会有 16 个分类(1 个分类代表患者 1 是否有照片 1 中的症状 A、B、C、D 等)。如果模型正确预测照片 1 中的患者 1 表现出症状 A,那么正确分类的总数就会增加 1。
multilabel-classification deep-learning multiclass-classification pytorch loss-function
我的目标是训练跨度预测模型
可以预测BERT输出序列中的位置
我的输入形状是 (batch_size, max_sequence_len(512),embedding_size(768))
输出的形状将为 (batch_size , max_sequence_len , 1) 并且第三个暗淡代表概率,然后我将输出重塑为 (batch_size,max_sequence_len)
我的标签的形状是(batch_size,max_sequence_len),在max_sequence_len(512)中,只有一个位置为1,其他位置为零
我已经检查过了
(batch_size is 2)
start_pos_labels.sum(dim=1)
output >>
tensor([1.0000, 1.0000], device='cuda:0', dtype=torch.float64)
start_pred.sum(dim=1)
tensor([1., 1.], device='cuda:0', dtype=torch.float64, grad_fn=<SumBackward1>)
Run Code Online (Sandbox Code Playgroud)
但是当我使用 nn.KLDivLoss() 时,输出仍然为负,我真的不知道为什么
有人可以帮助我吗?谢谢!
这是我的代码模型代码
class posClassfication_new(nn.Module):
def __init__(self):
super(posClassfication_new, self).__init__()
self.start_task = nn.Sequential(
nn.Linear(768, 1),
# nn.ReLU(),
# nn.Linear(256, 128),
# nn.ReLU(),
# nn.Linear(128, 1)
)
self.end_task = nn.Sequential(
nn.Linear(768, 1),
# nn.ReLU(),
# nn.Linear(256, 128),
# nn.ReLU(),
# nn.Linear(128, 1)
)
#
def forward(self, start_x,end_x):
start_x = …Run Code Online (Sandbox Code Playgroud) I\xe2\x80\x99m 尝试修改 Yolo v1 以处理我的任务,每个对象只有 1 个类。(例如:一个对象不能同时是猫和狗)
\n由于架构的原因(诸如本地化预测之类的其他输出必须使用回归),因此 sigmoid 被应用于模型的最后一个输出(f.sigmoid(nearly_last_output))。而对于分类,yolo 1 也使用 MSE 作为损失。但据我所知,与我想要的 one-hot 的交叉熵相比,MSE 有时表现不佳。
\n具体来说:GT是这样的:(0 0 0 0 1假设我们总共只有5个类,每个类只有1个类,所以其中只有一个数字1,当然在这个例子中这是第5类)
和分类部分的输出模型:0.1 0.1 0.9 0.2 0.1
我发现一些建议使用nn.BCE/nn.BCEWithLogitsLoss但我想我应该在这里要求更正确的,因为我\xe2\x80\x99m 不擅长数学,也许我\xe2\x80\x99m 在某个地方错了,所以只是要求了解更多,并确定应该做什么我使用正确吗?