A Python neural network library for simple networks and educational use.
Project description
PyNet 1.6.0
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
- Install PyNet with
pip install pynet-ai - Install PyGame with
pip install pygame(for the example) - 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pynet_ai-1.6.0.tar.gz.
File metadata
- Download URL: pynet_ai-1.6.0.tar.gz
- Upload date:
- Size: 11.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0de1ee217c49c93dc4c0c06d616f3c201b12d650f76ff7afbc8554af9d4f0645
|
|
| MD5 |
4ed36a2983607dede0eedc849daecd53
|
|
| BLAKE2b-256 |
98072d2f6399ddb302affbb4301caa17821b70d42b626417e289331fb9cbccc1
|
File details
Details for the file pynet_ai-1.6.0-py3-none-any.whl.
File metadata
- Download URL: pynet_ai-1.6.0-py3-none-any.whl
- Upload date:
- Size: 10.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8c0889e43edb716937e685113f758bc2084a40ad17061a9cdca07e821bed6900
|
|
| MD5 |
070eceddf22e8039aa44bfc7287fadae
|
|
| BLAKE2b-256 |
623ffb2864fe69cee7b30efb48039bec2dc0776806d4f208f7a9b0a1c07a5990
|