Why multiply and divided is not the same accuracy sometimes in lua? - lua

print((1 / 10 - 0.1) == 0) -- true
print((1 * 0.1 - 0.1) == 0) -- true
print((3 / 10 - 0.3) == 0) -- true
print((3 * 0.1 - 0.3) == 0) -- false
the last one is false, it means is not the 0.

Lua 5.3.5 Copyright (C) 1994-2018 Lua.org, PUC-Rio
> function a(x) return ("%A"):format(x) end
> a(0.1)
0X1.999999999999AP-4
> a(0.3)
0X1.3333333333333P-2
> a(0.1 * 3)
0X1.3333333333334P-2
They are different due to rounding errors.
More details:
0.1 =
0X1.9999999999999999999... * 2^(-4) =
(rounding)
0X1.999999999999A * 2^(-4) =
0X1.999999999999AP-4
0.3 =
0X1.333333333333333333... * 2^(-2) =
(rounding)
0X1.3333333333333 * 2^(-2) =
0X1.3333333333333P-2
0.1 * 3 =
0X1.999999999999AP-4 * 3 =
0X1.999999999999A * 2^(-4) * 3 =
0X4.CCCCCCCCCCCCE * 2^(-4) =
0X1.33333333333338 * 2^(-2) =
(rounding)
0X1.3333333333334 * 2^(-2) =
0X1.3333333333334P-2

Related

Cost does not converge/converges very slowly in the soft-coded version?

I don't understand. When I hardcode my script, it converges excellent, but in the softcode version, given the same structure and learning rate, it converges very slowly and then simply stops converging from some point on.
Here is the softcode version:
def BCE_loss(Y_hat, Y):
m = Y_hat.shape[1]
cost = (-1 / m) * (np.dot(Y, np.log(Y_hat+1e-5).T) + np.dot(1-Y, np.log(1-Y_hat+1e-5).T))
cost = np.squeeze(cost)
return cost
def BCE_loss_backward(Y_hat, Y):
dA_prev = - (np.divide(Y, Y_hat) - np.divide(1-Y, 1-Y_hat))
return dA_prev
def gradient(dZ, A_prev):
dW = np.dot(dZ, A_prev.T) * (1 / A_prev.shape[1])
db = np.sum(dZ, axis=1, keepdims=True) * (1 / A_prev.shape[1])
return dW, db
def update(W, b, dW, db, learning_rate):
W -= np.dot(learning_rate, dW)
b -= np.dot(learning_rate, db)
return W, b
for i in range(epochs+1):
## Forward pass
for l in range(1, L):
if l==L-1:
if out_dim==1:
grads_GD['Z'+str(l)] = linear(params_GD['W'+str(l)], grads_GD['A'+str(l-1)], params_GD['b'+str(l)])
grads_GD['A'+str(l)] = sigmoid(grads_GD['Z'+str(l)])
else:
grads_GD['Z'+str(l)] = linear(params_GD['W'+str(l)], grads_GD['A'+str(l-1)], params_GD['b'+str(l)])
grads_GD['A'+str(l)] = softmax(grads_GD['Z'+str(l)])
else:
grads_GD['Z'+str(l)] = linear(params_GD['W'+str(l)], grads_GD['A'+str(l-1)], params_GD['b'+str(l)])
grads_GD['A'+str(l)] = relu(grads_GD['Z'+str(l)])
## Compute cost
if out_dim==1:
cost_GD = BCE_loss(grads_GD['A'+str(L-1)], Y)
cost_list_GD.append(cost_GD)
else:
cost_GD = CE_loss(grads_GD['A'+str(L-1)], Y)
cost_list_GD.append(cost_GD)
## Print cost
if i % print_num == 0:
print(f"Cost for gradient descent optimizer after epoch {i}: {cost_GD: .4f}")
elif cost_GD < cost_lim or i == epochs:
last_epoch_GD = i
print(f"Cost for gradient descent optimizer after epoch {i}: {cost_GD: .4f}")
break
else:
continue
## Backward pass
if out_dim==1:
grads_GD['dA'+str(L-1)] = BCE_loss_backward(grads_GD['A'+str(L-1)], Y)
grads_GD['dZ'+str(L-1)] = sigmoid_backward(grads_GD['dA'+str(L-1)], grads_GD['Z'+str(L-1)])
else:
grads_GD['dA'+str(L-1)] = CE_loss_backward(grads_GD['A'+str(L-1)], Y)
grads_GD['dZ'+str(L-1)] = softmax_backward(grads_GD['dA'+str(L-1)], grads_GD['Z'+str(L-1)])
grads_GD['dW'+str(L-1)], grads_GD['db'+str(L-1)] = gradient(grads_GD['dZ'+str(L-1)], grads_GD['A'+str(L-2)])
for l in reversed(range(1, L-1)):
grads_GD['dA'+str(l)] = linear_backward(params_GD['W'+str(l+1)], grads_GD['dZ'+str(l+1)])
grads_GD['dZ'+str(l)] = relu_backward(grads_GD['dA'+str(l)], grads_GD['Z'+str(l)])
grads_GD['dW'+str(l)], grads_GD['db'+str(l)] = gradient(grads_GD['dZ'+str(l)], grads_GD['A'+str(l-1)])
## Update parameters
for l in range(1, L):
params_GD['W'+str(l)], params_GD['b'+str(l)] = update(params_GD['W'+str(l)], params_GD['b'+str(l)], grads_GD['dW'+str(l)], grads_GD['db'+str(l)], learning_rate)
and here is the hardcode version:
def cost_function(Y, A4, N, epsilon):
cost = (-1 / N) * np.sum(np.multiply(Y, np.log(A4 + epsilon)) + np.multiply(1 - Y, np.log(1 - A4 + epsilon)))
return cost
for i in range(epochs):
Z1_GD = np.dot(W1_GD, X) + b1_GD
A1_GD = np.maximum(0, Z1_GD)
Z2_GD = np.dot(W2_GD, A1_GD) + b2_GD
A2_GD = np.maximum(0, Z2_GD)
Z3_GD = np.dot(W3_GD, A2_GD) + b3_GD
A3_GD = np.maximum(0, Z3_GD)
Z4_GD = np.dot(W4_GD, A3_GD) + b4_GD
A4_GD = class_layer(Z4_GD)
dZ4_GD = A4_GD - Y
dW4_GD = np.dot(dZ4_GD, A3_GD.T) * (1. / A3_GD.shape[1])
db4_GD = np.sum(dZ4_GD, axis=1, keepdims=True) * (1. / A3_GD.shape[1])
dA3_GD = np.dot(W4_GD.T, dZ4_GD)
dZ3_GD = np.array(dA3_GD, copy=True)
dZ3_GD[Z3_GD <= 0] = 0
dW3_GD = np.dot(dZ3_GD, A2_GD.T) * (1. / A2_GD.shape[1])
db3_GD = np.sum(dZ3_GD, axis=1, keepdims=True) * (1. / A2_GD.shape[1])
dA2_GD = np.dot(W3_GD.T, dZ3_GD)
dZ2_GD = np.array(dA2_GD, copy=True)
dZ2_GD[Z2_GD <= 0] = 0
dW2_GD = np.dot(dZ2_GD, A1_GD.T) * (1. / A1_GD.shape[1])
db2_GD = np.sum(dZ2_GD, axis=1, keepdims=True) * (1. / A1_GD.shape[1])
dA1_GD = np.dot(W2_GD.T, dZ2_GD)
dZ1_GD = np.array(dA1_GD, copy=True)
dZ1_GD[Z1_GD <= 0] = 0
dW1_GD = np.dot(dZ1_GD, X.T) * (1. / X.shape[1])
db1_GD = np.sum(dZ1_GD, axis=1, keepdims=True) * (1. / X.shape[1])
W1_GD = W1_GD - learning_rate * dW1_GD
b1_GD = b1_GD - learning_rate * db1_GD
W2_GD = W2_GD - learning_rate * dW2_GD
b2_GD = b2_GD - learning_rate * db2_GD
W3_GD = W3_GD - learning_rate * dW3_GD
b3_GD = b3_GD - learning_rate * db3_GD
W4_GD = W4_GD - learning_rate * dW4_GD
b4_GD = b4_GD - learning_rate * db4_GD
cost_GD = cost_function(Y, A4_GD, N, epsilon)
cost_GD = np.squeeze(cost_GD)
cost_list_GD.append(cost_GD)
I suppose something went wrong during softcoding.
I solved it myself. Apparently, the "else: continue" line in the print cost section caused the algorithm to do a backward pass only once. After that, it was just looping through the forward pass. Can anyone please explain the reason for such behavior?

