python - 玩具神经网络还是花哨的波浪线发生器?
问题描述
我编写这段代码是为了更好地理解机器学习,但我不确定我是否走在正确的轨道上。到目前为止,它使用 python 3.7 在屏幕上绘制了随机的波浪线。
import turtle
import random
# Sets the Turtle main screen color
turtle.bgcolor("pink")
# Settings for bug sprite
bug = turtle.Turtle()
bug.penup()
bug.color("red")
bug_x = bug.setx(-150)
bug_y = bug.sety(12)
bug.pendown()
# Settings for food sprite
food = turtle.Turtle()
food.penup()
food.color("green")
food_x = food.setx(160)
food_y = food.sety(59)
food.pendown()
# Main Loop
while True:
# X and Y coordinate of Food
destination = [160,59]
# X and Y coordinate of Bug
x_1 = bug.xcor()
y_1 = bug.ycor()
origin = [x_1,y_1]
learn = .10
bias = 0
# Weights
wghts = [random.uniform(-1,1),random.uniform(-1,1),random.uniform(-1,1),
random.uniform(-1,1),random.uniform(-1,1),random.uniform(-1,1)]
#print(wghts)
# Output Neurons
output_1 = (wghts[0] * origin[0]) + (wghts[1] * origin[1]) + bias
output_2 = (wghts[2] * origin[0]) + (wghts[3] * origin[1]) + bias
output_3 = (wghts[4] * origin[0]) + (wghts[5] * origin[1]) + bias
#Relu Function
if output_1 >= 0.1:
output_1 = output_1
else:
output_1 = 0
if output_2 >= 0.1:
output_2 = output_2
else:
output_2 = 0
if output_3 >= 0.1:
output_3 = output_3
else:
output_3 = 0
# Compares food/destination X and Y with bug/origin X and Y.
# applies update ("learn") to all weights
if origin[0] != destination[0] and origin[1] != destination[1]:
wghts[0] = wghts[0] + learn
wghts[1] = wghts[1] + learn
wghts[2] = wghts[2] + learn
wghts[3] = wghts[3] + learn
wghts[4] = wghts[4] + learn
wghts[5] = wghts[5] + learn
else:
wghts[0] = wghts[0]
wghts[1] = wghts[1]
wghts[2] = wghts[2]
wghts[3] = wghts[3]
wghts[4] = wghts[4]
wghts[5] = wghts[5]
#print(wghts)
#print("\n")
# Creates a barrier for turtle
bug_1a = int(bug.xcor())
bug_2a = int(bug.ycor())
if bug_1a > 300 or bug_2a > 300:
bug.penup()
bug.setx(5)
bug.sety(5)
bug.pendown()
if bug_1a < -300 or bug_2a < -300:
bug.penup()
bug.setx(5)
bug.sety(5)
bug.pendown()
# Output values applied to turtle direction controls
bug.forward(output_1)
bug.right(output_2)
bug.left(output_3)
解决方案
我在您的程序中看到的问题:
从前一次迭代中wghts
什么都学不到——它们每次通过循环都被随机重置。
output_1
,output_2
和output_3
是根据新重新初始化的值计算得出的,因此wghts
所做的更改如下:
if origin[0] != destination[0] and origin[1] != destination[1]:
wghts[0] = wghts[0] + learn
...
wghts[5] = wghts[5] + learn
永远不会反映在output_*
变量中。
您正在添加错误的 X 和 Y 坐标,并将其用作转动的度数。两次。我不明白这有什么意义,但我想这是一个神经网络的东西。
你在代码中做障碍检查太晚了,以至于它与后面的内容不同步。错误没有移动,因此请早点进行检查。
以下代码清理不会使您的错误变得不那么随机——只是希望使您的代码更易于使用:
from turtle import Screen, Turtle
from random import uniform
# Sets the Turtle main screen color
screen = Screen()
screen.bgcolor("pink")
# X and Y coordinate of Food
destination = (160, 59)
# Settings for food sprite
food = Turtle()
food.color("green")
food.penup()
food.setposition(destination)
food.pendown()
start = (-150, 12)
# Settings for bug sprite
bug = Turtle()
bug.color("red")
bug.penup()
bug.setposition(start)
bug.pendown()
LEARN = 0.1
BIAS = 0
# Main Loop
while True:
# X and Y coordinate of Bug
x, y = bug.position()
# Creates a barrier for turtle
if not -300 <= x <= 300 or not -300 <= y <= 300:
bug.penup()
bug.goto(start)
bug.pendown()
origin = start
else:
origin = (x, y)
# Weights
wghts = [uniform(-1, 1), uniform(-1, 1), uniform(-1, 1), uniform(-1, 1), uniform(-1, 1), uniform(-1, 1)]
# Compares food/destination X and Y with bug/origin X and Y.
# applies update ("LEARN") to all weights
if origin != destination:
wghts[0] += LEARN
wghts[1] += LEARN
wghts[2] += LEARN
wghts[3] += LEARN
wghts[4] += LEARN
wghts[5] += LEARN
# Output Neurons
output_1 = (wghts[0] * origin[0]) + (wghts[1] * origin[1]) + BIAS
output_2 = (wghts[2] * origin[0]) + (wghts[3] * origin[1]) + BIAS
output_3 = (wghts[4] * origin[0]) + (wghts[5] * origin[1]) + BIAS
# Relu Function
if output_1 < 0.1:
output_1 = 0
if output_2 < 0.1:
output_2 = 0
if output_3 < 0.1:
output_3 = 0
# Output values applied to turtle direction controls
bug.forward(output_1)
bug.right(output_2)
bug.left(output_3)
推荐阅读
- tomcat - 如何配置windows tomcat8.5.71不保存日志如“”-“400-”
- jasmine - 如何监视元素两次以覆盖 karma -jasmine 中的代码
- python - Python Selenium:从三个循环更改为一个循环重复相同的信息
- reactjs - 打字稿。如果 props 对未定义的属性发誓怎么办?反应
- c# - 通过几个 nuget 包共享自定义异常
- nginx - 启动时在 Nginx 上设置多个上游并动态编辑它们
- c# - 如何在 Visual Studio 2019 中引用程序集?.NET Core 上的 WPF
- nginx - nginx - 重定向到不带扩展名的 url 并在末尾添加斜杠
- java - 无法单击网络警报中的“确定”按钮
- python - 如何在视图函数的异常中更新模型并使事务提交到数据库?