如何为神经网络建立一个最小且可复制的示例? [英] How do I make a minimal and reproducible example for neural networks?

查看:130
本文介绍了如何为神经网络建立一个最小且可复制的示例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何为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?

值错误:检查输入时出错:预期density_1_input具有形状 (4,)但形状为(1,)

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

推荐答案

以下是制作可重复的,最少的深度学习示例的一些技巧.无论是KerasPytorch还是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:

          我有一个错误.运行此代码时,它给了我这个错误:"

          ValueError:检查目标时出错:预期density_2具有形状(10,)但形状为数组

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

          这是我的体系结构,带有生成的数据:"

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

          这篇关于如何为神经网络建立一个最小且可复制的示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