卷积层输出在通过每个图像的完全连接层后变为1.

时间:2015-12-31 12:52:37

标签: python machine-learning neural-network theano conv-neural-network

我使用theano建立了CNN。卷积层和隐藏层的代码是:

class HiddenLayer(Layer):

    def __init__(self,n_in,n_out,inp_vector=T.dmatrix(),non_linearity='sigmoid',W=None,b=None):
        Layer.__init__(self,n_in,n_out,W,b)
        self.inp=inp_vector
        out=self.non_lins[non_linearity](inp_vector.dot(self.W.transpose())+self.b)
        self.output=out

class ConvolutionLayer(Layer):
    def __init__(self,W_shape,b_shape,image_shape,inp_vector=T.tensor4(),maxpool=(2,2),non_linearity='tanh',W=None,b=None,flatten=False,batch=1):
        W=theano.shared(numpy.random.standard_normal(W_shape))
        b=theano.shared(numpy.random.random(b_shape))
        Layer.__init__(self,0,0,W,b)
        self.inp=inp_vector
        out=convop.conv2d(input=inp_vector,filters=self.W,filter_shape=W_shape,image_shape=image_shape)
        self.output=self.non_lins[non_linearity](downsample.max_pool_2d(out,maxpool,ignore_border=True)+self.b.dimshuffle('x',0,'x','x'))
        if flatten:
            self.output=self.output.flatten(batch)
        else:
            self.output=self.output

我的神经网络有5层。但是为了检查出错的地方,我剥离了所有其他隐藏的层,只保留了连接到卷积层的层。我观察到卷积层的输出很好,但是在通过完全连接的层之后,它变成了:

(2, 5000)
[[-1. -1. -1. ..., -1. -1. -1.]
 [-1. -1. -1. ..., -1. -1. -1.]]

这里2,5000是隐藏层的输出矩阵的形状。网络实施是:

layer0=machinebrain.ConvolutionLayer(image_shape=(2,3,480,640),W_shape=
(2,3,5,5),maxpool=(4,4),b_shape=(2,))
layer1=machinebrain.ConvolutionLayer(image_shape=(2,2,119,159),inp_vector=
layer0.output,maxpool=(2,2),W_shape=(3,2,5,5),b_shape=(3,),flatten=True,batch=2)
layer3=machinebrain.HiddenLayer(inp_vector=layer1.output,
non_linearity='tanh',n_in=13167,n_out=5000)

知道什么可能导致卷积层的输出在通过隐藏层后变为全部1.?

1 个答案:

答案 0 :(得分:1)

您的非线性是tanh,其饱和值为-1(对应于非常负输入)和+1(对应于非常大的正输入)。也许层的激活会给你非常大的数值?您可以尝试对输入进行全局对比度归一化(零中心并除以标准偏差),以更好地调整输入值。还可以尝试切换到ReLU激活甚至线性激活,以检查从您引用的特定图层获得的激活值类型。