pyTorch mat1 和 mat2 不能相乘

pyTorch mat1 and mat2 cannot be multiplied

我收到以下错误:

RuntimeError: mat1 and mat2 shapes cannot be multiplied (32x33856 and 640000x256)

我不明白我需要如何更改我的网络参数。我拿了在this paper中创建的网络并尝试修改参数以满足我的needs.This是代码,我改变了第一个卷积的参数但仍然得到错误:

class ChordClassificationNetwork(nn.Module):
    def __init__(self, train_model=False):
        super(ChordClassificationNetwork, self).__init__()
        self.train_model = train_model
        self.flatten = nn.Flatten()
        self.firstConv = nn.Conv2d(3, 64, (3, 3))
        self.secondConv = nn.Conv2d(64, 64, (3, 3))
        self.pool = nn.MaxPool2d(2)
        self.drop = nn.Dropout(0.25)
        self.fc1 = nn.Linear(100*100*64, 256)
        self.fc2 = nn.Linear(256, 256)
        self.outLayer = nn.Linear(256, 7)

    def forward(self, x):
        x = self.firstConv(x)
        x = F.relu(x)
        x = self.pool(x)
        x = self.secondConv(x)
        x = F.relu(x)
        x = self.pool(x)
        x = self.drop(x)
        x = self.flatten(x)
        x = self.fc1(x)
        x = F.relu(x)
        x = self.drop(x)
        x = self.fc2(x)
        x = F.relu(x)
        x = self.drop(x)
        x = self.outLayer(x)
        output = F.softmax(x, dim=1)
        return output 

这是训练文件:

device = ("cuda" if torch.cuda.is_available() else "cpu")

transformations = transforms.Compose([
     transforms.Resize((100, 100))
])

num_epochs = 10
learning_rate = 0.001
train_CNN = False
batch_size = 32
shuffle = True
pin_memory = True
num_workers = 1

dataset = GuitarDataset("../chords_data/cropped_images/train", transform=transformations)
train_set, validation_set = torch.utils.data.random_split(dataset, [int(0.8 * len(dataset)), len(dataset) - int(0.8*len(dataset))])
train_loader = DataLoader(dataset=train_set, shuffle=shuffle, batch_size=batch_size, num_workers=num_workers,
                          pin_memory=pin_memory)
validation_loader = DataLoader(dataset=validation_set, shuffle=shuffle, batch_size=batch_size, num_workers=num_workers,
                               pin_memory=pin_memory)

model = ChordClassificationNetwork().to(device)

criterion = nn.BCELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)


def check_accuracy(loader, model):
    if loader == train_loader:
        print("Checking accuracy on training data")
    else:
        print("Checking accuracy on validation data")

    num_correct = 0
    num_samples = 0
    model.eval()

    with torch.no_grad():
        for x, y in loader:
            x = x.to(device=device)
            y = y.to(device=device)

            scores = model(x)
            predictions = torch.tensor([1.0 if i >= 0.5 else 0.0 for i in scores]).to(device)
            num_correct += (predictions == y).sum()
            num_samples += predictions.size(0)
            print(
                f"Got {num_correct} / {num_samples} with accuracy {float(num_correct) / float(num_samples) * 100:.2f}"
            )
    return f"{float(num_correct) / float(num_samples) * 100:.2f}"


def train():
    model.train()
    for epoch in range(num_epochs):
        loop = tqdm(train_loader, total=len(train_loader), leave=True)
        if epoch % 2 == 0:
            loop.set_postfix(val_acc=check_accuracy(validation_loader, model))
        for imgs, labels in loop:
            imgs = imgs.to(device)
            labels = labels.to(device)
            outputs = model(imgs)
            loss = criterion(outputs, labels)
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()
            loop.set_description(f"Epoch [{epoch}/{num_epochs}]")
            loop.set_postfix(loss=loss.item())


if __name__ == "__main__":
    train()

我做错了什么?

查看报错信息,问题出在fc1层,它没有所需数量的神经元。它正在接收形状为 (batch_size, 33856) 但期望为 (batch_size, 640000) 的张量。维度的减少是由您在 fc1.

之前应用于输入张量的不同层引起的

您可以通过定义 fc1 来解决此问题:

        self.fc1 = nn.Linear(33856, 256)

或者,您可以使用 nn.LazyLinear,它将根据接收到的输入在运行时使用适当数量的神经元初始化其权重。但这很懒惰:

        self.fc1 = nn.LazyLinear(256)