Exemple #1
0
def main():
    net_conf = NetConfig()
    net_conf.set_conf("./net_conf.txt")
    q_units = net_conf.inference_num_units
    q_layers = net_conf.inference_num_layers
    p_units = net_conf.prediction_num_units
    p_layers = net_conf.prediction_num_layers
    latent_dim = net_conf.latent_dim
    beta = net_conf.regularize_const
    
    train_conf = TrainConfig()
    train_conf.set_conf("./train_conf.txt")
    seed = train_conf.seed
    epoch = train_conf.epoch
    batch_len = train_conf.batch_length
    batchsize = train_conf.batchsize
    data_dim = 128
    
    save_dir = train_conf.save_dir
    if not os.path.exists(save_dir):
        os.mkdir(save_dir)
    save_file = os.path.join(save_dir, "model.ckpt")

    # data preparation
    train_data = read_target(train_conf.train_dir, data_dim)
    train_num = train_data.shape[1]
    train_len = train_data.shape[0]
        
    if train_conf.test:
        test_data = read_target(train_conf.test_dir)
        test_len = test_data.shape[0]
        test_num = test_data.shape[1]
    
    np.random.seed(seed)
    tf.reset_default_graph()
    tf.set_random_seed(seed)

    global_step = tf.Variable(0, name="global_step", trainable=False)

    x = tf.placeholder(tf.float32, [batch_len, batchsize, data_dim])
    z_mean, z_log_var = inference(x, q_units, q_layers, latent_dim)
    y = generation(x, z_mean, z_log_var, p_units, p_layers, data_dim)

    with tf.name_scope('loss'):
        recon_loss = tf.reduce_mean(-tf.reduce_sum(x*tf.log(y), axis=2))
        latent_loss = -tf.reduce_mean(tf.reduce_sum(1 + z_log_var - tf.square(z_mean) - tf.exp(z_log_var), axis=1))
        loss = recon_loss + beta * latent_loss
        
    train_step = tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss, global_step=global_step)

    gpuConfig = tf.ConfigProto(
        gpu_options=tf.GPUOptions(
            allow_growth=True,
            per_process_gpu_memory_fraction=train_conf.gpu_use_rate),
        device_count={'GPU': 1})
        
    sess = tf.Session(config=gpuConfig)
    sess.run(tf.global_variables_initializer())
    saver = tf.train.Saver(tf.global_variables())
    
    ckpt = tf.train.get_checkpoint_state(save_dir)
    latest_model = ckpt.model_checkpoint_path
    saver.restore(sess, latest_model)

    error_log = []
    for itr in range(epoch):
        batch_idx = np.random.permutation(train_num)[:batchsize]
        start_pos = np.random.randint(train_len - batch_len)
        batch = train_data[start_pos:start_pos+batch_len, batch_idx, :]
        feed_dict = {x: batch}
        _, latent, recon, total, step = sess.run([train_step,
                                                  latent_loss,
                                                  recon_loss,
                                                  loss,
                                                  global_step],
                                                 feed_dict=feed_dict)
        error_log.append([step, latent, recon, total])
        print "step:{} latent:{}, recon:{}, total:{}".format(step, latent, recon, total)
        
        if train_conf.test and itr % train_conf.test_interval == 0:
            batch_idx = np.random.permutation(test_num)[:batchsize]
            start_pos = np.random.randint(test_len - batch_len)
            batch = test_data[start_pos:start_pos+batch_len, batch_idx, :]
            feed_dict = {x: batch}
            latent, recon, total = sess.run([latent_loss,
                                             recon_loss,
                                             loss],
                                            feed_dict=feed_dict)
            print "-------test--------"
            print "step:{} latent:{}, recon:{}, total:{}".format(step, latent, recon, total)
            print "-------------------"
            
        if step % train_conf.log_interval == 0:
            saver.save(sess, save_file, global_step=global_step)

    error_log = np.array(error_log)
    old_error_log = np.loadtxt("error.log")
    np.savetxt("error.log", np.r_[old_error_log, error_log])
Exemple #2
0
def main():
    net_conf = NetConfig()
    net_conf.set_conf("./net_conf.txt")

    L_num_units = net_conf.L_num_units
    L_num_layers = net_conf.L_num_layers
    VB_num_units = net_conf.VB_num_units
    VB_num_layers = net_conf.VB_num_layers
    SHARE_DIM = net_conf.S_dim

    train_conf = TrainConfig()
    train_conf.set_conf("./train_conf.txt")

    seed = train_conf.seed
    batchsize = 1
    epoch = train_conf.epoch
    save_dir = train_conf.save_dir

    L_data_dir = train_conf.L_dir
    B_data_dir = train_conf.B_dir
    V_data_dir = train_conf.V_dir
    L_fw, L_bw, L_bin, L_len, L_filenames = read_sequential_target(
        L_data_dir, True)
    print len(L_filenames)
    B_fw, B_bw, B_bin, B_len, B_filenames = read_sequential_target(
        B_data_dir, True)
    V_fw, V_bw, V_bin, V_len = read_sequential_target(V_data_dir)
    L_shape = L_fw.shape
    B_shape = B_fw.shape
    V_shape = V_fw.shape

    if train_conf.test:
        L_data_dir_test = train_conf.L_dir_test
        B_data_dir_test = train_conf.B_dir_test
        V_data_dir_test = train_conf.V_dir_test
        L_fw_u, L_bw_u, L_bin_u, L_len_u, L_filenames_u = read_sequential_target(
            L_data_dir_test, True)
        print len(L_filenames_u)
        B_fw_u, B_bw_u, B_bin_u, B_len_u, B_filenames_u = read_sequential_target(
            B_data_dir_test, True)
        V_fw_u, V_bw_u, V_bin_u, V_len_u = read_sequential_target(
            V_data_dir_test)
        L_shape_u = L_fw_u.shape
        B_shape_u = B_fw_u.shape
        V_shape_u = V_fw_u.shape

    tf.reset_default_graph()
    placeholders = make_placeholders(L_shape, B_shape, V_shape, batchsize)

    ##### encoding #####
    L_enc_final_state = encoder(placeholders["L_bw"],
                                placeholders["L_len"],
                                n_units=L_num_units,
                                n_layers=L_num_layers,
                                scope="L_encoder")

    VB_input = tf.concat([placeholders["V_bw"], placeholders["B_bw"]], axis=2)
    VB_enc_final_state = encoder(VB_input,
                                 placeholders["V_len"],
                                 n_units=VB_num_units,
                                 n_layers=VB_num_layers,
                                 scope="VB_encoder")

    ##### sharing #####
    L_shared = fc(L_enc_final_state,
                  SHARE_DIM,
                  activation_fn=None,
                  scope="L_share")
    VB_shared = fc(VB_enc_final_state,
                   SHARE_DIM,
                   activation_fn=None,
                   scope="VB_share")

    gpuConfig = tf.ConfigProto(gpu_options=tf.GPUOptions(
        allow_growth=True,
        per_process_gpu_memory_fraction=train_conf.gpu_use_rate),
                               device_count={'GPU': 1})

    sess = tf.Session(config=gpuConfig)
    sess.run(tf.global_variables_initializer())
    saver = tf.train.Saver(tf.global_variables())
    saver.restore(sess, save_dir)

    for i in range(B_shape[1]):
        feed_dict = {
            placeholders["L_fw"]: L_fw[:, i:i + 1, :],
            placeholders["B_fw"]: B_fw[:, i:i + 1, :],
            placeholders["V_fw"]: V_fw[:, i:i + 1, :],
            placeholders["L_bw"]: L_bw[:, i:i + 1, :],
            placeholders["B_bw"]: B_bw[:, i:i + 1, :],
            placeholders["V_bw"]: V_bw[:, i:i + 1, :],
            placeholders["B_bin"]: B_bin[:, i:i + 1, :],
            placeholders["L_len"]: L_len[i:i + 1],
            placeholders["V_len"]: V_len[i:i + 1]
        }

        L_enc, VB_enc = sess.run([L_shared, VB_shared], feed_dict=feed_dict)
        save_latent(L_enc, L_filenames[i])
        save_latent(VB_enc, B_filenames[i])
        print B_filenames[i]

    if train_conf.test:
        for i in range(B_shape_u[1]):
            feed_dict = {
                placeholders["L_fw"]: L_fw_u[:, i:i + 1, :],
                placeholders["B_fw"]: B_fw_u[:, i:i + 1, :],
                placeholders["V_fw"]: V_fw_u[:, i:i + 1, :],
                placeholders["L_bw"]: L_bw_u[:, i:i + 1, :],
                placeholders["B_bw"]: B_bw_u[:, i:i + 1, :],
                placeholders["V_bw"]: V_bw_u[:, i:i + 1, :],
                placeholders["B_bin"]: B_bin_u[:, i:i + 1, :],
                placeholders["L_len"]: L_len_u[i:i + 1],
                placeholders["V_len"]: V_len_u[i:i + 1]
            }
            L_enc, VB_enc = sess.run([L_shared, VB_shared],
                                     feed_dict=feed_dict)
            save_latent(L_enc, L_filenames_u[i])
            save_latent(VB_enc, B_filenames_u[i])
            print B_filenames_u[i]
