Ejemplo n.º 1
0
def train():
    #initial learning rate
    initial_learning_rate = log_uniform(INITIAL_ALPHA_LOW, INITIAL_ALPHA_HIGH,
                                        INITIAL_ALPHA_LOG_RATE)

    # parameter server and worker information
    ps_hosts = np.zeros(FLAGS.ps_hosts_num, dtype=object)
    worker_hosts = np.zeros(FLAGS.worker_hosts_num, dtype=object)
    port_num = FLAGS.st_port_num
    for i in range(FLAGS.ps_hosts_num):
        ps_hosts[i] = str(FLAGS.hostname) + ":" + str(port_num)
        port_num += 1
    for i in range(FLAGS.worker_hosts_num):
        worker_hosts[i] = str(FLAGS.hostname) + ":" + str(port_num)
        port_num += 1
    ps_hosts = list(ps_hosts)
    worker_hosts = list(worker_hosts)
    # Create a cluster from the parameter server and worker hosts.
    cluster = tf.train.ClusterSpec({"ps": ps_hosts, "worker": worker_hosts})

    # Create and start a server for the local task.
    server = tf.train.Server(cluster,
                             job_name=FLAGS.job_name,
                             task_index=FLAGS.task_index)

    if FLAGS.job_name == "ps":
        server.join()
    elif FLAGS.job_name == "worker":
        device = tf.train.replica_device_setter(
            worker_device="/job:worker/task:%d" % FLAGS.task_index,
            cluster=cluster)

        learning_rate_input = tf.placeholder("float")

        grad_applier = RMSPropApplier(learning_rate=learning_rate_input,
                                      decay=RMSP_ALPHA,
                                      momentum=0.0,
                                      epsilon=RMSP_EPSILON,
                                      clip_norm=GRAD_NORM_CLIP,
                                      device=device)

        tf.set_random_seed(1)
        #There are no global network
        training_thread = A3CTrainingThread(0,
                                            "",
                                            initial_learning_rate,
                                            learning_rate_input,
                                            grad_applier,
                                            MAX_TIME_STEP,
                                            device=device,
                                            FLAGS=FLAGS,
                                            task_index=FLAGS.task_index)

        # prepare session
        with tf.device(
                tf.train.replica_device_setter(
                    worker_device="/job:worker/task:%d" % FLAGS.task_index,
                    cluster=cluster)):
            # flag for task
            flag = tf.get_variable('flag', [],
                                   initializer=tf.constant_initializer(0),
                                   trainable=False)
            flag_ph = tf.placeholder(flag.dtype, shape=flag.get_shape())
            flag_ops = flag.assign(flag_ph)
            # global step
            global_step = tf.get_variable(
                'global_step', [],
                initializer=tf.constant_initializer(0),
                trainable=False)
            global_step_ph = tf.placeholder(global_step.dtype,
                                            shape=global_step.get_shape())
            global_step_ops = global_step.assign(global_step_ph)
            # score for tensorboard and score_set for genetic algorithm
            score = tf.get_variable('score', [],
                                    initializer=tf.constant_initializer(-21),
                                    trainable=False)
            score_ph = tf.placeholder(score.dtype, shape=score.get_shape())
            score_ops = score.assign(score_ph)
            score_set = np.zeros(FLAGS.worker_hosts_num, dtype=object)
            score_set_ph = np.zeros(FLAGS.worker_hosts_num, dtype=object)
            score_set_ops = np.zeros(FLAGS.worker_hosts_num, dtype=object)
            for i in range(FLAGS.worker_hosts_num):
                score_set[i] = tf.get_variable(
                    'score' + str(i), [],
                    initializer=tf.constant_initializer(-1000),
                    trainable=False)
                score_set_ph[i] = tf.placeholder(
                    score_set[i].dtype, shape=score_set[i].get_shape())
                score_set_ops[i] = score_set[i].assign(score_set_ph[i])
            # fixed path of earlier task
            fixed_path_tf = np.zeros((FLAGS.L, FLAGS.M), dtype=object)
            fixed_path_ph = np.zeros((FLAGS.L, FLAGS.M), dtype=object)
            fixed_path_ops = np.zeros((FLAGS.L, FLAGS.M), dtype=object)
            for i in range(FLAGS.L):
                for j in range(FLAGS.M):
                    fixed_path_tf[i, j] = tf.get_variable(
                        'fixed_path' + str(i) + "-" + str(j), [],
                        initializer=tf.constant_initializer(0),
                        trainable=False)
                    fixed_path_ph[i, j] = tf.placeholder(
                        fixed_path_tf[i, j].dtype,
                        shape=fixed_path_tf[i, j].get_shape())
                    fixed_path_ops[i, j] = fixed_path_tf[i, j].assign(
                        fixed_path_ph[i, j])
            # parameters on PathNet
            vars_ = training_thread.local_network.get_vars()
            vars_ph = np.zeros(len(vars_), dtype=object)
            vars_ops = np.zeros(len(vars_), dtype=object)
            for i in range(len(vars_)):
                vars_ph[i] = tf.placeholder(vars_[i].dtype,
                                            shape=vars_[i].get_shape())
                vars_ops[i] = vars_[i].assign(vars_ph[i])

            # initialization
            init_op = tf.global_variables_initializer()
            # summary for tensorboard
            tf.summary.scalar("score", score)
            summary_op = tf.summary.merge_all()
            saver = tf.train.Saver()

        sv = tf.train.Supervisor(is_chief=(FLAGS.task_index == 0),
                                 global_step=global_step,
                                 logdir=FLAGS.log_dir,
                                 summary_op=summary_op,
                                 saver=saver,
                                 init_op=init_op)

        with sv.managed_session(server.target) as sess:
            if (FLAGS.task_index != (FLAGS.worker_hosts_num - 1)):
                for task in range(2):
                    while True:
                        if (sess.run([flag])[0] == (task + 1)):
                            break
                            time.sleep(2)
                    # Set fixed_path
                    fixed_path = np.zeros((FLAGS.L, FLAGS.M), dtype=float)
                    for i in range(FLAGS.L):
                        for j in range(FLAGS.M):
                            if (sess.run([fixed_path_tf[i, j]])[0] == 1):
                                fixed_path[i, j] = 1.0
                    training_thread.local_network.set_fixed_path(fixed_path)
                    # set start_time
                    wall_t = 0.0
                    start_time = time.time() - wall_t
                    training_thread.set_start_time(start_time)
                    while True:
                        if sess.run([global_step])[0] > (MAX_TIME_STEP *
                                                         (task + 1)):
                            break
                        diff_global_t = training_thread.process(
                            sess,
                            sess.run([global_step])[0], "", summary_op, "",
                            score_ph, score_ops, "", FLAGS,
                            score_set_ph[FLAGS.task_index],
                            score_set_ops[FLAGS.task_index],
                            score_set[FLAGS.task_index])
                        sess.run(
                            global_step_ops, {
                                global_step_ph:
                                sess.run([global_step])[0] + diff_global_t
                            })
            else:
                fixed_path = np.zeros((FLAGS.L, FLAGS.M), dtype=float)
                vars_backup = np.zeros(len(vars_), dtype=object)
                vars_backup = sess.run(vars_)
                winner_idx = 0
                for task in range(2):
                    # Generating randomly geopath
                    geopath_set = np.zeros(FLAGS.worker_hosts_num - 1,
                                           dtype=object)
                    for i in range(FLAGS.worker_hosts_num - 1):
                        geopath_set[i] = pathnet.get_geopath(
                            FLAGS.L, FLAGS.M, FLAGS.N)
                        tmp = np.zeros((FLAGS.L, FLAGS.M), dtype=float)
                        for j in range(FLAGS.L):
                            for k in range(FLAGS.M):
                                if ((geopath_set[i][j, k] == 1.0)
                                        or (fixed_path[j, k] == 1.0)):
                                    tmp[j, k] = 1.0
                        pathnet.geopath_insert(
                            sess, training_thread.local_network.
                            geopath_update_placeholders_set[i],
                            training_thread.local_network.
                            geopath_update_ops_set[i], tmp, FLAGS.L, FLAGS.M)
                    print("Geopath Setting Done")
                    sess.run(flag_ops, {flag_ph: (task + 1)})
                    print("=============Task" + str(task + 1) + "============")
                    score_subset = np.zeros(FLAGS.B, dtype=float)
                    score_set_print = np.zeros(FLAGS.worker_hosts_num,
                                               dtype=float)
                    rand_idx = range(FLAGS.worker_hosts_num - 1)
                    np.random.shuffle(rand_idx)
                    rand_idx = rand_idx[:FLAGS.B]
                    while True:
                        if sess.run([global_step])[0] > (MAX_TIME_STEP *
                                                         (task + 1)):
                            break
                        flag_sum = 0
                        for i in range(FLAGS.worker_hosts_num - 1):
                            score_set_print[i] = sess.run([score_set[i]])[0]
                        print(score_set_print)
                        for i in range(len(rand_idx)):
                            score_subset[i] = sess.run(
                                [score_set[rand_idx[i]]])[0]
                            if (score_subset[i] == -1000):
                                flag_sum = 1
                                break
                        if (flag_sum == 0):
                            winner_idx = rand_idx[np.argmax(score_subset)]
                            print(
                                str(sess.run([global_step])[0]) +
                                " Step Score: " +
                                str(sess.run([score_set[winner_idx]])[0]))
                            for i in rand_idx:
                                if (i != winner_idx):
                                    geopath_set[i] = np.copy(
                                        geopath_set[winner_idx])
                                    geopath_set[i] = pathnet.mutation(
                                        geopath_set[i], FLAGS.L, FLAGS.M,
                                        FLAGS.N)
                                    tmp = np.zeros((FLAGS.L, FLAGS.M),
                                                   dtype=float)
                                    for j in range(FLAGS.L):
                                        for k in range(FLAGS.M):
                                            if ((geopath_set[i][j, k] == 1.0)
                                                    or
                                                (fixed_path[j, k] == 1.0)):
                                                tmp[j, k] = 1.0
                                    pathnet.geopath_insert(
                                        sess, training_thread.local_network.
                                        geopath_update_placeholders_set[i],
                                        training_thread.local_network.
                                        geopath_update_ops_set[i], tmp,
                                        FLAGS.L, FLAGS.M)
                                    sess.run(score_set_ops[i],
                                             {score_set_ph[i]: -1000})
                            rand_idx = range(FLAGS.worker_hosts_num - 1)
                            np.random.shuffle(rand_idx)
                            rand_idx = rand_idx[:FLAGS.B]
                        else:
                            time.sleep(5)
                    # fixed_path setting
                    fixed_path = geopath_set[winner_idx]
                    for i in range(FLAGS.L):
                        for j in range(FLAGS.M):
                            if (fixed_path[i, j] == 1.0):
                                sess.run(fixed_path_ops[i, j],
                                         {fixed_path_ph[i, j]: 1})
                    training_thread.local_network.set_fixed_path(fixed_path)
                    # initialization of parameters except fixed_path
                    vars_idx = training_thread.local_network.get_vars_idx()
                    for i in range(len(vars_idx)):
                        if (vars_idx[i] == 1.0):
                            sess.run(vars_ops[i], {vars_ph[i]: vars_backup[i]})
        sv.stop()
        print("Done")
