コード例 #1
0
ファイル: trainers.py プロジェクト: tmbdev-tutorials/dl-2018
 def train_batch(self,
                 inputs,
                 targets,
                 weights=None,
                 update=True,
                 logname="train"):
     if update:
         self.set_training(True)
         self.optimizer.zero_grad()
     else:
         self.set_training(False)
     self.set_inputs(inputs)
     self.forward()
     if weights is not None:
         self.cuweights = autograd.Variable(torch.randn(1, 1).cuda())
         dlh.assign(self.cuweights, weights, False)
         self.cuoutput = self.weighted(self.cuoutput, self.cuweights)
     culoss = self.compute_loss(targets, weights=weights)
     if update:
         culoss.backward()
         self.optimizer.step()
     ploss = dlh.novar(culoss)[0]
     self.ntrain += dlh.size(inputs, 0)
     add_log(self.log,
             logname,
             loss=ploss,
             ntrain=self.ntrain,
             lr=self.current_lr)
     return self.get_outputs(), ploss
コード例 #2
0
ファイル: trainers.py プロジェクト: tmbdev-tutorials/dl-2018
 def set_targets(self, targets, weights=None):
     b, d, h, w = tuple(self.cuoutput.size())
     targets = dlh.as_nda(targets, (0, 2, 3, 1))
     targets = zoom_like(targets, (b, h, w, d))
     dlh.assign(self.cutarget, targets, (0, 3, 1, 2))
     assert self.cutarget.size() == self.cuoutput.size()
     if weights is not None:
         weights = dlh.as_nda(weights, (0, 2, 3, 1))
         weights = zoom_like(weights, (b, h, w, d))
         dlh.assign(self.cuweights, weights, (0, 3, 1, 2))
コード例 #3
0
ファイル: trainers.py プロジェクト: tmbdev-tutorials/dl-2018
 def set_targets(self, targets, weights=None):
     """Sets the cutarget variable from the given tensor.
     """
     dlh.assign(self.cutarget, targets, False)
     assert self.cuoutput.size() == self.cutargets.size()
     if weights is not None:
         dlh.assign(self.cuweights, weights, False)
         assert self.cuoutput.size() == self.cuweights.size()
     else:
         self.cuweights = None
コード例 #4
0
 def set_targets(self, targets, weights=None):
     assert weights is None, "weights not implemented"
     if dlh.rank(targets) == 1:
         targets = dlh.as_torch(targets)
         targets = targets.unsqueeze(1)
         b, c = dlh.shp(self.cuoutput)
         onehot = torch.zeros(b, c)
         onehot.scatter_(1, targets, 1)
         dlh.assign(self.cutarget, onehot)
     else:
         assert dlh.shp(targets) == dlh.shp(self.cuoutput)
         dlh.assign(self.cutarget, targets)
コード例 #5
0
ファイル: trainers.py プロジェクト: tmbdev-tutorials/dl-2018
 def set_inputs(self, images):
     dlh.assign(self.cuinput, images, (0, 3, 1, 2))
コード例 #6
0
ファイル: trainers.py プロジェクト: tmbdev-tutorials/dl-2018
 def set_inputs(self, images, depth1=False):
     dlh.assign(self.cuinput, images, transpose_on_convert=(0, 3, 1, 2))
コード例 #7
0
ファイル: trainers.py プロジェクト: tmbdev-tutorials/dl-2018
 def set_inputs(self, batch):
     """Sets the cuinput variable from the input data.
     """
     assert isinstance(batch, torch.Tensor)
     dlh.assign(self.cuinput, batch)