def test(self, num_function, num_shots, test_shots, epoch=1, input_range=(-2., 2.)): fig = plt.figure(figsize=(10,10)) # a = int(np.sqrt(num_function)) for i in range(num_function): # ax = fig.add_subplot(a,a,i+1) ax = fig.add_subplot(4,3,i+1) sampler = self.eval_set.sample(1)[0] c = [1, 4, 8, 16, 32, 64] num_shots = c[(i%6)] X_value, y_value = sampler.sample(num_shots+test_shots) X_c_value, X_t_value = X_value[:num_shots], X_value[num_shots:] y_c_value, y_t_value = y_value[:num_shots], y_value[num_shots:] m = self.parallel_models[0] X_gt, y_gt = sampler.get_all_samples() ax.plot(*sort_x(X_gt[:,0], y_gt), "-") ax.scatter(X_c_value[:,0], y_c_value) X_eval = np.linspace(self.eval_set.input_range[0], self.eval_set.input_range[1], num=100)[:,None] # step 1 y_hat = m.predict(self.session, X_c_value, y_c_value, X_eval, step=1) ax.plot(X_eval[:,0], y_hat, ":", color='gray', alpha=0.3) # step 5 y_hat = m.predict(self.session, X_c_value, y_c_value, X_eval, step=5) ax.plot(X_eval[:,0], y_hat, "--", color='gray', alpha=0.3) # step 10 y_hat = m.predict(self.session, X_c_value, y_c_value, X_eval, step=10) ax.plot(X_eval[:,0], y_hat, "-", color='gray', alpha=0.3) fig.savefig("figs/maml-{0}-{1}.pdf".format(self.eval_set.dataset_name, epoch)) plt.close()
def test(self, num_function, num_shots, test_shots, epoch=1): fig = plt.figure(figsize=(10, 10)) # a = int(np.sqrt(num_function)) for i in range(num_function): # ax = fig.add_subplot(a,a,i+1) ax = fig.add_subplot(4, 3, i + 1) sampler = self.eval_set.sample(1)[0] # # total_shots = np.random.randint(low=10, high=50) # num_shots = np.random.randint(low=total_shots-10, high=total_shots-1) # test_shots = total_shots - num_shots #num_shots = np.random.randint(low=1, high=30) #test_shots = np.random.randint(low=1, high=10) # num_shots, test_shots = 20, 10 c = [1, 4, 8, 16, 32, 64] num_shots = c[(i % 6)] # X_value, y_value = sampler.sample(num_shots + test_shots) X_c_value, X_t_value = X_value[:num_shots], X_value[num_shots:] y_c_value, y_t_value = y_value[:num_shots], y_value[num_shots:] m = self.parallel_models[0] X_gt, y_gt = sampler.get_all_samples() ax.plot(*sort_x(X_gt[:, 0], y_gt), "-") ax.scatter(X_c_value[:, 0], y_c_value) #ax.plot(*sort_x(X_value[:,0], y_value), "+") for k in range(20): X_eval = np.linspace(self.eval_set.input_range[0], self.eval_set.input_range[1], num=100)[:, None] y_hat = m.predict(self.session, X_c_value, y_c_value, X_eval) ax.plot(X_eval[:, 0], y_hat, "-", color='gray', alpha=0.3) #ax.plot(X_value[:,0], y_hat, "-", color='gray', alpha=0.3) fig.savefig("figs/np-{0}-{1}.pdf".format(self.eval_set.dataset_name, epoch)) plt.close()
def run_eval(self, num_func, num_shots=1, test_shots=50): m = self.parallel_models[0] saver = tf.train.Saver(var_list=self.variables) ckpt_file = self.save_dir + '/params.ckpt' print('restoring parameters from', ckpt_file) saver.restore(self.session, ckpt_file) evals = [] for _ in range(num_func): sampler = self.eval_set.sample(1)[0] X_value, y_value = sampler.sample(num_shots+test_shots) X_c_value, X_t_value = X_value[:num_shots], X_value[num_shots:] y_c_value, y_t_value = y_value[:num_shots], y_value[num_shots:] y_t_hat = m.predict(self.session, X_c_value, y_c_value, X_t_value, step=10) evals.append(np.mean(np.power(y_t_value - y_t_hat, 2))) eval = np.nanmean(evals) print(".......... EVAL : num_func {0} num_shots {1} test_shots {2}............".format(num_func, num_shots, test_shots)) print("\t{0}".format(eval)) fig = plt.figure(figsize=(10,10)) for i in range(4): # ax = fig.add_subplot(a,a,i+1) ax = fig.add_subplot(4,1,i+1) sampler = self.eval_set.sample(1)[0] c = [5, 10, 15, 20] num_shots = c[(i%4)] test_shots = 0 X_value, y_value = sampler.sample(num_shots+test_shots) X_c_value, X_t_value = X_value[:num_shots], X_value[num_shots:] y_c_value, y_t_value = y_value[:num_shots], y_value[num_shots:] m = self.parallel_models[0] X_gt, y_gt = sampler.get_all_samples() ax.plot(*sort_x(X_gt[:,0], y_gt), "-") ax.scatter(X_c_value[:,0], y_c_value) X_eval = np.linspace(self.eval_set.input_range[0], self.eval_set.input_range[1], num=100)[:,None] # step 1 y_hat = m.predict(self.session, X_c_value, y_c_value, X_eval, step=1) ax.plot(X_eval[:,0], y_hat, ":", color='gray', alpha=0.3) # step 5 y_hat = m.predict(self.session, X_c_value, y_c_value, X_eval, step=5) ax.plot(X_eval[:,0], y_hat, "--", color='gray', alpha=0.3) # step 10 y_hat = m.predict(self.session, X_c_value, y_c_value, X_eval, step=10) ax.plot(X_eval[:,0], y_hat, "-", color='gray', alpha=0.3) fig.savefig("figs/maml-{0}-{1}.pdf".format(self.eval_set.dataset_name, "eval")) plt.close()