Tensorflow学习备忘录

http://www.tensorfly.cn/images/tensors_flowing.gif

刚刚开始学习 tensorflow 很多东西不是很明白,所以在这里逐条记录,方便记忆和日后的查阅。不过话又说回来,为什么这API的命名如此混乱,一会大写一会小写?

开发环境

系统:Ubuntu 16.04 (WSL)

Python 版本:3.5.2-64bit

Tensorflow 版本: 1.14.0

张量

张量在tensorflow中表示数据的标准方式。简单地说,张量就是多维数组,时二维表到更高维度的扩展。

API 记录

通过 tf.constant() 生成常量张量。

1
tf.constant(value, dtype=None, shape=None, name='Const', verify_shape=False)

通过定义形状(shape)可以生成同属性(dtype)同值(value)的常量矩阵。其中 verify_shape 规定了 形状是否可以被改变,默认不允许。

1
2
constant_0 = tf.constant(0.1, dtype=tf.float32, shape=[2, 2]) 
# [[0.1 0.1]][[0.1 0.1]]

同样可以单独定义

1
2
constant_1 = tf.constant([1, 2, 3], tf.int32, name='constant_1')
# [1,2,3]

变量张量,变量主要是在机器学习中用于训练时候进行的输入和输出变化。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
Variable(
    initial_value=None, 
    trainable=None, 
    collections=None, 
    validate_shape=True, 
    caching_device=None, 
    name=None, 
    variable_def=None, 
    dtype=None, 
    expected_shape=None, 
    import_scope=None, 
    constraint=None, 
    use_resource=None, 
    synchronization=VariableSynchronization.AUTO, 
    aggregation=VariableAggregation.NONE, 
    shape=None
)

get_variable 也是初始化一个变量,通过 initializer 进行初始化。

1
2
3
4
5
6
7
8
9
x = tf.placeholder(tf.int32, name='x')
y = tf.placeholder(tf.int32, name='y')
get_v = tf.get_variable(
    'v1', initializer=tf.constant_initializer, shape=(3,3))
add = tf.add(x, y, name='add')
with tf.Session() as s:
    s.run(tf.global_variables_initializer())
    print(s.run(get_v))
# [[0. 0. 0.][0. 0. 0.][0. 0. 0.]]

初始化的操作有如下,详情见操作中表初始化操作。

通过 get_variable 初始化的张量,在进行session 运行的时候,要通过 global_variables_initializer 进行全局变量初始化。

注意 在使用该方法时,如果遇到命名冲突会进行报错。

placeholder 主要是声明一个张量( Tensor ),但是没有进行赋值,只给该张量规定了类型( dtype ),形状( shape ),和名称( name )。其中 形状,省略后为一维矩阵,名称可以省略。

1
num1 = tf.placeholder(tf.float32, [2, 2], name='num1')

该条语句生成了一个 2×2 的矩阵,类型全为 float32,名称为 num1

当前 num1 没有任何值,要想在后期使用它,在使用时应该给予赋值。

所有的运行都要在 Session 中,所有的操作都要在 Session 中进行。

  1. 首先要创建会话 Session sess = tf.Session()
  2. 通过 sess.run() 进行计算
  3. session.close() 关闭
1
2
3
4
5
6
run(
    fetches,
    feed_dict=None,
    options=None,
    run_metadata=None
)

对于 run 方法有四个参数。

  • fetches 可以看作一个操作,要将执行的操作进行传入。
  • feed_dict 可以对placeholder 进行赋值操作。对 placeholder 声明的张量进行赋值。
  • options
  • run_metadata

操作

Tensorflow 在数据流图上都是基于操作,故记录一些操作。

操作类型典型操作
基础算术add/multiply/mod/sqrt/sin/trace/argmin
数组运算size/rank/split/reverse/cast/one_hot/quantize
梯度裁剪clip_by_value/clip_by_norm/clip_by_global_norm
逻辑控制和调试identity/logical_and/equal/less/is_finite/is_nan
数据流控制enqueue/dequeue/size/take_grad/apply_grad
初始化操作zeros_initializer/random_normal_initializer/orthogonal_initializer
神经网络运算convolution/pool/bias_add/softmax/dropout/erosion2d
随机运算random_normal/random_shuffle/multinomial/random_gamma
字符串运算string_to_hash_bucket/reduce_join/substr/encode_base64
图像处理运算encode_png/resize_images/rot90/hsv_to_rgb/adjust_gamma

用于矩阵的交换,如果是二维矩阵表示矩阵转置,三位或者更高维度的矩阵,可以表示互换。

1
2
3
4
5
6
7
x = tf.placeholder(tf.int32, shape=(2, 3))
with tf.Session() as sess:
    print(sess.run(tf.transpose(x), feed_dict={x: [[1, 2, 3], [3, 4, 5]]}))

# [[1 3]
# [2 4]
# [3 5]]

该API是用于卷积神经网络的卷积操作,tf.nn.conv2d(x,w,strudes,padding) 。有着四个参数

truncated_normal(shape, dtype, seed=0, seed2=0, name=None)该函数会产生正太分布。

  • seed
  • seed2

参考

相关内容