V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
ALLROBOT
V2EX  ›  Python

CNN 入门, pycharm 报错:数组是 0 维的,但是有 1 个被索引了

  •  
  •   ALLROBOT · 2022-02-11 17:01:53 +08:00 · 1857 次点击
    这是一个创建于 797 天前的主题,其中的信息可能已经有所发展或是发生改变。

    IndexError: too many indices for array: array is 0-dimensional, but 1 were indexedS

    计算误差项反向传播,结果报错了

    具体代码参见https://github.com/allrobot/Transit_Depot/tree/main/cnn

    教程抄的代码,搞不懂有一个被索引了表达什么意思,能指教一下这里如何处理吗?

    013231
        1
    013231  
       2022-02-11 17:37:14 +08:00
    这是 numpy 的错误。对 0 维数组(一个标量转化成的数组)索引导致的。你看看 conv 的前 3 个参数是哪个出问题了。
    qwq11
        2
    qwq11  
       2022-02-11 17:44:10 +08:00 via Android
    0 维:
    arr
    1 维:
    arr[i]
    二维:
    arr[i, j]
    三维:
    arr[i, j, k]
    不知道 0 维怎么表示,总之就是 n 维数组你需要 n 个 index 去索引,但是对于 0 维数组你使用了 1 个 index 去索引
    ALLROBOT
        3
    ALLROBOT  
    OP
       2022-02-11 19:04:27 +08:00
    @qwq11 @013231 原来是这么解释的,感谢!报错挺奇怪的,conv 参数分别是卷积输入矩阵、卷积核( filter )、输出矩阵(全零矩阵)、步长和偏置项,计算的是卷积输出
    ```python
    def conv(input_array, kernel_array, output_array, stride, bias):
    """
    计算卷积,自动适配输入为 2D 和 3D 的情况
    """
    channel_number = input_array.ndim
    output_width = output_array.shape[1]
    output_height = output_array.shape[0]
    kernel_width = kernel_array.shape[-1]
    kernel_height = kernel_array.shape[-2]
    for i in range(output_height):
    for j in range(output_width):
    output_array[i][j] = (get_patch(input_array, i, j, kernel_width,
    kernel_height, stride) * kernel_array
    ).sum() + bias
    ```

    怎么会提示下标太多,多了一个 indexed ?
    013231
        4
    013231  
       2022-02-11 19:10:10 +08:00
    @ALLROBOT 问题不在 conv 内部,是调用处的问题。padded_array 或 flipped_weights 是 0 维数组。
    ALLROBOT
        5
    ALLROBOT  
    OP
       2022-02-11 19:35:43 +08:00
    @013231 解决了,教程的 CNN 代码是 python2 ,我改成 python3 ,有些函数不兼容导致报错~
    上面 180 度翻转误差项矩阵的代码中,map 在 py2 返回列表,py3 返回迭代器
    添加 list 就完事了
    ```
    flipped_weights = np.array(list(map(
    lambda i: np.rot90(i, 2),
    filter.get_weights())))
    ```
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   988 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 22:11 · PVG 06:11 · LAX 15:11 · JFK 18:11
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.