In [62]:
require 'torch'
require 'image'
require 'nn'
Plot = require 'iTorch.Plot'

ndims = 2;
nsamples = 1000

torch.seed()
classACenter = torch.randn(nsamples, ndims) - 0.5;
angles = torch.randn(nsamples, 1):mul(2 * math.pi);
radius = torch.randn(nsamples, 1):add(5);
xB = torch.cmul(radius, torch.sin(angles));
yB = torch.cmul(radius, torch.cos(angles));
classB1st = torch.cat(xB, yB, 2);
angles = torch.randn(nsamples, 1):mul(2 * math.pi);
radius = torch.randn(nsamples, 1):add(10);
xB = torch.cmul(radius, torch.sin(angles));
yB = torch.cmul(radius, torch.cos(angles));
classA2nd = torch.cat(xB, yB, 2);
classA = torch.cat(classACenter, classA2nd, 1);
angles = torch.randn(nsamples, 1):mul(2 * math.pi);
radius = torch.randn(nsamples, 1):add(15);
xB = torch.cmul(radius, torch.sin(angles));
yB = torch.cmul(radius, torch.cos(angles));
classB2nd = torch.cat(xB, yB, 2);
classB = torch.cat(classB1st, classB2nd, 1);

-- scatter plots
plot = Plot()
plot:circle(classA[{{}, 1}], classA[{{}, 2}], 'red', 'hi'):redraw()
plot:circle(classB[{{}, 1}], classB[{{}, 2}], 'green', 'yolo'):redraw()
plot:title('Random classes distribution'):redraw()
plot:xaxis('X-val'):yaxis('Y-val'):redraw()
plot:legend(true)
plot:redraw()



In [5]:
maxhidden = 50
math.randomseed(os.time());
for i = 1,10 do
    model1layer = nn.Sequential();
    nhidden = math.random(maxhidden) + 1;
    model1layer:add(nn.Linear(ndims, nhidden));
    model1layer:add(nn.Tanh());
--    model1layer:forward()
    curWeight = model1layer:get(1).weight
    slopes = torch.cdiv(-curWeight[{{}, 1}],curWeight[{{},2}]);
    bias = model1layer:get(1).bias
    plot = Plot()
    plot:segment(torch.ones(nhidden):fill(-4), torch.add(slopes * -4,bias), torch.ones(nhidden):fill(4), torch.add(slopes * 4,bias), 'red', 'hi'):redraw()
    plot:title('Random classes distribution'):redraw()
    plot:xaxis('X-val'):yaxis('Y-val'):redraw()
    plot:legend(true)
    plot:redraw()
end



In [ ]: