Python实现的递归神经网络简单示例

作者:罗兵 时间:2022-03-26 04:19:48 

本文实例讲述了Python实现的递归神经网络。分享给大家供大家参考,具体如下:


# Recurrent Neural Networks
import copy, numpy as np
np.random.seed(0)
# compute sigmoid nonlinearity
def sigmoid(x):
 output = 1/(1+np.exp(-x))
 return output
# convert output of sigmoid function to its derivative
def sigmoid_output_to_derivative(output):
 return output*(1-output)
# training dataset generation
int2binary = {}
binary_dim = 8
largest_number = pow(2,binary_dim)
binary = np.unpackbits(
 np.array([range(largest_number)],dtype=np.uint8).T,axis=1)
for i in range(largest_number):
 int2binary[i] = binary[i]
# input variables
alpha = 0.1
input_dim = 2
hidden_dim = 16
output_dim = 1
# initialize neural network weights
synapse_0 = 2*np.random.random((input_dim,hidden_dim)) - 1
synapse_1 = 2*np.random.random((hidden_dim,output_dim)) - 1
synapse_h = 2*np.random.random((hidden_dim,hidden_dim)) - 1
synapse_0_update = np.zeros_like(synapse_0)
synapse_1_update = np.zeros_like(synapse_1)
synapse_h_update = np.zeros_like(synapse_h)
# training logic
for j in range(10000):
 # generate a simple addition problem (a + b = c)
 a_int = np.random.randint(largest_number/2) # int version
 a = int2binary[a_int] # binary encoding
 b_int = np.random.randint(largest_number/2) # int version
 b = int2binary[b_int] # binary encoding
 # true answer
 c_int = a_int + b_int
 c = int2binary[c_int]
 # where we'll store our best guess (binary encoded)
 d = np.zeros_like(c)
 overallError = 0
 layer_2_deltas = list()
 layer_1_values = list()
 layer_1_values.append(np.zeros(hidden_dim))
 # moving along the positions in the binary encoding
 for position in range(binary_dim):
   # generate input and output
   X = np.array([[a[binary_dim - position - 1],b[binary_dim - position - 1]]])
   y = np.array([[c[binary_dim - position - 1]]]).T
   # hidden layer (input ~+ prev_hidden)
   layer_1 = sigmoid(np.dot(X,synapse_0) + np.dot(layer_1_values[-1],synapse_h))
   # output layer (new binary representation)
   layer_2 = sigmoid(np.dot(layer_1,synapse_1))
   # did we miss?... if so, by how much?
   layer_2_error = y - layer_2
   layer_2_deltas.append((layer_2_error)*sigmoid_output_to_derivative(layer_2))
   overallError += np.abs(layer_2_error[0])
   # decode estimate so we can print(it out)
   d[binary_dim - position - 1] = np.round(layer_2[0][0])
   # store hidden layer so we can use it in the next timestep
   layer_1_values.append(copy.deepcopy(layer_1))
 future_layer_1_delta = np.zeros(hidden_dim)
 for position in range(binary_dim):
   X = np.array([[a[position],b[position]]])
   layer_1 = layer_1_values[-position-1]
   prev_layer_1 = layer_1_values[-position-2]
   # error at output layer
   layer_2_delta = layer_2_deltas[-position-1]
   # error at hidden layer
   layer_1_delta = (future_layer_1_delta.dot(synapse_h.T) + layer_2_delta.dot(synapse_1.T)) * sigmoid_output_to_derivative(layer_1)
   # let's update all our weights so we can try again
   synapse_1_update += np.atleast_2d(layer_1).T.dot(layer_2_delta)
   synapse_h_update += np.atleast_2d(prev_layer_1).T.dot(layer_1_delta)
   synapse_0_update += X.T.dot(layer_1_delta)
   future_layer_1_delta = layer_1_delta
 synapse_0 += synapse_0_update * alpha
 synapse_1 += synapse_1_update * alpha
 synapse_h += synapse_h_update * alpha
 synapse_0_update *= 0
 synapse_1_update *= 0
 synapse_h_update *= 0
 # print(out progress)
 if j % 1000 == 0:
   print("Error:" + str(overallError))
   print("Pred:" + str(d))
   print("True:" + str(c))
   out = 0
   for index,x in enumerate(reversed(d)):
     out += x*pow(2,index)
   print(str(a_int) + " + " + str(b_int) + " = " + str(out))
   print("------------")

