def VFAE_training(source_data,
                  target_data,
                  n_train_batches,
                  n_epochs,
                  struct,
                  coef,
                  description,
                  process_display=True):

    #########################################################
    ###                        Data                       ###
    #########################################################

    # must input data like [
    #                       [[trainimg, label], [img, label], [img, label]],
    #                       [[validationimg, label], [img, label], [img, label]],
    #                       [[testimg, label], [img, label], [img, label]]
    #                       ]
    train_ftd_source, train_labeld_source = source_data[0]
    valid_ftd_source, valid_labeld_source = source_data[1]
    test_ftd_source, test_labeld_source = source_data[2]

    train_ftd_target, train_labeld_target = target_data[0]
    valid_ftd_target, valid_labeld_target = target_data[1]
    test_ftd_target, test_labeld_target = target_data[2]

    train_ftd_source, train_labeld_source = util.shared_dataset(
        (train_ftd_source, train_labeld_source))
    valid_ftd_source, valid_labeld_source = util.shared_dataset(
        (valid_ftd_source, valid_labeld_source))
    test_ftd_source, test_labeld_source = util.shared_dataset(
        (test_ftd_source, test_labeld_source))

    train_ftd_target, train_labeld_target = util.shared_dataset(
        (train_ftd_target, train_labeld_target))
    valid_ftd_target, valid_labeld_target = util.shared_dataset(
        (valid_ftd_target, valid_labeld_target))
    test_ftd_target, test_labeld_target = util.shared_dataset(
        (test_ftd_target, test_labeld_target))

    batch_size_S = train_ftd_source.get_value(
        borrow=True).shape[0] // n_train_batches
    batch_size_T = train_ftd_target.get_value(
        borrow=True).shape[0] // n_train_batches
    validate_S_size = valid_ftd_source.get_value(borrow=True).shape[0]
    validate_T_size = valid_ftd_target.get_value(borrow=True).shape[0]
    test_S_size = test_ftd_source.get_value(borrow=True).shape[0]
    test_T_size = test_ftd_target.get_value(borrow=True).shape[0]
    print(
        'number of minibatch at one epoch: %i, batch size source : %i, target : %i \n validation size, S:%i, T:%i, test size, S:%i, T:%i'
        % (n_train_batches, batch_size_S, batch_size_T, validate_S_size,
           validate_T_size, test_S_size, test_T_size))

    #######################################################################
    ###                        BUILD ACTUAL MODEL                       ###
    #######################################################################

    print('... building the model')

    # allocate symbolic variables for the data
    #index_source = T.lscalar()  # index to a [mini]batch
    #index_target = T.lscalar()  # index to a [mini]batch
    index = T.lscalar()  # index to a [mini]batch
    x_source = T.matrix(
        'x_source')  # the data is presented as rasterized images
    y_source = T.matrix(
        'y_source')  # the labels are presented as signal vector
    x_target = T.matrix(
        'x_target')  # the data is presented as rasterized images
    y_target = T.matrix(
        'y_target')  # the labels are presented as signal vector

    rng = np.random.RandomState(1234)

    # construct the VFAE class
    classifier = VFAE(rng=rng,
                      input_source=x_source,
                      input_target=x_target,
                      label_source=y_source,
                      batch_size=[batch_size_S, batch_size_T],
                      struct=struct,
                      coef=coef,
                      train=True)

    validate_classifier = VFAE(rng=rng,
                               input_source=x_source,
                               input_target=x_target,
                               label_source=y_source,
                               batch_size=[validate_S_size, validate_T_size],
                               struct=struct,
                               coef=coef,
                               init_params=classifier.params_symbol())

    test_classifier = VFAE(rng=rng,
                           input_source=x_source,
                           input_target=x_target,
                           label_source=y_source,
                           batch_size=[test_S_size, test_T_size],
                           struct=struct,
                           coef=coef,
                           init_params=classifier.params_symbol())

    #update function
    updates = classifier.updates

    test_model = theano.function(inputs=[],
                                 outputs=[
                                     test_classifier.cost,
                                     test_classifier.source_errors(y_source),
                                     test_classifier.target_errors(y_target),
                                     test_classifier.source_predict(),
                                     test_classifier.target_predict()
                                 ],
                                 givens={
                                     x_source: test_ftd_source,
                                     y_source: test_labeld_source,
                                     x_target: test_ftd_target,
                                     y_target: test_labeld_target
                                 })

    validate_model = theano.function(
        inputs=[],
        outputs=[
            validate_classifier.cost,
            validate_classifier.source_errors(y_source),
            validate_classifier.target_errors(y_target),
            validate_classifier.source_predict_raw(),
            validate_classifier.target_predict_raw()
        ],
        givens={
            x_source: valid_ftd_source,
            y_source: valid_labeld_source,
            x_target: valid_ftd_target,
            y_target: valid_labeld_target
        })

    validate_bytraindata_model = theano.function(
        inputs=[index],
        outputs=[
            classifier.cost,
            classifier.source_errors(y_source),
            classifier.target_errors(y_target),
            classifier.source_predict(),
            classifier.target_predict()
        ],
        givens={
            x_source:
            train_ftd_source[index * batch_size_S:(index + 1) *
                             batch_size_S, :],
            y_source:
            train_labeld_source[index * batch_size_S:(index + 1) *
                                batch_size_S, :],
            x_target:
            train_ftd_target[index * batch_size_T:(index + 1) *
                             batch_size_T, :],
            y_target:
            train_labeld_target[index * batch_size_T:(index + 1) *
                                batch_size_T, :]
        })

    train_model = theano.function(
        inputs=[index],
        outputs=[
            classifier.cost,
            classifier.source_errors(y_source),
            classifier.target_errors(y_target),
            classifier.source_predict(),
            classifier.target_predict()
        ],
        updates=updates,
        givens={
            x_source:
            train_ftd_source[index * batch_size_S:(index + 1) *
                             batch_size_S, :],
            y_source:
            train_labeld_source[index * batch_size_S:(index + 1) *
                                batch_size_S, :],
            x_target:
            train_ftd_target[index * batch_size_T:(index + 1) *
                             batch_size_T, :],
            y_target:
            train_labeld_target[index * batch_size_T:(index + 1) *
                                batch_size_T, :]
        })

    ################################################################
    ###                        TRAIN MODEL                       ###
    ################################################################
    '''
    Define :
        xx_loss : Cost function value
        xx_score : Classification accuracy rate        
    '''

    print('... training')

    # early-stopping parameters
    patience = 10000  # look as this many examples regardless
    patience_increase = 2  # wait this much longer when a new best is
    # found
    improvement_threshold = 0.995  # a relative improvement of this much is
    # considered significant
    validation_frequency = min(1, patience // 2)
    # go through this many
    # minibatche before checking the network
    # on the validation set; in this case we
    # check every epoch

    validation_frequency = n_train_batches

    best_iter = 0
    best_train_loss = np.inf
    best_validation_loss = np.inf
    test_loss = np.inf
    train_score = 0.
    validation_score = 0.
    test_score = 0.
    start_time = timeit.default_timer()

    epoch = 0
    iter = 0
    done_looping = False

    train_losses_record = []
    validate_losses_record = []

    test_losses = test_model()[1]
    test_score_S = 1 - np.mean(test_losses)
    test_losses = test_model()[2]
    test_score_T = 1 - np.mean(test_losses)

    print(
        ('Initial, test accuracy: source domain :%f %%, target domain %f %%') %
        (test_score_S * 100., test_score_T * 100.))

    while (epoch < n_epochs) and (not done_looping):
        epoch = epoch + 1
        for minibatch_index in range(n_train_batches):

            minibatch_avg_cost = train_model(minibatch_index)[0]

            # iteration number
            iter = (epoch - 1) * n_train_batches + minibatch_index

            if (iter + 1) % validation_frequency == 0:
                # compute loss on all training set
                train_losses = [
                    validate_bytraindata_model(i)[0]
                    for i in range(n_train_batches)
                ]
                this_train_loss = np.mean(train_losses)

                # compute loss on validation set
                this_validation_loss = validate_model()[0]

                if (iter + 1) % 5 == 0 and process_display:
                    print(
                        'epoch %i, minibatch %i/%i, training loss %f, validation loss %f '
                        % (epoch, minibatch_index + 1, n_train_batches,
                           this_train_loss, this_validation_loss))

                total_train_losses = [
                    validate_bytraindata_model(i)[0]
                    for i in range(n_train_batches)
                ]
                total_train_losses = np.mean(total_train_losses)
                train_losses_record.append(total_train_losses)
                validate_losses_record.append(this_validation_loss)

                # if we got the best validation score until now
                if this_validation_loss < best_validation_loss:
                    #improve patience if loss improvement is good enough
                    if (this_validation_loss <
                            best_validation_loss * improvement_threshold):
                        patience = max(patience, iter * patience_increase)

                    train_loss = this_train_loss
                    best_validation_loss = this_validation_loss
                    best_iter = iter

                    #Get Accuracy

                    train_losses = [
                        validate_bytraindata_model(i)[1]
                        for i in range(n_train_batches)
                    ]
                    train_score_S = 1 - np.mean(train_losses)
                    train_losses = [
                        validate_bytraindata_model(i)[2]
                        for i in range(n_train_batches)
                    ]
                    train_score_T = 1 - np.mean(train_losses)

                    validation_losses = validate_model()[1]
                    validation_score_S = 1 - np.mean(validation_losses)
                    validation_losses = validate_model()[2]
                    validation_score_T = 1 - np.mean(validation_losses)

                    # test it on the test set
                    test_losses = test_model()[1]
                    test_score_S = 1 - np.mean(test_losses)
                    test_losses = test_model()[2]
                    test_score_T = 1 - np.mean(test_losses)

                    trained_params_name = classifier.params_name()
                    trained_params_value = classifier.params_value()

                    if process_display:
                        print((
                            '     epoch %i, minibatch %i/%i, test accuracy of '
                            'best model: source domain :%f %%, target domain %f %%'
                        ) % (epoch, minibatch_index + 1, n_train_batches,
                             test_score_S * 100., test_score_T * 100.))

            if patience <= iter:
                done_looping = True
                break

    end_time = timeit.default_timer()
    if process_display:
        print((
            'Optimization complete. Best validation loss of %f '
            'obtained at iteration %i, with train loss %f \n'
            'train accuracy : source domain %f %%, target domain  %f %%\n'
            'validation accuracy : source domain %f %%, target domain  %f %%\n'
            'test accuracy : source domain %f %%, target domain  %f %%') %
              (best_validation_loss, best_iter + 1, train_loss,
               train_score_S * 100., train_score_T * 100.,
               validation_score_S * 100., validation_score_T * 100.,
               test_score_S * 100., test_score_T * 100.))

    print(
        '-------------------------------------------------------------------------'
    )

    #Converge curve
    index = range(len(train_losses_record))
    title = 'Converge_Curve_%s' % (description)
    fts = (index, train_losses_record, index, validate_losses_record)
    label = ('train loss', 'validation loss')
    color = [1, 2]
    marker = [0, 0]
    line = True
    legend = True
    util.data2plot(title=title,
                   fts=fts,
                   label=label,
                   color=color,
                   marker=marker,
                   line=line,
                   legend=legend,
                   plot_enable=process_display)

    print(
        '-------------------------------------------------------------------------'
    )

    trained_param = VFAE_params()
    trained_param.update_value(trained_params_name, trained_params_value,
                               struct)

    num_S = train_ftd_source.get_value(borrow=True).shape[0]
    num_T = train_ftd_target.get_value(borrow=True).shape[0]

    feature_classifier = VFAE(rng=rng,
                              input_source=x_source,
                              input_target=x_target,
                              label_source=y_source,
                              batch_size=[num_S, num_T],
                              struct=struct,
                              coef=coef,
                              init_params=trained_param)

    features_model = theano.function(
        inputs=[],
        outputs=feature_classifier.feature_outputs() + [
            feature_classifier.source_predict(),
            feature_classifier.target_predict()
        ] + [
            feature_classifier.source_reconstruct(),
            feature_classifier.target_reconstruct()
        ],
        givens={
            x_source: train_ftd_source,
            x_target: train_ftd_target
        })

    return features_model, test_model, trained_param
def features_plot(features_model, test_model, source_data, target_data,
                  description):

    train_fts_source, train_labels_source = source_data[0]
    valid_fts_source, valid_labels_source = source_data[1]
    test_fts_source, test_labels_source = source_data[2]

    train_fts_target, train_labels_target = target_data[0]
    valid_fts_target, valid_labels_target = target_data[1]
    test_fts_target, test_labels_target = target_data[2]

    y_dim = np.shape(train_labels_source)[1]
    S_labels = train_labels_source
    T_labels = train_labels_target

    zy_S = features_model()[0]
    zy_T = features_model()[1]

    zy_S, zy_T = util.feature_tsne(zy_S, zy_T)

    label_zy_S = []
    label_zy_T = []

    for i in range(y_dim):
        label_zy_S.append(zy_S[np.where(S_labels[:, i] == 1)[0], :])
        label_zy_T.append(zy_T[np.where(T_labels[:, i] == 1)[0], :])

    #Source zy feature
    title = 'Source_zy_feature_%s' % (description)
    fts = ()
    for i in range(y_dim):
        fts = fts + (label_zy_S[i][:, 0], label_zy_S[i][:, 1])
    label = [
        'back pack', 'bike', 'calculator', 'headphones', 'keyboard',
        'laptop computer', 'monitor', 'mouse', 'mug', 'projector'
    ]
    color = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    marker = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
    line = False
    legend = True
    util.data2plot(title=title,
                   fts=fts,
                   label=label,
                   color=color,
                   marker=marker,
                   line=line,
                   legend=legend)

    #Target zy feature
    title = 'Target_zy_feature_%s' % (description)
    fts = ()
    for i in range(y_dim):
        fts = fts + (label_zy_T[i][:, 0], label_zy_T[i][:, 1])
    label = [
        'back pack', 'bike', 'calculator', 'headphones', 'keyboard',
        'laptop computer', 'monitor', 'mouse', 'mug', 'projector'
    ]
    color = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    marker = [2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
    line = False
    legend = True
    util.data2plot(title=title,
                   fts=fts,
                   label=label,
                   color=color,
                   marker=marker,
                   line=line,
                   legend=legend)

    #Both source, target zy feature
    title = 'Zy_feature_%s' % (description)
    fts = ()
    tmp = ()
    for i in range(y_dim):
        fts = fts + (label_zy_S[i][:, 0], label_zy_S[i][:, 1])
        fts = fts + (label_zy_T[i][:, 0], label_zy_T[i][:, 1])
    label = [
        'Source:back pack', 'Target:back pack', 'Source:bike', 'Target:bike',
        'Source:calculator', 'Target:calculator', 'Source:headphones',
        'Target:headphones', 'Source:keyboard', 'Target:keyboard',
        'Source:laptop computer', 'Target:laptop computer', 'Source:monitor',
        'Target:monitor', 'Source:mouse', 'Target:mouse', 'Source:mug',
        'Target:mug', 'Source:projector', 'Target:projector'
    ]
    color = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10]
    marker = [1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2]
    line = False
    legend = True
    util.data2plot(title=title,
                   fts=fts,
                   label=label,
                   color=color,
                   marker=marker,
                   line=line,
                   legend=legend)

    #Both source, target zy feature
    title = 'Test_zy_feature_%s' % (description)
    fts = ()
    tmp = ()
    for i in range(y_dim):
        fts = fts + (label_zy_S[i][:, 0], label_zy_S[i][:, 1])
        fts = fts + (label_zy_T[i][:, 0], label_zy_T[i][:, 1])
    label = [
        'Source:back pack', 'Target:back pack', 'Source:bike', 'Target:bike',
        'Source:calculator', 'Target:calculator', 'Source:headphones',
        'Target:headphones', 'Source:keyboard', 'Target:keyboard',
        'Source:laptop computer', 'Target:laptop computer', 'Source:monitor',
        'Target:monitor', 'Source:mouse', 'Target:mouse', 'Source:mug',
        'Target:mug', 'Source:projector', 'Target:projector'
    ]
    color = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10]
    marker = [1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2]
    line = False
    legend = True
    util.data2plot(title=title,
                   fts=fts,
                   label=label,
                   color=color,
                   marker=marker,
                   line=line,
                   legend=legend)
