How do I make a minimal and reproducible example for neural networks?
I would like to know how to make a minimal and reproducible deep learning example for Stack Overflow. I want to make sure that people have enough information to pinpoint the exact problem with my code. Is it enough to just provide the traceback?

    c:\users\samuel\appdata\local\programs\python\python35\lib\site-packages\keras\engine\training_utils.py 
                         in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
        135                             ': expected ' + names[i] + ' to have shape ' +
        136                             str(shape) + ' but got array with shape ' +
    --&gt; 137                             str(data_shape))
        138     return data
        139 


Or should I simply post the error message?


  Value Error: Error when checking input: expected dense_1_input to have shape
  (4,) but got array with shape (1,)


Here are a few tips to make a reproducible, minimal deep learning Example. It's good advice whether it be for Keras, Pytorch, or Tensorflow. 


We can't use your data, but in most cases, it doesn't matter. All we need is the right shape. 


Use randomly generated numbers of the right shape. 


E.g., np.random.randint(0, 256, (100, 30, 30, 3) for 100 colored pictures of size 30x30
E.g., np.random.choice(np.arange(10), 100) for 100 samples of 10 categories


We don't need to see your entire pipeline.


Only provide the bare minimum to run your code.

Make the most out of Keras and its debugging abilities.


Include the traceback. It will most likely point out the exact problem.

Neural networks are all about fitting the right shapes.


At a minimum, always provide the input shapes.

Make it easy to test and reproduce.


Post your entire neural network architecture.
Include your library imports. Define all variables.



Here is an example of a perfect minimal and reproducible example:



"I have an error. When I run this code, it gives me this error:"


  ValueError: Error when checking target: expected dense_2 to have shape (10,) but got array with shape 


"Here is my architecture, with generated data:"

import numpy as np
import keras
from keras.models import Sequential
from keras.layers import Dense, Flatten, Conv2D, MaxPooling2D

xtrain, xtest = np.random.rand(2, 1000, 30, 30, 3)
ytrain, ytest = np.random.choice(np.arange(10), 2000).reshape(2, 1000) 

model = Sequential([
    Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=xtrain.shape[1:]),
    Conv2D(64, (3, 3), activation='relu'),
    MaxPooling2D(pool_size=(2, 2)),
    Flatten(),
    Dense(128, activation='relu'),
    Dense(10, activation='softmax')])

model.compile(loss=keras.losses.categorical_crossentropy,
              optimizer=keras.optimizers.Adam(),
              metrics=['accuracy'])

model.fit(xtrain, ytrain,
          batch_size=16,
          epochs=10,
          validation_data=(xtest, ytest))

