15Python环境搭建(推荐Anaconda方法)

软件万能安装起点
python官网
Anaconda官网

16Eclipse搭建python环境

软件万能安装起点
python官网
Eclipse官网

17动手完成简单神经网络

import numpy as npdef sigmoid (x,deriv=False):if(deriv==True):return x*(1-x)return 1/(1+np.exp(-x))x=np.array([[0,0,1],[0,1,1],[1,0,1],[1,1,1],[0,0,1]
])
print(x.shape)
y=np.array([[0],[1],[1],[0],[0]
])
print(y.shape)np.random.seed(1)w0=2*np.random.random((3,4))-1
w1=2*np.random.random((4,1))-1for j in range(100000):l0=xl1=sigmoid(np.dot(l0,w0))l2=sigmoid(np.dot(l1,w1))l2_error=y-l2if(j%10000)==0:print('Error'+str(np.mean(np.abs(l2_error))))l2_delta=l2_error*sigmoid(l2,deriv=True)l1_error=l2_delta.dot(w1.T)l1_delta=l1_error*sigmoid(l1,deriv=True)w1 += l1.T.dot(l2_delta)w0 += l0.T.dot(l1_delta)

代码分析

激活函数

def sigmoid (x,deriv=False):'''激活函数:param x::param deriv:标志位,当derive为假时,不计算导数,反之计算,控制传播方向。:return:'''if(deriv==True):# 求导return x*(1-x)return 1/(1+np.exp(-x))

激活函数参见:机器学习——01、机器学习的数学基础1 - 数学分析——Sigmoid/Logistic函数的引入

定义input和lable值

x=np.array([[0,0,1],[0,1,1],[1,0,1],[1,1,1],[0,0,1]
])
y=np.array([[0],[1],[1],[0],[0]
])

可以看一下x和y的维度:

print(x.shape)
print(y.shape)

指定随机种子

np.random.seed(1)

保证数据一致,方便对比结果。

初始化权重值

#定义w0和w1在-1到1之间
w0=2*np.random.random((3,4))-1
#三行对应x的三个特征,四列对应L1层的四个神经元
w1=2*np.random.random((4,1))-1
#四行对应L1的四个神经元,一列对应输出0或1

定义三层神经网络

    l0=x#L0层为输入层#进行梯度传播操作:l1=sigmoid(np.dot(l0,w0))#L0与W0进行矩阵运算得到L1l2=sigmoid(np.dot(l1,w1))#L1与W1进行矩阵运算得到L2

根据误差更新权重

    l2_error=y-l2#预测值与真实值之间的差异if(j%10000)==0:print('Error'+str(np.mean(np.abs(l2_error))))#打印误差值l2_delta=l2_error*sigmoid(l2,deriv=True)#反向传播求导,差异值越大,需要更新的越大l1_error=l2_delta.dot(w1.T)#w1进行转置之后与l2_delta进行矩阵运算l1_delta=l1_error*sigmoid(l1,deriv=True)#更新w1和w2w1 += l1.T.dot(l2_delta)w0 += l0.T.dot(l1_delta)

18感受神经网络的强大

假设有如下数据要将其分类

代码

import numpy as np
import matplotlib.pyplot as plt#ubuntu 16.04 sudo pip instal matplotlibplt.rcParams['figure.figsize'] = (10.0, 8.0) # set default size of plots
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'np.random.seed(0)
N = 100 # number of points per class
D = 2 # dimensionality
K = 3 # number of classes
X = np.zeros((N*K,D))
y = np.zeros(N*K, dtype='uint8')
for j in range(K):ix = range(N*j,N*(j+1))r = np.linspace(0.0,1,N) # radiust = np.linspace(j*4,(j+1)*4,N) + np.random.randn(N)*0.2 # thetaX[ix] = np.c_[r*np.sin(t), r*np.cos(t)]y[ix] = j
fig = plt.figure()
plt.scatter(X[:, 0], X[:, 1], c=y, s=40, cmap=plt.cm.Spectral)
plt.xlim([-1,1])
plt.ylim([-1,1])
plt.show()

传统AI算法