How do I improve my RBX.Lua procedural generation code?

This is my first post in stack overflow, and I have no clue whether I can post stuff about roblox development here.
But anyways, here I go. I'm making a top-down pixel art survival game, and am procedurally generating "flat" worlds with many different biomes. The code is working fine, it's just that the results look really unnatural, and I don't know what to do about it.
Some examples of what I mean:
world 1, world 2, world 3
Here is my code:
local biomeInfo = require(script.BiomeInfo)
local heightSeed = math.random(-2147483640, 2147483640)
local moistureSeed = math.random(-2147483640, 2147483640)
local heatSeed = math.random(-2147483640, 2147483640)
local mapSize = 250
local resolution = 20
local frequency = 15
local amplitude = 95
-- Generate noise maps
local grid = {}
for x = 1, mapSize do
grid[x] = {}
for z = 1, mapSize do
local heightMap = math.noise(x / resolution * frequency / amplitude, z / resolution * frequency / amplitude, heightSeed)
local moistureMap = math.noise(x / resolution * frequency / amplitude, z / resolution * frequency / amplitude, moistureSeed)
local heatMap = math.noise(x / resolution * frequency / amplitude, z / resolution * frequency / amplitude, heatSeed)
--[[local heightMap = math.noise(x / resolution * 1.2 / 3.2, z / resolution * 1.2 / 3.2, heightSeed)
local moistureMap = math.noise(x / resolution * 4 / 5.6, z / resolution * 4 / 5.6, moistureSeed)
local heatMap = math.noise(x / resolution * 2.7 / 7, z / resolution * 2.7 / 7, heatSeed)]]
grid[x][z] = {
["Height"] = math.clamp(heightMap, -0.5, 0.5) + 0.5,
["Moisture"] = math.clamp(moistureMap, -0.5, 0.5) + 0.5,
["Heat"] = math.clamp(heatMap, -0.5, 0.5) + 0.5,
}
end
end
-- Generate blocks
for x = 1, mapSize do
for z = 1, mapSize do
-- Get all available biomes for this block
local availableBiomes = {}
for name, value in pairs(biomeInfo) do
local condition = grid[x][z]["Height"] >= biomeInfo[name]["Height"] and grid[x][z]["Moisture"] >= biomeInfo[name]["Moisture"] and grid[x][z]["Heat"] >= biomeInfo[name]["Heat"]
if condition and not availableBiomes[name] then
table.insert(availableBiomes, name)
end
end
-- Calculate value differences for all available biomes for this block
local valueDiffs = {}
for _, biome in pairs(availableBiomes) do
valueDiffs[biome] = (grid[x][z]["Height"] - biomeInfo[biome]["Height"]) + (grid[x][z]["Moisture"] - biomeInfo[biome]["Moisture"]) + (grid[x][z]["Heat"] - biomeInfo[biome]["Heat"])
end
-- Get the lowest value difference, assign the corresponding biome
local lowestValue = 1
local selectedBiome
for biome, value in pairs(valueDiffs) do
if value < lowestValue then
lowestValue = value
selectedBiome = biome
end
end
-- Generate the block
local block = Instance.new("Part")
block.Anchored = true
block.Size = Vector3.new(8, 8, 8)
block.Position = Vector3.new(x * 8, 0, z * 8)
block.Parent = game.Workspace.World
if grid[x][z]["Height"] <= 0.2 then -- Water level
block.BrickColor = BrickColor.new("Electric blue")
elseif selectedBiome == "Grasslands" then
block.BrickColor = BrickColor.new("Bright green")
elseif selectedBiome == "Taiga" then
block.BrickColor = BrickColor.new("Baby blue")
elseif selectedBiome == "Desert" then
block.BrickColor = BrickColor.new("Cool yellow")
elseif selectedBiome == "Swamp" then
block.BrickColor = BrickColor.new("Slime green")
elseif selectedBiome == "Mountains" then
block.BrickColor = BrickColor.new("Dark stone grey")
elseif selectedBiome == "Jungle" then
block.BrickColor = BrickColor.new("Earth green")
elseif selectedBiome == "Snowy tundra" then
block.BrickColor = BrickColor.new("White")
else
block.BrickColor = BrickColor.new("Bright green")
end
block.Position = Vector3.new(x * 8, 4.5, z * 8)
block.Parent = game.Workspace.World
end
game:GetService("RunService").Heartbeat:Wait()
end
Note: I'm aware that the many elseif statements is inefficient, this is temporary, please ignore it.
The "BiomeInfo" module script:
return {
["Grasslands"] = {
["Height"] = 0.27,
["Moisture"] = 0.21,
["Heat"] = 0.25
},
["Desert"] = {
["Height"] = 0.15,
["Moisture"] = 0.05,
["Heat"] = 0.49
},
["Taiga"] = {
["Height"] = 0.28,
["Moisture"] = 0.16,
["Heat"] = 0.12
},
["Swamp"] = {
["Height"] = 0.16,
["Moisture"] = 0.44,
["Heat"] = 0.14
},
["Mountains"] = {
["Height"] = 0.43,
["Moisture"] = 0.08,
["Heat"] = 0.11
},
["Jungle"] = {
["Height"] = 0.22,
["Moisture"] = 0.51,
["Heat"] = 0.31
},
["Snowy tundra"] = {
["Height"] = 0.17,
["Moisture"] = 0.12,
["Heat"] = 0.03
},
}
This is my first time working with procedural generation, so I'm not sure if I'm applying the most efficient methods for everything.

Why macro F1 measure can't be calculated from macro precision and recall?

I'm interested in calculating macro f1-score by macro precision and recall manually. But the results aren't equal. What is the difference in the final formula between f1 and f1_new in code?
from sklearn.metrics import precision_score, recall_score, f1_score
y_true = [0, 1, 0, 1, 0 , 1, 1, 0]
y_pred = [0, 1, 0, 0, 1 , 1, 0, 0]
p = precision_score(y_true, y_pred, average='macro')
r = recall_score(y_true, y_pred, average='macro')
f1_new = (2 * p * r) / (p + r) # 0.6291390728476821
f1 = f1_score(y_true, y_pred, average='macro') # 0.6190476190476191
print(f1_new == f1)
# False
The f1_score is calculated in scikit-learn as follows:
all_positives = 4
all_negatives = 4
true_positives = 2
true_negatives = 3
true_positive_rate = true_positives/all_positives = 2/4
true_negative_rate = true_negatives/all_negatives = 3/4
pred_positives = 3
pred_negatives = 5
positive_predicted_value = true_positives/pred_positives = 2/3
negative_predicted_value = true_negatives/pred_negatives = 3/5
f1_score_pos = 2 * true_positive_rate * positive_predicted_value / (true_positive_rate + positive_predicted_value)
= 2 * 2/4 * 2/3 / (2/4 + 2/3)
f1_score_neg = 2 * true_negative_rate * negative_predicted_value / (true_negative_rate + negative_predicted_value)
= 2 * 3/4 * 3/5 / (3/4 + 3/5)
f1 = average(f1_score_pos, f1_score_neg)
= 2/4 * 2/3 / (2/4 + 2/3) + 3/4 * 3/5 / (3/4 + 3/5)
= 0.6190476190476191
This matches the definition given in the documentation for the 'macro' parameter of skicit-learn's f1_score: Calculate metrics for each label, and find their unweighted mean. This definition also applies to precision_score and recall_score.
Your manual calculation of the F1-score is as follows:
precision = average(positive_predicted_value, negative_predicted_value)
= average(2/3, 3/5)
= 19/30
recall = average(true_positive_rate, true_negative_rate)
= average(2/4, 3/4)
= 5/8
f1_new = 2 * precision * recall / (precision + recall)
= 2 * 19/30 * 5/8 / (19/30 + 5/8)
= 0.6291390728476821
In fact, the general formula F1 = 2 * (precision * recall) / (precision + recall) as presented in the docs is only valid for average='binary' and average='micro', but not for average='macro' and average='weighted'. In that sense, as it is currently presented in scikit-learn, the formula is misleading as it suggests that it holds irrespective of the chosen parameters, which is not the case.

The objective is not DCP. Its following subexpressions are not

Here I use the cvxpy solver to solve a problem. But the Problem does not follow DCP rules.
The objective is
import cvxpy as cp
import numpy as np
def bit_rate(alpha, beta, p, w):
return alpha * w * cp.log(1 + beta * p / w)
# Create scalar optimization variables.
p_1 = cp.Variable()
p_2 = cp.Variable()
p_3 = cp.Variable()
w_1 = cp.Variable()
w_2 = cp.Variable()
w_3 = cp.Variable()
r_1 = bit_rate(2, 2, p_1, w_1)
r_2 = bit_rate(2.4, 2.4, p_2, w_2)
r_3 = bit_rate(2.8, 2.8, p_3, w_3)
# Create constraints.
constraints = [p_1 + p_2 + p_3 == 0.5,
w_1 + w_2 + w_3 == 1,
p_1 >= 0, p_2 >= 0, p_3 >= 0,
w_1 >= 0, w_2 >= 0, w_3 >= 0]
# Form objective.
obj = cp.Maximize(r_1 + r_2 + r_3)
# Form and solve problem.
prob = cp.Problem(obj, constraints)
prob.solve() # Returns the optimal value.
print("status:", prob.status)
print("optimal value", prob.value)
print("p optimal var", p_1.value, p_2.value, p_3.value)
print("W optimal var", w_1.value, w_2.value, w_3.value)
The p and w are variables. I wanna ask how to convert the problem to be a DCP problem?THANKS!
The bug is
The objective is not DCP. Its following subexpressions are not:
2.0 * var0 / var3
2.4 * var1 / var4
2.8 * var2 / var5

Spline with multiple durations

I'm trying to make a spline that has multiple durations. Basically, I want to create a spline generated through user supplied key frames. There may be multiple key frames with different durations. So I ask, how would I make a that travels at different speeds through different key frames. Let's say I want there to be 1 second between key frame A and B, but 5 seconds between B and C. Similar to the way animation editors like Source Filmmaker, or Autodesk Maya do it. Every time I've seen someone make a spline, it always has one, constant speed. Always X seconds between key frames, but this is not how animation editors work, as they have different speeds, that's what I want.
Just to note, I have tried. I made the duration change once it gets to a different key frame, but that just instantly slows it down, like some slow motion movie effect, which is not what I'm looking for. Do I just gradually transition the speed to fit that of the next key frame? Is there just some equation?
function smooth( points, steps ) --points is an array, steps is how many frames inbetween spline points
if #points < 3 then
return points
end
local steps = steps or 5
local spline = {}
local count = #points - 1
local p0, p1, p2, p3, x, y, z
for i = 1, count do
if i == 1 then
p0, p1, p2, p3 = points[i], points[i], points[i + 1], points[i + 2]
elseif i == count then
p0, p1, p2, p3 = points[#points - 2], points[#points - 1], points[#points], points[#points]
else
p0, p1, p2, p3 = points[i - 1], points[i], points[i + 1], points[i + 2]
end
for t = 0, 1, 1 / steps do
-- Main spline equation
x = 1 * ( ( 2 * p1.x ) + ( p2.x - p0.x ) * t + ( 2 * p0.x - 5 * p1.x + 4 * p2.x - p3.x ) * t * t + ( 3 * p1.x - p0.x - 3 * p2.x + p3.x ) * t * t * t )
y = 1 * ( ( 2 * p1.y ) + ( p2.y - p0.y ) * t + ( 2 * p0.y - 5 * p1.y + 4 * p2.y - p3.y ) * t * t + ( 3 * p1.y - p0.y - 3 * p2.y + p3.y ) * t * t * t )
z = 1 * ( ( 2 * p1.z ) + ( p2.z - p0.z ) * t + ( 2 * p0.z - 5 * p1.z + 4 * p2.z - p3.z ) * t * t + ( 3 * p1.z - p0.z - 3 * p2.z + p3.z ) * t * t * t )
if not(#spline > 0 and spline[#spline].x == x and spline[#spline].y == y and spline[#spline].z == z) then
table.insert( spline , { x = x , y = y, z = z } )
end
end
end
return spline
end
Straightforward approach was used:
local zero_vector = {0, 0, 0}
local function get_slope(is_edge, left, right)
if is_edge then
return zero_vector
else
local t = right.time - left.time
assert(t > 0, "Non-increasing time sequence")
return {(right[1] - left[1])/t,
(right[2] - left[2])/t,
(right[3] - left[3])/t}
end
end
function smooth(checkpoints, frames_per_second)
frames_per_second = frames_per_second or 5
if #checkpoints < 2 then
return checkpoints
end
-- Prepare formulas for each segment of spline
local formulas = {}
for segment = 1, #checkpoints - 1 do
local left = checkpoints[segment]
local right = checkpoints[segment + 1]
local t = right.time - left.time
assert(t > 0, "Non-increasing time sequence")
local left_deriv = get_slope(segment == 1,
checkpoints[segment - 1], right)
local right_deriv = get_slope(segment == #checkpoints - 1,
left, checkpoints[segment + 2])
formulas[segment] = {}
for j = 1, 3 do
local d = left[j]
local c = left_deriv[j]
local a = (right[j] - d - c*t) / (t*t)
local b = 3*a + (c - right_deriv[j])/t
formulas[segment][j] = {(a - b)/t, b, c, d}
end
end
-- Calculate spline points
local total_seconds = checkpoints[#checkpoints].time - checkpoints[1].time
local segment = 1
local spline = {}
for frame_no = 0, total_seconds * frames_per_second do
local t = checkpoints[1].time + frame_no / frames_per_second
local point = {time = t}
while segment < #formulas and t > checkpoints[segment + 1].time do
segment = segment + 1
end
t = t - checkpoints[segment].time
for j = 1, 3 do
local c = formulas[segment][j]
point[j] = ((c[1]*t + c[2])*t + c[3])*t + c[4]
end
table.insert(spline, point)
end
return spline
end
Usage example:
-- x y z "timestamp in seconds"
local checkpoint_A = {11, 12, 13, time = 0}
local checkpoint_B = {21, 15, 18, time = 1} -- 1 second between A and B
local checkpoint_C = {13, 24, 20, time = 6} -- 5 seconds between B and C
local checkpoints = {checkpoint_A, checkpoint_B, checkpoint_C}
-- total duration is 6 seconds, 10 frames per second, 61 points returned
local array_of_61_points = smooth(checkpoints, 10)
for _, point in ipairs(array_of_61_points) do
print(string.format("time = %.1f, x = %.3f, y = %.3f, z = %.3f",
point.time, point[1], point[2], point[3]))
end

Resources