Ejemplo n.º 2
0
def train():
    # Get SVHN dataset
    svhn_maybe_download_and_extract()
    file_name = os.path.join(FLAGS.svhn_data_dir, "train_32x32.mat")
    train = sio.loadmat(file_name)
    tr_data_svhn = np.zeros((len(train['y']), 32 * 32 * 3), dtype=float)
    tr_label_svhn = np.zeros((len(train['y']), 10), dtype=float)
    for i in range(len(train['y'])):
        tr_data_svhn[i] = np.reshape(train['X'][:, :, :, i], [1, 32 * 32 * 3])
        tr_label_svhn[i, train['y'][i][0] - 1] = 1.0
    tr_data_svhn = tr_data_svhn / 255.0

    file_name = os.path.join(FLAGS.svhn_data_dir, "test_32x32.mat")
    test = sio.loadmat(file_name)
    ts_data_svhn = np.zeros((len(test['y']), 32 * 32 * 3), dtype=float)
    ts_label_svhn = np.zeros((len(test['y']), 10), dtype=float)
    for i in range(len(test['y'])):
        ts_data_svhn[i] = np.reshape(test['X'][:, :, :, i], [1, 32 * 32 * 3])
        ts_label_svhn[i, test['y'][i][0] - 1] = 1.0
    ts_data_svhn = ts_data_svhn / 255.0
    data_num_len_svhn = len(tr_label_svhn)

    # Get CIFAR 10  dataset
    cifar10.maybe_download_and_extract()
    tr_label_cifar10 = np.zeros((50000, 10), dtype=float)
    ts_label_cifar10 = np.zeros((10000, 10), dtype=float)
    for i in range(1, 6):
        file_name = os.path.join(FLAGS.cifar_data_dir,
                                 "data_batch_" + str(i) + ".bin")
        f = open(file_name, "rb")
        data = np.reshape(bytearray(f.read()), [10000, 3073])
        if (i == 1):
            tr_data_cifar10 = data[:, 1:] / 255.0
        else:
            tr_data_cifar10 = np.append(tr_data_cifar10,
                                        data[:, 1:] / 255.0,
                                        axis=0)
        for j in range(len(data)):
            tr_label_cifar10[(i - 1) * 10000 + j, data[j, 0]] = 1.0
    file_name = os.path.join(FLAGS.cifar_data_dir, "test_batch.bin")
    f = open(file_name, "rb")
    data = np.reshape(bytearray(f.read()), [10000, 3073])
    for i in range(len(data)):
        ts_label_cifar10[i, data[i, 0]] = 1.0
    ts_data_cifar10 = data[:, 1:] / 255.0
    data_num_len_cifar10 = len(tr_label_cifar10)

    print(ts_label_cifar10.shape)
    print(ts_label_cifar10[0])
    if (FLAGS.cifar_first):
        tr_data1 = tr_data_cifar10
        tr_label1 = tr_label_cifar10
        ts_data1 = ts_data_cifar10
        ts_label1 = ts_label_cifar10
        data_num_len1 = data_num_len_cifar10
        tr_data2 = tr_data_svhn
        tr_label2 = tr_label_svhn
        ts_data2 = ts_data_svhn
        ts_label2 = ts_label_svhn
        data_num_len2 = data_num_len_svhn
    else:
        tr_data1 = tr_data_svhn
        tr_label1 = tr_label_svhn
        ts_data1 = ts_data_svhn
        ts_label1 = ts_label_svhn
        data_num_len1 = data_num_len_svhn
        tr_data2 = tr_data_cifar10
        tr_label2 = tr_label_cifar10
        ts_data2 = ts_data_cifar10
        ts_label2 = ts_label_cifar10
        data_num_len2 = data_num_len_cifar10

    ## TASK 1
    sess = tf.InteractiveSession()

    # Input placeholders
    with tf.name_scope('input'):
        x = tf.placeholder(tf.float32, [None, 32 * 32 * 3], name='x-input')
        y_ = tf.placeholder(tf.float32, [None, 10], name='y-input')

    with tf.name_scope('input_reshape'):
        image_shaped_input = tf.reshape(x, [-1, 32, 32, 3])
        tf.summary.image('input', image_shaped_input, 2)

    # geopath_examples
    geopath = pathnet.geopath_initializer(FLAGS.L, FLAGS.M)

    # fixed weights list
    fixed_list = np.ones((FLAGS.L, FLAGS.M), dtype=str)
    for i in range(FLAGS.L):
        for j in range(FLAGS.M):
            fixed_list[i, j] = '0'

    # Hidden Layers
    weights_list = np.zeros((FLAGS.L, FLAGS.M), dtype=object)
    biases_list = np.zeros((FLAGS.L, FLAGS.M), dtype=object)

    # model define
    layer_modules_list = np.zeros(FLAGS.M, dtype=object)
    # conv layer
    i = 0
    for j in range(FLAGS.M):
        layer_modules_list[j], weights_list[i, j], biases_list[
            i,
            j] = pathnet.conv_module(image_shaped_input, FLAGS.filt, [5, 5],
                                     geopath[i, j], 1,
                                     'layer' + str(i + 1) + "_" + str(j + 1))
    net = np.sum(layer_modules_list) / FLAGS.M
    # res-fire layer
    i = 1
    for j in range(FLAGS.M):
        layer_modules_list[j], weights_list[i, j], biases_list[
            i, j] = pathnet.res_fire_layer(
                net, FLAGS.filt, 10, 10, geopath[i, j],
                'layer' + str(i + 1) + "_" + str(j + 1))
    net = np.sum(layer_modules_list) / FLAGS.M
    # dimensionality_reduction layer
    i = 2
    for j in range(FLAGS.M):
        layer_modules_list[j], weights_list[i, j], biases_list[
            i, j] = pathnet.Dimensionality_reduction_module(
                net, 10, geopath[i, j],
                'layer' + str(i + 1) + "_" + str(j + 1))
    net = np.sum(layer_modules_list) / FLAGS.M
    # conv layer
    i = 3
    for j in range(FLAGS.M):
        layer_modules_list[j], weights_list[i, j], biases_list[
            i,
            j] = pathnet.conv_module(image_shaped_input, FLAGS.filt, [5, 5],
                                     geopath[i, j], 1,
                                     'layer' + str(i + 1) + "_" + str(j + 1))
    net = np.sum(layer_modules_list) / FLAGS.M
    # output layer
    # reshape
    _shape = net.shape[1:]
    _length = 1
    for _i in _shape:
        _length *= int(_i)
    net = tf.reshape(net, [-1, _length])
    # full connection layer
    y, output_weights, output_biases = pathnet.nn_layer(
        net, 10, 'output_layer')

    # Cross Entropy
    with tf.name_scope('cross_entropy'):
        diff = tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y)
        with tf.name_scope('total'):
            cross_entropy = tf.reduce_mean(diff)
    tf.summary.scalar('cross_entropy', cross_entropy)

    # Need to learn variables
    var_list_to_learn = [] + output_weights + output_biases
    for i in range(FLAGS.L):
        for j in range(FLAGS.M):
            if (fixed_list[i, j] == '0'):
                var_list_to_learn += weights_list[i, j] + biases_list[i, j]

    # GradientDescent
    with tf.name_scope('train'):
        train_step = tf.train.GradientDescentOptimizer(
            FLAGS.learning_rate).minimize(cross_entropy,
                                          var_list=var_list_to_learn)

    # Accuracy
    with tf.name_scope('accuracy'):
        with tf.name_scope('correct_prediction'):
            correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
        with tf.name_scope('accuracy'):
            accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    tf.summary.scalar('accuracy', accuracy)

    # Merge all the summaries and write them out to /tmp/tensorflow/mnist/logs/mnist_with_summaries (by default)
    merged = tf.summary.merge_all()
    train_writer = tf.summary.FileWriter(FLAGS.log_dir + '/train1', sess.graph)
    test_writer = tf.summary.FileWriter(FLAGS.log_dir + '/test1')
    tf.global_variables_initializer().run()

    # Generating randomly geopath
    geopath_set = np.zeros(FLAGS.candi, dtype=object)
    for i in range(FLAGS.candi):
        geopath_set[i] = pathnet.get_geopath(FLAGS.L, FLAGS.M, FLAGS.N)

    # parameters placeholders and ops
    var_update_ops = np.zeros(len(var_list_to_learn), dtype=object)
    var_update_placeholders = np.zeros(len(var_list_to_learn), dtype=object)
    for i in range(len(var_list_to_learn)):
        var_update_placeholders[i] = tf.placeholder(
            var_list_to_learn[i].dtype, shape=var_list_to_learn[i].get_shape())
        var_update_ops[i] = var_list_to_learn[i].assign(
            var_update_placeholders[i])

    # geopathes placeholders and ops
    geopath_update_ops = np.zeros((len(geopath), len(geopath[0])),
                                  dtype=object)
    geopath_update_placeholders = np.zeros((len(geopath), len(geopath[0])),
                                           dtype=object)
    for i in range(len(geopath)):
        for j in range(len(geopath[0])):
            geopath_update_placeholders[i, j] = tf.placeholder(
                geopath[i, j].dtype, shape=geopath[i, j].get_shape())
            geopath_update_ops[i, j] = geopath[i, j].assign(
                geopath_update_placeholders[i, j])

    acc_geo = np.zeros(FLAGS.B, dtype=float)
    summary_geo = np.zeros(FLAGS.B, dtype=object)
    for i in range(FLAGS.max_steps):
        # Select Candidates to Tournament
        compet_idx = range(FLAGS.candi)
        np.random.shuffle(compet_idx)
        compet_idx = compet_idx[:FLAGS.B]
        # Learning & Evaluating
        for j in range(len(compet_idx)):
            # Shuffle the data
            idx = range(len(tr_data1))
            np.random.shuffle(idx)
            tr_data1 = tr_data1[idx]
            tr_label1 = tr_label1[idx]
            # Insert Candidate
            pathnet.geopath_insert(sess, geopath_update_placeholders,
                                   geopath_update_ops,
                                   geopath_set[compet_idx[j]], FLAGS.L,
                                   FLAGS.M)
            acc_geo_tr = 0
            for k in range(FLAGS.T):
                '''
        print(x.shape)
        print(tr_data1[k*FLAGS.batch_num:(k+1)*FLAGS.batch_num,:].shape)
        print(y.shape)
        print(tr_label1[k*FLAGS.batch_num:(k+1)*FLAGS.batch_num,:].shape)
        '''
                summary_geo_tr, _, acc_geo_tmp = sess.run(
                    [merged, train_step, accuracy],
                    feed_dict={
                        x:
                        tr_data1[k * FLAGS.batch_num:(k + 1) *
                                 FLAGS.batch_num, :],
                        y_:
                        tr_label1[k * FLAGS.batch_num:(k + 1) *
                                  FLAGS.batch_num, :]
                    })
                acc_geo_tr += acc_geo_tmp
            acc_geo[j] = acc_geo_tr / FLAGS.T
            summary_geo[j] = summary_geo_tr
        # Tournament
        winner_idx = np.argmax(acc_geo)
        acc = acc_geo[winner_idx]
        summary = summary_geo[winner_idx]
        # Copy and Mutation
        for j in range(len(compet_idx)):
            if (j != winner_idx):
                geopath_set[compet_idx[j]] = np.copy(
                    geopath_set[compet_idx[winner_idx]])
                geopath_set[compet_idx[j]] = pathnet.mutation(
                    geopath_set[compet_idx[j]], FLAGS.L, FLAGS.M, FLAGS.N)
        train_writer.add_summary(summary, i)
        print('Training Accuracy at step %s: %s' % (i, acc))

    acc_task1 = acc
    task1_optimal_path = geopath_set[compet_idx[winner_idx]]

    # Fix task1 Optimal Path
    for i in range(FLAGS.L):
        for j in range(FLAGS.M):
            if (task1_optimal_path[i, j] == 1.0):
                fixed_list[i, j] = '1'

    # Get variables of fixed list
    var_list_to_fix = []
    #var_list_to_fix=[]+output_weights+output_biases;
    for i in range(FLAGS.L):
        for j in range(FLAGS.M):
            if (fixed_list[i, j] == '1'):
                var_list_to_fix += weights_list[i, j] + biases_list[i, j]
    var_list_fix = pathnet.parameters_backup(var_list_to_fix)

    # parameters placeholders and ops
    var_fix_ops = np.zeros(len(var_list_to_fix), dtype=object)
    var_fix_placeholders = np.zeros(len(var_list_to_fix), dtype=object)
    for i in range(len(var_list_to_fix)):
        var_fix_placeholders[i] = tf.placeholder(
            var_list_to_fix[i].dtype, shape=var_list_to_fix[i].get_shape())
        var_fix_ops[i] = var_list_to_fix[i].assign(var_fix_placeholders[i])

    ## TASK 2
    # Need to learn variables
    var_list_to_learn = [] + output_weights + output_biases
    for i in range(FLAGS.L):
        for j in range(FLAGS.M):
            if (fixed_list[i, j] == '0'):
                var_list_to_learn += weights_list[i, j] + biases_list[i, j]

    for i in range(FLAGS.L):
        for j in range(FLAGS.M):
            if (fixed_list[i, j] == '1'):
                tmp = biases_list[i, j][0]
                break
        break

    # Initialization
    merged = tf.summary.merge_all()
    train_writer = tf.summary.FileWriter(FLAGS.log_dir + '/train2', sess.graph)
    test_writer = tf.summary.FileWriter(FLAGS.log_dir + '/test2')
    tf.global_variables_initializer().run()

    # Update fixed values
    pathnet.parameters_update(sess, var_fix_placeholders, var_fix_ops,
                              var_list_fix)

    # GradientDescent
    with tf.name_scope('train'):
        train_step = tf.train.GradientDescentOptimizer(
            FLAGS.learning_rate).minimize(cross_entropy,
                                          var_list=var_list_to_learn)

    # Generating randomly geopath
    geopath_set = np.zeros(FLAGS.candi, dtype=object)
    for i in range(FLAGS.candi):
        geopath_set[i] = pathnet.get_geopath(FLAGS.L, FLAGS.M, FLAGS.N)

    # parameters placeholders and ops
    var_update_ops = np.zeros(len(var_list_to_learn), dtype=object)
    var_update_placeholders = np.zeros(len(var_list_to_learn), dtype=object)
    for i in range(len(var_list_to_learn)):
        var_update_placeholders[i] = tf.placeholder(
            var_list_to_learn[i].dtype, shape=var_list_to_learn[i].get_shape())
        var_update_ops[i] = var_list_to_learn[i].assign(
            var_update_placeholders[i])

    acc_geo = np.zeros(FLAGS.B, dtype=float)
    summary_geo = np.zeros(FLAGS.B, dtype=object)
    for i in range(FLAGS.max_steps):
        # Select Candidates to Tournament
        compet_idx = range(FLAGS.candi)
        np.random.shuffle(compet_idx)
        compet_idx = compet_idx[:FLAGS.B]
        # Learning & Evaluating
        for j in range(len(compet_idx)):
            # Shuffle the data
            idx = range(len(tr_data2))
            np.random.shuffle(idx)
            tr_data2 = tr_data2[idx]
            tr_label2 = tr_label2[idx]
            geopath_insert = np.copy(geopath_set[compet_idx[j]])

            for l in range(FLAGS.L):
                for m in range(FLAGS.M):
                    if (fixed_list[l, m] == '1'):
                        geopath_insert[l, m] = 1.0

            # Insert Candidate
            pathnet.geopath_insert(sess, geopath_update_placeholders,
                                   geopath_update_ops, geopath_insert, FLAGS.L,
                                   FLAGS.M)
            acc_geo_tr = 0
            for k in range(FLAGS.T):
                summary_geo_tr, _, acc_geo_tmp = sess.run(
                    [merged, train_step, accuracy],
                    feed_dict={
                        x:
                        tr_data2[k * FLAGS.batch_num:(k + 1) *
                                 FLAGS.batch_num, :],
                        y_:
                        tr_label2[k * FLAGS.batch_num:(k + 1) *
                                  FLAGS.batch_num, :]
                    })
                acc_geo_tr += acc_geo_tmp
            acc_geo[j] = acc_geo_tr / FLAGS.T
            summary_geo[j] = summary_geo_tr
        # Tournament
        winner_idx = np.argmax(acc_geo)
        acc = acc_geo[winner_idx]
        summary = summary_geo[winner_idx]
        # Copy and Mutation
        for j in range(len(compet_idx)):
            if (j != winner_idx):
                geopath_set[compet_idx[j]] = np.copy(
                    geopath_set[compet_idx[winner_idx]])
                geopath_set[compet_idx[j]] = pathnet.mutation(
                    geopath_set[compet_idx[j]], FLAGS.L, FLAGS.M, FLAGS.N)
        train_writer.add_summary(summary, i)
        print('Training Accuracy at step %s: %s' % (i, acc))

    acc_task2 = acc

    if (FLAGS.cifar_first):
        print("CIFAR10_SVHN,TASK1:" + str(acc_task1) + ",TASK2:" +
              str(acc_task2) + ",Done")
    else:
        print("SVHN_CIFAR10,TASK1:" + str(acc_task1) + ",TASK2:" +
              str(acc_task2) + ",Done")

    train_writer.close()
    test_writer.close()
