from package.cudamat import cudamat as cm import numpy as np # Ensuring that Cudamat is working. cm.cublas_init() # create two random matrices and copy them to the GPU a = cm.CUDAMatrix(np.random.rand(32, 256)) b = cm.CUDAMatrix(np.random.rand(256, 32)) # perform calculations on the GPU c = cm.dot(a, b) d = c.sum(axis = 0) # copy d back to the host (CPU) and print print( d.asarray() )
print ( "Epoch " + str(epoch + 1) ) err = [] for batch in range(num_batches): # get current minibatch inp = dev_train.slice(batch*batch_size,(batch + 1)*batch_size) target = dev_lbl.slice(batch*batch_size,(batch + 1)*batch_size) # apply momentum wu_w1.mult(momentum) wu_b1.mult(momentum) wu_w2.mult(momentum) wu_b2.mult(momentum) # forward pass cm.dot(w_w1.T, inp, target = h) h.add_col_vec(w_b1) h.apply_sigmoid() cm.dot(w_w2.T, h, target = out) out.add_col_vec(w_b2) out.apply_sigmoid() # back prop errors out.subtract(target) # compute error # gradients for w_w2 and w_b2 wu_w2.add_dot(h, out.T) wu_b2.add_sums(out, axis = 1)
for epoch in range(num_epochs): print ( "Epoch " + str(epoch + 1) ) err = [] for batch in range(num_batches): # get current minibatch v_true = dev_dat.slice(batch*batch_size,(batch + 1)*batch_size) v.assign(v_true) # apply momentum wu_vh.mult(momentum) wu_v.mult(momentum) wu_h.mult(momentum) # positive phase cm.dot(w_vh.T, v, target = h) h.add_col_vec(w_h) h.apply_sigmoid() wu_vh.add_dot(v, h.T) wu_v.add_sums(v, axis = 1) wu_h.add_sums(h, axis = 1) # sample hiddens r.fill_with_rand() r.less_than(h, target = h) # negative phase cm.dot(w_vh, h, target = v) v.add_col_vec(w_v) v.apply_sigmoid()