Exemplo n.º 1
0
def main():
        # Get the data
    data_train = pd.read_csv('dataset/train.csv')
    data_test = pd.read_csv('dataset/test.csv')
        # Transforming and dividing features
    Id_test = data_test['PassengerId']
    selected_features = ['Pclass','Sex', 'Age', 'SibSp', 'Parch', 'Fare', 'Embarked']
    df_train, df_test = data_process.transform_features(data_train, data_test, selected_features)
    df_train, df_test = data_process.features_scaling(df_train, df_test, selected_features)
    X_train, Y_train, X_test, Y_test, test_X = data_process.split_data(df_train, df_test, selected_features)
        # Set parameters
    parameters = {}
    parameters['model_path'] = 'model/Titanic.ckpt'
    parameters['n_input'], parameters['n_features'] = X_train.shape
    parameters['n_hidden'] = 2
    parameters['hidden_dim'] = 40
    parameters['n_class'] = 1
    parameters['learning_rate'] = 0.01
    parameters['training_epochs'] = 15000
    parameters['visualize'] = False
    if ((len(argv) > 1 and argv[1] == '-v') or (len(argv) > 2 and argv[2] == '-v')):
        parameters['visualize'] = True

        # Get model & train
    titanic_model = model.make_model(parameters)
    if (len(argv) > 1 and argv[1] == '-n') or (len(argv) > 2 and argv[2] == '-n'):
        model.neural_network(X_train, Y_train, parameters, titanic_model, X_test, Y_test)
        # Print accuracy
    if os.path.isfile(parameters['model_path']) == True:
        accuracy_estimation.Accuracy(parameters, titanic_model, X_train, Y_train, X_test, Y_test)
        # Output the submission to estimation.csv
    if os.path.isfile(parameters['model_path']) == True:
        accuracy_estimation.Estimation(parameters, titanic_model, test_X, Id_test)
    else:
        print("\nNo model found, please create a new file named 'Titanic.ckpt' in a directory named 'model' and launch the programme with th folowing commande :\n'python3 main.py -n'\n")
Exemplo n.º 2
0
def run():
    X_train, Y_train, X_test, Y_test = load_dataset()

    print("SVM model...")
    SVM(X_train, Y_train, X_test, Y_test)
    print("Done SVM")

    print("Random forest model...")
    random_forest(X_train, Y_train, X_test, Y_test)
    print("Done random forest")

    print("Neural network model...")

    C = tf.constant(2, name='C')
    one_hot_matrix_train = tf.one_hot(Y_train, C, axis=0)
    one_hot_matrix_test = tf.one_hot(Y_test, C, axis=0)
    with tf.Session() as sess:
        one_hot_train = sess.run(one_hot_matrix_train)
        one_hot_test = sess.run(one_hot_matrix_test)

    Y_train = one_hot_train
    Y_test = one_hot_test
    X_train = X_train.T
    X_test = X_test.T

    weights = neural_network(X_train,
                             Y_train,
                             X_test,
                             Y_test, [18, 8, 2],
                             print_cost=True)

    return weights
def create_model():
    clf = neural_network()
    sgd = optimizers.SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
    model = clf.build(28, 28, 1, 10)
    model.compile(loss="categorical_crossentropy",
                  optimizer=sgd,
                  metrics=["accuracy"])
    return model
Exemplo n.º 4
0
def test_main(model_name):
    """
    main for testing mode
    """
    env = FrozenLake()
    model = neural_network(16, 16, 4, 0.01)
    model.load_model(model_name)

    env.render_wQ(get_q_values(model))
    state = env.reset()
    env.render()
    done = False
    for _ in range(100):
        time.sleep(0.5)
        action = np.argmax(model.predict(np.array([state])))
        next_state, _, done = env.step(action)
        state = next_state
        env.render()
        if done:
            break
