ELMAN神经网络代码
时间: 2023-10-16 22:11:21 浏览: 132
下面是一个简单的 Elman 神经网络的示例代码:
```python
import numpy as np
class ElmanNetwork:
def __init__(self, input_size, hidden_size, output_size):
self.input_size = input_size
self.hidden_size = hidden_size
self.output_size = output_size
self.Wih = np.random.rand(hidden_size, input_size)
self.Whh = np.random.rand(hidden_size, hidden_size)
self.Who = np.random.rand(output_size, hidden_size)
self.bh = np.zeros((hidden_size, 1))
self.bo = np.zeros((output_size, 1))
self.h_prev = np.zeros((hidden_size, 1))
def forward(self, inputs):
self.h = np.tanh(np.dot(self.Wih, inputs) + np.dot(self.Whh, self.h_prev) + self.bh)
self.y = np.dot(self.Who, self.h) + self.bo
return self.y
def train(self, inputs, targets, learning_rate):
self.forward(inputs)
dL_dYo = 2 * (self.y - targets) / inputs.shape[1]
dL_dWho = np.dot(dL_dYo, self.h.T)
dL_dbo = np.sum(dL_dYo, axis=1, keepdims=True)
dL_dYh = np.dot(self.Who.T, dL_dYo)
dL_dXh = (1 - self.h ** 2) * dL_dYh
dL_dWhh = np.dot(dL_dXh, self.h_prev.T)
dL_dbh = np.sum(dL_dXh, axis=1, keepdims=True)
self.Who -= learning_rate * dL_dWho
self.bo -= learning_rate * dL_dbo
self.Whh -= learning_rate * dL_dWhh
self.bh -= learning_rate * dL_dbh
self.h_prev = self.h
# 示例用法:
input_size = 2
hidden_size = 4
output_size = 1
net = ElmanNetwork(input_size, hidden_size, output_size)
# 训练数据
inputs = np.array([[0, 0, 1, 1], [0, 1, 0, 1]])
targets = np.array([[0, 1, 1, 0]])
# 训练模型
learning_rate = 0.1
for i in range(1000):
net.train(inputs, targets, learning_rate)
# 测试模型
test_inputs = np.array([[1, 1, 0, 0], [0, 0, 1, 1]])
predictions = net.forward(test_inputs)
print(predictions)
```
这段代码实现了一个简单的 Elman 神经网络,包括前向传播和训练过程。训练过程使用梯度下降法更新网络参数,然后使用测试数据进行预测。
阅读全文