Ejemplo n.º 3
0
def train():
    # Get SVHN dataset
    svhn_maybe_download_and_extract()
    file_name = os.path.join(FLAGS.svhn_data_dir, "train_32x32.mat")
    train = sio.loadmat(file_name)
    tr_data_svhn = np.zeros((len(train['y']), 32 * 32 * 3), dtype=float)
    tr_label_svhn = np.zeros((len(train['y']), 10), dtype=float)
    for i in range(len(train['y'])):
        tr_data_svhn[i] = np.reshape(train['X'][:, :, :, i], [1, 32 * 32 * 3])
        tr_label_svhn[i, train['y'][i][0] - 1] = 1.0
    tr_data_svhn = tr_data_svhn / 255.0
    tr_label_svhn = np.zeros((len(train['y']), 10), dtype=float)

    file_name = os.path.join(FLAGS.svhn_data_dir, "test_32x32.mat")
    test = sio.loadmat(file_name)
    ts_data_svhn = np.zeros((len(test['y']), 32 * 32 * 3), dtype=float)
    ts_label_svhn = np.zeros((len(test['y']), 10), dtype=float)
    for i in range(len(test['y'])):
        ts_data_svhn[i] = np.reshape(test['X'][:, :, :, i], [1, 32 * 32 * 3])
        ts_label_svhn[i, test['y'][i][0] - 1] = 1.0
    ts_data_svhn = ts_data_svhn / 255.0
    data_num_len_svhn = len(tr_label_svhn)

    # Get CIFAR 10  dataset
    cifar10.maybe_download_and_extract()
    tr_label_cifar10 = np.zeros((50000, 10), dtype=float)
    ts_label_cifar10 = np.zeros((10000, 10), dtype=float)
    for i in range(1, 6):
        file_name = os.path.join(FLAGS.cifar_data_dir,
                                 "data_batch_" + str(i) + ".bin")
        f = open(file_name, "rb")
        data = np.reshape(bytearray(f.read()), [10000, 3073])
        if (i == 1):
            tr_data_cifar10 = data[:, 1:] / 255.0
        else:
            tr_data_cifar10 = np.append(tr_data_cifar10,
                                        data[:, 1:] / 255.0,
                                        axis=0)
        for j in range(len(data)):
            tr_label_cifar10[(i - 1) * 10000 + j, data[j, 0]] = 1.0
    file_name = os.path.join(FLAGS.cifar_data_dir, "test_batch.bin")
    f = open(file_name, "rb")
    data = np.reshape(bytearray(f.read()), [10000, 3073])
    for i in range(len(data)):
        ts_label_cifar10[i, data[i, 0]] = 1.0
    ts_data_cifar10 = data[:, 1:] / 255.0
    data_num_len_cifar10 = len(tr_label_cifar10)

    tr_data1 = tr_data_cifar10
    tr_label1 = tr_label_cifar10
    ts_data1 = ts_data_cifar10
    ts_label1 = ts_label_cifar10
    data_num_len1 = data_num_len_cifar10
    tr_data2 = tr_data_svhn
    tr_label2 = tr_label_svhn
    ts_data2 = ts_data_svhn
    ts_label2 = ts_label_svhn
    data_num_len2 = data_num_len_svhn

    ## TASK 1 (CIFAR 10)
    sess = tf.InteractiveSession()
    # Create a multilayer model.

    # Input placeholders
    with tf.name_scope('input'):
        x = tf.placeholder(tf.float32, [None, 32 * 32 * 3], name='x-input')
        y_ = tf.placeholder(tf.float32, [None, 10], name='y-input')

    with tf.name_scope('input_reshape'):
        image_shaped_input = tf.reshape(x, [-1, 32, 32, 3])
        tf.summary.image('input', image_shaped_input, 10)

    # geopath_examples
    geopath = pathnet.geopath_initializer(FLAGS.L, FLAGS.M)

    # fixed weights list
    fixed_list = np.ones((FLAGS.L, FLAGS.M), dtype=str)
    for i in range(FLAGS.L):
        for j in range(FLAGS.M):
            fixed_list[i, j] = '0'

    # reinitializing weights list
    rein_list = np.ones((FLAGS.L, FLAGS.M), dtype=str)
    for i in range(FLAGS.L):
        for j in range(FLAGS.M):
            rein_list[i, j] = '0'

    # Input Layer
    """
  input_weights=pathnet.module_weight_variable([784,FLAGS.filt]);
  input_biases=pathnet.module_bias_variable([FLAGS.filt]);
  net = pathnet.nn_layer(x,input_weights,input_biases,'input_layer');
  """

    # Hidden Layers
    weights_list = np.zeros((FLAGS.L, FLAGS.M), dtype=object)
    biases_list = np.zeros((FLAGS.L, FLAGS.M), dtype=object)
    for i in range(FLAGS.L):
        for j in range(FLAGS.M):
            if (i == 0):
                weights_list[i, j] = pathnet.module_weight_variable(
                    [32 * 32 * 3, FLAGS.filt])
                biases_list[i, j] = pathnet.module_bias_variable([FLAGS.filt])
            else:
                weights_list[i, j] = pathnet.module_weight_variable(
                    [FLAGS.filt, FLAGS.filt])
                biases_list[i, j] = pathnet.module_bias_variable([FLAGS.filt])

    for i in range(FLAGS.L):
        layer_modules_list = np.zeros(FLAGS.M, dtype=object)
        for j in range(FLAGS.M):
            if (i == 0):
                layer_modules_list[j] = pathnet.module(
                    x, weights_list[i, j], biases_list[i, j],
                    'layer' + str(i + 1) + "_" + str(j + 1)) * geopath[i, j]
            else:
                layer_modules_list[j] = pathnet.module(
                    net, weights_list[i, j], biases_list[i, j],
                    'layer' + str(i + 1) + "_" + str(j + 1)) * geopath[i, j]
        net = np.sum(layer_modules_list)
    """
  with tf.name_scope('dropout'):
    keep_prob = tf.placeholder(tf.float32)
    tf.summary.scalar('dropout_keep_probability', keep_prob)
    dropped = tf.nn.dropout(hidden1, keep_prob)
  """

    # Do not apply softmax activation yet, see below.
    output_weights = pathnet.module_weight_variable([FLAGS.filt, 10])
    output_biases = pathnet.module_bias_variable([10])
    y = pathnet.nn_layer(net,
                         output_weights,
                         output_biases,
                         'output_layer',
                         act=tf.identity)

    with tf.name_scope('cross_entropy'):
        diff = tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y)
        with tf.name_scope('total'):
            cross_entropy = tf.reduce_mean(diff)
    tf.summary.scalar('cross_entropy', cross_entropy)
    # Need to learn variables
    #var_list_to_learn=[]+input_weights+input_biases+output_weights+output_biases;
    var_list_to_learn = [] + output_weights + output_biases
    for i in range(FLAGS.L):
        for j in range(FLAGS.M):
            if (fixed_list[i, j] == '0'):
                var_list_to_learn += weights_list[i, j] + biases_list[i, j]

    with tf.name_scope('train'):
        train_step = tf.train.AdamOptimizer(FLAGS.learning_rate).minimize(
            cross_entropy, var_list=var_list_to_learn)

    def feed_dict(train, tr_flag=0):
        #Make a TensorFlow feed_dict: maps data onto Tensor placeholders.
        if train or FLAGS.fake_data:
            xs = tr_data1[tr_flag:tr_flag + 16, :]
            ys = tr_label1[tr_flag:tr_flag + 16, :]
            k = FLAGS.dropout
        else:
            xs = ts_data1
            ys = ts_label1
            k = 1.0
        return {x: xs, y_: ys}

    with tf.name_scope('accuracy'):
        with tf.name_scope('correct_prediction'):
            correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
        with tf.name_scope('accuracy'):
            accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    tf.summary.scalar('accuracy', accuracy)

    # Merge all the summaries and write them out to /tmp/tensorflow/mnist/logs/mnist_with_summaries (by default)
    merged = tf.summary.merge_all()
    train_writer = tf.summary.FileWriter(FLAGS.log_dir + '/train', sess.graph)
    test_writer = tf.summary.FileWriter(FLAGS.log_dir + '/test')
    tf.global_variables_initializer().run()

    # Generating randomly geopath
    geopath_set = np.zeros(FLAGS.candi, dtype=object)
    for i in range(FLAGS.candi):
        geopath_set[i] = pathnet.get_geopath(FLAGS.L, FLAGS.M, FLAGS.N)

    # parameters placeholders and ops
    var_update_ops = np.zeros(len(var_list_to_learn), dtype=object)
    var_update_placeholders = np.zeros(len(var_list_to_learn), dtype=object)
    for i in range(len(var_list_to_learn)):
        var_update_placeholders[i] = tf.placeholder(
            var_list_to_learn[i].dtype, shape=var_list_to_learn[i].get_shape())
        var_update_ops[i] = var_list_to_learn[i].assign(
            var_update_placeholders[i])

    # geopathes placeholders and ops
    geopath_update_ops = np.zeros((len(geopath), len(geopath[0])),
                                  dtype=object)
    geopath_update_placeholders = np.zeros((len(geopath), len(geopath[0])),
                                           dtype=object)
    for i in range(len(geopath)):
        for j in range(len(geopath[0])):
            geopath_update_placeholders[i, j] = tf.placeholder(
                geopath[i, j].dtype, shape=geopath[i, j].get_shape())
            geopath_update_ops[i, j] = geopath[i, j].assign(
                geopath_update_placeholders[i, j])

    tr_flag = 0
    for i in range(FLAGS.max_steps):
        # Select Two Candidate to Tournament
        first, second = pathnet.select_two_candi(FLAGS.candi)

        # First Candidate
        pathnet.geopath_insert(sess, geopath_update_placeholders,
                               geopath_update_ops, geopath_set[first], FLAGS.L,
                               FLAGS.M)
        var_list_backup = pathnet.parameters_backup(var_list_to_learn)
        tr_flag_bak = tr_flag
        for j in range(FLAGS.T):
            summary_geo1_tr, _ = sess.run([merged, train_step],
                                          feed_dict=feed_dict(train=True,
                                                              tr_flag=tr_flag))
            tr_flag = (tr_flag + 16) % data_num_len1
        summary_geo1_ts, acc_geo1 = sess.run([merged, accuracy],
                                             feed_dict=feed_dict(train=False))
        var_list_task1 = pathnet.parameters_backup(var_list_to_learn)
        tr_flag = tr_flag_bak
        # Second Candidate
        pathnet.geopath_insert(sess, geopath_update_placeholders,
                               geopath_update_ops, geopath_set[second],
                               FLAGS.L, FLAGS.M)
        pathnet.parameters_update(sess, var_update_placeholders,
                                  var_update_ops, var_list_backup)
        for j in range(FLAGS.T):
            summary_geo2_tr, _ = sess.run([merged, train_step],
                                          feed_dict=feed_dict(train=True,
                                                              tr_flag=tr_flag))
            tr_flag = (tr_flag + 16) % data_num_len1
        summary_geo2_ts, acc_geo2 = sess.run([merged, accuracy],
                                             feed_dict=feed_dict(train=False))
        var_list_task2 = pathnet.parameters_backup(var_list_to_learn)

        # Compatition between two cases
        if (acc_geo1 > acc_geo2):
            geopath_set[second] = np.copy(geopath_set[first])
            pathnet.mutation(geopath_set[second], FLAGS.L, FLAGS.M, FLAGS.N)
            pathnet.parameters_update(sess, var_update_placeholders,
                                      var_update_ops, var_list_task1)
            train_writer.add_summary(summary_geo1_tr, i)
            test_writer.add_summary(summary_geo1_ts, i)
            print('Accuracy at step %s: %s' % (i + 1, acc_geo1))
        else:
            geopath_set[first] = np.copy(geopath_set[second])
            pathnet.mutation(geopath_set[first], FLAGS.L, FLAGS.M, FLAGS.N)
            pathnet.parameters_update(sess, var_update_placeholders,
                                      var_update_ops, var_list_task2)
            train_writer.add_summary(summary_geo2_tr, i)
            test_writer.add_summary(summary_geo2_ts, i)
            print('Accuracy at step %s: %s' % (i + 1, acc_geo2))

    if (acc_geo1 > acc_geo2):
        task1_acc = acc_geo1
        task1_optimal_path = geopath_set[first]
    else:
        task1_acc = acc_geo2
        task1_optimal_path = geopath_set[second]

    ## TASK 2 (SVHN)
    # Fix task1 Optimal Path
    for i in range(FLAGS.L):
        for j in range(FLAGS.M):
            if (task1_optimal_path[i, j] == 1.0):
                fixed_list[i, j] = '1'
            else:
                rein_list[i, j] = '1'
    # reinitializing weights
    var_list_to_reinitial = []
    for i in range(FLAGS.L):
        for j in range(FLAGS.M):
            if (rein_list[i, j] == '1'):
                var_list_to_reinitial += weights_list[i, j] + biases_list[i, j]
    tf.variables_initializer(var_list=var_list_to_reinitial).run()

    # Output Layer for Task2
    output_weights2 = pathnet.module_weight_variable([FLAGS.filt, 10])
    output_biases2 = pathnet.module_bias_variable([10])
    y2 = pathnet.nn_layer(net,
                          output_weights2,
                          output_biases2,
                          'output_layer2',
                          act=tf.identity)

    with tf.name_scope('cross_entropy2'):
        diff2 = tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y2)
        with tf.name_scope('total2'):
            cross_entropy2 = tf.reduce_mean(diff2)
    tf.summary.scalar('cross_entropy2', cross_entropy2)
    # Need to learn variables
    #var_list_to_learn=[]+input_weights+input_biases+output_weights2+output_biases2;
    var_list_to_learn = [] + output_weights2 + output_biases2
    for i in range(FLAGS.L):
        for j in range(FLAGS.M):
            if (fixed_list[i, j] == '0'):
                var_list_to_learn += weights_list[i, j] + biases_list[i, j]

    with tf.name_scope('train2'):
        train_step2 = tf.train.AdamOptimizer(FLAGS.learning_rate).minimize(
            cross_entropy2, var_list=var_list_to_learn)
        #train_step2 = tf.train.GradientDescentOptimizer(FLAGS.learning_rate).minimize(
        #    cross_entropy2,var_list=var_list_to_learn)

    with tf.name_scope('accuracy2'):
        with tf.name_scope('correct_prediction2'):
            correct_prediction2 = tf.equal(tf.argmax(y2, 1), tf.argmax(y_, 1))
        with tf.name_scope('accuracy2'):
            accuracy2 = tf.reduce_mean(tf.cast(correct_prediction2,
                                               tf.float32))
    tf.summary.scalar('accuracy2', accuracy2)

    # Merge all the summaries and write them out to /tmp/tensorflow/mnist/logs/mnist_with_summaries (by default)
    merged2 = tf.summary.merge_all()
    train_writer = tf.summary.FileWriter(FLAGS.log_dir + '/train2', sess.graph)
    test_writer = tf.summary.FileWriter(FLAGS.log_dir + '/test2')
    tf.global_variables_initializer().run()

    def feed_dict2(train, tr_flag=0):
        #Make a TensorFlow feed_dict: maps data onto Tensor placeholders.
        if train or FLAGS.fake_data:
            xs = tr_data2[tr_flag:tr_flag + 16, :]
            ys = tr_label2[tr_flag:tr_flag + 16, :]
            k = FLAGS.dropout
        else:
            xs = ts_data2
            ys = ts_label2
            k = 1.0
        return {x: xs, y_: ys}

    # Generating randomly geopath
    geopath_set = np.zeros(FLAGS.candi, dtype=object)
    for i in range(FLAGS.candi):
        geopath_set[i] = pathnet.get_geopath(FLAGS.L, FLAGS.M, FLAGS.N)

    # parameters placeholders and ops
    var_update_ops = np.zeros(len(var_list_to_learn), dtype=object)
    var_update_placeholders = np.zeros(len(var_list_to_learn), dtype=object)
    for i in range(len(var_list_to_learn)):
        var_update_placeholders[i] = tf.placeholder(
            var_list_to_learn[i].dtype, shape=var_list_to_learn[i].get_shape())
        var_update_ops[i] = var_list_to_learn[i].assign(
            var_update_placeholders[i])

    tr_flag = 0
    for i in range(FLAGS.max_steps):
        # Select Two Candidate to Tournament
        first, second = pathnet.select_two_candi(FLAGS.candi)

        # First Candidate
        pathnet.geopath_insert(sess, geopath_update_placeholders,
                               geopath_update_ops, geopath_set[first], FLAGS.L,
                               FLAGS.M)
        tr_flag_bak = tr_flag
        var_list_backup = pathnet.parameters_backup(var_list_to_learn)
        for j in range(FLAGS.T):
            summary_geo1_tr, _ = sess.run([merged2, train_step2],
                                          feed_dict=feed_dict2(True, tr_flag))
            tr_flag = (tr_flag + 16) % data_num_len2
        summary_geo1_ts, acc_geo1 = sess.run([merged2, accuracy2],
                                             feed_dict=feed_dict2(False))
        var_list_task1 = pathnet.parameters_backup(var_list_to_learn)

        # Second Candidate
        pathnet.geopath_insert(sess, geopath_update_placeholders,
                               geopath_update_ops, geopath_set[first], FLAGS.L,
                               FLAGS.M)
        tr_flag = tr_flag_bak
        pathnet.parameters_update(sess, var_update_placeholders,
                                  var_update_ops, var_list_backup)
        for j in range(FLAGS.T - 1):
            summary_geo2_tr, _, acc_geo2_tmp = sess.run(
                [merged2, train_step2, accuracy2],
                feed_dict=feed_dict2(True, tr_flag))
            tr_flag = (tr_flag + 16) % data_num_len2
        summary_geo2_ts, acc_geo2 = sess.run([merged2, accuracy2],
                                             feed_dict=feed_dict2(False))
        var_list_task2 = pathnet.parameters_backup(var_list_to_learn)

        # Compatition between two cases
        if (acc_geo1 > acc_geo2):
            geopath_set[second] = np.copy(geopath_set[first])
            pathnet.mutation(geopath_set[second], FLAGS.L, FLAGS.M, FLAGS.N)
            pathnet.parameters_update(sess, var_update_placeholders,
                                      var_update_ops, var_list_task1)
            train_writer.add_summary(summary_geo1_tr, i)
            test_writer.add_summary(summary_geo1_ts, i)
            print('Accuracy at step %s: %s' % (i, acc_geo1))
        else:
            geopath_set[first] = np.copy(geopath_set[second])
            pathnet.mutation(geopath_set[first], FLAGS.L, FLAGS.M, FLAGS.N)
            pathnet.parameters_update(sess, var_update_placeholders,
                                      var_update_ops, var_list_task2)
            train_writer.add_summary(summary_geo2_tr, i)
            test_writer.add_summary(summary_geo2_ts, i)
            print('Accuracy at step %s: %s' % (i, acc_geo2))

    if (acc_geo1 > acc_geo2):
        task2_acc = acc_geo1
    else:
        task2_acc = acc_geo2
    print("CIFAR10 Acc:" + str(task1_acc) + ",SVHN:" + str(task2_acc))

    train_writer.close()
    test_writer.close()
