【问题标题】:Error while working on chatbot-universal-sentence-encoder使用聊天机器人通用句子编码器时出错
【发布时间】:2022-06-22 20:43:43
【问题描述】:

我正在尝试“chatbot.py”,但出现以下错误。

Traceback(最近一次调用最后一次): 文件“chatbot.py”,第 11 行,在 嵌入 = 模型(df[“MESSAGE”].values)[“输出”] IndexError:只有整数、切片 (:)、省略号 (...)、numpy.newaxis (None) 和整数或布尔数组是有效的索引

###########################
# chatbot.py starts here
from urllib import response
from utils import *
# pip install pandas
import pandas as pd
import numpy as np
# pip install openpyxl

model = embed_useT(r"C:/Users/Hp/Downloads/universal-sentence-encoder_4")
df = pd.read_excel("chats.xlsx")
print ('printing df', df)
embeddings = model(df["MESSAGE"].values)["outputs"]
norm = np.linalg.norm(embeddings, axis = -1)

def reply(message):
    message_vector = model([message])["outputs"]
    similarities = cos_similarity(message_vector, embeddings, norm)
    index = np.argmax(similarities)
    response = df["RESPONSE"].values[index]
    return response

while True:
    message = input("Type your message: ")
    response = reply(message)
    print (response)
# chatbot.py ends here
###########################

###########################
# utils.py starts here
import numpy as np
# pip install tensorflow-hub
import tensorflow_hub as hub 
# pip install tensorflow

# import tensorflow as tf
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

def cos_similarity(vector, matrix, matrix_norm):
    dot = np.matmul(matrix, vector.T)
    vector_norm = np.linalg.norm(vector)
    norms = (vector_norm * matrix_norm); norms=norms.reshape(norms.shape[0],1)
    return dot / norms

def embed_useT(module):
    with tf.Graph().as_default():
        sentences = tf.placeholder(tf.string)
        embed = hub.KerasLayer(module)
        embeddings = embed(sentences)
        session = tf.train.MonitoredSession()
        return lambda x: session.run(embeddings, {sentences : x})
# utils.py ends here
###########################

chats.xlsx如下图

【问题讨论】:

    标签: python pandas numpy tensorflow-hub


    【解决方案1】:

    model(df["MESSAGE"].values) 已经将嵌入作为 numpy 数组返回。省略 ["outputs"] 应该可以正常工作。

    【讨论】:

    • 感谢您的建议。能否请你帮忙?我将第 11 行更改为 embeddings = model(df["MESSAGE"].values) 并将第 15 行更改为 message_vector = model({message}) 并引发错误,如下所示 File "C:\Users\chetan .chavan\Downloads\universal-sentence-encoder_4\chatbot.py",第 17 行,回复 message_vector = model({message}) raise type(e)(node_def, op, message) # pylint: disable=no-value- for-parameter tensorflow.python.framework.errors_impl.InternalError: Graph execution error:
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-06
    • 1970-01-01
    • 2022-09-29
    相关资源
    最近更新 更多