tensorflow2.0完全梳理(20201214)

对tensorflow2.0的常用函数做一个完全梳理。当前不是2.0,先来一个大概框架,之后再改。

1、运算方式设置 tf.ConfigProto()

实例

import tensorflow as tf

session_config = tf.ConfigProto(
    log_device_placement=True,
    inter_op_parallelism_threads=0,
    intra_op_parallelism_threads=0,
    allow_soft_placement=True
)

sess = tf.Session(config=session_config)

a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2,3], name='a')
b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3,2], name='b')

c = tf.matmul(a,b) #矩阵乘法
print(sess.run(c))

运行结果

2020-12-10 10:28:58.400766: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 AVX512F FMA
2020-12-10 10:28:58.429345: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 2200000000 Hz
2020-12-10 10:28:58.434247: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x55720b44a5a0 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2020-12-10 10:28:58.434306: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): Host, Default Version
2020-12-10 10:28:58.435937: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcuda.so.1
2020-12-10 10:28:58.448442: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1618] Found device 0 with properties: 
name: Tesla V100-PCIE-32GB major: 7 minor: 0 memoryClockRate(GHz): 1.38
pciBusID: 0000:3b:00.0
2020-12-10 10:28:58.448634: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudart.so.10.0
2020-12-10 10:28:58.449940: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcublas.so.10.0
2020-12-10 10:28:58.451073: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcufft.so.10.0
2020-12-10 10:28:58.451327: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcurand.so.10.0
2020-12-10 10:28:58.452851: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcusolver.so.10.0
2020-12-10 10:28:58.454033: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcusparse.so.10.0
2020-12-10 10:28:58.456886: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudnn.so.7
2020-12-10 10:28:58.458607: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1746] Adding visible gpu devices: 0
2020-12-10 10:28:58.458636: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudart.so.10.0
2020-12-10 10:28:58.653615: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1159] Device interconnect StreamExecutor with strength 1 edge matrix:
2020-12-10 10:28:58.653644: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1165]      0 
2020-12-10 10:28:58.653649: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1178] 0:   N 
2020-12-10 10:28:58.656172: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1304] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 17173 MB memory) -> physical GPU (device: 0, name: Tesla V100-PCIE-32GB, pci bus id: 0000:3b:00.0, compute capability: 7.0)
2020-12-10 10:28:58.657818: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x55720c1503d0 initialized for platform CUDA (this does not guarantee that XLA will be used). Devices:
2020-12-10 10:28:58.657834: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): Tesla V100-PCIE-32GB, Compute Capability 7.0
Device mapping:
/job:localhost/replica:0/task:0/device:XLA_CPU:0 -> device: XLA_CPU device
/job:localhost/replica:0/task:0/device:GPU:0 -> device: 0, name: Tesla V100-PCIE-32GB, pci bus id: 0000:3b:00.0, compute capability: 7.0
/job:localhost/replica:0/task:0/device:XLA_GPU:0 -> device: XLA_GPU device
2020-12-10 10:28:58.660647: I tensorflow/core/common_runtime/direct_session.cc:359] Device mapping:
/job:localhost/replica:0/task:0/device:XLA_CPU:0 -> device: XLA_CPU device
/job:localhost/replica:0/task:0/device:GPU:0 -> device: 0, name: Tesla V100-PCIE-32GB, pci bus id: 0000:3b:00.0, compute capability: 7.0
/job:localhost/replica:0/task:0/device:XLA_GPU:0 -> device: XLA_GPU device

MatMul: (MatMul): /job:localhost/replica:0/task:0/device:GPU:0
2020-12-10 10:28:58.663317: I tensorflow/core/common_runtime/placer.cc:54] MatMul: (MatMul): /job:localhost/replica:0/task:0/device:GPU:0
a: (Const): /job:localhost/replica:0/task:0/device:GPU:0
2020-12-10 10:28:58.663332: I tensorflow/core/common_runtime/placer.cc:54] a: (Const): /job:localhost/replica:0/task:0/device:GPU:0
b: (Const): /job:localhost/replica:0/task:0/device:GPU:0
2020-12-10 10:28:58.663339: I tensorflow/core/common_runtime/placer.cc:54] b: (Const): /job:localhost/replica:0/task:0/device:GPU:0
[[22. 28.]
 [49. 64.]]

参数解释

参数 说明
log_device_placement 设置为True时,会打印出Tensorflow使用了哪种操作
inter_op_parallelism_threads 设置线程一个操作内部并行运算的线程数,比如矩阵乘法,如果设置为0,则表示以最优的线程数处理
intra_op_parallelism_threads 设置多个操作并行运算的线程数,比如c=a+b,d=e+f,可以并行运算
allow_soft_placement 不同设备,cpu和gpu是不同的,如果将这个选项设置成True,当运行设备不满足要求时,会自动分配CPU或GPU

其他

# 使用gpu时,tensorflow运行自动慢慢达到最大gpu的内存
session_config.gpu_options.allow_growth = True
# 使用GPU时,设置gpu内存使用的最大比例
session_config.gpu_options.per_process_gpu_memory_fraction = 0.4
# 能够使用gpu
tf.test.is_built_with_cuda()

import tensorflow as tf

sess = tf.Session()

with tf.device('/cpu:0'):
  a = tf.constant([1.0, 3.0, 5.0], shape=[1, 3])
  b = tf.constant([2.0, 4.0, 6.0], shape=[3, 1])

  with tf.device('/gpu:0'):
    c = tf.matmul(a, b)
    c = tf.reshape(c, [-1])

  with tf.device('/gpu:0'):
    d = tf.matmul(b, a)
    flat_d = tf.reshape(d, [-1])

  combined = tf.multiply(c, flat_d)  # 对应元素各自相乘
  print(sess.run(combined))