Exemple #3
0
def main():
    net_conf = NetConfig()
    net_conf.set_conf("./net_conf.txt")

    L_num_units = net_conf.L_num_units
    L_num_layers = net_conf.L_num_layers
    VB_num_units = net_conf.VB_num_units
    VB_num_layers = net_conf.VB_num_layers
    SHARE_DIM = net_conf.S_dim

    train_conf = TrainConfig()
    train_conf.set_conf("./train_conf.txt")

    batchsize = 1
    save_dir = train_conf.save_dir

    L_data_dir = train_conf.L_dir
    B_data_dir = train_conf.B_dir
    V_data_dir = train_conf.V_dir
    L_fw, L_bw, L_bin, L_len, L_filenames = read_sequential_target(
        L_data_dir, True)
    print len(L_filenames)
    B_fw, B_bw, B_bin, B_len, B_filenames = read_sequential_target(
        B_data_dir, True)
    V_fw, V_bw, V_bin, V_len = read_sequential_target(V_data_dir)
    L_shape = L_fw.shape
    B_shape = B_fw.shape
    V_shape = V_fw.shape

    if train_conf.test:
        L_data_dir_test = train_conf.L_dir_test
        B_data_dir_test = train_conf.B_dir_test
        V_data_dir_test = train_conf.V_dir_test
        L_fw_u, L_bw_u, L_bin_u, L_len_u, L_filenames_u = read_sequential_target(
            L_data_dir_test, True)
        print len(L_filenames_u)
        B_fw_u, B_bw_u, B_bin_u, B_len_u, B_filenames_u = read_sequential_target(
            B_data_dir_test, True)
        V_fw_u, V_bw_u, V_bin_u, V_len_u = read_sequential_target(
            V_data_dir_test)
        L_shape_u = L_fw_u.shape
        B_shape_u = B_fw_u.shape
        V_shape_u = V_fw_u.shape

    tf.reset_default_graph()

    placeholders = make_placeholders(L_shape, B_shape, V_shape, batchsize)

    ##### encoding #####
    VB_input = tf.concat([placeholders["V_bw"], placeholders["B_bw"]], axis=2)
    VB_enc_final_state = encoder(VB_input,
                                 placeholders["V_len"],
                                 n_units=VB_num_units,
                                 n_layers=VB_num_layers,
                                 scope="VB_encoder")

    ##### sharing #####
    VB_shared = fc(VB_enc_final_state,
                   SHARE_DIM,
                   activation_fn=None,
                   scope="VB_share")
    L_dec_init_state = fc(VB_shared,
                          L_num_units * L_num_layers * 2,
                          activation_fn=None,
                          scope="L_postshare")

    ##### decoding #####
    L_output = L_decoder(placeholders["L_fw"][0],
                         L_dec_init_state,
                         length=L_shape[0],
                         out_dim=L_shape[2],
                         n_units=L_num_units,
                         n_layers=L_num_layers,
                         scope="L_decoder")
    L_output = tf.contrib.seq2seq.hardmax(L_output)

    gpuConfig = tf.ConfigProto(gpu_options=tf.GPUOptions(
        allow_growth=True,
        per_process_gpu_memory_fraction=train_conf.gpu_use_rate),
                               device_count={'GPU': 1})

    sess = tf.Session(config=gpuConfig)
    sess.run(tf.global_variables_initializer())
    saver = tf.train.Saver(tf.global_variables())
    saver.restore(sess, save_dir)

    for i in range(B_shape[1]):
        feed_dict = {
            placeholders["L_fw"]: L_fw[:, i:i + 1, :],
            placeholders["B_fw"]: B_fw[:, i:i + 1, :],
            placeholders["V_fw"]: V_fw[:, i:i + 1, :],
            placeholders["L_bw"]: L_bw[:, i:i + 1, :],
            placeholders["B_bw"]: B_bw[:, i:i + 1, :],
            placeholders["V_bw"]: V_bw[:, i:i + 1, :],
            placeholders["B_bin"]: B_bin[:, i:i + 1, :],
            placeholders["L_len"]: L_len[i:i + 1],
            placeholders["V_len"]: V_len[i:i + 1]
        }

        result = sess.run([L_output], feed_dict=feed_dict)
        r = result[0][:, 0, :].argmax(axis=1)
        t = L_fw[1:, i, :].argmax(axis=1)
        print(r == t).all()

    if train_conf.test:
        print "test!!!!!!!!!!!!!!!!"
        for i in range(B_shape_u[1]):
            feed_dict = {
                placeholders["L_fw"]: L_fw_u[:, i:i + 1, :],
                placeholders["B_fw"]: B_fw_u[:, i:i + 1, :],
                placeholders["V_fw"]: V_fw_u[:, i:i + 1, :],
                placeholders["L_bw"]: L_bw_u[:, i:i + 1, :],
                placeholders["B_bw"]: B_bw_u[:, i:i + 1, :],
                placeholders["V_bw"]: V_bw_u[:, i:i + 1, :],
                placeholders["B_bin"]: B_bin_u[:, i:i + 1, :],
                placeholders["L_len"]: L_len_u[i:i + 1],
                placeholders["V_len"]: V_len_u[i:i + 1]
            }
            result = sess.run([L_output], feed_dict=feed_dict)
            result = result[0][:, 0, :].argmax(axis=1)
            target = L_fw_u[1:, i, :].argmax(axis=1)
            print(result == target).all()
