Logistic Regression with a Neural Network mindset-1

版权为吴恩达老师所有,参考Koala_Tree的博客,部分根据自己实践添加

使用bing翻译,部分手工翻译

Logistic 回归使用神经网络模式

您将学习:
-构建学习算法的一般体系结构, 包括
:-初始化参数
-使用优化算法 (渐变下降)

计算成本函数及其梯度-以正确的顺序收集上面的所有三个函数到一个主模型函数中。 

1-包

首先, 让我们运行下面的单元格, 导入您在分配期间需要的所有包。
- numpy是使用 Python 进行科学计算的基本包。
- h5py是与存储在 H5 文件上的数据集进行交互的常用包。
- matplotlib是在 Python 中绘制图形的著名库。
- pillow和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
tips:
1.不能含有中文路径
2.PIL不支持64位python,改用pillow
3.以上均可以在cmd窗口中使用“pip install (包的名称)”下载

2-问题集合的概要

问题陈述: 给出一个数据集 ("data. h5"), 其中包含:

-一组标记为 cat (y=1) 或非 cat (y=0)的 m_train 图像的训练集

-一个标记为 cat 或非 cat的 m_test 图像的测试集, 每个图像都是形状 (num_px, num_px, 3), 其中3是3通道 (RGB)。因此, 每个图像是正方形 (高度 = num_px) 和 (宽度 = 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()

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

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

#Example of a picture
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.")
tips:
1.如果出现numpy.dtype已发生改变与h5py发生冲突的错误提示时,请使用旧版numpy,我当时使用的时1.15.0版本就发生了错误,按照如下步骤(在命令行中输入):
如果已经安装numpy
pip uninstall numpy
安装1.14.5版本
pip install numpy==1.14.5
如果安装失败,需要用管理员权限
pip install --user numpy==1.14.5
2.注意两个h5文件的打开,lr_utils.py中的文件路径我改为了完整的路径,否则会出现如下错误
Traceback (most recent call last):
  File "e:\study\deep learning\code\test.py", line 3, in <module>
    h=h5py.File("E:\study\deep learning\code\train_catvnoncat.h5", "r")
  File "E:\tools\python2.7.15\lib\site-packages\h5py\_hl\files.py", line 312, in __init__
    fid = make_fid(name, mode, userblock_size, fapl, swmr=swmr)
  File "E:\tools\python2.7.15\lib\site-packages\h5py\_hl\files.py", line 142, in make_fid
    fid = h5f.open(name, flags, fapl=fapl)
  File "h5py\_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
  File "h5py\_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
  File "h5py\h5f.pyx", line 78, in h5py.h5f.open
IOError: Unable to open file (unable to open file: name = 'E:\study\deeplearning\code       rain_catvnoncat.h5', errno = 22, error message = 'Invalid argument', flags = 0, o_flags = 0)
#lr_utils.py

import numpy as np
import h5py
      
def load_dataset():
    train_dataset = h5py.File(r"(直接从地址栏复制)train_catvnoncat.h5", "r")
    train_set_x_orig = np.array(train_dataset["train_set_x"][:]) # your train set features
    train_set_y_orig = np.array(train_dataset["train_set_y"][:]) # your train set labels

    test_dataset = h5py.File(r"(直接从地址栏复制)test_catvnoncat.h5", "r")
    test_set_x_orig = np.array(test_dataset["test_set_x"][:]) # your test set features
    test_set_y_orig = np.array(test_dataset["test_set_y"][:]) # your test set labels

    classes = np.array(test_dataset["list_classes"][:]) # the list of classes
    
    train_set_y_orig = train_set_y_orig.reshape((1, train_set_y_orig.shape[0]))
    test_set_y_orig = test_set_y_orig.reshape((1, test_set_y_orig.shape[0]))
    
    return train_set_x_orig, train_set_y_orig, test_set_x_orig, test_set_y_orig, classes
# encoding:utf-8
import numpy as np
import matplotlib.pyplot as plt
import h5py
import scipy
import pylab
from PIL import Image
from scipy import ndimage
from lr_utils import load_dataset

#Loading the data (cat/non-cat)
train_set_x_orig,train_set_y,test_set_x_orig,test_set_y,classes=load_dataset()

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

tips:
1.输出图像时只有plt.imshow(train_set_x_orig[index])这一句可能会无法显示图像,需要import pylab和在后面添加上pylab.show()

这是显示的图片:

需要的数据集可以通过这里下载https://pan.baidu.com/s/18SlHoGzW0qupc1OQZ9pSrg

猜你喜欢

转载自blog.csdn.net/smoggyxhdz/article/details/81427731