Ejemplo n.º 4
0
def train():
    # Import data
    mnist = input_data.read_data_sets(FLAGS.data_dir,
                                      one_hot=True,
                                      fake_data=FLAGS.fake_data)
    total_tr_data, total_tr_label = mnist.train.next_batch(
        mnist.train._num_examples)

    # Gathering a1 Data
    tr_data_a1 = total_tr_data[(total_tr_label[:, FLAGS.a1] == 1.0)]
    for i in range(len(tr_data_a1)):
        for j in range(len(tr_data_a1[0])):
            rand_num = np.random.rand()
            if (rand_num >= 0.5):
                tr_data_a1[i, j] = np.minimum(tr_data_a1[i, j] + rand_num, 1.0)

    # Gathering a2 Data
    tr_data_a2 = total_tr_data[(total_tr_label[:, FLAGS.a2] == 1.0)]
    for i in range(len(tr_data_a2)):
        for j in range(len(tr_data_a2[0])):
            rand_num = np.random.rand()
            if (rand_num >= 0.5):
                tr_data_a2[i, j] = np.minimum(tr_data_a2[i, j] + rand_num, 1.0)

    # Gathering b1 Data
    tr_data_b1 = total_tr_data[(total_tr_label[:, FLAGS.b1] == 1.0)]
    for i in range(len(tr_data_b1)):
        for j in range(len(tr_data_b1[0])):
            rand_num = np.random.rand()
            if (rand_num >= 0.5):
                tr_data_b1[i, j] = np.minimum(tr_data_b1[i, j] + rand_num, 1.0)

    # Gathering b2 Data
    tr_data_b2 = total_tr_data[(total_tr_label[:, FLAGS.b2] == 1.0)]
    for i in range(len(tr_data_b2)):
        for j in range(len(tr_data_b2[0])):
            rand_num = np.random.rand()
            if (rand_num >= 0.5):
                tr_data_b2[i, j] = np.minimum(tr_data_b2[i, j] + rand_num, 1.0)

    ## TASK 1
    sess = tf.InteractiveSession()

    # Input placeholders
    with tf.name_scope('input'):
        x = tf.placeholder(tf.float32, [None, 784], name='x-input')
        y_ = tf.placeholder(tf.float32, [None, 2], name='y-input')

    with tf.name_scope('input_reshape'):
        image_shaped_input = tf.reshape(x, [-1, 28, 28, 1])
        tf.summary.image('input', image_shaped_input, 2)

    # geopath_examples
    geopath = pathnet.geopath_initializer(FLAGS.L, FLAGS.M)

    # fixed weights list
    fixed_list = np.ones((FLAGS.L, FLAGS.M), dtype=str)
    for i in range(FLAGS.L):
        for j in range(FLAGS.M):
            fixed_list[i, j] = '0'

    # Hidden Layers
    weights_list = np.zeros((FLAGS.L, FLAGS.M), dtype=object)
    biases_list = np.zeros((FLAGS.L, FLAGS.M), dtype=object)
    for i in range(FLAGS.L):
        for j in range(FLAGS.M):
            if (i == 0):
                weights_list[i, j] = pathnet.module_weight_variable(
                    [784, FLAGS.filt])
                biases_list[i, j] = pathnet.module_bias_variable([FLAGS.filt])
            else:
                weights_list[i, j] = pathnet.module_weight_variable(
                    [FLAGS.filt, FLAGS.filt])
                biases_list[i, j] = pathnet.module_bias_variable([FLAGS.filt])

    for i in range(FLAGS.L):
        layer_modules_list = np.zeros(FLAGS.M, dtype=object)
        for j in range(FLAGS.M):
            if (i == 0):
                layer_modules_list[j] = pathnet.module(
                    x, weights_list[i, j], biases_list[i, j],
                    'layer' + str(i + 1) + "_" + str(j + 1)) * geopath[i, j]
            else:
                layer_modules_list[j] = pathnet.module2(
                    j, net, weights_list[i, j], biases_list[i, j],
                    'layer' + str(i + 1) + "_" + str(j + 1)) * geopath[i, j]
        net = np.sum(layer_modules_list)

    # Output Layer
    output_weights = pathnet.module_weight_variable([FLAGS.filt, 2])
    output_biases = pathnet.module_bias_variable([2])
    y = pathnet.nn_layer(net, output_weights, output_biases, 'output_layer')

    # Cross Entropy
    with tf.name_scope('cross_entropy'):
        diff = tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y)
        with tf.name_scope('total'):
            cross_entropy = tf.reduce_mean(diff)
    tf.summary.scalar('cross_entropy', cross_entropy)

    # Need to learn variables
    var_list_to_learn = [] + output_weights + output_biases
    for i in range(FLAGS.L):
        for j in range(FLAGS.M):
            if (fixed_list[i, j] == '0'):
                var_list_to_learn += weights_list[i, j] + biases_list[i, j]

    # GradientDescent
    with tf.name_scope('train'):
        train_step = tf.train.GradientDescentOptimizer(
            FLAGS.learning_rate).minimize(cross_entropy,
                                          var_list=var_list_to_learn)

    # Accuracy
    with tf.name_scope('accuracy'):
        with tf.name_scope('correct_prediction'):
            correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
        with tf.name_scope('accuracy'):
            accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    tf.summary.scalar('accuracy', accuracy)

    # Merge all the summaries and write them out to /tmp/tensorflow/mnist/logs/mnist_with_summaries (by default)
    merged = tf.summary.merge_all()
    train_writer = tf.summary.FileWriter(FLAGS.log_dir + '/train', sess.graph)
    test_writer = tf.summary.FileWriter(FLAGS.log_dir + '/test')
    tf.global_variables_initializer().run()

    # Make a TensorFlow feed_dict: maps data onto Tensor placeholders.
    def feed_dict(train, batch_num=0, tr_flag1=0, tr_flag2=0):
        if train or FLAGS.fake_data:
            x_1 = tr_data_a1[tr_flag1:tr_flag1 + batch_num, :]
            x_2 = tr_data_a2[tr_flag2:tr_flag2 + batch_num, :]
            if (len(x_1) < batch_num):
                x_1 = np.append(x_1,
                                tr_data_a1[:(tr_flag1 + batch_num) %
                                           len(tr_data_a1), :],
                                axis=0)
            if (len(x_2) < batch_num):
                x_2 = np.append(x_2,
                                tr_data_a2[:(tr_flag2 + batch_num) %
                                           len(tr_data_a2), :],
                                axis=0)
            xs = np.append(x_1, x_2, axis=0)
            ys = np.zeros((batch_num * 2, 2), dtype=float)
            for i in range(len(ys)):
                if (i < batch_num):
                    ys[i, 0] = 1.0
                else:
                    ys[i, 1] = 1.0
        return {x: xs, y_: ys}

    # Generating randomly geopath
    geopath_set = np.zeros(FLAGS.candi, dtype=object)
    for i in range(FLAGS.candi):
        geopath_set[i] = pathnet.get_geopath(FLAGS.L, FLAGS.M, FLAGS.N)

    # parameters placeholders and ops
    var_update_ops = np.zeros(len(var_list_to_learn), dtype=object)
    var_update_placeholders = np.zeros(len(var_list_to_learn), dtype=object)
    for i in range(len(var_list_to_learn)):
        var_update_placeholders[i] = tf.placeholder(
            var_list_to_learn[i].dtype, shape=var_list_to_learn[i].get_shape())
        var_update_ops[i] = var_list_to_learn[i].assign(
            var_update_placeholders[i])

    # geopathes placeholders and ops
    geopath_update_ops = np.zeros((len(geopath), len(geopath[0])),
                                  dtype=object)
    geopath_update_placeholders = np.zeros((len(geopath), len(geopath[0])),
                                           dtype=object)
    for i in range(len(geopath)):
        for j in range(len(geopath[0])):
            geopath_update_placeholders[i, j] = tf.placeholder(
                geopath[i, j].dtype, shape=geopath[i, j].get_shape())
            geopath_update_ops[i, j] = geopath[i, j].assign(
                geopath_update_placeholders[i, j])

    tr_flag1 = 0
    tr_flag2 = 0
    data_num1 = len(tr_data_a1)
    data_num2 = len(tr_data_a2)
    for i in range(FLAGS.max_steps):
        # Select Two Candidate to Tournament
        first, second = pathnet.select_two_candi(FLAGS.candi)

        # First Candidate
        pathnet.geopath_insert(sess, geopath_update_placeholders,
                               geopath_update_ops, geopath_set[first], FLAGS.L,
                               FLAGS.M)
        acc_geo1_tr = 0
        #var_list_backup=pathnet.parameters_backup(var_list_to_learn);
        #tr_flag_bak=tr_flag;
        for j in range(FLAGS.T):
            summary_geo1_tr, _, acc_geo1_tmp = sess.run(
                [merged, train_step, accuracy],
                feed_dict=feed_dict(True, int(FLAGS.batch_num / 2),
                                    int(tr_flag1), int(tr_flag2)))
            tr_flag1 = (tr_flag1 + FLAGS.batch_num / 2) % data_num1
            tr_flag2 = (tr_flag2 + FLAGS.batch_num / 2) % data_num2
            acc_geo1_tr += acc_geo1_tmp
        #var_list_task1=pathnet.parameters_backup(var_list_to_learn);
        #tr_flag=tr_flag_bak;
        # Second Candidate
        pathnet.geopath_insert(sess, geopath_update_placeholders,
                               geopath_update_ops, geopath_set[second],
                               FLAGS.L, FLAGS.M)
        acc_geo2_tr = 0
        #pathnet.parameters_update(sess,var_update_placeholders,var_update_ops,var_list_backup);
        for j in range(FLAGS.T):
            summary_geo2_tr, _, acc_geo2_tmp = sess.run(
                [merged, train_step, accuracy],
                feed_dict=feed_dict(True, int(FLAGS.batch_num / 2),
                                    int(tr_flag1), int(tr_flag2)))
            tr_flag1 = (tr_flag1 + FLAGS.batch_num / 2) % data_num1
            tr_flag2 = (tr_flag2 + FLAGS.batch_num / 2) % data_num2
            acc_geo2_tr += acc_geo2_tmp
        #var_list_task2=pathnet.parameters_backup(var_list_to_learn);

        # Compatition between two cases
        if (acc_geo1_tr > acc_geo2_tr):
            geopath_set[second] = np.copy(geopath_set[first])
            geopath_set[second] = pathnet.mutation(geopath_set[second],
                                                   FLAGS.L, FLAGS.M, FLAGS.N)
            #pathnet.parameters_update(sess,var_update_placeholders,var_update_ops,var_list_task1);
            train_writer.add_summary(summary_geo1_tr, i)
            print('Training Accuracy at step %s: %s' %
                  (i, acc_geo1_tr / FLAGS.T))
            if (acc_geo1_tr / FLAGS.T >= 0.998):
                print('Learning Done!!')
                print('Optimal Path is as followed.')
                print(geopath_set[first])
                task1_optimal_path = geopath_set[first]
                break
        else:
            geopath_set[first] = np.copy(geopath_set[second])
            geopath_set[first] = pathnet.mutation(geopath_set[first], FLAGS.L,
                                                  FLAGS.M, FLAGS.N)
            #pathnet.parameters_update(sess,var_update_placeholders,var_update_ops,var_list_task2);
            train_writer.add_summary(summary_geo2_tr, i)
            print('Training Accuracy at step %s: %s' %
                  (i, acc_geo2_tr / FLAGS.T))
            if (acc_geo2_tr / FLAGS.T >= 0.998):
                print('Learning Done!!')
                print('Optimal Path is as followed.')
                print(geopath_set[second])
                task1_optimal_path = geopath_set[second]
                break

    iter_task1 = i

    # Fix task1 Optimal Path
    for i in range(FLAGS.L):
        for j in range(FLAGS.M):
            if (task1_optimal_path[i, j] == 1.0):
                fixed_list[i, j] = '1'

    # Get variables of fixed list
    var_list_to_fix = []
    for i in range(FLAGS.L):
        for j in range(FLAGS.M):
            if (fixed_list[i, j] == '1'):
                var_list_to_fix += weights_list[i, j] + biases_list[i, j]
    var_list_fix = pathnet.parameters_backup(var_list_to_fix)

    # parameters placeholders and ops
    var_fix_ops = np.zeros(len(var_list_to_fix), dtype=object)
    var_fix_placeholders = np.zeros(len(var_list_to_fix), dtype=object)
    for i in range(len(var_list_to_fix)):
        var_fix_placeholders[i] = tf.placeholder(
            var_list_to_fix[i].dtype, shape=var_list_to_fix[i].get_shape())
        var_fix_ops[i] = var_list_to_fix[i].assign(var_fix_placeholders[i])

    ## TASK 2
    # Need to learn variables
    var_list_to_learn = [] + output_weights + output_biases
    for i in range(FLAGS.L):
        for j in range(FLAGS.M):
            if (fixed_list[i, j] == '0'):
                var_list_to_learn += weights_list[i, j] + biases_list[i, j]

    # Fixed variables
    var_list_to_fix = []
    for i in range(FLAGS.L):
        for j in range(FLAGS.M):
            if (fixed_list[i, j] == '1'):
                var_list_to_fix += weights_list[i, j] + biases_list[i, j]

    for i in range(FLAGS.L):
        for j in range(FLAGS.M):
            if (fixed_list[i, j] == '1'):
                tmp = biases_list[i, j][0]
                break
        break

    # Initialization
    tf.global_variables_initializer().run()

    # Update fixed values
    pathnet.parameters_update(sess, var_fix_placeholders, var_fix_ops,
                              var_list_fix)

    # GradientDescent
    with tf.name_scope('train'):
        train_step = tf.train.GradientDescentOptimizer(
            FLAGS.learning_rate).minimize(cross_entropy,
                                          var_list=var_list_to_learn)

    # Make a TensorFlow feed_dict: maps data onto Tensor placeholders.
    def feed_dict(train, batch_num, tr_flag1=0, tr_flag2=0):
        if train or FLAGS.fake_data:
            x_1 = tr_data_b1[tr_flag1:tr_flag1 + batch_num, :]
            x_2 = tr_data_b2[tr_flag2:tr_flag2 + batch_num, :]
            if (len(x_1) < batch_num):
                x_1 = np.append(x_1,
                                tr_data_b1[:(tr_flag1 + batch_num) %
                                           len(tr_data_b1), :],
                                axis=0)
            if (len(x_2) < batch_num):
                x_2 = np.append(x_2,
                                tr_data_b2[:(tr_flag2 + batch_num) %
                                           len(tr_data_b2), :],
                                axis=0)
            xs = np.append(x_1, x_2, axis=0)
            ys = np.zeros((batch_num * 2, 2), dtype=float)
            for i in range(len(ys)):
                if (i < batch_num):
                    ys[i, 0] = 1.0
                else:
                    ys[i, 1] = 1.0
        return {x: xs, y_: ys}

    # Generating randomly geopath
    geopath_set = np.zeros(FLAGS.candi, dtype=object)
    for i in range(FLAGS.candi):
        geopath_set[i] = pathnet.get_geopath(FLAGS.L, FLAGS.M, FLAGS.N)

    # parameters placeholders and ops
    var_update_ops = np.zeros(len(var_list_to_learn), dtype=object)
    var_update_placeholders = np.zeros(len(var_list_to_learn), dtype=object)
    for i in range(len(var_list_to_learn)):
        var_update_placeholders[i] = tf.placeholder(
            var_list_to_learn[i].dtype, shape=var_list_to_learn[i].get_shape())
        var_update_ops[i] = var_list_to_learn[i].assign(
            var_update_placeholders[i])

    tr_flag1 = 0
    tr_flag2 = 0
    data_num1 = len(tr_data_b1)
    data_num2 = len(tr_data_b2)
    for i in range(FLAGS.max_steps):
        # Select Two Candidate to Tournament
        first, second = pathnet.select_two_candi(FLAGS.candi)

        # First Candidate
        pathnet.geopath_insert(sess, geopath_update_placeholders,
                               geopath_update_ops, geopath_set[first], FLAGS.L,
                               FLAGS.M)
        acc_geo1_tr = 0
        tr_flag1_bak = tr_flag1
        tr_flag2_bak = tr_flag2
        #var_list_backup=pathnet.parameters_backup(var_list_to_learn);
        #tr_flag_bak=tr_flag;
        for j in range(FLAGS.T):
            summary_geo1_tr, _ = sess.run([merged, train_step],
                                          feed_dict=feed_dict(
                                              True, int(FLAGS.batch_num / 2),
                                              int(tr_flag1), int(tr_flag2)))
            tr_flag1 = (tr_flag1 + FLAGS.batch_num / 2) % data_num1
            tr_flag2 = (tr_flag2 + FLAGS.batch_num / 2) % data_num2
        tr_flag1 = tr_flag1_bak
        tr_flag2 = tr_flag2_bak
        for j in range(FLAGS.T):
            acc_geo1_tmp = sess.run([accuracy],
                                    feed_dict=feed_dict(
                                        True, int(FLAGS.batch_num / 2),
                                        int(tr_flag1), int(tr_flag2)))
            tr_flag1 = (tr_flag1 + FLAGS.batch_num / 2) % data_num1
            tr_flag2 = (tr_flag2 + FLAGS.batch_num / 2) % data_num2
            acc_geo1_tr += acc_geo1_tmp[0]
        #var_list_task1=pathnet.parameters_backup(var_list_to_learn);
        #tr_flag=tr_flag_bak;
        # Second Candidate
        pathnet.geopath_insert(sess, geopath_update_placeholders,
                               geopath_update_ops, geopath_set[second],
                               FLAGS.L, FLAGS.M)
        acc_geo2_tr = 0
        tr_flag1 = tr_flag1_bak
        tr_flag2 = tr_flag2_bak
        #pathnet.parameters_update(sess,var_update_placeholders,var_update_ops,var_list_backup);
        for j in range(FLAGS.T):
            summary_geo2_tr, _ = sess.run([merged, train_step],
                                          feed_dict=feed_dict(
                                              True, int(FLAGS.batch_num / 2),
                                              int(tr_flag1), int(tr_flag2)))
            tr_flag1 = (tr_flag1 + FLAGS.batch_num / 2) % data_num1
            tr_flag2 = (tr_flag2 + FLAGS.batch_num / 2) % data_num2
        tr_flag1 = tr_flag1_bak
        tr_flag2 = tr_flag2_bak
        for j in range(FLAGS.T):
            acc_geo2_tmp = sess.run([accuracy],
                                    feed_dict=feed_dict(
                                        True, int(FLAGS.batch_num / 2),
                                        int(tr_flag1), int(tr_flag2)))
            tr_flag1 = (tr_flag1 + FLAGS.batch_num / 2) % data_num1
            tr_flag2 = (tr_flag2 + FLAGS.batch_num / 2) % data_num2
            acc_geo2_tr += acc_geo2_tmp[0]
        #var_list_task2=pathnet.parameters_backup(var_list_to_learn);

        # Compatition between two cases
        if (acc_geo1_tr > acc_geo2_tr):
            geopath_set[second] = np.copy(geopath_set[first])
            pathnet.mutation(geopath_set[second], FLAGS.L, FLAGS.M, FLAGS.N)
            #pathnet.parameters_update(sess,var_update_placeholders,var_update_ops,var_list_task1);
            train_writer.add_summary(summary_geo1_tr, i)
            print('Training Accuracy at step %s: %s' %
                  (i, acc_geo1_tr / FLAGS.T))
            if (acc_geo1_tr / FLAGS.T >= 0.998):
                print('Learning Done!!')
                print('Optimal Path is as followed.')
                print(geopath_set[first])
                task2_optimal_path = geopath_set[first]
                break
        else:
            geopath_set[first] = np.copy(geopath_set[second])
            pathnet.mutation(geopath_set[first], FLAGS.L, FLAGS.M, FLAGS.N)
            #pathnet.parameters_update(sess,var_update_placeholders,var_update_ops,var_list_task2);
            train_writer.add_summary(summary_geo2_tr, i)
            print('Training Accuracy at step %s: %s' %
                  (i, acc_geo2_tr / FLAGS.T))
            if (acc_geo2_tr / FLAGS.T >= 0.998):
                print('Learning Done!!')
                print('Optimal Path is as followed.')
                print(geopath_set[second])
                task2_optimal_path = geopath_set[second]
                break

    iter_task2 = i
    overlap = 0
    for i in range(len(task1_optimal_path)):
        for j in range(len(task1_optimal_path[0])):
            if (task1_optimal_path[i, j] == task2_optimal_path[i, j]) & (
                    task1_optimal_path[i, j] == 1.0):
                overlap += 1
    print("Entire Iter:" + str(iter_task1 + iter_task2) + ",Overlap:" +
          str(overlap))

    train_writer.close()
    test_writer.close()
Ejemplo n.º 5
0
def train():
    # Import data
    mnist = input_data.read_data_sets(FLAGS.data_dir,
                                      one_hot=True,
                                      fake_data=FLAGS.fake_data)
    total_tr_data, total_tr_label = mnist.train.next_batch(
        mnist.train._num_examples)

    # Gathering a1 Data
    tr_data_a1 = total_tr_data[(total_tr_label[:, FLAGS.a1] == 1.0)]
    for i in range(len(tr_data_a1)):
        for j in range(len(tr_data_a1[0])):
            rand_num = np.random.rand()
            if (rand_num >= 0.5):
                tr_data_a1[i, j] = np.minimum(tr_data_a1[i, j] + rand_num, 1.0)

    # Gathering a2 Data
    tr_data_a2 = total_tr_data[(total_tr_label[:, FLAGS.a2] == 1.0)]
    for i in range(len(tr_data_a2)):
        for j in range(len(tr_data_a2[0])):
            rand_num = np.random.rand()
            if (rand_num >= 0.5):
                tr_data_a2[i, j] = np.minimum(tr_data_a2[i, j] + rand_num, 1.0)

    # Gathering b1 Data
    tr_data_b1 = total_tr_data[(total_tr_label[:, FLAGS.b1] == 1.0)]
    for i in range(len(tr_data_b1)):
        for j in range(len(tr_data_b1[0])):
            rand_num = np.random.rand()
            if (rand_num >= 0.5):
                tr_data_b1[i, j] = np.minimum(tr_data_b1[i, j] + rand_num, 1.0)

    # Gathering b2 Data
    tr_data_b2 = total_tr_data[(total_tr_label[:, FLAGS.b2] == 1.0)]
    for i in range(len(tr_data_b2)):
        for j in range(len(tr_data_b2[0])):
            rand_num = np.random.rand()
            if (rand_num >= 0.5):
                tr_data_b2[i, j] = np.minimum(tr_data_b2[i, j] + rand_num, 1.0)

    tr_data1 = np.append(tr_data_a1, tr_data_a2, axis=0)
    tr_label1 = np.zeros((len(tr_data1), 2), dtype=float)
    for i in range(len(tr_data1)):
        if (i < len(tr_data_a1)):
            tr_label1[i, 0] = 1.0
        else:
            tr_label1[i, 1] = 1.0

    tr_data2 = np.append(tr_data_b1, tr_data_b2, axis=0)
    tr_label2 = np.zeros((len(tr_data2), 2), dtype=float)
    for i in range(len(tr_data2)):
        if (i < len(tr_data_b1)):
            tr_label2[i, 0] = 1.0
        else:
            tr_label2[i, 1] = 1.0

    ## TASK 1
    sess = tf.InteractiveSession()

    # Input placeholders
    with tf.name_scope('input'):
        x = tf.placeholder(tf.float32, [None, 784], name='x-input')
        y_ = tf.placeholder(tf.float32, [None, 2], name='y-input')

    with tf.name_scope('input_reshape'):
        image_shaped_input = tf.reshape(x, [-1, 28, 28, 1])
        tf.summary.image('input', image_shaped_input, 2)

    # geopath_examples
    geopath = pathnet.geopath_initializer(FLAGS.L, FLAGS.M)

    # fixed weights list
    fixed_list = np.ones((FLAGS.L, FLAGS.M), dtype=str)
    for i in range(FLAGS.L):
        for j in range(FLAGS.M):
            fixed_list[i, j] = '0'

    # Hidden Layers
    weights_list = np.zeros(
        (FLAGS.L, FLAGS.M),
        dtype=object)  # weights_list also record conv_kernels
    biases_list = np.zeros((FLAGS.L, FLAGS.M), dtype=object)

    # model define
    layer_modules_list = np.zeros(FLAGS.M, dtype=object)
    # conv layer
    i = 0
    for j in range(FLAGS.M):
        layer_modules_list[j], weights_list[i, j], biases_list[
            i,
            j] = pathnet.conv_module(image_shaped_input, FLAGS.filt, [5, 5],
                                     geopath[i, j], 1,
                                     'layer' + str(i + 1) + "_" + str(j + 1))
    net = np.sum(layer_modules_list) / FLAGS.M
    # res-fire layer
    i = 1
    for j in range(FLAGS.M):
        layer_modules_list[j], weights_list[i, j], biases_list[
            i, j] = pathnet.res_fire_layer(
                net, FLAGS.filt, 10, 10, geopath[i, j],
                'layer' + str(i + 1) + "_" + str(j + 1))
    net = np.sum(layer_modules_list) / FLAGS.M
    # dimensionality_reduction layer
    i = 2
    for j in range(FLAGS.M):
        layer_modules_list[j], weights_list[i, j], biases_list[
            i, j] = pathnet.Dimensionality_reduction_module(
                net, 10, geopath[i, j],
                'layer' + str(i + 1) + "_" + str(j + 1))
    net = np.sum(layer_modules_list) / FLAGS.M
    # conv layer
    i = 3
    for j in range(FLAGS.M):
        layer_modules_list[j], weights_list[i, j], biases_list[
            i,
            j] = pathnet.conv_module(image_shaped_input, FLAGS.filt, [5, 5],
                                     geopath[i, j], 1,
                                     'layer' + str(i + 1) + "_" + str(j + 1))
    net = np.sum(layer_modules_list) / FLAGS.M
    # output layer
    # reshape
    _shape = net.shape[1:]
    _length = 1
    for _i in _shape:
        _length *= int(_i)
    net = tf.reshape(net, [-1, _length])
    # full connection layer
    y, output_weights, output_biases = pathnet.nn_layer(
        net, 2, 'output_layer')

    # Cross Entropy
    with tf.name_scope('cross_entropy'):
        diff = tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y)
        with tf.name_scope('total'):
            cross_entropy = tf.reduce_mean(diff)
    tf.summary.scalar('cross_entropy', cross_entropy)

    # Need to learn variables
    var_list_to_learn = [] + output_weights + output_biases
    for i in range(FLAGS.L):
        for j in range(FLAGS.M):
            if (fixed_list[i, j] == '0'):
                var_list_to_learn += weights_list[i, j] + biases_list[i, j]

    # GradientDescent
    with tf.name_scope('train'):
        train_step = tf.train.GradientDescentOptimizer(
            FLAGS.learning_rate).minimize(cross_entropy,
                                          var_list=var_list_to_learn)

    # Accuracy
    with tf.name_scope('accuracy'):
        with tf.name_scope('correct_prediction'):
            correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
        with tf.name_scope('accuracy'):
            accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    tf.summary.scalar('accuracy', accuracy)

    # Merge all the summaries and write them out to /tmp/tensorflow/mnist/logs/mnist_with_summaries (by default)
    merged = tf.summary.merge_all()
    train_writer = tf.summary.FileWriter(FLAGS.log_dir + '/train1', sess.graph)
    test_writer = tf.summary.FileWriter(FLAGS.log_dir + '/test1')

    tf.global_variables_initializer().run()

    # Generating randomly geopath
    geopath_set = np.zeros(FLAGS.candi, dtype=object)
    for i in range(FLAGS.candi):
        geopath_set[i] = pathnet.get_geopath(FLAGS.L, FLAGS.M, FLAGS.N)

    # parameters placeholders and ops
    var_update_ops = np.zeros(len(var_list_to_learn), dtype=object)
    var_update_placeholders = np.zeros(len(var_list_to_learn), dtype=object)
    for i in range(len(var_list_to_learn)):
        var_update_placeholders[i] = tf.placeholder(
            var_list_to_learn[i].dtype, shape=var_list_to_learn[i].get_shape())
        var_update_ops[i] = var_list_to_learn[i].assign(
            var_update_placeholders[i])

    # geopathes placeholders and ops
    geopath_update_ops = np.zeros((len(geopath), len(geopath[0])),
                                  dtype=object)
    geopath_update_placeholders = np.zeros((len(geopath), len(geopath[0])),
                                           dtype=object)
    for i in range(len(geopath)):
        for j in range(len(geopath[0])):
            geopath_update_placeholders[i, j] = tf.placeholder(
                geopath[i, j].dtype, shape=geopath[i, j].get_shape())
            geopath_update_ops[i, j] = geopath[i, j].assign(
                geopath_update_placeholders[i, j])

    acc_geo = np.zeros(FLAGS.B, dtype=float)
    summary_geo = np.zeros(FLAGS.B, dtype=object)
    for i in range(FLAGS.max_steps):
        # Select Candidates to Tournament
        compet_idx = range(FLAGS.candi)
        np.random.shuffle(compet_idx)
        compet_idx = compet_idx[:FLAGS.B]
        # Learning & Evaluating
        for j in range(len(compet_idx)):
            # Shuffle the data
            idx = range(len(tr_data1))
            np.random.shuffle(idx)
            tr_data1 = tr_data1[idx]
            tr_label1 = tr_label1[idx]
            # Insert Candidate
            pathnet.geopath_insert(sess, geopath_update_placeholders,
                                   geopath_update_ops,
                                   geopath_set[compet_idx[j]], FLAGS.L,
                                   FLAGS.M)
            acc_geo_tr = 0
            for k in range(FLAGS.T):
                summary_geo_tr, _, acc_geo_tmp = sess.run(
                    [merged, train_step, accuracy],
                    feed_dict={
                        x:
                        tr_data1[k * FLAGS.batch_num:(k + 1) *
                                 FLAGS.batch_num, :],
                        y_:
                        tr_label1[k * FLAGS.batch_num:(k + 1) *
                                  FLAGS.batch_num, :]
                    })
                acc_geo_tr += acc_geo_tmp
            acc_geo[j] = acc_geo_tr / FLAGS.T
            summary_geo[j] = summary_geo_tr
        # Tournament
        winner_idx = np.argmax(acc_geo)
        acc = acc_geo[winner_idx]
        summary = summary_geo[winner_idx]
        # Copy and Mutation
        for j in range(len(compet_idx)):
            if (j != winner_idx):
                geopath_set[compet_idx[j]] = np.copy(
                    geopath_set[compet_idx[winner_idx]])
                geopath_set[compet_idx[j]] = pathnet.mutation(
                    geopath_set[compet_idx[j]], FLAGS.L, FLAGS.M, FLAGS.N)
        train_writer.add_summary(summary, i)
        print('Training Accuracy at step %s: %s' % (i, acc))
        if (acc >= 0.99):
            print('Learning Done!!')
            print('Optimal Path is as followed.')
            print(geopath_set[compet_idx[winner_idx]])
            task1_optimal_path = geopath_set[compet_idx[winner_idx]]
            break
        """
    geopath_sum=np.zeros((len(geopath),len(geopath[0])),dtype=float);
    for j in range(len(geopath_set)):
      for k in range(len(geopath)):
        for l in range(len(geopath[0])):
          geopath_sum[k][l]+=geopath_set[j][k][l];
    print(geopath_sum);
    """
    # record steps to find optimal path in task1
    iter_task1 = i

    # Fix task1 Optimal Path
    for i in range(FLAGS.L):
        for j in range(FLAGS.M):
            if (task1_optimal_path[i, j] == 1.0):
                fixed_list[i, j] = '1'

    # Get variables of fixed list
    var_list_to_fix = []
    #var_list_to_fix=[]+output_weights+output_biases;
    for i in range(FLAGS.L):
        for j in range(FLAGS.M):
            if (fixed_list[i, j] == '1'):
                var_list_to_fix += weights_list[i, j] + biases_list[i, j]
    var_list_fix = pathnet.parameters_backup(var_list_to_fix)
    """
  for i in range(FLAGS.L):
    for j in range(FLAGS.M):
      if(task1_optimal_path[i,j]==1.0):
        fixed_list[i,j]='0';
  """

    # parameters placeholders and ops
    var_fix_ops = np.zeros(len(var_list_to_fix), dtype=object)
    var_fix_placeholders = np.zeros(len(var_list_to_fix), dtype=object)
    for i in range(len(var_list_to_fix)):
        var_fix_placeholders[i] = tf.placeholder(
            var_list_to_fix[i].dtype, shape=var_list_to_fix[i].get_shape())
        var_fix_ops[i] = var_list_to_fix[i].assign(var_fix_placeholders[i])

    ## TASK 2
    # Need to learn variables
    var_list_to_learn = [] + output_weights + output_biases
    for i in range(FLAGS.L):
        for j in range(FLAGS.M):
            if (fixed_list[i, j] == '0'):
                var_list_to_learn += weights_list[i, j] + biases_list[i, j]
    '''
  for i in range(FLAGS.L):
    for j in range(FLAGS.M):
      if(fixed_list[i,j]=='1'):
        tmp=biases_list[i,j][0];
        break;
    break;
  '''

    # Initialization
    merged = tf.summary.merge_all()
    train_writer = tf.summary.FileWriter(FLAGS.log_dir + '/train2', sess.graph)
    test_writer = tf.summary.FileWriter(FLAGS.log_dir + '/test2')
    tf.global_variables_initializer().run()

    # Update fixed values
    pathnet.parameters_update(sess, var_fix_placeholders, var_fix_ops,
                              var_list_fix)

    # GradientDescent
    with tf.name_scope('train'):
        train_step = tf.train.GradientDescentOptimizer(
            FLAGS.learning_rate).minimize(cross_entropy,
                                          var_list=var_list_to_learn)

    # Generating randomly geopath
    geopath_set = np.zeros(FLAGS.candi, dtype=object)
    for i in range(FLAGS.candi):
        geopath_set[i] = pathnet.get_geopath(FLAGS.L, FLAGS.M, FLAGS.N)

    # parameters placeholders and ops
    var_update_ops = np.zeros(len(var_list_to_learn), dtype=object)
    var_update_placeholders = np.zeros(len(var_list_to_learn), dtype=object)
    for i in range(len(var_list_to_learn)):
        var_update_placeholders[i] = tf.placeholder(
            var_list_to_learn[i].dtype, shape=var_list_to_learn[i].get_shape())
        var_update_ops[i] = var_list_to_learn[i].assign(
            var_update_placeholders[i])

    acc_geo = np.zeros(FLAGS.B, dtype=float)
    summary_geo = np.zeros(FLAGS.B, dtype=object)
    for i in range(FLAGS.max_steps):
        # Select Candidates to Tournament
        compet_idx = range(FLAGS.candi)
        np.random.shuffle(compet_idx)
        compet_idx = compet_idx[:FLAGS.B]
        # Learning & Evaluating
        for j in range(len(compet_idx)):
            # Shuffle the data
            idx = range(len(tr_data2))
            np.random.shuffle(idx)
            tr_data2 = tr_data2[idx]
            tr_label2 = tr_label2[idx]
            geopath_insert = np.copy(geopath_set[compet_idx[j]])

            for l in range(FLAGS.L):
                for m in range(FLAGS.M):
                    if (fixed_list[l, m] == '1'):
                        geopath_insert[l, m] = 1.0

            # Insert Candidate
            pathnet.geopath_insert(sess, geopath_update_placeholders,
                                   geopath_update_ops, geopath_insert, FLAGS.L,
                                   FLAGS.M)
            acc_geo_tr = 0
            for k in range(FLAGS.T):
                summary_geo_tr, _, acc_geo_tmp = sess.run(
                    [merged, train_step, accuracy],
                    feed_dict={
                        x:
                        tr_data2[k * FLAGS.batch_num:(k + 1) *
                                 FLAGS.batch_num, :],
                        y_:
                        tr_label2[k * FLAGS.batch_num:(k + 1) *
                                  FLAGS.batch_num, :]
                    })
                acc_geo_tr += acc_geo_tmp
            acc_geo[j] = acc_geo_tr / FLAGS.T
            summary_geo[j] = summary_geo_tr
        # Tournament
        winner_idx = np.argmax(acc_geo)
        acc = acc_geo[winner_idx]
        summary = summary_geo[winner_idx]
        # Copy and Mutation
        for j in range(len(compet_idx)):
            if (j != winner_idx):
                geopath_set[compet_idx[j]] = np.copy(
                    geopath_set[compet_idx[winner_idx]])
                geopath_set[compet_idx[j]] = pathnet.mutation(
                    geopath_set[compet_idx[j]], FLAGS.L, FLAGS.M, FLAGS.N)
        train_writer.add_summary(summary, i)
        print('Training Accuracy at step %s: %s' % (i, acc))
        if (acc >= 0.99):
            print('Learning Done!!')
            print('Optimal Path is as followed.')
            print(geopath_set[compet_idx[winner_idx]])
            task2_optimal_path = geopath_set[compet_idx[winner_idx]]
            break
        """
    geopath_sum=np.zeros((len(geopath),len(geopath[0])),dtype=float);
    for j in range(len(geopath_set)):
      for k in range(len(geopath)):
        for l in range(len(geopath[0])):
          geopath_sum[k][l]+=geopath_set[j][k][l];
    print(geopath_sum);
    """

    iter_task2 = i
    overlap = 0
    for i in range(len(task1_optimal_path)):
        for j in range(len(task1_optimal_path[0])):
            if (task1_optimal_path[i, j] == task2_optimal_path[i, j]) & (
                    task1_optimal_path[i, j] == 1.0):
                overlap += 1
    print("Entire Iter:" + str(iter_task1 + iter_task2) + ",TASK1:" +
          str(iter_task1) + ",TASK2:" + str(iter_task2) + ",Overlap:" +
          str(overlap))

    train_writer.close()
    test_writer.close()
