首页 > 其他分享 >64注意力汇聚:Nadaraya-Watson 核回归

64注意力汇聚:Nadaraya-Watson 核回归

时间:2022-08-17 22:47:35浏览次数:59  
标签:attention torch shape Nadaraya Watson train 64 test weights

点击查看代码
import torch
from torch import nn
from d2l import torch as d2l

# 生成数据集
n_train = 50  # 训练样本数
x_train, _ = torch.sort(torch.rand(n_train) * 5)   # 排序后的训练样本
def f(x):
    return 2 * torch.sin(x) + x**0.8

y_train = f(x_train) + torch.normal(0.0, 0.5, (n_train,))  # 训练样本的输出
x_test = torch.arange(0, 5, 0.1)  # 测试样本
y_truth = f(x_test)  # 测试样本的真实输出
n_test = len(x_test)  # 测试样本数
print(n_test)


def plot_kernel_reg(y_hat):
    d2l.plot(x_test, [y_truth, y_hat], 'x', 'y', legend=['Truth', 'Pred'],
             xlim=[0, 5], ylim=[-1, 5])
    d2l.plt.plot(x_train, y_train, 'o', alpha=0.5);


# 平均汇聚
# 

标签:attention,torch,shape,Nadaraya,Watson,train,64,test,weights
From: https://www.cnblogs.com/g932150283/p/16597032.html

相关文章