神经网络学习-python实现LSTM
2016-09-12 16:25
856 查看
对于风速这种时间序列的预测,常见的神经网络有:BP神经网络,径向基函数神经网络,Elman神经网络;另外还有一个自回归积分滑动平均模型ARIMA。以上两类做的人比较多,也比较成熟了,偶然了解到LSTM,决定那这个试一下,理由就是:没人做。
以下的代码为python实现lstm,另外KERAS里面有lstm的模型,如此一个深度学习的大坑,真是让人情不自禁的往里跳啊!
以下的代码为python实现lstm,另外KERAS里面有lstm的模型,如此一个深度学习的大坑,真是让人情不自禁的往里跳啊!
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("------------")
相关文章推荐
- 人人都能用Python写出LSTM-RNN的代码![你的神经网络学习最佳起步]
- 深度学习(08)_RNN-LSTM循环神经网络-03-Tensorflow进阶实现
- tensorflow 学习笔记12 循环神经网络RNN LSTM结构实现MNIST手写识别
- 神经网络与深度学习 使用Python实现基于梯度下降算法的神经网络和自制仿MNIST数据集的手写数字分类可视化程序 web版本
- [置顶] 【深度学习】RNN循环神经网络Python简单实现
- Python实现深度学习之-神经网络识别手写数字(更新中,更新日期:2017-07-12)
- 深度学习与神经网络-吴恩达(Part1Week3)-单隐层神经网络编程实现(python)
- 人人都能用Python写出LSTM-RNN的代码![你的神经网络学习最佳起步]
- 深度学习5:python实现三层神经网络
- 循环神经网络教程4-用Python和Theano实现GRU/LSTM RNN, Part 4 – Implementing a GRU/LSTM RNN with Python and Theano
- 人人都能用Python写出LSTM-RNN的代码![你的神经网络学习最佳起步]
- 【Python开发】【神经网络与深度学习】网络爬虫之python实现
- 神经网络与深度学习 1.6 使用Python实现基于梯度下降算法的神经网络和MNIST数据集的手写数字分类程序
- Python20行代码实现多层神经网络的学习
- 【深度学习】1.2:简单神经网络的python实现
- Deep Learning 学习笔记(二):神经网络Python实现
- 深度学习(08)_RNN-LSTM循环神经网络-03-Tensorflow进阶实现
- 人人都能用Python写出LSTM-RNN的代码![你的神经网络学习最佳起步]
- 深度学习第一课 第四周 深层神经网络用python的实现
- 深度学习与神经网络-吴恩达(Part1Week4)-深度神经网络编程实现(python)-基础篇