Ejemplo n.º 6
0
def train():
    ## Get imageNet dataset file queue for task1 and task2
    tr_data1, tr_label1 = imagenet_data.create_file_queue(
        FLAGS.imagenet_data_dir1)
    tr_data2, tr_label2 = imagenet_data.create_file_queue(
        FLAGS.imagenet_data_dir2)

    ## TASK 1
    sess = tf.InteractiveSession()

    # Input placeholders
    with tf.name_scope('input'):
        x = tf.placeholder(tf.float32, [None, 224 * 224 * 3], name='x-input')
        y_ = tf.placeholder(tf.float32, [None, 10], name='y-input')

    with tf.name_scope('input_reshape'):
        image_shaped_input = tf.reshape(x, [-1, 224, 224, 3])
        tf.summary.image('input', image_shaped_input, 2)

    # geopath_examples
    geopath = pathnet.geopath_initializer(FLAGS.L, FLAGS.M)

    # fixed weights list
    fixed_list = np.ones((FLAGS.L, FLAGS.M), dtype=str)
    for i in range(FLAGS.L):
        for j in range(FLAGS.M):
            fixed_list[i, j] = '0'

    # Hidden Layers
    weights_list = np.zeros((FLAGS.L, FLAGS.M), dtype=object)
    biases_list = np.zeros((FLAGS.L, FLAGS.M), dtype=object)

    # model define
    layer_modules_list = np.zeros(FLAGS.M, dtype=object)
    # conv layer
    i = 0
    for j in range(FLAGS.M):
        layer_modules_list[j], weights_list[i, j], biases_list[
            i,
            j] = pathnet.conv_module(image_shaped_input, FLAGS.filt, [11, 11],
                                     geopath[i, j], 1,
                                     'layer' + str(i + 1) + "_" + str(j + 1))
    net = np.sum(layer_modules_list) / FLAGS.M
    # dimensionality_reduction layer
    i = 1
    for j in range(FLAGS.M):
        layer_modules_list[j], weights_list[i, j], biases_list[
            i, j] = pathnet.Dimensionality_reduction_module(
                net, FLAGS.filt / 2, geopath[i, j],
                'layer' + str(i + 1) + "_" + str(j + 1))
    net = np.sum(layer_modules_list) / FLAGS.M
    # res_fire layer
    i = 2
    for j in range(FLAGS.M):
        layer_modules_list[j], weights_list[i, j], biases_list[
            i, j] = pathnet.res_fire_layer(
                net, FLAGS.filt / 2, geopath[i, j],
                'layer' + str(i + 1) + "_" + str(j + 1))
    net = np.sum(layer_modules_list) / FLAGS.M
    # dimensionality_reduction layer
    i = 3
    for j in range(FLAGS.M):
        layer_modules_list[j], weights_list[i, j], biases_list[
            i, j] = pathnet.Dimensionality_reduction_module(
                net, FLAGS.filt / 2, geopath[i, j],
                'layer' + str(i + 1) + "_" + str(j + 1))
    net = np.sum(layer_modules_list) / FLAGS.M
    # reshape before full connection layer
    _shape = net.shape[1:]
    _length = 1
    for _i in _shape:
        _length *= int(_i)
    net = tf.reshape(net, [-1, _length])
    # model1 layer
    i = 4
    for j in range(FLAGS.M):
        layer_modules_list[j], weights_list[i, j], biases_list[
            i, j] = pathnet.module(net, FLAGS.full_connection_filt, geopath[i,
                                                                            j],
                                   'layer' + str(i + 1) + "_" + str(j + 1))
    net = np.sum(layer_modules_list) / FLAGS.M

    # output layer
    y, output_weights, output_biases = pathnet.nn_layer(
        net, 10, 'output_layer')

    # Cross Entropy
    with tf.name_scope('cross_entropy'):
        diff = tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y)
        with tf.name_scope('total'):
            cross_entropy = tf.reduce_mean(diff)
    tf.summary.scalar('cross_entropy', cross_entropy)

    # Need to learn variables
    var_list_to_learn = [] + output_weights + output_biases
    for i in range(FLAGS.L):
        for j in range(FLAGS.M):
            if (fixed_list[i, j] == '0'):
                var_list_to_learn += weights_list[i, j] + biases_list[i, j]

    # GradientDescent
    with tf.name_scope('train'):
        train_step = tf.train.GradientDescentOptimizer(
            FLAGS.learning_rate).minimize(cross_entropy,
                                          var_list=var_list_to_learn)

    # Accuracy
    with tf.name_scope('accuracy'):
        with tf.name_scope('correct_prediction'):
            correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
        with tf.name_scope('accuracy'):
            accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    tf.summary.scalar('accuracy', accuracy)

    # Merge all the summaries and write them out to /tmp/tensorflow/mnist/logs/mnist_with_summaries (by default)
    merged = tf.summary.merge_all()
    train_writer = tf.summary.FileWriter(FLAGS.log_dir + '/train1', sess.graph)
    test_writer = tf.summary.FileWriter(FLAGS.log_dir + '/test1')

    # init
    tf.global_variables_initializer().run()
    tf.local_variables_initializer().run()

    # start data reading queue
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)

    # Generating randomly geopath
    geopath_set = np.zeros(FLAGS.candi, dtype=object)
    for i in range(FLAGS.candi):
        geopath_set[i] = pathnet.get_geopath(FLAGS.L, FLAGS.M, FLAGS.N)

    # parameters placeholders and ops
    var_update_ops = np.zeros(len(var_list_to_learn), dtype=object)
    var_update_placeholders = np.zeros(len(var_list_to_learn), dtype=object)
    for i in range(len(var_list_to_learn)):
        var_update_placeholders[i] = tf.placeholder(
            var_list_to_learn[i].dtype, shape=var_list_to_learn[i].get_shape())
        var_update_ops[i] = var_list_to_learn[i].assign(
            var_update_placeholders[i])

    # geopathes placeholders and ops
    geopath_update_ops = np.zeros((len(geopath), len(geopath[0])),
                                  dtype=object)
    geopath_update_placeholders = np.zeros((len(geopath), len(geopath[0])),
                                           dtype=object)
    for i in range(len(geopath)):
        for j in range(len(geopath[0])):
            geopath_update_placeholders[i, j] = tf.placeholder(
                geopath[i, j].dtype, shape=geopath[i, j].get_shape())
            geopath_update_ops[i, j] = geopath[i, j].assign(
                geopath_update_placeholders[i, j])

    acc_geo = np.zeros(FLAGS.B, dtype=float)
    summary_geo = np.zeros(FLAGS.B, dtype=object)

    for i in range(FLAGS.max_steps):
        # Select Candidates to Tournament
        compet_idx = range(FLAGS.candi)
        np.random.shuffle(compet_idx)
        compet_idx = compet_idx[:FLAGS.B]
        # Learning & Evaluating
        for j in range(len(compet_idx)):
            # Insert Candidate
            pathnet.geopath_insert(sess, geopath_update_placeholders,
                                   geopath_update_ops,
                                   geopath_set[compet_idx[j]], FLAGS.L,
                                   FLAGS.M)
            acc_geo_tr = 0
            for k in range(FLAGS.T):
                '''
        print(x.shape)
        print(tr_data1[k*FLAGS.batch_num:(k+1)*FLAGS.batch_num,:].shape)
        print(y.shape)
        print(tr_label1[k*FLAGS.batch_num:(k+1)*FLAGS.batch_num,:].shape)
        '''
                tr_data1_val, tr_label1_val = imagenet_data.read_batch(
                    sess, tr_data1, tr_label1, FLAGS.batch_num,
                    FLAGS.imagenet_data_dir1)
                summary_geo_tr, _, acc_geo_tmp = sess.run(
                    [merged, train_step, accuracy],
                    feed_dict={
                        x: tr_data1_val,
                        y_: tr_label1_val
                    })
                acc_geo_tr += acc_geo_tmp
            acc_geo[j] = acc_geo_tr / FLAGS.T
            summary_geo[j] = summary_geo_tr
        # Tournament
        winner_idx = np.argmax(acc_geo)
        acc = acc_geo[winner_idx]
        summary = summary_geo[winner_idx]
        # Copy and Mutation
        for j in range(len(compet_idx)):
            if (j != winner_idx):
                geopath_set[compet_idx[j]] = np.copy(
                    geopath_set[compet_idx[winner_idx]])
                geopath_set[compet_idx[j]] = pathnet.mutation(
                    geopath_set[compet_idx[j]], FLAGS.L, FLAGS.M, FLAGS.N)
        train_writer.add_summary(summary, i)
        print('Training Accuracy at step %s: %s' % (i, acc))

        if i == FLAGS.max_steps - 1:
            acc_task1 = acc
            task1_optimal_path = geopath_set[compet_idx[winner_idx]]
            print('Task1 Optimal Path is as followed.')
            print(task1_optimal_path)

    # Fix task1 Optimal Path
    for i in range(FLAGS.L):
        for j in range(FLAGS.M):
            if (task1_optimal_path[i, j] == 1.0):
                fixed_list[i, j] = '1'

    # Get variables of fixed list
    var_list_to_fix = []
    #var_list_to_fix=[]+output_weights+output_biases;
    for i in range(FLAGS.L):
        for j in range(FLAGS.M):
            if (fixed_list[i, j] == '1'):
                var_list_to_fix += weights_list[i, j] + biases_list[i, j]
    var_list_fix = pathnet.parameters_backup(var_list_to_fix)

    # parameters placeholders and ops
    var_fix_ops = np.zeros(len(var_list_to_fix), dtype=object)
    var_fix_placeholders = np.zeros(len(var_list_to_fix), dtype=object)
    for i in range(len(var_list_to_fix)):
        var_fix_placeholders[i] = tf.placeholder(
            var_list_to_fix[i].dtype, shape=var_list_to_fix[i].get_shape())
        var_fix_ops[i] = var_list_to_fix[i].assign(var_fix_placeholders[i])

    ## TASK 2
    # Need to learn variables
    var_list_to_learn = [] + output_weights + output_biases
    for i in range(FLAGS.L):
        for j in range(FLAGS.M):
            if (fixed_list[i, j] == '0'):
                var_list_to_learn += weights_list[i, j] + biases_list[i, j]

    for i in range(FLAGS.L):
        for j in range(FLAGS.M):
            if (fixed_list[i, j] == '1'):
                tmp = biases_list[i, j][0]
                break
        break

    # Initialization
    merged = tf.summary.merge_all()
    train_writer = tf.summary.FileWriter(FLAGS.log_dir + '/train2', sess.graph)
    test_writer = tf.summary.FileWriter(FLAGS.log_dir + '/test2')
    tf.global_variables_initializer().run()
    tf.local_variables_initializer().run()

    # Update fixed values
    pathnet.parameters_update(sess, var_fix_placeholders, var_fix_ops,
                              var_list_fix)

    # GradientDescent
    with tf.name_scope('train'):
        train_step = tf.train.GradientDescentOptimizer(
            FLAGS.learning_rate).minimize(cross_entropy,
                                          var_list=var_list_to_learn)

    # Generating randomly geopath
    geopath_set = np.zeros(FLAGS.candi, dtype=object)
    for i in range(FLAGS.candi):
        geopath_set[i] = pathnet.get_geopath(FLAGS.L, FLAGS.M, FLAGS.N)

    # parameters placeholders and ops
    var_update_ops = np.zeros(len(var_list_to_learn), dtype=object)
    var_update_placeholders = np.zeros(len(var_list_to_learn), dtype=object)
    for i in range(len(var_list_to_learn)):
        var_update_placeholders[i] = tf.placeholder(
            var_list_to_learn[i].dtype, shape=var_list_to_learn[i].get_shape())
        var_update_ops[i] = var_list_to_learn[i].assign(
            var_update_placeholders[i])

    acc_geo = np.zeros(FLAGS.B, dtype=float)
    summary_geo = np.zeros(FLAGS.B, dtype=object)
    for i in range(FLAGS.max_steps):
        # Select Candidates to Tournament
        compet_idx = range(FLAGS.candi)
        np.random.shuffle(compet_idx)
        compet_idx = compet_idx[:FLAGS.B]
        # Learning & Evaluating
        for j in range(len(compet_idx)):
            geopath_insert = np.copy(geopath_set[compet_idx[j]])
            for l in range(FLAGS.L):
                for m in range(FLAGS.M):
                    if (fixed_list[l, m] == '1'):
                        geopath_insert[l, m] = 1.0

            # Insert Candidate
            pathnet.geopath_insert(sess, geopath_update_placeholders,
                                   geopath_update_ops, geopath_insert, FLAGS.L,
                                   FLAGS.M)
            acc_geo_tr = 0
            for k in range(FLAGS.T):
                tr_data2_val, tr_label2_val = imagenet_data.read_batch(
                    sess, tr_data2, tr_label2, FLAGS.batch_num,
                    FLAGS.imagenet_data_dir2)
                summary_geo_tr, _, acc_geo_tmp = sess.run(
                    [merged, train_step, accuracy],
                    feed_dict={
                        x: tr_data2_val,
                        y_: tr_label2_val
                    })
                acc_geo_tr += acc_geo_tmp
            acc_geo[j] = acc_geo_tr / FLAGS.T
            summary_geo[j] = summary_geo_tr
        # Tournament
        winner_idx = np.argmax(acc_geo)
        acc = acc_geo[winner_idx]
        summary = summary_geo[winner_idx]
        # Copy and Mutation
        for j in range(len(compet_idx)):
            if (j != winner_idx):
                geopath_set[compet_idx[j]] = np.copy(
                    geopath_set[compet_idx[winner_idx]])
                geopath_set[compet_idx[j]] = pathnet.mutation(
                    geopath_set[compet_idx[j]], FLAGS.L, FLAGS.M, FLAGS.N)
        train_writer.add_summary(summary, i)
        print('Training Accuracy at step %s: %s' % (i, acc))

        if i == FLAGS.max_steps - 1:
            acc_task2 = acc
            task2_optimal_path = geopath_set[compet_idx[winner_idx]]
            print('Task2 Optimal Path is as followed.')
            print(task2_optimal_path)

    # close data reading queue
    coord.request_stop()
    coord.join(threads)

    overlap = 0
    for i in range(len(task1_optimal_path)):
        for j in range(len(task1_optimal_path[0])):
            if (task1_optimal_path[i, j] == task2_optimal_path[i, j]) & (
                    task1_optimal_path[i, j] == 1.0):
                overlap += 1
    print("ImageNet,TASK1:" + str(acc_task1) + ",TASK2:" + str(acc_task2) +
          ", Overlap:" + str(overlap))

    train_writer.close()
    test_writer.close()
def train():
    #initial learning rate
    initial_learning_rate = log_uniform(INITIAL_ALPHA_LOW,
                                        INITIAL_ALPHA_HIGH,
                                        INITIAL_ALPHA_LOG_RATE)

    # parameter server and worker information
    ps_hosts = np.zeros(FLAGS.ps_hosts_num,dtype=object);
    worker_hosts = np.zeros(FLAGS.worker_hosts_num,dtype=object);
    port_num=FLAGS.st_port_num;
    for i in range(FLAGS.ps_hosts_num):
        ps_hosts[i]=str(FLAGS.hostname)+":"+str(port_num);
        port_num+=1;
    for i in range(FLAGS.worker_hosts_num):
        worker_hosts[i]=str(FLAGS.hostname)+":"+str(port_num);
        port_num+=1;
    ps_hosts=list(ps_hosts);
    worker_hosts=list(worker_hosts);
    # Create a cluster from the parameter server and worker hosts.
    cluster = tf.train.ClusterSpec({"ps": ps_hosts, "worker": worker_hosts})

    # Create and start a server for the local task.
    server = tf.train.Server(cluster,
                             job_name=FLAGS.job_name,
                             task_index=FLAGS.task_index)


    if FLAGS.job_name == "ps":
        server.join();
    elif FLAGS.job_name == "worker":
        # gpu_assignment = FLAGS.task_index % NUM_GPUS
        # print("Assigning worker #%d to GPU #%d" % (FLAGS.task_index, gpu_assignment))
        # device=tf.train.replica_device_setter(
        #             worker_device="/job:worker/task:%d/gpu:%d" % (FLAGS.task_index, gpu_assignment),
        #             cluster=cluster);

        device=tf.train.replica_device_setter(
              worker_device="/job:worker/task:%d" % FLAGS.task_index,
              cluster=cluster);



        learning_rate_input = tf.placeholder("float")

        grad_applier = RMSPropApplier(learning_rate = learning_rate_input,
                                                                    decay = RMSP_ALPHA,
                                                                    momentum = 0.0,
                                                                    epsilon = RMSP_EPSILON,
                                                                    clip_norm = GRAD_NORM_CLIP,
                                                                    device = device)

        tf.set_random_seed(1);
        #There are no global network

        #lock = multiprocessing.Lock()

        #wrapper = ToDiscrete('constant-7')
        #env = wrapper(gym.make('gym_doom/DoomBasic-v0'))
        #env.close()

        training_thread = A3CTrainingThread(0,"",0,initial_learning_rate,learning_rate_input,grad_applier,MAX_TIME_STEP,device=device,FLAGS=FLAGS,task_index=FLAGS.task_index)

        # prepare session
        with tf.device(device):
            # flag for task
            flag = tf.get_variable('flag',[],initializer=tf.constant_initializer(0),trainable=False);
            flag_ph=tf.placeholder(flag.dtype,shape=flag.get_shape());
            flag_ops=flag.assign(flag_ph);
            # global step
            global_step = tf.get_variable('global_step',[],initializer=tf.constant_initializer(0),trainable=False);
            global_step_ph=tf.placeholder(global_step.dtype,shape=global_step.get_shape());
            global_step_ops=global_step.assign(global_step_ph);
            # score for tensorboard and score_set for genetic algorithm
            score = tf.get_variable('score',[],initializer=tf.constant_initializer(-21),trainable=False);
            score_ph=tf.placeholder(score.dtype,shape=score.get_shape());
            score_ops=score.assign(score_ph);
            score_set=np.zeros(FLAGS.worker_hosts_num,dtype=object);
            score_set_ph=np.zeros(FLAGS.worker_hosts_num,dtype=object);
            score_set_ops=np.zeros(FLAGS.worker_hosts_num,dtype=object);
            for i in range(FLAGS.worker_hosts_num):
                score_set[i] = tf.get_variable('score'+str(i),[],initializer=tf.constant_initializer(-1000),trainable=False);
                score_set_ph[i]=tf.placeholder(score_set[i].dtype,shape=score_set[i].get_shape());
                score_set_ops[i]=score_set[i].assign(score_set_ph[i]);
            # fixed path of earlier task
            fixed_path_tf=np.zeros((FLAGS.L,FLAGS.M),dtype=object);
            fixed_path_ph=np.zeros((FLAGS.L,FLAGS.M),dtype=object);
            fixed_path_ops=np.zeros((FLAGS.L,FLAGS.M),dtype=object);
            for i in range(FLAGS.L):
                for j in range(FLAGS.M):
                    fixed_path_tf[i,j]=tf.get_variable('fixed_path'+str(i)+"-"+str(j),[],initializer=tf.constant_initializer(0),trainable=False);
                    fixed_path_ph[i,j]=tf.placeholder(fixed_path_tf[i,j].dtype,shape=fixed_path_tf[i,j].get_shape());
                    fixed_path_ops[i,j]=fixed_path_tf[i,j].assign(fixed_path_ph[i,j]);
            # parameters on PathNet
            vars_=training_thread.local_network.get_vars();
            vars_ph=np.zeros(len(vars_),dtype=object);
            vars_ops=np.zeros(len(vars_),dtype=object);
            for i in range(len(vars_)):
                vars_ph[i]=tf.placeholder(vars_[i].dtype,shape=vars_[i].get_shape());
                vars_ops[i]=vars_[i].assign(vars_ph[i]);
            # initialization
            init_op=tf.global_variables_initializer();
            # summary for tensorboard
            tf.summary.scalar("score", score);
            summary_op = tf.summary.merge_all()
            saver = tf.train.Saver();

        sv = tf.train.Supervisor(is_chief=(FLAGS.task_index == 0),
                                 global_step=global_step,
                                 logdir=FLAGS.log_dir,
                                 summary_op=summary_op,
                                 saver=saver,
                                 init_op=init_op)
        try:
            os.mkdir("./data/graphs")
        except:
            pass

        # config = tf.ConfigProto(
        #         device_count = {'GPU': 0}
        #     )
        # config = tf.ConfigProto()
        # config.gpu_options.allow_growth = True
        # config.gpu_options.per_process_gpu_memory_fraction = 0.1

        with sv.managed_session(server.target) as sess:
            if(FLAGS.task_index!=(FLAGS.worker_hosts_num-1)):
                 for task in range(2):
                    training_thread.set_training_stage(task)

                    while sess.run([flag])[0] != (task+1):
                        time.sleep(2)

                    # Set fixed_path
                    fixed_path=np.zeros((FLAGS.L,FLAGS.M),dtype=float);
                    for i in range(FLAGS.L):
                        for j in range(FLAGS.M):
                            if(sess.run([fixed_path_tf[i,j]])[0]==1):
                                fixed_path[i,j]=1.0;
                    training_thread.local_network.set_fixed_path(fixed_path);
                    # set start_time
                    wall_t=0.0;
                    start_time = time.time() - wall_t
                    training_thread.set_start_time(start_time)
                    while True:
                        if sess.run([global_step])[0] > (MAX_TIME_STEP*(task+1)):
                            break
                        diff_global_t = training_thread.process(sess, sess.run([global_step])[0], "",
                                                                                                    summary_op, "",score_ph,score_ops,"",FLAGS,score_set_ph[FLAGS.task_index],score_set_ops[FLAGS.task_index])
                        sess.run(global_step_ops,{global_step_ph:sess.run([global_step])[0]+diff_global_t});
            else:
                fixed_path=np.zeros((FLAGS.L,FLAGS.M),dtype=float)
                vars_backup=np.zeros(len(vars_),dtype=object)
                vars_backup=sess.run(vars_)
                winner_idx=0

                vis = visualize.GraphVisualize([FLAGS.M] * FLAGS.L, True)


                for task in range(2):
                    # Generating randomly geopath
                    geopath_set=np.zeros(FLAGS.worker_hosts_num-1,dtype=object);
                    for i in range(FLAGS.worker_hosts_num-1):
                        geopath_set[i]=pathnet.get_geopath(FLAGS.L,FLAGS.M,FLAGS.N);
                        tmp=np.zeros((FLAGS.L,FLAGS.M),dtype=float);
                        for j in range(FLAGS.L):
                            for k in range(FLAGS.M):
                                if((geopath_set[i][j,k]==1.0)or(fixed_path[j,k]==1.0)):
                                    tmp[j,k]=1.0;
                        pathnet.geopath_insert(sess,training_thread.local_network.geopath_update_placeholders_set[i],training_thread.local_network.geopath_update_ops_set[i],tmp,FLAGS.L,FLAGS.M);
                    print("Geopath Setting Done");
                    sess.run(flag_ops,{flag_ph:(task+1)});
                    print("=============Task "+str(task+1)+"============");
                    score_subset=np.zeros(FLAGS.B,dtype=float);
                    score_set_print=np.zeros(FLAGS.worker_hosts_num,dtype=float);
                    rand_idx=np.arange(FLAGS.worker_hosts_num-1);
                    np.random.shuffle(rand_idx);
                    rand_idx=rand_idx[:FLAGS.B];
                    while sess.run([global_step])[0] <= (MAX_TIME_STEP*(task+1)):
                        # if (sess.run([global_step])[0]) % 1000 == 0:
                        #     print("Saving summary...")
                        #     tf.logging.info('Running Summary operation on the chief.')
                        #     summary_str = sess.run(summary_op)
                        #     sv.summary_computed(sess, summary_str)
                        #     tf.logging.info('Finished running Summary operation.')
                        #
                        #     # Determine the next time for running the summary.


                        decodePath = lambda p: [np.where(l==1.0)[0] for l in p]

                        flag_sum=0;
                        for i in range(FLAGS.worker_hosts_num-1):
                            score_set_print[i]=sess.run([score_set[i]])[0];
                        for i in range(len(rand_idx)):
                            score_subset[i]=sess.run([score_set[rand_idx[i]]])[0];
                            if(score_subset[i]==-1000):
                                flag_sum=1;
                                break;
                        if(flag_sum==0):
                            vispaths = [np.array(decodePath(p)) for p in geopath_set]
                            vis.show(vispaths, 'm')

                            winner_idx=rand_idx[np.argmax(score_subset)];
                            print(str(sess.run([global_step])[0])+" Step Score: "+str(sess.run([score_set[winner_idx]])[0]));
                            for i in rand_idx:
                                if(i!=winner_idx):
                                    geopath_set[i]=np.copy(geopath_set[winner_idx]);
                                    geopath_set[i]=pathnet.mutation(geopath_set[i],FLAGS.L,FLAGS.M,FLAGS.N);
                                    tmp=np.zeros((FLAGS.L,FLAGS.M),dtype=float);
                                    for j in range(FLAGS.L):
                                        for k in range(FLAGS.M):
                                            if((geopath_set[i][j,k]==1.0)or(fixed_path[j,k]==1.0)):
                                                tmp[j,k]=1.0;
                                    pathnet.geopath_insert(sess,training_thread.local_network.geopath_update_placeholders_set[i],training_thread.local_network.geopath_update_ops_set[i],tmp,FLAGS.L,FLAGS.M);
                                sess.run(score_set_ops[i],{score_set_ph[i]:-1000})
                            rand_idx=np.arange(FLAGS.worker_hosts_num-1)
                            np.random.shuffle(rand_idx)
                            rand_idx=rand_idx[:FLAGS.B]
                        else:
                            time.sleep(2);
                    # fixed_path setting
                    fixed_path=geopath_set[winner_idx]

                    vis.set_fixed(decodePath(fixed_path), 'r' if task == 0 else 'g')
                    vis.show(vispaths, 'm')
                    print('fix')
                    for i in range(FLAGS.L):
                        for j in range(FLAGS.M):
                            if(fixed_path[i,j]==1.0):
                                sess.run(fixed_path_ops[i,j],{fixed_path_ph[i,j]:1});
                    training_thread.local_network.set_fixed_path(fixed_path);

                    # backup fixed vars
                    # FIXED_VARS_BACKUP = training_thread.local_network.get_fixed_vars();
                    # FIXED_VARS_IDX_BACKUP = training_thread.local_network.get_fixed_vars_idx();

                    # initialization of parameters except fixed_path
                    vars_idx=training_thread.local_network.get_vars_idx();
                    for i in range(len(vars_idx)):
                        if(vars_idx[i]==1.0):
                            sess.run(vars_ops[i],{vars_ph[i]:vars_backup[i]});

                vis.waitForButtonPress()
        sv.stop();