国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

TensorFlow實現iris數據集線性回歸

2020-01-04 14:34:15
字體:
來源:轉載
供稿:網友

本文將遍歷批量數據點并讓TensorFlow更新斜率和y截距。這次將使用Scikit Learn的內建iris數據集。特別地,我們將用數據點(x值代表花瓣寬度,y值代表花瓣長度)找到最優直線。選擇這兩種特征是因為它們具有線性關系,在后續結果中將會看到。本文將使用L2正則損失函數。

 

# 用TensorFlow實現線性回歸算法#----------------------------------## This function shows how to use TensorFlow to# solve linear regression.# y = Ax + b## We will use the iris data, specifically:# y = Sepal Length# x = Petal Widthimport matplotlib.pyplot as pltimport numpy as npimport tensorflow as tffrom sklearn import datasetsfrom tensorflow.python.framework import opsops.reset_default_graph()# Create graphsess = 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])# 批量大小batch_size = 25# Initialize 占位符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]))# 增加線性模型,y=Ax+bmodel_output = tf.add(tf.matmul(x_data, A), b)# 聲明L2損失函數,其為批量損失的平均值。loss = tf.reduce_mean(tf.square(y_target - model_output))# 聲明優化器 學習率設為0.05my_opt = tf.train.GradientDescentOptimizer(0.05)train_step = my_opt.minimize(loss)# 初始化變量init = tf.global_variables_initializer()sess.run(init)# 批量訓練遍歷迭代# 迭代100次,每25次迭代輸出變量值和損失值loss_vec = []for i in range(100):  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)%25==0:    print('Step #' + str(i+1) + ' A = ' + str(sess.run(A)) + ' b = ' + str(sess.run(b)))    print('Loss = ' + str(temp_loss))# 抽取系數[slope] = sess.run(A)[y_intercept] = sess.run(b)# 創建最佳擬合直線best_fit = []for i in x_vals: best_fit.append(slope*i+y_intercept)# 繪制兩幅圖# 擬合的直線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# 迭代100次的L2正則損失函數plt.plot(loss_vec, 'k-')plt.title('L2 Loss per Generation')plt.xlabel('Generation')plt.ylabel('L2 Loss')plt.show()

結果:

Step #25 A = [[ 1.93474162]] b = [[ 3.11190438]]Loss = 1.21364Step #50 A = [[ 1.48641717]] b = [[ 3.81004381]]Loss = 0.945256Step #75 A = [[ 1.26089203]] b = [[ 4.221035]]Loss = 0.254756Step #100 A = [[ 1.1693294]] b = [[ 4.47258472]]Loss = 0.281654

TensorFlow,iris,數據集,線性回歸

TensorFlow,iris,數據集,線性回歸

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到python教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 商城县| 武邑县| 思南县| 宁武县| 多伦县| 中卫市| 如皋市| 沁阳市| 英德市| 南城县| 福建省| 康保县| 武威市| 利津县| 乾安县| 个旧市| 东光县| 光泽县| 南丹县| 涞水县| 漳平市| 同江市| 余江县| 新化县| 大厂| 屯门区| 琼结县| 定边县| 九寨沟县| 晋宁县| 资阳市| 牟定县| 新昌县| 盐边县| 肥城市| 崇州市| 海丰县| 井研县| 新建县| 调兵山市| 项城市|