训练聊天机器人时Tensorflow模型错误

nzkunb0c  于 2022-12-27  发布在  其他
关注(0)|答案(1)|浏览(151)

我的代码中有一个问题,训练聊天机器人的函数有一些错误。
我们的想法是让Tensorflow v2从Sqlite3数据库中训练,但是每当我修复一个错误时,另一个错误就会出现。
下面我留下的代码和规范,我正在使用.
这是它返回的错误:* *"列表"对象没有属性"dtype"**/输入= tf. keras. layers。嵌入(1000,64,输入长度= 10)(输入)

▶ Python 3.10.9
▶ openai == 0.25.0
▶ tensorflow == 3.9.0
▶ pyttsx3 == 2.90
▶ speech_recognition == 3.9.0
▶ requests == 2.28.1
▶ numpy == 1.24.0
▶ nltk == 3.8
import sqlite3
import numpy as np
import openai
import requests
import speech_recognition as sr
import pyttsx3
import tensorflow as tf

DATABASE = "chatbot.db"

# Connection to the database and creation of the "conversas" table with two columns "pergunta" / "resposta"
conn = sqlite3.connect(DATABASE)
cursor = conn.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS conversations (question text, answer text)")
conn.commit()

# Voice recognition function
def recognize_voice():
    # Create a voice recognition object
    r = sr.Recognizer()

    # Start capturing audio from the microphone
    with sr.Microphone() as source:
        print("\n▶ ")
        audio = r.listen(source)
      
    # Use the OS's voice recognition to convert the audio to text
    try:
        text = r.recognize_google(audio, language='pt-BR')
    except sr.UnknownValueError:
        print("I didn't understand what you said")
        text = None
        
    if text.strip() == "":
        print("No text could be recognized")
        text = None
    return text

## Voice synthesis function
def synthesize_voice(text):
    # Initialize the voice synthesis engine
    engine = pyttsx3.init()

    # Set the voice synthesis language
    voices = engine.getProperty('voices')
    engine.setProperty('voice', voices[-2].id)

    # Voice synthesis
    engine.say(text)

    # Run the voice synthesis
    engine.runAndWait()

# Function to search Wikipedia
def search_wikipedia(keyword):
    # Search Wikipedia for the specified topic
    url = f"https://pt.wikipedia.org/w/api.php?action=opensearch&format=json&search={keyword}"
    response = requests.get(url)
    data = response.json()

    # Check if there were any results for the search
    if len(data[1]) > 0:
        # Get the URL of the results page
        page_url = data[3][0]

        # Make a new HTTP call to get the content of the results page
        page_response = requests.get(page_url)
        page_html = page_response.text

        # Extract the first paragraph from the page
        start = page_html.index("<p>")
        end = page_html.index("</p>")
        paragraph = page_html[start:end+4]

        return paragraph
    else:
        return "No information could be found about the specified topic."

# Function to train the neural network
def train_conversation_model(vocabulary_size=10000, maximum_sequence_length=100, embedding_dimension=32):
    # Open a connection to the database
    conn = sqlite3.connect(DATABASE)
        
    # Create a cursor to access the data
    cursor = conn.cursor()

    # Execute a query to retrieve the data from the database
    cursor.execute("SELECT question, answer FROM conversations")
    data = cursor.fetchall()

    # Define the inputs and outputs of the model
    inputs = []
    outputs = []

    # Iterate over the data and separate the inputs (strings) from the outputs (labels)
    for datum in data:
        inputs.append(datum[0])
        outputs.append(datum[1])

    # Transform the inputs into tensors with TensorFlow    
    if inputs is not None and inputs != []:
        print("Attention: the 'inputs' variable is invalid!")
    else:
        inputs = [str(i) for i in inputs]
        inputs = tf.keras.preprocessing.text.Tokenizer(num_words=vocabulary_size, lower=True).texts_to_sequences(inputs)
        inputs = tf.keras.preprocessing.sequence.pad_sequences(inputs, maxlen=maximum_sequence_length)

    # Add an embedding layer
    inputs = tf.keras.layers.Embedding(1000, 64, input_length=10)(inputs)

    # Define the model
    model = tf.keras.Sequential()
    model.add(tf.keras.layers.LSTM(units=64, return_sequences=True))
    model.add(tf.keras.layers.LSTM(units=32))
    model.add(tf.keras.layers.Dense(units=1, activation='sigmoid'))

    # Compile the model
    model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

    # Train the model
    model.fit(inputs, outputs, epochs=10, batch_size=64)

    # Save the trained model
    model.save_weights('minha_rede_neural.h5')

# Use the openAI GPT-3 library to generate the chatbot's responses.
def generate_response(question):
    openai.api_key = "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"
    model_engine = "text-davinci-003"
    prompt = (f"What is the answer to the following question?\n{question}")

    completions = openai.Completion.create(
        engine=model_engine,
        prompt=prompt,
        max_tokens=2048,
        n=1,
        stop=None,
        temperature=0.8,
        top_p=1,
        frequency_penalty=1,
        presence_penalty=1,
    )

    message = completions.choices[0].text
    return message.strip()

## Main function to interact with the chatbot
def chatbot():
    while True:
        print("\nChoose the interaction method:")
        print("1. Voice")
        print("2. Chat")
        print("3. Train the neural network")
        print("4. Exit")
        option = input("Enter the desired option: ")
    
        if option == "1":
            # Voice interaction
            question = recognize_voice()
            if question is None:
                continue
            response = generate_response(question)
            synthesize_voice(response)
        elif option == "2":
            # Chat interaction
            question = input("\nYou: ")
            response = generate_response(question)
            print(f"\nChatbot: {response}")
        elif option == "3":
            # Train the neural network
            train_conversation_model('chatbot.db')
        elif option == "4":
            # Exit the chatbot
            break

# Start the chatbot
chatbot()
vnjpjtjt

vnjpjtjt1#

我认为问题在于将输入定义为填充序列,然后用inputs = tf.keras.layers.Embedding(1000, 64, input_length=10)(inputs)覆盖它。嵌入层需要成为神经网络架构的一部分才能工作,所以请尝试删除这一行,并将模型定义调整为如下内容(尚未测试):

model.add(tf.keras.layers.Embedding(1000, 64, input_length=10))
    model.add(tf.keras.layers.LSTM(units=64, return_sequences=True))
    model.add(tf.keras.layers.LSTM(units=32))
    model.add(tf.keras.layers.Dense(units=1, activation='sigmoid'))

相关问题