I have issues with dynamically typed languages, and I tend to worry about type a lot.
Numpy has different behaviour depending on if something is a matrix or a plain ndarray, or a list. I didn't originally have all these asserts, but I inserted them while trying to debug a type error.
from numpy import * sigmoid = vectorize(lambda(x): 1.0/(1.0+exp(-x))) def prob_hOn_givenV (v,b,w): assert isinstance(v,matrix) assert isinstance(w,matrix) assert isinstance(b,matrix) assert shape(v)[1]==shape(w)[0] #print("|v|="+str(shape(v)) +"|w|="+str(shape(w)) ) return sigmoid(b+v*w) #sum up rows (v is a column vector) def prob_vOn_givenH (h,a,w): assert isinstance(h,matrix) assert isinstance(a,matrix) assert isinstance(w,matrix) assert shape(h)[1]==shape(w.T)[0] return sigmoid(a+h*w.T) #sum up columns. (h is a row vector) Is there a better way? Perhaps a defensive programming toolkit? There is also duplication in the code, but I can't see a nice way of removing it while maintaining meaning. People familiar with Restricted Boltzman Machines will recognise the conditional formula used for Gibbs sampling for contrastive divergence.
from numpy import *-- don't do this. This will replace some common Python builtins, likeanyandall, withnumpyversions which can silently return exactly the opposite values. \$\endgroup\$