weixin_51034009 2023-04-11 10:43 采纳率: 0%
浏览 74
已结题

RuntimeError: mat1 and mat2 shapes cannot be multiplied (40x96000 and 96004x10)


input_size = 24001#excel数据点数

dir_list = os.listdir("E:/pythonProject/doing")#实验数据路径,./表示当下文件夹
all_data_list=[]
all_label_list=[]
for i in range(len(dir_list)):
    label = int(dir_list[i].split("_")[-1])
    print(label)
    dir = dir_list[i]
    file_list = os.listdir("./doing/"+dir)
    print(file_list)
    tmp_data_list = []
    tmp_label_list = []
    for j in range(len(file_list)):
        filename = "./doing/"+dir+"/"+file_list[j]
        #print(filename)
        tmp_df = pd.read_excel(filename)
        tmp_data = np.array(tmp_df.values[:, 1])
        # print(len(tmp_data))
        tmp_data = F.interpolate(torch.tensor(tmp_data).unsqueeze(0).unsqueeze(0).unsqueeze(0),
                                   (1, input_size)).squeeze(0).squeeze(0).squeeze(0).numpy()
        #plt.plot(tmp_data)
        #plt.show()
        print(tmp_data.shape)
        tmp_data_list.append(tmp_data)
        tmp_label_list.append(label)
    tmp_data = np.array(tmp_data_list)
    tmp_label = np.array(tmp_label_list)
    all_data_list.append(tmp_data)
    all_label_list.append(tmp_label)

all_data = np.array(all_data_list).reshape(-1,input_size)
all_data = (all_data - np.mean(all_data)) / (np.max(all_data) - np.min(all_data))
print(all_data.shape)
label = np.array(all_label_list)
label = label.reshape(label.shape[0]*label.shape[1])
print(label.shape)




class Load_dataset(data.Dataset):
    def __init__(self, data, label):
        self.len = label.shape[0]
        self.data = data
        self.label = label

    def __len__(self):
        return self.len

    def __getitem__(self, idx):
        x = self.data[idx]
        y = self.label[idx]

        return x, y

    def collate_fn(self, data):
        x, y = zip(*data)
        return x, y


full_dataset = Load_dataset(all_data, label)
train_num = 80
test_num = 20
train_set, test_set = torch.utils.data.random_split(full_dataset, [train_num, test_num],
                                                    generator=torch.Generator().manual_seed(6))

loader_train = torch.utils.data.DataLoader(train_set, batch_size=int(train_num / 2), shuffle=True)
loader_test = torch.utils.data.DataLoader(test_set, batch_size=1, shuffle=False)


# 模型定义函数
class model(nn.Module):
    def __init__(self):
        super(model, self).__init__()

        self.conv1 = nn.Conv1d(in_channels=1, out_channels=64, kernel_size=5, padding=2)
        self.pool1 = nn.MaxPool1d(kernel_size=2, stride=2)
        self.conv2 = nn.Conv1d(in_channels=64, out_channels=128, kernel_size=5, padding=2)
        self.pool2 = nn.MaxPool1d(kernel_size=2, stride=2)
        self.conv3 = nn.Conv1d(in_channels=128, out_channels=256, kernel_size=5, padding=2)
        self.pool3 = nn.MaxPool1d(kernel_size=2, stride=2)
        self.fc = nn.Linear(input_size * 4, 10)
 def forward(self, x):
        y = F.relu(self.conv1(x))
        y = self.pool1(y)
        y = F.relu(self.conv2(y))
        y = self.pool2(y)
        y = F.relu(self.conv3(y))
        y = self.pool3(y)
        y = y.reshape(y.shape[0], -1)
        y = self.fc(y)

        return y


net = model()

opt = torch.optim.Adam(net.parameters(), lr=0.001)