Exemple #4
0
def main():
    encoder_conf = open("./encoder.json", "r")
    encoder_layers = json.load(encoder_conf)
    decoder_conf = open("./decoder.json", "r")
    decoder_layers = json.load(decoder_conf)

    net_conf = NetConfig()
    net_conf.set_conf("./net_conf.txt")
    q_units = net_conf.inference_num_units
    q_layers = net_conf.inference_num_layers
    p_units = net_conf.prediction_num_units
    p_layers = net_conf.prediction_num_layers
    latent_dim = net_conf.latent_dim
    beta = net_conf.regularize_const

    train_conf = TrainConfig()
    train_conf.set_conf("./train_conf.txt")
    seed = train_conf.seed
    epoch = train_conf.epoch
    save_dir = train_conf.save_dir + "/"
    save_file = os.path.join(save_dir, "model.ckpt")

    data_file = train_conf.data_file
    if not os.path.exists(data_file):
        if not os.path.exists(os.path.dirname(data_file)):
            os.mkdir(os.path.dirname(data_file))
        import wget
        url = "http://www.cs.toronto.edu/~nitish/unsupervised_video/mnist_test_seq.npy"
        wget.download(url, out=data_file)
    data = np.load(data_file)
    data = (data / 255.) * 0.9 + 0.05
    train_data = data[:, :8000, :, :]
    test_data = data[:, 8000:, :, :]

    data_len = train_data.shape[0]
    batchsize = train_conf.batchsize
    height = train_data.shape[2]
    width = train_data.shape[3]
    n_channel = 1

    np.random.seed(seed)
    tf.compat.v1.reset_default_graph()
    tf.compat.v1.set_random_seed(seed)

    global_step = tf.Variable(0, name="global_step", trainable=False)

    x = tf.compat.v1.placeholder(
        tf.float32, [data_len, batchsize, height, width, n_channel])
    reshaped_x = tf.reshape(x, [-1, height, width, n_channel])

    with tf.compat.v1.variable_scope("encoder", reuse=False):
        h = image_processor(reshaped_x, encoder_layers)
    h = tf.reshape(h, [data_len, batchsize, -1])

    z_post_mean, z_post_log_var = inference(h, q_units, q_layers, latent_dim)
    z_prior_mean = tf.zeros(z_post_mean.get_shape().as_list(), tf.float32)
    z_prior_log_var = tf.ones(z_post_log_var.get_shape().as_list(), tf.float32)

    is_train = tf.compat.v1.placeholder(tf.bool)
    z_mean = tf.cond(pred=is_train,
                     true_fn=lambda: z_post_mean,
                     false_fn=lambda: z_prior_mean)
    z_log_var = tf.cond(pred=is_train,
                        true_fn=lambda: z_post_log_var,
                        false_fn=lambda: z_prior_log_var)
    g = generation(h, z_mean, z_log_var, p_units, p_layers,
                   int(h.get_shape()[2]))
    g = tf.reshape(g, [-1, int(h.get_shape()[2])])
    with tf.compat.v1.variable_scope("decoder", reuse=False):
        reshaped_y = image_processor(g, decoder_layers)
    y = tf.reshape(reshaped_y, x.get_shape().as_list())
    with tf.compat.v1.name_scope('loss'):
        # mse is ok? not cross entropy?
        recon_loss = tf.reduce_mean(input_tensor=tf.square(x - y))
        latent_loss = -tf.reduce_mean(input_tensor=1 + z_log_var -
                                      tf.square(z_mean) - tf.exp(z_log_var))
        # what is the appropriate beta...
        loss = recon_loss + beta * latent_loss

    train_step = tf.compat.v1.train.AdamOptimizer(
        learning_rate=0.001).minimize(loss, global_step=global_step)

    gpuConfig = tf.compat.v1.ConfigProto(gpu_options=tf.compat.v1.GPUOptions(
        allow_growth=True,
        per_process_gpu_memory_fraction=train_conf.gpu_use_rate),
                                         device_count={'GPU': 1})

    sess = tf.compat.v1.Session(config=gpuConfig)
    sess.run(tf.compat.v1.global_variables_initializer())
    saver = tf.compat.v1.train.Saver(tf.compat.v1.global_variables())

    ckpt = tf.train.get_checkpoint_state(save_dir)
    latest_model = ckpt.model_checkpoint_path
    saver.restore(sess, latest_model)

    error_log = []
    for itr in range(epoch):
        batch_idx = np.random.permutation(8000)[:batchsize]
        batch = train_data[:, batch_idx, :, :]
        batch = np.reshape(batch, list(batch.shape) + [1])
        feed_dict = {is_train: True, x: batch}
        _, latent, recon, total, step = sess.run(
            [train_step, latent_loss, recon_loss, loss, global_step],
            feed_dict=feed_dict)
        error_log.append([step, latent, recon, total])
        print("step:{} latent:{}, recon:{}, total:{}".format(
            step, latent, recon, total))
        if train_conf.test and (step + 1) % train_conf.test_interval == 0:
            batch_idx = np.random.permutation(2000)[:batchsize]
            batch = test_data[:, batch_idx, :, :]
            batch = np.reshape(batch, list(batch.shape) + [1])
            feed_dict = {is_train: False, x: batch}
            latent, recon, total = sess.run([latent_loss, recon_loss, loss],
                                            feed_dict=feed_dict)
            print("test.")
            print("step:{} latent:{}, recon:{}, total:{}".format(
                step, latent, recon, total))
        if step % train_conf.log_interval == 0:
            saver.save(sess, save_file, global_step=global_step)
    error_log = np.array(error_log)
    old_error_log = np.loadtxt("error.log")
    np.savetxt("error.log", np.r_[old_error_log, error_log])
