写一个简单神经网络和对应的训练脚本
时间: 2024-05-07 13:16:56 浏览: 96
好的,以下是一个简单的神经网络的代码:
```
import numpy as np
class SimpleNeuralNetwork:
def __init__(self, num_inputs, num_hidden, num_outputs):
self.weights1 = np.random.randn(num_inputs, num_hidden)
self.biases1 = np.zeros((1, num_hidden))
self.weights2 = np.random.randn(num_hidden, num_outputs)
self.biases2 = np.zeros((1, num_outputs))
def forward(self, X):
self.z1 = np.dot(X, self.weights1) + self.biases1
self.a1 = np.tanh(self.z1)
self.z2 = np.dot(self.a1, self.weights2) + self.biases2
output = self.softmax(self.z2)
return output
def softmax(self, X):
expX = np.exp(X)
return expX / np.sum(expX, axis=1, keepdims=True)
def backward(self, X, y, output, learning_rate):
delta2 = output - y
dweights2 = np.dot(self.a1.T, delta2)
dbiases2 = np.sum(delta2, axis=0, keepdims=True)
delta1 = np.dot(delta2, self.weights2.T) * (1 - np.power(self.a1, 2))
dweights1 = np.dot(X.T, delta1)
dbiases1 = np.sum(delta1, axis=0)
self.weights1 -= learning_rate * dweights1
self.biases1 -= learning_rate * dbiases1
self.weights2 -= learning_rate * dweights2
self.biases2 -= learning_rate * dbiases2
```
接下来是使用该神经网络进行训练的脚本:
```
import numpy as np
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import OneHotEncoder
from simple_neural_network import SimpleNeuralNetwork
# 加载鸢尾花数据集
iris = load_iris()
X, y = iris.data, iris.target
# 将输出标签进行独热编码
encoder = OneHotEncoder()
y = encoder.fit_transform(y[:, np.newaxis]).toarray()
# 将数据集分为训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
# 初始化神经网络
network = SimpleNeuralNetwork(num_inputs=X.shape[1], num_hidden=5, num_outputs=y.shape[1])
# 训练神经网络
learning_rate = 0.001
num_epochs = 1000
for epoch in range(num_epochs):
output = network.forward(X_train)
network.backward(X_train, y_train, output, learning_rate)
loss = -np.sum(y_train * np.log(output))
print("Epoch {}: Loss {:.2f}".format(epoch, loss))
# 在测试集上进行预测
predictions = np.argmax(network.forward(X_test), axis=1)
labels = np.argmax(y_test, axis=1)
accuracy = np.mean(predictions == labels)
print("Accuracy on test set: {:.2f}%".format(accuracy * 100))
```
这个脚本利用鸢尾花数据集训练一个简单的神经网络来对鸢尾花的种类进行分类,最后在测试集上获得了约96%的准确率。
阅读全文