1 #######################数据结构部分#############################################

2 importnumpy as np3 importmatplotlib.pyplot as plt4

5 #%matplotlib inline

6

7 classNode(object):8 def __init__(self, inbound_nodes =[]):9 self.inbound_nodes =inbound_nodes10 self.value =None11 self.outbound_nodes =[]12

13 self.gradients ={}14

15 for node ininbound_nodes:16 node.outbound_nodes.append(self)17

18 defforward(self):19 raiseNotImplementedError20

21 defbackward(self):22 raiseNotImplementedError23

24

25 classInput(Node):26 def __init__(self):27 Node.__init__(self)28

29 defforward(self):30 pass

31

32 defbackward(self):33 self.gradients ={self : 0}34 for n inself.outbound_nodes:35 self.gradients[self] +=n.gradients[self]36

37 ##################################################################################

38 classLinear(Node):39 def __init__(self, X, W, b):40 Node.__init__(self, [X, W, b])41

42 defforward(self):43 X =self.inbound_nodes[0].value44 W = self.inbound_nodes[1].value45 b = self.inbound_nodes[2].value46 self.value = np.dot(X, W) +b47

48 defbackward(self):49 self.gradients = {n: np.zeros_like(n.value) for n inself.inbound_nodes }50 for n inself.outbound_nodes:51 grad_cost =n.gradients[self]52 self.gradients[self.inbound_nodes[0]] += np.dot(grad_cost, self.inbound_nodes[1].value.T)53 self.gradients[self.inbound_nodes[1]] +=np.dot(self.inbound_nodes[0].value.T, grad_cost)54 self.gradients[self.inbound_nodes[2]] += np.sum(grad_cost, axis = 0, keepdims =False)55

56 ###################################################################################

57 classSigmoid(Node):58 def __init__(self, node):59 Node.__init__(self, [node])60

61 def_sigmoid(self, x):62 return 1. / (1. + np.exp(-x)) #exp() 方法返回x的指数,e的x次幂

63

64 defforward(self):65 input_value =self.inbound_nodes[0].value66 self.value =self._sigmoid(input_value)67

68 defbackward(self):69 self.gradients = {n: np.zeros_like(n.value) for n inself.inbound_nodes}70 for n inself.outbound_nodes:71 grad_cost =n.gradients[self]72 sigmoid =self.value73 self.gradients[self.inbound_nodes[0]] += sigmoid * (1 - sigmoid) *grad_cost74

75

76 classMSE(Node):77 def __init__(self, y, a):78 Node.__init__(self, [y, a])79

80

81 defforward(self):82 y = self.inbound_nodes[0].value.reshape(-1, 1)83 a = self.inbound_nodes[1].value.reshape(-1, 1)84

85 self.m =self.inbound_nodes[0].value.shape[0]86 self.diff = y -a87 self.value = np.mean(self.diff**2)88

89

90 defbackward(self):91 self.gradients[self.inbound_nodes[0]] = (2 / self.m) *self.diff92 self.gradients[self.inbound_nodes[1]] = (-2 / self.m) *self.diff93

94

95

96 ##########################计算图部分#############################################

97 deftopological_sort(feed_dict):98 input_nodes = [n for n infeed_dict.keys()]99 G ={}100 nodes = [n for n ininput_nodes]101 while len(nodes) >0:102 n =nodes.pop(0)103 if n not inG:104 G[n] = {'in' : set(), 'out': set()}105 for m inn.outbound_nodes:106 if m not inG:107 G[m] = {'in' : set(), 'out': set()}108 G[n]['out'].add(m)109 G[m]['in'].add(n)110 nodes.append(m)111

112 L =[]113 S =set(input_nodes)114 while len(S) >0 :115 n =S.pop()116 ifisinstance(n, Input):117 n.value =feed_dict[n]118 L.append(n)119 for m inn.outbound_nodes:120 G[n]['out'].remove(m)121 G[m]['in'].remove(n)122 if len(G[m]['in']) ==0 :123 S.add(m)124 returnL125

