【发布时间】:2018-03-06 02:29:33
【问题描述】:
我已经定义了自己的损失函数。它确实有效。前馈可能没有问题。但是我不确定它是否正确,因为我没有定义backward()。
class _Loss(nn.Module):
def __init__(self, size_average=True):
super(_Loss, self).__init__()
self.size_average = size_average
class MyLoss(_Loss):
def forward(self, input, target):
loss = 0
weight = np.zeros((BATCH_SIZE,BATCH_SIZE))
for a in range(BATCH_SIZE):
for b in range(BATCH_SIZE):
weight[a][b] = get_weight(target.data[a][0])
for i in range(BATCH_SIZE):
for j in range(BATCH_SIZE):
a_ij= (input[i]-input[j]-target[i]+target[j])*weight[i,j]
loss += F.relu(a_ij)
return loss
我想问的问题是
1) 我需要定义backward() 到损失函数吗?
2) 如何定义backward()?
3) 在torch中做SGD的时候有什么办法可以做数据的索引吗?
【问题讨论】:
标签: python-2.7 torch pytorch