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)==shape(w) #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)==shape(w.T) 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.