我想计算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)
我遇到了计算类别正确概率的问题。
我们如何解决这个问题并获得正确的成本值呢?
回答:
你的问题在于你不能在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)