def features_plot(features,labels_source, labels_target ,sample_n, description):
    # features = [source_share_feature, target_share_feature, source_private_feature, target_private_feature]
    y_dim=np.shape(labels_source)[1]

    S_labels=labels_source[0:sample_n]
    T_labels=labels_target[0:sample_n]
    
    source_share_feature=features[0][0:sample_n,:]
    target_share_feature=features[1][0:sample_n,:]
    source_private_feature=features[2][0:sample_n,:]
    target_private_feature=features[3][0:sample_n,:]
    
    z_ssf, z_tsf, z_spf, z_tpf = util.feature_tsne(source_share_feature, target_share_feature, source_private_feature, target_private_feature)
    
    #save tsen feature
    np.savetxt('z_ssf.txt', z_ssf)
    np.savetxt('z_tsf.txt', z_tsf)
    np.savetxt('z_spf.txt', z_spf)
    np.savetxt('z_tpf.txt', z_tpf)
    
    label_z_ssf=[]
    label_z_tsf=[]
    label_z_spf=[]
    label_z_tpf=[]
    
    for i in range(y_dim):
        label_z_ssf.append( z_ssf[np.where(S_labels[:,i]==1)[0],:] )
        label_z_tsf.append( z_tsf[np.where(T_labels[:,i]==1)[0],:] )
        label_z_spf.append( z_spf[np.where(S_labels[:,i]==1)[0],:] )
        label_z_tpf.append( z_tpf[np.where(T_labels[:,i]==1)[0],:] )
    
    # Source zy feature 
    title = 'Source_z_feature_%s' % (description)
    fts=()
    for i in range(y_dim):
        fts=fts+(label_z_ssf[i][:,0],label_z_ssf[i][:,1])
        fts=fts+(label_z_spf[i][:,0],label_z_spf[i][:,1])
        
    label = ['negative_share', 'positive_share', 'negative_private', 'positive_private']
   
    color=[1, 2, 3 , 7]
    marker=[1,1, 1, 1]
    line = False
    legend=True
    util.data2plot(title=title, fts=fts, label=label, color=color, marker=marker, line=line, legend = legend, plot_enable=True)
    
    # Target zy feature
    title = 'Target_z_feature_%s' % (description)
    fts=()
    for i in range(y_dim):
        fts=fts+(label_z_tsf[i][:,0],label_z_tsf[i][:,1])
        fts=fts+(label_z_tpf[i][:,0],label_z_tpf[i][:,1])
        
    label = ['negative_share', 'positive_share', 'negative_private', 'positive_private']
    color=[1, 2, 3, 7]
    marker=[3,3, 3,3]
    line = False
    legend=True
    util.data2plot(title=title, fts=fts, label=label, color=color, marker=marker, line=line, legend = legend, plot_enable=True)
    
    # Both source, target zy feature
    title = 'Zy_feature_%s' % (description)
    fts = ()
    tmp = ()
    for i in range(y_dim):
        fts=fts+(label_z_ssf[i][:,0],label_z_ssf[i][:,1])
        fts=fts+(label_z_spf[i][:,0],label_z_spf[i][:,1])
        fts=fts+(label_z_tsf[i][:,0],label_z_tsf[i][:,1])
        fts=fts+(label_z_tpf[i][:,0],label_z_tpf[i][:,1])
        
    label = ['negative_share_S', 'positive_share_S', 'negative_private_S', 'positive_private_S', 'negative_share_T', 'positive_share_T', 'negative_private_T', 'positive_private_T']
    color = [1, 2, 3, 7, 1, 2, 3, 7]
    marker = [1, 1, 1, 1, 3, 3, 3, 3]
    line = False 
    legend = True
    util.data2plot(title=title, fts=fts, label=label, color=color, marker=marker, line=line, legend = legend, plot_enable=True)
    
    
    
