- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图弄清楚损失函数公式到底是什么以及如何在 class_weight='auto'
时手动计算它在 svm.svc
的情况下, svm.linearSVC
和 linear_model.LogisticRegression
.
对于平衡数据,假设您有一个经过训练的分类器:clf_c
.物流损失应该是(我说得对吗?):
def logistic_loss(x,y,w,b,b0):
'''
x: nxp data matrix where n is number of data points and p is number of features.
y: nx1 vector of true labels (-1 or 1).
w: nx1 vector of weights (vector of 1./n for balanced data).
b: px1 vector of feature weights.
b0: intercept.
'''
s = y
if 0 in np.unique(y):
print 'yes'
s = 2. * y - 1
l = np.dot(w, np.log(1 + np.exp(-s * (np.dot(x, np.squeeze(b)) + b0))))
return l
predict_log_proba()
当数据平衡时,它可以准确地为您提供:
b, b0 = clf_c.coef_, clf_c.intercept_
w = np.ones(len(y))/len(y)
-(clf_c.predict_log_proba(x[xrange(len(x)), np.floor((y+1)/2).astype(np.int8)]).mean() == logistic_loss(x,y,w,b,b0)
np.floor((y+1)/2).astype(np.int8)
简单地将 y=(-1,1) 映射到 y=(0,1)。
class_weight=None
时,您希望分类器(此处为逻辑回归)具有类似的性能(就损失函数值而言)。对比数据不平衡和
class_weight='auto'
.我需要有一种方法来计算两种场景的损失函数(没有正则化项)并比较它们。
class_weight = 'auto'
有什么用?
正好意思?是不是意思
class_weight = {-1 : (y==1).sum()/(y==-1).sum() , 1 : 1.}
或者更确切地说
class_weight = {-1 : 1./(y==-1).sum() , 1 : 1./(y==1).sum()}
?
最佳答案
class_weight
启发式
我对您对 class_weight='auto'
的第一个提议感到有些困惑。启发式,如:
class_weight = {-1 : (y == 1).sum() / (y == -1).sum(),
1 : 1.}
class_weight="auto"
有,看这个问题:
This means that each class you have (in classes) gets a weight equal to 1 divided by the number of times that class appears in your data (y), so classes that appear more often will get lower weights. This is then further divided by the mean of all the inverse class frequencies.
class_weight='balanced'
.
The "balanced" mode uses the values of y to automatically adjust weights inversely proportional to class frequencies in the input data:
n_samples / (n_classes * np.bincount(y))
.
np.bincount(y)
是一个数组,其中元素 i 是 i 类样本的计数。
import numpy as np
from sklearn.datasets import make_classification
from sklearn.utils import compute_class_weight
n_classes = 3
n_samples = 1000
X, y = make_classification(n_samples=n_samples, n_features=20, n_informative=10,
n_classes=n_classes, weights=[0.05, 0.4, 0.55])
print("Count of samples per class: ", np.bincount(y))
balanced_weights = n_samples /(n_classes * np.bincount(y))
# Equivalent to the following, using version 0.17+:
# compute_class_weight("balanced", [0, 1, 2], y)
print("Balanced weights: ", balanced_weights)
print("'auto' weights: ", compute_class_weight("auto", [0, 1, 2], y))
Count of samples per class: [ 57 396 547]
Balanced weights: [ 5.84795322 0.84175084 0.60938452]
'auto' weights: [ 2.40356854 0.3459682 0.25046327]
SVC
和
linearSVC
文档字符串很清楚
Set the parameter C of class i to class_weight[i]*C for SVC.
class_weight
中的权重
不直接影响 predict_proba
等方法 .他们改变输出是因为分类器优化了不同的损失函数。
lr = LogisticRegression(class_weight="auto")
lr.fit(X, y)
# We get some probabilities...
print(lr.predict_proba(X))
new_lr = LogisticRegression(class_weight={0: 100, 1: 1, 2: 1})
new_lr.fit(X, y)
# We get different probabilities...
print(new_lr.predict_proba(X))
# Let's cheat a bit and hand-modify our new classifier.
new_lr.intercept_ = lr.intercept_.copy()
new_lr.coef_ = lr.coef_.copy()
# Now we get the SAME probabilities.
np.testing.assert_array_equal(new_lr.predict_proba(X), lr.predict_proba(X))
关于scikit-learn - class_weight 在 linearSVC 和 LogisticRegression 损失函数中的作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31657263/
我是pytorch的新手。请问添加'loss.item()'有什么区别?以下2部分代码: for epoch in range(epochs): trainingloss =0 for
我有一个包含 4 列的 MySQL 表,如下所示。 TransactionID | Item | Amount | Date ------------------------------------
我目前正在使用 cocos2d、Box2D 和 Objective-C 为 iPad 和 iPhone 制作游戏。 每次更新都会发生很多事情,很多事情必须解决。 我最近将我的很多代码重构为几个小方法,
我一直在关注 Mixed Precision Guide .因此,我正在设置: keras.mixed_precision.set_global_policy(mixed_precision) 像这样
double lnumber = Math.pow(2, 1000); 打印 1.0715086071862673E301 我尝试过的事情 我尝试使用 BigDecimal 类来扩展这个数字: St
我正在尝试创建一个神经网络来近似函数(正弦、余弦、自定义...),但我在格式上遇到困难,我不想使用输入标签,而是使用输入输出。我该如何更改它? 我正在关注this tutorial import te
我有一个具有 260,000 行和 35 列的“单热编码”(全一和零)数据矩阵。我正在使用 Keras 训练一个简单的神经网络来预测一个连续变量。制作网络的代码如下: model = Sequenti
什么是像素级 softmax 损失?在我的理解中,这只是一个交叉熵损失,但我没有找到公式。有人能帮我吗?最好有pytorch代码。 最佳答案 您可以阅读 here所有相关内容(那里还有一个指向源代码的
我正在训练一个 CNN 架构来使用 PyTorch 解决回归问题,其中我的输出是一个 20 个值的张量。我计划使用 RMSE 作为模型的损失函数,并尝试使用 PyTorch 的 nn.MSELoss(
在每个时代结束时,我得到例如以下输出: Epoch 1/25 2018-08-06 14:54:12.555511: 2/2 [==============================] - 86
我正在使用 Keras 2.0.2 功能 API (Tensorflow 1.0.1) 来实现一个网络,该网络接受多个输入并产生两个输出 a 和 b。我需要使用 cosine_proximity 损失
我正在尝试设置很少层的神经网络,这将解决简单的回归问题,这应该是f(x) = 0,1x 或 f(x) = 10x 所有代码如下所示(数据生成和神经网络) 4 个带有 ReLu 的全连接层 损失函数 R
我正在研究在 PyTorch 中使用带有梯度惩罚的 Wasserstein GAN,但始终得到大的、正的生成器损失,并且随着时间的推移而增加。 我从 Caogang's implementation
我正在尝试在 TensorFlow 中实现最大利润损失。这个想法是我有一些积极的例子,我对一些消极的例子进行了采样,并想计算类似的东西 其中 B 是我的批处理大小,N 是我要使用的负样本数。 我是 t
我正在尝试预测一个连续值(第一次使用神经网络)。我已经标准化了输入数据。我不明白为什么我会收到 loss: nan从第一个纪元开始的输出。 我阅读并尝试了以前对同一问题的回答中的许多建议,但没有一个对
我目前正在学习神经网络,并尝试训练 MLP 以使用 Python 中的反向传播来学习 XOR。该网络有两个隐藏层(使用 Sigmoid 激活)和一个输出层(也是 Sigmoid)。 网络(大约 20,
尝试在 keras 中自定义损失函数(平滑 L1 损失),如下所示 ValueError: Shape must be rank 0 but is rank 5 for 'cond/Switch' (
我试图在 tensorflow 中为门牌号图像创建一个卷积神经网络 http://ufldl.stanford.edu/housenumbers/ 当我运行我的代码时,我在第一步中得到了 nan 的成
我正在尝试使用我在 Keras 示例( https://github.com/keras-team/keras/blob/master/examples/variational_autoencoder
我试图了解 CTC 损失如何用于语音识别以及如何在 Keras 中实现它。 我认为我理解的内容(如果我错了,请纠正我!)总体而言,CTC 损失被添加到经典网络之上,以便逐个元素(对于文本或语音而言逐个
我是一名优秀的程序员,十分优秀!