# pseudo-code template for boltzman machine

from numpy import *

# import data
data = genfromtxt(blah)
digits = data[:,1]
X = ones(shape)

#initialise variables
hidden = 625 # 25**2			# make it square so can plot filters in a square
visible = blah + 10			# visible nodes are input values + ten for integers 0, .., 9
DELTA_W = ones([10*visible, 10*hidden])	# coefficients for bonds between hidden and visible nodes
a = ones([visible, 10])	# coefficients for visible nodes
b = ones([hidden, 10])	# coefficients for hidden nodes
alpha = 2 		# learning rate
time = 0.0
epochs = 10
N = len(digits)

while time < N*epochs:

	row = get_random_row
	digit = digits[row]
	local_W = DELTA_W[digit*visible:(digit+1)*visible, digit*hidden: (digit+1)*hidden]
	use p(h|row) to randomly select h
	positive = kron(row, h)
	use p(v|h) to randomly select a v'
	use p(h'|v') to randomly select a h'
	negative = kron(v',h')
	localW += alpha*(positive - negative)
	DELTA_W[digit*visible:(digit+1)*visible, digit*hidden: (digit+1)*hidden] = local_W

	time +=1
	print 'time: ', (t/(N*epochs))*100
	

@@ -1,142 +1,255 @@
 # 
-pseudo-code template for boltzman machine%0A%0Afrom numpy import *%0A%0A# import data%0Adata = genfromtxt(blah)%0Adigits = data%5B:,1%5D%0AX = ones(shape)
+learn paramters for restricted boltzman machine%0A# rupert small - august 2015%0A#%0A%0Afrom numpy import *%0A%0A# import data%0Adata = genfromtxt('test2.csv',delimiter=',')%0Adigits = data%5B:,0%5D%0Adim = shape(data%5B:,1:%5D)%0AX = ones(dim)*(data%5B:,1:%5D %3E 0)%0Apixels = dim%5B1%5D
 %0A%0A#i
@@ -354,12 +354,14 @@
 e = 
-blah
+pixels
  + 1
@@ -738,22 +738,33 @@
 w = 
-get_
 random
-_row
+.randint(0, dim%5B0%5D)
 %0A%09di
@@ -786,25 +786,16 @@
 w%5D%0A%09
-local_W = DELTA_W
+index = 
 %5Bdig
@@ -808,94 +808,147 @@
 ible
-:(
+, 
 digit
-+1)*visible, digit*hidden: (digit+1)*hidden%5D%0A%09use p(h%7Crow) to randomly select h
+*hidden%5D%0A%09local_W = DELTA_W%5Bindex%5B0%5D:index%5B0%5D+visible, index%5B1%5D: index%5B1%5D+hidden%5D%0A%09v = X%5Brow,:%5D%0A%09h = prob_h_with_v(v, local_W, a, b)
 %0A%09po
@@ -965,89 +965,90 @@
 ron(
-row
+v
 , h)%0A%09
-use p(v%7Ch) to randomly select a v'%0A%09use p(h'%7Cv') to randomly select a h'
+v2 = prob_v_with_h(h, local_W, a, b)%0A%09h2 = prob_h_with_v(v2, local_W, a, b)
 %0A%09ne
@@ -1066,20 +1066,20 @@
 on(v
-',h'
+2,h2
 )%0A%09local
 W +=
@@ -1074,16 +1074,17 @@
 )%0A%09local
+_
 W += alp
@@ -1121,65 +1121,54 @@
 A_W%5B
-digit*visible:(digit+1)*visible, digit*hidden: (digit+1)*
+index%5B0%5D:index%5B0%5D+visible, index%5B1%5D: index%5B1%5D+
 hidd
@@ -1190,16 +1190,17 @@
 %09time +=
+ 
 1%0A%09print
@@ -1231,8 +1231,366 @@
 )*100%0A%09%0A
+%0Adef prob_v_with_h(h, W, a):%0A%09la = len(a)%0A%09probs = %5B1/(1. + exp(a%5Bi%5D + dot(W%5Bi,:%5D,h))) for i in range(0, la)%5D%0A%09n = %5Brandom.binomial(1, probs%5Bi%5D) for i in range(0,la)%5D%0A%09return n%0A%0Adef prob_h_with_v(v, W, b):%0A%09lb = len(b)%0A%09probs = %5B1/(1. + exp(b%5Bi%5D + dot(v,W%5B:,i%5D))) for i in range(0, len(b))%5D%0A%09n = %5Brandom.binomial(1, probs%5Bi%5D) for i in range(0,lb)%5D%0A%09return n%0A

Update boltzman_machine_opt.py