#Train a Linear Classifier
import numpy as np
import matplotlib.pyplot as pltnp.random.seed(0)
N = 100 # number of points per class
D = 2 # dimensionality
K = 3 # number of classes
X = np.zeros((N*K,D))
y = np.zeros(N*K, dtype='uint8')
for j in range(K):ix = range(N*j,N*(j+1))r = np.linspace(0.0,1,N) # radiust = np.linspace(j*4,(j+1)*4,N) + np.random.randn(N)*0.2 # thetaX[ix] = np.c_[r*np.sin(t), r*np.cos(t)]y[ix] = jW = 0.01 * np.random.randn(D,K)
b = np.zeros((1,K))# some hyperparameters
step_size = 1e-0
reg = 1e-3 # regularization strength# gradient descent loop
num_examples = X.shape[0]
for i in range(1000):#print X.shape# evaluate class scores, [N x K]scores = np.dot(X, W) + b   #x:300*2 scores:300*3#print scores.shape # compute the class probabilitiesexp_scores = np.exp(scores)probs = exp_scores / np.sum(exp_scores, axis=1, keepdims=True) # [N x K] probs:300*3print (probs.shape )# compute the loss: average cross-entropy loss and regularizationcorect_logprobs = -np.log(probs[range(num_examples),y]) #corect_logprobs:300*1print (corect_logprobs.shape)data_loss = np.sum(corect_logprobs)/num_examplesreg_loss = 0.5*reg*np.sum(W*W)loss = data_loss + reg_lossif i % 100 == 0:print ("iteration %d: loss %f" % (i, loss))# compute the gradient on scoresdscores = probsdscores[range(num_examples),y] -= 1dscores /= num_examples# backpropate the gradient to the parameters (W,b)dW = np.dot(X.T, dscores)db = np.sum(dscores, axis=0, keepdims=True)dW += reg*W # regularization gradient# perform a parameter updateW += -step_size * dWb += -step_size * dbscores = np.dot(X, W) + b
predicted_class = np.argmax(scores, axis=1)
print ('training accuracy: %.2f' % (np.mean(predicted_class == y)))h = 0.02
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),np.arange(y_min, y_max, h))
Z = np.dot(np.c_[xx.ravel(), yy.ravel()], W) + b
Z = np.argmax(Z, axis=1)
Z = Z.reshape(xx.shape)
fig = plt.figure()
plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral, alpha=0.8)
plt.scatter(X[:, 0], X[:, 1], c=y, s=40, cmap=plt.cm.Spectral)
plt.xlim(xx.min(), xx.max())
plt.ylim(yy.min(), yy.max())
plt.show()

神经网络算法

import numpy as np
import matplotlib.pyplot as pltnp.random.seed(0)
N = 100 # number of points per class
D = 2 # dimensionality
K = 3 # number of classes
X = np.zeros((N*K,D))
y = np.zeros(N*K, dtype='uint8')
for j in range(K):ix = range(N*j,N*(j+1))r = np.linspace(0.0,1,N) # radiust = np.linspace(j*4,(j+1)*4,N) + np.random.randn(N)*0.2 # thetaX[ix] = np.c_[r*np.sin(t), r*np.cos(t)]y[ix] = jh = 100 # size of hidden layer
W = 0.01 * np.random.randn(D,h)# x:300*2  2*100
b = np.zeros((1,h))
W2 = 0.01 * np.random.randn(h,K)
b2 = np.zeros((1,K))# some hyperparameters
step_size = 1e-0
reg = 1e-3 # regularization strength# gradient descent loop
num_examples = X.shape[0]
for i in range(2000):# evaluate class scores, [N x K]hidden_layer = np.maximum(0, np.dot(X, W) + b) # note, ReLU activation hidden_layer:300*100#print hidden_layer.shapescores = np.dot(hidden_layer, W2) + b2  #scores:300*3#print scores.shape# compute the class probabilitiesexp_scores = np.exp(scores)probs = exp_scores / np.sum(exp_scores, axis=1, keepdims=True) # [N x K]#print probs.shape# compute the loss: average cross-entropy loss and regularizationcorect_logprobs = -np.log(probs[range(num_examples),y])data_loss = np.sum(corect_logprobs)/num_examplesreg_loss = 0.5*reg*np.sum(W*W) + 0.5*reg*np.sum(W2*W2)loss = data_loss + reg_lossif i % 100 == 0:print ("iteration %d: loss %f" % (i, loss))# compute the gradient on scoresdscores = probsdscores[range(num_examples),y] -= 1dscores /= num_examples# backpropate the gradient to the parameters# first backprop into parameters W2 and b2dW2 = np.dot(hidden_layer.T, dscores)db2 = np.sum(dscores, axis=0, keepdims=True)# next backprop into hidden layerdhidden = np.dot(dscores, W2.T)# backprop the ReLU non-linearitydhidden[hidden_layer <= 0] = 0# finally into W,bdW = np.dot(X.T, dhidden)db = np.sum(dhidden, axis=0, keepdims=True)# add regularization gradient contributiondW2 += reg * W2dW += reg * W# perform a parameter updateW += -step_size * dWb += -step_size * dbW2 += -step_size * dW2b2 += -step_size * db2
hidden_layer = np.maximum(0, np.dot(X, W) + b)
scores = np.dot(hidden_layer, W2) + b2
predicted_class = np.argmax(scores, axis=1)
print ('training accuracy: %.2f' % (np.mean(predicted_class == y)))h = 0.02
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),np.arange(y_min, y_max, h))
Z = np.dot(np.maximum(0, np.dot(np.c_[xx.ravel(), yy.ravel()], W) + b), W2) + b2
Z = np.argmax(Z, axis=1)
Z = Z.reshape(xx.shape)
fig = plt.figure()
plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral, alpha=0.8)
plt.scatter(X[:, 0], X[:, 1], c=y, s=40, cmap=plt.cm.Spectral)
plt.xlim(xx.min(), xx.max())
plt.ylim(yy.min(), yy.max())
plt.show()

