require 'torch'
require 'gnuplot' local nData =
local kWidth =
local xTrain = torch.linspace(-, , nData)
local yTrain = torch.pow(xTrain, )
print(xTrain)
print(yTrain)
local yTrain = yTrain + torch.mul(torch.randn(nData), 0.1)
print(yTrain) local function phi(x, y)
return torch.exp(-(/kWidth)*torch.sum(torch.pow(x-y,)))
end local Phi = torch.Tensor(nData, nData)
for i = , nData do
for j = , 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 =
local xTest = torch.linspace(-, , nTestData) local PhiTest = torch.Tensor(nData, nTestData)
for i = , nData do
for j = , 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, '-'})