《神经网络和深度学习》之神经网络基础(第二周)课后作业——神经网络思维的逻辑回归

欢迎来到你的第一个编程作业,在这次作业中你将会用逻辑回归去识别一个猫。并且在这次作业中你将会用神经网络的思维去一步一步的去解决这个问题和磨练你的深度学习的直觉。

说明:

  • 在你的代码中不能使用for或while循环,除非说明明确要你这么做。

你将会学习到:

1.建立一个学习算法的一般结构,包括

  • 初始化参数
  • 计算代价函数和它的梯度
  • 使用最优化算法(梯度下降)

2.用正确的顺序将上面三个函数集合到一个主函数模型里。

1 程序包

h5py 是与存储在一个H5文件上的数据集交互的公共包。

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 问题集的概述

问题集合”data.h5”包括

  • 被标记的训练集m_train
  • 未标记的测试集m_test
  • 每个图像由(num_px, num_px, 3)构成,对应于(height ,width,channels)
# Loading the data (cat/non-cat)
train_set_x_orig, train_set_y, test_set_x_orig, test_set_y, classes = load_dataset()

我们在图像集前加了 “_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.")

问题:找出下面的值

  • m_train(训练样例的个数)
  • m_test(测试样例的个数)
  • num_px (= height = width 的训练图片)
### 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[1]
### 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: (209L, 64L, 64L, 3L)
train_set_y shape: (1L, 209L)
test_set_x shape: (50L, 64L, 64L, 3L)
test_set_y shape: (1L, 50L)

问题: 重构训练集和测试集使得一个矩阵图像扁平成一个向量。

X_flatten = X.reshape(X.shape[0], -1).T # X.T is the transpose of X

# Reshape the training and test examples

### START CODE HERE ### (≈ 2 lines of code)
train_set_x_flatten = train_set_x_orig.reshape(train_set_x_orig.shape[0], -1).T
test_set_x_flatten = test_set_x_orig.reshape(test_set_x_orig.shape[0], -1).T
### END CODE HERE ###

print ("train_set_x_flatten shape: " + str(train_set_x_flatten.shape))
print ("train_set_y shape: " + str(train_set_y.shape))
print ("test_set_x_flatten shape: " + str(test_set_x_flatten.shape))
print ("test_set_y shape: " + str(test_set_y.shape))
print ("sanity check after reshaping: " + str(train_set_x_flatten[0:5,0]))

输出:train_set_x_flatten shape: (12288L, 209L)
train_set_y shape: (1L, 209L)
test_set_x_flatten shape: (12288L, 50L)
test_set_y shape: (1L, 50L)
sanity check after reshaping: [17 31 56 22 33]

归一化数据集

train_set_x = train_set_x_flatten/255.
test_set_x = test_set<
  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值