Ejemplo n.º 1
0
    def build_validation_data_loader(self) -> InputData:
        """
        Create synthetic data loaders for testing and benchmarking purposes.
        """
        batch_size = self.context.get_per_slot_batch_size()
        image_size = imagenet_preprocessing.DEFAULT_IMAGE_SIZE

        data_shape = (batch_size, image_size, image_size,
                      imagenet_preprocessing.NUM_CHANNELS)
        labels_shape = (batch_size, )

        return data.SyntheticData(50, data_shape, labels_shape)
Ejemplo n.º 2
0
        )
    optimizer = common.get_optimizer(lr_schedule)

    model = resnet_model.resnet50(
        num_classes=imagenet_preprocessing.NUM_CLASSES)
    model = ctx.wrap_model(model)

    model.compile(
        loss="sparse_categorical_crossentropy",
        optimizer=optimizer,
        metrics=(["sparse_categorical_accuracy"]),
    )

    data_shape = (
        ctx.get_per_slot_batch_size(),
        imagenet_preprocessing.DEFAULT_IMAGE_SIZE,
        imagenet_preprocessing.DEFAULT_IMAGE_SIZE,
        imagenet_preprocessing.NUM_CHANNELS,
    )
    labels_shape = (ctx.get_per_slot_batch_size(), )
    model.fit(
        data.SyntheticData(ctx.get_per_slot_batch_size(), data_shape,
                           labels_shape),
        epochs=1,
        steps_per_epoch=1,
        validation_steps=1,
        validation_data=data.SyntheticData(ctx.get_per_slot_batch_size(),
                                           data_shape, labels_shape),
        verbose=2,
    )
Ejemplo n.º 3
0
def experiments_synthetic_data(n,
                               degree,
                               dim_space,
                               dim_simplex,
                               sigma,
                               method,
                               seed,
                               results_dir,
                               opt_flag=1):
    """
    conduct experiments with synthetic data

    Parameters
    ----------
    n : int
        number of sample points to be trained
    degree : int
        max degree of bezier simplex fittng
    dim_space : int
        the number of dimension of the Eucledian space
        where the bezier simplex is embedded
    dim_simplex : int
        the number of dimension of bezier simplex
    sigma : float
        the scale of noise.
        Noises are chosen form a normal distribution N(0,sigma^2I)
    method: "borges"/"inductive"
    result_dir: str
        where to output results
    opt_flag : 0/1 (default is 1)
        0 : optimal sampling strategy for inductive skeleton fitting
        1 : nonoptimal sampling strategy inductive skeketon fitting
        "borges" does not care about this parameter.
    """
    # data generation class
    synthetic_data = data.SyntheticData(degree=degree,
                                        dim_space=dim_space,
                                        dim_simplex=dim_simplex)

    # train
    if method == "borges":
        param_trn, data_trn = synthetic_data.sampling_borges(n=n,
                                                             seed=seed,
                                                             sigma=sigma)
        monomial_degree_list = list(
            subfunction.BezierIndex(dim=dim_simplex, deg=degree))
        borges_pastva_trainer = trainer.BorgesPastvaTrainer(
            dim_space=dim_space, dim_simplex=dim_simplex, degree=degree)
        control_point = borges_pastva_trainer.update_control_point(
            t_mat=param_trn,
            data=data_trn,
            c={},
            indices_all=monomial_degree_list,
            indices_fix=[],
        )
    elif method == "inductive":
        # calculate sample size of each skeleton
        calc_sample_size = sampling.CalcSampleSize(degree=degree,
                                                   dim_simplex=dim_simplex)
        train_sample_size_list = calc_sample_size.get_sample_size_list(
            n=n, opt_flag=opt_flag)
        # data generation
        param_trn, data_trn = synthetic_data.sampling_inductive(
            n=n,
            seed=seed,
            sample_size_list=train_sample_size_list,
            sigma=sigma)
        monomial_degree_list = list(
            subfunction.BezierIndex(dim=dim_simplex, deg=degree))
        inductive_skeleton_trainer = trainer.InductiveSkeletonTrainer(
            dim_space=dim_space, dim_simplex=dim_simplex, degree=degree)
        control_point = inductive_skeleton_trainer.update_control_point(
            t_dict=param_trn,
            data_dict=data_trn,
            c={},
            indices_all=monomial_degree_list,
            indices_fix=[],
        )
    else:
        pass

    # generate test data which does not include gaussian noise
    param_tst, data_tst = synthetic_data.sampling_borges(n=10000,
                                                         seed=seed * 2,
                                                         sigma=0)
    bezier_simplex = model.BezierSimplex(dim_space=dim_space,
                                         dim_simplex=dim_simplex,
                                         degree=degree)
    data_pred = bezier_simplex.generate_points(c=control_point, tt=param_tst)
    l2_risk = subfunction.calculate_l2_expected_error(true=data_tst,
                                                      pred=data_pred)

    # output result
    settings = {}
    settings["n"] = n
    settings["degree"] = degree
    settings["dim_space"] = dim_space
    settings["dim_simplex"] = dim_simplex
    settings["sigma"] = sigma
    settings["method"] = method
    settings["seed"] = seed
    settings["opt_flag"] = opt_flag
    results = {}
    results["l2_risk"] = "{:5E}".format(l2_risk)

    o = {}
    o["reults"] = results
    o["settings"] = settings

    ymlfilename = results_dir + "/"
    for key in [
            "dim_simplex", "dim_space", "degree", "n", "method", "opt_flag",
            "seed"
    ]:
        ymlfilename += key + "." + str(settings[key]) + "_"
    ymlfilename += ".yml"
    wf = open(ymlfilename, "w")
    wf.write(yaml.dump(o, default_flow_style=False))
    wf.close()