Skip to content

GalaktaGlareLSTM

Wesley Yan Soares Brehmer edited this page Jun 14, 2024 · 1 revision

GalaktaGlareLSTM Documentation

Overview

The GalaktaGlareLSTM package is a simple implementation of a Long Short-Term Memory (LSTM) model for training and responding to text-based questions. It provides a straightforward interface for feeding in training data and querying the model with questions. This document provides a comprehensive guide to using the GalaktaGlareLSTM package, complete with examples and detailed explanations.

Installation

To use the GalaktaGlareLSTM package, simply import it into your Go project. Ensure that your Go environment is set up correctly.

import "github.com/simplyYan/GalaktaGlare/src/GalaktaGlareLSTM"

Usage

Creating an LSTM Instance

To create a new instance of the LSTM model, use the New function:

lstm := GalaktaGlareLSTM.New()

This initializes an LSTM object with an empty knowledge base.

Training the LSTM

To train the LSTM model, use the Train method. This method accepts a single string argument, where each line represents a training example in the format question=answer. Here is an example of how to train the model:

trainingData := `
{What is the capital of France?}=Paris
{Who wrote '1984'?}=George Orwell
`

lstm.Train(trainingData)

Explanation

  • Data Format: The training data should consist of lines, each containing a question and an answer separated by an = sign.
  • Question and Answer Trimming: Both the question and the answer are trimmed of whitespace and enclosing characters such as {}, [], and " to ensure clean inputs.

Querying the LSTM

To get an answer from the trained model, use the Run method. This method takes a single string argument (the question) and returns the corresponding answer if it exists in the knowledge base.

question := "What is the capital of France?"
answer := lstm.Run(question)
fmt.Println(answer)  // Output: Paris

If the question is not found in the knowledge base, the model returns a default response:

unknownQuestion := "What is the meaning of life?"
unknownAnswer := lstm.Run(unknownQuestion)
fmt.Println(unknownAnswer)  // Output: I don't know the answer to that.

Full Example

Here is a complete example demonstrating the creation, training, and querying of an LSTM instance:

package main

import (
	"fmt"
	"github.com/simplyYan/GalaktaGlare/src/GalaktaGlareLSTM"
)

func main() {
	// Create a new LSTM instance
	lstm := GalaktaGlareLSTM.New()

	// Define training data
	trainingData := `
	{What is the capital of France?}=Paris
	{Who wrote '1984'?}=George Orwell
	{What is the capital of Germany?}=Berlin
	`

	// Train the LSTM model
	lstm.Train(trainingData)

	// Query the LSTM model
	questions := []string{
		"What is the capital of France?",
		"Who wrote '1984'?",
		"What is the capital of Germany?",
		"What is the capital of Italy?",
	}

	// Print answers
	for _, question := range questions {
		answer := lstm.Run(question)
		fmt.Printf("Q: %s\nA: %s\n", question, answer)
	}
}

Expected Output

Q: What is the capital of France?
A: Paris
Q: Who wrote '1984'?
A: George Orwell
Q: What is the capital of Germany?
A: Berlin
Q: What is the capital of Italy?
A: I don't know the answer to that.

Detailed Function Descriptions

func New() *LSTM

  • Description: Initializes a new LSTM instance with an empty knowledge base.
  • Returns: A pointer to the newly created LSTM instance.

func (l *LSTM) Train(data string)

  • Description: Trains the LSTM model with the provided training data.
  • Parameters:
    • data: A string containing training examples in the format question=answer, separated by new lines.
  • Behavior:
    • Splits the data into lines.
    • Processes each line to extract the question and answer.
    • Stores the question-answer pairs in the knowledge base.

func (l *LSTM) Run(question string) string

  • Description: Queries the LSTM model with a given question.
  • Parameters:
    • question: The question to be answered.
  • Returns:
    • The answer if the question exists in the knowledge base.
    • "I don't know the answer to that." if the question is not found.

Conclusion

The GalaktaGlareLSTM package offers a simple and effective way to implement a text-based question-answer model using LSTM. With easy-to-understand training and querying methods, this package is suitable for applications that require a basic but functional LSTM model for handling text data.