Exemplo n.º 5
0
        test_label = test_data[:, 445:446]
    elif args.choice == 'permeability':
        test_label = test_data[:, 444:445] * 100
    else:
        raise NotImplementedError()
    valid_input = Variable(torch.FloatTensor(valid_data[:, 0:444]),
                           requires_grad=False)
    if args.choice == 'rejection':
        valid_label = valid_data[:, 445:446]
    elif args.choice == 'permeability':
        valid_label = valid_data[:, 444:445] * 100
    else:
        raise NotImplementedError()

    model = neural_network(choice=args.choice,
                           init='load',
                           data=args.data,
                           device='cpu')
    train_predict = model(train_input)
    test_predict = model(test_input)
    valid_predict = model(valid_input)
    if args.choice == 'permeability':
        train_predict *= 100
        test_predict *= 100
        valid_predict *= 100

    # the first figure
    plt.figure(1)
    plt.plot(train_label,
             train_label,
             color=color1,
             linestyle=":",
Exemplo n.º 6
0
def train_main(r_mode):
    """
    main for training mode
    """
    env = FrozenLake()
    model = neural_network(16, 16, 4, 0.01)
    memory = deque(maxlen=1000)
    memory = fill_memory(env, memory)
    epsilon = 1

    for eps in range(10000):
        state = env.reset()
        last_position = env.position

        already = np.empty((0, 16))
        already = np.append(already, np.array([state]), axis=0)

        done = False

        if r_mode == "weights":
            time.sleep(0.01)
            env.render_wQ(get_q_values(model))
        elif r_mode == "map":
            time.sleep(0.2)
            env.render()

        for _ in range(100):
            if np.random.rand() > epsilon:
                action = np.argmax(model.predict(np.array([state])))
            else:
                action = np.random.randint(0, 4, size=1)[0]

            next_state, reward, done = env.step(action)

            for _, item in enumerate(already):
                flag = True
                for e, elem in enumerate(next_state):
                    if item[e] != elem:
                        flag = False
                        break
                if flag:
                    reward -= 0.1
                break

            already = np.append(already, np.array([next_state]), axis=0)

            if epsilon > 0.1:
                epsilon -= 0.001

            if last_position == env.position:
                reward -= 0.1

            memory.append((state, action, reward, next_state, done))
            train(model, memory, 64, 0.9)

            if r_mode == "weights":
                time.sleep(0.01)
                env.render_wQ(get_q_values(model))
            elif r_mode == "map":
                time.sleep(0.2)
                env.render()

            state = next_state
            last_position = env.position

            if done:
                if test(env, eps, epsilon, model, r_mode):
                    model.save_model("model")
                    print("[SUCCESSFUL RUN]")
                    return
                break
Exemplo n.º 7
0
def main():

    # tf flag
    flags = tf.flags
    flags.DEFINE_string("data_path", "D:/M1_lecture/lecture/image_analysis/no_normalized_data.csv", "data path")
    flags.DEFINE_string("outdir", "D:/M1_lecture/lecture/image_analysis/no_norm", "outdir path")
    flags.DEFINE_string("gpu_index", "0", "GPU-index")
    flags.DEFINE_integer("num_epoch", 1000, "number of iteration")
    flags.DEFINE_integer("kfold", 10, "number of fold")
    flags.DEFINE_list("input_size", [22], "input vector size")
    flags.DEFINE_bool("cv", True, "if 10 fold CV or not")
    FLAGS = flags.FLAGS

    # load train data
    data = np.loadtxt(FLAGS.data_path, delimiter=",").astype(np.float32)
    feature = data[:, :data.shape[1]-1]
    label = data[:, data.shape[1]-1]

    # initializer
    init_op = tf.group(tf.initializers.global_variables(),
                       tf.initializers.local_variables())

    with tf.Session(config = utils.config(index=FLAGS.gpu_index)) as sess:
        # Resubstitution Method
        if FLAGS.cv == False:
            # set network
            kwargs = {
                'sess': sess,
                'input_size': FLAGS.input_size,
                'learning_rate': 1e-3
            }
            NN = neural_network(**kwargs)

            # print parmeters
            utils.cal_parameter()

            # check floder
            if not (os.path.exists(os.path.join(FLAGS.outdir, 'R', 'tensorboard'))):
                os.makedirs(os.path.join(FLAGS.outdir, 'R', 'tensorboard'))
            if not (os.path.exists(os.path.join(FLAGS.outdir, 'R', 'model'))):
                os.makedirs(os.path.join(FLAGS.outdir, 'R', 'model'))
            if not (os.path.exists(os.path.join(FLAGS.outdir, 'R', 'predict'))):
                os.makedirs(os.path.join(FLAGS.outdir, 'R', 'predict'))

            # prepare tensorboard
            writer_train = tf.summary.FileWriter(os.path.join(FLAGS.outdir, 'R', 'tensorboard', 'train'), sess.graph)
            writer_test = tf.summary.FileWriter(os.path.join(FLAGS.outdir, 'R', 'tensorboard', 'test'))
            value_loss = tf.Variable(0.0)
            tf.summary.scalar("loss", value_loss)
            merge_op = tf.summary.merge_all()

            # initialize
            sess.run(init_op)

            # training
            tbar = tqdm(range(FLAGS.num_epoch), ascii=True)
            for i in tbar:
                train_step, train_data = utils.batch_iter(feature, label, batch_size=feature.shape[0], shuffle=True)
                train_data_batch = next(train_data)

                train_loss = NN.update(train_data_batch[0], np.reshape(train_data_batch[1] , (train_data_batch[1].shape[0],1)))
                s = "Loss: {:.4f}".format(np.mean(train_loss))
                tbar.set_description(s)

                summary_train_loss = sess.run(merge_op, {value_loss: np.mean(train_loss)})
                writer_train.add_summary(summary_train_loss, i+1)

                NN.save_model(i+1, outdir=os.path.join(FLAGS.outdir, 'R'))

            # test
            test_loss_min = []
            sess.run(init_op)
            test_step, test_data = utils.batch_iter(feature, label, batch_size=feature.shape[0], shuffle=False)

            tbar = tqdm(range(FLAGS.num_epoch), ascii=True)
            for i in tbar:
                NN.restore_model(os.path.join(FLAGS.outdir, 'R', 'model', 'model_{}'.format(i+1)))
                test_data_batch = next(test_data)
                test_loss, predict = NN.test(test_data_batch[0], np.reshape(test_data_batch[1], (test_data_batch[1].shape[0], 1)))
                s = "Loss: {:.4f}".format(np.mean(test_loss))
                tbar.set_description(s)
                test_loss_min.append(np.mean(test_loss))

                summary_test_loss = sess.run(merge_op, {value_loss: np.mean(test_loss)})
                writer_test.add_summary(summary_test_loss, i+1)

                predict_label = np.zeros((test_data_batch[0].shape[0], 2))
                predict_label[:, 0] = test_data_batch[1]
                predict_label[:, 1] = np.where(predict > 0.5, 1, 0)[:, 0]
                np.savetxt(os.path.join(FLAGS.outdir, 'R', 'predict', 'result_{}.csv'.format(i + 1)),
                           predict_label.astype(np.int), delimiter=',', fmt="%d")

            test_loss_min = np.argmin(np.asarray(test_loss_min))
            np.savetxt(os.path.join(FLAGS.outdir, 'R' ,'min_loss_index.txt'), [test_loss_min+1], fmt="%d")

        # Leave-one-out method (10-fold CV)
        if FLAGS.cv == True:
            kfold = StratifiedKFold(n_splits=FLAGS.kfold, shuffle=True, random_state=1)
            fold_index = 0

            # set network
            kwargs = {
                'sess': sess,
                'input_size': FLAGS.input_size,
                'learning_rate': 1e-3
            }
            NN = neural_network(**kwargs)

            # print parmeters
            utils.cal_parameter()
            for train, test in kfold.split(feature, label):
                fold_index += 1

                # check folder
                if not (os.path.exists(os.path.join(FLAGS.outdir, 'L', str(fold_index) , 'tensorboard'))):
                    os.makedirs(os.path.join(FLAGS.outdir, 'L', str(fold_index), 'tensorboard'))
                if not (os.path.exists(os.path.join(FLAGS.outdir, 'L', str(fold_index), 'model'))):
                    os.makedirs(os.path.join(FLAGS.outdir, 'L', str(fold_index), 'model'))
                if not (os.path.exists(os.path.join(FLAGS.outdir, 'L', str(fold_index), 'predict'))):
                    os.makedirs(os.path.join(FLAGS.outdir, 'L', str(fold_index), 'predict'))

                # prepare tensorboard
                writer_train = tf.summary.FileWriter(os.path.join(FLAGS.outdir, 'L', str(fold_index), 'tensorboard', 'train'),
                                                     sess.graph)
                writer_test = tf.summary.FileWriter(os.path.join(FLAGS.outdir, 'L', str(fold_index), 'tensorboard', 'test'))
                value_loss = tf.Variable(0.0)
                tf.summary.scalar("loss", value_loss)
                merge_op = tf.summary.merge_all()

                # initialize
                sess.run(tf.global_variables_initializer())

                # training
                tbar = tqdm(range(FLAGS.num_epoch), ascii=True)
                for i in tbar:
                    train_step, train_data = utils.batch_iter(feature[train], label[train],
                                                              batch_size=feature[train].shape[0], shuffle=True)
                    train_data_batch = next(train_data)

                    train_loss = NN.update(train_data_batch[0],
                                           np.reshape(train_data_batch[1], (train_data_batch[1].shape[0], 1)))
                    s = "Loss: {:.4f}".format(np.mean(train_loss))
                    tbar.set_description(s)

                    summary_train_loss = sess.run(merge_op, {value_loss: np.mean(train_loss)})
                    writer_train.add_summary(summary_train_loss, i + 1)

                    NN.save_model(i + 1, outdir=os.path.join(FLAGS.outdir, 'L', str(fold_index)))

                # test
                sess.run(init_op)
                test_loss_min = []
                test_step, test_data = utils.batch_iter(feature[test], label[test],
                                                        batch_size=feature[test].shape[0], shuffle=False)
                tbar = tqdm(range(FLAGS.num_epoch), ascii=True)
                for i in tbar:
                    NN.restore_model(os.path.join(FLAGS.outdir, 'L', str(fold_index) , 'model', 'model_{}'.format(i + 1)))
                    test_data_batch = next(test_data)
                    test_loss, predict = NN.test(test_data_batch[0],
                                                 np.reshape(test_data_batch[1], (test_data_batch[1].shape[0], 1)))
                    s = "Loss: {:.4f}".format(np.mean(test_loss))
                    tbar.set_description(s)
                    test_loss_min.append(np.mean(test_loss))

                    summary_test_loss = sess.run(merge_op, {value_loss: np.mean(test_loss)})
                    writer_test.add_summary(summary_test_loss, i + 1)

                    predict_label = np.zeros((test_data_batch[0].shape[0], 2))
                    predict_label[:, 0] = test_data_batch[1]
                    predict_label[:, 1] = np.where(predict > 0.5, 1, 0)[:,0]
                    np.savetxt(os.path.join(FLAGS.outdir, 'L', str(fold_index), 'predict', 'result_{}.csv'.format(i + 1)),
                               predict_label.astype(np.int), delimiter=',', fmt="%d")

                test_loss_min = np.argmin(np.asarray(test_loss_min))
                np.savetxt(os.path.join(FLAGS.outdir, 'L', str(fold_index),'min_loss_index.txt'), [test_loss_min+1], fmt="%d")
Exemplo n.º 8
0
if args.choice == 'rejection':
    batch_size, epoch, lr = 200, 5, 1e-3
    regularization = 1e-2 if args.data[0:2] == '72' else 1e-1
    # manually pick the threshold to save the model parameters
    threshold = 0
elif args.choice == 'permeability':
    batch_size, epoch, lr = 200, 5, 5e-5
    # it is very likely to encounter overfitting when training permeability,
    # you can set regularization to a recommending range from 3e-1 to 3, .
    regularization = 0 if args.data[0:2] == '72' else 0
    # manually pick the threshold to save the model parameters
    threshold = -1


'''model structure'''
model = neural_network(choice=args.choice, init=args.init, data=args.data, device=args.device)
if args.init == 'start' and args.manual_init:
    model.apply(weight_init)
if args.optim == 'adam':
    optimizer = optim.Adam(model.parameters(), lr=lr, weight_decay=regularization)
elif args.optim == 'sgd':
    optimizer = optim.SGD(model.parameters(), lr=lr, weight_decay=regularization, momentum=0.8)
else:
    raise NotImplementedError("Please select between adam and sgd")
# learning_rate decay
if args.choice == 'rejection':
    scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=50, gamma=0.95)
