一個利用Tensorflow求解幾何問題的例子

知乎上有一個問題,內容是已知空間三個點的座標,求三個點所構成的圓的圓心座標(編程實現)?python

根據圓的定義,這道題的核心就是找到一個點,到已知的三個點的距離相等,利用數學知識能夠求解以下:算法

例如 :給定a(x1,y1) b(x2,y2) c(x3,y3)求外接圓心座標O(x,y)
1. 首先,外接圓的圓心是三角形三條邊的垂直平分線的交點,咱們根據圓心到頂點的距離相等,能夠列出如下方程:       
(x1-x)*(x1-x)+(y1-y)*(y1-y)=(x2-x)*(x2-x)+(y2-y)*(y2-y);       
(x2-x)*(x2-x)+(y2-y)*(y2-y)=(x3-x)*(x3-x)+(y3-y)*(y3-y);
2.化簡獲得:        
2*(x2-x1)*x+2*(y2-y1)y=x2^2+y2^2-x1^2-y1^2;        
2*(x3-x2)*x+2*(y3-y2)y=x3^2+y3^2-x2^2-y2^2;        
令:A1=2*(x2-x1);            
B1=2*(y2-y1);            
C1=x2^2+y2^2-x1^2-y1^2;            
A2=2*(x3-x2);            
B2=2*(y3-y2);            
C2=x3^2+y3^2-x2^2-y2^2;        
即:A1*x+B1y=C1;            
A2*x+B2y=C2;
3.最後根據克拉默法則:          
x=((C1*B2)-(C2*B1))/((A1*B2)-(A2*B1));          
y=((A1*C2)-(A2*C1))/((A1*B2)-(A2*B1));編程

固然,咱們今天不是來學習數學公式和數學推導的。Tensorflow是google開源的一款深度學習的工具,其實咱們能夠利用Tensoflow提供了強大的數學計算能力來求解相似的數學問題。bash

這道題,咱們能夠利用梯度降低算法,由於圓心是一個最優解,任何其它點都不滿條件。(前提是這三個點不在一條直線上,不然是沒有解的)session

好了,咱們先看代碼先,而後在解釋。機器學習

import tensorflow as tf
import numpy

# Parameters
learning_rate = 0.1
training_epochs = 3000
display_step = 50

# Training Data, 3 points that form a triangel
train_X = numpy.asarray([3.0,6.0,9.0])
train_Y = numpy.asarray([7.0,9.0,7.0])

# tf Graph Input
X = tf.placeholder("float")
Y = tf.placeholder("float")

# Set vaibale for center
cx = tf.Variable(3, name="cx",dtype=tf.float32)
cy = tf.Variable(3, name="cy",dtype=tf.float32)

# Caculate the distance to the center and make them as equal as possible
distance = tf.pow(tf.add(tf.pow((X-cx),2),tf.pow((Y-cy),2)),0.5)
mean = tf.reduce_mean(distance)
cost = tf.reduce_sum(tf.pow((distance-mean),2)/3)
# Gradient descent
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)

# Initialize the variables (i.e. assign their default value)
init = tf.global_variables_initializer()

# Start training
with tf.Session() as sess:
    sess.run(init)

    # Fit all training data
    for epoch in range(training_epochs):
        sess.run(optimizer, feed_dict={X: train_X, Y: train_Y})
        c = sess.run(cost, feed_dict={X: train_X, Y:train_Y})
        if (c - 0) < 0.0000000001:
            break
        #Display logs per epoch step
        if (epoch+1) % display_step == 0:
            c = sess.run(cost, feed_dict={X: train_X, Y:train_Y})
            m = sess.run(mean, feed_dict={X: train_X, Y:train_Y})
            print "Epoch:", '%04d' % (epoch+1), "cost=", "{:.9f}".format(c), \
                "CX=", sess.run(cx), "CY=", sess.run(cy), "Mean=", "{:.9f}".format(m)

    print "Optimization Finished!"
    training_cost = sess.run(cost, feed_dict={X: train_X, Y: train_Y})
    print "Training cost=", training_cost, "CX=", round(sess.run(cx),2), "CY=", round(sess.run(cy),2), "R=", round(m,2), '\n'

運行以上的python代碼,結果以下:函數