深度学习——02、深度学习入门 15-18相关推荐

  1. 人工智能教程第一课 深度学习和计算机视觉TensorFlow入门

    深度学习 学习目标 知道什么是深度学习 知道深度学习的应用场景 1.什么是深度学习 在介绍深度学习之前,我们先看下人工智能,机器学习和深度学习之间的关系: 机器学习是实现人工智能的一种途径,深度学习是 ...

  2. 视频教程-深度学习与TensorFlow 2入门实战-深度学习

    深度学习与TensorFlow 2入门实战 新加坡国立大学研究员 龙良曲 ¥399.00 立即订阅 扫码下载「CSDN程序员学院APP」,1000+技术好课免费看 APP订阅课程,领取优惠,最少立减5 ...

  3. 视频教程-人人都会深度学习之Tensorflow基础入门-深度学习

    人人都会深度学习之Tensorflow基础入门 大数据工程师/算法工程师/大数据讲师,毕业于西华大学软件工程专业.在大数据领域有着丰富的实战经验. 擅长领域:Spark/Hadoop.算法设计及系统架 ...

  4. 【github干货】主流深度学习开源框架从入门到熟练

    文章首发于微信公众号<有三AI> [github干货]主流深度学习开源框架从入门到熟练 今天送上有三AI学院第一个github项目 01项目背景 目前深度学习框架呈百家争鸣之态势,光是为人 ...

  5. 深度学习试题_初学者入门宝典-机器学习入门资料汇总及学习建议(2018版)

    机器学习初学者公众号自从2018年10月开设以来,发表了不少机器学习入门的宝贵资料,受到广大机器学习爱好者的好评,本文对2018年本站发过的文章进行分类和汇总,以便初学者更好地学习. 机器学习入门,初 ...

  6. 专访 | 杨强教授谈CCAI、深度学习泡沫与人工智能入门

    文 | 胡永波 7 月 22 - 23 日,由中国人工智能学会.阿里巴巴集团 & 蚂蚁金服主办,CSDN.中国科学院自动化研究所承办的第三届中国人工智能大会(CCAI 2017)将在杭州国际会 ...

  7. 五本必读的深度学习圣经书籍,入门 AI 从 深度学习 开始

    原标题:`五本必读的深度学习圣经书籍,入门 AI 从「深度学习」开始` (以下以 Daniel Jeffries 第一人称撰写) 多年来,由于实验室研究和现实应用效果之间的鸿沟,少有人持续研究人工智能 ...

  8. PyTorch 深度学习: 60 分钟极速入门

    PyTorch 深度学习: 60 分钟极速入门 2019年年初,ApacheCN组织志愿者翻译了PyTorch1.2版本中文文档(github地址),同时也获得了PyTorch官方授权,我相信已经有许 ...

  9. 【完结】给新手的12大深度学习开源框架快速入门项目

    文/编辑 | 言有三 这是一篇总结文,给大家来捋清楚12大深度学习开源框架的快速入门,这是有三AI的GitHub项目,欢迎大家star/fork. https://github.com/longpen ...

  10. wandb(wb)(weights and biases): 深度学习轻量级可视化工具入门教程

    参考文章:wandb: 深度学习轻量级可视化工具入门教程 这wandb有点蛋疼啊,说登录https://wandb.ai/authorize?signup=true获取API KEY,但貌似要梯子才能 ...

最新文章

  1. 骚操作 | 不重启 JVM,替换掉已经加载的类,偷天换日?
  2. JSP页面图片路径为中文时乱码解决办法
  3. 【转】WSS3.0开发--你还在为写CAML痛苦吗?
  4. linux下watch常见用法,watch命令详解(linux)
  5. Spring Cloud笔记-eureka及openfeign基本使用
  6. 自己定义AlertDialog对话框布局
  7. preparedStatement和Statement 有什么不一样
  8. SELinux系列(二)——SELinux有什么作用
  9. java程序员技术_JAVA程序员需要懂得哪些技术
  10. dbf文件怎么创建_DBC文件到底是个啥
  11. 对Java语言的byte类型变量进行无符号提升
  12. 电脑开机界面如何设置个性签名?
  13. 未转变者服务器可作弊,未转变者作弊指令有哪些-未转变者作弊指令大全
  14. matlab处理各种数据、文件
  15. CMD命令窗口光标消失解决方案
  16. GRP-U8如何修改账套主管
  17. Maple希腊字母按键查表
  18. 安天每日安全简讯20160712
  19. Delphi中JSON的使用
  20. 项目经理CMMI3工作指南

热门文章

  1. 20169207《Linux内核原理与分析》第五周作业
  2. js设计模式-组合模式
  3. [转]仿163网盘无刷新文件上传系统
  4. Java黑皮书课后题第9章:*9.4(使用Random类)编写一个程序,创建一个种子为1000的Random对象,然后使用nextInt(100)方法显示0到100之间的前50个随机整数
  5. 日志 php_高性能的PHP日志系统 SeasLog 使用
  6. 2011年上海交通大学计算机研究生机试真题
  7. 解题:POI 2013 Triumphal arch
  8. mqtt异步publish方法
  9. Jzoj5235 好的排列
  10. 【php学习笔记】ticks篇