首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >由于矩阵形状/大小不兼容而无法进行预测

由于矩阵形状/大小不兼容而无法进行预测
EN

Stack Overflow用户
提问于 2020-12-31 06:39:27
回答 2查看 368关注 0票数 0

我试图建立一个基于个人的保险成本预测模型。这就是密码。

代码语言:javascript
运行
复制
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import DataLoader, TensorDataset

import numpy as np
import pandas as pd

from LSR import ListSearchReplace as LSR

csv = pd.read_csv("main.csv")
partialInputs = csv[["age", "bmi", "children"]]
smoker, sex = list(csv["smoker"]), list(csv["sex"])

L1 = LSR(smoker)
L1.replace("yes", 1, True)
L1.replace("no", 0, True)

L2 = LSR(sex)
L2.replace("female", 1, True)
L2.replace("male", 0, True)

pdReadySmoker = pd.DataFrame({"smoker": smoker})
pdReadySex = pd.DataFrame({"sex": sex})

SmokerAndSex = pd.merge(pdReadySmoker, pdReadySex, how="outer", left_index=True, right_index=True)

INPUTS = pd.merge(partialInputs, SmokerAndSex, how="outer", left_index=True, right_index=True)
TARGETS = csv["charges"]

INPUTS = torch.from_numpy(np.array(INPUTS, dtype='float32'))
TARGETS = torch.from_numpy(np.array(TARGETS, dtype='float32'))

print(INPUTS.shape, TARGETS.shape)

loss_fn = F.mse_loss
model = nn.Linear(5, 3) # <-- changing this, changes the error message.
opt = torch.optim.SGD(model.parameters(), lr=1e-5)

trainDataset = TensorDataset(INPUTS, TARGETS)
BATCH_SIZE = 5
trainDataloader = DataLoader(trainDataset, BATCH_SIZE, shuffle=True)

def fit(numEpochs, model, loss_fn, opt, trainDataloader):
    for epochs in range(numEpochs):
        for inputBatch, targetBatch in trainDataloader:
            preds = model(inputBatch)
            loss = loss_fn(preds, targetBatch)
            loss.backward()
            
            opt.step()
            
            opt.zero_grad()
        
        e = epoch + 1
        if e % 10 == 0:
            print(f"Epoch: {e/numEpochs}, loss: {loss.item():.4f}")

fit(100, model, loss_fn, opt, trainDataloader) <-- error

产生的错误:

代码语言:javascript
运行
复制
<ipython-input-7-b7028a3d94fd>:5: UserWarning: Using a target size (torch.Size([5])) that is different to the input size (torch.Size([5, 3])). This will likely lead to incorrect results due to broadcasting. Please ensure they have the same size.
  loss = loss_fn(preds, targetBatch)

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-20-d8f5bcdc847d> in <module>
----> 1 fit(100, model, loss_fn, opt, trainDataloader)

<ipython-input-7-b7028a3d94fd> in fit(numEpochs, model, loss_fn, opt, trainDataloader)
      3         for inputBatch, targetBatch in trainDataloader:
      4             preds = model(inputBatch)
----> 5             loss = loss_fn(preds, targetBatch)
      6             loss.backward()
      7 

D:\coding\machine-learning\env-ml\lib\site-packages\torch\nn\functional.py in mse_loss(input, target, size_average, reduce, reduction)
   2657         reduction = _Reduction.legacy_get_string(size_average, reduce)
   2658 
-> 2659     expanded_input, expanded_target = torch.broadcast_tensors(input, target)
   2660     return torch._C._nn.mse_loss(expanded_input, expanded_target, _Reduction.get_enum(reduction))
   2661 

D:\coding\machine-learning\env-ml\lib\site-packages\torch\functional.py in broadcast_tensors(*tensors)
     69         if any(type(t) is not Tensor for t in tensors) and has_torch_function(tensors):
     70             return handle_torch_function(broadcast_tensors, tensors, *tensors)
---> 71     return _VF.broadcast_tensors(tensors)  # type: ignore
     72 
     73 

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

我尝试过更改of模型的维度,这些是所做的一些更改和相关的错误:

代码语言:javascript
运行
复制
model = nn.Linear(5, 1338)

Error:
RuntimeError: The size of tensor a (1338) must match the size of tensor b (5) at non-singleton dimension 1
代码语言:javascript
运行
复制
model = nn.Linear(1338, 1338)

Error:
RuntimeError: mat1 and mat2 shapes cannot be multiplied (5x5 and 1338x1338)

有时,此错误会使我将矩阵更改为正确的形状,但这将导致先前有关non-singleton dimension的错误。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-12-31 11:58:14

这应该是相当直截了当的,你只有一个单层.这是一个正确分类形状的问题。

您正在为一个nn.Linear层提供一个带有形状input_shape的输入。这种类型的层有两个参数:in_features (输入向量中的特征数)和out_features (结果向量中的特征数)。由于您使用的是F.mse_loss,您的目标向量需要具有与您的预测相同的形状。

请记住,第一个维度是批处理维度。总之,输入张量具有形状(batch, input_size),密集层定义为nn.Linear(input_size, out_size),目标张量具有形状(batch, output_size)

回到你的情况,你的TARGETS张量是形状的(1338),所以你要么想:

  • 有一个与1338组件匹配的1338组件预测,它实际上对应于(1, 1338) (批处理中的单个元素)。这可以用TARGETS = TARGETS.unsqueeeze(0).

修复。

  • 或者,实际上有一个元素1338预测,它将匹配一个nn.Linear(?, 1),合适的目标形状将是(1338, 1)。这可以用TARGETS = TARGETS.unsqueeeze(-1) (在最后一个维度之后添加一个附加的轴)来修复。
票数 2
EN

Stack Overflow用户

发布于 2020-12-31 11:40:29

您的输入维度为5,并且您为每个输入预测一个标量值(target)。

因此,您的线性模型应该是大小的:

代码语言:javascript
运行
复制
model = nn.Linear(5, 1)  # from 5-dim inputs to 1-dim output

我认为将批处理大小设置为5(类似于输入维度)使您感到困惑。尝试更改批处理大小,看看它如何不影响模型的尺寸。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65517495

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档