深度学习系列作业1----by 吴恩达
import numpy as np
import matplotlib.pyplot as plt
import h5py # 是与数据集(存储在H5文件中的数据集)交互的常用软件包
import scipy.misc
import scipy.ndimage
from lr_utils import load_dataset
import matplotlib
"""使用logistic regression进行猫的图片分类"""
# 注意:向量一般字母大写;标量一般字母小写
################下面是数据加载与预处理过程###############
train_set_x_orig, train_set_y, test_set_x_orig, test_set_y, classes = load_dataset()
index = 1
# 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.")
print('训练集样本维度信息train_set_x_orig:', train_set_x_orig.shape) # 209张图片作为训练样本
print('训练标签维度信息train_set_y:', train_set_y.shape)
print('测试集样本维度信息test_set_x_orig:', test_set_x_orig.shape) # 50张图片作为测试样本
print('测试标签维度信息test_set_y:', test_set_y.shape)
m_train = train_set_x_orig.shape[0]
m_test = test_set_x_orig.shape[0]
num_px = train_set_x_orig.shape[1] # 每张图片的高和宽是64*64
print("训练样本数量:{}".format(m_train))
print("测试样本数量:{}".format(m_test))
print("每张彩色图片的宽高信息:({} {})".format(num_px, num_px))
# 使用下面的命令将每张图片的(64,64,3)的像素值信息,转化为一个特征列向量x:(64*64*3,1)
train_set_x_flatten = train_set_x_orig.reshape(train_set_x_orig.shape[0], -1).T
print('train_set_x_orig转化后的维度信息train_set_x_flatten:', train_set_x_flatten.shape)
test_set_x_flatten = test_set_x_orig.reshape(test_set_x_orig.shape[0], -1).T
print('test_set_x_orig转化后的维度信息test_set_x_flatten:', test_set_x_flatten.shape)
print('第一个样本的前5个像素值:', train_set_x_flatten[0:5, 0])
# 数据预处理(归一化操作):对数据集进行中心化和标准化:每个样本减去整个numpy数组的平均值,然后将每个样本除以整个numpy数组的标准偏差
# 对于图片数据集来说,可以将数据集的每一行除以255(像素通道的最大值)。
train_set_x = train_set_x_flatten / 255
test_set_x = test_set_x_flatten / 255
#################下面是建立神经网络的过程#####################
def advanced_sigmoid(z):
result = 1 / (1 + np.exp(-z))
return result
# z = np.array([0, 2])
# print(advanced_sigmoid(z))
# 参数初始化:将w参数初始化为0向量
def initialize_parameters(dim):
w = np.zeros((dim, 1))
b = 0
assert (w.shape == (dim, 1)) # 加入断言语句,如果为真,不做任何事情;如果它为假,会抛出异常
assert (isinstance(b, float) or isinstance(b, int))
return w, b
dim = 2
w, b = initialize_parameters(dim)
# print('w = \n', w)
# print('b = ', b)
# 通过前向传播来计算损失cost,通过反向传播来计算梯度gradient
def propagate(w, b, X, Y):
m = X.shape[1]
A = advanced_sigmoid(np.dot(w.T, X) + b)
cost = -1 / m * np.sum(Y * np.log(A) + (1 - Y) * np.log(1 - A))
dw = 1 / m * np.dot(X, (A - Y).T)
db = 1 / m * np.sum(A - Y)
assert(dw.shape == w.shape)
assert(db.dtype == float)
cost = np.squeeze(cost) # 将shape中维度为1的去掉
assert (cost.shape == ())
grads = {"dw": dw, "db": db}
return grads, cost
w, b, X, Y = np.array([[1], [2]]), 2, np.array([[1, 2], [3, 4]]), np.array([[1, 0]])
grads, cost = propagate(w, b, X, Y)
print("dw = \n", grads['dw'])
print("db = ", grads['db'])
print("cost = ", cost)
# 梯度下降法优化参数w, b
def optimize(w, b, X, Y, iterations, learning_rate, print_cost=False):
costs = []
for i in range(iterations):
grads, cost = propagate(w, b, X, Y)
dw = grads["dw"]
db = grads["db"]
# 更新参数操作
w = w - learning_rate * dw
b = b - learning_rate * db
if i % 100 == 0:
costs.append(cost)
if print_cost and i % 100 == 0:
print("经过%d次迭代后,cost的值是%f" % (i, cost))
params = {"w": w, "b": b}
grads = {"dw": dw, "db": db}
return params, grads, costs
params, grads, costs = optimize(w, b, X, Y, iterations=100, learning_rate=0.009, print_cost=False)
print("w = \n", params["w"])
print("b = ", params["b"])
print("dw = \n", grads["dw"])
print("db = ", grads["db"])
print("costs = ", costs)
# 预测部分
def predict(w, b, X):
m = X.shape[1]
y_prediction = np.zeros((1, m))
w = w.reshape(X.shape[0], 1)
A = advanced_sigmoid(np.dot(w.T, X) + b)
for i in range(A.shape[1]):
if A[0, i] <= 0.5:
y_prediction[0, i] = 0
else:
y_prediction[0, i] = 1
assert (y_prediction.shape == (1, m))
return y_prediction
print("y_prediction:", predict(w, b, X))
################下面将上面各个部分的函数组合成一个完整的模型#################
def model(X_train, Y_train, X_test, Y_test, iterations=1000, learning_rate=0.5, print_cost=False):
w, b = initialize_parameters(X_train.shape[0])
params, grads, costs = optimize(w, b, X_train, Y_train, iterations, learning_rate, print_cost)
w = params["w"]
b = params["b"]
Y_prediction_test = predict(w, b, X_test)
Y_prediction_train = predict(w, b, X_train)
print("训练集上的准确率:{}%".format(100 - np.mean(np.abs(Y_prediction_train - Y_train)) * 100))
print("测试集上的准确率:{}%".format(100 - np.mean(np.abs(Y_prediction_test - Y_test)) * 100))
d = {"costs": costs,
"Y_prediction_test": Y_prediction_test,
"Y_prediction_train": Y_prediction_train,
"w": w,
"b": b,
"learning_rate": learning_rate,
"iterations": iterations}
return d
d = model(train_set_x, train_set_y, test_set_x, test_set_y, iterations=1000, learning_rate=0.005, print_cost=True)
# 绘制学习曲线
costs = np.squeeze(d['costs'])
plt.plot(costs)
plt.ylabel('cost')
plt.xlabel('迭代次数(每100次)', fontproperties='FangSong', fontsize=18)
plt.title("Learning rate ={}".format(d["learning_rate"]))
plt.show()
# 设置不同的学习率进行对比,从而反应出学习率不同对梯度下降算法的影响
learning_rates = [0.01, 0.001, 0.0001]
models = {}
for i in learning_rates:
print("当前的学习率是:{}".format(i))
models[i] = model(train_set_x, train_set_y, test_set_x, test_set_y, iterations=1500, learning_rate=i, print_cost=False)
print('-------------------分隔线-----------------------')
for i in learning_rates:
plt.plot(np.squeeze(models[i]["costs"]), label=str(models[i]["learning_rate"]))
plt.ylabel('cost')
plt.xlabel('迭代次数', fontproperties='FangSong', fontsize=18)
legend = plt.legend(loc='upper center', shadow=True)
frame = legend.get_frame()
frame.set_facecolor('0.90')
plt.title("不同学习率对cost曲线的影响", fontproperties='FangSong', fontsize=18)
plt.show()
# 使用自己的图片来进行测试
my_img = 'la_defense.jpg'
fname = "images/" + my_img
image = np.array(scipy.ndimage.imread(fname, flatten=False))
my_image = scipy.misc.imresize(image, size=(num_px,num_px)).reshape((1, num_px*num_px*3)).T
my_predicted_image = predict(d["w"], d["b"], my_image)
plt.imshow(image)
print("y = " + str(np.squeeze(my_predicted_image)) + ", your algorithm predicts a \"" + classes[int(np.squeeze(my_predicted_image))].decode("utf-8") + "\" picture.")
深度学习系列作业1----by 吴恩达相关推荐
- 【深度学习下一大突破】吴恩达对话 Hinton、Bengio、Goodfellow(视频)
[深度学习下一大突破]吴恩达对话 Hinton.Bengio.Goodfellow(视频) [日期:2017-08-11] 来源:新智元 作者: [字体:大 中 小] [新智元导读]吴恩达深度 ...
- 撒花!斯坦福深度学习最新视频发布,吴恩达主讲!
点击上方"AI有道",选择"置顶"公众号 重磅干货,第一时间送达 就在 3 月 21 日,由吴恩达开设的斯坦福深度学习课程 CS230 课程视频发布到了网上.视 ...
- 深度学习入门首推资料--吴恩达深度学习全程笔记分享
本文首发于微信公众号"StrongerTang",可打开微信搜一搜,或扫描文末二维码,关注查看更多文章. 原文链接:(https://mp.weixin.qq.com/s?__bi ...
- 深度学习笔记目录大全(吴恩达)--终于有翻译版本了
第一门课 神经网络和深度学习(Neural Networks and Deep Learning) 第一周:深度学习引言(Introduction to Deep Learning) 1.1 欢迎(W ...
- 大总结-深度学习全五课-Stanford吴恩达教授
大总结 深度学习符号 此笔记中使用的数学符号参考自<深度学习>和 Deep learning specialization 常用的定义 原版符号定义中, x(i)x^{(i)}x(i) 与 ...
- “AI界漫威” 深度学习超级英雄联盟漫画:吴恩达,李飞飞…
ML的传奇英雄壮举的惊险故事. https://github.com/acmi-lab/superheroes-deep-learning 喜欢您就点个在看!
- 第九章 神经网络学习-机器学习老师板书-斯坦福吴恩达教授
第九章 神经网络学习 9.1 代价函数 9.2 反向传播算法 9.3 反向传播直观理解 9.4 使用注意:展开参数 9.5 梯度检测 9.6 随机初始化 9.7 聚在一起 9.8 反向传播例子:自动驾 ...
- 免费分享全套吴恩达深度学习课程笔记以及编程作业集合
分享吴恩达深度学习全套 笔记 笔记来源于吴恩达老师课程中口述翻译,并包含板书.可以取代看视频,做到更快速学习. (部分目录) (部分目录) (板书) 编程作业 扫描二维码后台回复"0&quo ...
- 资源 | 吴恩达斯坦福CS230深度学习课程全套资料放出(附下载)
原文链接:点击打开链接 摘要: 在人工智能领域,深度学习的重要性不言而喻.各大高校纷纷推出具有自己特色的课程,斯坦福大学也不例外. 在deeplearning.ai深度学习专项课程之后,吴恩达在斯坦福 ...
最新文章
- 深度优先搜索_0基础学算法 搜索篇第一讲 深度优先搜索
- SAP WM 确认TO单的时候不能修改目的地STORAGE BIN
- SSH连接服务器报错(WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED)的解决方案
- UWP开发入门(十一)——Attached Property的简单应用
- Andros系列排爆机器人原理_中国製造2025系列M之二:高档数控机床和机器人
- Java判断一个数是不是快乐数
- 从 SGD 到 Adam —— 深度学习优化算法概览(一) 重点
- 腾讯测试岗位的面试题合集,请查收
- Linux:文件系统和数据资料
- 分时线的9代表什么_一位深藏不露的股神告诉你:为什么要打板?
- 《精力管理》读书笔记——第二部分
- DataFun: 微信NLP算法微服务治理
- 百度TTS,支持离线环境下使用
- C语言编译器(C语言编程软件)
- java 随机发牌_java实现扑克牌发牌器
- UE4中英文语言切换的三种方式(当然也可以多种语言)
- 数据仓库之ODS层设计概要
- 关于电脑黑屏后st-link下载不了的问题 Error: Flash Download failed - Target DLL has been cancelled
- 电脑新机测试软件,怎么检测新买电脑是否是新机
- 【Spring】Spring SpringMVC MyBatis 整合 applicationContext.xml文件笔记