1-2神经网络编程基础

本文介绍了深度学习中神经网络编程的基础知识,包括常用库如numpy、h5py,魔法函数,squeeze和reshape函数的应用,数据集标准化过程,预处理步骤,算法框架,numpy.zeros函数,isinstance函数,以及matplotlib.pyplot的使用。讨论了向量和矩阵操作,如[:, n]选取特定元素,以及数据预处理和模型构建的重要性。" 81364057,7524584,Java数据库编程:创建图书管理数据库模型,"['数据库设计', 'SQL语法', 'Java数据库连接', '数据建模']
摘要由CSDN通过智能技术生成

1:库

numpy
h5py
matplotlib
PIL、scipy

2:魔法函数

IPython组内预先定义好的函数,通过命令行语句形式访问

#加载包和数据
import numpy as np		#python科学计算中最重要的库
import matplotlib.pyplot as plt			#python画图的库
import h5py			#python与H5文件交互的库
import scipy		#python科学计算相关库
from PIL import Image		#python图像相关库
from scipy import ndimage		#ndimage为数据文件夹
from lr_utils import load_dataset		#???
%matplotlib inline		#IPython编译器内嵌绘图,可以省掉plt.show()
#加载数据集
#说明:带orig的后缀为图像数据集(train、test),后续将处理它们。不带orig后缀的为返回值,且不需要进行处理
train_set_x_orig
train_set_y
test_set_x_orig
test_Set_y
classes=load_dataset()

3:squeeze函数

A=squeeze(B)
如果B存在单行单列的维度,去掉即为B,反之则A、B元素相同

4:[ : , n ]

[: , n]取所有行第n个(第n+1列)元素
[n , :]取第一维下标为n(第n+1行)的元素所有值

5:classes函数

???

#一个图片例子
index = 3		#可以修改
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."
#运行结果:y = [0], it's a 'non-cat' picture.

6:BUG

深度学习中代码错误中最常见的就是:向量或矩阵维数对不上,写代码要多注意检查

7:shape函数

X.shape[n]——>X的第n维数据个数
X.shape——>X的维数信息(

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))		#209
print ("Number of testing examples: m_test = " + str(m_test))		#50
print ("Height/Width of each image: num_px = " + str(num_px))		#64
print ("Each image is of size: (" + str(num_px) + ", " + str(num_px) + ", 3)")		#(64,64,3)
print ("train_set_x shape: " + str(train_set_x_orig.shape))		#(209,64,64,3)
print ("train_set_y shape: " + str(train_set_y.shape))		#(1,209)
print ("test_set_x shape: " + str(test_set_x_orig.shape))		#(50,64,64,3)
print ("test_set_y shape: " + str(test_set_y.shape))		#(1,50)

8:reshape函数

X.reshape(X.shape[0],-1)重塑矩形形状,化为二维数据

train_set_x_flatten = train_set_x_orig.reshape(train_set_x_orig.shape[0],-1).T		#重塑矩形形状后转置,train_set_x_orig.shape[0]=209,身下的一维数据为其他维元素相乘的结果
test_set_x_flatten = test_set_x_orig.reshape(test_set_x_orig.shape[0],-1).T
print ("train_set_x_flatten shape: " + str(train_set_x_flatten.shape))		#(12288, 209)
print ("test_set_x_flatten shape: " + str(test_set_x_flatten.shape))		#(12288, 50)
print ("sanity check after reshaping: " + str(train_set_x_flatten[0:5,0])
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值