首页 > 解决方案 > 如何将具有形状 (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)

标签: pythontensorflowkeras

解决方案


您可以使用 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])

推荐阅读