由淺入深之Tensorflow(1)----linear_regression實現


  Tensorflow是目前非常流行的deeplearning框架,學習Tensorflow最好的方法是github上的tf項目https://github.com/tensorflow/tensorflow

或者閱讀極客學院主導翻譯的中文教程http://wiki.jikexueyuan.com/project/tensorflow-zh/how_tos/reading_data.html 。

  此處對tensorflow的基本語法不予贅述,直接貼上源碼:

import numpy as np
import tensorflow as tf

#准備數據 trainX
= np.linspace(-1, 1, 101) trainY = 2 * trainX + np.random.randn(*trainX.shape) * 0.33
#定義模型
def model(X, w): return tf.mul(X, w)
#初始化數據流圖 X
= tf.placeholder('float') Y = tf.placeholder('float') w = tf.Variable(0.0, name = 'weights') y_ = model(X, w)

#評估模型 cost
= tf.square(Y - y_) train_op = tf.train.GradientDescentOptimizer(0.01).minimize(cost) sess = tf.InteractiveSession() init = tf.initialize_all_variables() #訓練 sess.run(init) for i in range(100): for (x, y) in zip(trainX, trainY): sess.run(train_op, feed_dict = {X: x, Y: y}) print sess.run(w) sess.close()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM