Gradient Descent Sphere Fitting

1 Introduction

A long time ago, there was a small problem in the second question of Question A of the school digital simulation school competition , which was based on a given point to find the volume. Because the title was described as a near sphere, sphere fitting can be done. At that time, I happened to be learning about deep learning, so I had this gradient descent method for sphere fitting.

2. Text

2.1 loss function loss-function

The theoretical value is: set the center and radius of the sphere as (a, b, c, R) (interpretation: a, b, c are x, y, z coordinates respectively. The objective function is designed as: L = ∑ i = 1 n [
(
xi − a ) 2 + ( yi − b ) 2 + ( zi − c ) 2 − R 2 ] 2 L=\sum_{i=1}^n[(x_i−a)^2+(y_i−b)^2 +(z_i−c)^2−R^2]^2L=i=1n[(xia)2+(yib)2+(zic)2R2]2

2.2 Partial derivative

∂ L ∂ a = − 4 ∑ i = 1 n ( x i − a ) [ ( x i − a ) 2 + ( y i − b ) 2 + ( z i − c ) 2 − R 2 ] \frac{∂L}{∂a}=−4\sum_{i=1}^n(x_i-a)[(x_i−a)^2+(y_i−b)^2+(z_i−c)^2−R^2] aL=4i=1n(xia)[(xia)2+(yib)2+(zic)2R2]
∂ L ∂ b = − 4 ∑ i = 1 n ( y i − b ) [ ( x i − a ) 2 + ( y i − b ) 2 + ( z i − c ) 2 − R 2 ] \frac{∂L}{∂b}=−4\sum_{i=1}^n(y_i-b)[(x_i−a)^2+(y_i−b)^2+(z_i−c)^2−R^2] bL=4i=1n(yib)[(xia)2+(yib)2+(zic)2R2]
∂ L ∂ c = − 4 ∑ i = 1 n ( z i − c ) [ ( x i − a ) 2 + ( y i − b ) 2 + ( z i − c ) 2 − R 2 ] \frac{∂L}{∂c}=−4\sum_{i=1}^n(z_i-c)[(x_i−a)^2+(y_i−b)^2+(z_i−c)^2−R^2] cL=4i=1n(zic)[(xia)2+(yib)2+(zic)2R2]
∂ L ∂ R = − 4 ∑ i = 1 n R [ ( x i − a ) 2 + ( y i − b ) 2 + ( z i − c ) 2 − R 2 ] \frac{∂L}{∂R}=−4\sum_{i=1}^nR[(x_i−a)^2+(y_i−b)^2+(z_i−c)^2−R^2] RL=4i=1nR[(xia)2+(yib)2+(zic)2R2]

2.3 Gradient descent (backpropagation)

Use the gradient descent method to update (a,b,c,R) after each iteration

lr representative learning rate
ai + 1 = ai − lr ∗ ∂ L ∂ a a_{i+1}=a_{i}-lr*\frac{∂L}{∂a}ai+1=ailraL
b i + 1 = b i − l r ∗ ∂ L ∂ b b_{i+1}=b_{i}-lr*\frac{∂L}{∂b} bi+1=bilrbL
c i + 1 = c i − l r ∗ ∂ L ∂ c c_{i+1}=c_{i}-lr*\frac{∂L}{∂c} ci+1=cilrcL
R i + 1 = R i − l r ∗ ∂ L ∂ R R_{i+1}=R_{i}-lr*\frac{∂L}{∂R} Ri+1=RilrRL

2.4 Hyperparameters

Batch size (batch_size)
learning rate (learning_rate)
training algebra (epoch_num)

2.5 Key pseudocode

# 对迭代次数做循环
for epoch in range(num_epochs):
    #对数据集按batch_size做访问
    for i in range(batch):
        #分批取出batch大小的数据
        x,y,z=data(i)
        
        # X和y的每个批损失(在L()中做平均可以避免loss损失值太大的问题
        l = L(x,y,z,a,b,c,R)
        
        # 反向传播,更新目标参数
        #lr是学习率
        a = a - lr * (dL/dx)
        b = b - lr * (dL/dy)
        c = c - lr * (dL/dz)
        R = R - lr * (dL/dR)

2.6 Other ideas

2.6.1 Fitting a sphere by the least squares method

2.6.2 Volume of integrating sphere

To find the volume of an irregular object, you can use triple integral
V = ∭ Ω f ( x , y , z ) dv = ∭ Ω f ( x , y , z ) dxdydz V=∭_\Omega f(x,y,z )dv=∭_\Omega f(x,y,z)dxdydzV=Ohf(x,y,z)dv=Ohf(x,y,z ) d x d y d z
There are two ways to calculate the triple integral: "first one and then two", "first two and then one"

Among them, the first two and the first one are the area integral, and then the high integral. According to the idea of ​​differentiation, take a small dz and then take the plane according to the interval of dz for the range of z, and traverse each surface (area*dz rough estimate is the volume of the segment), what needs to be considered is the error involved in this method.
V = ∭ Ω f ( x , y , z ) dv = ∫ dz ∫ f ( x , y , z ) dxdy V=∭_\Omega f(x,y,z)dv=\int dz\int f(x ,y,z)dxdyV=Ohf(x,y,z)dv=dzf(x,y,z)dxdy

2.6.3 Triangulation to find volume

3. Epilogue

Only sharing my own thoughts, comments and pointers are very grateful

Guess you like

Origin blog.csdn.net/qq_51204877/article/details/125494649