Created
October 12, 2022 08:37
-
-
Save catwell/821554cb251e114cad5cc635056f9bb4 to your computer and use it in GitHub Desktop.
Pallene routines used in a backpropagation algorithm in Teal
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
local record plnutil | |
vec_zeros: function(integer): {number} | |
mat_zeros: function(integer, integer): {{number}} | |
mat_vec_mul: function({{number}}, {number}): {number} | |
mat_t_vec_mul: function({{number}}, {number}): {number} | |
vec_vec_2d_mul: function({number}, {number}): {{number}} | |
vec_pointwise_mul: function({number}, {number}): {number} | |
vec_add_inplace: function({number}, {number}, number) | |
mat_add_inplace: function({{number}}, {{number}}, number) | |
vec_clip_inplace: function({number},number, number) | |
mat_clip_inplace: function({{number}}, number, number) | |
softmax: function({number}): {number} | |
end | |
return plnutil |
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
local M: module = {} | |
function M.vec_zeros(n: integer): {float} | |
local r : {float} = {} | |
for i = 1, n do r[i] = 0.0 end | |
return r | |
end | |
function M.mat_zeros(rows: integer, cols: integer): {{float}} | |
local r : {{float}} = {} | |
for x = 1, rows do r[x] = M.vec_zeros(cols) end | |
return r | |
end | |
function M.mat_vec_mul(m: {{float}}, v: {float}): {float} | |
local rows, cols = #m, #m[1] | |
-- assert(#v == cols) | |
local r: {float} = {} | |
for i = 1, rows do | |
r[i] = 0.0 | |
for j = 1, cols do | |
r[i] = r[i] + m[i][j] * v[j] | |
end | |
end | |
return r | |
end | |
function M.mat_t_vec_mul(m: {{float}}, v: {float}): {float} | |
-- multiply transpose of m by v | |
local rows, cols = #m, #m[1] | |
-- assert(#v == rows) | |
local r: {float} = {} | |
for i = 1, cols do | |
r[i] = 0.0 | |
for j = 1, rows do | |
r[i] = r[i] + m[j][i] * v[j] | |
end | |
end | |
return r | |
end | |
function M.vec_vec_2d_mul(v1: {float}, v2: {float}): {{float}} | |
local r: {{float}} = {} | |
for row = 1, #v1 do | |
r[row] = {} | |
for col = 1, #v2 do | |
r[row][col] = v1[row] * v2[col] | |
end | |
end | |
return r | |
end | |
function M.vec_pointwise_mul(v1: {float}, v2: {float}): {float} | |
local l = #v1 | |
-- assert(l > 0 and #v2 == l) | |
local r: {float} = {} | |
for i = 1, l do | |
r[i] = v1[i] * v2[i] | |
end | |
return r | |
end | |
function M.vec_add_inplace(v1: {float}, v2: {float}, w: float) | |
-- v1 <- v1 + w * v2 | |
local l = #v1 | |
-- assert(#v2 == l) | |
for i = 1, l do | |
v1[i] = v1[i] + w * v2[i] | |
end | |
end | |
function M.mat_add_inplace(m1: {{float}}, m2: {{float}}, w: float) | |
-- m1 <- m1 + w * m2 | |
local rows, cols = #m1, #m1[1] | |
-- assert(#m2 == rows and #m2[1] == cols) | |
for row = 1, rows do | |
for col = 1, cols do | |
m1[row][col] = m1[row][col] + w * m2[row][col] | |
end | |
end | |
end | |
function M.vec_clip_inplace(v: {float}, vmin: float, vmax: float) | |
for i = 1, #v do | |
if v[i] < vmin then | |
v[i] = vmin | |
elseif v[i] > vmax then | |
v[i] = vmax | |
end | |
end | |
end | |
function M.mat_clip_inplace(m: {{float}}, vmin: float, vmax: float) | |
for i = 1, #m do | |
M.vec_clip_inplace(m[i], vmin, vmax) | |
end | |
end | |
function M.softmax(v: {float}): {float} | |
local n = #v | |
local r: {float} = {} | |
for i = 1, n do | |
r[i] = math.exp(v[i]) | |
end | |
local s = 0.0 | |
for i = 1, n do | |
s = s + r[i] | |
end | |
for i = 1, n do | |
r[i] = r[i] / s | |
end | |
return r | |
end | |
return M |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment