莫烦视频总结笔记
【1】第一个例子
import tensorflow as tf
a = tf.constant('hello,tensorflow!')
sess = tf.Session()
print(sess.run(a))
【2】第二个例子
import tensorflow as tf
a = tf.constant(10)
b = tf.constant(32)
print(sess.run(a+b))
【3】查看版本
import tensorflow as tf
a = tf.__version__ #两个杠杠
print(a)
【4】未完的例子
【5】第一个神经网络训练的例子
import tensorflow as tf
import numpy as np
#creat 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)) #权重可能是个矩阵,所以大写W好标识
biases = tf.Variable(tf.zeros([1]))
y = Weights*x_data + biases
loss = tf.reduce_mean(tf.square(y-y_data)) #损失函数
optimizer = tf.train.GradientDescentOptimizer(0.5) #建立一个optimizer优化器,0.5是学习率
train = optimizer.minimize(loss) #用优化器来减少误差
init = tf.global_variables_initializer() #初始化参数
###create tensorflow structure end###
sess = tf.Session()
sess.run(init) #激活了上面神经网络的结构
for step in range(201): #开始训练,设置训练201次
sess.run(train)
if step % 20 == 0:
print(step,sess.run(Weights),sess.run(biases)) #每隔20次输出一次参数
【6】session的两种打开模式
import tensorflow as tf
matrix1 = tf.constant([[3,3]]) #建立一个一行两列的矩阵,数值就是(3,3)
matrix2 = tf.constant([[2],
[2]]) #这是一个两行一列的矩阵
product = tf.matmul(matrix1,matrix2) #矩阵的乘法
#在numpy中矩阵乘法是np.dot(m1,m2)
#method 1 有两种形式执行session会话控制
sess = tf.Session()
result = sess.run(product)
print(result)
sess.close() #这句可以没有
#method 2
with tf.Session() as sess:
result2 = sess.run(product)
print(result2)
【7】变量的用法
import tensorflow as tf
state = tf.Variable(0,name='counter') #必须要定义成它是个变量(tf.Variable)它才是个变量
#0是变量的初始值,name是给变量了一个名字
print(state.name)
one = tf.constant(1) #tf.constant是个常量,1是常量的值
new_value = tf.add(state,one) #tf.add是加号
update = tf.assign(state,new_value) #tf.assign相当于赋值,把new_value的值赋给state
init = tf.global_variables_initializer() #只要有定义的变量,就必须有这句,用以初始化
with tf.Session() as sess: #此步是真正用来激活变量
sess.run(init)
for step in range(3): #做三次循环
sess.run(update)
print(sess.run(state)) #直接print(state)是不行的,必须把sess指针放上去
【8】placeholder的用法
placeholder相当于一个占位符。变量在定义是必须要初始化,但是有些变量刚开始并不知道它的值(或者有的变量我想让它每次都是不同的值),无法初始化。这时,就需要用占位符。
import tensorflow as tf
input1 = tf.placeholder(tf.float32) #需要给定一个类型,tf中大部分都是float32
input2 = tf.placeholder(tf.float32)
input3 = tf.placeholder(tf.float32,[2,2]) #[2,2]是给定input的结构,两行两列
output = tf.multiply(input1,input2) #乘法
with tf.Session() as sess:
print(sess.run(output,feed_dict={input1:[7.0],input2:[2.]})) #这里2.就相当于2.0
#使用占位符,每次就要用feed_dict去传值
【9】定义一个神经网络的层函数
import tensorflow as tf
def add_layer(inputs,in_size,out_size,activation_function=None): #定义一个add_layer函数,input是输入数据
Weights = tf.Variable(tf.random_normal([in_size,out_size]) #定义Weights是一个矩阵,有in_size行,out_size列
biases = tf.Variable(tf.zeros([1,out_size]) + 0.1) #biase是一个类似于列表,一行,out_size列,初始值为0+0.1
Wx_plus_b = tf.matmul(inputs,Weights) + biases #Wx_plus_b是该层输出的未激活的值
if activation_function is None:
outputs = Wx_plus_b
else:
outputs = activation_function(Wx_plus_b)
return outputs
【0.1】随机数
tf.random_normal(shape,mean=0.0,stddev=1.0,dtype=tf.float32,seed=None,name=None)
tf.truncated_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)
tf.random_uniform(shape,minval=0,maxval=None,dtype=tf.float32,seed=None,name=None)
→
这几个都是用于生成随机数tensor的,尺寸是shape:
1、random_normal: 正太分布随机数,均值mean,标准差stddev;
2、truncated_normal:截断正态分布随机数,均值mean,标准差stddev,不过只保留[mean-2*stddev,mean+2*stddev]范围内的随机数 ;
3、random_uniform:均匀分布随机数,范围为[minval,maxval]。