Beispiel #1
0
def test_recon_error(mix_model, model, model_path, data_path,
                                save=True, save_path=""):
    M = mix_model.load_checkpoint(model, model_path)
    models = [M.model1, M.model2, M.model3, M.model4]
    test_data = Data.load(data_path)["data"]
    with torch.no_grad():
        recon_error_matrix = np.zeros((4,4))

        pairs = list(combinations_with_replacement(np.arange(len(test_data)), r=2))
        for (i, j) in pairs:
            for x1, y1 in test_data[i]:
                for x2, y2 in test_data[j]:

                    h1 = models[i].encode(x1)
                    h2 = models[j].encode(x2)

                    out11 = models[i].decode(h1)
                    out12 = models[i].decode(h2)
                    out22 = models[j].decode(h2)
                    out21 = models[j].decode(h1)

                    recon_loss_xx = Loss.mse_loss(out11, y1)
                    recon_loss_yy = Loss.mse_loss(out22, y2)
                    recon_loss_xy = Loss.mse_loss(out12, y1)
                    recon_loss_yx = Loss.mse_loss(out21, y2)

                    recon_error_matrix[i,i] = recon_loss_xx
                    recon_error_matrix[i,j] = recon_loss_xy
                    recon_error_matrix[j,j] = recon_loss_yy
                    recon_error_matrix[j,i] = recon_loss_yx
    if save:
        if not os.path.exists(save_path): os.mkdir(save_path)
        names = ["M1", "M2", "M3", "M4"]
        recon_df = pd.DataFrame(data=recon_error_matrix, index=names, columns=names)
        recon_df.to_csv(M.name + "_recon_error.csv")
Beispiel #2
0
def test_recon_error_withLabel_withProb(mix_model, model, model_path, data_path,
                               save=True, save_path=""):
    M = mix_model.load_checkpoint(model, model_path)
    models = [M.model1, M.model2, M.model3, M.model4]
    test_data = Data.load(data_path)["data"]
    with torch.no_grad():
        recon_error_matrix = np.zeros((4,4))
        latent_error_matrix = np.zeros((4,4))
        kl_error_matrix = np.zeros((4,4))
        error_matrix = np.zeros((4,4))
        pairs = list(combinations_with_replacement(np.arange(len(test_data)), r=2))
        for (i, j) in pairs:
            for x1, y1 in test_data[i]:
                for x2, y2 in test_data[j]:
                    h1, l1, mu1, logvar1 = models[i].encode(x1)
                    h2, l2, mu2, logvar2 = models[j].encode(x2)

                    out11, _, _, _ = models[i].decode(h1, l1, mu1, logvar1)
                    out12, _, _, _ = models[i].decode(h2, l1, mu2, logvar2)
                    out22, _, _, _ = models[j].decode(h2, l2, mu2, logvar2)
                    out21, _, _, _ = models[j].decode(h1, l2, mu1, logvar1)

                    loss_xx, kl_loss_xx, recon_loss_xx = Loss.kl_loss(out11, y1, mu1, logvar1)
                    loss_yy, kl_loss_yy, recon_loss_yy = Loss.kl_loss(out22, y2, mu2, logvar2)
                    loss_xy, kl_loss_xy, recon_loss_xy = Loss.kl_loss(out12, y1, mu1, logvar1)
                    loss_yx, kl_loss_yx, recon_loss_yx = Loss.kl_loss(out21, y2, mu2, logvar2)

                    latent_loss = Loss.mse_loss(h1, h2)

                    recon_error_matrix[i,i] = recon_loss_xx
                    recon_error_matrix[i,j] = recon_loss_xy
                    recon_error_matrix[j,j] = recon_loss_yy
                    recon_error_matrix[j,i] = recon_loss_yx

                    error_matrix[i, i] = loss_xx
                    error_matrix[i, j] = loss_xy
                    error_matrix[j, j] = loss_yy
                    error_matrix[j, i] = loss_yx

                    latent_error_matrix[i,j] = latent_error_matrix[j,i] = latent_loss
                    kl_error_matrix[i,j] =  kl_loss_xy
                    kl_error_matrix[j, i] = kl_loss_yx

    if save:
        if not os.path.exists(save_path): os.mkdir(save_path)
        names = ["M1", "M2", "M3", "M4"]
        recon_df = pd.DataFrame(data=recon_error_matrix, index=names, columns=names)
        latent_df = pd.DataFrame(data=latent_error_matrix, index=names, columns=names)
        error_df = pd.DataFrame(data=error_matrix, index=names, columns=names)
        kl_df = pd.DataFrame(data=kl_error_matrix, index=names, columns=names)
        recon_df.to_csv(M.name + "_recon_error.csv")
        latent_df.to_csv(M.name + "_latent_error.csv")
        error_df.to_csv(M.name + "_error.csv")
        kl_df.to_csv(M.name + "_kl_error.csv")
Beispiel #3
0
def train_single_model_withLabel(model,
                                 datapaths: list,
                                 featureList: list,
                                 extra_feature_len: int = 0,
                                 config: dict = None,
                                 n_epochs=300,
                                 n_samples: int = 30,
                                 model_name: str = "model"):
    # load data
    datasets = [
        Data.load(os.path.join(DATA_PATH, path)) for path in datapaths[0]
    ]
    train_set, val_set, test_set = Data.prepare_data_withLabel(
        datasets, featureList, extra_feature_len=extra_feature_len)

    dim = len(train_set[0][0])

    _tune_withLabel(model=model,
                    train_set=train_set,
                    val_set=val_set,
                    config=config,
                    dim=dim,
                    EPOCHS=n_epochs,
                    extra_feature_len=extra_feature_len,
                    n_samples=n_samples,
                    model_name=model_name)

    path = os.path.join(MODEL_PATH, model_name)
    Data.clean_checkpoints(num_keep=3, path=path)
    Data.save_testData([test_set], path=path)
Beispiel #4
0
def test_single_model(model, model_path, data_path, save=True, save_path=""):
    model = model.load_checkpoint(model_path)
    test_data = Data.load(data_path)["data"][0]
    N = len(test_data)
    print(model, N)
    with torch.no_grad():
        recon_errors = np.zeros(N)

        for i, (x, y) in enumerate(test_data):
            h, l = model.encode(x)
            out = model.decode(h, l)

            recon_loss = Loss.mse_loss(out, y)
            recon_errors[i] = recon_loss

    if save:
        if not os.path.exists(save_path): os.mkdir(save_path)
        recon_df = pd.DataFrame(data=recon_errors, index=np.arange(N))
        recon_df.to_csv(model.name + "_recon_error.csv")
Beispiel #5
0
def test_single_model(model, model_path, data_path,
                               save=True, save_path=""):
    model = model.load_checkpoint(model_path)
    test_data = Data.load(data_path)["data"][0]
    N = len(test_data)
    print(model, N)
    with torch.no_grad():
        recon_errors = np.zeros(N)

        for i, (x, y) in enumerate(test_data):
                    # models[j].to("cpu")
                    # h1, l1, mu1, logvar1 = models[i].encode(x1)
                    # start_time = time.time()
                    # h2, l2, mu2, logvar2 = models[j].encode(x2)
                    # time1 = time.time() - start_time
                    #
                    # # out11, _, _, _ = models[i].decode(h1, l1, mu1, logvar1)
                    # # out12, _, _, _ = models[i].decode(h2, l1, mu2, logvar2)
                    # start_time = time.time()
                    # out22, _, _, _ = models[j].decode(h2, l2, mu2, logvar2)
                    # time2 = time.time() - start_time
                    #
                    # print(models[j])
                    # print("time 1: ", time1)
                    # print("time 2:", time2)
                    # return
                    # out21, _, _, _ = models[j].decode(h1, l2, mu1, logvar1)
                    h, l = model.encode(x)
                    out = model.decode(h, l)

                    recon_loss = Loss.mse_loss(out,y)
                    recon_errors[i] = recon_loss

    if save:
        if not os.path.exists(save_path): os.mkdir(save_path)
        recon_df = pd.DataFrame(data=recon_errors, index=np.arange(N))
        recon_df.to_csv(model.name + "_recon_error.csv")
Beispiel #6
0
def train_multi_model(model,
                      datapaths: list,
                      featureList: list,
                      config: dict = None,
                      n_epochs=300,
                      n_samples: int = 30,
                      model_name: str = "model"):
    # load data
    datasets1 = [
        Data.load(os.path.join(DATA_PATH, path)) for path in datapaths[0]
    ]
    datasets2 = [
        Data.load(os.path.join(DATA_PATH, path)) for path in datapaths[1]
    ]
    datasets3 = [
        Data.load(os.path.join(DATA_PATH, path)) for path in datapaths[2]
    ]
    datasets4 = [
        Data.load(os.path.join(DATA_PATH, path)) for path in datapaths[3]
    ]

    train_set1, val_set1, test_set1 = Data.prepare_data(datasets1, featureList)
    train_set2, val_set2, test_set2 = Data.prepare_data(datasets2, featureList)
    train_set3, val_set3, test_set3 = Data.prepare_data(datasets3, featureList)
    train_set4, val_set4, test_set4 = Data.prepare_data(datasets4, featureList)

    dims = [
        len(train_set1[0][0]),
        len(train_set2[0][0]),
        len(train_set3[0][0]),
        len(train_set4[0][0])
    ]
    train_set = [
        (torch.cat([x[0], y[0], z[0], w[0]],
                   dim=0), torch.cat([x[1], y[1], z[1], w[1]], dim=0))
        for x, y, z, w in zip(train_set1, train_set2, train_set3, train_set4)
    ]
    val_set = [(torch.cat([x[0], y[0], z[0], w[0]],
                          dim=0), torch.cat([x[1], y[1], z[1], w[1]], dim=0))
               for x, y, z, w in zip(val_set1, val_set2, val_set3, val_set4)]

    tune4(model=model,
          train_set=train_set,
          val_set=val_set,
          config=config,
          dims=dims,
          EPOCHS=n_epochs,
          n_samples=n_samples,
          model_name=model_name)

    path = os.path.join(MODEL_PATH, model_name)
    Data.clean_checkpoints(num_keep=3, path=path)
    Data.save_testData([test_set1, test_set2, test_set3, test_set4], path=path)