Пример #1
0
    def __init__(self,
                 num_in=0,
                 num_h1=0,
                 num_h2=0,
                 num_out=0,
                 dropout=1.0,
                 weight_scale=1e-2):
        super(ThreeLayerNet, self).__init__()
        self.fc1 = nn.FC(num_in, num_h1, weight_scale=weight_scale)
        self.fc2 = nn.FC(num_h1, num_h2, weight_scale=weight_scale)
        self.fc3 = nn.FC(num_h2, num_out, weight_scale=weight_scale)

        self.relu = nn.ReLU()
        self.z1_cache = None
        self.z1 = None
        self.z2_cache = None
        self.z2 = None
        self.z3_cache = None

        self.use_dropout = dropout != 1.0
        self.dropout_param = {}
        if self.use_dropout:
            self.dropout_param['mode'] = 'train'
            self.dropout_param['p'] = dropout
            self.dropout = nn.Dropout()
            self.U1 = None
            self.U2 = None

        self.params = self._get_params()
Пример #2
0
    def __init__(self,
                 in_channels=1,
                 out_channels=10,
                 dropout=1.0,
                 weight_scale=1e-2):
        super(LeNet5, self).__init__()
        self.conv1 = nn.Conv2d2(in_channels,
                                5,
                                5,
                                6,
                                stride=1,
                                padding=0,
                                weight_scale=weight_scale)
        self.conv2 = nn.Conv2d2(6,
                                5,
                                5,
                                16,
                                stride=1,
                                padding=0,
                                weight_scale=weight_scale)
        self.conv3 = nn.Conv2d2(16, 5, 5, 120, stride=1, padding=0)

        self.maxPool1 = nn.MaxPool2(2, 2, 6, stride=2)
        self.maxPool2 = nn.MaxPool2(2, 2, 16, stride=2)
        self.fc1 = nn.FC(120, 84)
        self.fc2 = nn.FC(84, out_channels)

        self.relu = nn.ReLU()
        self.z1 = None
        self.z1_cache = None
        self.z2 = None
        self.z2_cache = None
        self.z3 = None
        self.z3_cache = None
        self.z4 = None
        self.z4_cache = None
        self.z5 = None
        self.z5_cache = None
        self.z6 = None
        self.z6_cache = None
        # self.z7 = None
        self.z7_cache = None

        self.use_dropout = dropout != 1.0
        self.dropout_param = {}
        if self.use_dropout:
            self.dropout_param['mode'] = 'train'
            self.dropout_param['p'] = dropout
            self.dropout = nn.Dropout()
            self.U1 = None

        self.params = self._get_params()
Пример #3
0
    def __init__(self,
                 hidden_dims,
                 input_dim=3 * 32 * 32,
                 num_classes=10,
                 normalization=None,
                 dropout=1.0,
                 seed=None,
                 weight_scale=1e-2,
                 dtype=np.double):
        super(FCNet, self).__init__()
        self.hidden_dims = hidden_dims
        self.input_dim = input_dim
        self.num_classes = num_classes
        self.normalization = normalization
        self.weight_scale = weight_scale
        self.dtype = dtype

        self.use_dropout = dropout != 1
        self.num_layers = 1 + len(hidden_dims)
        self.relu = nn.ReLU()

        self.fcs = self._get_fcs()
        self.params = self._get_params()
        self.caches = self._get_caches()

        self.use_dropout = dropout != 1.0
        self.dropout_param = {}
        if self.use_dropout:
            self.dropout = nn.Dropout()

            self.dropout_param['mode'] = 'train'
            self.dropout_param['p'] = dropout
            if seed is not None:
                self.dropout_param['seed'] = seed

        self.bn_params = []
        if self.normalization == 'batchnorm':
            self.bn = nn.BN()

            for i in range(len(hidden_dims)):
                self.params['gamma%d' % (i + 1)] = np.ones(hidden_dims[i])
                self.params['beta%d' % (i + 1)] = np.zeros(hidden_dims[i])
                self.bn_params.append({'mode': 'train'})

        # 转换可学习参数为指定数据类型
        for k, v in self.params.items():
            self.params[k] = v.astype(dtype)
Пример #4
0
    def __init__(self, in_channels, out_channels, momentum=0, nesterov=False):
        super(AlexNet, self).__init__()
        self.conv1 = nn.Conv2d(in_channels,
                               11,
                               11,
                               96,
                               stride=4,
                               padding=0,
                               momentum=momentum,
                               nesterov=nesterov)
        self.conv2 = nn.Conv2d(96,
                               5,
                               5,
                               256,
                               stride=1,
                               padding=2,
                               momentum=momentum,
                               nesterov=nesterov)
        self.conv3 = nn.Conv2d(256,
                               3,
                               3,
                               384,
                               stride=1,
                               padding=1,
                               momentum=momentum,
                               nesterov=nesterov)
        self.conv4 = nn.Conv2d(384,
                               3,
                               3,
                               384,
                               stride=1,
                               padding=1,
                               momentum=momentum,
                               nesterov=nesterov)
        self.conv5 = nn.Conv2d(384,
                               3,
                               3,
                               256,
                               stride=1,
                               padding=1,
                               momentum=momentum,
                               nesterov=nesterov)

        self.maxPool1 = nn.MaxPool(3, 3, 96, stride=2)
        self.maxPool2 = nn.MaxPool(3, 3, 256, stride=2)
        self.maxPool3 = nn.MaxPool(3, 3, 256, stride=2)
        self.fc1 = nn.FC(9216, 4096, momentum=momentum, nesterov=nesterov)
        self.fc2 = nn.FC(4096, 4096, momentum=momentum, nesterov=nesterov)
        self.fc3 = nn.FC(4096,
                         out_channels,
                         momentum=momentum,
                         nesterov=nesterov)

        self.relu1 = nn.ReLU()
        self.relu2 = nn.ReLU()
        self.relu3 = nn.ReLU()
        self.relu4 = nn.ReLU()
        self.relu5 = nn.ReLU()
        self.relu6 = nn.ReLU()
        self.relu7 = nn.ReLU()

        self.dropout = nn.Dropout()

        self.U1 = None
        self.U2 = None