How can I store numbers with a large decimal without getting inf or -inf returned?

Here is the function im running to get these values:

local function Loss(Inputs, Targets)
    local Results = {}

    for Index, Input in ipairs(Inputs) do
        local SubResult  = 0

        for SampleIndex, Sample in ipairs(Input) do
            local Product = Sample * Targets[SampleIndex]
            SubResult += math.log(Product)
        end

        Results[Index] = -SubResult
    end

    return Results
end

Example:

 Loss({
        { 0.02100454376380645, 0.9789954562361937 },
        { 5.075780888480321e-05, 0.9999492421911151 },
        { 1.255685226570402e-67, 1 },
        { 7.185125660791943e-75, 1 }
    }, {1, 0})

Output:

{ inf, inf, inf, inf }

I’ve tried running this same function in the output however but doing the math manually, and it returns a proper number instead of inf or -inf.

Try this:

local function Loss(Inputs, Targets)
	local Results = {}

	for Index, Input in ipairs(Inputs) do
		local SubResult = 0

		for SampleIndex, Sample in ipairs(Input) do
			local Product = Sample * Targets[SampleIndex]
			local SmoothedProduct = Product + 1e-10
			SubResult += math.log(SmoothedProduct)
		end

		Results[Index] = -SubResult
	end

	return Results
end

I figured it out, I wasn’t considering the fact that natural log of 0 is undefined or inf on roblox.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.