==Current Codes==
=Implementation=
Backpropagation neuron chip
Houses code for 1 neuron, to be networked and such in a physical non-loop model. Presently only functions properly in about 1 in 6 runs due to unknown code glitches, which also seem to hinder networking. Code:
@name BProp neuron
@inputs [In1 In2 In3 In4 In5 In6 In7 In8 In9 In10] End
@inputs [Ou1 Ou2 Ou3 Ou4 Ou5 Ou6 Ou7 Ou8 Ou9 Ou10]
@persist Weights:array N ID It NetOut Learn
@outputs OUT ID
if(first()){
#Learning rate
Learn=0.25
#T is the target, a string (to allow learning towards zero)
ID=entity():id()
#Initial randomised weights
Weights=array()
for(V=1,10){
Weights:pushNumber(round(random(-1,1),2))
}
#Indication holograms
B=entity():toWorld(vec(0,0,5))
A=entity():angles()
holoCreate(1,B,vec(),A) holoCreate(2,B,vec(),A)
holoModel(1,"pyramid") holoModel(2,"pyramid")
holoScaleUnits(1,vec(1,1,1)) holoScaleUnits(2,vec(5,5,5))
holoParent(1,entity()) holoParent(2,entity())
holoAlpha(2,170)
}
interval(150)
#Input data (previous layer) and forward data IDs (next layer)
Input=array() IDs=array()
Input:pushNumber(gGetNum(In1)) IDs:pushNumber(Ou1)
Input:pushNumber(gGetNum(In2)) IDs:pushNumber(Ou2)
Input:pushNumber(gGetNum(In3)) IDs:pushNumber(Ou3)
Input:pushNumber(gGetNum(In4)) IDs:pushNumber(Ou4)
Input:pushNumber(gGetNum(In5)) IDs:pushNumber(Ou5)
Input:pushNumber(gGetNum(In6)) IDs:pushNumber(Ou6)
Input:pushNumber(gGetNum(In7)) IDs:pushNumber(Ou7)
Input:pushNumber(gGetNum(In8)) IDs:pushNumber(Ou8)
Input:pushNumber(gGetNum(In9)) IDs:pushNumber(Ou9)
Input:pushNumber(gGetNum(In10)) IDs:pushNumber(Ou10)
#For ease of reference and collection, stored in arrays
#Output production
O=0
for(V=1,Input:count()){
O+=Input[V,number]*Weights[V,number]
}
OUT=1/1-exp(-O)
gSetNum(ID,OUT)
#Network output definer (Wire 1 into End if multiple chips)
if(!IDs:sum()|End){gSetNum("NetOut",OUT)}
#Backpropagation
NetOut=gGetNum("NetOut") #Network output
N=gGetStr("Target"):toNumber() #Target output
Error=N-NetOut #Network error
if($N & Error){
#Forward alteration
if(IDs:sum()){
Fo=0
for(V=1,IDs:count()){
Fo+=gGetNum(IDs[V,number])
}
For=Fo/Input:count()
Mod=Learn*Error*OUT*For*(1-For)
}
else{
Mod=Learn*Error*OUT
}
#Weight modification
for(V=1,Input:count()){
W=Weights[V,number]+Mod*Input[V,number]
Weights[V,number]=round(W,4)
}
S=round((1-Error)/(!N ? 1 : N),2)*5
holoScaleUnits(1,vec(S,S,S))
if(S>5|S<0){holoColor(2,vec(1,0,0)*255)}
else{holoColor(2,vec(1,1,1)*255)}
}
#Rebooting simultaneous with nearby training chip
runOnSignal("Start",1,1)
Dis=signalSender():pos():distance(entity():pos())
if(signalClk("Start") & Dis<=100){
reset()
} Code:
@name BProp Learner
@outputs A B
@persist N Err AE BE
if(first()){
N=1
gDeleteAll()
A=entity():id()
B=A*10
holoCreate(1,entity():toWorld(vec(0,0,10)))
holoCreate(2,entity():toWorld(vec(0,0,10)))
holoScaleUnits(1,vec(3,3,10))
holoScaleUnits(2,vec(3,3,1))
holoAlpha(1,170)
signalSend("Start",1)
}
interval(150)
if(N){
BE=(gGetStr("Target"):toNumber()-gGetNum("NetOut"))^2
gSetNum(A,0)
gSetNum(B,1)
gSetStr("Target","0")
}
else{
AE=(gGetStr("Target"):toNumber()-gGetNum("NetOut"))^2
gSetNum(A,1)
gSetNum(B,1)
gSetStr("Target","1")
}
N=!N
if(AE & BE){
Err=round(AE+BE,2)
Cap=min((1-Err),1)
Cap=max(Cap,0)*10
holoScaleUnits(2,vec(3,3,Cap))
} Forwardpropagation neuron chip
Houses code for 1 neuron with forward propagation. Works well in singular and networked states on a ground-level physical model. Code:
@name Neuron
@inputs V1 V2 Target Reset
@persist W1 W2 Learn It Train:entity
@persist IN1 IN2 N Wire
@outputs OUT
if(first()){
Wire=0
#If null, globals from the Train chip
#otherwise, from the input wires
#Learning rate
Learn=0.25
#T is the target, a string (to allow learning towards zero)
#Initial randomised weights
W1=round(random(-1,1),2)
W2=round(random(-1,1),2)
print("Weight 1: "+W1)
print("Weight 2: "+W2)
#Indication holograms
B=entity():toWorld(vec(0,0,5))
holoCreate(1,B) holoCreate(2,B)
holoModel(1,"pyramid") holoModel(2,"pyramid")
holoScaleUnits(1,vec(1,1,1)) holoScaleUnits(2,vec(5,5,5))
holoParent(1,entity()) holoParent(2,entity())
holoAlpha(2,170)
if(!Wire){
holoCreate(3,entity():toWorld(vec(0,0,1)))
holoParent(3,entity()) holoModel(3,"hqcone")
holoScaleUnits(3,vec(3,3,7))
Train=owner()
}
}
interval(150)
#Training data
if(!Wire){
T=gGetStr(Train:id()*3+3):explode("/")
IN1=T[1,string]:toNumber()
IN2=T[2,string]:toNumber()
N=T[3,string]:toNumber() #Target output number
}
else{
IN1=V1
IN2=V2
N=Target
}
#Output production
O=(IN1*W1)+(IN2*W2)
OUT=1/1-exp(-O)
#Forward propagation
if($N){
Error=N-OUT
W1+=(Learn*IN1*Error) #Modify weight 1
W2+=(Learn*IN2*Error) #Modify weight 2
#Scale indication hologram
S=((1-Error)/(!N ? 1 : N))*5
holoScaleUnits(1,vec(S,S,S))
if(Error){
holoColor(1,vec(1,1,1)*255)
It=1
}
else{
holoColor(1,vec(0,1,0)*255)
if(It){
holoEntity(1):soundPlay(1,0,"buttons/blip1.wav")
soundPitch(1,150)
It=0
}
}
if(N){gSetNum(Train:id()*3+1,Error)}
else{gSetNum(Train:id()*3+2,Error)}
}
#Rebooting simultaneous with nearby training chip
runOnSignal("Start",1,1)
Dis=signalSender():pos():distance(entity():pos())
if(signalClk("Start") & Dis<=100 & !Wire|Wire & Reset & $Reset){
W1=round(random(-1,1),2)
W2=round(random(-1,1),2)
Train=signalSender()
}
V=Train:pos()-holoEntity(3):pos()
holoAng(3,V:toAngle()+ang(90,0,0)) Code:
@persist T:string IN1 IN2
@persist A Net Stop
if(first()){
A=1
#Net error computation
gDeleteAll()
signalSend("Start",1)
Net=100
#Indication holograms
B=entity():toWorld(vec(0,0,5))
holoCreate(1,B) holoCreate(2,B)
holoModel(1,"hqicosphere2") holoModel(2,"hqicosphere2")
holoParent(1,entity()) holoParent(2,entity())
holoScaleUnits(1,vec(1,1,1)) holoScaleUnits(2,vec(3,3,3))
holoAlpha(2,170)
}
interval(150)
if(A){
IN1=1
IN2=1
T="1"
A=0
}
else{
IN1=0
IN2=1
T="0"
A=1
}
gSetStr(entity():id()*3+3,IN1+"/"+IN2+"/"+T)
if(A & $A){
#Net error
Net=gGetNum(entity():id()*3+1)^2
Net+=gGetNum(entity():id()*3+2)^2
if(round($Net,2)){print("Net error: "+round(Net,2))}
S=(1-Net)*3
holoScaleUnits(1,vec(S,S,S))
} =Testing=
Holo-drone for testing Neural Network concepts[highlight=E2]@name gBrain neural net test drone
@inputs R L F B
@outputs Ins:array
@persist H:entity [Dow Rv Bv]:vector
if(first()){
S=rangerOffset(6000,entity()
os(),vec(0,0,-1))
osition()
holoCreate(1,S+vec(0,0,5),vec(1,1,1),ang())
H=holoEntity(1)
runOnTick(1)
Hcen=H:toWorld(H:boxCenter())
holoCreate(2,Hcen-vec(0,7,0),vec(1,1,1)/4,ang(90,-90,0))
holoCreate(3,Hcen+vec(0,7,0),vec(1,1,1)/4,ang(90,90,0))
holoCreate(4,Hcen+vec(7,0,0),vec(1,1,1)/4,ang(90,0,0))
holoCreate(5,Hcen-vec(7,0,0),vec(1,1,1)/4,ang(-90,0,0))
holoCreate(6,Hcen+vec(6,0,0),vec(2,2,3)/4,ang(90,0,0))
holoCreate(7,vec(),vec(1,1,1)/8,ang())
holoParent(2,1) holoParent(3,1) holoParent(4,1)
holoParent(5,1) holoParent(6,1)
Co="hqcone"
holoModel(2,Co) holoModel(3,Co) holoModel(4,Co)
holoModel(5,Co) holoModel(6,"hqtorus2") holoModel(7,"hqicosphere2")
Rv=vec(1,0,0)*255
Bv=-vec(1,1,1)*255
Dow=vec(0,0,1)
}
##Operation##
#Sensory perception
Hcen=H:toWorld(H:boxCenter())
Forw=vec(1,0,0):rotate(ang(0,H:angles():yaw(),0))
Righ=vec(0,1,0):rotate(ang(0,H:angles():yaw(),0))
Fo=rangerOffset(500,Hcen,Forw)
Ba=rangerOffset(500,Hcen,-Forw)
Ri=rangerOffset(500,Hcen,Righ)
Le=rangerOffset(500,Hcen,-Righ)
rangerHitWater(1)
Do=Forw-Dow/3
Wa=rangerOffset(500,Hcen,Do)
Wat=Wa:distance()
holoPos(7,Hcen+Do*Wa:distance()+2)
Wet=holoEntity(7):isUnderWater()
Fod=Fo:distance()
Bad=Ba:distance()
Rid=Ri:distance()
Led=Le:distance()
holoColor(2,(Led/500)*(Led<=15 ? Rv : Bv))
holoColor(3,(Rid/500)*(Rid<=15 ? Rv : Bv))
holoColor(4,(Fod/500)*(Fod<=15 ? Rv : Bv))
holoColor(5,(Bad/500)*(Bad<=15 ? Rv : Bv))
#Sensory outputs
Ins[1,number]=Fod #Forward distance
Ins[2,number]=Bad #Reverse distance
Ins[3,number]=Rid #Right distance
Ins[4,number]=Led #Left distance
Ins[5,number]=Wet #Whether Wa ranger is hitting water
Ins[6,number]=Wat #Forward distance to water
#Good/Bad responses
A=F & Fod<=15|F & Wet & Wat
#Going forward with little room
#Similarly, going forward towards nearby water
B=B & Bad<=15
#Reversing with little room
C=H:isUnderWater()
#If the drone has fallen into the water
D=B & Fod<=15|B & Wet & Wat
#Moving away from water and cramped spaces
E=F & Bad<=15
#Moving away from backing walls/objects
if(A|B|C){ #Bad
Out=-1
if(C){reset()} #Drone "dies" in water
}
elseif(A|B){ #Good
Out=1
}
else{Out=0} #Neutral response
#Functional motion
An=H:angles()+ang(0,R-L,0)
holoAng(1,An)
Do=rangerOffset(500,Hcen,vec(0,0,-1))
osition()+vec(0,0,5)
Do+=Forw*(F-B)
holoPos(1,Do)[/highlight]
Bookmarks