簡體   English   中英

訓練聊天機器人時 Tensorflow 模型錯誤

[英]Tensorflow model error when training chatbot

我的代碼有問題,訓練聊天機器人的功能有一些錯誤。

這個想法是從 Sqlite3 數據庫中訓練 tensorflow v2。 但是每當我修復一個錯誤時,另一個錯誤就會出現。

下面我留下了我正在使用的代碼和規范。

這是它返回的錯誤: 'list' object has no attribute 'dtype' / inputs = tf.keras.layers.Embedding(1000, 64, input_length=10)(inputs)

▶ 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()

我認為問題在於將輸入定義為填充序列,然后用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'))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM