I’m attempting to create a function that can take a table, and broadcast it into a compatible shape. I already have a function that checks if a shape is compatible with the inputted table, but my broadcasting function currently doesn’t work as intended.
GOAL:
If you aren’t familiar with how array broadcasting works in Numpy, here are the docs explaining it:
https://numpy.org/doc/stable/user/basics.broadcasting.html
I’ve created a few tests, and currently this is the results of my broadcasting code (I am planning to rewrite the broadcasting code from scratch, as it isn’t working as intended:
--test passes
TestUtil:RunTest("Broadcast 1", function()
--[1,1,1]
local x = array.ones(3)
--[[
[
[rand,rand,rand],
[rand,rand,rand]
]
]]
local y = array.rand(2,3)
print(x)
print(y)
local z = x + y
print(z)
end)
--incorrect broadcasting behavior, as stated below.
TestUtil:RunTest("Broadcast 2", function()
--[1,1,1,1]
local x = array.ones(4) --size: 4
--[[
[
[rand],
[rand],
[rand],
[rand]
]
]]
local y = array.rand(4,1) --size: 4,1
print(x)
print(y)
--result should be 4,4
--instead it is 4,1
--only the first tensor ends up being broadcasted
local z = x + y
print(z)
end, false)
--test fails
--error: attempt to index number with number
TestUtil:RunTest("Broadcast 3", function()
--[[
[
[1],
[1],
[1],
[1]
]
]]
local x = array.ones(4,1)
--[[rand,rand,rand,rand]]
local y = array.rand(1,4)
print(x)
print(y)
local z = x + y
print(z)
end, false)
RESULTS:
test 1:
Works as intended
[
[ 1.5571, 1.5200, 1.2368 ],
[ 1.2068, 1.5486, 1.4700 ]
]
test 2:
Runs, but doesn’t have correct behavior (see example picture above)
[
[ 1.9340 ],
[ 1.4463 ],
[ 1.6272 ],
[ 1.7989 ]
]
test 3:
Error: attempt to index number with number