例子2

创建数据

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

搭建模型

tf.Variable 来创建描述 y 的参数

神经网络也就是学着把 Weights 变成 0.1, biases 变成 0.3

Weights = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
biases = tf.Variable(tf.zeros([1]))

y = Weights*x_data + biases

计算误差

loss = tf.reduce_mean(tf.square(y-y_data))

传播误差

误差传递方法是梯度下降法: Gradient Descent

optimizer 来进行参数的更新.

训练

使用这个结构之前, 必须先初始化所有之前定义的Variable

Session 来执行 init 初始化步骤

并且, 用 Sessionrun 每一次 training 的数据. 逐步提升神经网络的预测准确性.

Last updated

Was this helpful?