def main(): # MLP to be fit net_mlp = None with open("/Users/alange/programming/MNIST/store/classic_32.json", "r") as f: net_mlp = Network.read_from_json(f) # Initial particle network net = ParticleNetwork(cost="categorical_cross_entropy", particle_input=ParticleInput(784, s=2.0)) net.append(Particle(784, 32, activation="tanh", zeta=1.0, s=2.0)) net.append(Particle(32, 10, activation="softmax", zeta=1.0, s=2.0)) compute_matrices(net) error = compute_error(net_mlp, net) de_db, de_dq, de_dt, de_drx, de_dry, de_drz = compute_grad_w(net_mlp, net) fd_b, fd_q, fd_t, fd_x, fd_y, fd_z = compute_fd_grad(net_mlp, net) for l, layer in enumerate(net.layers): diff_b = np.mean(de_db[l] - fd_b[l]) diff_q = np.mean(de_dq[l] - fd_q[l]) diff_t = np.mean(de_dt[l] - fd_t[l]) diff_x = np.mean(de_drx[l] - fd_x[l]) print("b", diff_b) print("q", diff_q) print("t", diff_t) print("x", diff_x)
def main4(): net = Network(cost="quadratic") net.append(Dense(2, 5)) net.append(Dense(5, 3)) train_X = np.asarray([[0.2, -0.3]]) train_Y = np.asarray([[0.0, 1.0, 0.0]]) with open("/tmp/tmp.json", "w") as f: net.write_to_json(f) net2 = None with open("/tmp/tmp.json", "r") as f: net2 = Network.read_from_json(f) print(net.cost(train_X, train_Y)) print(net2.cost(train_X, train_Y))