keyerror: 2

huangapple go评论68阅读模式
英文:

encoded_sentence = [label2int[start_index] for generated_text in input_sentence] keyerror:2

问题

这是一个SentenceRecognitionModel,它不能工作,我在这方面没有太多经验,能有人帮帮我吗?

这部分是代码它用于导入所需的库和模块
# 将文本中的字符转换成数字
chars = list(set(text))
# 建立标签,用于存储不同的句子
labels = [""]
# 建立标签的数字映射
int2label = dict(enumerate(labels))
label2int = {label: index for index, label in int2label.items()}
# 将文本进行编码
encoded_labels = [label2int[label] for label in labels]
# 建立字符和数字的映射
int2char = dict(enumerate(chars))
char2int = {char: index for index, char in int2char.items()}
# 将文本进行编码
encoded_text = [char2int[char] for char in text]
# 定义训练模型
class SentenceRecognitionModel(nn.Module):
# 創建模型
model = SentenceRecognitionModel(len(chars),embedding_dim = 256, hidden_dim = 64, num_layers = 2, num_classes=len(labels))
# 創建優化器
optimizer = optim.Adam(model.parameters())
# 紀錄模型訓練損耗
loss_fn = nn.CrossEntropyLoss()
# 訓練模型
for epoch in range(num_epochs):
# 將模型設為推理模式,以保持一致的輸出
model.eval()
# 接收用户输入的句子
input_sentence = input("请输入句子:")
# 判断输入的句子是否存在于训练数据中
if input_sentence not in labels:
# 将模型的状态保存到文件中
model.save(model.state_dict(), 'SentenceRecognizedModel.pt')

在运行代码时,出现了KeyError: 2的错误。encoded_sentence = [label2int[start_index] for generated_text in input_sentence] 将句子转换为数字,但是AI模型可能无法接受这样的输入。

英文:

This is a SentenceRecoghizedModel,it can't work,I don't have much experience on this,can somebody help me?

import torch
import torch.nn as nn
import torch.optim as optim

#導入訓練模型
text = ""
#將text中的字數轉換成數字
chars = list(set(text))
#建立labels,用以儲存不同的句子
labels = [""]
int2label = dict(enumerate(labels))
label2int = {label: index for index, label in int2label.items()}
encoded_labels = [label2int[label] for label in labels]
int2char = dict(enumerate(chars))
char2int = {char: index for index, char in int2char.items()}
encoded_text = [char2int[char] for char in text]

#定義訓練環境
class SentenceRecognitionModel(nn.Module):
    def __init__(self, vocab_size, embedding_dim, hidden_dim, num_layers,num_classes):
        super().__init__()
        self.embedding = nn.Embedding(vocab_size,embedding_dim)
        self.lstm = nn.LSTM(embedding_dim,hidden_dim, num_layers)
        self.fc = nn.Linear(hidden_dim, num_classes)
    def forward(self, x):
        x = self.embedding(x)
        x = self.fc(x)
        return x

#創建模型
model = SentenceRecognitionModel(len(chars),embedding_dim = 256, hidden_dim = 64, num_layers =     2, num_classes=len(labels))
#創建優化器
optimizer = optim.Adam(model.parameters())
#紀錄模型訓練損耗
loss_fn = nn.CrossEntropyLoss()

#訓練模型
num_epochs = 25000 #訓練二萬五千次模型
for epoch in range(num_epochs):
    #將每一次訓練前的梯度設為零,這樣在下次迭代時就不會衝突到了
    optimizer.zero_grad()
    input_seq = torch.tensor(encoded_text).unsqueeze(0)
    target_label = torch.tensor(encoded_labels).unsqueeze(0)
    output = model(input_seq)
    loss = loss_fn(output.squeeze(), target_label.squeeze())
    loss.backward()
    optimizer.step()
    if (epoch+1) % 10 == 0:
    print(f'Epoch [{epoch+1}/{num_epochs}], Loss: {loss.item():.4f}')
#將模型設為推理模式,以讓模型的輸出保持一致
model.eval()
while True:
  # 接收用戶輸入的句子
  input_sentence = input("請輸入句子:")
  if input_sentence not in labels:
    print("輸入的句子不存在於訓練數據中")
  else:
    start_index = label2int[input_sentence]
    generated_text = int2char[start_index]
    # 選擇要生成的文本長度
    num_generated = 100

    # 對輸入的句子進行預處理,將其編碼為數字
    encoded_sentence = [label2int[start_index] for generated_text in input_sentence]
    input_seq = torch.tensor(encoded_sentence).unsqueeze(0)

    # 將輸入的句子傳遞到模型中
 
    with torch.no_grad():
        output = model(input_seq)

    #使用模型去辨識句子
    output_idx = torch.argmax(output.squeeze()).item()
    recognized_label = int2label[output_idx]

# 輸出結果
print(f"Recognized Label: {recognized_label}")
FILE = 'model_state_dict.pt'
model.save(model.state_dict(), 'SentenceRecognizedModel.pt')    

When I run the code,the following code appear KeyError: 2 wrong

encoded_sentence = [label2int[start_index] for generated_text in input_sentence]
input_seq = torch.tensor(encoded_sentence).unsqueeze(0)

encoded_sentence = [label2int[start_index] for generated_text in input_sentence] is convert sentences into numbers,but ai model do not accept.

答案1

得分: 0

终于,我解决了代码问题。

encoded_sentence = [label2int[start_index] for generated_text in input_sentence]

应该改成:

encoded_sentence = [start_index]
因为start_index是整数,所以将其添加到encoded_sentence中就可以了。
英文:

Finally, I solved the code problem.

 encoded_sentence = [label2int[start_index] for generated_text in input_sentence]

should be changed to this

encoded_sentence = [start_index]

because the start_index is int, so add it to encoded_sentence is just ok.

huangapple
  • 本文由 发表于 2023年2月8日 11:37:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75381157.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定