Example #4
0
def features_plot(features_model,
                  test_model,
                  source_data,
                  target_data,
                  sample_n,
                  description,
                  reconstruction=False):

    train_fts_source, train_labels_source = source_data[0]
    valid_fts_source, valid_labels_source = source_data[1]
    test_fts_source, test_labels_source = source_data[2]

    train_fts_target, train_labels_target = target_data[0]
    valid_fts_target, valid_labels_target = target_data[1]
    test_fts_target, test_labels_target = target_data[2]

    y_dim = np.shape(train_labels_source)[1]
    S_labels = train_labels_source
    T_labels = train_labels_target

    #-------------------------------------------------------------------------------------------

    zy_S = features_model()[0][0:sample_n, :]
    zy_T = features_model()[1][0:sample_n, :]

    zy_S, zy_T = util.feature_tsne(zy_S, zy_T)

    label_zy_S = []
    label_zy_T = []
    for i in range(y_dim):
        label_zy_S.append(zy_S[np.where(S_labels[0:sample_n, i] == 1)[0], :])
        label_zy_T.append(zy_T[np.where(T_labels[0:sample_n, i] == 1)[0], :])

    #Source zy feature
    title = 'Source_zy_feature_%s' % (description)
    fts = ()
    for i in range(y_dim):
        fts = fts + (label_zy_S[i][:, 0], label_zy_S[i][:, 1])
    label = ('label 0', 'label 1')
    color = [1, 2]
    marker = [1, 1]
    line = False
    util.data2plot(title=title,
                   fts=fts,
                   label=label,
                   color=color,
                   marker=marker,
                   line=line)

    #Target zy feature
    title = 'Target_zy_feature_%s' % (description)
    fts = ()
    for i in range(y_dim):
        fts = fts + (label_zy_T[i][:, 0], label_zy_T[i][:, 1])
    label = ('label 0', 'label 1')
    color = [1, 2]
    marker = [2, 2]
    line = False
    util.data2plot(title=title,
                   fts=fts,
                   label=label,
                   color=color,
                   marker=marker,
                   line=line)

    #Both source, target zy feature
    title = 'Zy_feature_%s' % (description)
    fts = ()
    for i in range(y_dim):
        fts = fts + (label_zy_S[i][:, 0], label_zy_S[i][:, 1])
        fts = fts + (label_zy_T[i][:, 0], label_zy_T[i][:, 1])
    label = ('Source label 0', 'Target label 0', 'Source label 1',
             'Target label 1')
    color = [1, 1, 2, 2]
    marker = [1, 2, 1, 2]
    line = False
    legend = False
    util.data2plot(title=title,
                   fts=fts,
                   label=label,
                   color=color,
                   marker=marker,
                   line=line,
                   legend=legend)

    #print('-------------------------------------------------------------------------')
    #Classification Result of Training Data
    S_labels_predict = features_model()[2]
    T_labels_predict = features_model()[3]

    S_0 = train_fts_source[np.where(S_labels_predict == 0)[0], :]
    S_1 = train_fts_source[np.where(S_labels_predict == 1)[0], :]
    T_0 = train_fts_target[np.where(T_labels_predict == 0)[0], :]
    T_1 = train_fts_target[np.where(T_labels_predict == 1)[0], :]

    title = 'Train_classification_%s' % (description)
    fts = (S_0[:, 0], S_0[:, 1], S_1[:, 0], S_1[:, 1], T_0[:, 0], T_0[:, 1],
           T_1[:, 0], T_1[:, 1])
    label = ('Source label 0', 'Target label 0', 'Source label 1',
             'Target label 1')
    color = [1, 2, 1, 2]
    marker = [1, 1, 2, 2]
    line = False
    legend = False
    util.data2plot(title=title,
                   fts=fts,
                   label=label,
                   color=color,
                   marker=marker,
                   line=line,
                   legend=legend)

    #Classification Result of Testing Data
    S_labels_predict = test_model()[3]
    T_labels_predict = test_model()[4]

    S_0 = test_fts_source[np.where(S_labels_predict == 0)[0], :]
    S_1 = test_fts_source[np.where(S_labels_predict == 1)[0], :]
    T_0 = test_fts_target[np.where(T_labels_predict == 0)[0], :]
    T_1 = test_fts_target[np.where(T_labels_predict == 1)[0], :]

    title = 'Test_classification_%s' % (description)
    fts = (S_0[:, 0], S_0[:, 1], S_1[:, 0], S_1[:, 1], T_0[:, 0], T_0[:, 1],
           T_1[:, 0], T_1[:, 1])
    label = ('Source label 0', 'Target label 0', 'Source label 1',
             'Target label 1')
    color = [1, 2, 1, 2]
    marker = [1, 1, 2, 2]
    line = False
    legend = False
    util.data2plot(title=title,
                   fts=fts,
                   label=label,
                   color=color,
                   marker=marker,
                   line=line,
                   legend=legend)

    print(
        '-------------------------------------------------------------------------'
    )

    #Reconstruction of generative model(VFAE, VLDF)
    if reconstruction == False:
        return

    S_recon_x = features_model()[4]
    T_recon_x = features_model()[5]

    title = 'Reconstruction_%s' % (description)
    fts = (S_recon_x[:, 0], S_recon_x[:, 1], T_recon_x[:, 0], T_recon_x[:, 1])
    label = ('Source', 'Target')
    color = [2, 2]
    marker = [1, 2]
    line = False
    legend = True
    util.data2plot(title=title,
                   fts=fts,
                   label=label,
                   color=color,
                   marker=marker,
                   line=line,
                   legend=legend)
