吴恩达深度学习L1W2逻辑回归神经网络识别猫

本文介绍了如何用神经网络思想实现Logistic回归分类器,以识别猫和非猫的图像。首先,数据被预处理,包括重塑和标准化。接着,通过初始化模型参数、前向传播和反向传播进行学习。最后,评估模型在训练集和测试集上的表现,展示了一个简单的图像识别算法的工作原理。
摘要由CSDN通过智能技术生成

用神经网络思想实现Logistic回归分类器用来识别猫

1-问题描述
问题说明:你将获得一个包含以下内容的数据集("data.h5"):


- 标记为cat(y = 1)或非cat(y = 0)的m_train训练图像集
- 标记为cat或non-cat的m_test测试图像集
- 图像维度为(num_px,num_px,3),其中3表示3个通道(RGB)。 因此,每个图像都是正方形(高度= num_px)和(宽度= num_px)。

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

 2-装包
- numpy 是Python科学计算的基本包。
- h5py是一个常用的包,可以处理存储为H5文件格式的数据集。
- matplotlib是一个著名的Python图形库。

import numpy as np
import matplotlib.pyplot as plt
import matplotlib
import h5py
import scipy
from PIL import Image
from scipy import ndimage
from lr_utils import load_dataset

%matplotlib inline

通过运行以下代码来加载数据。

#加载数据(猫/非猫)
train_set_x_orig, train_set_y, test_set_x_orig, test_set_y, classes = load_dataset()

我们在图像数据集(训练和测试)的末尾添加了"_orig",以便对其进行预处理。 预处理后,我们将得到train_set_x和test_set_x(标签train_set_y和test_set_y不需要任何预处理)。


train_set_x_orig和test_set_x_orig的每一行都是代表图像的数组。 可以通过运行以下代码来可视化示例。 可以随意更改index值并重新运行以查看其他图像。

# 显示图片例子
index = 25
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.")
#squeeze函数用于去掉维度为1的条目

“ train_set_x_orig”是一个维度为(m_train,num_px,num_px,3)的numpy数组。 例如,你可以通过编写“ train_set_x_orig.shape [0]”来访问“ m_train”。

m_train = train_set_x_orig.shape[0]
m_test = test_set_x_orig.shape[0]
num_px = train_set_x_orig.shape[1]

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))

向量化

为了方便起见,你现在应该以维度(num_px ∗num_px ∗3, 1)的numpy数组重塑维度(num_px,num_px,3)的图像。 此后,我们的训练(和测试)数据集是一个numpy数组,其中每列代表一个展平的图像。 应该有m_train(和m_test)列。

当你想将维度为(a,b,c,d)的矩阵X展平为形状为(b∗c∗d, a)的矩阵X_flatten时的一个技巧是:
X_flatten = X.reshape(X.shape [0],-1).T     # 其中X.T是X的转置矩阵

train_set_x
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值