Пример #1
0
def make_xor_data_sequences(
    shuffle: bool = False,
    seed: Optional[int] = None,
    dtype: np.dtype = np.int64,
    multi_input_output: bool = False,
    batch_size: int = 1,
) -> Tuple[keras_utils.Sequence, keras_utils.Sequence]:
    """
    Generates data loaders for the toy XOR problem.  The dataset only has four
    possible inputs.  For the purposes of testing, the validation set is the
    same as the training dataset.
    """
    training_data, training_labels = xor_data(dtype)

    if shuffle:
        if seed is not None:
            np.random.seed(seed)
        idxs = np.random.permutation(4)
        training_data = training_data[idxs]
        training_labels = training_labels[idxs]

    return (
        keras._ArrayLikeAdapter(training_data,
                                training_labels,
                                batch_size=batch_size),
        keras._ArrayLikeAdapter(training_data,
                                training_labels,
                                batch_size=batch_size),
    )
Пример #2
0
def test_arraylike_data_adapter_drop_leftovers() -> None:
    seq = keras._ArrayLikeAdapter(np.arange(0, 100),
                                  np.arange(100, 200),
                                  batch_size=16,
                                  drop_leftovers=False)
    assert len(seq) == 7  # ceil(100/16)
    assert np.array_equal(seq[6], (np.arange(96, 100), np.arange(196, 200)))

    seq = keras._ArrayLikeAdapter(np.arange(0, 100),
                                  np.arange(100, 200),
                                  batch_size=16,
                                  drop_leftovers=True)
    assert len(seq) == 6  # floor(100/16)
    assert np.array_equal(seq[3], (np.arange(48, 64), np.arange(148, 164)))
Пример #3
0
    config = {
        "hyperparameters": {
            "global_batch_size": det.Constant(value=32),
            "dense1": det.Constant(value=128),
        },
        "searcher": {"name": "single", "metric": "val_accuracy", "max_steps": 40},
    }
    config.update(json.loads(args.config))

    context = init(config, local=args.local, test=args.test, context_dir=str(pathlib.Path.cwd()))

    train_images, train_labels = data.load_training_data()
    train_images = train_images / 255.0
    train_data = _ArrayLikeAdapter(
        x=train_images, y=train_labels, batch_size=context.get_per_slot_batch_size()
    )

    test_images, test_labels = data.load_validation_data()
    test_images = test_images / 255.0
    test_data = _ArrayLikeAdapter(
        x=test_images, y=test_labels, batch_size=context.get_per_slot_batch_size()
    )

    model = build_model(context)

    if args.use_fit:
        model.fit(
            x=train_images,
            y=train_labels,
            batch_size=context.get_per_slot_batch_size(),
Пример #4
0
def test_arraylike_data_adapter_with_unmatched_batch_size() -> None:
    with pytest.raises(check.CheckFailedError):
        keras._ArrayLikeAdapter(np.arange(0, 16),
                                np.arange(0, 16),
                                batch_size=32)
Пример #5
0
def test_minimal_arraylike_data_adapter() -> None:
    seq = keras._ArrayLikeAdapter(np.array([0]), np.array([1]), batch_size=1)
    assert len(seq) == 1
    assert seq[0] == (np.array([0]), np.array([1]))