deeplearning.ai——构建一个LR分类器来识别猫

Logistic Regression with a Neural Network mindset

目录

1 - Packages

2 - Overview of the Problem set

3 - General Architecture of the learning algorithm

4 - Building the parts of our algorithm

4.1 - Helper functions

4.2 - Initializing parameters

4.3 - Forward and Backward propagation

5 - Merge all functions into a model

6 - Further analysis (optional/ungraded exercise)

7 - Test with your own image (optional/ungraded exercise)


这节作业会学到:

  • 构建学习算法的通用架构,包括:初始化参数、计算损失函数及其梯度、使用优化算法(梯度下降)
  • 按正确的顺序将上述所有三个函数组合在主模型函数中

 

1 - Packages

首先,运行下面的单元来导入在这个作业中需要的所有包:

  • numpy是使用python进行科学计算的基础包。
  • h5py是一个与存储在h5文件中的与数据集交互的通用包。
  • Matplotlib是一个著名的用Python绘制图形的库。
  • 这里使用pil和scipy并用你自己的图片测试你的模型。
import numpy as np
import matplotlib.pyplot as plt
import h5py
import scipy
from PIL import Image
from scipy import ndimage
from lr_utils import load_dataset

%matplotlib inline

 

2 - Overview of the Problem set

你的数据集(“data.h5”)包含:

  • 训练集,m_train个图片,标记了猫(y=1)或非猫(y=0)。
  • 测试集,m_test个图片的标记,标记为猫或非猫。
  • 每张图片的shape都为(num_px, num_px, 3),其中3表示3通道(RGB)。因此,每张图都是方形的,即长宽都为num_px。

构建一个简单的图像识别算法,可以正确地将图片分类为猫或非猫。

运行以下代码加载数据:

# Loading the data (cat/non-cat)
train_set_x_orig, train_set_y, test_set_x_orig, test_set_y, classes = load_dataset()
(209, 64, 64, 3)
(1, 209)
(50, 64, 64, 3)
(1, 50)
[b'non-cat' b'cat']

我们在图像数据集(训练和测试)的末尾添加了“_orig”,因为我们要对它们进行预处理。在预处理之后,我们将以train_set_x和test_set_x结束(标签train_set_y和test_set_y不需要任何预处理)。

来看一个图片的例子:

# Example of a picture
index = 99
plt.imshow(train_set_x_orig[index])
print ("y = " + str(train_set_y[:, index]) + ", it's a '" + classes[np.squeeze(train_set_y[:, index])].decode("utf-8") +  "' picture.")
y = [0], it's a 'non-cat' picture.

练习:得到下列值:

  • m_train(训练样本的数量)
  • m_test(测试样本的数量)
  • num_px(训练图片的长宽)

train_set_x_orig是一个形状为(m_train, num_px, num_px, 3)的numpy数组,例如,你可以通过train_set_x_orig.shape[0]得到m_train的值。

### START CODE HERE ### (≈ 3 lines of code)
m_train = train_set_x_orig.shape[0]
m_test = test_set_x_orig.shape[0]
num_px = train_set_x_orig.shape[2]
### END CODE HERE ###

print ("Number of training examples: m_train = " + str(m_train))
print ("Number of testing examples: m_test = " + str(m_test))
print ("Height/Width of each image: num_px = " + str(num_px))
print ("Each image is of size: (" + str(num_px) + ", " + str(num_px) + ", 3)")
print ("train_set_x shape: " + str(train_set_x_orig.shape))
print ("train_set_y shape: " + str(train_set_y.shape))
print ("test_set_x shape: " + str(test_set_x_orig.shape))
print ("test_set_y shape: " + str(test_set_y.shape))
Number of training examples: m_train = 209
Number of testing examples: m_test = 50
Height/Width of each image: num_px = 64
Each image is of size: (64, 64, 3)
train_set_x shape: (209, 64, 64, 3)
train_set_y shape: (1, 209)
test_set_x shape: (50, 64, 64, 3)
test_set_y shape: (1, 50)

为了方便,可以reshape图片的形状(num_px, num_px, 3)变为(num_px*num_px*3, 1),之后,训练集和测试集就是一个每个列向量都是一个扁平图像的numpy数组了,且有m_train(训练集是m_test)个列向量。

练习:对训练集和测试集数据进行reshape,使其形状(num_px, num_px, 3)扁平化为一个向量(num

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值