1. tensorflow的简介与安装
现在tensorflow已经出到了2了,但是相关的教程大多数还是1的教程,为了方便学习,选择使用老版本的tensorflow1.15.2。并且使用的cpu版本的,一键pip安装。
!pip3 install tensorflow==1.15.2 -i https://pypi.tuna.tsinghua.edu.cn/simple --user
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple Requirement already satisfied: tensorflow==1.15.2 in /Users/jus4fun/Library/Python/3.7/lib/python/site-packages (1.15.2) Requirement already satisfied: wrapt>=1.11.1 in /Users/jus4fun/Library/Python/3.7/lib/python/site-packages (from tensorflow==1.15.2) (1.12.0) Requirement already satisfied: keras-preprocessing>=1.0.5 in /Users/jus4fun/Library/Python/3.7/lib/python/site-packages (from tensorflow==1.15.2) (1.1.0) Requirement already satisfied: numpy<2.0,>=1.16.0 in /usr/local/lib/python3.7/site-packages (from tensorflow==1.15.2) (1.18.1) Requirement already satisfied: protobuf>=3.6.1 in /Users/jus4fun/Library/Python/3.7/lib/python/site-packages (from tensorflow==1.15.2) (3.11.3) Requirement already satisfied: gast==0.2.2 in /Users/jus4fun/Library/Python/3.7/lib/python/site-packages (from tensorflow==1.15.2) (0.2.2) Requirement already satisfied: google-pasta>=0.1.6 in /Users/jus4fun/Library/Python/3.7/lib/python/site-packages (from tensorflow==1.15.2) (0.1.8) Requirement already satisfied: grpcio>=1.8.6 in /Users/jus4fun/Library/Python/3.7/lib/python/site-packages (from tensorflow==1.15.2) (1.27.2) Requirement already satisfied: six>=1.10.0 in /Users/jus4fun/Library/Python/3.7/lib/python/site-packages (from tensorflow==1.15.2) (1.13.0) Requirement already satisfied: termcolor>=1.1.0 in /Users/jus4fun/Library/Python/3.7/lib/python/site-packages (from tensorflow==1.15.2) (1.1.0) Requirement already satisfied: opt-einsum>=2.3.2 in /Users/jus4fun/Library/Python/3.7/lib/python/site-packages (from tensorflow==1.15.2) (3.1.0) Requirement already satisfied: wheel>=0.26; python_version >= "3" in /usr/local/lib/python3.7/site-packages (from tensorflow==1.15.2) (0.33.1) Requirement already satisfied: tensorflow-estimator==1.15.1 in /Users/jus4fun/Library/Python/3.7/lib/python/site-packages (from tensorflow==1.15.2) (1.15.1) Requirement already satisfied: astor>=0.6.0 in /Users/jus4fun/Library/Python/3.7/lib/python/site-packages (from tensorflow==1.15.2) (0.8.1) Requirement already satisfied: tensorboard<1.16.0,>=1.15.0 in /Users/jus4fun/Library/Python/3.7/lib/python/site-packages (from tensorflow==1.15.2) (1.15.0) Requirement already satisfied: absl-py>=0.7.0 in /Users/jus4fun/Library/Python/3.7/lib/python/site-packages (from tensorflow==1.15.2) (0.9.0) Requirement already satisfied: keras-applications>=1.0.8 in /Users/jus4fun/Library/Python/3.7/lib/python/site-packages (from tensorflow==1.15.2) (1.0.8) Requirement already satisfied: setuptools in /Users/jus4fun/Library/Python/3.7/lib/python/site-packages (from protobuf>=3.6.1->tensorflow==1.15.2) (45.2.0) Requirement already satisfied: markdown>=2.6.8 in /Users/jus4fun/Library/Python/3.7/lib/python/site-packages (from tensorboard<1.16.0,>=1.15.0->tensorflow==1.15.2) (3.2.1) Requirement already satisfied: werkzeug>=0.11.15 in /Users/jus4fun/Library/Python/3.7/lib/python/site-packages (from tensorboard<1.16.0,>=1.15.0->tensorflow==1.15.2) (1.0.0) Requirement already satisfied: h5py in /Users/jus4fun/Library/Python/3.7/lib/python/site-packages (from keras-applications>=1.0.8->tensorflow==1.15.2) (2.10.0)
2.第一个例子
本例为使用梯度下降法,对于一条直线的拟合过程,输入为随机的点阵。
import tensorflow as tf import numpy as np #创建数据 x_data = np.random.rand(100).astype(np.float32) y_data = x_data*0.1+0.3 #也就是要预测的曲线 ###开始创建训练结构### Weights = tf.Variable(tf.random_uniform([1],-1,1)) #权重设置成一维单个的的,范围从-1到1的随机变量 baises = tf.Variable(tf.zeros([1])) y = Weights*x_data + baises loss = tf.reduce_mean(tf.square(y-y_data)) #设置损失函数为平方差的均值 optimizer = tf.train.GradientDescentOptimizer(0.5) #优化器设置,梯度下降法,步长0.5 train = optimizer.minimize(loss) #定义训练函数,设置训练过程为优化器让loss最小化 init = tf.initialize_all_variables() #定义初始化所有变量的函数 ###结束创建训练结构### sess = tf.Session() sess.run(init) for step in range(200): sess.run(train) if step % 20==0: print(step,sess.run(Weights),sess.run(baises))
0 [-0.31533128] [0.75299066] 20 [-0.02941] [0.37147093] 40 [0.06716046] [0.31813672] 60 [0.09166652] [0.30460244] 80 [0.09788527] [0.30116794] 100 [0.09946335] [0.3002964] 120 [0.09986381] [0.30007523] 140 [0.09996543] [0.30001912] 160 [0.09999122] [0.30000487] 180 [0.09999775] [0.30000126]
3.tensorflow Session
Session是tensorflow1中非常重要的一个概念,有点类似于我们shellcode加载器,尤其是Session.run,如果指向一个函数,他就会去运行那个函数,如果指向一个变量,他就会返回变量值。
以下是Session两种打开方式:
import tensorflow as tf matrix1 = tf.constant([[3,3]]) matrix2 = tf.constant([[2],[2]]) product = tf.matmul(matrix1,matrix2) #Session管理方法方法1:自己关闭和打开sess sess = tf.Session() res = sess.run(product) sess.close() print(res) #Session管理方法2:with方法自动管理 with tf.Session() as sess: res2 = sess.run(product) print(res2)
[[12]] [[12]]
4.tensorflow变量
tf每一个变量都需呀按照它规定的方式进行定义
import tensorflow as tf state = tf.Variable(0,name='counter') #print(stat.name) one = tf.constant(1) get_new_value = tf.add(state,one) update = tf.assign(state,get_new_value) init = tf.initialize_all_variables() #如果定义了变量,变量一定要记得初始化 with tf.Session() as sess: sess.run(init) #print(new_value) for i in range(3): sess.run(update) print(i,sess.run(state))
0 1 1 2 2 3
5.Placeholder
Placeholder和Variable最大的区别就是Placeholder的值在执行的时候才传入具体的值,也就是在具体调用的时候,再去指定具体的值。具体解释参考如下代码:
import tensorflow as tf input1 = tf.placeholder(tf.float32) #placeholder要给一个type,大多数tf的函数定义时变量都用的float32类型 #input2 = tf.placeholder(tf.float32,[2,2]) #规定一个结构,如两行两列 input2 = tf.placeholder(tf.float32) output = tf.multiply(input1,input2) with tf.Session() as sess: print(sess.run(output,feed_dict={input1:[7.5],input2:[8.5]})) #这里就要传两个值过去input1和input2
[63.75]