码迷,mamicode.com
首页 > 其他好文 > 详细

tensorflow版线性回归

时间:2020-01-31 19:04:29      阅读:75      评论:0      收藏:0      [点我收藏+]

标签:std   ini   oss   esc   __name__   orm   import   run   ntop   

import os
os.environ[‘TF_CPP_MIN_LOG_LEVEL‘] = ‘2‘

import tensorflow as tf

def linearregression():
    X = tf.random_normal([100,1],mean=0.0,stddev=1.0)
    y_true = tf.matmul(X,[[0.8]]) + [[0.7]]

    weights = tf.Variable(initial_value=tf.random_normal([1,1]))
    bias = tf.Variable(initial_value=tf.random_normal([1,1]))

    y_predict = tf.matmul(X,weights)+bias

    loss = tf.reduce_mean(tf.square(y_predict-y_true))
    optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01).minimize(loss)
    init = tf.global_variables_initializer()

    with tf.Session() as sess:
        sess.run(init)

        for i in range(1000):
            sess.run(optimizer)
            print("loss:", sess.run(loss))
            print("weight:", sess.run(weights))
            print("bias:", sess.run(bias))

if __name__ == ‘__main__‘:
    linearregression()

  

tensorflow版线性回归

标签:std   ini   oss   esc   __name__   orm   import   run   ntop   

原文地址:https://www.cnblogs.com/LiuXinyu12378/p/12246138.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!