126

127

128 #######################使用方法##############################################

129 #首先由图的定义执行顺序

130 #graph = topological_sort(feed_dict)

131 defforward_and_backward(graph):132 for n ingraph :133 n.forward()134

135 for n in graph[:: -1]:136 n.backward()137

138 #对各个模块进行正向计算和反向求导

139 #forward_and_backward(graph)

140

141 #########################介绍梯度下降################

142 def sgd_update(trainables, learning_rate = 1e-2):143 for t intrainables :144 t.value = t.value - learning_rate *t.gradients[t]145

146 ###########使用这个模型#################################

147 from sklearn.utils importresample148 from sklearn importdatasets149

150 #%matplotlib inline

151

152 data =datasets.load_iris()153 X_ =data.data154 y_ =data.target155 y_[y_ == 2] = 1 #0 for virginica, 1 for not virginica

156 print(X_.shape, y_.shape) #out (150,4) (150,)

157

158 ########################用写的模块来定义这个神经网络#########################

159

160 np.random.seed(0)161 n_features = X_.shape[1]162 n_class = 1

163 n_hidden = 3

164

165 X, y =Input(), Input()166 W1, b1 =Input(), Input()167 W2, b2 =Input(), Input()168

169 l1 =Linear(X, W1, b1)170 s1 =Sigmoid(l1)171 l2 =Linear(s1, W2, b2)172 t1 =Sigmoid(l2)173 cost =MSE(y, t1)174

175

176 ###########训练模型###########################################

177 #随即初始化参数值

178 W1_0 = np.random.random(X_.shape[1] * n_hidden).reshape([X_.shape[1], n_hidden])179 W2_0 = np.random.random(n_hidden *n_class).reshape([n_hidden, n_class])180 b1_0 =np.random.random(n_hidden)181 b2_0 =np.random.random(n_class)182

183 #将输入值带入算子

184 feed_dict ={185 X: X_, y: y_,186 W1:W1_0, b1: b1_0,187 W2:W2_0, b2: b2_0188 }189

190 #训练参数

191 #这里训练100轮(eprochs),每轮抽4个样本(batch_size),训练150/4次(steps_per_eproch),学习率 0.1

192 epochs = 100

193 m =X_.shape[0]194 batch_size = 4

195 steps_per_eproch = m //batch_size196 lr = 0.1

197

198 graph =topological_sort(feed_dict)199 trainables =[W1, b1,W2, b2]200

201 l_Mat_W1 =[W1_0]202 l_Mat_W2 =[W2_0]203

204 l_loss =[]205 for i inrange(epochs):206 loss =0207 for j inrange(steps_per_eproch):208 X_batch, y_batch = resample(X_, y_, n_samples =batch_size)209 X.value =X_batch210 y.value =y_batch211

212 forward_and_backward(graph)213 sgd_update(trainables, lr)214 loss += graph[-1].value215

216 l_loss.append(loss)217 if i % 10 ==9:218 print("Eproch %d, Loss = %1.5f" %(i, loss))219

220

221 #图形化显示

222 plt.plot(l_loss)223 plt.title("Cross Entropy value")224 plt.xlabel("Eproch")225 plt.ylabel("Loss")226 plt.show()227

228

229 ##########最后用模型预测所有的数据的情况

230 X.value =X_231 y.value =y_232 for n ingraph:233 n.forward()234

235

236 plt.plot(graph[-2].value.ravel())237 plt.title("predict for all 150 Iris data")238 plt.xlabel("Sample ID")239 plt.ylabel("Probability for not a virginica")240 plt.show()

