学习视频:https://morvanzhou.github.io/tutorials/machine-learning/tensorflow/
在Tensorflow中,任何参数的运算和操作都需要转换成对应的TensorFlow数据类型。
Tensorflow提供了一些函数用于生成常量值。
#tf.zeros(shape, dtype=tf.float32, name=None)
# 创建一个所有的参数为0的tensor对象
#tf.zeros_like(tensor, dtype=None, name=None)
# 给定一个tensor(tensor对象),该方法会返回一个类似当前参数类型以及维度的对象,但是所有参数的值均为0
#tf.ones(shape, dtype=tf.float32, name=None)
#tf.ones_like(tensor, dtype=None, name=None)
#tf.fill(dims, value, name=None)
# 创建一个维度为dims,值为value的tensor对象
#tf.constant(value, dtype=None, shape=None, name='Const')
#创建一个常量tensor,按照给出value来赋值,可以用shape来指定其形状。
# value可以是一个数,也可以是一个list。
# 如果是一个数,那么这个常亮中所有值的按该数来赋值。
# 如果是list,那么len(value)一定要小于等于shape展开后的长度。
# 赋值时,先将value中的值逐个存入。不够的部分,则全部存入value的最后一个值
# 创建一个维度为1, 类型为int的对象;double
data = tf.zeros([1], dtype=tf.int32)
#random_uniform
tf.random_uniform((6, 6), minval=low,maxval=high,dtype=tf.float32)))
# 返回6*6的矩阵,产生于low和high之间,产生的值是均匀分布的。
1.Session
Session 是 Tensorflow 为了控制,和输出文件的执行的语句. 运行 session.run() 可以获得你要得知的运算结果, 或者是你所要运算的部分.
# create two matrixes
matrix1 = tf.constant([[3, 3]])
matrix2 = tf.constant([[2],
[2]])
product = tf.matmul(matrix1, matrix2)
print(product)
# method 1
sess = tf.Session()
result = sess.run(product)
print(result)
sess.close()
# method 2
with tf.Session() as sess:
result2 = sess.run(product)
print(result2)
# [[12]]
因为 product 不是直接计算的步骤, 所以我们会要使用 Session 来激活 product 并得到计算结果. 有两种形式使用会话控制 Session 。
2. Variable
import tensorflow as tf
state = tf.Variable(0, name='counter')
# 定义常量 one
one = tf.constant(1)
# 定义加法步骤 (注: 此步并没有直接计算)
new_value = tf.add(state, one)
# 将 State 更新成 new_value
update = tf.assign(state, new_value)
# 如果定义 Variable, 就一定要 initialize
# init = tf.initialize_all_variables() # tf 马上就要废弃这种写法
init = tf.global_variables_initializer() # 替换成这样就好
# 使用 Session
with tf.Session() as sess:
sess.run(init)
for _ in range(3):
sess.run(update)
print(sess.run(state))
#注意:直接 print(state) 不起作用!!
#一定要把 sess 的指针指向 state 再进行 print 才能得到想要的结果!
例子
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
#Strat
Weights = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
biases = tf.Variable(tf.zeros([1]))
y = Weights*x_data + biases
#计算 y 和 y_data 的误差:
loss = tf.reduce_mean(tf.square(y-y_data))
#反向传递误差的工作就教给optimizer了, 我们使用的误差传递方法是梯度下降法:
#Gradient Descent 让后我们使用 optimizer 来进行参数的更新.
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)
#End
#先初始化所有之前定义的Variable, 所以这一步是很重要的!
init = tf.global_variables_initializer() # 替换成这样就好
#再创建会话 Session. 我们会在下一节中详细讲解 Session. 我们用 Session 来执行 init 初始化步骤. #并且, 用 Session 来 run 每一次 training 的数据. 逐步提升神经网络的预测准确性.
sess = tf.Session()
sess.run(init) # Very important
for step in range(201):
sess.run(train)
if step % 20 == 0:
print(step, sess.run(Weights), sess.run(biases))
3. truncated_normal
tf.truncated_normal(shape, mean, stddev) :shape表示生成张量的维度,mean是均值,stddev是标准差。这个函数产生正太分布,均值和标准差自己设定。这是一个截断的产生正太分布的函数,就是说产生正太分布的值如果与均值的差值大于两倍的标准差,那就重新生成。和一般的正太分布的产生随机数据比起来,这个函数产生的随机数与均值的差距不会超过两倍的标准差,但是一般的别的函数是可能的。
c = tf.truncated_normal(shape=[10,10], mean=0, stddev=1)