Skip to main content

A Python neural network library for simple networks and educational use.

Project description

PyNet 1.5.1

A simple educational neural network library built from scratch in Python.

PyNet focuses on understanding how neural networks work internally, including layers, activations, loss functions, backpropagation, optimizers, weight initialization, and model serialization.

Features

Layers

  • Dense (fully connected) layers
  • Dropout layers for reducing overfitting through neuron regularization

Activations

  • ReLU
  • LeakyReLU
  • Sigmoid
  • Tanh
  • Softmax
  • ELU
  • Swish
  • GELU

Loss Functions

  • MSELoss
  • CrossEntropyLoss
  • MAELoss
  • HuberLoss
  • BinaryCrossEntropyLoss

Optimizers

  • SGD
  • Momentum
  • Adam

Weight Initialization

  • He
  • Xavier
  • RandomNormal
  • Zeros

Utilities

  • Metrics
  • Model saving/loading

Quick Start Guide

  1. Install PyNet with pip install pynet-ai
  2. Install PyGame with pip install pygame (for the example)
  3. Paste example program below
import random
import pygame
from pynet import Network, Dense, LeakyReLU, Tanh, MSELoss, Adam, Xavier

WIDTH, HEIGHT = (480, 360)
FPS = 60
EPOCHS = 10000
DELAY = 1000
LEARNING_RATE = 0.0001
ACCURACY_TRIES = 50
SPEED = 100
RADIUS = 10


def normalize(x, y):
    length = (x * x + y * y) ** 0.5
    if length < 0.0001:
        return (0.0, 0.0)
    return (x / length, y / length)


def random_direction():
    return normalize(random.uniform(-1, 1), random.uniform(-1, 1))


def direction_to(x, y, target_x, target_y):
    return normalize(target_x - x, target_y - y)


def calculate_accuracy(network):
    total = 0.0
    for _ in range(ACCURACY_TRIES):
        x, y = random_direction()
        predicted_x, predicted_y = network.predict([x, y])
        predicted_x, predicted_y = normalize(predicted_x, predicted_y)
        total += (predicted_x * x + predicted_y * y + 1) / 2
    return total / ACCURACY_TRIES * 100


def train_network(network):
    loss_function = MSELoss()
    optimizer = Adam(learning_rate=LEARNING_RATE)
    for epoch in range(EPOCHS):
        x, y = random_direction()
        prediction = network.forward([x, y])
        loss = loss_function.forward(prediction, [x, y])
        gradient = loss_function.backward(prediction, [x, y])
        network.backward(gradient)
        optimizer.step(network.modules)
        if (epoch + 1) % DELAY == 0:
            print(f"Epoch {epoch + 1}/{EPOCHS} Loss: {loss:.5f} Accuracy: {calculate_accuracy(network):.2f}%")


def main():
    pygame.init()
    screen = pygame.display.set_mode((WIDTH, HEIGHT), pygame.RESIZABLE)
    pygame.display.set_caption("PyNet Follow Mouse")
    font = pygame.font.SysFont(None, 28)
    small_font = pygame.font.SysFont(None, 20)
    network = Network(Dense(2, 8, initializer=Xavier()), LeakyReLU(), Dense(8, 8, initializer=Xavier()), LeakyReLU(), Dense(8, 2, initializer=Xavier()), Tanh())
    screen.fill((20, 20, 20))
    text = font.render("Training network...", True, "white")
    screen.blit(text, text.get_rect(center=(WIDTH // 2, HEIGHT // 2)))
    pygame.display.flip()
    train_network(network)
    print(network.summary())
    accuracy = calculate_accuracy(network)
    clock = pygame.time.Clock()
    player_position = [WIDTH / 2, HEIGHT / 2]
    windowed_size = (WIDTH, HEIGHT)
    fullscreen = False
    paused = False
    running = True
    while running:
        delta_time = clock.tick(FPS) / 1000.0
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    paused = not paused
                elif event.key == pygame.K_F11:
                    fullscreen = not fullscreen
                    if fullscreen:
                        screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
                    else:
                        screen = pygame.display.set_mode(windowed_size, pygame.RESIZABLE)
            elif event.type == pygame.VIDEORESIZE and (not fullscreen):
                windowed_size = (event.w, event.h)
                screen = pygame.display.set_mode(windowed_size, pygame.RESIZABLE)
        mouse_position = pygame.mouse.get_pos()
        screen_width, screen_height = screen.get_size()
        if not paused:
            target_direction = direction_to(player_position[0], player_position[1], mouse_position[0], mouse_position[1])
            output_x, output_y = network.predict(list(target_direction))
            move_x, move_y = normalize(output_x, output_y)
            player_position[0] += move_x * SPEED * delta_time
            player_position[1] += move_y * SPEED * delta_time
            player_position[0] = max(RADIUS, min(screen_width - RADIUS, player_position[0]))
            player_position[1] = max(RADIUS, min(screen_height - RADIUS, player_position[1]))
        screen.fill((20, 20, 20))
        pygame.draw.line(screen, (80, 80, 80), (int(player_position[0]), int(player_position[1])), mouse_position, 1)
        pygame.draw.circle(screen, (255, 70, 70), mouse_position, RADIUS)
        pygame.draw.circle(screen, (70, 230, 120), (int(player_position[0]), int(player_position[1])), RADIUS)
        screen.blit(font.render(f"Accuracy: {accuracy:.1f}%", True, "white"), (10, 10))
        screen.blit(small_font.render("Space: Pause", True, (180, 180, 180)), (10, 42))
        status = "PAUSED" if paused else "RUNNING"
        screen.blit(font.render(status, True, "white"), (screen_width - 120, 10))
        pygame.display.flip()
    pygame.quit()


if __name__ == "__main__":
    main()

This example program will train a neural network to follow the mouse pointer.

Note: No comments or blank lines have been provided in example program.

Installation

pip install pynet-ai

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pynet_ai-1.5.1.tar.gz (11.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pynet_ai-1.5.1-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file pynet_ai-1.5.1.tar.gz.

File metadata

  • Download URL: pynet_ai-1.5.1.tar.gz
  • Upload date:
  • Size: 11.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for pynet_ai-1.5.1.tar.gz
Algorithm Hash digest
SHA256 8f8e9151bc9cf5c6c10cceccfa59d0beecd76421225b628298d535cb30ce56d8
MD5 3733eb594595990296cff13fc338e332
BLAKE2b-256 2ecf47b6363fd850188a209043bce9b40e1743db2c16a058a1740275a1a6a6a7

See more details on using hashes here.

File details

Details for the file pynet_ai-1.5.1-py3-none-any.whl.

File metadata

  • Download URL: pynet_ai-1.5.1-py3-none-any.whl
  • Upload date:
  • Size: 9.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for pynet_ai-1.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 cc421fb64fa5be4018b9a1fc7a12fa8f6b66cd50ee2d67979f1bcf71e35118ff
MD5 cb8df075e3a0a1e02edab51fc8762b56
BLAKE2b-256 3daa2645529102bbedd7acc5f90f7edc948718efedb5ed8a7442ac727989e908

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page