Stanford CS230深度学习(一)

article/2025/10/5 11:09:36

斯坦福CS230可以作为深度学习的入门课,最近我也在跟着看视频、完成编程作业。首先列出使用的资源链接,然后给出第一课的理解和编程作业的代码。

所有资料如下:

一、课程连接:

  1. b站课堂讲授版:Stanford CS230(吴恩达 深度学习 Deep Learning | Autumn 2018)(中英双字幕)
    (一共10个视频,每周一个,但是每个视频对应下面的配套视频较多)
  2. Coursera配套视频:Neural Networks and Deep Learning
    (注:内容和下面是一样的,只不过在每个讲解视频完之后多了一些小quiz,但是视频可能播放不了,需要设置root权限,嫌麻烦请转3)
  3. b站配套视频:【中文字幕】深度学习_吴恩达_DeepLearning.ai

二、官方slide和网络上其他人的笔记:

  • Syllabus(包含课程安排、讲义等)
  • 深度学习笔记目录

三、GitHub上编程作业:

  • 有quiz、没有数据
  • 没有quiz、有数据
  • CSDN上一个很不错的中文作业目录

第一课:用神经网络来看逻辑回归

  • 逻辑回归原本是广义线性模型中用于分类的一种算法,它通过一个非线性变换使得最后的输出值在 [ 0 , 1 ] [0,1] [0,1]之间,这个值可以解释为给出 x x x y y y为正例的概率,即: P ( y = 1 ∣ X ) = σ ( W T X + b ) P(y=1|X)=\sigma(W^TX+b) P(y=1X)=σ(WTX+b)然后设定阈值,例如0.5,即可得到分类结果。
  • 从神经网络的视角来看,输入层即为 X X X,线性变换 W T X + b W^TX+b WTX+b得到隐层,最后经过一个sigmoid激活函数得到输出层。这是神经网络的前向传播过程(Forward Propagation)。
  • 对于这个结果的评价,使用的是对数似然损失(Log-likelihood Loss), 也称交叉熵损失(cross-entropy Loss),对于二分类的情况 L ( y ^ , y ) = − 1 n ∑ i = 1 n [ y i log ⁡ y ^ i + ( 1 − y i ) log ⁡ ( 1 − y ^ i ) ] L(\hat y,y)=-{1\over n}\sum_{i=1}^n\big[y_i\log \hat y_i + (1-y_i)\log (1-\hat y_i)\big] L(y^,y)=n1i=1n[yilogy^i+(1yi)log(1y^i)]
  • 我们的目的是学习到整个网络的参数,也就是这里的 W W W b b b,方法是通过迭代来更新参数使得错误率最小(也就是损失函数最小)。因此需要将每次前向传播得到的结果与标签数据进行比较,得到一个损失,考察参数对这个损失的影响就要通过损失函数对参数的梯度来体现。但是这个梯度直接求导不行,因为本来这个模型就不是一个线性函数,而是一个复合函数,因此需要通过链式法则一层一层求导得到这个导数。直观上理解就是让误差从输出层传到隐层,再从隐层传到输入层,这就是误差的反向传播过程(Bcakward Propagation)。
    在这里插入图片描述

