问题描述
我想知道如何为Stack Overflow制作一个最小且可重现的深度学习示例.我想确保人们有足够的信息来查明我的代码的确切问题.仅提供回溯就足够了吗?
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 data
139
还是我应该简单地发布错误消息?
Or should I simply post the error message?
推荐答案
以下是制作可重复的,最少的深度学习示例的一些技巧.无论是Keras
,Pytorch
还是Tensorflow
,这都是一个很好的建议.
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
.
- 我们无法使用您的数据,但是在大多数情况下,这并不重要.我们需要的只是正确的形状.
- 使用随机生成的正确形状的数字.
- 例如
np.random.randint(0, 256, (100, 30, 30, 3)
用于100张 30x30 尺寸的彩色图片 - 例如
np.random.choice(np.arange(10), 100)
用于10个类别的100个样本
- 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
- 仅提供运行代码的最低要求.
- 包括追溯.最有可能指出确切的问题.
- Include the traceback. It will most likely point out the exact problem.
- 至少总是提供输入形状.
- 发布整个神经网络架构.
- 包括您的库导入.定义所有变量.
- Post your entire neural network architecture.
- Include your library imports. Define all variables.
以下是完美最小且可重现的示例:
Here is an example of a perfect minimal and reproducible example:
我有一个错误.运行此代码时,它给了我这个错误:"
这是我的体系结构,带有生成的数据:"
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))
这篇关于如何为神经网络建立一个最小且可复制的示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
- E.g.,
- Use randomly generated numbers of the right shape.
- 例如
- 使用随机生成的正确形状的数字.