salesforce / fsnet

BSD 3-Clause "New" or "Revised" License
113 stars 24 forks source link

RuntimeError: The size of tensor a (7) must match the size of tensor b (343) at non-singleton dimension 1 #3

Open shuoshanz opened 1 year ago

shuoshanz commented 1 year ago

I used the parameters: --data ETTh1 --method fsnet --test_bsz 1 --seq_len 60 --pred_len 1 The training process works fine, but the testing error is as follows: “RuntimeError: The size of tensor a (7) must match the size of tensor b (343) at non-singleton dimension 1” Does anyone know how to fix this? Thanks!

techzzt commented 1 year ago

I also same problem has occurred during test process. Have you solved the problem?

HappyWalkers commented 1 year ago

Replace the _ol_one_batch in the exp/exp_fsnet.py with the following one.

    def _ol_one_batch(self,dataset_object, batch_x, batch_y, batch_x_mark, batch_y_mark):
        batch_y = batch_y.float()
        f_dim = -1 if self.args.features=='MS' else 0
        batch_y = batch_y[:,-self.args.pred_len:,f_dim:].to(self.device)
        true = rearrange(batch_y, 'b t d -> b (t d)').float().to(self.device)
        criterion = self._select_criterion()

        x = torch.cat([batch_x.float(), batch_x_mark.float()], dim=-1).to(self.device)
        for _ in range(self.n_inner):
            if self.args.use_amp:
                with torch.cuda.amp.autocast():
                    outputs = self.model(x)
            else:
                outputs = self.model(x)

            # breakpoint()
            loss = criterion(outputs, true)
            loss.backward()
            self.opt.step()       
            self.model.store_grad()
            self.opt.zero_grad()

        return outputs, true

I find the issue is related to the test_batchsize. If the test_batchsize equals one, then the original code works well. But if the test_batchsize is larger than one, then the updated code works.