Exemple #5
0
def main():
    net_conf = NetConfig()
    net_conf.set_conf("./net_conf.txt")

    L_num_units = net_conf.L_num_units
    L_num_layers = net_conf.L_num_layers
    VB_num_units = net_conf.VB_num_units
    VB_num_layers = net_conf.VB_num_layers
    SHARE_DIM = net_conf.S_dim
    
    train_conf = TrainConfig()
    train_conf.set_conf("./train_conf.txt")
    
    batchsize = 1
    save_dir = train_conf.save_dir
    
    L_data_dir = train_conf.L_dir
    B_data_dir = train_conf.B_dir
    V_data_dir = train_conf.V_dir
    L_fw, L_bw, L_bin, L_len, L_filenames = read_sequential_target(L_data_dir, True)
    print len(L_filenames)
    B_fw, B_bw, B_bin, B_len, B_filenames = read_sequential_target(B_data_dir, True)
    V_fw, V_bw, V_bin, V_len = read_sequential_target(V_data_dir)
    L_shape = L_fw.shape
    B_shape = B_fw.shape
    V_shape = V_fw.shape

    if train_conf.test:
        L_data_dir_test = train_conf.L_dir_test
        B_data_dir_test = train_conf.B_dir_test
        V_data_dir_test = train_conf.V_dir_test
        L_fw_u, L_bw_u, L_bin_u, L_len_u, L_filenames_u = read_sequential_target(L_data_dir_test, True)
        print len(L_filenames_u)
        B_fw_u, B_bw_u, B_bin_u, B_len_u, B_filenames_u = read_sequential_target(B_data_dir_test, True)
        V_fw_u, V_bw_u, V_bin_u, V_len_u = read_sequential_target(V_data_dir_test)
        L_shape_u = L_fw_u.shape
        B_shape_u = B_fw_u.shape
        V_shape_u = V_fw_u.shape

    placeholders = make_placeholders(L_shape, B_shape, V_shape, batchsize)

    ##### encoding #####
    L_enc_final_state = encoder(placeholders["L_bw"],
                                placeholders["L_len"],
                                n_units=L_num_units,
                                n_layers=L_num_layers,
                                scope="L_encoder")
    
    ##### sharing #####
    L_shared = fc(L_enc_final_state, SHARE_DIM,
                  activation_fn=None, scope="L_share")
    VB_dec_init_state = fc(L_shared, VB_num_units*VB_num_layers*2,
                           activation_fn=None, scope="VB_postshare")
    
    ##### decoding #####
    B_output = VB_decoder(placeholders["V_fw"],
                          placeholders["B_fw"][0],
                          VB_dec_init_state,
                          length=B_shape[0],
                          out_dim=B_shape[2],
                          n_units=VB_num_units,
                          n_layers=VB_num_layers,
                          scope="VB_decoder")
    
    gpuConfig = tf.ConfigProto(
        gpu_options=tf.GPUOptions(allow_growth=True,
                                  per_process_gpu_memory_fraction=train_conf.gpu_use_rate),
        device_count={'GPU': 1})
        
    sess = tf.Session(config=gpuConfig)
    sess.run(tf.global_variables_initializer())
    saver = tf.train.Saver(tf.global_variables())
    saver.restore(sess, save_dir)

    for i in range(B_shape[1]):
        feed_dict = {placeholders["L_fw"]: L_fw[:, i:i+1, :],
                     placeholders["B_fw"]: B_fw[:, i:i+1, :],
                     placeholders["V_fw"]: V_fw[:, i:i+1, :],
                     placeholders["L_bw"]: L_bw[:, i:i+1, :],
                     placeholders["B_bw"]: B_bw[:, i:i+1, :],
                     placeholders["V_bw"]: V_bw[:, i:i+1, :],
                     placeholders["B_bin"]: B_bin[:, i:i+1, :],
                     placeholders["L_len"]: L_len[i:i+1],
                     placeholders["V_len"]: V_len[i:i+1]}
                    
        result = sess.run(B_output, feed_dict=feed_dict)
        save_latent(np.transpose(result, (1,0,2)), B_filenames[i], "generation")
    
    if train_conf.test:
        for i in range(B_shape_u[1]):
            feed_dict = {placeholders["L_fw"]: L_fw_u[:, i:i+1, :],
                         placeholders["B_fw"]: B_fw_u[:, i:i+1, :],
                         placeholders["V_fw"]: V_fw_u[:, i:i+1, :],
                         placeholders["L_bw"]: L_bw_u[:, i:i+1, :],
                         placeholders["B_bw"]: B_bw_u[:, i:i+1, :],
                         placeholders["V_bw"]: V_bw_u[:, i:i+1, :],
                         placeholders["B_bin"]: B_bin_u[:, i:i+1, :],
                         placeholders["L_len"]: L_len_u[i:i+1],
                         placeholders["V_len"]: V_len_u[i:i+1]}
            result = sess.run(B_output, feed_dict=feed_dict)
            save_latent(np.transpose(result, (1,0,2)), B_filenames_u[i], "generation")
Exemple #6
0
def main():
    net_conf = NetConfig()
    net_conf.set_conf("./net_conf.txt")

    L_num_units = net_conf.L_num_units
    L_num_layers = net_conf.L_num_layers
    VB_num_units = net_conf.VB_num_units
    VB_num_layers = net_conf.VB_num_layers
    SHARE_DIM = net_conf.S_dim
    
    train_conf = TrainConfig()
    train_conf.set_conf("./train_conf.txt")
    
    seed = train_conf.seed
    batchsize = 1
    epoch = train_conf.epoch
    save_dir = train_conf.save_dir
    
    L_data_dir = train_conf.L_dir
    B_data_dir = train_conf.B_dir
    V_data_dir = train_conf.V_dir
    L_fw, L_bw, L_bin, L_len, L_filenames = read_sequential_target(L_data_dir, True)
    print len(L_filenames)
    B_fw, B_bw, B_bin, B_len, B_filenames = read_sequential_target(B_data_dir, True)
    V_fw, V_bw, V_bin, V_len = read_sequential_target(V_data_dir)
    L_shape = L_fw.shape
    B_shape = B_fw.shape
    V_shape = V_fw.shape

    if train_conf.test:
        L_data_dir_test = train_conf.L_dir_test
        B_data_dir_test = train_conf.B_dir_test
        V_data_dir_test = train_conf.V_dir_test
        L_fw_u, L_bw_u, L_bin_u, L_len_u, L_filenames_u = read_sequential_target(L_data_dir_test, True)
        print len(L_filenames_u)
        B_fw_u, B_bw_u, B_bin_u, B_len_u, B_filenames_u = read_sequential_target(B_data_dir_test, True)
        V_fw_u, V_bw_u, V_bin_u, V_len_u = read_sequential_target(V_data_dir_test)
        L_shape_u = L_fw_u.shape
        B_shape_u = B_fw_u.shape
        V_shape_u = V_fw_u.shape
        
    tf.reset_default_graph()
    placeholders = make_placeholders(L_shape, B_shape, V_shape, batchsize)

    ##### encoding #####
    L_enc_final_state = encoder(placeholders["L_bw"],
                                placeholders["L_len"],
                                n_units=L_num_units,
                                n_layers=L_num_layers,
                                scope="L_encoder")

    VB_input = tf.concat([placeholders["V_bw"],
                          placeholders["B_bw"]],
                         axis=2)
    VB_enc_final_state = encoder(VB_input,
                                 placeholders["V_len"], 
                                 n_units=VB_num_units,
                                 n_layers=VB_num_layers,
                                 scope="VB_encoder")

    ##### sharing #####
    L_shared = fc(L_enc_final_state, SHARE_DIM,
                  activation_fn=None, scope="L_share")
    VB_shared = fc(VB_enc_final_state, SHARE_DIM,
                   activation_fn=None, scope="VB_share")  
    
    gpuConfig = tf.ConfigProto(
        gpu_options=tf.GPUOptions(allow_growth=True,
                                  per_process_gpu_memory_fraction=train_conf.gpu_use_rate),
        device_count={'GPU': 1})
        
    sess = tf.Session(config=gpuConfig)
    sess.run(tf.global_variables_initializer())
    saver = tf.train.Saver(tf.global_variables())
    saver.restore(sess, save_dir)    

    for i in range(B_shape[1]):
        feed_dict = {placeholders["L_fw"]: L_fw[:, i:i+1, :],
                     placeholders["B_fw"]: B_fw[:, i:i+1, :],
                     placeholders["V_fw"]: V_fw[:, i:i+1, :],
                     placeholders["L_bw"]: L_bw[:, i:i+1, :],
                     placeholders["B_bw"]: B_bw[:, i:i+1, :],
                     placeholders["V_bw"]: V_bw[:, i:i+1, :],
                     placeholders["B_bin"]: B_bin[:, i:i+1, :],
                     placeholders["L_len"]: L_len[i:i+1],
                     placeholders["V_len"]: V_len[i:i+1]}

        L_enc, VB_enc = sess.run([L_shared, VB_shared],
                                  feed_dict=feed_dict)
        save_latent(L_enc, L_filenames[i])
        save_latent(VB_enc, B_filenames[i])
        print B_filenames[i]

    if train_conf.test:
        for i in range(B_shape_u[1]):
            feed_dict = {placeholders["L_fw"]: L_fw_u[:, i:i+1, :],
                         placeholders["B_fw"]: B_fw_u[:, i:i+1, :],
                         placeholders["V_fw"]: V_fw_u[:, i:i+1, :],
                         placeholders["L_bw"]: L_bw_u[:, i:i+1, :],
                         placeholders["B_bw"]: B_bw_u[:, i:i+1, :],
                         placeholders["V_bw"]: V_bw_u[:, i:i+1, :],
                         placeholders["B_bin"]: B_bin_u[:, i:i+1, :],
                         placeholders["L_len"]: L_len_u[i:i+1],
                         placeholders["V_len"]: V_len_u[i:i+1]}
            L_enc, VB_enc = sess.run([L_shared, VB_shared],
                                     feed_dict=feed_dict)
            save_latent(L_enc, L_filenames_u[i])
            save_latent(VB_enc, B_filenames_u[i])
            print B_filenames_u[i]
