-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
158 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Builds 35 different quizes naming them testXX, XX = 01 -> 35 | ||
|
||
for i in 0{1..9} {10..35}; do | ||
rubber --pdf --jobname test$i quiz.tex | ||
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
\documentclass{article} | ||
\usepackage{pgf} | ||
\usepackage{amsmath,amssymb,bm} | ||
% Random int | ||
\pgfmathsetseed{\number\pdfrandomseed} | ||
\newcommand\rint{\pgfmathparse{random(10)}\pgfmathresult} | ||
\begin{document} | ||
|
||
\title{Quiz: forward propagation dimensionality} | ||
\author{BME595 DeepLearning} | ||
\date{\today} | ||
\maketitle | ||
|
||
Given a network with the size vector $\bm{s}=(\rint, \rint, \rint, \rint)^\top$, determine $n$, $K$, $L$ and write the dimensionality of: | ||
% | ||
\begin{align*} | ||
\bm{x} \\ | ||
\bm{\hat x} \\ | ||
\bm{a}^{(1)} \\ | ||
\bm{\hat a}^{(1)} \\ | ||
\bm{\Theta}^{(1)} \\ | ||
\bm{z}^{(2)} \\ | ||
\bm{a}^{(2)} \\ | ||
\bm{\hat a}^{(2)} \\ | ||
\bm{\Theta}^{(2)} \\ | ||
\bm{z}^{(3)} \\ | ||
\bm{a}^{(3)} \\ | ||
\bm{\hat a}^{(3)} \\ | ||
\bm{\Theta}^{(3)} \\ | ||
\bm{z}^{(4)} \\ | ||
\bm{a}^{(4)} \\ | ||
\bm{\hat a}^{(4)} \\ | ||
h_{\bm{\Theta}}(\bm{x}) \\ | ||
\bm{y} | ||
\end{align*} | ||
|
||
\end{document} |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
-- Recording script, not a runnable script | ||
|
||
require 'nn'; | ||
lin = nn.Linear(5, 3) | ||
lin | ||
{lin} | ||
lin.weight | ||
lin.bias | ||
Theta_1 = torch.cat(lin.bias, lin.weight, 2) -- New Tensor | ||
Theta_1 | ||
lin:zeroGradParameters() | ||
|
||
sig = nn.Sigmoid() | ||
{sig} | ||
require 'gnuplot'; | ||
z = torch.linspace(-10, 10, 21) | ||
gnuplot.plot(z, sig:forward(z)) | ||
-- Forward pass | ||
x = torch.randn(5) | ||
a1 = x | ||
h_Theta = sig:forward(lin:forward(x)):clone() | ||
z2 = Theta_1 * torch.cat(torch.ones(1), x, 1) | ||
a2 = z_1:clone():apply(function (z) return 1/(1 + math.exp(-z)) end) | ||
|
||
-- Backward pass | ||
loss = nn.MSECriterion() | ||
? nn.MSECriterion | ||
loss | ||
loss.sizeAverage = false | ||
y = torch.rand(3) | ||
-- forward(input, target) | ||
E = loss:forward(h_Theta, y) | ||
E | ||
(h_Theta - y):pow(2):sum() | ||
|
||
dE_dh = loss:updateGradInput(h_Theta, y):clone() | ||
dE_dh | ||
2 * (h_Theta - y) | ||
|
||
delta2 = sig:updateGradInput(z2, dE_dh) | ||
dE_dh:clone():cmul(a2):cmul(1 - a2) | ||
|
||
lin:accGradParameters(x, delta2) | ||
{lin} | ||
lin.gradWeight | ||
lin.gradBias | ||
delta2:view(-1, 1) * torch.cat(torch.ones(1), x, 1):view(1, -1) | ||
|
||
lin_gradInput = lin:updateGradInput(x, delta2) | ||
lin.weight:t() * delta2 | ||
|
||
net = nn.Sequential() | ||
net:add(lin); | ||
net:add(sig); | ||
net | ||
|
||
-- While true | ||
pred = net:forward(x) | ||
pred | ||
h_Theta | ||
err = loss:forward(pred, y) | ||
err | ||
E | ||
gradCriterion = loss:backward(pred, y) | ||
gradCriterion | ||
dE_dh | ||
net:zeroGradParameters() | ||
net:get(1) | ||
torch.cat(net:get(1).gradBias, net:get(1).gradWeight, 2) | ||
|
||
oldWeight = net:get(1).weight:clone() | ||
oldBias = net:get(1).bias:clone() | ||
etha = 0.01 | ||
net:updateParameters(etha) | ||
net:get(1).weight | ||
oldWeight - 0.01 * net:get(1).gradWeight | ||
net:get(1).bias | ||
oldBias - 0.01 * net:get(1).gradBias | ||
|
||
-- X: design matrix | ||
-- Y: labels / targets matrix / vector | ||
|
||
for i = 1, m do | ||
local pred = net:forward(X[i]) | ||
local err = criterion:forward(pred, Y[i]) | ||
local gradCriterion = criterion:backward(pred, Y[i]) | ||
net:zeroGradParameters() | ||
net:backward(X[i], gradCriterion) | ||
net:updateParameters(learningRate) | ||
end | ||
|
||
for i = 1, m, batchSize do | ||
net:zeroGradParameters() | ||
for j = 0, batchSize - 2 do | ||
if i+j > m then break end | ||
local pred = net:forward(X[i+j]) | ||
local err = criterion:forward(pred, Y[i+j]) | ||
local gradCriterion = criterion:backward(pred, Y[i+j]) | ||
net:backward(X[i+j], gradCriterion) | ||
end | ||
net:updateParameters(learningRate) | ||
end | ||
|
||
dataset = {} | ||
function dataset:size() return m end | ||
for i = 1, m do dataset[i] = {X[i], Y[i]} end | ||
trainer = nn.StochasticGradient(net, loss) | ||
trainer:train(dataset) | ||
|
Binary file not shown.