如何使用PyTorch计算Softmax回归的成本

我想计算Softmax回归的成本。需要计算的成本函数在页面底部给出。

对于NumPy,我可以按如下方式获取成本:

"""X.shape = 2,300 # floatsy.shape = 300,  # integersW.shape = 2,3b.shape = 3,1"""import numpy as npnp.random.seed(100)# Data and labelsX = np.random.randn(300,2)y = np.ones(300)y[0:100] = 0y[200:300] = 2y = y.astype(np.int) # weights and biasW = np.random.randn(2,3)b = np.random.randn(3)N = X.shape[0]scores = np.dot(X, W) + bhyp = np.exp(scores-np.max(scores, axis=0, keepdims=True)) probs = hyp / np.sum(hyp, axis = 0)logprobs = np.log(probs[range(N),y])cost_data = -1/N * np.sum(logprobs)print("hyp.shape = {}".format(hyp.shape)) # hyp.shape = (300, 3)print(cost_data)

但是,当我尝试使用PyTorch时,我无法得到相同的结果。到目前为止,我得到了以下内容:

"""X.shape = 2,300 # floatsy.shape = 300,  # integersW.shape = 2,3b.shape = 3,1"""import numpy as npimport torchfrom torch.autograd import Variablenp.random.seed(100)# Data and labelsX = np.random.randn(300,2)y = np.ones(300)y[0:100] = 0y[200:300] = 2y = y.astype(np.int)X = Variable(torch.from_numpy(X),requires_grad=True).type(torch.FloatTensor)y = Variable(torch.from_numpy(y),requires_grad=True).type(torch.LongTensor)# weights and biasW = Variable(torch.randn(2,3),requires_grad=True)b = Variable(torch.randn(3),requires_grad=True)N = X.shape[0]scores = torch.mm(X, W) + bhyp = torch.exp(scores - torch.max(scores))probs = hyp / torch.sum(hyp)correct_probs = probs[range(N),y] # got problem HERE# logprobs = np.log(correct_probs)# cost_data = -1/N * torch.sum(logprobs)# print(cost_data)

我遇到了计算类别正确概率的问题。

我们如何解决这个问题并获得正确的成本值呢?

需要计算的成本函数如下所示:enter image description here


回答:

你的问题在于你不能在PyTorch中使用range(N),应该使用切片0:N来替代:

hyp = torch.exp(scores - torch.max(scores))probs = hyp / torch.sum(hyp)correct_probs = probs[0:N,y] # problem solvedlogprobs = torch.log(correct_probs)cost_data = -1/N * torch.sum(logprobs)

另外一点是,你的标签y不需要梯度,你最好这样设置:

y = Variable(torch.from_numpy(y),requires_grad=False).type(torch.LongTensor)

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注