2、模型保存与调用 saver与restore

saver()与restore()只是保存了session中相关变量对应的值,不涉及模型的结构。

saver

saver的作用是将训练好的模型保存下来,以便下一次继续用于训练或测试;

restore的作用是将训练好的参数提取出来。Saver类训练完后,是以checkpoints文件形式保存。提取的时候也是从checkpoints文件中恢复变量。Checkpoints文件是一个二进制文件,它把变量名映射到对应的tensor值 。

一般地,Saver会自动的管理Checkpoints文件。我们可以指定保存最近的N个Checkpoints文件,当然每一步都保存ckpt文件也是可以的,只是没必要,费存储空间。

参考:https://www.jianshu.com/p/b0c789757df6

3、计算函数

乘(矩阵乘,点乘)

tf.matmul(a, b, transpose_a=False, transpose_b=False, adjoint_a=False, adjoint_b=False, a_is_sparse=False, b_is_sparse=False, name=None)


参数: 
a: 一个类型为 float16, float32, float64, int32, complex64, complex128 且张量秩 > 1 的张量。 
b: 一个类型跟张量a相同的张量。 
transpose_a: 如果为真, a则在进行乘法计算前进行转置。 
transpose_b: 如果为真, b则在进行乘法计算前进行转置。 
adjoint_a: 如果为真, a则在进行乘法计算前进行共轭和转置。 
adjoint_b: 如果为真, b则在进行乘法计算前进行共轭和转置。 
a_is_sparse: 如果为真, a会被处理为稀疏矩阵。 
b_is_sparse: 如果为真, b会被处理为稀疏矩阵。 
name: 操作的名字(可选参数) 
返回值: 一个跟张量a和张量b类型一样的张量且最内部矩阵是a和b中的相应矩阵的乘积。 
注意: 
(1)输入必须是矩阵(或者是张量秩 >2的张量,表示成批的矩阵),并且其在转置之后有相匹配的矩阵尺寸。 
(2)两个矩阵必须都是同样的类型,支持的类型如下:float16, float32, float64, int32, complex64, complex128。 
tf.multiply(x, y, name=None)

*表示点乘

tf.nn.bias_add(value, bias, name=None)
将bias加到value上
bias必须为一维的,value可以为任何维度。
支持广播

a=tf.constant([[1,1],[2,2],[3,3]],dtype=tf.float32)
b=tf.constant([1,-1],dtype=tf.float32)
c=tf.constant([1],dtype=tf.float32)
 
with tf.Session() as sess:
    print('bias_add:')
    print(sess.run(tf.nn.bias_add(a, b)))
    #执行下面语句错误
    #print(sess.run(tf.nn.bias_add(a, c)))
tf.add(x,y,name=None)
x矩阵和y矩阵相加
tf.add_n(inputs,name=None)
实现一个列表的元素的相加,输入的对象是一个列表,列表里的元素可以是向量,矩阵等

参考:https://blog.csdn.net/mieleizhi0522/article/details/80416668

判断

tf.equal(x,y,name=None)
对比两个矩阵或者向量的相等元素,如果是相等的,则返回True,否则返回False
返回值的矩阵维度与x相同

例子

import tensorflow as tf
a = [[1,2,3],[4,5,6]]
b = [[1,0,3],[1,5,1]]
with tf.Session() as sess:
    print(sess.run(tf.equal(a,b)))

'''
[[ True False  True]
 [False  True False]]
'''

https://blog.csdn.net/m0_37546065/article/details/89969954

One_hot

tf.one_hot(
    indices,
    depth,
    on_value=None,
    off_value=None,
    axis=None,
    dtype=None,
    name=None    
)

参数:

indices:指数的张量.
depth:一个标量,用于定义一个 one hot 维度的深度.
on_value:定义在 indices[j] = i 时填充输出的值的标量.(默认:1)
off_value:定义在 indices[j] != i 时填充输出的值的标量.(默认:0)
axis:要填充的轴(默认:-1,一个新的最内层轴).
dtype:输出张量的数据类型.
返回值:

output: one-hot 张量.

激活函数

tf.nn.log_softmax(
    logits,
    axis=None,
    name=None,
)

参数:

logits:一个非空的Tensor.必须是下列类型之一:half,float32,float64.
axis:将执行softmax的维度.默认值为-1,表示最后一个维度.
name:操作的名称(可选).
dim:axis的已弃用的别名.
返回:

一Tensor.与...有相同的类型logits.形状相同logits.

数据转化

格式转换 tf.cast(x, dtype, name)

tf.cast(
    x,
    dtype,
    name=None
)
将x的数据格式转化成dtype

tf.expand_dims(input, axis, name=None) 增加维度

tf.expand_dims(
    input,
    axis=None,
    name=None,
    dim=None
)
# 函数功能:给定一个input时,在axis轴处给input增加一个维度
# input是输入张量
# axis是指定扩大输入张量形状的维度索引值
# dim等同于轴,一般不推荐使用

 例子:

# 't' is a tensor of shape [2]
shape(expand_dims(t, 0)) ==> [1, 2]
shape(expand_dims(t, 1)) ==> [2, 1]
shape(expand_dims(t, -1)) ==> [2, 1]

# 't2' is a tensor of shape [2, 3, 5]
shape(expand_dims(t2, 0)) ==> [1, 2, 3, 5]
shape(expand_dims(t2, 2)) ==> [2, 3, 1, 5]
shape(expand_dims(t2, 3)) ==> [2, 3, 5, 1]

tf.greater(x, y, name=None) 返回(x>y元素的真值)  bool

如果两个张量的维度不一致的话,会进行类似Numpy一样的广播操作。

greater_equal(
    x,
    y,
    name=None
)

猜你喜欢

转载自blog.csdn.net/caicai0001000/article/details/110947559