Neural Networks?

I wanna create neural network.
And i done with this code, and help of Sebastian Lague tutorial videos:

code
local NeuralNetwork = {}
NeuralNetwork.__index = NeuralNetwork

--[[
	Actually what to do:
		New : function - creates new neuralnetwrok(woow)
		Forward : function - just get output from input
		Backpropagate : function - just get weights from output
		Activation : function - sigmoid function yep
	
	Very very important things:
	
]]

local matrix = require(script.matrix)

local function zip(...)
	local args, args_values = table.pack(...), { }
	local i = 0

	return function()
		i = i + 1

		for pos, t in ipairs(args) do
			args_values[pos] = t[i]

			if args_values[pos] == nil then
				return nil
			end				
		end
		return table.unpack(args_values)
	end
end

function NeuralNetwork.new(layer_sizes : {})
	local t1, t2 = layer_sizes, layer_sizes table.remove(t1, 1) table.remove(t2, #t2)
	local self = setmetatable({}, NeuralNetwork)
	local weight_shapes={} for a,b in zip(t1, t2) do table.insert(weight_shapes, {a,b}) end
	self.weights = {} for i,s in pairs(weight_shapes) do table.insert(self.weights, matrix.random(s[1], s[2])) end
	self.biases={} for i,s in pairs(t1) do table.insert(self.biases,matrix.new(s, 1)) end
	
	print(self.weights)
	return self
end

function NeuralNetwork:Forward(input)
	for w, b in zip(self.weights, self.biases) do
		input = self:Activation(matrix.mul(w, input)) -- matrix.mul isn't correct working
	end
	return input
end

function NeuralNetwork:Activation(x)
	return 1/(1+math.exp(-x))
end

return NeuralNetwork

My current problem is: matrix multiplication, and better understanding all of this

2 Likes

Can you explain what the problem is with your code? “Matrix multiplication” doesn’t really mean anything specific.

1 Like

like, i can’t do it, for some reason

1 Like

But what do you want to do in the first place?

1 Like

Send us the source of “script.matrix” since that’s your only focusable issue. We can try and help you out then.

Yep, ofcourse, sorry for long no response

function matrix.mul(m1, m2)
	if #m1[1] ~= #m2 then       -- inner matrix-dimensions must agree
		return nil      
	end 

	local res = {}

	for i = 1, #m1 do
		res[i] = {}
		for j = 1, #m2[1] do
			res[i][j] = 0
			for k = 1, #m2 do
				res[i][j] = res[i][j] + m1[i][k] * m2[k][j]
			end
		end
	end

	return res
end
function matrix.mul(m1, m2)
	if #m1[1] ~= #m2 then       -- inner matrix-dimensions must agree
        warn(("Matrix dimensions do not agree! Matrix A: %i | Matrix B: %i"):format(#m1[1], #m2))
		return nil      
	end 

	local res = {}

	for i = 1, #m1 do
		res[i] = {}
		for j = 1, #m2[1] do
			res[i][j] = 0
			for k = 1, #m2 do
				res[i][j] = res[i][j] + m1[i][k] * m2[k][j]
			end
		end
	end

	return res
end

Try this modification, and let me know if it warns you.