def test_chain_rule_factors(self): image_data = [0.1, 0.3, 0.7] image_layer = Image(image_data=image_data, shape=(3, 1), maximum=1) softmax = SoftMax([1, 2]) model = Model(layers=[image_layer, softmax]) model.compile(build=True) # bump and grind p0 = model.value() epsilon = 1e-6 crf_bumped = [] for i in range(len(image_data)): model.update_data_layer([((d + epsilon) if i == j else d) for j, d in enumerate(image_data)], maximum=1) p1 = model.value() crf_bumped.append([(pp1 - pp0) / epsilon for pp0, pp1 in zip(p0, p1)]) crf_bumped = np.array(crf_bumped).transpose() crf_layer = softmax.chain_rule_factors(cache={}) crf_model = model.chain_rule_factors() for g, j, m in zip(flatten(crf_bumped), flatten(crf_layer), flatten(crf_model)): print(f'{g:8.6f} {j:8.6f} {m:8.6f}') for g, j, m in zip(flatten(crf_bumped), flatten(crf_layer), flatten(crf_model)): self.assertAlmostEqual(g, j, delta=0.001) self.assertAlmostEqual(j, m, delta=0.000001)
pixels = 81 image = Image(image_data=[random.randint(59, 59) for _ in range(pixels)]) cnn_a = CNN(reLU(), (3, 3), name='a') cnn_b = CNN(reLU(), (3, 3), name='b') cnn_c = CNN(reLU(), (3, 3), name='c') dense = Dense(reLU(), (10, 1), name='d') category = Category([i for i in range(10)]) model = Model(layers=[ image, cnn_a, cnn_b, cnn_c, dense, category, ]) model.compile(build=True) v = model.value() # v = model.probability() analytic_jacobian = model.jacobian() # finite_differences(model, False) print(v) model.update_data_layer([random.randint(199, 199) for _ in range(pixels)]) v = model.value() # analytic_jacobian = model.jacobian() finite_differences(model, False, nmax=500) print(v) pass
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')