【问题标题】:How to set the input shape for a chatbot NN如何为聊天机器人 NN 设置输入形状
【发布时间】:2021-10-17 10:49:14
【问题描述】:

我一直在尝试用 python 制作一个简单的聊天机器人,但输入形状有问题 我的神经网络如下:

from keras import models
from keras import layers
model = models.Sequential()
model.add(layers.Dense(8,activation='relu', input_shape=(46,)))
model.add(layers.Dense(8,activation='relu'))
model.add(layers.Dense(len(output[0]), activation = 'softmax'))


model.compile(optimizer='rmsprop',
              loss='binary_crossentropy',
              metrics=['accuracy'])
model.fit(training,output,epochs = 200, batch_size=8)

我的数据的输入形状是(26, 46),是一袋词的形式 当我尝试与模型聊天时,会显示此错误

Error when checking input: expected dense_141_input to have shape (46,) but got array with shape (1,)

还有我的聊天方式

def chat():
    print("Chat with the bot type quit to end ")
    while True:
        inp = input("You: ")
        if inp.lower() == "quit":
            break

        results = model.predict([bag_of_words(inp, words)])
        results_index = numpy.argmax(results)
        tag = labels[results_index]

        for tg in data["intents"]:
            if tg['tag'] == tag:
                responses = tg['responses']

        print(random.choice(responses))

【问题讨论】:

    标签: keras neural-network chatbot


    【解决方案1】:

    原来在 predict 方法中我必须使用 [[]] 并且因为我没有出现错误,所以 chat 方法的固定代码看起来像这样

    def chat():
        print("Chat with the bot type quit to end ")
        while True:
            inp = input("You: ")
            if inp.lower() == "quit":
                break
    
            results = model.predict([[bag_of_words(inp, words)]])
            results_index = numpy.argmax(results)
            tag = labels[results_index]
    
            for tg in data["intents"]:
                if tg['tag'] == tag:
                    responses = tg['responses']
    
            print(random.choice(responses))
    

    希望这对遇到类似问题的人有所帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-10
      相关资源
      最近更新 更多