如何使用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

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

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