Exemple #7
0
def main():
    encoder_conf = open("./encoder.json", "r")
    encoder_layers = json.load(encoder_conf)
    decoder_conf = open("./decoder.json", "r")
    decoder_layers = json.load(decoder_conf)

    net_conf = NetConfig()
    net_conf.set_conf("./net_conf.txt")
    q_units = net_conf.inference_num_units
    q_layers = net_conf.inference_num_layers
    p_units = net_conf.prediction_num_units
    p_layers = net_conf.prediction_num_layers
    latent_dim = net_conf.latent_dim
    beta = net_conf.regularize_const

    train_conf = TrainConfig()
    train_conf.set_conf("./train_conf.txt")
    seed = train_conf.seed
    epoch = train_conf.epoch
    save_dir = train_conf.save_dir
    if not os.path.exists(os.path.dirname(save_dir)):
        os.mkdir(os.path.dirname(save_dir))

    data_file = train_conf.data_file
    if not os.path.exists(data_file):
        if not os.path.exists(os.path.dirname(data_file)):
            os.mkdir(os.path.dirname(data_file))
        import wget
        url = "http://www.cs.toronto.edu/~nitish/unsupervised_video/mnist_test_seq.npy"
        wget.download(url, out=data_file)
    data = np.load(data_file)
    data = (data / 255.) * 0.9 + 0.05
    train_data = data[:, :8000, :, :]
    test_data = data[:, 8000:, :, :]

    data_len = train_data.shape[0]
    batchsize = 1
    height = train_data.shape[2]
    width = train_data.shape[3]
    n_channel = 1

    np.random.seed(seed)
    tf.reset_default_graph()
    tf.set_random_seed(seed)

    x = tf.placeholder(tf.float32,
                       [data_len, batchsize, height, width, n_channel])
    reshaped_x = tf.reshape(x, [-1, height, width, n_channel])

    with tf.variable_scope("encoder", reuse=False):
        h = image_processor(reshaped_x, encoder_layers)
    h = tf.reshape(h, [data_len, batchsize, -1])

    z_post_mean, z_post_log_var = inference(h, q_units, q_layers, latent_dim)
    z_prior_mean = tf.zeros(z_post_mean.get_shape().as_list(), tf.float32)
    z_prior_log_var = tf.ones(z_post_log_var.get_shape().as_list(), tf.float32)

    is_train = tf.placeholder(tf.bool)
    z_mean = tf.cond(is_train, lambda: z_post_mean, lambda: z_prior_mean)
    z_log_var = tf.cond(is_train, lambda: z_post_log_var,
                        lambda: z_prior_log_var)
    g = generation(h, z_mean, z_log_var, p_units, p_layers,
                   int(h.get_shape()[2]))
    g = tf.reshape(g, [-1, int(h.get_shape()[2])])
    with tf.variable_scope("decoder", reuse=False):
        reshaped_y = image_processor(g, decoder_layers)
    y = tf.reshape(reshaped_y, x.get_shape().as_list())
    with tf.name_scope('loss'):
        # mse is ok? not cross entropy?
        recon_loss = tf.reduce_mean(tf.square(x - y))
        latent_loss = -tf.reduce_mean(1 + z_log_var - tf.square(z_mean) -
                                      tf.exp(z_log_var))
        # what is the appropriate beta...
        loss = recon_loss + beta * latent_loss

    train_step = tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss)

    gpuConfig = tf.ConfigProto(gpu_options=tf.GPUOptions(
        allow_growth=True,
        per_process_gpu_memory_fraction=train_conf.gpu_use_rate),
                               device_count={'GPU': 1})

    sess = tf.Session(config=gpuConfig)
    sess.run(tf.global_variables_initializer())
    saver = tf.train.Saver(tf.global_variables())
    saver.restore(sess, save_dir)

    error_log = []
    for i in range(train_data.shape[1]):
        print "train {}".format(i)
        batch = train_data[:, i:i + 1, :, :]
        batch = np.reshape(batch, list(batch.shape) + [1])
        feed_dict = {is_train: True, x: batch}
        outputs = sess.run(y, feed_dict=feed_dict)
        save_as_images(outputs, i, "train")

    for i in range(test_data.shape[1]):
        print "test {}".format(i)
        batch = test_data[:, i:i + 1, :, :]
        batch = np.reshape(batch, list(batch.shape) + [1])
        feed_dict = {is_train: True, x: batch}
        outputs = sess.run(y, feed_dict=feed_dict)
        save_as_images(outputs, i, "test")