elif args.choice == 'permeability':
    scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=50, gamma=0.95)

criterion = nn.MSELoss(reduction='mean')
Exemplo n.º 9
0
     mkdir(combination_folder)
 training_set = [X_word]
 right_set = [R_word]
 # approximate_set = [A_word]
 wrong_set = [W_word]
 if pos_tag:
     training_set += [X_pos]
     right_set += [R_pos]
     # approximate_set += [A_pos]
     wrong_set += [W_pos]
 if offset:
     training_set += [X_d1, X_d2]
     right_set += [R_d1, R_d2]
     # approximate_set += [A_d1, A_d2]
     wrong_set += [W_d1, W_d2]
 model = neural_network(dim, lstm_units, dropout, r_dropout, pos_tag,
                        offset)
 history = model.fit(training_set,
                     Y_train,
                     validation_split=0.15,
                     batch_size=batch_size,
                     epochs=epochs,
                     verbose=2)
 plot(
     combination_folder, 'loss_accuracy_' +
     datetime.datetime.now().strftime("%Y%m%d-%H%M%S"), history)
 total_right_labels, right_predictions = generate_predictions(
     model, right_set, right_labels,
     generate_negative_labels(right_negative))
 '''total_approximate_labels, approximate_predictions = generate_predictions(model, approximate_set, approximate_labels,
                                                                    generate_negative_labels(
                                                                        approximate_negative))'''
