吴裕雄 python深度学习与实践(11)
import numpy as np
from matplotlib import pyplot as plt A = np.array([[5],[4]])
C = np.array([[4],[6]])
B = A.T.dot(C)
AA = np.linalg.inv(A.T.dot(A))
l=AA.dot(B)
P=A.dot(l)
x=np.linspace(-2,2,10)
x.shape=(1,10)
xx=A.dot(x)
fig = plt.figure()
ax= fig.add_subplot(111)
ax.plot(xx[0,:],xx[1,:])
ax.plot(A[0],A[1],'ko') ax.plot([C[0],P[0]],[C[1],P[1]],'r-o')
ax.plot([0,C[0]],[0,C[1]],'m-o') ax.axvline(x=0,color='black')
ax.axhline(y=0,color='black') margin=0.1
ax.text(A[0]+margin, A[1]+margin, r"A",fontsize=20)
ax.text(C[0]+margin, C[1]+margin, r"C",fontsize=20)
ax.text(P[0]+margin, P[1]+margin, r"P",fontsize=20)
ax.text(0+margin,0+margin,r"O",fontsize=20)
ax.text(0+margin,4+margin, r"y",fontsize=20)
ax.text(4+margin,0+margin, r"x",fontsize=20)
plt.xticks(np.arange(-2,3))
plt.yticks(np.arange(-2,3)) ax.axis('equal')
plt.show()
x = [(2, 0, 3), (1, 0, 3), (1, 1, 3), (1,4, 2), (1, 2, 4)]
y = [5, 6, 8, 10, 11] alpha = 0.02
diff = [0, 0]
error0 = 0
error1 = 0
w0 = 0
w1 = 0
w2 = 0 cnt = 0
while True:
cnt += 1
for i in range(len(x)):
diff[0] = (w0 * x[i][0] + w1 * x[i][1] + w2 * x[i][2]) - y[i]
w0 -= alpha * diff[0] * x[i][0]
w1 -= alpha * diff[0] * x[i][1]
w2 -= alpha * diff[0] * x[i][2]
error1 = 0
for lp in range(len(x)):
error1 += (y[lp] - (w0 + w1 * x[lp][1] + w2 * x[lp][2])) ** 2 / 2
if abs(error1 - error0) < 0.002:
break
else:
error0 = error1 print('theta0 : %f, theta1 : %f, theta2 : %f, error1 : %f' % (w0, w1, w2, error1))
print('Done: theta0 : %f, theta1 : %f, theta2 : %f' % (w0, w1, w2))
print('迭代次数: %d' % cnt)
import math
import random
import numpy as np def rand(a, b):
return (b - a) * random.random() + a def make_matrix(m,n,fill=0.0):
mat = []
for i in range(m):
mat.append([fill] * n)
return mat def sigmoid(x):
return 1.0 / (1.0 + math.exp(-x)) def sigmod_derivate(x):
return x * (1 - x) class BPNeuralNetwork: def __init__(self):
self.input_n = 0
self.hidden_n = 0
self.output_n = 0
self.input_cells = []
self.hidden_cells = []
self.output_cells = []
self.input_weights = []
self.output_weights = [] def setup(self,ni,nh,no):
self.input_n = ni + 1
self.hidden_n = nh
self.output_n = no self.input_cells = [1.0] * self.input_n
self.hidden_cells = [1.0] * self.hidden_n
self.output_cells = [1.0] * self.output_n self.input_weights = make_matrix(self.input_n,self.hidden_n)
self.output_weights = make_matrix(self.hidden_n,self.output_n) # random activate
for i in range(self.input_n):
for h in range(self.hidden_n):
self.input_weights[i][h] = rand(-0.2, 0.2)
for h in range(self.hidden_n):
for o in range(self.output_n):
self.output_weights[h][o] = rand(-2.0, 2.0) def predict(self,inputs):
for i in range(self.input_n - 1):
self.input_cells[i] = inputs[i] for j in range(self.hidden_n):
total = 0.0
for i in range(self.input_n):
total += self.input_cells[i] * self.input_weights[i][j]
self.hidden_cells[j] = sigmoid(total) for k in range(self.output_n):
total = 0.0
for j in range(self.hidden_n):
total += self.hidden_cells[j] * self.output_weights[j][k]
self.output_cells[k] = sigmoid(total) return self.output_cells[:] def back_propagate(self,case,label,learn): self.predict(case)
#计算输出层的误差
output_deltas = [0.0] * self.output_n
for k in range(self.output_n):
error = label[k] - self.output_cells[k]
output_deltas[k] = sigmod_derivate(self.output_cells[k]) * error #计算隐藏层的误差
hidden_deltas = [0.0] * self.hidden_n
for j in range(self.hidden_n):
error = 0.0
for k in range(self.output_n):
error += output_deltas[k] * self.output_weights[j][k]
hidden_deltas[j] = sigmod_derivate(self.hidden_cells[j]) * error #更新输出层权重
for j in range(self.hidden_n):
for k in range(self.output_n):
self.output_weights[j][k] += learn * output_deltas[k] * self.hidden_cells[j] #更新隐藏层权重
for i in range(self.input_n):
for j in range(self.hidden_n):
self.input_weights[i][j] += learn * hidden_deltas[j] * self.input_cells[i] error = 0
for o in range(len(label)):
error += 0.5 * (label[o] - self.output_cells[o]) ** 2 return error def train(self,cases,labels,limit = 100,learn = 0.05):
for i in range(limit):
error = 0
for i in range(len(cases)):
label = labels[i]
case = cases[i]
error += self.back_propagate(case, label, learn)
pass def test(self):
cases = [
[0, 0],
[0, 1],
[1, 0],
[1, 1],
]
labels = [[0], [1], [1], [0]]
self.setup(2, 5, 1)
self.train(cases, labels, 100000, 0.001)
for case in cases:
print(self.predict(case)) if __name__ == '__main__':
nn = BPNeuralNetwork()
nn.test()
吴裕雄 python深度学习与实践(11)的更多相关文章
- 吴裕雄 python深度学习与实践(16)
import struct import numpy as np import matplotlib.pyplot as plt dateMat = np.ones((7,7)) kernel = n ...
- 吴裕雄 python深度学习与实践(7)
import cv2 import numpy as np img = np.mat(np.zeros((,))) cv2.imshow("test",img) cv2.waitK ...
- 吴裕雄 python深度学习与实践(18)
# coding: utf-8 import time import numpy as np import tensorflow as tf import _pickle as pickle impo ...
- 吴裕雄 python深度学习与实践(17)
import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data import time # 声明输 ...
- 吴裕雄 python深度学习与实践(15)
import tensorflow as tf import tensorflow.examples.tutorials.mnist.input_data as input_data mnist = ...
- 吴裕雄 python深度学习与实践(14)
import numpy as np import tensorflow as tf import matplotlib.pyplot as plt threshold = 1.0e-2 x1_dat ...
- 吴裕雄 python深度学习与实践(13)
import numpy as np import matplotlib.pyplot as plt x_data = np.random.randn(10) print(x_data) y_data ...
- 吴裕雄 python深度学习与实践(12)
import tensorflow as tf q = tf.FIFOQueue(,"float32") counter = tf.Variable(0.0) add_op = t ...
- 吴裕雄 python深度学习与实践(10)
import tensorflow as tf input1 = tf.constant(1) print(input1) input2 = tf.Variable(2,tf.int32) print ...
随机推荐
- C# 异步通信 网络聊天程序开发 局域网聊天室开发
Prepare 本文将使用一个NuGet公开的组件技术来实现一个局域网聊天程序,利用组件提供的高性能异步网络机制实现,免去了手动编写底层的困扰,易于二次开发,扩展自己的功能. 在Visual Stud ...
- JAVA常用设计模式(一、抽象工厂模式)
抽象工厂模式 抽象工厂模式(Abstract Factory Pattern)是围绕一个超级工厂创建其他工厂.该超级工厂又称为其他工厂的工厂.这种类型的设计模式属于创建型模式,它提供了一种创建对象的最 ...
- Bootstrap如何禁止响应式布局 不适配
Bootstrap 会自动帮你针对不同的屏幕尺寸调整你的页面,使其在各个尺寸的屏幕上表现良好.下面我们列出了如何禁用这一特性,就像这个非响应式布局实例页面一样. 禁止响应式布局有如下几步: 移除 此 ...
- Python学习之路基础篇--01Python的基本常识
1 计算机基础 首先认识什么是CPU(Central Processing Unit),即中央处理器,相当于人类的大脑.内存,临时储存数据,断电即消失.硬盘,可以长久的储存数据,有固态硬盘,机械硬盘之 ...
- spring jpa 动态查询(Specification)
//dao层 继承 扩展仓库接口JpaSpecificationExecutor (JPA 2引入了一个标准的API)public interface CreditsEventDao extends ...
- linux自启动tomcat
第一种方式 1.修改脚本文件rc.local:vim /etc/rc.d/rc.local 这个脚本是使用者自定的开机启动程序,可以在里面添加想在系统启动之后执行的脚本或者脚本执行命令 2.添加如下内 ...
- angular 使用window事件
1. 使用host 2. 使用HostListener 推荐使用第二种方式. 不推荐下面的方法,虽然也能进行window事件的绑定,但组件销毁后,window事件任然保留,即使手动在组件的ngOn ...
- 第一节《Git初始化》
创建版本库以及第一次提交 首先我看查看一下git的版本,本地的git是用的yum安装方式,如果想使用源码安装请参考官方文档. [root@git ~]# git --versiongit versio ...
- 为什么redis使用单线程还能这么快?
通常来讲,单线程处理能力要比多线程差,但是redis为什么就快了,这主要得益于以下几个原因: 1.纯内存访问,redis将所有数据放在内存中,内存的响应时长大约为100纳秒,这是redis达到每秒万级 ...
- 第3章 Java数组(上): 一维数组和二维数组
3.数组及排序算法(2天) 3.1 数组的概述 2课时 3.2 一维数组的使用 3课时 3.3 多维数组的使用 3课时 3.4 数组中涉及到的常见算法 3课时 3.5 Arrays工具类的使用 3课时 ...