Exemple #8
0
def main():
    net_conf = NetConfig()
    net_conf.set_conf("./net_conf.txt")

    L_num_units = net_conf.L_num_units
    L_num_layers = net_conf.L_num_layers
    VB_num_units = net_conf.VB_num_units
    VB_num_layers = net_conf.VB_num_layers
    SHARE_DIM = net_conf.S_dim
    
    train_conf = TrainConfig()
    train_conf.set_conf("./train_conf.txt")
    
    seed = train_conf.seed
    batchsize = train_conf.batchsize
    epoch = train_conf.epoch
    save_dir = train_conf.save_dir
    if not os.path.exists(os.path.dirname(save_dir)):
        os.mkdir(os.path.dirname(save_dir))
    
    L_data_dir = train_conf.L_dir
    B_data_dir = train_conf.B_dir
    V_data_dir = train_conf.V_dir
    L_fw, L_bw, L_bin, L_len, filenames = read_sequential_target(L_data_dir, True)
    print len(filenames)
    B_fw, B_bw, B_bin, B_len = read_sequential_target(B_data_dir)
    V_fw, V_bw, V_bin, V_len = read_sequential_target(V_data_dir)
    L_shape = L_fw.shape
    B_shape = B_fw.shape
    V_shape = V_fw.shape

    if train_conf.test:
        L_data_dir_test = train_conf.L_dir_test
        B_data_dir_test = train_conf.B_dir_test
        V_data_dir_test = train_conf.V_dir_test
        L_fw_u, L_bw_u, L_bin_u, L_len_u, filenames_u = read_sequential_target(L_data_dir_test, True)
        print len(filenames_u)
        B_fw_u, B_bw_u, B_bin_u, B_len_u = read_sequential_target(B_data_dir_test)
        V_fw_u, V_bw_u, V_bin_u, V_len_u = read_sequential_target(V_data_dir_test)
        L_shape_u = L_fw_u.shape
        B_shape_u = B_fw_u.shape
        V_shape_u = V_fw_u.shape
        
    np.random.seed(seed)

    tf.reset_default_graph()
    tf.set_random_seed(seed)

    placeholders = make_placeholders(L_shape, B_shape, V_shape, batchsize)

    ##### encoding #####
    L_enc_final_state = encoder(placeholders["L_bw"],
                                placeholders["L_len"],
                                n_units=L_num_units,
                                n_layers=L_num_layers,
                                scope="L_encoder")

    VB_input = tf.concat([placeholders["V_bw"],
                          placeholders["B_bw"]],
                         axis=2)
    VB_enc_final_state = encoder(VB_input,
                                 placeholders["V_len"], 
                                 n_units=VB_num_units,
                                 n_layers=VB_num_layers,
                                 scope="VB_encoder")

    ##### sharing #####
    L_shared = fc(L_enc_final_state, SHARE_DIM,
                  activation_fn=None, scope="L_share")
    VB_shared = fc(VB_enc_final_state, SHARE_DIM,
                   activation_fn=None, scope="VB_share")  
    L_dec_init_state = fc(L_shared, L_num_units*L_num_layers*2,
                          activation_fn=None, scope="L_postshare")
    VB_dec_init_state = fc(VB_shared, VB_num_units*VB_num_layers*2,
                           activation_fn=None, scope="VB_postshare")

    ##### decoding #####
    L_output = L_decoder(placeholders["L_fw"][0],
                         L_dec_init_state,
                         length=L_shape[0],
                         out_dim=L_shape[2],
                         n_units=L_num_units,
                         n_layers=L_num_layers,
                         scope="L_decoder")
    B_output = VB_decoder(placeholders["V_fw"],
                          placeholders["B_fw"][0],
                          VB_dec_init_state,
                          length=B_shape[0],
                          out_dim=B_shape[2],
                          n_units=VB_num_units,
                          n_layers=VB_num_layers,
                          scope="VB_decoder")
    
    with tf.name_scope('loss'):
        L_output = L_output # no need to multiply binary array
        B_output = B_output * placeholders["B_bin"][1:]
        L_loss = tf.reduce_mean(-tf.reduce_sum(placeholders["L_fw"][1:]*tf.log(L_output),
                                               reduction_indices=[2])) 
        B_loss = tf.reduce_mean(tf.square(B_output-placeholders["B_fw"][1:]))
        share_loss = aligned_discriminative_loss(L_shared, VB_shared)
        loss = net_conf.L_weight*L_loss + net_conf.B_weight*B_loss + net_conf.S_weight*share_loss
        
    update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
    with tf.control_dependencies(update_ops):
        train_step = tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss)

    gpuConfig = tf.ConfigProto(
        gpu_options=tf.GPUOptions(allow_growth=True,
                                  per_process_gpu_memory_fraction=train_conf.gpu_use_rate),
        device_count={'GPU': 1})
        
    sess = tf.Session(config=gpuConfig)
    sess.run(tf.global_variables_initializer())
    saver = tf.train.Saver(tf.global_variables())

    previous = time.time()
    for step in range(epoch):
        batch_idx = np.random.permutation(B_shape[1])[:batchsize]
        feed_dict = {placeholders["L_fw"]: L_fw[:, batch_idx, :],
                     placeholders["B_fw"]: B_fw[:, batch_idx, :],
                     placeholders["V_fw"]: V_fw[:, batch_idx, :],
                     placeholders["L_bw"]: L_bw[:, batch_idx, :],
                     placeholders["B_bw"]: B_bw[:, batch_idx, :],
                     placeholders["V_bw"]: V_bw[:, batch_idx, :],
                     placeholders["B_bin"]: B_bin[:, batch_idx, :],
                     placeholders["L_len"]: L_len[batch_idx],
                     placeholders["V_len"]: V_len[batch_idx]}
                    
        _, l, b, s, t = sess.run([train_step,
                                  L_loss,
                                  B_loss,
                                  share_loss,
                                  loss],
                                 feed_dict=feed_dict)
        print "step:{} total:{}, language:{}, behavior:{}, share:{}".format(step, t, l, b, s)
        if train_conf.test and (step+1) % train_conf.test_interval == 0:
            batch_idx = np.random.permutation(B_shape_u[1])[:batchsize]
            feed_dict = {placeholders["L_fw"]: L_fw_u[:, batch_idx, :],
                         placeholders["B_fw"]: B_fw_u[:, batch_idx, :],
                         placeholders["V_fw"]: V_fw_u[:, batch_idx, :],
                         placeholders["L_bw"]: L_bw_u[:, batch_idx, :],
                         placeholders["B_bw"]: B_bw_u[:, batch_idx, :],
                         placeholders["V_bw"]: V_bw_u[:, batch_idx, :],
                         placeholders["B_bin"]: B_bin_u[:, batch_idx, :],
                         placeholders["L_len"]: L_len_u[batch_idx],
                         placeholders["V_len"]: V_len_u[batch_idx]}
            
            l, b, s, t = sess.run([L_loss, B_loss, share_loss, loss],
                                  feed_dict=feed_dict)
            print "test"
            print "step:{} total:{}, language:{}, behavior:{}, share:{}".format(step, t, l, b, s)
            
        if (step + 1) % train_conf.log_interval == 0:
            saver.save(sess, save_dir)
    past = time.time()
    print past-previous
Exemple #9
0
def main():
    net_conf = NetConfig()
    net_conf.set_conf("./net_conf.txt")

    L_num_units = net_conf.L_num_units
    L_num_layers = net_conf.L_num_layers
    VB_num_units = net_conf.VB_num_units
    VB_num_layers = net_conf.VB_num_layers
    SHARE_DIM = net_conf.S_dim

    train_conf = TrainConfig()
    train_conf.set_conf("./train_conf.txt")

    batchsize = 1
    save_dir = train_conf.save_dir

    L_data_dir = train_conf.L_dir
    B_data_dir = train_conf.B_dir
    V_data_dir = train_conf.V_dir
    L_fw, L_bw, L_bin, L_len, L_filenames = read_sequential_target(
        L_data_dir, True)
    print len(L_filenames)
    B_fw, B_bw, B_bin, B_len, B_filenames = read_sequential_target(
        B_data_dir, True)
    V_fw, V_bw, V_bin, V_len = read_sequential_target(V_data_dir)
    L_shape = L_fw.shape
    B_shape = B_fw.shape
    V_shape = V_fw.shape

    if train_conf.test:
        L_data_dir_test = train_conf.L_dir_test
        B_data_dir_test = train_conf.B_dir_test
        V_data_dir_test = train_conf.V_dir_test
        L_fw_u, L_bw_u, L_bin_u, L_len_u, L_filenames_u = read_sequential_target(
            L_data_dir_test, True)
        print len(L_filenames_u)
        B_fw_u, B_bw_u, B_bin_u, B_len_u, B_filenames_u = read_sequential_target(
            B_data_dir_test, True)
        V_fw_u, V_bw_u, V_bin_u, V_len_u = read_sequential_target(
            V_data_dir_test)
        L_shape_u = L_fw_u.shape
        B_shape_u = B_fw_u.shape
        V_shape_u = V_fw_u.shape

    placeholders = make_placeholders(L_shape, B_shape, V_shape, batchsize)

    ##### encoding #####
    L_enc_final_state = encoder(placeholders["L_bw"],
                                placeholders["L_len"],
                                n_units=L_num_units,
                                n_layers=L_num_layers,
                                scope="L_encoder")

    ##### sharing #####
    L_shared = fc(L_enc_final_state,
                  SHARE_DIM,
                  activation_fn=None,
                  scope="L_share")
    VB_dec_init_state = fc(L_shared,
                           VB_num_units * VB_num_layers * 2,
                           activation_fn=None,
                           scope="VB_postshare")

    ##### decoding #####
    B_output = VB_decoder(placeholders["V_fw"],
                          placeholders["B_fw"][0],
                          VB_dec_init_state,
                          length=B_shape[0],
                          out_dim=B_shape[2],
                          n_units=VB_num_units,
                          n_layers=VB_num_layers,
                          scope="VB_decoder")

    gpuConfig = tf.ConfigProto(gpu_options=tf.GPUOptions(
        allow_growth=True,
        per_process_gpu_memory_fraction=train_conf.gpu_use_rate),
                               device_count={'GPU': 1})

    sess = tf.Session(config=gpuConfig)
    sess.run(tf.global_variables_initializer())
    saver = tf.train.Saver(tf.global_variables())
    saver.restore(sess, save_dir)

    for i in range(B_shape[1]):
        feed_dict = {
            placeholders["L_fw"]: L_fw[:, i:i + 1, :],
            placeholders["B_fw"]: B_fw[:, i:i + 1, :],
            placeholders["V_fw"]: V_fw[:, i:i + 1, :],
            placeholders["L_bw"]: L_bw[:, i:i + 1, :],
            placeholders["B_bw"]: B_bw[:, i:i + 1, :],
            placeholders["V_bw"]: V_bw[:, i:i + 1, :],
            placeholders["B_bin"]: B_bin[:, i:i + 1, :],
            placeholders["L_len"]: L_len[i:i + 1],
            placeholders["V_len"]: V_len[i:i + 1]
        }

        result = sess.run(B_output, feed_dict=feed_dict)
        save_latent(np.transpose(result, (1, 0, 2)), B_filenames[i],
                    "generation")

    if train_conf.test:
        for i in range(B_shape_u[1]):
            feed_dict = {
                placeholders["L_fw"]: L_fw_u[:, i:i + 1, :],
                placeholders["B_fw"]: B_fw_u[:, i:i + 1, :],
                placeholders["V_fw"]: V_fw_u[:, i:i + 1, :],
                placeholders["L_bw"]: L_bw_u[:, i:i + 1, :],
                placeholders["B_bw"]: B_bw_u[:, i:i + 1, :],
                placeholders["V_bw"]: V_bw_u[:, i:i + 1, :],
                placeholders["B_bin"]: B_bin_u[:, i:i + 1, :],
                placeholders["L_len"]: L_len_u[i:i + 1],
                placeholders["V_len"]: V_len_u[i:i + 1]
            }
            result = sess.run(B_output, feed_dict=feed_dict)
            save_latent(np.transpose(result, (1, 0, 2)), B_filenames_u[i],
                        "generation")
