激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

腳本之家,腳本語(yǔ)言編程技術(shù)及教程分享平臺(tái)!
分類導(dǎo)航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務(wù)器之家 - 腳本之家 - Python - 用TensorFlow實(shí)現(xiàn)戴明回歸算法的示例

用TensorFlow實(shí)現(xiàn)戴明回歸算法的示例

2021-02-08 00:54lilongsy Python

這篇文章主要介紹了用TensorFlow實(shí)現(xiàn)戴明回歸算法的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

如果最小二乘線性回歸算法最小化到回歸直線的豎直距離(即,平行于y軸方向),則戴明回歸最小化到回歸直線的總距離(即,垂直于回歸直線)。其最小化x值和y值兩個(gè)方向的誤差,具體的對(duì)比圖如下圖。

用TensorFlow實(shí)現(xiàn)戴明回歸算法的示例

線性回歸算法和戴明回歸算法的區(qū)別。左邊的線性回歸最小化到回歸直線的豎直距離;右邊的戴明回歸最小化到回歸直線的總距離。

線性回歸算法的損失函數(shù)最小化豎直距離;而這里需要最小化總距離。給定直線的斜率和截距,則求解一個(gè)點(diǎn)到直線的垂直距離有已知的幾何公式。代入幾何公式并使tensorflow最小化距離。

損失函數(shù)是由分子和分母組成的幾何公式。給定直線y=mx+b,點(diǎn)(x0,y0),則求兩者間的距離的公式為:

用TensorFlow實(shí)現(xiàn)戴明回歸算法的示例

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# 戴明回歸
#----------------------------------
#
# this function shows how to use tensorflow to
# solve linear deming regression.
# y = ax + b
#
# we will use the iris data, specifically:
# y = sepal length
# x = petal width
 
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
from sklearn import datasets
from tensorflow.python.framework import ops
ops.reset_default_graph()
 
# create graph
sess = tf.session()
 
# load the data
# iris.data = [(sepal length, sepal width, petal length, petal width)]
iris = datasets.load_iris()
x_vals = np.array([x[3] for x in iris.data])
y_vals = np.array([y[0] for y in iris.data])
 
# declare batch size
batch_size = 50
 
# initialize placeholders
x_data = tf.placeholder(shape=[none, 1], dtype=tf.float32)
y_target = tf.placeholder(shape=[none, 1], dtype=tf.float32)
 
# create variables for linear regression
a = tf.variable(tf.random_normal(shape=[1,1]))
b = tf.variable(tf.random_normal(shape=[1,1]))
 
# declare model operations
model_output = tf.add(tf.matmul(x_data, a), b)
 
# declare demming loss function
demming_numerator = tf.abs(tf.subtract(y_target, tf.add(tf.matmul(x_data, a), b)))
demming_denominator = tf.sqrt(tf.add(tf.square(a),1))
loss = tf.reduce_mean(tf.truediv(demming_numerator, demming_denominator))
 
# declare optimizer
my_opt = tf.train.gradientdescentoptimizer(0.1)
train_step = my_opt.minimize(loss)
 
# initialize variables
init = tf.global_variables_initializer()
sess.run(init)
 
# training loop
loss_vec = []
for i in range(250):
  rand_index = np.random.choice(len(x_vals), size=batch_size)
  rand_x = np.transpose([x_vals[rand_index]])
  rand_y = np.transpose([y_vals[rand_index]])
  sess.run(train_step, feed_dict={x_data: rand_x, y_target: rand_y})
  temp_loss = sess.run(loss, feed_dict={x_data: rand_x, y_target: rand_y})
  loss_vec.append(temp_loss)
  if (i+1)%50==0:
    print('step #' + str(i+1) + ' a = ' + str(sess.run(a)) + ' b = ' + str(sess.run(b)))
    print('loss = ' + str(temp_loss))
 
# get the optimal coefficients
[slope] = sess.run(a)
[y_intercept] = sess.run(b)
 
# get best fit line
best_fit = []
for i in x_vals:
 best_fit.append(slope*i+y_intercept)
 
# plot the result
plt.plot(x_vals, y_vals, 'o', label='data points')
plt.plot(x_vals, best_fit, 'r-', label='best fit line', linewidth=3)
plt.legend(loc='upper left')
plt.title('sepal length vs pedal width')
plt.xlabel('pedal width')
plt.ylabel('sepal length')
plt.show()
 
# plot loss over time
plt.plot(loss_vec, 'k-')
plt.title('l2 loss per generation')
plt.xlabel('generation')
plt.ylabel('l2 loss')
plt.show()

結(jié)果:

用TensorFlow實(shí)現(xiàn)戴明回歸算法的示例

用TensorFlow實(shí)現(xiàn)戴明回歸算法的示例

本文的戴明回歸算法與線性回歸算法得到的結(jié)果基本一致。兩者之間的關(guān)鍵不同點(diǎn)在于預(yù)測(cè)值與數(shù)據(jù)點(diǎn)間的損失函數(shù)度量:線性回歸算法的損失函數(shù)是豎直距離損失;而戴明回歸算法是垂直距離損失(到x軸和y軸的總距離損失)。

注意,這里戴明回歸算法的實(shí)現(xiàn)類型是總體回歸(總的最小二乘法誤差)。總體回歸算法是假設(shè)x值和y值的誤差是相似的。我們也可以根據(jù)不同的理念使用不同的誤差來(lái)擴(kuò)展x軸和y軸的距離計(jì)算。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://blog.csdn.net/lilongsy/article/details/79363189

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25
主站蜘蛛池模板: 黄色一级片免费在线观看 | 毛片午夜 | av影院在线播放 | 欧美wwwwww| 日本娇小videos高潮 | 欧美久久久久久久久 | 99在线精品视频免费观看20 | 亚洲免费视 | 少妇一级淫片免费看 | 亚洲福利在线视频 | 国产91精品一区二区麻豆亚洲 | 欧美片一区二区 | 老师你怎么会在这第2季出现 | 国产影视| 欧美一区二区三区免费电影 | 黄色片网页 | 亚洲码无人客一区二区三区 | 一级视频在线播放 | 亚洲成人第一区 | 一边吃奶一边插下面 | 中国国语毛片免费观看视频 | 国产精品色综合 | 精品成人免费一区二区在线播放 | 国产精品成人免费一区久久羞羞 | 欧美成人一区二区三区电影 | 成人免费一区二区三区视频网站 | h视频免费观看 | 国产亚洲自拍一区 | 免费久久久久 | 国产自91精品一区二区 | 久久99国产综合精品 | 亚洲精品无码不卡在线播放he | 成人18免费观看 | 福利在线国产 | 久久亚洲精品视频 | 国产一级桃视频播放 | 成人h精品动漫一区二区三区 | 精品三级内地国产在线观看 | 亚洲精品有限 | 天天草天天干天天射 | 羞羞的视频免费观看 |