tensorflow版线性回归
Posted liuxinyu12378
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了tensorflow版线性回归相关的知识,希望对你有一定的参考价值。
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版线性回归的主要内容,如果未能解决你的问题,请参考以下文章