def main(args): if args.seed >= 0: random_seed(args.seed) train_set, valid_set, test_set = prepare_dataset(args.data_dir) train_x, train_y = train_set test_x, test_y = test_set # train_y = get_one_hot(train_y, 2) net = Net([Dense(100), ReLU(), Dense(30), ReLU(), Dense(1)]) model = Model(net=net, loss=SigmoidCrossEntropy(), optimizer=Adam(lr=args.lr)) iterator = BatchIterator(batch_size=args.batch_size) loss_list = list() for epoch in range(args.num_ep): t_start = time.time() for batch in iterator(train_x, train_y): pred = model.forward(batch.inputs) loss, grads = model.backward(pred, batch.targets) model.apply_grad(grads) loss_list.append(loss) print("Epoch %d time cost: %.4f" % (epoch, time.time() - t_start)) # evaluate model.set_phase("TEST") test_y_idx = np.asarray(test_y).reshape(-1) test_pred = model.forward(test_x) test_pred[test_pred > 0] = 1 test_pred[test_pred <= 0] = 0 test_pred_idx = test_pred.reshape(-1) res = accuracy(test_pred_idx, test_y_idx) print(res) model.set_phase("TRAIN")
def mlp_D(): w_init = Normal(0.0, 0.02) return Net([ Dense(300, w_init=w_init), LeakyReLU(), Dense(100, w_init=w_init), LeakyReLU(), Dense(1, w_init=w_init) ])
def mlp_G(): w_init = Normal(0.0, 0.02) return Net([ Dense(100, w_init=w_init), LeakyReLU(), Dense(300, w_init=w_init), LeakyReLU(), Dense(784, w_init=w_init), Sigmoid() ])
def main(args): if args.seed >= 0: random_seed(args.seed) # data preparing train_x, train_y, img_shape = prepare_dataset(args.img) net = Net([ Dense(30), ReLU(), Dense(100), ReLU(), Dense(100), ReLU(), Dense(30), ReLU(), Dense(3), Sigmoid() ]) model = Model(net=net, loss=MSE(), optimizer=Adam()) iterator = BatchIterator(batch_size=args.batch_size) for epoch in range(args.num_ep): for batch in iterator(train_x, train_y): preds = model.forward(batch.inputs) loss, grads = model.backward(preds, batch.targets) model.apply_grad(grads) # evaluate preds = net.forward(train_x) mse = mean_square_error(preds, train_y) print("Epoch %d %s" % (epoch, mse)) # generate painting if epoch % 5 == 0: preds = preds.reshape(img_shape[0], img_shape[1], -1) preds = (preds * 255.0).astype("uint8") name, ext = os.path.splitext(args.img) filename = os.path.basename(name) out_filename = filename + "-paint-epoch" + str(epoch) + ext if not os.path.exists(args.output_dir): os.makedirs(args.output_dir) out_path = os.path.join(args.output_dir, out_filename) Image.fromarray(preds).save(out_path) print("save painting to %s" % out_path)
def cnn_model(): net = Net([ Conv2D(kernel=[3, 3, 1, 2]), MaxPool2D(pool_size=[2, 2], stride=[2, 2]), Conv2D(kernel=[3, 3, 2, 4]), MaxPool2D(pool_size=[2, 2], stride=[2, 2]), Flatten(), Dense(1) ]) return Model(net, loss=MSE(), optimizer=SGD())
def test_struct_param(): net = Net([Dense(10, 1)]) params = net.params assert isinstance(params, StructuredParam) assert isinstance(params.values, np.ndarray) assert len(params.values) == 2 # w and b # test operations on StructParam bigger = params * 10 assert isinstance(bigger, StructuredParam) assert params.shape == bigger.shape assert (params.values[-1] * 10 == bigger.values[-1]).all()
def test_parameters_change(fake_dataset): # make sure the parameters does change after apply gradients # fake dataset X, y = fake_dataset # simple model net = Net([Dense(10), Dense(1)]) loss = MSE() opt = SGD(lr=1.0) model = Model(net, loss, opt) # forward and backward pred = model.forward(X) loss, grads = model.backward(pred, y) # parameters change test params_before = model.net.params.values model.apply_grad(grads) params_after = model.net.params.values for p1, p2 in zip(params_before, params_after): assert np.all(p1 != p2)
def main(args): if args.seed >= 0: random_seed(args.seed) train_set, valid_set, test_set = mnist(args.data_dir) train_x, train_y = train_set test_x, test_y = test_set train_y = get_one_hot(train_y, 10) if args.model_type == "cnn": train_x = train_x.reshape((-1, 28, 28, 1)) test_x = test_x.reshape((-1, 28, 28, 1)) if args.model_type == "cnn": # a LeNet-5 model with activation function changed to ReLU net = Net([ Conv2D(kernel=[5, 5, 1, 6], stride=[1, 1], padding="SAME"), ReLU(), MaxPool2D(pool_size=[2, 2], stride=[2, 2]), Conv2D(kernel=[5, 5, 6, 16], stride=[1, 1], padding="SAME"), ReLU(), MaxPool2D(pool_size=[2, 2], stride=[2, 2]), Flatten(), Dense(120), ReLU(), Dense(84), ReLU(), Dense(10) ]) elif args.model_type == "dense": net = Net([ Dense(200), ReLU(), Dense(100), ReLU(), Dense(70), ReLU(), Dense(30), ReLU(), Dense(10) ]) else: raise ValueError("Invalid argument: model_type") model = Model(net=net, loss=SoftmaxCrossEntropy(), optimizer=Adam(lr=args.lr)) iterator = BatchIterator(batch_size=args.batch_size) loss_list = list() for epoch in range(args.num_ep): t_start = time.time() for batch in iterator(train_x, train_y): pred = model.forward(batch.inputs) loss, grads = model.backward(pred, batch.targets) model.apply_grad(grads) loss_list.append(loss) print("Epoch %d time cost: %.4f" % (epoch, time.time() - t_start)) # evaluate model.set_phase("TEST") test_pred = model.forward(test_x) test_pred_idx = np.argmax(test_pred, axis=1) test_y_idx = np.asarray(test_y) res = accuracy(test_pred_idx, test_y_idx) print(res) model.set_phase("TRAIN")
def build_net(self): q_net = Net([Dense(100), ReLU(), Dense(self.action_dim)]) return q_net
def main(args): if args.seed >= 0: random_seed(args.seed) # create output directory for saving result images if not os.path.exists(args.output_dir): os.makedirs(args.output_dir) # prepare and read dataset train_set, _, test_set = mnist(args.data_dir) train_x, train_y = train_set test_x, test_y = test_set # specify the encoder and decoder net structure encoder_net = Net([Dense(256), ReLU(), Dense(64)]) decoder_net = Net([ReLU(), Dense(256), Tanh(), Dense(784), Tanh()]) nets = (encoder_net, decoder_net) optimizers = (Adam(args.lr), Adam(args.lr)) model = AutoEncoder(nets, loss=MSE(), optimizer=optimizers) # for pre-trained model, test generated images from latent space if args.load_model is not None: # load pre-trained model model.load(os.path.join(args.output_dir, args.load_model)) print("Loaded model fom %s" % args.load_model) # transition from test[from_idx] to test[to_idx] in n steps idx_arr, n = [2, 4, 32, 12, 82], 160 print("Transition in numbers", [test_y[i] for i in idx_arr], "in %d steps ..." % n) stops = [model.en_net.forward(test_x[i]) for i in idx_arr] k = int(n / (len(idx_arr) - 1)) # number of code per transition # generate all transition codes code_arr = [] for i in range(len(stops) - 1): t = [c.copy() for c in transition(stops[i], stops[i + 1], k)] code_arr += t # apply decoding all n "code" from latent space... batch = None for code in code_arr: # translate latent space to image genn = model.de_net.forward(code) # save decoded results in a batch if batch is None: batch = np.array(genn) else: batch = np.concatenate((batch, genn)) output_path = os.path.join(args.output_dir, "genn-latent.png") save_batch_as_images(output_path, batch) quit() # train the auto-encoder iterator = BatchIterator(batch_size=args.batch_size) for epoch in range(args.num_ep): for batch in iterator(train_x, train_y): origin_in = batch.inputs # make noisy inputs m = origin_in.shape[0] # batch size mu = args.gaussian_mean # mean sigma = args.gaussian_std # standard deviation noises = np.random.normal(mu, sigma, (m, 784)) noises_in = origin_in + noises # noisy inputs # forward genn = model.forward(noises_in) # back-propagate loss, grads = model.backward(genn, origin_in) # apply gradients model.apply_grad(grads) print("Epoch: %d Loss: %.3f" % (epoch, loss)) # save all the generated images and original inputs for this batch noises_in_path = os.path.join(args.output_dir, "ep%d-input.png" % epoch) genn_path = os.path.join(args.output_dir, "ep%d-genn.png" % epoch) save_batch_as_images(noises_in_path, noises_in, titles=batch.targets) save_batch_as_images(genn_path, genn, titles=batch.targets) # save the model after training model.save(os.path.join(args.output_dir, args.save_model))
def fc_model(): net = Net([Dense(10), Dense(1)]) loss = MSE() opt = SGD() return Model(net, loss, opt)