def test_jacobian_category_3(self): image_data = [0.2, 0.5, 0.3] categories = [1, 2, 4] model = Model(layers=[ Image(image_data=image_data, shape=(3, 1), maximum=1), Category(categories), ]) model.compile(build=True) v = model.value() self.assertEqual(v, [max(image_data)]) p = model.predict() self.assertEqual(p, [categories[int(np.argmax(np.array(image_data)))]]) pass
def test_jacobian_dense_31_category_3(self): image_data = [0.2, 0.5, 0.3] categories = [1, 2, 4] model = Model(layers=[ Image(image_data=image_data, shape=(3, 1), maximum=1), Dense(reLU(), (3, 1)), Category(categories), ]) self._run_derivatives_test(model) v = model.value() # compute value manually for this image_data: manual_dense_layer = [] for i in range(3): weights = model._layers[1]._nodes[i]._weights manual_dense_layer.append( sum([w.weight() * d for w, d in zip(weights, image_data)])) max_value = max(manual_dense_layer) imax_value = int(np.argmax(np.array(manual_dense_layer))) self.assertEqual(v, [max_value]) p = model.predict() self.assertEqual(p, [categories[imax_value]]) pass
from j2learn.etc.tools import finite_difference mndata = MNISTData(path='../mnist') images, labels = mndata.training() r = 49 # a nice number three image_layer = Image(image_data=images[r], label=labels[r]) cnn = CNN(reLU(), (3, 3), (0, 0)) dense = Dense(reLU(), (10, 1)) category = Category([i for i in range(10)]) model = Model(layers=[image_layer, cnn, dense, category]) model.compile(build=True) cat = model.predict() print(cat) prob = model.probability() print(prob) n_weights = model.weight_count() gradient = 0 i = 0 while gradient == 0 and i < n_weights: gradient = finite_difference(model, i, epsilon=0.01) i += 1 print(gradient) weights = model.weights() print(weights)
model.compile(build=True) # ## Prepare images for training mndata = MNISTData(path='../test/mnist') images, labels = mndata.training() train_images = [] train_labels = [] i = 0 train_n = len(images) while i < len(images) and len(train_images) < train_n: train_images.append(reduce_image(images[i]) if reduce else images[i]) train_labels.append(labels[i]) i += 1 # ## Stochastic Gradient Descent gd = GradientDescent(model=model, learning_rate=0.1, labels=predict_labels) gd.sgd(train_images, train_labels, iterations=20000) # ## Test the model ### test_images, test_labels = mndata.testing() predictions = [] for i, (ti, tl) in enumerate(zip(test_images, test_labels)): model.update_data_layer(reduce_image(ti) if reduce else ti) p = model.predict() if i % 100 == 0: print(f'{p}, {tl}, {max(model.value()):6.4f}') predictions.append(dict(label=tl, predicted_label=p, probability=max(model.value()))) predictions = pd.DataFrame(predictions) predictions.to_csv(f'mnist_{datetime.datetime.now():%Y%m%dT%H%M}.csv')