Python无法识别已定义的函数

时间:2019-03-09 23:17:50

标签: python conv-neural-network

我有一个名为 <div class="emoji-container"> <div class="emoji">&#x1F3E0;</div> </div>的对象,其中包含定义激活函数network3linear以及层ReLu的代码:

ConvPoolLayer

但是,在Jupyter笔记本中,当我运行以下代码时:

### network3.py
# Libraries
import numpy as np
import theano
import theano.tensor as T
from theano.tensor.nnet import conv
from theano.tensor.nnet import softmax
from theano.tensor import shared_randomstreams
from theano.tensor.signal import pool

# Activation functions for neurons
def linear(z): return z
def ReLU(z): return T.maximum(0.0, z)
from theano.tensor.nnet import sigmoid
from theano.tensor import tanh
...
class ConvPoolLayer(object):
    def __init__(self, filter_shape, image_shape, poolsize=(2, 2), activation_fn=sigmoid):
    self.activation_fn=activation_fn
...              

返回错误:

import network3
from network3 import Network
from network3 import ConvPoolLayer , FullyConnectedLayer , SoftmaxLayer

net = Network([
    ConvPoolLayer(image_shape=(mini_batch_size , 1, 28, 28),
                  filter_shape=(20, 1, 5, 5),
                  poolsize=(2, 2),
                  activation_fn=ReLU),
    ConvPoolLayer(image_shape=(mini_batch_size , 20, 12, 12),
                  filter_shape=(40, 20, 5, 5),
                  poolsize=(2, 2),
                  activation_fn=ReLU),
    FullyConnectedLayer(n_in=40*4*4, n_out=100, activation_fn=ReLU),
    SoftmaxLayer(n_in=100, n_out=10)], 
    mini_batch_size)

net.SGD(expanded_training_data, 60, mini_batch_size, 0.03, 
        validation_data, test_data , lmbda=0.1)

我将代码更改为NameError Traceback (most recent call last)<ipython-input-4-55105ca84f86> in <module> 5 filter_shape=(20, 1, 5, 5), 6 poolsize=(2, 2), ----> 7 activation_fn=ReLU), 8 ConvPoolLayer(image_shape=(mini_batch_size , 20, 12, 12), 9 filter_shape=(40, 20, 5, 5), NameError: name 'ReLU' is not defined ,并且遇到了类似的错误。

您知道为什么无法识别定义的激活功能吗?

1 个答案:

答案 0 :(得分:1)

正如@khelwood在使用not checked导入模块时评论的那样,您需要提供名称空间来访问import network3中定义的函数和类:

network3

要仅使用network3.ReLu network3.linear network3.ConvPoolLayer ReLulinear,就可以将导入行更改为以下之一:

ConvPoolLayer

from network3 import *