best_test_acc = 0
patience = 0
train_loss = []
train_acc = []
test_acc = []
for epoch in range(100):
    net.train()
    pred_list = []
    label_list = []
    for i, (x, y) in enumerate(loader_train):
        pred = net(x.unsqueeze(1).float())
        loss = torch.nn.CrossEntropyLoss()(pred, y.long())

        acc = torch.mean((y == pred.argmax(1)).float())

        print(epoch, i)
        print("train_loss:", loss.item())
        print("train_acc", round(acc.item(), 4))
        train_acc.append(acc.item())
        train_loss.append(loss.item())

        opt.zero_grad()
        loss.backward()
        opt.step()

    net.eval()
    acc = 0
    for i, (x, y) in enumerate(loader_test):
        pred = net(x.unsqueeze(1).float())
        pred_list.append(pred.argmax(1).item())
        label_list.append(y.item())
        acc += torch.sum((y == pred.argmax(1)).float())
    acc = acc / test_num
    if acc > best_test_acc:
        patience = 0
        best_test_acc = acc.item()
        best_pred_list = pred_list
        torch.save(net.state_dict(), 'net_best.pkl')
    else:
        patience += 1
    # if patience == 3:
    #     break

    print("test_acc", round(acc.item(), 4))
    test_acc.append(acc.item())

print("best_test_acc", round(best_test_acc, 4))

# 损失函数图
import matplotlib.pyplot as plt

print()
plt.plot(train_loss)
train_acc= 'lossrecord/loss.txt'
plt.xlabel("step", font='Times New Roman', fontsize=16)
plt.ylabel("loss", font='Times New Roman', fontsize=16)
plt.show()

from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay

confusion_mat = confusion_matrix(np.array(label_list), np.array(best_pred_list))

disp = ConfusionMatrixDisplay(confusion_matrix=confusion_mat)
disp.plot(
    include_values=True,
    cmap="viridis",
    ax=None,
    xticks_rotation="horizontal",
    values_format="d"
)
plt.show()

报错“RuntimeError: mat1 and mat2 shapes cannot be multiplied (40x96000 and 96004x10)”,大神们,这个怎么修改啊?让这个py正常工作。

  • 写回答

6条回答 默认 最新

  • 「已注销」 2023-04-11 11:17
    关注

    引用new bing作答:
    错误消息“RuntimeError:mat1和mat2形状无法相乘(40x96000和96004x10)”表示矩阵乘法运算中存在大小不匹配。要修复此错误,您需要修改其中一个或两个矩阵的形状,以便它们与乘法兼容。
    在这种情况下,当试图计算模型中的完全连接层时,似乎出现了问题。因为模型中最终完全连接层的大小 (self.fc) 期望输入的形状为 (batch_size, input_size * 4 )(其中 input_size 为 24001)但传递给模型的输入形状具有不同的形状((batch_size,1,input_size))。
    要解决此问题,您可以修改模型类中的前向函数以在输入张量通过全连接层之前对其进行整形。 可以替换以下行:

    y = y.reshape(y.shape[0], -1)
    

    替换为:

    y = y.view(y.size(0), -1)
    

    y = y.view(y.size(0), -1) 行将张量 y 重塑为 (batch_size, input_size * 4) 的形状,然后再将其传递给全连接层。 这应该可以解决您遇到的错误。

    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 4月19日
  • 修改了问题 4月11日
  • 赞助了问题酬金15元 4月11日
  • 创建了问题 4月11日

悬赏问题

  • ¥15 根据会员信息表绘制堆积柱状图
  • ¥100 华为手机私有App后台保活
  • ¥15 sqlserver中加密的密码字段查询问题
  • ¥20 有谁能看看我coe文件到底哪儿有问题吗?
  • ¥20 我的这个coe文件到底哪儿出问题了
  • ¥15 matlab使用自定义函数时一直报错输入参数过多
  • ¥15 设计一个温度闭环控制系统
  • ¥100 rtmpose姿态评估
  • ¥15 通联支付网上收银统一下单接口
  • ¥15 angular有偿编写,