【TensorFlow】【算子解析】【tf.math】tf.math.abs

【算子功能描述】node

tf.math.abs算子的做用是计算输入数据的绝对值 y = |x|python

【案例】c++

# -*- coding:utf-8 -*-
import tensorflow as tf
import numpy as np

img = np.random.standard_normal(size=(1,3,3)).astype(np.float32)

print(img)

x = tf.placeholder(shape=(1,3,3),dtype=tf.float32)

y = tf.math.abs(x)

with tf.Session() as sess:
    y = sess.run(fetches=y,feed_dict={x:img})
    print(y)
    tf.train.write_graph(graph_or_graph_def=sess.graph,logdir="./",name="./model/tf_math_abs.pb",as_text=True)

输入数据以下:dom

[[[-1.0353351   1.0076202  -2.4349942 ]
  [ 0.9931925  -0.14527066 -1.4662837 ]
  [ 0.31412837 -3.0430195  -0.3829416 ]]]

输出数据以下:fetch

[[[1.0353351  1.0076202  2.4349942 ]
  [0.9931925  0.14527066 1.4662837 ]
  [0.31412837 3.0430195  0.3829416 ]]]

【模型结构】优化

能够看出该算子的模型结构比较简单,属性"T”表示当前处理的数据类型,为dtype。该属性值时tf自动推断出来并填写的。code

该算子的输入节点为name为“Placeholder”的节点。tf pb模型中,算子的name是惟一的,tf用name做为算子的id,用做标识算子,graph中的node的边关系,也是经过name来创建的。orm

 

【算子定义】blog

该算子的定义在 tensorflow/core/ops/math_ops.cc ,具体定义以下:接口

PS: 咱们再作TensorFlow模型优化时,咱们能够将pb模型反序列化后,创建本身的图结构,而后将tf模型转换为咱们本身的模型。所以。了解每个算子的定义比较重要。固然,这须要一个TensorFlow基线版本,存在一样的python接口,在不一样的tf版本上,底层实现算子不同的状况,所以,再作tf模型优化时,咱们须要了解tf的c++算子定义。

相关文章
相关标签/搜索