莫烦theano学习自修第五天【定义神经层】

时间:2023-03-08 15:26:39
莫烦theano学习自修第五天【定义神经层】

1. 代码如下:

#!/usr/bin/env python
#! _*_ coding:UTF-8 _*_

import numpy as np
import theano.tensor as T
import theano

class Layer(object):
    '''
    定义一个神经层
    '''

    def __init__(self, inputs, in_size, out_size, activation_function=None):

        '''神经层的初始化方法'''
        self.W = theano.shared(np.random.normal(0, 1, (in_size, out_size)))

        self.b = theano.shared(np.zeros((out_size, ) + 0.1))

        self.Wx_plus_b = T.dot(inputs, self.W) + self.b

        self.activation_function = activation_function

        # 增加激励函数的处理
        if activation_function is None:
            self.outputs = self.Wx_plus_b
        else:
            self.outputs = self.activation_function(self.Wx_plus_b)