首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Neupy神经网络问题

Neupy神经网络问题
EN

Stack Overflow用户
提问于 2017-01-12 15:09:30
回答 1查看 723关注 0票数 1

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

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

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

有关守则:

代码语言:javascript
复制
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)

输出:

代码语言:javascript
复制
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

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

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-01-12 16:28:07

有几件事情您需要修改:

  1. 您已经提到您正在尝试解决回归问题。您的网络有一个Softmax层作为输出,这意味着您的网络只能提供0,1范围的输出,而不是-1,1。您可以将其更改为Tanh层。它将产生-1,1范围的输出。
  2. 只适用于分类问题的交叉熵误差 误差=“范畴交叉熵” 对于回归,您可以使用MSE或RMSE (更多的错误函数可以找到这里)。 误差=‘mse’
  3. 我假设在(66,160,3)形状中,数字3定义了每个RGB通道。NeuPy与Theano库一起工作,这意味着您需要在图像的宽度和高度之前定义通道形状。正确的顺序是:(n_samples, n_channels, height, width)。在您的情况下,我假设您有84个样本,66个像素的高度,160个像素的宽度和3个通道(RGB)。如果这是正确的,那么您需要按如下方式转换输入:将此形状(n_samples、高度、宽度、n_channels) #转换为(n_samples、n_channels、高度、宽度) x_train =x_train.transpose(0、3、1、2)打印(x_train.shape)# (84、3、66、160)
  4. 最后一层的输出应该是1而不是10。这意味着每个样本只能预测一个值,而不是10个值的向量(使用layers.Tanh(1)而不是layers.Softmax(10))。

以下代码工作正常,没有错误(由于值是随机的),因此不能正确地进行训练:

代码语言:javascript
复制
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)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41616550

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档