【问题标题】:Neupy neural network issuesNeupy 神经网络问题
【发布时间】:2017-05-27 18:13:33
【问题描述】:

我正在尝试为一个项目训练/使用带有 neupy 库的卷积神经网络,但我在训练阶段遇到了错误。

我有很多图像(rgb, shape=66, 160, 3),我将它们分成训练集和测试集。然后我正在尝试训练一个卷积神经网络(稍后我将尝试使用不同的算法、层数和大小进行优化)。我的项目的目标输出是一个数字 [-1, 1],我正在解决一个回归问题,但我之前遇到过问题。

我现在遇到的错误是: ValueError:无法洗牌矩阵。所有矩阵应该有相同的行数

相关代码:

print numpy.array(y_train).shape
# outputs (84, 66, 160, 3)
print numpy.array(y_test).shape
# outputs (15, 66, 160, 3)

cgnet = algorithms.Adadelta(
    [
        layers.Input((6, 66, 160*3)),

        layers.Convolution((8, 3, 3)),
        layers.Relu(),
        layers.Convolution((8, 3, 3)),
        layers.Relu(),
        layers.MaxPooling((2, 2)),
        layers.Reshape(),
        layers.Linear(1024),
        layers.Softmax(10),
    ],

    error='categorical_crossentropy',
    step=1.0,
    verbose=True,
    shuffle_data=True,
    #shuffle_data=False,

    reduction_freq=8,
    addons=[algorithms.StepDecay],
)

print cgnet.architecture()
cgnet.train(x_train, y_train, x_test, y_test, epochs=100)

输出:

Main information

[ALGORITHM] Adadelta

[OPTION] batch_size = 128
[OPTION] verbose = True
[OPTION] epoch_end_signal = None
[OPTION] show_epoch = 1
[OPTION] shuffle_data = True
[OPTION] step = 1.0
[OPTION] train_end_signal = None
[OPTION] error = categorical_crossentropy
[OPTION] addons = ['StepDecay']
[OPTION] decay = 0.95
[OPTION] epsilon = 1e-05
[OPTION] reduction_freq = 8

[THEANO] Initializing Theano variables and functions.
[THEANO] Initialization finished successfully. It took 7.01 seconds

Network's architecture

-------------------------------------------------
| # | Input shape  | Layer Type  | Output shape |
-------------------------------------------------
| 1 | (6, 66, 480) | Input       | (6, 66, 480) |
| 2 | (6, 66, 480) | Convolution | (8, 64, 478) |
| 3 | (8, 64, 478) | Relu        | (8, 64, 478) |
| 4 | (8, 64, 478) | Convolution | (8, 62, 476) |
| 5 | (8, 62, 476) | Relu        | (8, 62, 476) |
| 6 | (8, 62, 476) | MaxPooling  | (8, 31, 238) |
| 7 | (8, 31, 238) | Reshape     | 59024        |
| 8 | 59024        | Linear      | 1024         |
| 9 | 1024         | Softmax     | 10           |
-------------------------------------------------

None

Start training

[TRAIN DATA] 84 samples, feature shape: (66, 160, 3)
[TEST DATA] 15 samples, feature shape: (66, 160, 3)
[TRAINING] Total epochs: 100