Exemple #10
0
def main():
    net_conf = NetConfig()
    net_conf.set_conf("./net_conf.txt")

    L_num_units = net_conf.L_num_units
    L_num_layers = net_conf.L_num_layers
    VB_num_units = net_conf.VB_num_units
    VB_num_layers = net_conf.VB_num_layers
    SHARE_DIM = net_conf.S_dim

    train_conf = TrainConfig()
    train_conf.set_conf("./train_conf.txt")

    seed = train_conf.seed
    batchsize = train_conf.batchsize
    epoch = train_conf.epoch
    save_dir = train_conf.save_dir
    if not os.path.exists(os.path.dirname(save_dir)):
        os.mkdir(os.path.dirname(save_dir))

    L_data_dir = train_conf.L_dir
    B_data_dir = train_conf.B_dir
    V_data_dir = train_conf.V_dir
    L_fw, L_bw, L_bin, L_len, filenames = read_sequential_target(
        L_data_dir, True)
    print len(filenames)
    B_fw, B_bw, B_bin, B_len = read_sequential_target(B_data_dir)
    V_fw, V_bw, V_bin, V_len = read_sequential_target(V_data_dir)
    L_shape = L_fw.shape
    B_shape = B_fw.shape
    V_shape = V_fw.shape

    if train_conf.test:
        L_data_dir_test = train_conf.L_dir_test
        B_data_dir_test = train_conf.B_dir_test
        V_data_dir_test = train_conf.V_dir_test
        L_fw_u, L_bw_u, L_bin_u, L_len_u, filenames_u = read_sequential_target(
            L_data_dir_test, True)
        print len(filenames_u)
        B_fw_u, B_bw_u, B_bin_u, B_len_u = read_sequential_target(
            B_data_dir_test)
        V_fw_u, V_bw_u, V_bin_u, V_len_u = read_sequential_target(
            V_data_dir_test)
        L_shape_u = L_fw_u.shape
        B_shape_u = B_fw_u.shape
        V_shape_u = V_fw_u.shape

    np.random.seed(seed)

    tf.reset_default_graph()
    tf.set_random_seed(seed)

    placeholders = make_placeholders(L_shape, B_shape, V_shape, batchsize)

    ##### encoding #####
    L_enc_final_state = encoder(placeholders["L_bw"],
                                placeholders["L_len"],
                                n_units=L_num_units,
                                n_layers=L_num_layers,
                                scope="L_encoder")

    VB_input = tf.concat([placeholders["V_bw"], placeholders["B_bw"]], axis=2)
    VB_enc_final_state = encoder(VB_input,
                                 placeholders["V_len"],
                                 n_units=VB_num_units,
                                 n_layers=VB_num_layers,
                                 scope="VB_encoder")

    ##### sharing #####
    L_shared = fc(L_enc_final_state,
                  SHARE_DIM,
                  activation_fn=None,
                  scope="L_share")
    VB_shared = fc(VB_enc_final_state,
                   SHARE_DIM,
                   activation_fn=None,
                   scope="VB_share")
    L_dec_init_state = fc(L_shared,
                          L_num_units * L_num_layers * 2,
                          activation_fn=None,
                          scope="L_postshare")
    VB_dec_init_state = fc(VB_shared,
                           VB_num_units * VB_num_layers * 2,
                           activation_fn=None,
                           scope="VB_postshare")

    ##### decoding #####
    L_output = L_decoder(placeholders["L_fw"][0],
                         L_dec_init_state,
                         length=L_shape[0],
                         out_dim=L_shape[2],
                         n_units=L_num_units,
                         n_layers=L_num_layers,
                         scope="L_decoder")
    B_output = VB_decoder(placeholders["V_fw"],
                          placeholders["B_fw"][0],
                          VB_dec_init_state,
                          length=B_shape[0],
                          out_dim=B_shape[2],
                          n_units=VB_num_units,
                          n_layers=VB_num_layers,
                          scope="VB_decoder")

    with tf.name_scope('loss'):
        L_output = L_output  # no need to multiply binary array
        B_output = B_output * placeholders["B_bin"][1:]
        L_loss = tf.reduce_mean(-tf.reduce_sum(placeholders["L_fw"][1:] *
                                               tf.log(L_output),
                                               reduction_indices=[2]))
        B_loss = tf.reduce_mean(tf.square(B_output - placeholders["B_fw"][1:]))
        share_loss = aligned_discriminative_loss(L_shared, VB_shared)
        loss = net_conf.L_weight * L_loss + net_conf.B_weight * B_loss + net_conf.S_weight * share_loss

    update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
    with tf.control_dependencies(update_ops):
        train_step = tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss)

    gpuConfig = tf.ConfigProto(gpu_options=tf.GPUOptions(
        allow_growth=True,
        per_process_gpu_memory_fraction=train_conf.gpu_use_rate),
                               device_count={'GPU': 1})

    sess = tf.Session(config=gpuConfig)
    sess.run(tf.global_variables_initializer())
    saver = tf.train.Saver(tf.global_variables())

    previous = time.time()
    for step in range(epoch):
        batch_idx = np.random.permutation(B_shape[1])[:batchsize]
        feed_dict = {
            placeholders["L_fw"]: L_fw[:, batch_idx, :],
            placeholders["B_fw"]: B_fw[:, batch_idx, :],
            placeholders["V_fw"]: V_fw[:, batch_idx, :],
            placeholders["L_bw"]: L_bw[:, batch_idx, :],
            placeholders["B_bw"]: B_bw[:, batch_idx, :],
            placeholders["V_bw"]: V_bw[:, batch_idx, :],
            placeholders["B_bin"]: B_bin[:, batch_idx, :],
            placeholders["L_len"]: L_len[batch_idx],
            placeholders["V_len"]: V_len[batch_idx]
        }

        _, l, b, s, t = sess.run(
            [train_step, L_loss, B_loss, share_loss, loss],
            feed_dict=feed_dict)
        print "step:{} total:{}, language:{}, behavior:{}, share:{}".format(
            step, t, l, b, s)
        if train_conf.test and (step + 1) % train_conf.test_interval == 0:
            batch_idx = np.random.permutation(B_shape_u[1])[:batchsize]
            feed_dict = {
                placeholders["L_fw"]: L_fw_u[:, batch_idx, :],
                placeholders["B_fw"]: B_fw_u[:, batch_idx, :],
                placeholders["V_fw"]: V_fw_u[:, batch_idx, :],
                placeholders["L_bw"]: L_bw_u[:, batch_idx, :],
                placeholders["B_bw"]: B_bw_u[:, batch_idx, :],
                placeholders["V_bw"]: V_bw_u[:, batch_idx, :],
                placeholders["B_bin"]: B_bin_u[:, batch_idx, :],
                placeholders["L_len"]: L_len_u[batch_idx],
                placeholders["V_len"]: V_len_u[batch_idx]
            }

            l, b, s, t = sess.run([L_loss, B_loss, share_loss, loss],
                                  feed_dict=feed_dict)
            print "test"
            print "step:{} total:{}, language:{}, behavior:{}, share:{}".format(
                step, t, l, b, s)

        if (step + 1) % train_conf.log_interval == 0:
            saver.save(sess, save_dir)
    past = time.time()
    print past - previous
