tensorflow--深度学习/入门例子

学习tensorflow框架结构,一定要在头脑里面有一个网络图,当然,tensorflow带有tensorboard可视化工具
初识:

import tensorflow as tf
import numpy as np

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

#creat tensoflow structure start###
Wights = tf.Variable(tf.random_uniform([1],-1.0,1.0))
biases = tf.Variable(tf.zeros([1]))

y = Wights*x_data + biases
loss = tf.reduce_mean(tf.square(y-y_data))
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)

init = tf.initialize_all_variables()
#creat tensoflow structure end###

sess = tf.Session()
sess.run(init)

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

其中weights、biases、loss、optimizer、train是除初始与目标外的结构包含所需,是为重点
结果:

0 [-0.2135416] [0.6913942]
20 [-0.01687181] [0.3659571]
40 [0.06630152] [0.3190179]
60 [0.0902835] [0.30548355]
80 [0.09719837] [0.3015811]
100 [0.09919218] [0.3004559]
120 [0.09976708] [0.30013147]
140 [0.09993283] [0.30003792]
160 [0.09998064] [0.30001095]
180 [0.09999442] [0.30000317]
200 [0.0999984] [0.3000009]

每20步长一个输出,逐步接近目标值

你可能感兴趣的:(tensorflow--深度学习/入门例子)