import numpy as np
import matplotlib.pyplot as plt
import h5py
from skimage.transform import resize # 导入数据
def load_dataset():train_dataset = h5py.File('datasets/train_catvnoncat.h5', "r")train_set_x_orig = np.array(train_dataset["train_set_x"][:]) # your train set featurestrain_set_y_orig = np.array(train_dataset["train_set_y"][:]) # your train set labelstest_dataset = h5py.File('datasets/test_catvnoncat.h5', "r")test_set_x_orig = np.array(test_dataset["test_set_x"][:]) # your test set featurestest_set_y_orig = np.array(test_dataset["test_set_y"][:]) # your test set labelsclasses = np.array(test_dataset["list_classes"][:]) # the list of classestrain_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# 得到训练集和测试集
train_set_x_orig, train_set_y, test_set_x_orig, test_set_y, classes = load_dataset()# 图片展示
index = 2
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.")# 数据展开
train_set_x_flatten = train_set_x_orig.reshape((209,-1)).T #(12288, 209)
test_set_x_flatten = test_set_x_orig.reshape((50,-1)).T #(12288, 50)# 标准化
train_set_x = train_set_x_flatten/255
test_set_x = test_set_x_flatten/255# 定义一些重要的函数以供后面调用
def sigmoid(x):s = 1 / (1 + np.exp(-x))return sdef initalize(length_of_features):W = np.zeros((length_of_features,1))b = 0return W, bdef acc(Y, pred_Y):n = Y.shape[1]right = np.sum(Y-pred_Y==0)return right/n# Forward and backward propagation
def propagation(W, b, X, Y):n = X.shape[1]Z = np.dot(W.T, X) + bA = sigmoid(Z)cost = - np.sum(Y * np.log(A) + (1-Y) * np.log(1-A)) / ndW = (1/n) * np.dot(X, (A-Y).T)db = (1/n) * np.sum(A-Y)return dW, db, cost# 测试
w, b, X, Y = np.array([[1],[2]]), 2, np.array([[1,2],[3,4]]), np.array([[1,0]])
# propagation(w, b, X, Y)
# (array([[0.99993216],
#        [1.99980262]]),
# 0.49993523062470574,
# 6.000064773192205)# 通过迭代优化参数
def optimize(W, b, X, Y, num_iterations, learning_rate, verbose=False):costs = []for i in range(num_iterations):dW, db, cost = propagation(W, b, X, Y)W = W - learning_rate * dWb = b - learning_rate * dbcosts.append(cost)if verbose == True and i % 100 == 0:print('Cost after iteration %d: %f' %(i, cost))return W, b, costs# 测试
optimize(w, b, X, Y, num_iterations=100, learning_rate=0.009, verbose=True)
# array([[0.1124579 ],
#         [0.23106775]]),
#  1.5593049248448891,# 输出预测结果
def predict(W, b, X):prob = sigmoid(np.dot(W.T, X) + b)y_pred = np.zeros(prob.shape)for i in range(y_pred.shape[1]):if prob[0,i] > 0.5:y_pred[0,i] = 1return y_pred# predict(w, b, X)
# array([[1., 1.]])# 整合得到最终的模型,后面只需调用这个就行
def model(X_train, Y_train, X_test, Y_test, num_iterations, learning_rate, verbose=False):W, b = initalize(X_train.shape[0])W_final, b_final, costs = optimize(W, b, X_train, Y_train, num_iterations, learning_rate, verbose)pred_train_y = predict(W_final, b_final, X_train)pred_test_y = predict(W_final, b_final, X_test)print('Train accuracy:%f' %acc(Y_train, pred_train_y))print('Test accuracy:%f' %acc(Y_test, pred_test_y))output = {'costs': costs,'W': W_final,'b': b_final,'pred_train_y': pred_train_y,'pred_test_y': pred_test_y}return output# 调用,在这个训练集上训练,然后在测试集上看模型表现
d = model(train_set_x, train_set_y, test_set_x, test_set_y, num_iterations=2000, learning_rate=0.005, verbose=True)# 画出分类错误的样本图案
index = 46
plt.imshow(test_set_x[:,index].reshape((64,64,3)))
print("y = %d means this is a cat, but pred_y = %d" %(test_set_y[0,index],int(d['pred_test_y'][0,index])))# 画出学习曲线
plt.plot(np.squeeze(d['costs']))# 比较不同的学习率的表现
learning_rates = [0.01, 0.001, 0.005]
models = {}
for i in learning_rates:print ("learning rate is: " + str(i))models[str(i)] = model(train_set_x, train_set_y, test_set_x, test_set_y, num_iterations = 1500, learning_rate = i, verbose = False)print ('\n' + "-------------------------------------------------------" + '\n')for i in learning_rates:plt.plot(np.squeeze(models[str(i)]["costs"]), label= str(i))plt.ylabel('cost')
plt.xlabel('iterations')legend = plt.legend(loc='upper right')
frame = legend.get_frame()
frame.set_facecolor('0.90')
plt.show()# 在自己的图片上试试效果
my_image = "myimage2.jpg"
# 读入图片数据
image = plt.imread(my_image) 
plt.imshow(image)
# 调整像素大小
image = resize(image, output_shape=(64,64)).reshape((1, 64*64*3)).T
predicted_image = predict(d['W'], d["b"], image)
# 预测结果以及调整后的图片
plt.imshow(image.reshape(64,64,3))
print(predicted_image)

最后试了几个自己的图片,看看预测效果:

预测正确的:
在这里插入图片描述 在这里插入图片描述
在这里插入图片描述
预测错误的:
在这里插入图片描述


http://chatgpt.dhexx.cn/article/viQSYZKZ.shtml

相关文章

csp-202206

202206 题目一:归一化处理【100分】题目二:寻宝!大冒险!【100分】题目三:角色授权【100分】题目四:光线追踪【15分】 题目一:归一化处理【100分】 水题,直接上 AC代码: …

cs229-1

本文全部参考自https://blog.csdn.net/stdcoutzyx?typeblog,仅作学习使用 文章目录 监督学习supervised learning线性回归局部加权回归LWR,Locally/Loess Weighted Regression最小二乘法的概率解释逻辑斯蒂回归logistic regression感知器算法牛顿方法NewTons Metho…

CS231n_learn

CS231n CS 程序:https://cs.stanford.edu/people/karpathy/convnetjs/demo/cifar10.html CS 课件http://cs231n.stanford.edu/slides/2017/: CS 课程首页:http://cs231n.stanford.edu/ CS 附带教程网页版:https://cs.stanford…

csp-202203

202203 题目一&#xff1a;未初始化警告【100分】题目二&#xff1a;出行计划【100分】题目三&#xff1a;计算资源调度器 【100分】 题目一&#xff1a;未初始化警告【100分】 简单数组操作题 #include<iostream> using namespace std; int n,k; bool ready[10000000]…

【CS231n系列】

Stanford-cs231n课程学习笔记&#xff08;一&#xff09; Stanford课程原版是英文&#xff0c;奈何本人英语菜的一批。原版网站放在下面&#xff0c;xdm可以多多学习。BUT&#xff01; B站up<同济子豪兄>yyds好吧&#xff01;&#xff01;&#xff01; Stanford231n 文章…

斯坦福CS230官方指南:CNN、RNN及使用技巧速查(打印收藏)

向AI转型的程序员都关注了这个号&#x1f447;&#x1f447;&#x1f447; 机器学习AI算法工程 公众号&#xff1a; datayx 作为全球计算机四大名校之一&#xff0c;斯坦福大学的CS230《深度学习》课程一直受到全球计算机学子和从业人员的热烈欢迎。 CS230授课人为全球著名计算…

CS230学习笔记(一)

CS230学习笔记(一) 1.前言 ok&#xff0c;前思后想&#xff0c;左思右想&#xff0c;我还是觉得自己得督促一下自己&#xff0c;所以&#xff0c;我觉得开始更新cs230的笔记&#xff0c;当然&#xff0c;我前面的六篇pytorch学习笔记我是不会放着不管的&#xff0c;后面肯定会…

目标检测(CS230)

内容来自CS230课程。 目录 目标定位&#xff08;Object localization&#xff09; 特征点检测&#xff08;Landmark detection&#xff09; 基于滑动窗口的目标检测算法 滑动窗口的卷积实现 &#xff08;Convolutional implementation of sliding windows&#xff09; 网络中的…

PHP配置环境变量

1.找到“高级系统设置”&#xff08;二选一的方法找到环境变量&#xff09; ① 我的电脑-属性-高级-环境变量 ②win8,10 直接在搜索框搜 “查看高级系统设置”-环境变量 2.找到变量"Path" ①加上 “E:\phpStudy\PHPTutorial\php\php-7.0.12-nts” &#xff08;php.e…

PHPstudy 设置PHP为环境变量

