tensorflow 2.0 函数详解

 

日萌社

人工智能AI:Keras PyTorch MXNet TensorFlow PaddlePaddle 深度学习实战(不定时更新)


1.tf.split
	tf.split(
		value,
		num_or_size_splits,
		axis=0,
		num=None,
		name='split'
	)
	num_or_size_splits可以为整数,也可以为列表。
	num_or_size_splits为整数的话,表示切割出的每个子tensor上的axis=0上的维度数为原始输入tensor的axis=0上的维度数除以num_or_size_splits。
	num_or_size_splits为列表的话,表示切割出的子tensor数量和列表元素数量一致,并且每个子tensor在axis=0维度上按照对应列表元素数值进行切割出相应的数量。

	import tensorflow as tf
	# 'value'是一个输入的tensor,shape为[5, 30]
	# 按照[4, 15, 11]的切割份数在指定的axis维度为1上的输入数据进行切割为3个子tensor
	split0, split1, split2 = tf.split(value, [4, 15, 11], 1)
	tf.shape(split0)  # 第1个子tensor的shape为[5, 4]
	tf.shape(split1)  # 第2个子tensor的shape为[5, 15]
	tf.shape(split2)  # 第3个子tensor的shape为[5, 11]
	# 按照指定的axis维度为1上进行切割为3个子tensor,每个子tensor的axis维度为1上的数量为30除以num_or_size_splits等于10
	split0, split1, split2 = tf.split(value, num_or_size_splits=3, axis=1)
	tf.shape(split0)  # [5, 10]
	tf.shape(split1)  # [5, 10]
	tf.shape(split2)  # [5, 10]

2.tf.identity
	tf.identity(
		input,
		name=None
	) 
	返回与input的形状和内容均相同的张量
	Args:
		input: 一个Tensor
		name: 操作的名称(可选)
	Returns:
		与输入类型相同的一个Tensor

	import tensorflow as tf
	#<tf.Tensor: id=2, shape=(1,), dtype=float32, numpy=array([1.], dtype=float32)>
	val0 = tf.ones((1,), dtype=tf.float32)
	#array([1.], dtype=float32)
	val0.numpy()
	#<tf.Tensor: id=3, shape=(1,), dtype=float32, numpy=array([0.7853982], dtype=float32)>
	a = tf.atan2(val0, val0)
	#array([0.7853982], dtype=float32)
	a.numpy()
	#<tf.Tensor: id=3, shape=(1,), dtype=float32, numpy=array([0.7853982], dtype=float32)>
	a_identity = tf.identity(a)
	print(a.numpy())          #[0.7853982]
	print(a_identity.numpy()) #[0.7853982]

3.tf.pad
	tf.pad(
		tensor,
		paddings,
		mode='CONSTANT',
		constant_values=0,
		name=None
	)
	此操作根据指定的paddings来填充张量。填充是形状为[n,2]的整数张量,其中n是张量的级别。
	对于输入的每个维度D,paddings[D,0]表示在该维度的张量内容之前要添加多少个值,paddings[D,1]表示在该维度的张量内容之后要添加多少个值。
	如果模式为"REFLECT",则paddings[D, 0]和paddings[D, 1]都不得大于张量尺寸tensor.dim_size(D) - 1。
	如果模式是"SYMMETRIC",那么paddings[D, 0]和paddings[D, 1]都不能大于张量尺寸tensor.dim_size(D)。
	输出的每个维度D的填充大小为:paddings[D, 0] + tensor.dim_size(D) + paddings[D, 1]

	import tensorflow as tf
	t = tf.constant([[1, 2, 3], [4, 5, 6]])
	#<tf.Tensor: id=0, shape=(2, 3), dtype=int32, numpy=
	#array([[1, 2, 3],
       	#       [4, 5, 6]])>

	#该paddings表示在输入tensor中对应位置的上下填充为0行,左右填充为0列
	paddings = tf.constant([[0,0], [0,0]])
	tf.pad(t, paddings, "CONSTANT")
	#<tf.Tensor: id=6, shape=(2, 3), dtype=int32, numpy=
	#array([[1, 2, 3],
	#       [4, 5, 6]])>

	#该paddings表示在输入tensor中对应位置的上下填充为0行,左右填充为1列
	paddings = tf.constant([[0,0], [1,1]])
	tf.pad(t, paddings, "CONSTANT")
	#array([[0, 1, 2, 3, 0],
	#       [0, 4, 5, 6, 0]])>

	#该paddings表示在输入tensor中对应位置的上下填充为1行,左右填充为0列
	paddings = tf.constant([[1,1], [0,0]])
	tf.pad(t, paddings, "CONSTANT")
	#array([[0, 0, 0],
	#       [1, 2, 3],
	#       [4, 5, 6],
	#       [0, 0, 0]])>

	#该paddings表示在输入tensor中对应位置的上下填充为1行,左右填充为1列
	paddings = tf.constant([[1,1], [1,1]])
	tf.pad(t, paddings, "CONSTANT")
	#array([[0, 0, 0, 0, 0],
	#       [0, 1, 2, 3, 0],
	#       [0, 4, 5, 6, 0],
	#       [0, 0, 0, 0, 0]])>

	#该paddings表示在输入tensor中对应位置的上下填充为1行,左右填充为2列
	paddings = tf.constant([[1, 1], [2, 2]])
	# 使用了默认参数mode='CONSTANT'和constant_values=0,即默认填充的为常数0
	# 't'的等级是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]]

4.tf.reduce_mean
	tf.math.reduce_mean(
		input_tensor,
		axis=None,
		keepdims=False,
		name=None
	)
	设置keepdims=True的话,会保留维度为1。

	import tensorflow as tf
	t = tf.constant([[[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]], [[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]]], dtype=tf.float64)
	out = tf.reduce_mean(t, [1, 2], keepdims=True)
	#<tf.Tensor: id=23, shape=(2, 1, 1, 3),dtype=float64, numpy=
	#array([[[[2.5, 3.5, 4.5]]],
	#       [[[2.5, 3.5, 4.5]]]])>
 	   
	t = tf.constant([[[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]], [[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]]])
	out = tf.reduce_mean(t, [1, 2])
	#<tf.Tensor: id=26, shape=(2, 3), dtype=float64, numpy=
	#array([[2.5, 3.5, 4.5],
	#       [2.5, 3.5, 4.5]])>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

あずにゃん

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值