Exemplo n.º 10
0
complete_test = np.zeros((X_test.shape[0],n_class))

# Training set
X_train = train_data['X_train']
y_train = train_data['y_train']
mask_train = train_data['mask_train']
partition = train_data['partition']

# Number of features
n_feat = np.shape(X_test)[2]

# Training
for i in range(1,5):
	# Network compilation
	print("Compilation model {}\n".format(i))
	train_fn, val_fn, network_out = neural_network(batch_size, n_hid, n_feat, n_class, lr, drop_per, drop_hid, n_filt)
	
	# Train and validation sets
	train_index = np.where(partition != i)
	val_index = np.where(partition == i)
	X_tr = X_train[train_index].astype(np.float32)
	X_val = X_train[val_index].astype(np.float32)
	y_tr = y_train[train_index].astype(np.int32)
	y_val = y_train[val_index].astype(np.int32)
	mask_tr = mask_train[train_index].astype(np.float32)
	mask_val = mask_train[val_index].astype(np.float32)

	print("Validation shape: {}".format(X_val.shape))
	print("Training shape: {}".format(X_tr.shape))
	
	eps = []
Exemplo n.º 11
0
# Observe how increasing the size and number of layers
# results in overfitting on the dataset

# UNCOMMENT THE NEXT THREE LINES AND FILL IN YOUR HYPERPARAMETERS
# no_epochs = ?
# lr = ?
# layer_sizes = [input_dim, hidden_1, hidden_2, ..., no_classes] # list specifying network architecture

####################################################
# MODIFY HYPERPARAMETERS (END)
####################################################

no_layers = len(layer_sizes) + 1

# create a neural network model
model_nn = model.neural_network(layer_sizes)

begin_time = time.time()

# The data is a 2D spiral. So the input is a 2 dimensional point, and they are classified into three
# classes
# data taken from http://cs231n.github.io/neural-networks-case-study/

# load the data
X_train = np.load('train_X.npy')
Y_train = np.load('train_Y.npy')

X_val = np.load('val_X.npy')
Y_val = np.load('val_Y.npy')

# make the dimensions of data as [no_features x batch_size]
Exemplo n.º 12
0
    x[ix] = oldval # restore

    # compute the partial derivative with centered formula
    grad[ix] = (fxph - fxmh) / (2 * h) # the slope
    if verbose:
      print(ix, grad[ix])
    it.iternext() # step to next dimension

  return grad

# define an arbitrary neural network
layer_sizes = [2, 4, 2, 4, 5]
no_layer = len(layer_sizes) - 1
batch_size = 16

model_nn = model.neural_network(layer_sizes)

# create a single sample example
X = np.random.randn(layer_sizes[0], batch_size)
Y = np.random.randint(0, layer_sizes[-1], batch_size)

# analytical evaluation
Y_hat, loss = model_nn.forward(X, Y)
model_nn.compute_gradients()

# numerical evaluation
grad_W = [None] * (no_layer+1)
grad_b = [None] * (no_layer+1)

f = lambda W: model_nn.forward(X, Y)[1]