0

I am trying to build a model with keras and tensorflow to play Go:

    training_data = encode_from_file_info(training_files)
    testing_data = encode_from_file_info(testing_files)

    input_shape = (1, 19, 19)
    model = Sequential(
        [
            keras.layers.Input(input_shape),
            keras.layers.ZeroPadding2D(padding=3, data_format='channels_first'),
        ]
    )

    model.compile(
        loss="categorical_crossentropy", optimizer="sgd", metrics=["accuracy"]
    )
    model.fit(
        training_data, batch_size=64, epochs=15, verbose=1, validation_data=testing_data
    )

I get the following error when I call model.fit():

Invalid input shape for input Tensor("Cast:0", shape=(None, 19, 19), dtype=float32). Expected shape (None, 1, 19, 19), but input has incompatible shape (None, 19, 19)

I have verified that the generators training_data and testing_data yield tuples of two ndarray with shape (1, 19, 19) and (361,).

I'm new to tensorflow and it is a black box to me. Obviously I'm missing something and making incorrect assumptions about my training data and the neural network. How do I troubleshoot issues like this? What tools are available to debug my model to find the shape mismatch?

3
  • Updated my question with the call to model.fit(). Commented Apr 10 at 4:41
  • 1
    Layers expect data to come in batches, so your generator needs to return (batch_size, 1, 19, 19). Commented Apr 10 at 5:19
  • @xdurch0 Thanks for the tip. I'll see what I can do. Commented Apr 10 at 7:12

1 Answer 1

0

I ended up wrapping my data generator to batch the data:

def grouper(iterable, n, *, incomplete="fill", fillvalue=None):
    """Collect data into non-overlapping fixed-length chunks or blocks."""
    # grouper('ABCDEFG', 3, fillvalue='x') → ABC DEF Gxx
    # grouper('ABCDEFG', 3, incomplete='strict') → ABC DEF ValueError
    # grouper('ABCDEFG', 3, incomplete='ignore') → ABC DEF
    iterators = [iter(iterable)] * n
    match incomplete:
        case "fill":
            return itertools.zip_longest(*iterators, fillvalue=fillvalue)
        case "strict":
            return zip(*iterators, strict=True)
        case "ignore":
            return zip(*iterators)
        case _:
            raise ValueError("Expected fill, strict, or ignore")


def batches(data, batch_size):
    for batch in grouper(data, batch_size):
        features = []
        labels = []
        for feature, label in batch:
            features.append(feature)
            labels.append(label)
        yield np.array(features), np.array(labels)

And then changing the call to model.fit:

    model.fit(
        batches(training_data, batch_size),
        epochs=15,
        verbose=1,
        validation_data=batches(testing_data, batch_size),
    )
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.