Epoch: 0050 cost= 0.290830940 CX= 5.5859795 CY= 2.6425467 Mean= 5.657848835
Epoch: 0100 cost= 0.217094064 CX= 5.963002 CY= 3.0613017 Mean= 5.280393124
Epoch: 0150 cost= 0.173767462 CX= 5.997781 CY= 3.5245996 Mean= 4.885882378
Epoch: 0200 cost= 0.126330480 CX= 5.9999194 CY= 4.011508 Mean= 4.485837936
Epoch: 0250 cost= 0.078660280 CX= 5.9999976 CY= 4.4997787 Mean= 4.103584766
Epoch: 0300 cost= 0.038911112 CX= 5.9999976 CY= 4.945466 Mean= 3.775567770
Epoch: 0350 cost= 0.014412695 CX= 5.999998 CY= 5.2943544 Mean= 3.535865068
Epoch: 0400 cost= 0.004034557 CX= 5.999998 CY= 5.5200934 Mean= 3.390078306
Epoch: 0450 cost= 0.000921754 CX= 5.999998 CY= 5.6429324 Mean= 3.314131498
Epoch: 0500 cost= 0.000187423 CX= 5.999998 CY= 5.7023263 Mean= 3.278312683
Epoch: 0550 cost= 0.000035973 CX= 5.999998 CY= 5.7292333 Mean= 3.262284517
Epoch: 0600 cost= 0.000006724 CX= 5.999998 CY= 5.7410445 Mean= 3.255288363
Epoch: 0650 cost= 0.000001243 CX= 5.999998 CY= 5.746154 Mean= 3.252269506
Epoch: 0700 cost= 0.000000229 CX= 5.999998 CY= 5.7483506 Mean= 3.250972748
Epoch: 0750 cost= 0.000000042 CX= 5.999998 CY= 5.749294 Mean= 3.250416517
Epoch: 0800 cost= 0.000000008 CX= 5.999998 CY= 5.749697 Mean= 3.250178576
Epoch: 0850 cost= 0.000000001 CX= 5.999998 CY= 5.749871 Mean= 3.250076294
Epoch: 0900 cost= 0.000000000 CX= 5.999998 CY= 5.7499437 Mean= 3.250033140
Optimization Finished!
Training cost= 9.8869656e-11 CX= 6.0 CY= 5.75 R= 3.25

通過900屢次的迭代,圓心位置是(6.0,5.75),半徑是3.25。工具

# Parameters
learning_rate = 0.1
training_epochs = 3000
display_step = 50
  • learning_rate 是梯度降低的速率,這個值越大,收斂的越快,但也有可能會錯過最優解
  • training_epochs是學習迭代的次數
  • display_step是每多少次迭代顯示當前的計算結果
# Training Data, 3 points that form a triangel
train_X = numpy.asarray([3.0,6.0,9.0])
train_Y = numpy.asarray([7.0,9.0,7.0])

# tf Graph Input
X = tf.placeholder("float")
Y = tf.placeholder("float")

# Set vaibale for center
cx = tf.Variable(3, name="cx",dtype=tf.float32)
cy = tf.Variable(3, name="cy",dtype=tf.float32)
  • train_X,train_Y是三個點的x,y座標,這裏咱們選了(3,7)(6,9)(9,7)三個點
  • X,Y是計算的輸入,在計算過程當中咱們會使用訓練數據輸入X,Y
  • cx,cy是咱們想要找的圓心點,初始值設置爲(3,3),通常的學習算法會使用隨機的初始值,這裏我選了三角形中的一個點,這樣作通常會減小迭代的次數。
# Caculate the distance to the center and make them as equal as possible
distance = tf.pow(tf.add(tf.pow((X-cx),2),tf.pow((Y-cy),2)),0.5)
mean = tf.reduce_mean(distance)
cost = tf.reduce_sum(tf.pow((distance-mean),2)/3)
# Gradient descent
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)

這幾行代碼是算法的核心。學習

  • distance是利用兩個點的距離公式算出三個點到圓心的距離
  • mean是三個距離的平均值
  • cost是三個距離的方差,咱們的目標是讓三個點到圓心的距離同樣,也就是方差最小(cx/cy爲圓心的時候,這個方差爲零)
  • optimizer是梯度降低的訓練函數,目標是使得cost(方差)最小

下面就是訓練的過程了:google

# Start training
with tf.Session() as sess:
    sess.run(init)

    # Fit all training data
    for epoch in range(training_epochs):
        sess.run(optimizer, feed_dict={X: train_X, Y: train_Y})
        c = sess.run(cost, feed_dict={X: train_X, Y:train_Y})
        if (c - 0) < 0.0000000001:
            break
        #Display logs per epoch step
        if (epoch+1) % display_step == 0:
            c = sess.run(cost, feed_dict={X: train_X, Y:train_Y})
            m = sess.run(mean, feed_dict={X: train_X, Y:train_Y})
            print "Epoch:", '%04d' % (epoch+1), "cost=", "{:.9f}".format(c), \
                "CX=", sess.run(cx), "CY=", sess.run(cy), "Mean=", "{:.9f}".format(m)

    print "Optimization Finished!"
    training_cost = sess.run(cost, feed_dict={X: train_X, Y: train_Y})
    print "Training cost=", training_cost, "CX=", round(sess.run(cx),2), "CY=", round(sess.run(cy),2), "R=", round(m,2), '\n'
  • 初始化tf的session
  • 開始迭代
  • 計算cost值,當cost小於必定的值的時候,推出迭代,說明咱們已經找到了圓心
  • 最後打印出訓練的結果

原題目是空間上的點,個人例子是平面上的點,其實沒有本質差異。能夠加一個Z軸的數據。這個題,三維實際上是多餘的,徹底能夠把空間上的三個點投影到平面上來解決。

我用JS實現過另外一個算法,可是不是總收斂。你們能夠參考着看一看:

其中,綠色三個點條件點,紅色的圓是最終的學習結果,黃色的中心點學習的軌跡。

利用這個例子,我想說的是:

  • Tensorflow不單單是一個深度學習的工具,它提供了強大的數據計算能力,能夠用於解決不少的數學問題
  • 機器學習的本質是經過一組數據來找到答案,這也是數學的做用,因此不少的數學問題均可以用機器學習的思路來解決。
相關文章
相關標籤/搜索