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

首頁 > 編程 > Python > 正文

TensorFlow實現隨機訓練和批量訓練的方法

2020-02-22 23:55:13
字體:
來源:轉載
供稿:網友

TensorFlow更新模型變量。它能一次操作一個數據點,也可以一次操作大量數據。一個訓練例子上的操作可能導致比較“古怪”的學習過程,但使用大批量的訓練會造成計算成本昂貴。到底選用哪種訓練類型對機器學習算法的收斂非常關鍵。

為了TensorFlow計算變量梯度來讓反向傳播工作,我們必須度量一個或者多個樣本的損失。

隨機訓練會一次隨機抽樣訓練數據和目標數據對完成訓練。另外一個可選項是,一次大批量訓練取平均損失來進行梯度計算,批量訓練大小可以一次上擴到整個數據集。這里將顯示如何擴展前面的回歸算法的例子——使用隨機訓練和批量訓練。

批量訓練和隨機訓練的不同之處在于它們的優化器方法和收斂。

# 隨機訓練和批量訓練#----------------------------------## This python function illustrates two different training methods:# batch and stochastic training. For each model, we will use# a regression model that predicts one model variable.import matplotlib.pyplot as pltimport numpy as npimport tensorflow as tffrom tensorflow.python.framework import opsops.reset_default_graph()# 隨機訓練:# Create graphsess = tf.Session()# 聲明數據x_vals = np.random.normal(1, 0.1, 100)y_vals = np.repeat(10., 100)x_data = tf.placeholder(shape=[1], dtype=tf.float32)y_target = tf.placeholder(shape=[1], dtype=tf.float32)# 聲明變量 (one model parameter = A)A = tf.Variable(tf.random_normal(shape=[1]))# 增加操作到圖my_output = tf.multiply(x_data, A)# 增加L2損失函數loss = tf.square(my_output - y_target)# 初始化變量init = tf.global_variables_initializer()sess.run(init)# 聲明優化器my_opt = tf.train.GradientDescentOptimizer(0.02)train_step = my_opt.minimize(loss)loss_stochastic = []# 運行迭代for i in range(100): rand_index = np.random.choice(100) rand_x = [x_vals[rand_index]] rand_y = [y_vals[rand_index]] sess.run(train_step, feed_dict={x_data: rand_x, y_target: rand_y}) if (i+1)%5==0:  print('Step #' + str(i+1) + ' A = ' + str(sess.run(A)))  temp_loss = sess.run(loss, feed_dict={x_data: rand_x, y_target: rand_y})  print('Loss = ' + str(temp_loss))  loss_stochastic.append(temp_loss)# 批量訓練:# 重置計算圖ops.reset_default_graph()sess = tf.Session()# 聲明批量大小# 批量大小是指通過計算圖一次傳入多少訓練數據batch_size = 20# 聲明模型的數據、占位符x_vals = np.random.normal(1, 0.1, 100)y_vals = np.repeat(10., 100)x_data = tf.placeholder(shape=[None, 1], dtype=tf.float32)y_target = tf.placeholder(shape=[None, 1], dtype=tf.float32)# 聲明變量 (one model parameter = A)A = tf.Variable(tf.random_normal(shape=[1,1]))# 增加矩陣乘法操作(矩陣乘法不滿足交換律)my_output = tf.matmul(x_data, A)# 增加損失函數# 批量訓練時損失函數是每個數據點L2損失的平均值loss = tf.reduce_mean(tf.square(my_output - y_target))# 初始化變量init = tf.global_variables_initializer()sess.run(init)# 聲明優化器my_opt = tf.train.GradientDescentOptimizer(0.02)train_step = my_opt.minimize(loss)loss_batch = []# 運行迭代for i in range(100): rand_index = np.random.choice(100, 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}) if (i+1)%5==0:  print('Step #' + str(i+1) + ' A = ' + str(sess.run(A)))  temp_loss = sess.run(loss, feed_dict={x_data: rand_x, y_target: rand_y})  print('Loss = ' + str(temp_loss))  loss_batch.append(temp_loss)plt.plot(range(0, 100, 5), loss_stochastic, 'b-', label='Stochastic Loss')plt.plot(range(0, 100, 5), loss_batch, 'r--', label='Batch Loss, size=20')plt.legend(loc='upper right', prop={'size': 11})plt.show()            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 榆中县| 平湖市| 定日县| 调兵山市| 楚雄市| 崇阳县| 彩票| 鄯善县| 华亭县| 邢台市| 兴和县| 怀安县| 安吉县| 普兰县| 屯昌县| 普兰县| 福建省| 清苑县| 云阳县| 平遥县| 金阳县| 桦甸市| 南靖县| 玛纳斯县| 汝州市| 永和县| 惠东县| 黄平县| 宣威市| 乡宁县| 葵青区| 舟曲县| 黄山市| 中阳县| 马山县| 梅河口市| 康乐县| 阳曲县| 怀集县| 师宗县| 晴隆县|