用Python进行神经网络初始化、正则、优化

初始化:

def model(...initilalization = 'he'):

if intitialization == 'he':

parameters = initialize_parameters_he(layers_dims)

-------------------------------------------------

def initialize_parameters_he(layers_dims):

np.random.seed(3)

parameters = {}

L = len(layers_dims) - 1

for i in range(1, L+1):

parameters['W' + str(l)] = np.random.randn(layers_dims[l], layers_dims[l-1]) * np.sqrt(2. / layers_dims[l-1])

parameters['b' + str(l)] = np.zeros((layers_dims[l], 1))

return parameters

正则化:

def model(..., lambd = 0, keep_prob = 1):

...

for i in range(0, num_iterations):

if keep_prob == 1:

a3, cache = forward_propagation(X, parameters)

elif keep_prob < 1:

a3, cache = forward_propagation_with_dropout(X, parameters, keep_prob)

if lambd == 0:

cost = compute_cost(a3, Y)

else:

cost = compute_cost_with_regularization(a3, Y, parameters, lambd)

...

return parameters

----------------------------------------------------------

def compute_cost_with_regularization(A3, Y, parameters, lambd):

m = Y.shape[1]

W1 = parameters['W1']

W2 = parameters['W2']

W3 = parameters['W3']

cross_entropy_cost = compute _cost(A3, Y)

L2_regularization_cost = (np.sum(np.square(W1)) + np.sum(np.square(W2)) + np.sum(np.square(W3))) * lambd / 2 / m

cost = cross_entropy_cost + L2_regularization_cost

return cost

-----------------------------------------------------------

def backward_propagation_with_regularization(X, Y, cache, lambd):

m = X.shape[1]

(Z1, A1, W1, b1, Z2, A2, W2, b2, Z3, A3, W3, b3) = cache

dZ3 = A3 - Y

dW3 = 1. / m * np.dot(dZ3,  A2.T) + lambd / m * W3

db3 = 1. / m * np.sum(dZ3, axis=1, keepdims = True)

dA2 = np.dot(W3.T, dZ3)

dZ2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过在定义网络结构时,使用 PyTorch 的 nn.Module 和 nn.Sequential 来添加则化层,例如L1则化和L2则化,如下所示: ```python import torch import torch.nn as nn class Net(nn.Module): def __init__(self): super(Net, self).__init__() self.conv_layer = nn.Sequential( nn.Conv2d(in_channels=3, out_channels=16, kernel_size=3, stride=1, padding=1), nn.BatchNorm2d(num_features=16), nn.ReLU(), nn.MaxPool2d(kernel_size=2, stride=2), nn.Conv2d(in_channels=16, out_channels=32, kernel_size=3, stride=1, padding=1), nn.BatchNorm2d(num_features=32), nn.ReLU(), nn.MaxPool2d(kernel_size=2, stride=2) ) self.fc_layer = nn.Sequential( nn.Linear(in_features=32*8*8, out_features=256), nn.BatchNorm1d(num_features=256), nn.ReLU(), nn.Linear(in_features=256, out_features=10) ) # add L1 regularization to convolutional layers self.conv_layer[0].register_forward_pre_hook(self.l1_norm) self.conv_layer[4].register_forward_pre_hook(self.l1_norm) # add L2 regularization to fully connected layers self.fc_layer[0].register_forward_pre_hook(self.l2_norm) self.fc_layer[3].register_forward_pre_hook(self.l2_norm) def forward(self, x): x = self.conv_layer(x) x = x.view(-1, 32*8*8) x = self.fc_layer(x) return x def l1_norm(self, module, input): module.weight.data = torch.nn.functional.l1_normalize(module.weight.data, dim=0, eps=1e-12) def l2_norm(self, module, input): module.weight.data = torch.nn.functional.normalize(module.weight.data, p=2, dim=0, eps=1e-12) ``` 在上述代码中,通过定义一个名为 Net 的卷积神经网络类,并在初始化方法中添加则化层,即可在卷积神经网络中加入则化。具体实现方式为: 对于 L1 则化,我们通过 register_forward_pre_hook 方法来拦截则化层前的输出,并使用 PyTorch 提供的 L1 则化函数 l1_normalize 对卷积核权重进行 L1 则化处理。 对于 L2 则化,我们同样通过 register_forward_pre_hook 方法来拦截则化层前的输出,并使用 PyTorch 提供的 L2 则化函数 normalize 对全连接层权重进行 L2 则化处理。 这样,在训练中,我们就可以通过传入适当的则化系数,来对网络的参数进行则化处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值