pytorch model =您的模型类(输入大小,隐藏大小,层数,输出大小)名称错误:未定义名称“input_size”

rjee0c15  于 2023-02-04  发布在  其他
关注(0)|答案(1)|浏览(258)
import torch
import torch.nn as nn
import torch.optim as optim
answer = input()

class YourModelClass(nn.Module):
    def __init__(self, input_size, hidden_size, num_layers, output_size):
        super().__init__()
        self.lstm = nn.LSTM(input_size, hidden_size, num_layers)
    self.fc = nn.Linear(hidden_size, output_size)

def forward(self, x):
    out, (h_n, c_n) = self.lstm(x)
    out = self.fc(out[:, -1, :])
    return out

model = YourModelClass(input_size, hidden_size, num_layers, output_size)
model.load_state_dict(torch.load('model.ckpt'))
model.eval()

start_index = answer
generated_text = int2char[start_index.item()]

num_generated = 10

with torch.no_grad():
    for i in range(num_generated):
        input_seq = torch.tensor([char2int[generated_text[-1]]]).unsqueeze(0)
        output = model(input_seq)
        output_idx = torch.argmax(output.squeeze()).item()
        generated_text += int2char[output_idx]

print(generated_text)

我无法定义输入大小、隐藏大小、层数和输出大小
编辑给了我一个错误:model =您的模型类(输入大小,隐藏大小,层数,输出大小)名称错误:未定义名称“input_size”

j5fpnvbx

j5fpnvbx1#

您尚未定义任何变量。请尝试以下操作:

input_size = 100
hidden_size = 128
num_layers = 2
output_size = 50
        
model = YourModelClass(input_size, hidden_size, num_layers, output_size)

相关问题