Skip to content

能在ChatterBot中加入注意力机制吗? #41

Description

@win10ogod

我不太了解编程不过试着问了chatgpt。
这是chatgpt的代碼纪录:

from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer
from chatterbot.conversation import Statement
import numpy as np

# Define a function to calculate the dot product attention score
def dot_product_attention(query, values):
    # Calculate the dot product between the query and each value
    scores = np.dot(query, values.T)
    # Apply softmax to the scores to get the attention weights
    attention_weights = np.softmax(scores)
    # Calculate the weighted average of the values using the attention weights
    weighted_values = np.dot(attention_weights, values)
    return weighted_values
def multihead_attention(query, keys, values, num_heads):
    # Split the query, keys, and values into multiple heads
    query_heads = np.array_split(query, num_heads, axis=-1)
    key_heads = np.array_split(keys, num_heads, axis=-1)
    value_heads = np.array_split(values, num_heads, axis=-1)

    # Concatenate the heads along the last axis
    query_heads = np.concatenate(query_heads, axis=-1)
    key_heads = np.concatenate(key_heads, axis=-1)
    value_heads = np.concatenate(value_heads, axis=-1)

    # Calculate the dot product between the query and each key head
    scores = np.dot(query_heads, key_heads.T)
    # Scale the scores by the square root of the number of features
    scores = scores / np.sqrt(query.shape[-1])

    # Apply softmax to the scores to get the attention weights
    attention_weights = np.softmax(scores, axis=-1)

    # Calculate the weighted average of the value heads using the attention weights
    weighted_values = np.dot(attention_weights, value_heads)

    # Concatenate the weighted value heads along the last axis
    weighted_values = np.concatenate(np.split(weighted_values, num_heads, axis=-1), axis=-1)

    return weighted_values

# Read the conversation corpus file
with open('db.txt', 'r', encoding='utf-8') as f:
    corpus = f.readlines()

# Create a ChatBot instance and train it
my_bot = ChatBot(input('请输入ChatBot名称:'))
trainer = ListTrainer(my_bot)

print('开始训练!')

# Train the ChatBot instance with each conversation in the corpus
for conversation in corpus:
    # Split the conversation into two statements
    statements = conversation.strip().split('\t')
    if len(statements) == 2:
        # Create a Statement object for each statement
        statement_1 = Statement(text=statements[0])
        statement_2 = Statement(text=statements[1])
        # Train the ChatBot with the two statements as a pair
        trainer.train([statement_1, statement_2])
    else:
        # If the conversation is not in the expected format, skip it
        continue

print('训练完毕!')

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions