吴恩达深度学习第二课第二周:L2正则化和dropout

本文探讨了深度学习中防止过拟合的两种常用方法:L2正则化和Dropout。首先,介绍了如何使用L2正则化调整成本函数,通过增加权重的平方和项来限制模型复杂度。接着,详细阐述了Dropout技术,这是一种在训练过程中随机关闭神经元的策略,以避免过度依赖特定节点。通过实验,模型在不使用正则化和Dropout时存在过拟合,但应用正则化和Dropout后,模型在训练集和测试集上的表现均有显著提升,特别是在测试集上的准确率达到了95%,展示了正则化和Dropout的有效性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

# coding: utf-8

# # Regularization
# 
# Welcome to the second assignment of this week. Deep Learning models have so much
# flexibility and capacity that overfitting can be a serious problem, if the training
# dataset is not big enough. Sure it does well on the training set, but the learned
# network doesn't generalize to new examples that it has never seen!
# 
# You will learn to:Use regularization in your deep learning models.
# Let's first import the packages you are going to use.

# In[1]:调用数据的各个库

# import packages
import numpy as np
import matplotlib.pyplot as plt
from reg_utils import sigmoid, relu, plot_decision_boundary, initialize_parameters, load_2D_dataset, predict_dec
from reg_utils import compute_cost, predict, forward_propagation, backward_propagation, update_parameters
import sklearn
import sklearn.datasets
import scipy.io
from testCases import *

#get_ipython().magic('matplotlib inline')
plt.rcParams['figure.figsize'] = (7.0, 4.0) # set default size of plots
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'

# **Problem Statement: You have just been hired as an AI expert by the French
# Football Corporation. They would like you to recommend positions where France's
# goal keeper should kick the ball so that the French team's players can then hit it
# with their head.

# The goal keeper kicks the ball in the air, the players of each team are fighting
# to hit the ball with their head
#
# They give you the following 2D dataset from France's past 10 games.
#In[2]:读取数据

train_X, train_Y, test_X, test_Y = load_2D_dataset()

# Each dot corresponds to a position on the football field where a football player has
# hit the ball with his/her head after the French goal keeper has shot the ball from the
# left side of the football field.
# - If the dot is blue, it means the French player managed to hit the ball with his/her head
# - If the dot is red, it means the other team's player hit the ball with their head
# 
#Your goal: Use a deep learning model to find the positions on the field where the
# goalkeeper should kick the ball.

#Analysis of the dataset: This dataset is a little noisy, but it looks like a diagonal
# line separating the upper left half (blue) from the lower right half (red) would work well.
# 
# You will first try a non-regularized model. Then you'll learn how to regularize it and
# decide which model you will choose to solve the French Football Corporation's problem.

# ## In[1]:- Non-regularized model
# 
# You will use the following neural network (already implemented for you below). This model
# can be used:
# - in regularization mode -- by setting the `lambd` input to a non-zero value. We use
# "`lambd`" instead of "`lambda`" because "`lambda`" is a reserved keyword in Python.
# - in dropout mode-- by setting the `keep_prob` to a value less than one
# 
# You will first try the model without any regularization. Then, you will implement:
# - L2 regularization -- functions: "`compute_cost_with_regularization()`" and
# "`backward_propagation_with_regularization()`"
# - Dropout -- functions: "`forward_propagation_with_dropout()`" and
# "`backward_propagation_with_dropout()`"
# 
# In each part, you will run this model with the correct inputs so that it calls the
# functions you've implemented. Take a look at the code below to familiarize yourself
# with the model.

# In[3]:设计模型,并可以根据输入lambd和keep_prob来判断

def model(X, Y, learning_rate = 0.3, num_iterations = 30000, print_cost = True, lambd = 0, keep_prob = 1):
    """
    Implements a three-layer neural network: LINEAR->RELU->LINEAR->RELU->LINEAR->SIGMOID.
    
    Arguments:
    X -- input data, of shape (input size, number of examples)
    Y -- true "label" vector (1 for blue dot / 0 for red dot), of shape (output size, number of examples)
    learning_rate -- learning rate of the optimization
    num_iterations -- number of iterations of the optimization loop
    print_cost -- If True, print the cost every 10000 iterations
    lambd -- regularization hyperparameter, scalar
    keep_prob - probability of keeping a neuron active during drop-out, scalar.
    
    Returns:
    parameters -- parameters learned by the model. They can then be used to predict.
    """
        
    grads = {}
    costs = []                            # to keep track of the cost
    m = X.shape[1]                        # number of examples
    layers_dims = [X.shape[0], 20, 3, 1]
    
    # Initialize parameters dictionary.
    parameters = initialize_parameters(layers_dims)

    # Loop (gradient descent)

    for i in range(0, num_iterations):

        # Forward propagation: LINEAR -> RELU -> LINEAR -> RELU -> LINEAR -> SIGMOID.
        if keep_prob == 1:
            a3, cache = forward_propagation(X, parameters)
        elif keep_prob <
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值