深度学习-基础入门

为了能够更好的理解tensorflow来进行学习我用一个简单的例子来说明。

import tensorflow as tf
import numpy as np

#creat the real data start
x_data=np.random.rand(100).astype(np.float32)
y_data=2*x_data+6
#creat the real data stop


#creat the tensorflow structure start
Weights=tf.compat.v1.Variable(tf.compat.v1.random_uniform([1],-1.0,1.0))
Biases=tf.compat.v1.Variable(tf.compat.v1.zeros([1]))
y=Weights*x_data+Biases
loss=tf.compat.v1.reduce_mean(tf.compat.v1.square(y-y_data))
optimizer=tf.compat.v1.train.GradientDescentOptimizer(0.5)
train=optimizer.minimize(loss)
init=tf.compat.v1.global_variables_initializer()
#creat the tensorflow structure stop


#trainint start
sess=tf.compat.v1.Session()
sess.run(init)
for steps in range(201):
    sess.run(train)
    if steps%20==0:
        print(steps,":",sess.run(Weights),sess.run(Biases))
sess.close()

结果:

2019-07-15 12:14:39.809264: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
0 : [2.8191812] [7.4471025]
20 : [2.0189333] [5.9902945]
40 : [2.0058506] [5.9970007]
60 : [2.001808] [5.999073]
80 : [2.0005586] [5.9997134]
100 : [2.0001726] [5.9999113]
120 : [2.0000536] [5.9999723]
140 : [2.0000167] [5.9999914]
160 : [2.000005] [5.9999976]
180 : [2.0000026] [5.9999986]
200 : [2.0000026] [5.9999986]

好,那么现在我们就来详细说明这个例子:
目的:让计算机学出方程y=2*x+6中的2和6,其中我们称这个2为权重(weight),称这个6(biase)为偏置.

第一步:收集数据()

  x_data=np.random.rand(100).astype(np.float32)
    y_data=2*x_data+6

我们随机生成这些数据x_data,而y_data是方程的结果y,相当于样本的标记,是已经知道的正确的结果
np.random.rand(100)的作用是生成随机生成具有100个元素的数列(数组),而astype(np.float32)是用来转换类型为float,毕竟如果是int(小数点后的数被省去)难免误差较大。

第二步:建立学习的结构(从代码上来看,实际上建立神经网络的各个部分(或者说是各个变量和常量),而至于内部的运行情况,tensorflow的各种函数已经包装好了,我们只需要把材料准备好,然后向其说明怎么做就可以了)

Weights=tf.compat.v1.Variable(tf.compat.v1.random_uniform([1],-1.0,1.0))
Biases=tf.compat.v1.Variable(tf.compat.v1.zeros([1]))

首先,是权重和偏置的声明,有些教学视频的代码中不含compat.v1.,这是因为版本兼容问题,不需在意。
random_uniform([1],-1.0,1.0),作用是随机生成一个在-1到1之间的一维且仅一个的随机数作为权重,为什么能随机生成呢,因为随着计算机的学习,自然会去改变这个值,自动使它接近正确值。
tf.compat.v1.zeros([1]),作用是生成一个一维且仅一个的数值为0的数。
y=Weights*x_data+Biases,这一句就是我们需要计算机自己去做的操作,通过这个操作得到的结果与前面所说y_data作比较,再根据其误差去调整Weights和Biases的值。

loss=tf.compat.v1.reduce_mean(tf.compat.v1.square(y-y_data))
optimizer=tf.compat.v1.train.GradientDescentOptimizer(0.5)
train=optimizer.minimize(loss)

这部分操作便是上一步说的如何根据其误差去调整Weights和Biases的值,loss便是误差。
tf.compat.v1.square(y-y_data),作用就是计算y-y_data(y和y_data都是一个一维数组)的结果的每一个元素的平方。
reduce_mean,作用是计算所有元素的平均值,optimizer就是所谓的优化器,
GradientDescentOptimizer,是梯度下降优化器,而0.5是我们先作的学习率。
optimizer.minimize(loss),就是令误差最小化从而使所求的权重和偏置最接近真实值。
init=tf.compat.v1.global_variables_initializer()
这一步是为了初始化,任何有关Variables(变量)的操作都需要这个进行初始化。

sess=tf.compat.v1.Session()
sess.run(init)
for steps in range(201):
    sess.run(train)
    if steps%20==0:    
        print(steps,":",sess.run(Weights),sess.run(Biases))
sess.close()

Session就是会话,对于深度学习的所有操作都要放在会话里面进行,for steps in range(201)的意思就是学习201次,学得越多次,基本就越接近真实值。

好了,以上就是所有内容。

发布了27 篇原创文章 · 获赞 16 · 访问量 1906

猜你喜欢

转载自blog.csdn.net/weixin_43979090/article/details/95950647