------------------------------------------------
| Epoch # | Train err | Valid err | Time       |
------------------------------------------------
Traceback (most recent call last):
  File "./ml_neupy.py", line 68, in <module>
    cgnet.train(x_train, y_train, x_test, y_test, epochs=100)
  File "/usr/local/lib/python2.7/dist-packages/neupy/algorithms/constructor.py", line 539, in train
    *args, **kwargs
  File "/usr/local/lib/python2.7/dist-packages/neupy/algorithms/learning.py", line 49, in train
    summary=summary
  File "/usr/local/lib/python2.7/dist-packages/neupy/algorithms/base.py", line 409, in train
    target_train)
  File "/usr/local/lib/python2.7/dist-packages/neupy/algorithms/utils.py", line 146, in shuffle
    raise ValueError("Cannot shuffle matrices. All matrices should "
ValueError: Cannot shuffle matrices. All matrices should have the same number of rows

输入数据或网络有什么问题?

谢谢

【问题讨论】:

  • x_train 和 x_test 变量的形状是什么?
  • x_train.shape[0] != y_train.shape[0] 时出现此错误
  • 另外,形状(6、66、160*3)代表什么?数字 6 是什么意思?
  • x_train (84, 3, 66, 160) x_test (15, 3, 66, 160) y_train (84,) y_test (15,)
  • 我试图通过重塑图像来合并 3 个 rgb 通道,160*3 是一个测试,试图让它接受输入

标签: conv-neural-network neupy


【解决方案1】:

有几处需要修改:

  1. 您提到您正在尝试解决回归问题。您的网络有一个 Softmax 层作为输出,这意味着您的网络只能为您提供 [0, 1] 范围内的输出,而不是 [-1, 1]。您可以将其更改为 Tanh 图层。它将产生 [-1, 1] 范围内的输出。

  2. 只适用于分类问题的交叉熵误差

    error='categorical_crossentropy'
    

    对于回归,您可以使用 MSE 或 RMSE(更多误差函数您可以找到 here

    error='mse'
    
  3. 我假设在 (66, 160, 3) 形状中,数字 3 定义了每个 RGB 通道。 NeuPy 与 Theano 库一起使用,这意味着您需要在图像的宽度和高度之前定义通道形状。正确的顺序是:(n_samples, n_channels, height, width)。在您的情况下,我假设您有 84 个样本、66 像素的高度、160 像素的宽度和 3 个通道 (RGB)。如果是这样,那么您需要按如下方式转换您的输入

    # convert this shape (n_samples, height, width, n_channels)
    # to (n_samples, n_channels, height, width)
    x_train = x_train.transpose((0, 3, 1, 2))
    print(x_train.shape)  # (84, 3, 66, 160)
    
  4. 最后一层的输出应该是 1 而不是 10。这意味着每个样本只预测一个值,而不是具有 10 个值的向量(使用 layers.Tanh(1) 而不是 layers.Softmax(10)

以下代码可以正常工作(没有正确训练,因为值是随机的):

import numpy
from neupy import algorithms, layers


x_train = numpy.random.random((84, 3, 66, 160))
x_test = numpy.random.random((15, 3, 66, 160))
y_train = numpy.random.random(84)
y_test = numpy.random.random(15)

cgnet = algorithms.Adadelta(
    [
        layers.Input((3, 66, 160)),

        layers.Convolution((8, 3, 3)),
        layers.Relu(),
        layers.Convolution((8, 3, 3)),
        layers.Relu(),
        layers.MaxPooling((2, 2)),

        layers.Reshape(),
        layers.Linear(1024),
        layers.Tanh(1),
    ],

    error='mse',
    step=1.0,
    verbose=True,
    shuffle_data=True,

    reduction_freq=8,
    addons=[algorithms.StepDecay],
)

cgnet.architecture()
cgnet.train(x_train, y_train, x_test, y_test, epochs=100)

【讨论】:

  • 现在我得到:CorrMM 图像和内核必须具有相同的堆栈大小错误:CorrMM{valid, (1, 1)}(algo:network/var:network-input, Subtensor{::, ::, ::int64, ::int64}.0) 拓扑排序索引:28 类型:[TensorType(float64, 4D), TensorType(float64, 4D)] 形状:[(84, 3, 66, 160), (8, 6, 3, 3)] 步幅: [(253440, 84480, 1280, 8), (432, 72, -24, -8)] 输出客户端:[[Elemwise{add,no_inplace}(CorrMM{有效,(1, 1)}.0, Reshape{4}.0), Elemwise{Composite{(i0 * (Abs(i1) + i2 + i3))}}[(0, 2)](TensorConstant{( 1, 1, 1, 1) of 0.5}, Elemwise{add,no_inplace}.0, CorrMM{valid, (1, 1)}.0, Reshape{4}.0)]]
  • 尝试修改输入层的形状,如上面的代码(layers.Input((3, 66, 160))
  • 现在训练!我还没有看到最终结果,但它已经经历了几个时代。谢谢
猜你喜欢
  • 2011-02-22
  • 2013-10-09
  • 1970-01-01
  • 2015-04-18
  • 1970-01-01
  • 1970-01-01
  • 2022-08-15
  • 1970-01-01
  • 2011-03-29
相关资源
最近更新 更多