1.首先启动phpstudy点击‘切换版本’查看当前使用环境的php版本 2.在右键点击桌面的phpstudy图标进入文件夹位置 2.点击PHPTutorial->PHP 3.点击你的开发版本的php文件&#xff0c;我们会看到php.exe文件&#xff0c;复制当前文件位置路径 4.右键点击计算机或者我的电脑选择…

windows环境下设置多个PHP版本的环境变量

windows环境下设置多个PHP版本的环境变量 所在位置修改系统变量修改用户变量重启电脑 所在位置 我的电脑->属性->高级系统设置->高级->环境变量 根据图示&#xff0c;找到相应的变量 修改系统变量 环境变量->系统变量->Path 系统变量&#xff1a;把两个…

windows10的PHP环境变量

win10 环境变量配置 如何在命令行运行php文件 1.配置环境变量 2.进入php所在路径 然后输入 php 文件路径/文件名 即可 参考文献&#xff1a; https://blog.csdn.net/QQ2542920524/article/details/78692116

Windows环境下,PHPStudy设置环境变量

win7系统设置环境变量 1、选中计算机&#xff0c;点击 鼠标右键&#xff0c;选择属性 2、选择高级系统设置&#xff0c;打开&#xff0c;打开后选择高级&#xff0c;然后就能看到环境变量 3、打开环境变量&#xff0c;查找Path &#xff0c;选中path&#xff0c;再点击编辑即可…

【PHP】配置环境变量,查看php版本(保姆级图文)

目录 配置环境变量找到php所在的目录&#xff08;有一个php.exe文件&#xff09;环境变量path中添加重启电脑&#xff08;可选&#xff09; 查看php版本&#xff08;检测是否成功配置了php&#xff09;总结 『PHP』分享PHP环境配置到项目实战个人学习笔记。 欢迎关注 『PHP』 系…

windows中设置php环境变量

1.我的电脑-》右键&#xff08;选择我的属性&#xff09; 2.点击高级设置 3.点击环境变量 4.在系统变量中找到Path 点击 5.找到php.exe的文件目录&#xff0c;添加到path中 6.php -v 显示版本&#xff0c;表示成功

Linux有多个php版本的时候指定php版本设置环境变量

最近在安装swoole的时候老是出错&#xff0c;安装完成以后再php-m中能看到swoole扩展已经开启&#xff0c;而在 phpinfo中却看不到。查看了下php.ini的位置发现这两个指向的路径不同。查看了下安装的php有两个版本&#xff0c;一个是自带的在/usr/bin/php&#xff0c;一个是自己…

windows设置php环境变量

1、找到要设置的php版本路径,然后进行复制 2、添加环境变量&#xff08;控制面板->高级系统设置->环境变量->最上方的李硕的系统变量Path->新增两条php路径即可&#xff09; 3、打开cmd输入php-v 即可查看添加的php版本信息 最后问题没有解决的话&#xff0c;或者有…

配置windows系统中 PHP的环境变量

1&#xff0e; 首先到php官网下载php-5.3.6-nts-Win32-VC9-x86.ZIP 解压到电脑硬盘。将文件解压到文件夹php5.3.6 下载地址&#xff1a;http://www.php.net/downloads.php 2&#xff0e; 将解压后的php5.3.6文件夹放到E:\Program Files文件夹下面 3&#xff0e; php目录下的“p…

w11 php 环境变量

PHP 安装 解压完成后 进入目录 php -v 得到版本号就OK&#xff0c;配置环境变量 安装composer 全部下一步就可以了 命令行中 composer 出现composer版本号安装没问题&#xff0c;如果没有从新找下 加入到环境变量中&#xff0c;如果还不行&#xff0c;就重新安装 更新 co…

Windows 系统配置 PHP 环境变量(PhpStudy集成环境)

打开系统的【高级系统设置】&#xff0c;点击【环境变量】。 选择【系统变量】中的【Path】&#xff0c;点击编辑。 点击【新建】&#xff0c;填入当前 PHP 版本 php.exe 所在目录&#xff0c;点击确定。 验证有效性 打开命令行窗口&#xff0c;输入 php -v 查看。 出现 PHP 的…