Exemple #11
0
def main():
    net_conf = NetConfig()
    net_conf.set_conf("./net_conf.txt")

    L_num_units = net_conf.L_num_units
    L_num_layers = net_conf.L_num_layers
    VB_num_units = net_conf.VB_num_units
    VB_num_layers = net_conf.VB_num_layers
    SHARE_DIM = net_conf.S_dim
    
    train_conf = TrainConfig()
    train_conf.set_conf("./train_conf.txt")
    
    batchsize = 1
    save_dir = train_conf.save_dir
    
    L_data_dir = train_conf.L_dir
    B_data_dir = train_conf.B_dir
    V_data_dir = train_conf.V_dir
    L_fw, L_bw, L_bin, L_len, L_filenames = read_sequential_target(L_data_dir, True)
    print len(L_filenames)
    B_fw, B_bw, B_bin, B_len, B_filenames = read_sequential_target(B_data_dir, True)
    V_fw, V_bw, V_bin, V_len = read_sequential_target(V_data_dir)
    L_shape = L_fw.shape
    B_shape = B_fw.shape
    V_shape = V_fw.shape


    if train_conf.test:
        L_data_dir_test = train_conf.L_dir_test
        B_data_dir_test = train_conf.B_dir_test
        V_data_dir_test = train_conf.V_dir_test
        L_fw_u, L_bw_u, L_bin_u, L_len_u, L_filenames_u = read_sequential_target(L_data_dir_test, True)
        print len(L_filenames_u)
        B_fw_u, B_bw_u, B_bin_u, B_len_u, B_filenames_u = read_sequential_target(B_data_dir_test, True)
        V_fw_u, V_bw_u, V_bin_u, V_len_u = read_sequential_target(V_data_dir_test)
        L_shape_u = L_fw_u.shape
        B_shape_u = B_fw_u.shape
        V_shape_u = V_fw_u.shape
        
    tf.reset_default_graph()

    placeholders = make_placeholders(L_shape, B_shape, V_shape, batchsize)

    ##### encoding #####
    VB_input = tf.concat([placeholders["V_bw"],
                          placeholders["B_bw"]],
                         axis=2)
    VB_enc_final_state = encoder(VB_input,
                                 placeholders["V_len"], 
                                 n_units=VB_num_units,
                                 n_layers=VB_num_layers,
                                 scope="VB_encoder")

    ##### sharing #####
    VB_shared = fc(VB_enc_final_state, SHARE_DIM,
                   activation_fn=None, scope="VB_share")  
    L_dec_init_state = fc(VB_shared, L_num_units*L_num_layers*2,
                          activation_fn=None, scope="L_postshare")

    ##### decoding #####
    L_output = L_decoder(placeholders["L_fw"][0],
                         L_dec_init_state,
                         length=L_shape[0],
                         out_dim=L_shape[2],
                         n_units=L_num_units,
                         n_layers=L_num_layers,
                         scope="L_decoder")
    L_output = tf.contrib.seq2seq.hardmax(L_output)

    gpuConfig = tf.ConfigProto(
        gpu_options=tf.GPUOptions(allow_growth=True,
                                  per_process_gpu_memory_fraction=train_conf.gpu_use_rate),
        device_count={'GPU': 1})
        
    sess = tf.Session(config=gpuConfig)
    sess.run(tf.global_variables_initializer())
    saver = tf.train.Saver(tf.global_variables())
    saver.restore(sess, save_dir)    
    
    for i in range(B_shape[1]):
        feed_dict = {placeholders["L_fw"]: L_fw[:, i:i+1, :],
                     placeholders["B_fw"]: B_fw[:, i:i+1, :],
                     placeholders["V_fw"]: V_fw[:, i:i+1, :],
                     placeholders["L_bw"]: L_bw[:, i:i+1, :],
                     placeholders["B_bw"]: B_bw[:, i:i+1, :],
                     placeholders["V_bw"]: V_bw[:, i:i+1, :],
                     placeholders["B_bin"]: B_bin[:, i:i+1, :],
                     placeholders["L_len"]: L_len[i:i+1],
                     placeholders["V_len"]: V_len[i:i+1]}

        result = sess.run([L_output], feed_dict=feed_dict)
        r = result[0][:,0,:].argmax(axis=1)
        t = L_fw[1:,i,:].argmax(axis=1)
        print (r == t).all()

    if train_conf.test:
        print "test!!!!!!!!!!!!!!!!"
        for i in range(B_shape_u[1]):
            feed_dict = {placeholders["L_fw"]: L_fw_u[:, i:i+1, :],
                         placeholders["B_fw"]: B_fw_u[:, i:i+1, :],
                         placeholders["V_fw"]: V_fw_u[:, i:i+1, :],
                         placeholders["L_bw"]: L_bw_u[:, i:i+1, :],
                         placeholders["B_bw"]: B_bw_u[:, i:i+1, :],
                         placeholders["V_bw"]: V_bw_u[:, i:i+1, :],
                         placeholders["B_bin"]: B_bin_u[:, i:i+1, :],
                         placeholders["L_len"]: L_len_u[i:i+1],
                         placeholders["V_len"]: V_len_u[i:i+1]}
            result = sess.run([L_output], feed_dict=feed_dict)
            result = result[0][:,0,:].argmax(axis=1)
            target = L_fw_u[1:,i,:].argmax(axis=1)
            print (result == target).all()