pandas pytorch网的尺寸不匹配

qco9c6ql  于 2023-03-16  发布在  其他
关注(0)|答案(1)|浏览(82)

我使用下面的代码从训练过的网络中得到一个预测:

import pandas as pd
import torch
import torch.nn as nn
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer

FC1_WEIGHT = 105282  # features in trained net
MODEL_NAME = '/path/to/net.pth'

# Define the model
class Net(nn.Module):
    def __init__(self, input_dim):
        super(Net, self).__init__()
        self.fc1 = nn.Linear(input_dim, 64)
        self.fc2 = nn.Linear(64, 32)
        self.fc3 = nn.Linear(32, 2)
        self.dropout = nn.Dropout(0.5)
        self.relu = nn.ReLU()

    def forward(self, x):
        x = self.fc1(x)
        x = self.relu(x)
        x = self.dropout(x)
        x = self.fc2(x)
        x = self.relu(x)
        x = self.dropout(x)
        x = self.fc3(x)
        return x

# Load the data
test_df = pd.read_csv('input.csv')

# Replace missing values with an empty string
test_df = test_df.fillna('')

# Load the vectorizer
vectorizer = TfidfVectorizer(stop_words='english')
vectorizer.fit(test_df['title'] + ' ' + test_df['text'])

# Load the model
# model = Net(len(vectorizer.get_feature_names_out()))
model = Net(FC1_WEIGHT)  # features in trained net
model.load_state_dict(torch.load(MODEL_NAME, map_location=torch.device('cpu')))
model.eval()

# Make predictions
with torch.no_grad():
    x_title = test_df['title']
    x_text = test_df['text']
    x = vectorizer.transform(x_title + ' ' + x_text).toarray()
    x = np.pad(x, ((0, 0), (0, 105282 - x.shape[1])), 'constant')
    inputs = torch.FloatTensor(x).resize(2, FC1_WEIGHT)
    outputs = model(inputs)
    _, predicted = torch.max(outputs.data, 1)
    # print(predicted)
    
    # Format output into readable format: 0 for fake, 1 for real
    predicted = str(predicted)
    if '0' in predicted:
        print('Fake')
    else:
        print('Real')

但是,我得到以下错误:

Traceback (most recent call last):
  File "/path/to/main.py", line 51, in <module>
    outputs = model(inputs)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1194, in _call_impl
    return forward_call(*input, **kwargs)
  File "/path/to/main.py", line 20, in forward
    x = self.fc1(x)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1194, in _call_impl
    return forward_call(*input, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/torch/nn/modules/linear.py", line 114, in forward
    return F.linear(input, self.weight, self.bias)
RuntimeError: mat1 and mat2 shapes cannot be multiplied (2x633 and 105282x64)

我已经检查了我的网的尺寸,它是105282x64。我不知道为什么会有大小不匹配。任何帮助将不胜感激。
编辑:添加了导致错误的整个python文件。

ghhkc1vu

ghhkc1vu1#

也许您需要将其松开以获得批次表(1、2、105282)
torch.unsqueeze()

相关问题