tf.keras.layers运算

tf.keras.layers.Permute

在这里插入图片描述

tf.keras.layers.Permute(
    dims, **kwargs
)

使用案例:

model = Sequential()
model.add(Permute((2, 1), input_shape=(10, 64)))
# now: model.output_shape == (None, 64, 10)
# note: `None` is the batch dimension

参数:

dims : Tuple of integers. Permutation pattern does not include the samples dimension. Indexing starts at 1. For instance, (2, 1) permutes the first and second dimensions of the input.

tf.keras.layers.Multiply

函数原型:

tf.keras.layers.Multiply(
    **kwargs
)

使用案例:

tf.keras.layers.Multiply()([np.arange(5).reshape(5, 1),
                            np.arange(5, 10).reshape(5, 1)])

输出:

tf.Tensor(
[[ 0]
 [ 6]
 [14]
 [24]
 [36]], shape=(5, 1), dtype=int32)

由此可见,tf.keras.layers.Multiply就是相同shape的tensor对应位置元素相乘。

tf.keras.layers.Reshape

函数原型

tf.keras.layers.Reshape(
    target_shape, **kwargs
)

使用案例:

n = np.arange(32).reshape(2, 16)
k = tf.convert_to_tensor(n)
xx = tf.keras.layers.Reshape((8, 2))(k)
xn = tf.keras.layers.Reshape((4, 4))(xx)

n1 = np.arange(32).reshape(16, 2)
k1 = tf.convert_to_tensor(n1)
xx1 = tf.keras.layers.Reshape((1, 2))(k1)

可以看出,输出为(batch_size,) + target_shape
第一维肯定是batch_size,第二维开始按行的顺序重新排列。重新排列时,按照维度从前到后的顺序重新排列。每次reshape都按照这个顺序,乱不了。

model = tf.keras.Sequential()
model.add(tf.keras.layers.Reshape((3, 4), input_shape=(12,)))
# model.output_shape == (None, 3, 4), `None` is the batch size.
model.output_shape

输出:(None, 3, 4)

tf.keras.layers.RepeatVector

函数原型:

tf.keras.layers.RepeatVector(
    n, **kwargs
)

使用案例:

model = Sequential()
model.add(Dense(32, input_dim=32))
# now: model.output_shape == (None, 32)
# note: `None` is the batch dimension

model.add(RepeatVector(3))
# now: model.output_shape == (None, 3, 32)

Input shape:
2D tensor of shape (num_samples, features).

Output shape:
3D tensor of shape (num_samples, n, features).

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值