机器学习:大小不匹配,m1:[46 x 20],m2:[12 x 12]

时间:2019-12-02 12:37:37

标签: python numpy machine-learning error-handling pytorch

我真的是机器学习社区的新手,我正在做Pytorch / Captum教程。具体来说就是:https://captum.ai/tutorials/Titanic_Basic_Interpret 现在我正尝试对另一个数据集执行相同的操作: https://www.kaggle.com/hb20007/gender-classification

我的代码如下:

dataset_path = "assets/Transformed Data Set - Sheet1.csv"
gender_data = pd.read_csv(dataset_path)

gender_data = pd.concat([gender_data,
                          pd.get_dummies(gender_data['Favorite Color'], prefix="color"),
                          pd.get_dummies(gender_data['Favorite Music Genre'],prefix="music"),
                          pd.get_dummies(gender_data['Favorite Beverage'],prefix="beverage"),
                          pd.get_dummies(gender_data['Favorite Soft Drink'],prefix="drink")], axis=1)
gender_data = gender_data.drop(['Favorite Color','Favorite Music Genre','Favorite Beverage','Favorite Soft Drink'], axis=1)


# Set random seed for reproducibility.
np.random.seed(131254)

# Convert features and labels to numpy arrays.
labels = gender_data["Gender"].to_numpy()
gender_data = gender_data.drop(['Gender'], axis=1)
feature_names = list(gender_data.columns)
data = gender_data.to_numpy()

# Separate training and test sets using 
train_indices = np.random.choice(len(labels), int(0.7*len(labels)), replace=False)
test_indices = list(set(range(len(labels))) - set(train_indices))
train_features = data[train_indices]
train_labels = labels[train_indices]
test_features = data[test_indices]
test_labels = labels[test_indices]

import torch
import torch.nn as nn
torch.manual_seed(1)  # Set seed for reproducibility.
class GenderSimpleNNModel(nn.Module):
    def __init__(self):
        super().__init__()
        self.linear1 = nn.Linear(12, 12)
        self.sigmoid1 = nn.Sigmoid()
        self.linear2 = nn.Linear(12, 8)
        self.sigmoid2 = nn.Sigmoid()
        self.linear3 = nn.Linear(8, 2)
        self.softmax = nn.Softmax(dim=1)

    def forward(self, x):
        lin1_out = self.linear1(x)
        sigmoid_out1 = self.sigmoid1(lin1_out)
        sigmoid_out2 = self.sigmoid2(self.linear2(sigmoid_out1))
        return self.softmax(self.linear3(sigmoid_out2))

net = GenderSimpleNNModel()
USE_PRETRAINED_MODEL = False

if USE_PRETRAINED_MODEL:
    net.load_state_dict(torch.load('models/gender_model.pt'))
    print("Model Loaded!")
else:
    criterion = nn.CrossEntropyLoss()
    num_epochs = 200

    optimizer = torch.optim.Adam(net.parameters(), lr=0.1)
    input_tensor = torch.from_numpy(train_features).type(torch.FloatTensor)
    label_tensor = torch.from_numpy(train_labels)
    for epoch in range(num_epochs):    
        output = net(input_tensor)
        loss = criterion(output, label_tensor)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        if epoch % 20 == 0:
            print ('Epoch {}/{} => Loss: {:.2f}'.format(epoch+1, num_epochs, loss.item()))

    torch.save(net.state_dict(), 'models/gender_model.pt')

现在我已经完成了教程中的操作,但是出现了一个错误,具体来说就是这个错误:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-7-c565b5b7e2f2> in <module>
     13     label_tensor = torch.from_numpy(train_labels)
     14     for epoch in range(num_epochs):
---> 15         output = net(input_tensor)
     16         loss = criterion(output, label_tensor)
     17         optimizer.zero_grad()

~/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    539             result = self._slow_forward(*input, **kwargs)
    540         else:
--> 541             result = self.forward(*input, **kwargs)
    542         for hook in self._forward_hooks.values():
    543             hook_result = hook(self, input, result)

<ipython-input-6-dbf1be8585cd> in forward(self, x)
     13 
     14     def forward(self, x):
---> 15         lin1_out = self.linear1(x)
     16         sigmoid_out1 = self.sigmoid1(lin1_out)
     17         sigmoid_out2 = self.sigmoid2(self.linear2(sigmoid_out1))

~/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    539             result = self._slow_forward(*input, **kwargs)
    540         else:
--> 541             result = self.forward(*input, **kwargs)
    542         for hook in self._forward_hooks.values():
    543             hook_result = hook(self, input, result)

~/anaconda3/lib/python3.7/site-packages/torch/nn/modules/linear.py in forward(self, input)
     85 
     86     def forward(self, input):
---> 87         return F.linear(input, self.weight, self.bias)
     88 
     89     def extra_repr(self):

~/anaconda3/lib/python3.7/site-packages/torch/nn/functional.py in linear(input, weight, bias)
   1368     if input.dim() == 2 and bias is not None:
   1369         # fused op is marginally faster
-> 1370         ret = torch.addmm(bias, input, weight.t())
   1371     else:
   1372         output = input.matmul(weight.t())

RuntimeError: size mismatch, m1: [46 x 20], m2: [12 x 12] at /opt/conda/conda-bld/pytorch_1570710822989/work/aten/src/TH/generic/THTensorMath.cpp:197

我很绝望。我不知道在哪里放东西,以便尺寸匹配,并且我不会再收到该错误了。请帮助:S

0 个答案:

没有答案