损失函数可以有两种 1.点到直线的竖直距离 2.点到直线的垂直距离 import tensorflow as tf import matplotlib.pyplot as plt import numpy as np from sklearn import datasets
sess = tf.Session()
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])
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)
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))
loss = tf.reduce_mean(tf.truediv(demming_upper,demming_down))
init = tf.global_variables_initializer() sess.run(init)
my_opt = tf.train.GradientDescentOptimizer(0.1) train_step = my_opt.minimize(loss)
loss_dev = []
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()