Numpy 中clip函数的使用
numpy.clip(a, a_min, a_max, out=None)[source]
其中a是一个数组,后面两个参数分别表示最小和最大值,怎么用呢,老规矩,我们看代码:
import numpy as np
x=np.array([1,2,3,5,6,7,8,9])
np.clip(x,3,8)
Out[88]:
array([3, 3, 3, 5, 6, 7, 8, 8])
- 1
- 2
- 3
- 4
- 5
也就是说clip这个函数将将数组中的元素限制在a_min, a_max之间,大于a_max的就使得它等于 a_max,小于a_min,的就使得它等于a_min。
x=np.array([[1,2,3,5,6,7,8,9],[1,2,3,5,6,7,8,9]])
np.clip(x,3,8)
Out[90]:
array([[3, 3, 3, 5, 6, 7, 8, 8],
[3, 3, 3, 5, 6, 7, 8, 8]])
值域范围 tf.clip_by_value的用
tf.clip_by_value(A, min, max):输入一个张量A,把A中的每一个元素的值都压缩在min和max之间。小于min的让它等于min,大于max的元素的值等于max。
例如:
- import tensorflow as tf;
- import numpy as np;
- A = np.array([[1,1,2,4], [3,4,8,5]])
- with tf.Session() as sess:
- print sess.run(tf.clip_by_value(A, 2, 5))
输出:
[[2 2 2 4]
[3 4 5 5]]