用tensorflow實現(xiàn)彈性網(wǎng)絡(luò)回歸算法
更新時間:2018年01月09日 08:39:17 作者:xckkcxxck
這篇文章主要介紹了用tensorflow實現(xiàn)彈性網(wǎng)絡(luò)回歸算法
本文實例為大家分享了tensorflow實現(xiàn)彈性網(wǎng)絡(luò)回歸算法,供大家參考,具體內(nèi)容如下
python代碼:
#用tensorflow實現(xiàn)彈性網(wǎng)絡(luò)算法(多變量)
#使用鳶尾花數(shù)據(jù)集,后三個特征作為特征,用來預(yù)測第一個特征。
#1 導(dǎo)入必要的編程庫,創(chuàng)建計算圖,加載數(shù)據(jù)集
import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
from sklearn import datasets
from tensorflow.python.framework import ops
ops.get_default_graph()
sess = tf.Session()
iris = datasets.load_iris()
x_vals = np.array([[x[1], x[2], x[3]] for x in iris.data])
y_vals = np.array([y[0] for y in iris.data])
#2 聲明學(xué)習(xí)率,批量大小,占位符和模型變量,模型輸出
learning_rate = 0.001
batch_size = 50
x_data = tf.placeholder(shape=[None, 3], dtype=tf.float32) #占位符大小為3
y_target = tf.placeholder(shape=[None, 1], dtype=tf.float32)
A = tf.Variable(tf.random_normal(shape=[3,1]))
b = tf.Variable(tf.random_normal(shape=[1,1]))
model_output = tf.add(tf.matmul(x_data, A), b)
#3 對于彈性網(wǎng)絡(luò)回歸算法,損失函數(shù)包括L1正則和L2正則
elastic_param1 = tf.constant(1.)
elastic_param2 = tf.constant(1.)
l1_a_loss = tf.reduce_mean(abs(A))
l2_a_loss = tf.reduce_mean(tf.square(A))
e1_term = tf.multiply(elastic_param1, l1_a_loss)
e2_term = tf.multiply(elastic_param2, l2_a_loss)
loss = tf.expand_dims(tf.add(tf.add(tf.reduce_mean(tf.square(y_target - model_output)), e1_term), e2_term), 0)
#4 初始化變量, 聲明優(yōu)化器, 然后遍歷迭代運行, 訓(xùn)練擬合得到參數(shù)
init = tf.global_variables_initializer()
sess.run(init)
my_opt = tf.train.GradientDescentOptimizer(learning_rate)
train_step = my_opt.minimize(loss)
loss_vec = []
for i in range(1000):
rand_index = np.random.choice(len(x_vals), size=batch_size)
rand_x = 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)%250 == 0:
print('Step#' + str(i+1) +'A = ' + str(sess.run(A)) + 'b=' + str(sess.run(b)))
print('Loss= ' +str(temp_loss))
#現(xiàn)在能觀察到, 隨著訓(xùn)練迭代后損失函數(shù)已收斂。
plt.plot(loss_vec, 'k--')
plt.title('Loss per Generation')
plt.xlabel('Generation')
plt.ylabel('Loss')
plt.show()
本文參考書《Tensorflow機(jī)器學(xué)習(xí)實戰(zhàn)指南》
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- tensorflow實現(xiàn)簡單邏輯回歸
- Tensorflow使用支持向量機(jī)擬合線性回歸
- TensorFlow實現(xiàn)iris數(shù)據(jù)集線性回歸
- 用TensorFlow實現(xiàn)戴明回歸算法的示例
- 用TensorFlow實現(xiàn)lasso回歸和嶺回歸算法的示例
- 詳解用TensorFlow實現(xiàn)邏輯回歸算法
- TensorFlow實現(xiàn)Softmax回歸模型
- 運用TensorFlow進(jìn)行簡單實現(xiàn)線性回歸、梯度下降示例
- 用tensorflow構(gòu)建線性回歸模型的示例代碼
- TensorFlow實現(xiàn)Logistic回歸
相關(guān)文章
在NumPy中創(chuàng)建空數(shù)組/矩陣的方法
今天小編就為大家分享一篇在NumPy中創(chuàng)建空數(shù)組/矩陣的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06
Python基于pygame實現(xiàn)圖片代替鼠標(biāo)移動效果
這篇文章主要介紹了Python基于pygame實現(xiàn)圖片代替鼠標(biāo)移動效果,可實現(xiàn)將鼠標(biāo)箭頭轉(zhuǎn)換成圖形的功能,涉及pygame圖形操作的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-11-11
公眾號接入chatGPT的詳細(xì)教程 附Python源碼
這篇文章主要介紹了公眾號接入chatGPT教程附Python源碼,這里需要大家準(zhǔn)備一個域名,一臺服務(wù)器和一個公眾號,本文結(jié)合實例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02
python利用有道翻譯實現(xiàn)"語言翻譯器"的功能實例
小編就為大家分享一篇python利用有道翻譯實現(xiàn)"語言翻譯器"的功能實例。具有比較好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-11-11

