model = YourModelClass(input_size, hidden_size, num_layers, output_size) NameError: name 'input_size' is not defined

225 views Asked by At
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)

I can't to definition input_size, hidden_size, num_layers, output_size

the editor gived me a error: model = YourModelClass(input_size, hidden_size, num_layers, output_size) NameError: name 'input_size' is not defined

1

There are 1 answers

1
Horseman On

You haven't defined any variables. Try something like this:

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