【问题标题】:Python 2.7 and Cython: global name col2im_6d_cython is not definedPython 2.7 和 Cython:未定义全局名称 col2im_6d_cython
【发布时间】:2017-04-17 01:59:17
【问题描述】:

我在 Ubuntu 16.04 上遇到了 python 2.7 和 Cython 的问题。 我正在尝试运行 cs231n 课程(卷积神经网络)中的代码。 但唯一的功能 col2im_6d_cython 不起作用。错误是:

NameError: global name 'col2im_6d_cython' is not defined

函数col2im_6d_cython定义在im2col_cython.pyx:

def col2im_6d_cython(np.ndarray[DTYPE_t, ndim=6] cols, int N, int C, int H, int W,
        int HH, int WW, int pad, int stride):
    cdef np.ndarray x = np.empty((N, C, H, W), dtype=cols.dtype)
    cdef int out_h = (H + 2 * pad - HH) / stride + 1
    cdef int out_w = (W + 2 * pad - WW) / stride + 1
    cdef np.ndarray[DTYPE_t, ndim=4] x_padded = np.zeros((N, C, H + 2 * pad, W + 2 * pad),
                                                         dtype=cols.dtype)

    col2im_6d_cython_inner(cols, x_padded, N, C, H, W, HH, WW, out_h, out_w, pad, stride)

    if pad > 0:
        return x_padded[:, :, pad:-pad, pad:-pad]
    return x_padded 

调用col2im_6d_cython的文件是fast_layers.py

from cs231n.im2col_cython import col2im_cython, im2col_cython
from cs231n.im2col_cython import col2im_6d_cython

def conv_backward_strides(dout, cache):
        x, w, b, conv_param, x_cols = cache
    stride, pad = conv_param['stride'], conv_param['pad']

    N, C, H, W = x.shape
    F, _, HH, WW = w.shape
    _, _, out_h, out_w = dout.shape

    db = np.sum(dout, axis=(0, 2, 3))

    dout_reshaped = dout.transpose(1, 0, 2, 3).reshape(F, -1)
    dw = dout_reshaped.dot(x_cols.T).reshape(w.shape)

    dx_cols = w.reshape(F, -1).T.dot(dout_reshaped)
    dx_cols.shape = (C, HH, WW, N, out_h, out_w)
    dx = col2im_6d_cython(dx_cols, N, C, H, W, HH, WW, pad, stride)

    return dx, dw, db

col2im_cythonim2col_cython 工作正常,但只有 col2im_6d_cython 不起作用。

在我看来,Cython 安装存在问题。我已经通过运行安装它:python setup.py build_ext --inplace

setup.py 是:

from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize
import numpy

extensions = [
Extension('im2col_cython', ['im2col_cython.pyx'],
        include_dirs = [numpy.get_include()]
),
]

setup(
    ext_modules = cythonize(extensions),
)

安装 Cython 时出现警告:

Warning: Extension name 'im2col_cython' does not match fully qualified name 'cs231n.im2col_cython' of 'im2col_cython.pyx'
running build_ext

为什么只有 col2im_6d_cython 不起作用?有什么办法可以解决吗?

提前谢谢大家!

【问题讨论】:

  • 这对您没有帮助,但是有一长串与 cs231 课程有类似问题的人在这里发布了相关问题,但没有得到任何真正的解决方案。

标签: python cython


【解决方案1】:

通过完全卸载 Anaconda3 并安装 Anaconda2 解决了问题。然后我创建了新环境并重新安装了所有需要的包。现在没有出现错误

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-16
    • 1970-01-01
    • 2017-02-03
    • 2013-08-23
    • 2013-11-12
    • 2014-12-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多