-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtsLinearRegression.py
More file actions
85 lines (66 loc) · 2.44 KB
/
Copy pathtsLinearRegression.py
File metadata and controls
85 lines (66 loc) · 2.44 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import pygame
import sys
from pygame.locals import *
import tensorflow as tf
import random as rd
window = pygame.display.set_mode((500, 500), 0, 32)
pygame.display.set_caption('Linear Regression')
_with, _height = window.get_size()
# Set up the colors
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
WHITE = (255, 255, 255)
def translate(n, start1, stop1, start2, stop2):
return ((n-start1)/(stop1-start1))*(stop2-start2)+start2
def draw_circle(start, radius, _color):
pygame.draw.circle(window, _color, start, radius)
def draw_line(_start, _end, _stroke, _color):
pygame.draw.line(window, _color, _start, _end, _stroke)
m = tf.Variable(rd.random(), dtype='float32')
b = tf.Variable(rd.random(), dtype='float32')
x_tens = tf.placeholder(dtype='float32')
y_tens = tf.placeholder(dtype='float32')
linear_model = m*x_tens + b
predict = m*(tf.constant([-1, 1], dtype='float32'))+b
square_error = tf.square(linear_model-y_tens)
loss = tf.reduce_sum(square_error)
optimizer_function = tf.train.GradientDescentOptimizer(0.05)
train = optimizer_function.minimize(loss)
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
# Main Draw Loop
points_x = []
points_y = []
line = [-1, 1]
while True:
for event in pygame.event.get():
if event.type == QUIT:
sess.close()
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONUP:
pos = pygame.mouse.get_pos()
maped_x = translate(pos[0], 0, _with, -1, 1)
maped_y = translate(pos[1], 0, _height, 1, -1)
points_x.append(maped_x)
points_y.append(maped_y)
for i in range(550):
sess.run(train, {x_tens: points_x, y_tens: points_y})
line = sess.run(predict)
print(sess.run(loss, {x_tens: points_x, y_tens: points_y}))
for i in range(len(points_x)):
maped_x = translate(points_x[i], -1, 1, 0, _with)
maped_y = translate(points_y[i], -1, 1, _height, 0)
draw_circle((int(maped_x), int(maped_y)), 4, WHITE)
line_y1 = translate(line[0], -1, 1, _height, 0)
line_y2 = translate(line[1], -1, 1, _height, 0)
line_x1 = translate(-1, -1, 1, 0, _with)
line_x2 = translate(1, -1, 1, 0, _with)
if line_y1 < _height or line_y2 < _height:
draw_line((line_x1, line_y1), (line_x2, line_y2), 3, GREEN)
pygame.display.update()
pygame.time.wait(40)
window.fill(BLACK)