
procedure learn3layer layer in conn inhid layer hid conn hidout layer out
    call activate3layer(in,inhid,hid,hidout,out)
    backward hidout
    delta hid
    learn inhid
    learn hidout
    learnbias out
    learnbias hid
end
procedure activate3layer layer in conn inhid layer hid conn hidout layer out
    input in
    forward inhid
    activation hid
    forward hidout
    activation out
    target out
end

procedure resize3layer l in ex nin l hid ex nhid l out ex nout
    resize in nin
    resize out nout
    resize hid nhid connect
end
		# brain-state-in-a-box updating
procedure activationBSB layer L expr mu
    L = bound01(L:net*mu+L)
end
		# boltzmann machine updating
procedure activationBoltz layer L expr temperature 
    L = randomstate(logistic(L:net/temperature))
end
		# Mean-Field-Theory updating
procedure activationMFT  layer L expr temperature
    L = logistic(L:net/temperature)
end

# annealInteractive(update,schedule,repeat,step)
# Simulated Annealing.
# 
	# arguments
	# procedure update: 	procedure for updating.
	# expression schedule: 	annealing schedule - a vector of temperatures
	# expression repeat:	no of updates at each temperature -single value
	# expression step:	is assigned step no. in schedule
procedure anneal  procedure update ex schedule ex repeat ex step
    step = -1
    repeat sizeof(schedule)
	step +=1
	repeat repeat
	    call update(schedule[step])
	endrepeat
	if schedule[step] <= 0 endrepeat
    endrepeat
end
# annealInteractive(update,schedule,repeat,step)
# Interactive Simulated Annealing. Same arguments as 'anneal'
# 
procedure annealInteractive  procedure update ex schedule ex repeat ex step
    step = -1
    printf click\sleft\sbutton\sto\supdate.\sright\sbutton\sto\sstop\n
    repeat sizeof(schedule)
	step +=1
	repeat repeat
	    call update(schedule[step])
	endrepeat
	print
	display
	clist do
	if $mouseClick[2]==3 do
	    printf terminated\sat\sstep=%.0f step
	    here endrepeat
	endif
	if schedule[step] <= 0 endrepeat
    endrepeat
end
		# repeat a procedure interactively
procedure repeatInteractive procedure proc vector nrepeat
    printf click\sleft\sbutton\sto\scontinue.\smiddle\sbutton\sto\sturn\soff\sinteractive.\sright\sbutton\sto\sstop\n
    float wait; wait=1; float button
    repeat nrepeat 
	call proc
	print
	display
	clist do
	if wait do
	    if (button=$mouseClick[2])==3 do
		printf terminated
		here endrepeat
	    endif
	    if button==2
		wait=0
	endif
    endrepeat
end

procedure savePat ex pat ex npat ex new
    float n; n=0; new=0
    repeat npat
	if pat==xall[n] end
	n += 1
    endrepeat
    xall[npat] = pat
    npat += 1
    new=1
end

procedure printPatternValue ex val
    if val>$max || val<$min then
	printf %g:\svalue\sout\sof\srange
        here end
    endif
    float pat; 
    pat = (val-$min)/($max-$min)
    if val==$max then
	printf a
    else
        printf 
    endif
end
