python - 如何将具有形状 (None, 512) 的张量转换为具有形状 (None, 12, 12, 512) 的张量
问题描述
我正在使用 Python 3.7.7.、Tensorflow 2.1.0 和 Functional Api 来定义具有此摘要的编码器:
Model: "encoder"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) [(None, 200, 200, 1)] 0
_________________________________________________________________
conv1_1 (Conv2D) (None, 200, 200, 64) 1664
_________________________________________________________________
conv1_2 (Conv2D) (None, 200, 200, 64) 102464
_________________________________________________________________
pool1 (MaxPooling2D) (None, 100, 100, 64) 0
_________________________________________________________________
conv2_1 (Conv2D) (None, 100, 100, 96) 55392
_________________________________________________________________
conv2_2 (Conv2D) (None, 100, 100, 96) 83040
_________________________________________________________________
pool2 (MaxPooling2D) (None, 50, 50, 96) 0
_________________________________________________________________
conv3_1 (Conv2D) (None, 50, 50, 128) 110720
_________________________________________________________________
conv3_2 (Conv2D) (None, 50, 50, 128) 147584
_________________________________________________________________
pool3 (MaxPooling2D) (None, 25, 25, 128) 0
_________________________________________________________________
conv4_1 (Conv2D) (None, 25, 25, 256) 295168
_________________________________________________________________
conv4_2 (Conv2D) (None, 25, 25, 256) 1048832
_________________________________________________________________
pool4 (MaxPooling2D) (None, 12, 12, 256) 0
_________________________________________________________________
conv5_1 (Conv2D) (None, 12, 12, 512) 1180160
_________________________________________________________________
conv5_2 (Conv2D) (None, 12, 12, 512) 2359808
_________________________________________________________________
global_average_pooling2d (Gl (None, 512) 0
=================================================================
Total params: 5,384,832
Trainable params: 5,384,832
Non-trainable params: 0
_________________________________________________________________
None
我使用以下代码从 U-Net 网络获取编码器:
u_net: Model = get_unet_uncompiled(img_shape = (200, 200, 1))
encoder_input = Model(inputs=u_net.layers[0].input, outputs=u_net.layers[14].output)
encoder_output = GlobalAveragePooling2D()(encoder_input.layers[-1].output)
encoder = Model(encoder_input.input, encoder_output, name='encoder')
print(encoder.summary())
我想将它的输出汇集到一个(None, 12, 12, 512)
.
我试过这个:
u_net: Model = get_unet_uncompiled(img_shape = (200, 200, 1))
encoder_input = Model(inputs=u_net.layers[0].input, outputs=u_net.layers[14].output)
encoder_output = GlobalAveragePooling2D()(encoder_input.layers[-1].output)
up = UpSampling2D(size=(12, 12))(encoder_output)
encoder = Model(encoder_input.input, up, name='encoder')
但我有这个错误:
ValueError: Input 0 of layer up_sampling2d is incompatible with the layer: expected ndim=4, found ndim=2. Full shape received: [None, 512]
我不知道我在做什么。也许UpSampling2D
这里是错的。
但是,如何将其转换为 shape (None, 12, 12, 512)
?
解决方案
您可以使用 Lambda 层:
import tensorflow as tf
inputs = tf.random.uniform((100, 512),0, 1, dtype=tf.int32)
layer = tf.keras.layers.Lambda(lambda x: tf.tile(tf.reshape(x, (100, 1, 1, 512)),
(1, 12, 12, 1)))
print(layer(inputs).shape)
TensorShape([100, 12, 12, 512])
推荐阅读
- django - 如何通过 Django 通道共享文件,现在有不完整的解决方案,例如来自 S3 存储桶和所有
- arrays - 当作为道具的一部分传递给自定义反应组件时,为什么“索引”不能作为唯一键?
- python - 当多个键具有相同的最大值时如何在字典中查找最大值并返回键
- python - 这个 python 字符串排序有效,但它不应该。应该是?
- java - 如何为在 lombok 中使用较少参数的构造函数建模
- javascript - Setinterval 在某个函数中立即运行
- python - 如何逐行读取“数据”属性?Python
- python - 访问全局字典
- javascript - 手动设置日期时如何防止日期选择器触发事件
- twitter - twitter 卡验证器错误:未找到卡(卡错误),即使我有元标记