首页 > 其他 > 详细

Kernel Regression from Nando's Deep Learning lecture 5

时间:2015-09-26 07:01:56      阅读:443      评论:0      收藏:0      [点我收藏+]

 

require torch
require gnuplot

local nData = 10
local kWidth = 1
local xTrain = torch.linspace(-1, 1, nData)
local yTrain = torch.pow(xTrain, 2)
print(xTrain)
print(yTrain)
local yTrain = yTrain + torch.mul(torch.randn(nData), 0.1)
print(yTrain)

local function phi(x, y)
    return torch.exp(-(1/kWidth)*torch.sum(torch.pow(x-y,2)))
end

local Phi = torch.Tensor(nData, nData)
for i = 1, nData do
    for j = 1, nData do
        Phi[i][j] = phi(xTrain[{{i}}], xTrain[{{j}}])
    end
end

local regularizer = torch.mul(torch.eye(nData), 0.001)
local theta = torch.inverse((Phi:t()*Phi) + regularizer) * Phi:t() * yTrain

local nTestData = 100
local xTest = torch.linspace(-1, 1, nTestData)

local PhiTest = torch.Tensor(nData, nTestData)
for i = 1, nData do
    for j = 1, nTestData do
        PhiTest[i][j] = phi(xTrain[{{i}}], xTest[{{j}}])
    end
end

local yPred = PhiTest:t() * theta

gnuplot.plot({Data, xTrain, yTrain, +}, {Prediction, xTest, yPred, -})

 

技术分享

Kernel Regression from Nando's Deep Learning lecture 5

原文:http://www.cnblogs.com/devai/p/4839923.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!