How do I make a minimal and reproducible example for neural networks?

2024/11/17 13:40:19

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 ' +--> 137                             str(data_shape))138     return data139 

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,)

Answer

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, MaxPooling2Dxtrain, 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))
https://en.xdnf.cn/q/118802.html

Related Q&A

Increase the capture and stream speed of a video using OpenCV and Python [duplicate]

This question already has answers here:OpenCV real time streaming video capture is slow. How to drop frames or get synced with real time?(4 answers)Closed 2 years ago.I need to take a video and analyz…

Getting Pyphons Tkinter to update a label with a changing variable [duplicate]

This question already has answers here:Making python/tkinter label widget update?(5 answers)Closed 8 years ago.I have a python script which I have written for a Raspberry Pi project, the script reads …

Can someone help me installing pyHook?

I have python 3.5 and I cant install pyHook. I tried every method possible. pip, open the cmd directly from the folder, downloaded almost all the pyHook versions. Still cant install it.I get this error…

What is the bit-wise NOT operator in Python? [duplicate]

This question already has answers here:The tilde operator in Python(10 answers)Closed last year.Is there a function that takes a number with binary numeral a, and does the NOT? (For example, the funct…

PyQt QScrollArea no scrollarea

I haveclass View(QtWidgets.QLabel):def __init__(self):super(View,self).__init__()self.cropLabel = QtWidgets.QLabel(self)self.label = QtWidgets.QLabel(self)self.ogpixmap = QtGui.QPixmap()fileName = rC:/…

svg tag scraping from funnels

I am trying to scrape data from here but getting error. I have taken code from here Scraping using Selenium and pythonThis code was working perfectly fine but now I am getting errorwait.until(EC.visibi…

Python search for multiple values and show with boundaries

I am trying to allow the user to do this:Lets say initially the text says:"hello world hello earth"when the user searches for "hello" it should display:|hello| world |hello| earthhe…

Python: create human-friendly string from a list of datetimes

Im actually looking for the opposite of this question: Converting string into datetimeI have a list of datetime objects and I want to create a human-friendly string from them, e.g., "Jan 27 and 3…

Python replace non digit character in a dataframe [duplicate]

This question already has answers here:Removing non numeric characters from a string in Python(9 answers)Closed 5 years ago.I have the following dataframe column>>> df2[Age]1 25 2 35 3 …

PyGame.error in ubuntu

I have problem with pygame and python 3 in ubuntu 12.10. When i tried load an image i got this error: pygame.error: File is not a Windows BMP fileBut in python 2.7 all works fine. I use code from http:…