tensorflow常用方法

1.tf,floor

tf.floor(
    x
,
    name
=None
)

将x向下取整

2.tf.ceil

tf.ceil(
    x
,
    name
=None
)

将x向上取整

3.tf.stack

tf.stack(
    values
,
    axis
=0,
    name
='stack'
)

将values按照axis轴进行合并,例如:

x = tf.constant([1, 4])
y
= tf.constant([2, 5])
z
= tf.constant([3, 6])
tf
.stack([x, y, z])  # [[1, 4], [2, 5], [3, 6]] (Pack along first dim.)
tf
.stack([x, y, z], axis=1)  # [[1, 2, 3], [4, 5, 6]]

4.tf.cast

tf.cast(
    x
,
    dtype
,
    name
=None
)

将x转为dtype类型,例如

x = tf.constant([1.8, 2.2], dtype=tf.float32)
tf
.cast(x, tf.int32)  # [1, 2], dtype=tf.int32

5.tf.pad

tf.pad(
    tensor
,
    paddings
,
    mode
='CONSTANT',
    name
=None,
    constant_values
=0
)

将tensor按照padding进行边缘扩充,例如

t = tf.constant([[1, 2, 3], [4, 5, 6]])
paddings
= tf.constant([[1, 1,], [2, 2]])
# 'constant_values' is 0.
# rank of 't' is 2.
tf
.pad(t, paddings, "CONSTANT")  # [[0, 0, 0, 0, 0, 0, 0],
                                 
#  [0, 0, 1, 2, 3, 0, 0],
                                 
#  [0, 0, 4, 5, 6, 0, 0],
                                 
#  [0, 0, 0, 0, 0, 0, 0]]

tf
.pad(t, paddings, "REFLECT")  # [[6, 5, 4, 5, 6, 5, 4],
                               
#  [3, 2, 1, 2, 3, 2, 1],
                               
#  [6, 5, 4, 5, 6, 5, 4],
                               
#  [3, 2, 1, 2, 3, 2, 1]]

tf
.pad(t, paddings, "SYMMETRIC")  # [[2, 1, 1, 2, 3, 3, 2],
                                 
#  [2, 1, 1, 2, 3, 3, 2],
                                 
#  [5, 4, 4, 5, 6, 6, 5],
                                 
#  [5, 4, 4, 5, 6, 6, 5]]

6.tf.reduce_max

tf.reduce_max(
    input_tensor
,
    axis
=None,
    keepdims
=None,
    name
=None,
    reduction_indices
=None,
    keep_dims
=None
)
根据第 axis维度进行比较大小,例如
 
  
a = tf.constant([1,2,3,4,5,6,7,8,9])
a = tf.reshape(a,(3,3))
b = tf.reduce_max(a,0)#,keep_dims=True)
c = tf.reduce_max(a,0,keep_dims=True)
sess = tf.Session()
print(sess.run(a))
print(sess.run(b))
print(sess.run(c))
[[1 2 3]
 [4 5 6]
 [7 8 9]]
[7 8 9]
[[7 8 9]]
 
 

 
 

猜你喜欢

转载自blog.csdn.net/u012235003/article/details/80080530