Posts

Showing posts with the label pytorch

Add / substract between matrix and vector in pytorch

Add / substract between matrix and vector in pytorch I want to do + / - / * between matrix and vector in pytorch. How can I do with good performance? I tried to use expand, but it's really slow (I am using big matrix with small vector). a = torch.rand(2,3) print(a) 0.7420 0.2990 0.3896 0.0715 0.6719 0.0602 [torch.FloatTensor of size 2x3] b = torch.rand(2) print(b) 0.3773 0.6757 [torch.FloatTensor of size 2] a.add(b) Traceback (most recent call last): File "C:ProgramDataAnaconda3libsite-packagesIPythoncoreinteractiveshell.py", line 3066, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "<ipython-input-17-a1cb1b03d031>", line 1, in <module> a.add(b) RuntimeError: inconsistent tensor size, expected r_ [2 x 3], t [2 x 3] and src [2] to have the same number of elements, but got 6, 6 and 2 elements respectively at c:miniconda2conda-bldpytorch-cpu_1519449358620worktorchlibthgeneric/THTensorMath.c:1021 Expected result: ...

pytorch: zero_grad vs zero_grad() - role of parenthesis?

pytorch: zero_grad vs zero_grad() - role of parenthesis? I'm trying to train a pytorch model as follows: start = time.time() for epoch in range(100): t_loss = 0 for i in range(100): optimizer.zero_grad scores = my_model(sent_dict_list[i]) scores = scores.permute(0, 2, 1) loss = loss_function(scores, torch.tensor(targ_list[i]).cuda()) t_loss += loss.item() loss.backward() optimizer.step() print("t_loss = ", t_loss) I find that when I call "optimizer.zero_grad" my loss decreases at the end of every epoch whereas when I call "optimizer.zero_grad()" with the parenthesis it stays almost exactly the same. I don't know what difference this makes and was hoping someone could explain it to me. optimizer.zero_grad is just a function name. you call the function with parenthesis – xssChauhan Jun 28 at 12:10 ...