Hippo模型预测外汇价格时的张量维度与训练报错问题
使用Hippo模型进行外汇价格预测的问题
背景
我正在用Hippo模型做外汇价格预测,数据集包含datetime列和开盘价列。
遇到的问题
- 更新状态u的第二维度应为batch size,预期形状为
[64,1],我已经将其设置为1,但实际输出形状仍为[1,64]; - 训练阶段出现RuntimeError:pred张量尺寸(64)与y_train张量尺寸(4322)在非单例维度0上不匹配。
错误详情
Cell In[18], line 17, in train(X_train, y_train, model, loss_fn, optimizer, batch_size, device) 14 print('pred.shpae:',pred.shape) 15 print('y_train',y_train.shape) ---> 17 loss = loss_fn(pred, torch.tensor(y_train)) 18 optimizer.zero_grad() 19 loss_backward() File ~\anaconda3\lib\site-packages\torch\nn\modules\module.py:1511, in Module._wrapped_call_impl(self, *args, **kwargs) 1509 return self._compiled_call_impl(*args, **kwargs) # type: ignore[misc] 1510 else: -> 1511 return self._call_impl(*args, **kwargs) File ~\anaconda3\lib\site-packages\torch\nn\modules\module.py:1520, in Module._call_impl(self, *args, **kwargs) 1515 # If we don't have any hooks, we want to skip the rest of the logic in 1516 # this function, and just call forward. 1517 if not (self._backward_hooks or self._backward_pre_hooks or self._forward_hooks or self._forward_pre_hooks 1518 or _global_backward_pre_hooks or _global_backward_hooks 1519 or _global_forward_hooks or _global_forward_pre_hooks): -> 1520 return forward_call(*args, **kwargs) 1522 try: 1523 result = None File ~\anaconda3\lib\site-packages\torch\nn\modules\loss.py:535, in MSELoss.forward(self, input, target) 534 def forward(self, input: Tensor, target: Tensor) -> Tensor: --> 535 return F.mse_loss(input, target, reduction=self.reduction) File ~\anaconda3\lib\site-packages\torch\nn\functional.py:3338, in mse_loss(input, target, size_average, reduce, reduction) 3335 if size_average is not None or reduce is not None: 3336 reduction = _Reduction.legacy_get_string(size_average, reduce) -> 3338 expanded_input, expanded_target = torch.broadcast_tensors(input, target) 3339 return torch._C._nn.mse_loss(expanded_input, expanded_target, _Reduction.get_enum(reduction)) File ~\anaconda3\lib\site-packages\torch\functional.py:76, in broadcast_tensors(*tensors) 74 if has_torch_function(tensors): 75 return handle_torch_function(broadcast_tensors, tensors, *tensors) ---> 76 return _VF.broadcast_tensors(tensors) RuntimeError: The size of tensor a (64) must match the size of tensor b (4322) at non-singleton dimension 0
报错的train方法代码
def train(X_train, y_train, model, loss_fn, optimizer, batch_size = None, device = None): size = len(X_train) if device is None: device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') model.to(device) if batch_size is None: # X_train_tensor= torch.tensor(X_train, dtype = torch.float32).to(device) # y_train_tensor = torch.tensor(y_train, dtype = torch.float32).to(device) model.train() pred = model(X_train)[0] print('pred.shpae:',pred.shape) print('y_train',y_train.shape) loss = loss_fn(pred, torch.tensor(y_train)) optimizer.zero_grad() loss_backward() optimizer.step() loss_value = loss.item() print(f'loss:{loss_value:>7f}, [{size:>5d}/{size:>5d}]') else: dataset = TimeSeriesDataset(X_train, y_train) dataloader = DataLoader(dataset, batch_size = batch_size, shuffle = False) model.train() for batch_idx, (X_batch, y_batch )in enumerate(dataloader): X_batch, y_batch = X_batch.to(device,dtype = torch.float32), y_batch.to(device,dtype = torch.float32) # computing prediction error pred = model(X_batch) loss_ = loss_fn(pred, y_batch) optimizer.zero_grad() loss.backward() optimizer.step() if batch%10==0: loss_value = loss.item() current = batch_idx * len(X_batch) print(f"loss: {loss_value:>7f} [{current:>5d}/{len(dataset):>5d}]")
内容的提问来源于stack exchange,提问作者yazi
相关产品推荐
相关产品推荐