Example #5
0
def features_plot(features_model,
                  source_data,
                  target_data,
                  sample_n,
                  description,
                  plot_enable=True):

    train_fts_source, train_labels_source = source_data[0]
    valid_fts_source, valid_labels_source = source_data[1]
    test_fts_source, test_labels_source = source_data[2]

    train_fts_target, train_labels_target = target_data[0]
    valid_fts_target, valid_labels_target = target_data[1]
    test_fts_target, test_labels_target = target_data[2]

    y_dim = np.shape(train_labels_source)[1]

    S_labels = train_labels_source[0:sample_n, :]
    T_labels = train_labels_target[0:sample_n, :]

    zj_S = features_model[0][0:sample_n, :]
    zj_T = features_model[1][0:sample_n, :]
    zi_S = features_model[2][0:sample_n, :]
    zi_T = features_model[3][0:sample_n, :]

    zj_S, zj_T, zi_S, zi_T = util.domain_feature_tsne(zj_S, zj_T, zi_S, zi_T)

    filename = '/home/jay/DSN/tensorflow/DSN_GAN_loss/Experimental_Result/Amazon/%s.png'
    label_zj_S = []
    label_zj_T = []
    label_zi_S = []
    label_zi_T = []

    for i in range(y_dim):
        '''
        label_zj_S.append( zj_S[np.where(S_labels[:,i] == 1)[0], :] )
        label_zj_T.append( zj_T[np.where(T_labels[:,i] == 1)[0], :] )
        label_zi_S.append( zi_S[np.where(S_labels[:,i] == 1)[0], :] )
        label_zi_T.append( zi_T[np.where(T_labels[:,i] == 1)[0], :] )
        '''

    #Source zy feature
    title = 'Source_zy_feature_%s' % (description)
    fts = ()
    '''
    for i in range(y_dim):
        fts = fts+(label_zj_S[i][:,0], label_zj_S[i][:,1])
    label = ['negative', 'positive']
    '''
    fts = (zj_S[:, 0], zj_S[:, 1]) + (zi_S[:, 0], zi_S[:, 1])

    label = ['joint', 'individual']
    color = [1, 2]
    marker = [1, 1]
    line = False
    legend = True
    util.data2plot(title=title,
                   fts=fts,
                   label=label,
                   color=color,
                   marker=marker,
                   line=line,
                   legend=legend,
                   plot_enable=plot_enable,
                   filename=filename)

    #Target zy feature
    title = 'Target_zy_feature_%s' % (description)
    fts = ()
    '''
    for i in range(y_dim):
        fts = fts+(label_zy_T[i][:,0], label_zy_T[i][:,1])
    label = ['negative', 'positive']
    '''
    fts = (zj_T[:, 0], zj_T[:, 1]) + (zi_T[:, 0], zi_T[:, 1])
    label = ['joint', 'individual']
    color = [1, 2]
    marker = [3, 3]
    line = False
    legend = True
    util.data2plot(title=title,
                   fts=fts,
                   label=label,
                   color=color,
                   marker=marker,
                   line=line,
                   legend=legend,
                   plot_enable=plot_enable,
                   filename=filename)

    #Both source, target zy feature
    title = 'Zy_feature_%s' % (description)
    fts = ()
    tmp = ()
    '''
    for i in range(y_dim):
        fts = fts+(label_zy_S[i][:,0], label_zy_S[i][:,1])
        fts = fts+(label_zy_T[i][:,0], label_zy_T[i][:,1])
    label = ['Source:negative', 'Target:negative', 'Source:positive', 'Target:positive']
    '''
    fts = (zj_S[:, 0], zj_S[:, 1]) + (zi_S[:, 0], zi_S[:, 1]) + (
        zj_T[:, 0], zj_T[:, 1]) + (zi_T[:, 0], zi_T[:, 1])
    label = [
        'source joint', 'source individual', 'target joint',
        'target individual'
    ]
    color = [1, 2, 1, 2]
    marker = [1, 1, 3, 3]
    line = False
    legend = True
    util.data2plot(title=title,
                   fts=fts,
                   label=label,
                   color=color,
                   marker=marker,
                   line=line,
                   legend=legend,
                   plot_enable=plot_enable,
                   filename=filename)
