def main(self, x_train, y_train, x_test, y_test, x_val, y_val): print("############################# Prime Train ################################") model_prime = nn_models() model_prime.ip_shape = x_train.shape model_p = model_prime.lenet5() model_prime = Model(inputs=model_p.input, outputs=model_p.get_layer('dense2').output) prime_op_tr = model_prime.predict(x_train) prime_op_tt = model_prime.predict(x_test) prime_op_v = model_prime.predict(x_val) for i in range(self.iters): split_buckets = self.bucket_function(i) experts_out_train = [] experts_out_test = [] experts_out_val = [] for j in sorted(split_buckets): X = x_train[split_buckets[j]] y = y_train[split_buckets[j]] model = self.train_model(X, y, x_val, y_val, i) yhats_train = model.predict(x_train, batch_size=256) yhats_test = model.predict(x_test, batch_size=256) yhats_val = model.predict(x_val, batch_size=256) experts_out_train.append(yhats_train) experts_out_test.append(yhats_test) experts_out_val.append(yhats_val) yhat_tr = np.hstack(experts_out_train) yhat_tt = np.hstack(experts_out_test) yhat_val = np.hstack(experts_out_val) model = self.gater() history = model.fit([prime_op_tr, yhat_tr], y_train, shuffle=True, batch_size=256, verbose=1, validation_data=([prime_op_v, yhat_val], y_val), epochs=500, callbacks=[self.early_stopping]) yhats_train = model.predict([prime_op_tr, yhat_tr], batch_size=256) yhats_test = model.predict([prime_op_tt, yhat_tt], batch_size=256) yhats_val = model.predict([prime_op_v, yhat_val], batch_size=256) tre = eval_target(yhats_train, y_train) tte = eval_target(yhats_test, y_test) vale = eval_target(yhats_val, y_val) logging.info('{}, {}, {}, {}'.format(i, tre, vale, tte)) expert_units = Model(inputs=model.input, outputs=model.get_layer('layer_op').output) self.wm_xi = expert_units.predict([prime_op_tr, yhat_tr]) return None
def train_model(self, X, y, x_val, y_val, i): model = nn_models() model.ip_shape = X.shape model = model.lenet5() model.fit(X, y, batch_size=256, epochs=500, validation_data=(x_val, y_val), verbose=1, callbacks=[self.early_stopping]) yhat_train = model.predict(X, batch_size=256) yhat_val = model.predict(x_val, batch_size=256) train_error = eval_target(yhat_train, y) val_error = eval_target(yhat_val, y_val) self.warn_log.append([i, train_error, val_error]) return model
def model_train(self, X, y, X_val, y_val): model = nn_models() model.ip_shape = X.shape model = model.lenet5() early_callback = CustomCallback() model.fit(X, y, batch_size=256, epochs=500, validation_data=(X_val, y_val), callbacks=[early_callback], verbose=1) yhat_train = model.predict(X, batch_size=256) yhat_val = model.predict(x_val, batch_size=256) train_error = eval_target(yhat_train, y) val_error = eval_target(yhat_val, y_val) self.warn_log.append([train_error, val_error]) return model
def model_train(self, X, y, x_test, y_test, X_val, y_val, i): model = nn_models() model.ip_shape = X.shape model = model.lenet5() model.learning_rate = 0.0001 model.fit(X, y, batch_size=256, epochs=self.epoch, validation_data=(X_val, y_val), verbose=1, callbacks=[self.early_stopping]) yhat_train = model.predict(X, batch_size=256) yhat_val = model.predict(x_val, batch_size=256) yhat_test = model.predict(x_test, batch_size=256) train_error = eval_target(yhat_train, y) val_error = eval_target(yhat_val, y_val) test_error = eval_target(yhat_test, y_test) self.warn_log.append([i, train_error, val_error, test_error]) return model
def main(self, x_train, y_train, x_test, y_test, x_val, y_val): print("############################# Prime Train ################################") model_p = nn_models() model_p.ip_shape = x_train.shape model_p.learning_rate = 0.0001 model_p = model_p.lenet5() model_p.fit(x_train, y_train, batch_size=256, epochs=self.epoch, validation_data=(x_val, y_val), verbose=1, callbacks=[self.early_stopping]) model_prime = Model(inputs=model_p.input, outputs=model_p.get_layer('dense2').output) prime_op_tr = model_prime.predict(x_train) prime_op_tt = model_prime.predict(x_test) prime_op_v = model_prime.predict(x_val) prime_op_train = model_p.predict(x_train) prime_op_val = model_p.predict(x_val) prime_op_test = model_p.predict(x_test) prime_train_e = eval_target(prime_op_train, y_train) prime_val_e = eval_target(prime_op_val, y_val) prime_test_e = eval_target(prime_op_test, y_test) self.warn_log.append([-1, prime_train_e, prime_val_e, prime_test_e]) for i in range(self.iters): yhat_train_exp = [] yhats_test_exp = [] yhats_val_exp = [] for j in range(self.experts): print("############################# Expert {} Iter {} ################################".format(j, i)) buckets = self.split_buckets(i, j, y_train) X = x_train[buckets] y = y_train[buckets] model = self.model_train(X, y, x_test, y_test, x_val, y_val, i) yhat_train = model.predict(x_train, batch_size=256) yhats_test = model.predict(x_test, batch_size=256) yhats_val = model.predict(x_val, batch_size=256) yhat_train_exp.append(yhat_train) yhats_test_exp.append(yhats_test) yhats_val_exp.append(yhats_val) yhat_tr = np.hstack(yhat_train_exp) yhat_tt = np.hstack(yhats_test_exp) yhat_val = np.hstack(yhats_val_exp) model = self.gater() history = model.fit([prime_op_tr, yhat_tr], y_train, shuffle=True, batch_size=256, verbose=1, validation_data=([prime_op_v, yhat_val], y_val), epochs=self.epoch, callbacks=[self.early_stopping]) yhats_train = model.predict([prime_op_tr, yhat_tr], batch_size=256) yhats_test = model.predict([prime_op_tt, yhat_tt], batch_size=256) yhats_val = model.predict([prime_op_v, yhat_val], batch_size=256) tre = eval_target(yhats_train, y_train) tte = eval_target(yhats_test, y_test) vale = eval_target(yhats_val, y_val) logging.info('{}, {}, {}, {}'.format(i, tre, vale, tte)) expert_units = Model(inputs=model.input, outputs=model.get_layer('layer_op').output) self.wm_xi = expert_units.predict([prime_op_tr, yhat_tr]) return None
def main(self, x_train, y_train, x_test, y_test, x_val, y_val): model_prime = nn_models() model_prime.ip_shape = x_train.shape model_p = model_prime.lenet5() model_p.fit(x_train, y_train, batch_size=256, epochs=500, validation_data=(x_val, y_val), verbose=1, callbacks=[self.early_stopping]) model_prime = Model(inputs=model_p.input, outputs=model_p.get_layer('dense2').output) prime_op_tr = model_prime.predict(x_train) prime_op_tt = model_prime.predict(x_test) prime_op_v = model_prime.predict(x_val) prime_op_train = model_p.predict(x_train) prime_op_val = model_p.predict(x_val) tre = eval_target(prime_op_train, y_train) vale = eval_target(prime_op_val, y_val) self.warn_log.append([-1, tre, vale]) for i in range(self.iters): split_buckets = self.bucket_function(i) yhat_train_exp = [] yhats_test_exp = [] yhats_val_exp = [] for expert_index in sorted(split_buckets): y = y_train[split_buckets[expert_index]] X = x_train[split_buckets[expert_index]] model = self.model_train(X, y, x_val, y_val, i) yhat_train = model.predict(x_train, batch_size=256) yhats_test = model.predict(x_test, batch_size=256) yhats_val = model.predict(x_val, batch_size=256) yhat_train_exp.append(yhat_train) yhats_test_exp.append(yhats_test) yhats_val_exp.append(yhats_val) print("Expert Index {}".format(expert_index)) yhat_tr = np.hstack(yhat_train_exp) yhat_tt = np.hstack(yhats_test_exp) yhat_val = np.hstack(yhats_val_exp) model = self.gater() history = model.fit([prime_op_tr, yhat_tr], y_train, shuffle=True, batch_size=256, verbose=1, validation_data=([prime_op_v, yhat_val], y_val), epochs=500, callbacks=[self.early_stopping]) yhats_train = model.predict([prime_op_tr, yhat_tr], batch_size=256) yhats_test = model.predict([prime_op_tt, yhat_tt], batch_size=256) yhats_val = model.predict([prime_op_v, yhat_val], batch_size=256) tre = eval_target(yhats_train, y_train) tte = eval_target(yhats_test, y_test) vale = eval_target(yhats_val, y_val) logging.info('{}, {}, {}, {}'.format(i, tre, vale, tte)) expert_units = Model(inputs=model.input, outputs=model.get_layer('layer_op').output) self.wm_xi = expert_units.predict([prime_op_tr, yhat_tr]) return "Gater Training Complete"
x_val = x_val.astype('float32') y_train = np_utils.to_categorical(y_train, 10) y_test = np_utils.to_categorical(y_test, 10) y_val = np_utils.to_categorical(y_val, 10) x_train /= 255 x_test /= 255 x_val /= 255 mean_image = np.mean(x_train, axis=0) x_train -= mean_image x_test -= mean_image x_val -= mean_image model = nn_models() model.ip_shape = x_train.shape model = model.lenet5() early_callback = CustomCallback() model.fit(x_train, y_train, batch_size=256, verbose=1, validation_data=(x_val, y_val), epochs=500, callbacks=[early_callback]) yhats_train = model.predict(x_train, batch_size=256) yhats_val = model.predict(x_val, batch_size=256) yhats_test = model.predict(x_test, batch_size=256) train_error = eval_target(yhats_train, y_train) val_error = eval_target(yhats_val, y_val) test_error = eval_target(yhats_test, y_test)