def MLP_classifier2(X, T, n_items, nr_output=2): from chainer.optimizers import Adam from chainer import Variable import chainer.functions as F from chainer import optimizers from chainer import iterators import random as r data = list(zip(X, T)) r.shuffle(data) train = data[0:int(len(data) * 0.8)] test = data[int(len(data) * 0.8):] print(len(train), len(test)) batchsize = 100 max_label = int(max(T)) + 1 train_iter = iterators.SerialIterator(train, batchsize) test_iter = iterators.SerialIterator(test, batchsize, repeat=False, shuffle=False) model = MLP(n_hidden=int(n_items / 2), n_output=nr_output) gpu_id = -1 # Set to -1 if you use CPU if gpu_id >= 0: model.to_gpu(gpu_id) optimizer = optimizers.Adam(alpha=0.001) optimizer.setup(model) import numpy as np from chainer.dataset import concat_examples from chainer.backends.cuda import to_cpu max_epoch = 30 train_accs = [] train_accs_epochs = [] val_acc_epochs = [] while train_iter.epoch < max_epoch: # ---------- One iteration of the training loop ---------- train_batch = train_iter.next() image_train, target_train = concat_examples(train_batch, gpu_id) image_train = Variable(image_train).data.astype(np.float32) target_train = Variable(target_train).data.astype(np.float32) OH_T = np.asarray([one_hot(int(x), max_label) for x in target_train]) OH_T = Variable(OH_T).data.astype(np.float32) # Calculate the prediction of the network prediction_train = model(image_train) final_pred = np.zeros(shape=(len(prediction_train), )) for i in range(len(prediction_train)): dummy = list(prediction_train[i].data) final_pred[i] = dummy.index(max(dummy)) # Calculate the loss with MSE loss = F.mean_squared_error(prediction_train, OH_T) # Calculate the gradients in the network model.cleargrads() loss.backward() # Update all the trainable parameters optimizer.update() train_acc = classification_accuracy(final_pred, target_train) train_accs.append(train_acc) # --------------------- until here --------------------- # Check the validation accuracy of prediction after every epoch if train_iter.is_new_epoch: # If this iteration is the final iteration of the current epoch # Display the training loss print('epoch:{:02d} train_loss:{:.04f}'.format( train_iter.epoch, float(to_cpu(loss.array))), end='') print(" train acc: {:f} ".format( sum(train_accs) / len(train_accs)), end='') train_accs_epochs.append(sum(train_accs) / len(train_accs)) train_accs = [] test_losses = [] test_accuracies = [] while True: test_batch = test_iter.next() image_test, target_test = concat_examples(test_batch, gpu_id) image_test = Variable(image_test).data.astype(np.float32) target_test = Variable(target_test).data.astype(np.float32) OH_T = np.asarray( [one_hot(int(x), max_label) for x in target_test]) OH_T = Variable(OH_T).data.astype(np.float32) target_test = Variable(target_test).data.astype(np.float32) # Forward the test data prediction_test = model(image_test) final_pred = np.zeros(shape=(len(prediction_test), )) for i in range(len(prediction_test)): dummy = list(prediction_test[i].data) final_pred[i] = dummy.index(max(dummy)) # Calculate the loss loss_test = F.mean_squared_error(prediction_test, OH_T) #loss_test = F.mean_squared_error(prediction_test, OH_T) test_losses.append(to_cpu(loss_test.array)) # Calculate the accuracy #prediction_test = Variable(prediction_test).data.astype(np.int) target_test = Variable(target_test).data.astype(np.int) accuracy = classification_accuracy(final_pred, target_test.data) #print(prediction_test, target_test) test_accuracies.append(accuracy) if test_iter.is_new_epoch: test_iter.epoch = 0 test_iter.current_position = 0 test_iter.is_new_epoch = False test_iter._pushed_position = None break print('val_loss:{:.04f} val_accuracy:{:.04f}'.format( np.mean(test_losses), np.mean(test_accuracies))) val_acc_epochs.append(np.mean(test_accuracies)) confusion_matrix(final_pred, target_test.data, size=max_label) #print(train_accs_epochs, val_acc_epochs) #plot_line(range(max_epoch), train_accs_epochs, show=False) #plot_line(range(max_epoch), val_acc_epochs, legend=['train accuracy', 'validation accuracy'], xlabel='epoch', ylabel='accuracy') X_train, T_train = X[0:int(0.9 * len(X))], T[0:int(0.9 * len(X))] X_test, T_test = X[int(0.9 * len(X)):], T[int(0.9 * len(X)):] X_test = Variable(X_test).data.astype(np.float32) plot_ROC = True if plot_ROC: import sklearn.metrics as metrics # calculate the fpr and tpr for all thresholds of the classification preds = model(X_test) final_pred = np.zeros(shape=(len(preds), )) for i in range(len(preds)): dummy = list(preds[i].data) final_pred[i] = int(dummy.index(max(dummy))) #print(len(T_test), len(final_pred), len(X_test)) fpr, tpr, threshold = metrics.roc_curve(T_test, final_pred) roc_auc = metrics.auc(fpr, tpr) # method I: plt import matplotlib.pyplot as plt plt.title('Receiver Operating Characteristic') plt.plot(fpr, tpr, 'b', label='AUC = %0.2f' % roc_auc) plt.legend(loc='lower right') plt.plot([0, 1], [0, 1], 'r--') plt.xlim([0, 1]) plt.ylim([0, 1]) plt.ylabel('True Positive Rate') plt.xlabel('False Positive Rate') plt.show() return np.mean(test_accuracies)
with open("../UCI-None/config.json", "r") as f: config_list = json.load(f) config = config_list["housing"] shape = [n_input] for i in range(config[0]): shape.append(config[1]) shape.append(1) # Create the model tf.reset_default_graph() X = tf.placeholder("float", [None, n_input], name="X_in") network = MLP(shape) with tf.variable_scope("model", reuse=tf.AUTO_REUSE): pred = network.model(X) saver = tf.train.Saver(max_to_keep=1) # Create tf session sess = tf.Session() # Wrap it for LIME wrapper = Wrapper(sess, pred, X) wrapper.set_index(0) # Configure LIME exp = lime_tabular.LimeTabularExplainer(X_train, discretize_continuous=False,
from Environment import Board import numpy as np from Models import MLP, REINFORCEAgent, Adam if __name__ == '__main__': WIDTH, HEIGHT = 7, 10 max_env_steps = 100 board = Board(WIDTH, HEIGHT, max_env_steps) network = MLP(n_output=WIDTH, n_hidden=20) agent = REINFORCEAgent(board.get_action_space, network, optimizer=Adam()) episode_count = 10001 done = False reward = 0 R = np.zeros(episode_count) winners = [] for i in range(episode_count): #print(i) board.reset() ob = board.get_state() loss = 0 while True: action, policy = agent.act(ob, reward, done) ob, reward, done, winner = board.step(action[0]) #print(policy) # get reward associated with taking the previous action in the previous state agent.rewards.append(reward) R[i] += reward
yTest = torch.tensor(yTest) #x = torch.tensor(x).to(device) #y = torch.tensor(y).to(device) #x = torch.randn(N, inputDim).to(device) #y = torch.randn(N, 1).to(device) ### NN architecture ### hiddenLayerNo = int(configs["hiddenLayerNo"].values) unitsPerLayer = inputDim + 1 layerWidths = [unitsPerLayer for i in range(hiddenLayerNo)] outputDim = 1 activation = F.relu # Mean Squared Error (MSE) as loss function lossFn = torch.nn.MSELoss(reduction='mean') device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") # generate net net = MLP.MLP(inputDim, layerWidths, outputDim, activation, nonce) #net.to(device) ### termination critera ### runTime = int(configs["runtime"].values) # in seconds maxFEs = 150 * populationSize * N # 150 epochs for each individual having the full propagation
name='B_1'), 'b2': initializers.bias_initializer([hidden_size], name='B_2'), 'out': initializers.bias_initializer([1], name='B_out') } x = tf.placeholder(tf.float32, [None, input_feats]) y = tf.placeholder(tf.float32, [None]) learning_rate = tf.placeholder(tf.float32) keep_prob = tf.placeholder(tf.float32) optimizer, output, sharpe_plus_l2_loss = \ MLP(x, y, weights, biases, keep_prob, curr_optimizer, learning_rate, objective, batch_size=batch_size, markets=markets, activation=network_activation, l2Reg=l2Reg, l2RegOutput=l2Reg * 1., l2Reg_biases=l2Reg * 1.) saver = tf.train.Saver() with tf.Session() as sess: # init = tf.global_variables_initializer() saver.restore( sess, './MLP-checkpointFiles/run%d-s-%d-LR-%.6f-epoch-%d-l2-%.5f.ckpt' % (R + 1, hidden_size, LR, training_epochs, l2Reg)) trainPred = sess.run(output, feed_dict={
logging.info("begin training %s model ......" % args.model) logging.info("dataset:%s pretrain:%d embedding_size:%d" % (args.dataset, args.pretrain, args.embed_size)) logging.info("regs:%.8f, %.8f learning_rate:%.4f train_loss:%d" % (regs[0], regs[1], args.lr, args.train_loss)) if __name__ == '__main__': # initialize logging args = parse_args() init_logging(args) #initialize dataset dataset = Dataset(args.path + args.dataset) #initialize models if args.model == "FISM" : model = FISM(dataset.num_items, args) elif args.model == "GMF" : model = GMF(dataset.num_users, dataset.num_items, args) elif args.model == "MLP" : model = MLP(dataset.num_users, dataset.num_items, args) elif args.model == "MF": model = MF(dataset.num_users, dataset.num_items, args) model.build_graph() #start training saver = SaverFactory.getSaver(model) saver.setPrefix(args.param_prefix) training(model, dataset, args, saver)
} biases = { 'b1': initializers.bias_initializer([hidden1_size], name='B_h1'), 'b2': initializers.bias_initializer([hidden2_size], name='B_h2'), 'out': initializers.bias_initializer([1], name='B_out') } x = tf.placeholder(tf.float32, [None, input_feats]) y = tf.placeholder(tf.float32, [None]) learning_rate = tf.placeholder(tf.float32) optimizer, output, sharpe_plus_l2_loss = MLP(x, y, weights, biases, curr_optimizer, learning_rate, objective, batch_size=batch_size, markets=markets, activation=network_activation, l2Reg=l2Reg, l2RegOutput=l2Reg * 0.5, l2Reg_biases=l2Reg * 0.5) # initialize all tensors- to be run in Session! init = tf.global_variables_initializer() # saver for restoring the whole model graph of # tensors from the checkpoint file saver = tf.train.Saver() # launch default graph: with tf.Session() as sess: for train_step in range(total_training_steps): total_batches = train_data_dict[
def eval( source, # Network parameters hidden_layer_sizes=[100, 100, 100], learning_rate=0.001, # Regularizer parameters regularizer=None, c=1.0, # Training parameters batch_size=2, reg_batch_size=1, stopping_epochs=200, min_epochs=500, stop_on_loss=True, evaluate_explanation=True): # Allow multiple sessions on a single GPU. tf_config = tf.ConfigProto() tf_config.gpu_options.allow_growth = True # Reset TF graph (avoids issues with repeat experiments) tf.reset_default_graph() # Get Data if regularizer is not None: data = ToyDataManager(source, train_batch_size=batch_size, reg_batch_size=reg_batch_size) else: data = ToyDataManager(source, train_batch_size=batch_size) n = data.X_train.shape[0] n_input = data.X_train.shape[1] n_out = data.y_train.shape[1] # Regularizer Parameters if regularizer is not None: # Weight of the regularization term in the loss function c = tf.constant(c) # Number of neighbors to hallucinate per point num_samples = np.max((20, np.int(2 * n_input))) # Network Parameters shape = [n_input] for size in hidden_layer_sizes: shape.append(size) shape.append(n_out) # Graph inputs X = tf.placeholder("float", [None, n_input], name="X_in") Y = tf.placeholder("float", [None, n_out], name="Y_in") if regularizer is not None: X_reg = tf.placeholder("float", [None, n_input], name="X_reg") # Link the graph inputs into the DataManager so its train_feed() and eval_feed() functions work if regularizer is not None: data.link_graph(X, Y, X_reg=X_reg) else: data.link_graph(X, Y) # Build the model network = MLP(shape) with tf.variable_scope("model", reuse=tf.AUTO_REUSE): pred = network.model(X) # Build the regularizer if regularizer == "Causal": regularizer = Regularizer(network.model, n_input, num_samples) reg = regularizer.causal(X_reg) # Define the loss and optimization process model_loss = tf.losses.mean_squared_error(labels=Y, predictions=pred) tf.summary.scalar("MSE", model_loss) perf_op = model_loss smaller_is_better = True if regularizer is None: loss_op = model_loss else: loss_op = model_loss + c * reg tf.summary.scalar("Regularizer", reg) tf.summary.scalar("Loss", loss_op) summary_op = tf.summary.merge_all() optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate) train_op = optimizer.minimize(loss_op) # Train the model init = [ tf.global_variables_initializer(), tf.local_variables_initializer() ] saver = tf.train.Saver(max_to_keep=1) if stop_on_loss: # Stop on validation loss function best_loss = np.inf else: # Stop on the validation performance if smaller_is_better: best_perf = np.inf else: best_perf = 0.0 best_epoch = 0 with tf.Session(config=tf_config) as sess: train_writer = tf.summary.FileWriter("train", sess.graph) val_writer = tf.summary.FileWriter("val") sess.run(init) epoch = 0 while True: # Stopping condition if epoch - best_epoch > stopping_epochs and epoch > min_epochs: break # Run a training epoch total_batch = int(n / batch_size) for i in range(total_batch): dict = data.train_feed() sess.run(train_op, feed_dict=dict) # Run model metrics if epoch % 10 == 0: dict = data.eval_feed() summary = sess.run(summary_op, feed_dict=dict) train_writer.add_summary(summary, epoch) dict = data.eval_feed(val=True) if stop_on_loss: summary, val_loss = sess.run([summary_op, loss_op], feed_dict=dict) if val_loss < best_loss: print(os.getcwd(), " ", epoch, " ", val_loss) best_loss = val_loss best_epoch = epoch saver.save(sess, "./model.cpkt") else: summary, val_perf = sess.run([summary_op, perf_op], feed_dict=dict) if smaller_is_better and val_perf < best_perf: progress = True elif not smaller_is_better and val_perf > best_perf: progress = True else: progress = False if progress: print(os.getcwd(), " ", epoch, " ", val_perf) best_perf = val_perf best_epoch = epoch saver.save(sess, "./model.cpkt") val_writer.add_summary(summary, epoch) epoch += 1 train_writer.close() val_writer.close() ### # Evaluate the chosen model ### saver.restore(sess, "./model.cpkt") pred_grid = sess.run(pred, {X: x_grid}) plt.plot(x_grid, pred_grid) ### # Make explanations #### def predict_fn(x): return np.squeeze(sess.run(pred, {X: x})) exp = MAPLE(data.X_train, predict_fn(data.X_train), data.X_val, predict_fn(data.X_val)) x = np.zeros((1, 1)) x[0] = 0.1 e = exp.explain(x) c1 = e["coefs"] x[0] = 0.4 e = exp.explain(x) c2 = e["coefs"] return c1, c2
def main(): ################################################################################################ # This process, set up the whole models and parameters # Get Hyper parameters and sets # General arg arg_DeviceIds = [0] arg_NumEpoch = 50 arg_InfoInterval = 10 arg_SavePath = './checkpoints/softmax_MYO_' arg_SaveEpochInterbal = 5 # Data arg ########################################### arg_TrainDataPath = './data/data/data1/1' ########################################### arg_TrainBatchSize = 512 arg_TestBatchSize = 1024 arg_InputDim = 10 arg_FeatureDim = 2048 arg_classNum = 53 arg_DenseDim = 256 # Learning rate arg arg_BaseLr = 0.2 arg_Momentum = 0.5 arg_WeightDecay = 0.0005 # Learning rate scheduler arg_LrEpochStep = 10 arg_Gamma = 0.5 # Dataset Loading TrainLoader, TestLoader = Dataloader.LoadCSV(arg_TrainBatchSize, arg_TestBatchSize, arg_TrainDataPath, isbalanced=True) # Model Constructing # Inference Model Constructing Inference = MLP(input_dim=arg_InputDim, feature_dim=arg_FeatureDim, dense_dim=arg_DenseDim) # Innerproduct Construction # InnerProduct = torch.nn.Linear(arg_FeatureDim, arg_classNum) InnerProduct = VariantInnerProduct.NormalizedInnerProductWithScale(arg_FeatureDim, arg_classNum, scale=7.0) MetricLoss = EuclideanBasedLosses.RingLoss(loss_weight=1) Model = torch.nn.DataParallel(TrainingModel(Inference, InnerProduct, MetricLoss), arg_DeviceIds) # Losses and optimizers Defining # Softmax CrossEntropy SoftmaxLoss = nn.CrossEntropyLoss() if torch.cuda.is_available(): SoftmaxLoss = SoftmaxLoss.cuda() Model = Model.cuda() criterion = [SoftmaxLoss] # Optimzer Optimizer = torch.optim.SGD(Model.parameters(), lr=arg_BaseLr, momentum=arg_Momentum, weight_decay=arg_WeightDecay) # Learning rate Schedule LrScheduler = torch.optim.lr_scheduler.StepLR(Optimizer, arg_LrEpochStep, gamma=arg_Gamma) ################################################################################################ # Resume from a checkpoint/pertrain # Training models # Testing models Processing(arg_NumEpoch, LrScheduler, Optimizer, TrainLoader, TestLoader, Model, criterion, arg_InfoInterval, arg_SavePath)