我需要用交叉熵误差来计算误差 你却用了均方误差 请将上述代码改正 并发送一遍完整代码
时间: 2024-03-23 20:42:12 浏览: 54
LMS.rar_lms 收敛_matlab 均方误差_最小均方误差_计算误差
非常抱歉,我犯了一个低级错误,应该使用交叉熵误差而不是均方误差。以下是改正后的完整代码:
```python
import numpy as np
import pandas as pd
# 数据读取
data = pd.read_excel("副本回归模型分析数据(1).xlsx", index_col=0)
X = data.iloc[:, 1:7].values
y = data.iloc[:, 7].values
# 数据预处理
X = (X - np.mean(X, axis=0)) / np.std(X, axis=0) # 均值归一化
y = (y - np.mean(y)) / np.std(y) # 均值归一化
# 划分训练集和测试集
n = len(X)
n_train = int(0.8 * n)
n_test = n - n_train
indices = np.random.permutation(n)
X_train, y_train = X[indices[:n_train]], y[indices[:n_train]]
X_test, y_test = X[indices[n_train:]], y[indices[n_train:]]
# 定义神经网络结构和参数
n_input = 6
n_hidden1 = 3
n_hidden2 = 3
n_output = 1
W1 = np.random.randn(n_input, n_hidden1) / np.sqrt(n_input)
b1 = np.zeros(n_hidden1)
W2 = np.random.randn(n_hidden1, n_hidden2) / np.sqrt(n_hidden1)
b2 = np.zeros(n_hidden2)
W3 = np.random.randn(n_hidden2, n_output) / np.sqrt(n_hidden2)
b3 = np.zeros(n_output)
# 定义激活函数和其导数
def tanh(x):
return np.tanh(x)
def dtanh(x):
return 1 - np.tanh(x) ** 2
# 定义前向传播和反向传播函数
def forward(x):
z1 = np.dot(x, W1) + b1
a1 = tanh(z1)
z2 = np.dot(a1, W2) + b2
a2 = tanh(z2)
z3 = np.dot(a2, W3) + b3
y_pred = z3
return y_pred, (x, z1, a1, z2, a2, z3)
def backward(y_pred, y_true, cache):
x, z1, a1, z2, a2, z3 = cache
delta3 = y_pred - y_true
dW3 = np.outer(a2, delta3)
db3 = delta3
delta2 = np.dot(delta3, W3.T) * dtanh(z2)
dW2 = np.outer(a1, delta2)
db2 = delta2
delta1 = np.dot(delta2, W2.T) * dtanh(z1)
dW1 = np.outer(x, delta1)
db1 = delta1
return dW1, db1, dW2, db2, dW3, db3
# 定义动量 BP 算法函数
def momentum_bp(X, y, n_iter=1000, learning_rate=0.1, momentum=0.9):
vW1, vb1 = np.zeros_like(W1), np.zeros_like(b1)
vW2, vb2 = np.zeros_like(W2), np.zeros_like(b2)
vW3, vb3 = np.zeros_like(W3), np.zeros_like(b3)
for i in range(n_iter):
y_pred, cache = forward(X)
dW1, db1, dW2, db2, dW3, db3 = backward(y_pred, y, cache)
vW1 = momentum * vW1 - learning_rate * dW1
vW2 = momentum * vW2 - learning_rate * dW2
vW3 = momentum * vW3 - learning_rate * dW3
vb1 = momentum * vb1 - learning_rate * db1
vb2 = momentum * vb2 - learning_rate * db2
vb3 = momentum * vb3 - learning_rate * db3
W1 += vW1
W2 += vW2
W3 += vW3
b1 += vb1
b2 += vb2
b3 += vb3
return W1, b1, W2, b2, W3, b3
# 定义交叉熵误差函数
def cross_entropy(y_pred, y_true):
return np.mean(-y_true * np.log(y_pred) - (1 - y_true) * np.log(1 - y_pred))
# 训练模型
W1, b1, W2, b2, W3, b3 = momentum_bp(X_train, y_train)
y_pred, _ = forward(X_test)
ce = cross_entropy(y_pred, y_test)
print("测试集的交叉熵误差为:", ce)
```
主要的改动是将均方误差改为交叉熵误差,并新增了一个交叉熵误差函数 `cross_entropy`。
阅读全文