神经网络解线性公式 y=ax+b

神经网络的 hello world


In [1]:
import numpy as np #科学计算模块

In [2]:
# create data 创建最简单的神经网络模型 ,生成一组(x,y)用作训练数据
# 我们要提供一系列已经存在的(x,y) 组合,这个叫做训练集,我们先用代码生成 100 组训练集,先随机生成 5 组 x 的值,命名为 x_data,其中使用 np.random.random(]) 来生成随机数,令数据类型为浮点数 np.float32 以便于计算:
x_data = np.random.rand(100).astype(np.float32) #生成100个随机数列,数据类型是float32,tf中大多数都用的是float32

#根据公式求得 y_data 的值
y_data = x_data*0.1 + 1.3 #0.1为权重,1.3为偏置

In [3]:
# 结果如下,其中随机数每次执行会不一样(只展示前5个):
print('x_data:',x_data[0:5],'\n','y_data',y_data[0:5])


x_data: [0.15943949 0.20421818 0.72433054 0.40392435 0.8618312 ] 
 y_data [1.315944  1.3204218 1.372433  1.3403924 1.386183 ]

In [4]:
import tensorflow as tf
### create tensorflow structure start ###设计预测模型

Weights = tf.Variable(tf.random_uniform([1], -1.0, 1.0))#用Variable初始化Weights权重变量-随机数列生成-1到1的1维数据,

biases = tf.Variable(tf.zeros([1])) #偏置初始化为0,

y = Weights*x_data + biases #所要预测的y,通过训练使得y逼近真实的y_data,优化得到的参数变量就即Weights和biases

loss = tf.reduce_mean(tf.square(y-y_data)) #预测的y和实际的y_data的差别,即均方差

optimizer = tf.train.GradientDescentOptimizer(0.5) #用tf的梯度下降优化器减少误差loss,提升参数的准确度,0.5为学习效率(一般小于1))

train = optimizer.minimize(loss) #训练以减少误差

 

init = tf.global_variables_initializer() #初始化tf结构图中的所有变量,这里是Weights和biases

### create tensorflow structure end ###


C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\tensorflow\python\framework\dtypes.py:516: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint8 = np.dtype([("qint8", np.int8, 1)])
C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\tensorflow\python\framework\dtypes.py:517: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_quint8 = np.dtype([("quint8", np.uint8, 1)])
C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\tensorflow\python\framework\dtypes.py:518: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint16 = np.dtype([("qint16", np.int16, 1)])
C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\tensorflow\python\framework\dtypes.py:519: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_quint16 = np.dtype([("quint16", np.uint16, 1)])
C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\tensorflow\python\framework\dtypes.py:520: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint32 = np.dtype([("qint32", np.int32, 1)])
C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\tensorflow\python\framework\dtypes.py:525: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  np_resource = np.dtype([("resource", np.ubyte, 1)])

In [5]:
sess = tf.Session() #激活创建的结构图,Sesssion是神经网路的执行命令的对话控制

sess.run(init) #激活init,也就是所有结构

In [6]:
for step in range(201):#让神经网络一步一步的训练 201步
    sess.run(train) # 开始训练
    if step % 20 == 0: #每间隔20步打印以下训练得到的变量值
        print(step, sess.run(Weights), sess.run(biases),abs(loss))


0 [0.79038495] [1.2611924] Tensor("Abs:0", shape=(), dtype=float32)
20 [0.22643316] [1.2332475] Tensor("Abs_1:0", shape=(), dtype=float32)
40 [0.12875503] [1.2848183] Tensor("Abs_2:0", shape=(), dtype=float32)
60 [0.10653979] [1.2965472] Tensor("Abs_3:0", shape=(), dtype=float32)
80 [0.10148738] [1.2992147] Tensor("Abs_4:0", shape=(), dtype=float32)
100 [0.10033824] [1.2998214] Tensor("Abs_5:0", shape=(), dtype=float32)
120 [0.10007691] [1.2999593] Tensor("Abs_6:0", shape=(), dtype=float32)
140 [0.10001747] [1.2999907] Tensor("Abs_7:0", shape=(), dtype=float32)
160 [0.10000402] [1.2999978] Tensor("Abs_8:0", shape=(), dtype=float32)
180 [0.1000009] [1.2999995] Tensor("Abs_9:0", shape=(), dtype=float32)
200 [0.10000036] [1.2999997] Tensor("Abs_10:0", shape=(), dtype=float32)

In [ ]: