1.tf.split(axis, num_or_size_splits,value)
该函数是通道拆分函数,将原来的的多通道tensor,拆分为单通道
axis:拆分的维度
num_or_size_splits:拆分为几份
value:需要拆分的tensor
实例:
import tensorflow as tf
a = tf.random_uniform((4,2,3))
c1,c2,c3 = tf.split(axis = 2, num_or_size_splits = 3, value = a)
sess = tf.Session()
print(sess.run(a))
print(sess.run(c1))
ouptut:
>>>[[[0.0217129 0.0620898 0.9197613 ]
[0.00816453 0.26460588 0.45891762]]
[[0.5298958 0.92715514 0.9984776 ]
[0.8907709 0.3258146 0.7756392 ]]
[[0.20484614 0.7365011 0.8002026 ]
[0.8510926 0.44176805 0.10259783]]
[[0.53181267 0.49676466 0.527159 ]
[0.07174098 0.03045177 0.8065448 ]]]
>>>[[[0.0217129 ]
[0.00816453 ]]
[[0.5298958 ]
[0.8907709 ]]
[[0.20484614 ]
[0.8510926 ]]
[[0.53181267 ]
[0.07174098 ]]]
值得注意的是,num_or_size_splits必须能被选中的(axis = ‘’)维度的维度值整除,不然会报错。
2.tf.clip_by_value(image, min, max)
该函数是将张量中小于min的值取min,大于max的取max
实例:
import tensorflow as tf
a = tf.constant([-1,0,0.5,1,1.1])
o1 = tf.clip_by_value(a,0.0,1.0)
sess = tf.Session()
print(sess.run(o1))
output:
>>>[0. 0. 0.5 1. 1. ]
3.tf.cond()
类似if....else....的分支结构
函数原型:cond ( pred , true_fn = None , false_fn = None , strict = False , name = None , fn1 = None , fn2 = None )
参数解释:
- pred:标量决定是否返回 true_fn 或 false_fn 结果.
- true_fn:要调用的已定义的函数,如果 pred 为 true,则被调用.
- false_fn:要调用的已定义的函数,如果 pred 为 false,则被调用.
- strict:启用/禁用 “严格”模式的布尔值.
- name:返回的张量的可选名称前缀.
实例:
do_it = tf.less(tf.random_uniform([]), probability)
image = tf.cond(do_it, lambda: manipulate(image), lambda: image)