tensorflow 戴明線性迴歸

損失函數能夠有兩種 1.點到直線的豎直距離 2.點到直線的垂直距離 import tensorflow as tf import matplotlib.pyplot as plt import numpy as np from sklearn import datasetsapp

sess = tf.Session()dom

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])code

learn_rate = 0.05 batchsize = 50 x_data = tf.placeholder(shape=[None,1],dtype=tf.float32) y_target = tf.placeholder(shape=[None,1],dtype=tf.float32)orm

A = tf.Variable(tf.random_normal(shape=[1,1])) b = tf.Variable(tf.random_normal(shape=[1,1])) demming_upper = tf.abs(tf.subtract(y_target,tf.add(tf.matmul(x_data,A),b))) demming_down = tf.sqrt(tf.add(tf.square(A),1))get

loss = tf.reduce_mean(tf.truediv(demming_upper,demming_down))it

init = tf.global_variables_initializer() sess.run(init)io

my_opt = tf.train.GradientDescentOptimizer(0.1) train_step = my_opt.minimize(loss)import

loss_dev = []tensorflow

for i in range(250): rand_index = np.random.choice(len(x_vals), size=batchsize) 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_dev.append(temp_loss)

[slope] = sess.run(A) [intercept] = sess.run(b)

best_line = [] for x in x_vals: best_line.append(slope*x+intercept)

plt.plot(x_vals,y_vals,'bo',label = 'data') plt.plot(x_vals,best_line,'r--',label = 'best line') plt.legend(loc = 'upper right') plt.show()

plt.plot(loss_dev) plt.show()

相關文章
相關標籤/搜索