[TensorFlow学习手记] 1-简单例子

版权声明:小博主大三在读,水平有限,希望大家多多指导,Personal Page:holeungliu.com https://blog.csdn.net/soulmeetliang/article/details/78617343

这里写图片描述

Code

import tensorflow as tf
import numpy as np

# create data
x_data = np.random.rand(100).astype(np.float32)
y_data = x_data*0.1 + 0.3

### create tensorflow structure start ###
Weights = tf.Variable(tf.random_uniform([1],-1.0,1.0))  # 随机数列生成的一维结构,初始值-1到1
biases = tf.Variable(tf.zeros([1])) # 初始值为0的一维结构

y = Weights*x_data + biases

loss = tf.reduce_mean(tf.square(y - y_data)) # 损失函数
optimizer = tf.train.GradientDescentOptimizer(0.5) # 优化器  学习效率:0.5
train = optimizer.minimize(loss)

init = tf.initialize_all_variables()  # 初始化
### create tensorflow structure end ###

# 激活
sess = tf.Session()
sess.run(init)  

for step in range(201):
    sess.run(train)
    if step % 20 == 0:
        print(step,sess.run(Weights),sess.run(biases))


Result

0 [-0.22749242] [ 0.72290248]
20 [-0.01494898] [ 0.36628792]
40 [ 0.06918389] [ 0.31777081]
60 [ 0.09173864] [ 0.30476409]
80 [ 0.09778526] [ 0.30127719]
100 [ 0.09940628] [ 0.30034241]
120 [ 0.09984083] [ 0.3000918]
140 [ 0.09995733] [ 0.30002463]
160 [ 0.09998855] [ 0.3000066]
180 [ 0.09999692] [ 0.3000018]
200 [ 0.09999918] [ 0.30000049]

猜你喜欢

转载自blog.csdn.net/soulmeetliang/article/details/78617343