运行输出:


Error:[ 3.45638663]
Pred:[0 0 0 0 0 0 0 1]
True:[0 1 0 0 0 1 0 1]
9 + 60 = 1
------------
Error:[ 3.63389116]
Pred:[1 1 1 1 1 1 1 1]
True:[0 0 1 1 1 1 1 1]
28 + 35 = 255
------------
Error:[ 3.91366595]
Pred:[0 1 0 0 1 0 0 0]
True:[1 0 1 0 0 0 0 0]
116 + 44 = 72
------------
Error:[ 3.72191702]
Pred:[1 1 0 1 1 1 1 1]
True:[0 1 0 0 1 1 0 1]
4 + 73 = 223
------------
Error:[ 3.5852713]
Pred:[0 0 0 0 1 0 0 0]
True:[0 1 0 1 0 0 1 0]
71 + 11 = 8
------------
Error:[ 2.53352328]
Pred:[1 0 1 0 0 0 1 0]
True:[1 1 0 0 0 0 1 0]
81 + 113 = 162
------------
Error:[ 0.57691441]
Pred:[0 1 0 1 0 0 0 1]
True:[0 1 0 1 0 0 0 1]
81 + 0 = 81
------------
Error:[ 1.42589952]
Pred:[1 0 0 0 0 0 0 1]
True:[1 0 0 0 0 0 0 1]
4 + 125 = 129
------------
Error:[ 0.47477457]
Pred:[0 0 1 1 1 0 0 0]
True:[0 0 1 1 1 0 0 0]
39 + 17 = 56
------------
Error:[ 0.21595037]
Pred:[0 0 0 0 1 1 1 0]
True:[0 0 0 0 1 1 1 0]
11 + 3 = 14
------------

英文原文:https://iamtrask.github.io/2015/11/15/anyone-can-code-lstm/

希望本文所述对大家Python程序设计有所帮助。

来源:http://www.cnblogs.com/hhh5460/p/5782539.html

标签:Python,递归
0
投稿

猜你喜欢

  • python修改包导入时搜索路径的方法

    2023-05-12 07:40:23
  • 拖拽翻页(ThrowPage)详解 cool

    2009-12-02 09:54:00
  • RDFa介绍——构建更友好的web页面

    2009-09-19 17:01:00
  • 事件检测

    2009-04-11 18:03:00
  • yui3的AOP(面向切面编程)和OOP(面向对象编程)

    2009-09-24 14:47:00
  • 47个惊人的CSS3动画演示

    2010-02-07 12:32:00
  • 谈谈我的“分离”观

    2010-08-31 14:47:00
  • css基础教程布局篇之一

    2008-07-31 17:21:00
  • python中的内置函数max()和min()及mas()函数的高级用法

    2023-01-21 18:45:43
  • ASP用户登录模块的设计

    2008-11-21 16:55:00
  • Python代码调试的几种方法总结

    2022-06-14 18:21:27
  • 学会Python数据可视化必须尝试这7个库

    2022-03-26 20:37:25
  • SQL根据时间范围条件查询数据

    2009-01-18 13:32:00
  • Python科学计算包numpy用法实例详解

    2021-11-12 15:07:20
  • 回调函数的意义以及python实现实例

    2021-07-17 11:42:07
  • Phar反序列化超详细介绍

    2023-06-05 07:06:02
  • ASP 下载时重命名已上传文件的新下载文件名的实现代码

    2012-11-30 20:33:45
  • python列表的常用操作方法小结

    2023-06-21 16:14:34
  • 教你怎么使用sql游标实例分享

    2012-07-11 16:14:57
  • 简单了解什么是神经网络

    2023-10-11 22:26:34
  • asp之家 网络编程 m.aspxhome.com