深度学习的始祖框架,grandfather级别的框架 —— Theano —— 示例代码学习(3)

实战:写一个卷积层ConvolutionLayer



二维卷积的前向操作:

代码:

import theano.tensor as TT
import theano
import numpy as np
# from theano.tensor.shared_randomstreams import RandomStreams


Identity = lambda x: x
ReLU = lambda x: TT.maximum(x, 0.0)
Sigmoid = lambda x: TT.nnet.sigmoid(x)
Tanh = lambda x: TT.tanh(x)


"""
input:  (batchsize, nb_channel, nb_i_row, nb_i_column)
filter: (nb_filters,nb_channel, nb_f_row, nb_f_column)
output: (batchsize, nb_filters, nb_o_row, nb_o_column)
"""
class ConvLayer(object):
    def __init__(self, image_shape, filter_shape, activation):
        self.activation = eval(activation)
        self.image_shape = image_shape
        self.filter_shape = filter_shape
        rng = np.random.RandomState(seed=42)
        w_init = np.asarray(rng.uniform(low=-1, high=1,  size=filter_shape), dtype='float32')
        b_init = np.zeros(filter_shape[0], dtype='float32')
        self.w = theano.shared(value=w_init, borrow=True)
        self.b = theano.shared(value=b_init, borrow=True)

    def feedforward(self, x):
        cout = TT.nnet.conv.conv2d(x, self.w, self.image_shape, self.filter_shape)

        # dimshuffle后,shape在'x'位置都被补1
        output = self.activation(cout + self.b.dimshuffle('x', 0, 'x', 'x'))
        return output

x = TT.tensor4('x', dtype="float32")  # 维度为4的tensor, tensor4

con = ConvLayer((32, 3, 224, 224), (64, 3, 5, 5), "ReLU")
out_put = con.feedforward(x)
con_run = theano.function([x, ], out_put, allow_input_downcast=True)


image_input = np.array( np.random.rand(32, 3, 224, 224), dtype="float32" )
# print( con_run(image_input) )
print( con_run(image_input).shape )

运行结果:

image



参考:

https://zhuanlan.zhihu.com/p/24275551



posted on 2024-02-12 17:32  Angry_Panda  阅读(29)  评论(0)    收藏  举报

导航