1.0.2 • Published 8 months ago

plx.js v1.0.2

Weekly downloads
-
License
Apache-2.0
Repository
-
Last release
8 months ago

PLX.js

PLX.js is a comprehensive JavaScript library designed for various machine learning tasks, including question answering, translation, text classification, and image classification. This library leverages neural networks and provides a flexible API for training, saving, and loading models. Below is a detailed overview of the features, systems, and usage examples for each component of PLX.js.

Features

  • Question Answering: Utilize neural networks to answer questions based on provided contexts.
  • Translation: Translate text from one language to another using trained models.
  • Text Classification: Classify text into predefined categories.
  • Image Classification: Classify images into predefined categories.
  • Model Persistence: Save and load models for reuse without retraining.
  • Customizable Neural Networks: Configure network types and layers for specific tasks.

Installation

To use PLX.js, clone the repository and install the necessary dependencies:

npm install plx.js

Usage

Question Answering

The Question Answering system uses neural networks to find answers to questions based on given contexts.

Example

const { QuestionAnswer } = require("plx.js");

// Create a new network
const net = new QuestionAnswer({
    hiddenLayers: [8, 4],
    fast: false // Uses LSTM
});

// Dataset
const dataset = [
    {
        input: "What is the capital of France?",
        output: "Paris"
    },
    // Add more data as needed
];

// Train the network
net.train(dataset, {
    iterations: 100,
    error: 0.005,
    log: true,
    rate: 0.01,
    shuffle: true,
    clear: true,
    momentum: 0
});

// Save the model
net.save("model2.json");

// Test the model
async function testModel() {
    const net2 = await QuestionAnswer.load("model2.json");
    const result = net2.run("What is the capital of France?", [
        "France is a country in Western Europe. Its capital is Paris."
    ]);
    console.log("Test result:", result);
}

testModel();

Translation

Translate text from one language to another using a trained neural network.

Example

const { Translation } = require("plx.js");

// Create a new translation model
const net = new Translation({
    hiddenLayers: [16, 8],
    fast: false // Uses LSTM
});

// Dataset
const dataset = [
    {
        input: "Merhaba dünya",
        output: "Hello world"
    },
    // Add more data as needed
];

// Train the model
net.train(dataset, {
    iterations: 100,
    error: 0.005,
    log: true,
    rate: 0.01,
    shuffle: true,
    clear: true,
    momentum: 0
});

// Save the model
net.save("translation_model.json");

// Load and test the model
async function testTranslation() {
    const net2 = await Translation.load("translation_model.json");
    const result = net2.run("Merhaba dünya");
    console.log("Translation result:", result);
}

testTranslation();

Text Classification

Classify text into categories such as "Sports" or "Technology".

Example

const { TextClassification } = require("plx.js");

// Create a new text classification model
const net = new TextClassification({
    hiddenLayers: [16, 8],
    fast: false // Uses LSTM
});

// Dataset
const dataset = [
    {
        text: "Lionel Messi transferred to PSG.",
        label: "Sports"
    },
    // Add more data as needed
];

// Train the model
net.train(dataset, {
    iterations: 10,
    error: 0.005,
    log: true,
    rate: 0.01,
    shuffle: true,
    clear: true,
    momentum: 0
});

// Save the model
net.save("classification_model.json");

// Load and test the model
async function testClassification() {
    const net2 = await TextClassification.load("classification_model.json");
    const result = net2.classify("Lionel Messi transferred to PSG.");
    console.log("Classification result:", result);
}

testClassification();

Image Classification

Classify images into predefined categories using a neural network.

Example

const { ImageClassification } = require("plx.js");

// Create a new image classification model
const classifier = new ImageClassification({
    hiddenLayers: [32, 16],
    type: "Perceptron"
});

// Train the model
classifier.train('dataset', {
    iterations: 10,
    error: 0.005,
    rate: 0.01,
    shuffle: true,
    log: true,
    clear: true,
    momentum: 0
});

// Save the model
classifier.save("image_model.json");

// Load and test the model
async function testImageClassification() {
    const classifier2 = await ImageClassification.load("image_model.json");
    const result = await classifier2.classify('image.png');
    console.log('Image classification result:', result);
}

testImageClassification();

Engine.js Usage

The engine.js file provides the core functionality for creating and managing neural networks. Below is an example of how to use it to create a custom network.

Example

const { Network } = require("plx.js");

// Create a new network
const net = new Network.LSTM({
    inputSize: 10,
    hiddenLayers: [20, 10],
    outputSize: 5
});

// Example dataset
const dataset = [
    { input: [0, 1, 0, 1, 0, 1, 0, 1, 0, 1], output: [1, 0, 0, 0, 0] },
    // Add more data as needed
];

// Train the network
net.train(dataset, {
    iterations: 100,
    error: 0.005,
    log: true,
    rate: 0.01,
    shuffle: true,
    clear: true,
    momentum: 0
});

// Save the model
net.save("custom_model.json");

// Load and test the model
async function testCustomNetwork() {
    const net2 = await Network.loadModel("custom_model.json");
    const result = net2.run([0, 1, 0, 1, 0, 1, 0, 1, 0, 1]);
    console.log("Custom network result:", result);
}

testCustomNetwork();

Model Persistence

PLX.js supports saving and loading models to and from JSON files, allowing for easy model reuse without retraining.

Saving a Model

await net.save("model.json");

Loading a Model

const net2 = await Network.loadModel("model.json");

Customization

PLX.js allows customization of neural network configurations, including the number of hidden layers and the choice between GRU and LSTM networks.

Contributing

Contributions are welcome! Please fork the repository and submit a pull request for any improvements or bug fixes.

License

This project is licensed under the Apache-2.0 License.