tensorflow 之 最近用到的几个小操作tf.reshape,tf.convert_to_tensor,tf.where

1.tf.reshape() #调整tensor的形状

img = ....
img_res = tf.reshape(img, [-1])

着重记录是shape=[-1]时,相当于flatten操作,与此对应的是在tf.placeholder()中shape=None时,要求输入为一维。
2.tf.convert_to_tensor() #将ndarray等转换为tensor

x = np.array([[1,1],[2,2]])
tx = tf.convert_to_tensor(x)

3.tf.where()#有两种用法,分开讨论
   (1)tf.where(tensor),#tensor 为一个bool 型张量,where函数将返回其中为true的元素的索引
   (2)tf.where(tensor, a , b), #根据tensor的对应位置的bool值来选择a或b中对应位置的元素,true,从a中选择,false,从b中选择。该函数在语义分割等任务中常用。注(tensor, a , b 的shape一致

猜你喜欢

转载自blog.csdn.net/qq_41368074/article/details/111638757