-
Notifications
You must be signed in to change notification settings - Fork 0
/
chapter05project.py
68 lines (54 loc) · 2.13 KB
/
chapter05project.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import numpy as np
import pygame
def neural_network(input, weights):
return np.clip(input.dot(weights), -1, 1)
weights = np.array([0.5, 0.5, 0.5])
data_points = []
def train():
for point in data_points:
input = np.array([point[0], point[1], 1])
pred = neural_network(input, weights)
delta = pred - point[2]
for i in range(len(weights)):
weight_delta = delta * input[i] * 0.1
weights[i] -= weight_delta
# Инициализация Pygame
pygame.init()
# Размеры окна и массива фона
window_size = (512, 512)
background_size = (64, 64)
# Создание окна
screen = pygame.display.set_mode(window_size)
pygame.display.set_caption("Chapter 5 Project")
# Основной цикл программы
running = True
while running:
train()
print(f"Weights: {weights}")
# Отрисовка фона
for y in range(background_size[1]):
for x in range(background_size[0]):
pred = neural_network(np.array([x/background_size[0], y/background_size[1], 1]), weights)
if pred > 0:
color = [0, 0, np.clip(pred*255, 0, 255)]
else:
color = [np.clip(-pred*255, 0, 255), np.clip(-pred*127, 0, 255), 0]
pygame.draw.rect(screen, color, (x * 8, y * 8, 8, 8))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button in (1, 3):
x, y = event.pos
if 0 <= x < window_size[0] and 0 <= y < window_size[1]:
if event.button == 1:
data_points.append([x/512, y/512, -1])
elif event.button == 3:
data_points.append([x/512, y/512, 1])
for point in data_points:
# Создание кружка
pygame.draw.circle(screen, (204, 85, 0) if point[2] == -1 else (0, 0, 102), (point[0]*512, point[1]*512), 16)
# Обновление экрана
pygame.display.flip()
# Завершение работы Pygame
pygame.quit()