Logistic Regression with a Neural Network mindset-2

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

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

练习:查找值:

-m_train (训练示例数)

-m_test (测试示例数)
-num_px (= 高度 = 训练图像的宽度)
请记住, train_set_x_orig是一个 numpy 数组的形状 (m_train, num_px, num_px, 3)。

例如, 您可以通过train_set_x_orig.shape[0]获取m_train

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

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

为了方便起见, 现在应该在形状 numpy 阵列中重塑形状 (num_px、num_px、3) 的图像 (num_px**num_px**3, 1)。在此之后, 我们的训练 (和测试) 数据集是一个 numpy 数组, 其中每个列表示一个扁平图像。应该有 m_train (分别为 m_test) 列

练习:重塑训练和测试数据集, 使尺寸 (num_px, num_px, 3) 的图像被拼合成单一的形状向量 (num_px**num_px**3, 1)。

如果要将形状 (a、b、c、d) 的矩阵 X 拼合为形状的矩阵 X_flatten (b**C**d、a) 时使用:

X_flatten = X.reshape(X.shape[0], -1).T      # X.T是X的转置
tips:
1.reshape里面的参数-1作用是通过确定好其他参数的值,自动计算出-1出应当对应的值,例如一个有64个元素的数组,reshape(4,-1),那么生成的数组应当是4行16列
train_set_x_flatten=train_set_x_orig.reshape(m_train,-1).T
test_set_x_flatten=test_set_x_orig.reshape(m_test,-1).T

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)
#12288=64*64*3,一幅图像大小为64*64,且每个像素位RGB(3)
#训练集共有209张图片,每一列代表一张图片,共209列
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]

 要表示颜色图像, 必须为每个像素指定红色、绿色和蓝色通道 (RGB), 因此像素值实际上是三个数字 (从0到255不等) 的向量。

机器学习中一个常见的预处理步骤是对数据集进行中心和标准化, 这意味着您从每个示例中减整个 numpy 数组的平均值, 然后按整个 numpy 数组的标准偏差划分每个示例。但是对于图片数据集来说, 它更简单、更方便, 并且几乎可以将数据集的每一行除以 255 (像素通道的最大值)。

让我们标准化我们的数据集。

train_set_x=train_set_x_flatten/255.0
test_set_x=test_set_x_flatten/255.0
tips:
1.注意用python2的同学,这里一定是“255.0”或者“255.”,否则精度会有损失,影响结果

 您需要记住的内容:

预处理新数据集的常用步骤是:
-计算问题的尺寸和形状 (m_train、m_test、num_px、...)
-重新调整数据集, 使每个示例现在都是大小的向量 (num_px * num_px * 3, 1)

-"标准化" 的数据

猜你喜欢

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