如何解决为什么使用内核技巧的 LMS 中的参数测试版会爆炸?
我正在尝试使用内核技巧解决 LMS 问题。参考资料取自 this 注释。
我的问题是 beta 的值会暴涨。它在大约 5 或 6 次迭代中达到 10e17 的数量级。我注意到的一件事是,beta 中的符号条目会随着循环中 beta 的连续更新而波动(从正到负,反之亦然)。
此外,降低 alpha(学习率)的值也无济于事。我尝试了 alpha = 0.01
但测试版的价值仍然爆炸。
这是代码
import numpy as np
import pandas as pd
file = pd.read_csv("weatherHistory.csv")
#selecting only the required columns
useful_index = [3,4,5,6,7,8,10]
file = file.iloc[:,useful_index]
# getting training set and test set
file_randomized = file.sample(frac=1,random_state=1)
# useable data size
usable_dataset_size = 1200
# training set size
training_set_index = int(0.75 * usable_dataset_size)
# get rid of unnecessary data
file = file_randomized[:usable_dataset_size]
# make training and test set
training_set = file[:training_set_index]
test_set = file[training_set_index:]
# Select the columns
input_index = [0,2,3,6]
X = training_set.iloc[:,input_index]
output_index = [1]
Y = training_set.iloc[:,output_index]
# Convert to numpy into suitable format
X = X.to_numpy()
n = X.shape[0]
d = X.shape[1]
Y = Y.to_numpy()
# This function calculates K(phi(x),phi(y))
def kernel_matrix(K,degree=1):
"""K is the matrix of dot product. This applies kernel function to the matrix"""
result = np.zeros(K.shape)
for i in range(0,degree+1):
result += np.power(K,i)
return result
# Main training function
def lms_with_kt(X,Y,alpha,degree = 1,num_iters = 1000):
"""X: nxd vector,Y: nx1 vector,beta: nx1 zero vector,alpha: number,degree: number"""
# normalize x
x_min = X.min(axis = 0,keepdims=True)
x_max = X.max(axis = 0,keepdims=True)
X = (X - x_min) / (x_max - x_min)
n = X.shape[0]
# add the column of 1 in the front
X = np.hstack((np.ones((n,1)),X))
# make K_matrix (kernel matrix)
K = np.matmul(X,X.T)
K = kernel_matrix(K,degree)
# initialize beta
beta = np.zeros((n,1))
# update beta
for i in range(num_iters):
beta += alpha * (Y - np.matmul(K,beta))
print(beta)
def predict(x):
"""x: 1xd matrix"""
x_norm = (x - x_min) / (x_max - x_min)
n_predict = x_norm.shape[0]
x_norm = np.hstack((np.ones((n_predict,x_norm))
K_for_prediction = np.matmul(X,x_norm.T)
K_for_prediction = kernel_matrix(K_for_prediction,degree)
return np.dot(beta.T,K_for_prediction)
return predict
predictor = lms_with_kt(X,0.1,1000)
数据集的链接是 here。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。