python中如何调用类takes no arguments_关于python中的 take no arguments 的解决方法相关推荐

  1. 线程中这么调用类_一文学会 Python 多线程编程

    Threading 模块从Python 1.5.2版开始出现,用于增强底层的多线程模块 thread .Threading 模块让操作多线程变得更简单,并且支持程序同时运行多个操作. 注意,Pytho ...

  2. python中如何调用类_python中如何调用类的方法

    类的方法的调用: 与普通的函数调用类似 1.类的内部调用:self.<方法名>(参数列表). 2.在类的外部调用:<实例名>.<方法名>(参数列表). 注意:以上两 ...

  3. python中如何调用类的方法

    python中如何调用类的方法 类的方法的调用: 与普通的函数调用类似 1.类的内部调用:self.<方法名>(参数列表). 2.在类的外部调用:<实例名>.<方法名&g ...

  4. python爬取百度使用kw关键字爬取时出现,百度安全验证,解决方法

    python爬取百度使用kw关键字爬取时出现,百度安全验证,解决方法 之前爬取百度用kw时的代码(没有任何问题) import requestsurl = 'http://www.baidu.com/ ...

  5. VS2008中的“解决方案配置”和“解决方案平台”不见了(Release和Debug)的解决方法...

    VS2008中的"解决方案配置"和"解决方案平台"不见了(Release和Debug)的解决方法 1.视图->工具栏->自定义 中->命令,选 ...

  6. PL/SQL中查询Oracle大数(17位以上)时显示科学计数法的解决方法

    PL/SQL中查询Oracle大数(17位以上)时显示科学计数法的解决方法 参考文章: (1)PL/SQL中查询Oracle大数(17位以上)时显示科学计数法的解决方法 (2)https://www. ...

  7. jquery中ajax请求后台数据成功后既不执行success也不执行error解决方法

    jquery中ajax请求后台数据成功后既不执行success也不执行error解决方法 参考文章: (1)jquery中ajax请求后台数据成功后既不执行success也不执行error解决方法 ( ...

  8. mysql安装服务和安装中常见问题install/Remove of the Service Denied与net start mysql服务启动失败解决方法

    mysql安装服务和安装中常见问题install/Remove of the Service Denied与net start mysql服务启动失败解决方法 参考文章: (1)mysql安装服务和安 ...

  9. mysql 1093 you can_mysql中错误:1093-You can’t specify target table for update in FROM clause的解决方法...

    发现问题 最近在处理一些数据库中数据的时候,写了下面的这一条sql语句: UPDATE f_student SET school_id = 0 WHERE id > ( SELECT id FR ...

最新文章

  1. java对redis的基本操作
  2. java spi机制_Java是如何实现自己的SPI机制的? JDK源码(一)
  3. 产品经理在早期如何快速学习?
  4. 第三方支付平台源码 商业支付源码
  5. Ubuntu下安装LXR
  6. 华三 h3c Vlan静态路由
  7. 空间统计分析之距离-思维导图(1)
  8. 科沃斯扫地机器人阿尔法_科沃斯阿尔法智能清洁扫地机器人配置详解
  9. JSP教程 JSP教程 JSP教程 JSP教程 JSP教程 JSP教程
  10. c语言实现字符串连接
  11. android的随机之魂
  12. 重积分 | 第二类曲面积分投影法正负判断
  13. 520情人节|用Python跟‘喜欢的人’表白
  14. linux中mtd动态加载,Linux 2.6.11 下 MTD驱动情景实例分析
  15. 对接LeetCode-完成代码提交校验
  16. jdk 1.8安装教程
  17. Android 3年外包工面试笔记,有机会还是要去大厂学习提升
  18. 利用VBA批量发送Excel中工资单邮件
  19. List Set Map 集合不可修改
  20. 19. 详解网络请求Axios

热门文章

  1. 95-10-020-启动-初始化ZK
  2. Spring Boot : Spring boot 的 AutoConfigurationImportSelector 自动配置原理
  3. Linux : 文件处理命令
  4. 【Spark】Spark Streaming的back pressure
  5. Spring Boot : Bean标签属性
  6. mac vim 无颜色 增加颜色
  7. 【Hbase】HBase的shell命令总结
  8. 云计算教程学习入门视频课件:云计算基础服务组件讲解
  9. java cipher用法_java使用Cipher 执行RSA解密报错
  10. 如何用多线程方式,提高rabbitmq消息处理效率?