def features_plot(features_model,
                  test_model,
                  source_data,
                  target_data,
                  sample_n,
                  description,
                  plot_enable=True):

    train_fts_source, train_labels_source = source_data[0]
    valid_fts_source, valid_labels_source = source_data[1]
    test_fts_source, test_labels_source = source_data[2]

    train_fts_target, train_labels_target = target_data[0]
    valid_fts_target, valid_labels_target = target_data[1]
    test_fts_target, test_labels_target = target_data[2]

    y_dim = np.shape(train_labels_source)[1]
    S_labels = train_labels_source[0:sample_n, :]
    T_labels = train_labels_target[0:sample_n, :]

    zy_S = features_model()[0][0:sample_n, :]
    zy_T = features_model()[1][0:sample_n, :]

    zy_S, zy_T = util.feature_tsne(zy_S, zy_T)

    label_zy_S = []
    label_zy_T = []

    for i in range(y_dim):
        label_zy_S.append(zy_S[np.where(S_labels[:, i] == 1)[0], :])
        label_zy_T.append(zy_T[np.where(T_labels[:, i] == 1)[0], :])

    #Source zy feature
    title = 'Source_zy_feature_%s' % (description)
    fts = ()
    for i in range(y_dim):
        fts = fts + (label_zy_S[i][:, 0], label_zy_S[i][:, 1])
    label = ['negative', 'positive']
    color = [1, 2]
    marker = [1, 1]
    line = False
    legend = True
    util.data2plot(title=title,
                   fts=fts,
                   label=label,
                   color=color,
                   marker=marker,
                   line=line,
                   legend=legend,
                   plot_enable=plot_enable)

    #Target zy feature
    title = 'Target_zy_feature_%s' % (description)
    fts = ()
    for i in range(y_dim):
        fts = fts + (label_zy_T[i][:, 0], label_zy_T[i][:, 1])
    label = ['negative', 'positive']
    color = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    marker = [2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
    line = False
    legend = True
    util.data2plot(title=title,
                   fts=fts,
                   label=label,
                   color=color,
                   marker=marker,
                   line=line,
                   legend=legend,
                   plot_enable=plot_enable)

    #Both source, target zy feature
    title = 'Zy_feature_%s' % (description)
    fts = ()
    tmp = ()
    for i in range(y_dim):
        fts = fts + (label_zy_S[i][:, 0], label_zy_S[i][:, 1])
        fts = fts + (label_zy_T[i][:, 0], label_zy_T[i][:, 1])
    label = [
        'Source:negative', 'Target:negative', 'Source:positive',
        'Target:positive'
    ]
    color = [1, 1, 2, 2]
    marker = [1, 2, 1, 2]
    line = False
    legend = True
    util.data2plot(title=title,
                   fts=fts,
                   label=label,
                   color=color,
                   marker=marker,
                   line=line,
                   legend=legend,
                   plot_enable=plot_enable)