TensorFlow学习程序(一):一个简单的神经网络模型

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))
biases = tf.Variable(tf.zeros([1]))

y = Weights*x_data + biases

lose = tf.reduce_mean(tf.square(y-y_data))
optimizer = tf.train.GradientDescentOptimizer(0.5) #定义一个优化器以提升参数准确度,0.5为学习效率,一般小于1
train = optimizer.minimize(lose) #优化Weights与biases使lose达到局域最优解或全局最优解

init = tf.initialize_all_variables() #初始化所有变量
#create tensorflow structure end

with tf.Session() as sess:
    sess.run(init) #激活神经网络
    for step in range(200): #训练200次
        sess.run(train)
        if step % 20 == 0:  #每训练20次输出一次Weights和biases
            print(sess.run(Weights),sess.run(biases)) #Variable中包含着数值,需要通过Session中的run获取

你可能感兴趣的:(TensorFlow)