Example #1
0
def main():
    args = get_macro_args()

    if args.recommended_arch:
        filename = args.recommended_arch

    ctx = get_extension_context(args.context,
                                device_id=args.device_id,
                                type_config=args.type_config)
    nn.set_default_context(ctx)
    ext = nn.ext_utils.import_extension_module(args.context)

    data_iterator = data_iterator_cifar10
    tdata = data_iterator(args.batch_size, True)
    vdata = data_iterator(args.batch_size, False)
    mean_val_train, std_val_train, channel, img_height, img_width, num_class = get_data_stats(
        tdata)
    mean_val_valid, std_val_valid, _, _, _, _ = get_data_stats(vdata)

    data_dict = {
        "train_data": (tdata, mean_val_train, std_val_train),
        "valid_data": (vdata, mean_val_valid, std_val_valid),
        "basic_info": (channel, img_height, img_width, num_class)
    }

    check_arch = np.load(filename, allow_pickle=True)
    print("Train the model whose architecture is:")
    show_arch(check_arch)

    val_acc = CNN_run(args,
                      check_arch.tolist(),
                      data_dict,
                      with_train=True,
                      after_search=True)
Example #2
0
def main():
    """
        Start architecture search and save the architecture found by the controller during the search.
    """
    args = get_macro_args()
    arguments_assertion(args)

    ctx = get_extension_context(args.context,
                                device_id=args.device_id,
                                type_config=args.type_config)
    nn.set_default_context(ctx)
    ext = nn.ext_utils.import_extension_module(args.context)

    if args.sampling_only:
        sample_from_pretrained_controller(args)
        return

    data_iterator = data_iterator_cifar10
    tdata = data_iterator(args.batch_size, True)
    vdata = data_iterator(args.batch_size, False)
    mean_val_train, std_val_train, channel, img_height, img_width, num_class = get_data_stats(
        tdata)
    mean_val_valid, std_val_valid, _, _, _, _ = get_data_stats(vdata)

    data_dict = {
        "train_data": (tdata, mean_val_train, std_val_train),
        "valid_data": (vdata, mean_val_valid, std_val_valid),
        "basic_info": (channel, img_height, img_width, num_class)
    }

    initializer = I.UniformInitializer((-0.1, 0.1))

    # Prepare all the weights in advance
    controller_weights_and_shape = {
        'controller_lstm/0/lstm/affine/W':
        (2 * args.lstm_size, 4, args.lstm_size),
        'controller_lstm/0/lstm/affine/b': (4, args.lstm_size),
        'controller_lstm/1/lstm/affine/W':
        (2 * args.lstm_size, 4, args.lstm_size),
        'controller_lstm/1/lstm/affine/b': (4, args.lstm_size),
        'ops/affine/W': (args.lstm_size, args.num_ops),
        'skip_affine_1/affine/W': (args.lstm_size, args.lstm_size),
        'skip_affine_2/affine/W': (args.lstm_size, 1),
        'skip_affine_3/affine/W': (args.lstm_size, args.lstm_size)
    }
    for w_name, w_shape in controller_weights_and_shape.items():
        nn.parameter.get_parameter_or_create(w_name,
                                             w_shape,
                                             initializer=initializer,
                                             need_grad=True)

    # create dictionary of controller's weights
    controller_weights_dict = {
        w_name: nn.get_parameters()[w_name]
        for w_name in controller_weights_and_shape.keys()
    }

    arch_change, best_arch = search_architecture(args, data_dict,
                                                 controller_weights_dict)

    if args.select_strategy == "best":
        print(
            "saving the model which achieved the best validation accuracy as {}."
            .format(args.recommended_arch))
        check_arch = best_arch
    else:
        # Use the latest architecture. it's not necessarily the one with the best architecture.
        print("saving the latest model recommended by the controller as {}.".
              format(args.recommended_arch))
        check_arch = arch_change[-1]
        np.save(args.recommended_arch, np.array(check_arch))

    print("The saved architecture is;")
    show_arch(check_arch)
    print("when you want to train the network from scratch,\n\
    type 'python macro_retrain.py <OPTION> --recommended-arch {}'".format(
        args.recommended_arch))

    # save the controller's weights so that another architectures can be made.
    all_params = nn.get_parameters(grad_only=False)
    controller_weights = list(controller_weights_and_shape.keys()) + ["w_emb"]
    for param_name in all_params.keys():
        if param_name not in controller_weights:
            nn.parameter.pop_parameter(param_name)
    nn.save_parameters(
        os.path.join(args.model_save_path, 'controller_params.h5'))

    # If you want to train the model recommended by the controller from scratch
    # right after architecture search, uncomment the lines below
    # nn.clear_parameters()
    # ext.clear_memory_cache()  # clear all the Variables
    # val_acc = CNN_run(args, check_arch, data_dict, with_train=True, after_search=True)
    return