tf commonly used functions
tf.cast(tensor name, dtype=data type)
Force tensor to be converted to a data type of dtype type
tf.reduce_min(tensor name)
Calculate the minimum value of elements in the tensor dimension
tf.reduce_max(tensor name)
Calculate the maximum value of the elements in the tensor dimension
The axis parameter controls the operation dimension
tf.random function
tf.random_uniform random uniform distribution
At present, I use this more, mainly for generating some parameters, such as w matrix
format : tf.random_uniform(shape, minval=0.0, maxval=1.0, dtype=tf.float32, seed=None, name=None)
parameters Description :
- shape defines dimensions
- minval minimum interval
- maxval maximum value of interval
- dtype defines the type, such as float, int
- seed defines the seed
- name defines the name
Example
import tensorflow as tf
# 定义 W 的shape[5,3],最小值-1.0,最大值1.0
w1 = tf.random_uniform([5,3], -1.0, 1.0)
# 推荐w2写法,提高代码可读性
w2 = tf.random_uniform(shape=[5,3], minval=-1.0, maxval=1.0)
with tf.Session() as sess:
print(sess.run(w1))
print(sess.run(w2))
rf.random_normal random normal distribution
Data generated in accordance with the normal distribution, its name is intended
format : tf.random_normal (shape, mean = 0.0 , stddev = 1.0, dtype = tf.float32, seed = None, name = None)
Parameters :
- shape defines the dimension (same as above)
- mean defines the mean
- stddev defines variance
- dtype defines the type (same as above)
- seed defines the seed
- name defines the name
Example
import tensorflow as tf
# 定义 W 的shape[5,3],均值-1.0,方差 4.0
w1 = tf.random_normal([5,3], -1.0, 4.0)
# 推荐w2写法,提高代码可读性
w2 = tf.random_normal(shape=[5,3], mean=-1.0, stddev=4.0)
with tf.Session() as sess:
print(sess.run(w1))
print(sess.run(w2))
tf.truncated_normal standard normal distribution
Compared with random_normal random normal distribution, truncated_normal truncated normal distribution random numbers, and only keep random numbers in the range of [mean-2stddev,mean+2stddev]
Format : tf.truncated_normal(shape, mean=0.0, stddev=1.0, dtype= tf.float32, seed=None, name=None)
parameter description :
- shape defines the dimension (same as above)
- mean defines the mean
- stddev defines variance
- dtype defines the type (same as above)
- seed defines the seed
- name defines the name
Example
import tensorflow as tf
# 定义 W 的shape[5,3],均值-1.0,方差 4.0
w1 = tf.truncated_normal([5,3], 0.0, 1.0)
# 推荐w2写法,提高代码可读性
w2 = tf.truncated_normal(shape=[5,3], mean=0.0, stddev=1.0)
with tf.Session() as sess:
print(sess.run(w1))
print(sess.run(w2))
tf.random_shuffle randomly swap positions (shuffle)
This is a rarely used
format : tf.random_shuffle(value, seed=None, name=None)
parameter description :
- value is a given tensor
- seed defined seed
- name defines the name
Example
import tensorflow as tf
#定义一个张量
vec = tf.constant([[1,2],[3,4],[5,6],[7,8],[9,10],[11,12]])
#定一随机交换位置(打乱)计算
shuff = tf.random_shuffle(value=vec,seed=1,name="shuff")
with tf.Session() as sess:
print (sess.run(shuff))
Reference: https://cloud.tencent.com/developer/article/1392320 with a little modification