def nbody_wrapper(): galaxy = nbody.random_galaxy(NUM_BODIES) nbody.simulate(galaxy, TIMESTEPS) r = spartan.sum(galaxy['x'] + galaxy['y'] + galaxy['z']) r.optimized() print 'r = ', r.glom()
def run(self): (train_data, test_data) = load_mb_from_mat(self.data_file, self.mb_size) #num_test_samples = test_data[0].shape[0] #(test_samples, test_labels) = test_data count = 1 begin = time.time() for epoch in range(self.num_epochs): print '---Start epoch #%d' % epoch # train iterations = len(train_data) for (mb_samples, mb_labels) in train_data: num_samples = mb_samples.shape[0] a1 = mb_samples.T target = mb_labels.T # ff #a2 = relu(np.dot(self.w1, a1) + self.b1) #a3 = np.dot(self.w2, a2) + self.b2 a2 = relu(spartan.dot(self.w1, a1) + self.b1) a3 = spartan.dot(self.w2, a2) + self.b2 # softmax & error out = softmax(a3) s3 = out - target # bp #s2 = np.dot(self.w2.T, s3) s2 = spartan.dot(self.w2.T, s3) s2 = relu_back(s2, a2) # grad #gw1 = np.dot(s2, a1.T) / num_samples #gb1 = np.sum(s2, axis=1, keepdims=True) / num_samples #gw2 = np.dot(s3, a2.T) / num_samples #gb2 = np.sum(s3, axis=1, keepdims=True) / num_samples gw1 = spartan.dot(s2, a1.T) / num_samples gb1 = (spartan.sum(s2, axis=1) / num_samples).reshape(s2.shape[0], 1) gw2 = spartan.dot(s3, a2.T) / num_samples gb2 = (spartan.sum(s3, axis=1) / num_samples).reshape(s3.shape[0], 1) # update self.w1 -= self.eps_w * gw1 self.w2 -= self.eps_w * gw2 self.b1 -= self.eps_b * gb1 self.b2 -= self.eps_b * gb2 iterations -= 1 out.evaluate() self.w1.evaluate() self.w2.evaluate() self.b1.evaluate() self.b2.evaluate() #if count % 40 == 0 or iterations == 0: if count % 40 == 0: #correct = np.argmax(out, axis=0) - np.argmax(target, axis=0) #print 'Training error:', float(np.count_nonzero(correct)) / num_samples correct = spartan.argmax(out, axis=0) - spartan.argmax(target, axis=0) print 'Training error:', (float(spartan.count_nonzero(correct).glom()) / num_samples) count = count + 1 print 'spent ', (time.time() - begin) # test #a1 = test_samples.T #a2 = relu(np.dot(self.w1, a1) + self.b1) #a3 = np.dot(self.w2, a2) + self.b2 #correct = np.argmax(a3, axis=0) - np.argmax(test_labels.T, axis=0) a1 = test_samples.T a2 = relu(spartan.dot(self.w1, a1) + self.b1) a3 = spartan.dot(self.w2, a2) + self.b2 correct = spartan.argmax(a3, axis=0) - spartan.argmax(test_labels.T, axis=0)