具体内容:径向基网络函数(最小二乘法)python实现)_class rbfnetwork:-CSDN博客
pytorch代码实现:
import torch
import torch.nn as nn
import matplotlib.pyplot as plt
import torch.optim as optim
import time
from sklearn.datasets import load_iris
from sklearn.cluster import KMeans
from sklearn.model_selection import train_test_split
import numpy as np
start = time.perf_counter()
class RBF(nn.Module):
def __init__(self,centers_dim,out_dim,centers,sigma):
super(RBF,self).__init__()
self.flatten = nn.Flatten()#变成二维的
self.centers_dim=centers_dim
self.out_dim=out_dim
self.centers = nn.Parameter(centers)
self.sigma = nn.Parameter(sigma)
self.linear = nn.Linear(self.centers_dim, self.out_dim)
def forward(self,X):
x= self.flatten(X)
distance = torch.cdist(x, self.centers)
gauss = torch.exp(-distance ** 2 / (2 * self.sigma ** 2))
y=self.linear(gauss)
return y
#数据运行
iris = load_iris()
X = iris.data # 获取特征值
y = iris.target # 获取特征值
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=10)
X_tensor = torch.tensor(X_train, dtype=torch.float32) # 明确指定 dtype 以避免潜在的自动转换问题
y_tensor = torch.tensor(y_train, dtype=torch.long) # 类别标签应使用整数类
X_test = torch.tensor(X_test, dtype=torch.float32) # 明确指定 dtype 以避免潜在的自动转换问题
y_test = torch.tensor(y_test, dtype=torch.long)
centers_dim=10
input_dim=X_tensor.shape[1]
out_dim=3
#聚类产生聚类中心等
def P_centers(X_tensor,centers_dim):
kmeans = KMeans(n_clusters=centers_dim)
kmeans.fit(X_tensor) # 找到一组适当的中心点
centers = kmeans.cluster_centers_ # 用kmeans找中心点位
centers=torch.tensor(centers,dtype=torch.float32)
# 计算标准差
distances = torch.cdist(X_tensor,centers)
sigma = torch.std(distances, axis=0) # 计算了所有聚类中心的距离标准差的平均值
return centers,sigma
centers,sigma=P_centers(X_tensor,centers_dim)
rbf= RBF(centers_dim,out_dim,centers,sigma)
optimizer = optim.Adam(rbf.parameters(),lr=0.001)
loss_fun = nn.CrossEntropyLoss()#分类模型
start = time.time()
epochs = 10000
Loss=[]
for epoch in range(epochs):
Y_pre = rbf(X_tensor)
loss = loss_fun(Y_pre,y_tensor)
optimizer.zero_grad()
loss.backward()
optimizer.step()
print("epoch:{}\t loss:{:>.9}".format(epoch+1,loss.item()))
Loss.append(loss.item())
end = time.time()
print("time:",end-start)
plt.plot(Loss)
plt.xlabel("epoch")
plt.ylabel("Loss")
plt.show()
from sklearn.metrics import precision_score, recall_score, accuracy_score
def evaluation( y_test, y_predict):
precision = precision_score(y_test, y_predict, average='macro')
accuracy = accuracy_score(y_test, y_predict)
recall = recall_score(y_test, y_predict, average='macro')
print("accuracy:", accuracy)
print(" precision:", precision)
print(" recall:", recall)
#训练效果
print("!!!训练集!!!")
Y_pre= torch.max(Y_pre, 1)[1]
pred_y = Y_pre.data.numpy()
target_y = y_tensor.data.numpy()
# 衡量训练集准确率
evaluation(pred_y ,target_y)
#预测效果
print("!!!测试集!!!")
y_pre = rbf(X_test)
y_pre= torch.max(y_pre, 1)[1]
pred_y = y_pre.data.numpy()
target_y = y_test.data.numpy()
# 衡量测试集准确率
evaluation(pred_y ,target_y)
数据来源:Iris - UCI 机器学习存储库
运行结果: