基本概念一
Declaring Tensor
Tensor是TensorFLow总的基本数据类型,它与numpy中的张量(多维数组)在概念和使用上十分的类似,可以通过以下的方式声明一些张量
import tensorflow as tf
import numpy as np
# 声明一个常量的张量值
x_constant = tf.constant(np.arange(24).reshape((2,3,4)), name="x_constant")
print(x_constant)
## out: Tensor("x_constant:0", shape=(2, 3, 4), dtype=int64)
# 声明一个使用shape=(3,4,5,6),并使用高斯分布随机生成的张量
x_normal = tf.random_normal(shape=(3,4,5,6),mean=1,stddev=2,name="x_normal")
print(x_normal)
## out: Tensor("x_normal:0", shape=(3, 4, 5, 6), dtype=float32)
# 对多维数组的理解 (tensor)
Scalar = tf.constant([2])
Vector = tf.constant([5,6,2])
Matrix = tf.constant([[1,2,3],[2,3,4],[3,4,5]])
Tensor = tf.constant( [ [[1,2,3],[2,3,4],[3,4,5]] , [[4,5,6],[5,6,7],[6,7,8]] , [[7,8,9],[8,9,10],[9,10,11]] ] )
with tf.Session() as session:
result = session.run(Scalar)
print "Scalar (1 entry):\n %s \n" % result
result = session.run(Vector)
print "Vector (3 entries) :\n %s \n" % result
result = session.run(Matrix)
print "Matrix (3x3 entries):\n %s \n" % result
result = session.run(Tensor)
print "Tensor (3x3x3 entries) :\n %s \n" % result
Variable
state = tf.Variable(0) one = tf.constant(1) new_value=tf.add(state,one) update = tf.assign(state,new_value) #Variables must be initialized by running an initialization operation after having launched the graph. # We first have to add the initialization operation to the graph: init_op = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init_op) print("State1:") print(sess.run(state)) for _ in range(3): sess.run(update) print("State2:") print(sess.run(state))
Graph
在有了tensor之后,就可以根据定义一些tensor之间的运算操作形成计算图。一个TensorFlow程序中可以有多个的计算图,TensorFlow自身会维护一个默认的计算图,因此在大部分只需要一个计算图的情况下可以不显示的对计算图进行指定,下面是一个构建简单计算图的示例
x_1 = tf.random_normal((2,3,4), name = "x_1") x_2 = tf.random_normal((2,3,4), name = "x_2") y_1 = x_1 + x_2 y_2 = x_1 * x_2
Tensorborad 可视化数据的流向过程:
事实上,x_1和x_2是也是由random_normal操作生成的,将x_1展开,可以观察到它的生成过程如下所示,它是由标准正态分布根据指定的方差和均值生成。
Session
Variable (must initialize) & constant
placeholder() 占位符 - 暂时store variable . 目的是可以从外部传入data, ,用法:
sess.run(***, feed_dict={input: **})
#在 Tensorflow 中需要定义 placeholder 的 type ,一般为 float32 形式 input1 = tf.placeholder(tf.float32) input2 = tf.placeholder(tf.float32) # mul = multiply 是将input1和input2 做乘法运算,并输出为 output ouput = tf.multiply(input1, input2) #需要传入的值放在了feed_dict={} 并一一对应每一个 input. #placeholder 与 feed_dict={} 是绑定在一起出现的。 with tf.Session() as sess: print(sess.run(ouput, feed_dict={input1: [7.], input2: [2.]}
Activation 激活函数
评价某个激活函数是否有用时,需要考虑的因素有:
1)该函数应是单调的, 这样输出便会随着输入的增长而增长,从而使利用梯度下降法寻找局部极值点成为可能.
2)该函数应是可微分的,以保证该函数定义域内的任意一点上导数都存在,从而使得梯度下降法能够正常使用来自这类激活函数的输出.
所有激活函数 输入 和 输出 的维度是一样的
f.nn.relu()
tf.nn.sigmoid()
tf.nn.tanh()
tf.nn.elu()
tf.nn.bias_add()
tf.nn.crelu()
tf.nn.relu6()
tf.nn.softplus()
tf.nn.softsign()
tf.nn.dropout()
def relu_layer(x, weights, biases, name=None):
"""Computes Relu(x * weight + biases).
Args:
x: a 2D tensor. Dimensions typically: batch, in_units
weights: a 2D tensor. Dimensions typically: in_units, out_units
biases: a 1D tensor. Dimensions: out_units
name: A name for the operation (optional). If not specified
"nn_relu_layer" is used.
Returns:
A 2-D Tensor computing relu(matmul(x, weights) + biases).
Dimensions typically: batch, out_units.
"""