# How to write modules that run efficiently

I 've written a dedicated loss function module, and it can normal operation. But, the operation speed is so slow that much slower than the loss function of the pytorch.
Why is my moudule so much worse and how am i going to optimize my moudle?
The following codes is the main part of the loss function and the Net forward() function

loss function

``````def MyLoss(pred, label):
tpred1 , tpred2 = pred
for inxi, di in enumerate(tpred2, 0):
hiOri = tpred2[inxi]                                   # hiCode 二值化数据， hiOri 二值化之后的数据
tempLd = torch.norm(torch.abs(hiOri)-1.)               # 求解二值化损失
Ld = Ld + tempLd

# 损失函数2
if(inxi == len(labels)):
break
else:
for inxj, dj in enumerate(tpred2[inxi+1:], inxi+1):
if label[inxi] == label[inxj]:
Rij = 1.0
else:
Rij = 0.0
tempLh1 = Rij * torch.norm(hiOri - dj)
tempLh2 = ((1.0 - Rij) * max(180.0 - torch.norm(hiOri - dj), 0.))*0.5
Lh = Lh + tempLh1 + tempLh2

temp_loss = 0.5 * Ld + Lh
return temp_loss
``````

forward() function

``````# judge sign of a scalar
def signJudge(self, v1):
if v1>=0.0:
return 1.0
else:
return -1.0

# sign()
def MySignFun(self, vx):
res = Variable(torch.zeros_like(vx).cuda())
for index, data in enumerate(vx, 0):
res[index] = torch.FloatTensor([self.signJudge(x) for x in data])
return res

# forward
def forward(self, x):
x = self.pool1(F.leaky_relu(self.conv1(x), negative_slope = reluP))
x = self.pool1(F.leaky_relu(self.conv2(x), negative_slope = reluP))
x = self.pool2(F.leaky_relu(self.conv3(x), negative_slope = reluP))
x = self.pool2(F.leaky_relu(self.conv4(x), negative_slope = reluP))
x = self.pool2(self.pool1(x))

x = x.view(-1, 32 * 14 * 14)
x = F.leaky_relu(self.fc1(x), negative_slope = reluP)
x = F.leaky_relu(self.fc2(x), negative_slope = reluP)
x = torch.tanh(self.fc3(x))
tempx = x
x = self.MySignFun(x)
x = x, tempx
return x
``````

The loops in your customized loss function slow down the computation. You can try to re-code it into no-loops forms to speed up the computation.

1 Like