TransWikia.com

How can I easily retrieve the latent space encodings in tensorflow?

Data Science Asked on August 29, 2021

Here is where I’m at. I built and trained an autoencoder in tensorflow. The model summary looks like so:

Model: "model_3"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_3 (InputLayer)         [(None, None, None, 3)]   0         
_________________________________________________________________
conv2d_4 (Conv2D)            (None, None, None, 4)     112       
_________________________________________________________________
conv2d_5 (Conv2D)            (None, None, None, 16)    592       
_________________________________________________________________
dense_5 (Dense)              (None, None, None, 32)    544       
_________________________________________________________________
dense_6 (Dense)              (None, None, None, 8)     264       
_________________________________________________________________
dense_7 (Dense)              (None, None, None, 32)    288       
_________________________________________________________________
conv2d_transpose_2 (Conv2DTr (None, None, None, 16)    4624      
_________________________________________________________________
conv2d_transpose_3 (Conv2DTr (None, None, None, 3)     435       
=================================================================
Total params: 6,859
Trainable params: 6,859
Non-trainable params: 0
_________________________________________________________________

The layer ‘dense_6’ is my latent space. Now, I am passing through a (256,256,3) image into the encoder part and am getting out (1, 252, 252, 8) tensor. Obviously, the output shape is listed as (None, None, None, 8), so it makes sense that I am getting this tensor out, but I was expecting to get just an 8-dimensional vector out.

Did I design my autoencoder wrong maybe? I’m quite confused and could definitely use some clarification. Thanks!

One Answer

Found out what I was doing wrong. Yes, I built the autoencoder wrong. I didn't think about how I need to flatten the tensor before passing it into a dense layer. Important step but easy to forget...

Here is my new model summary in case someone else needs some guidance:

Model: "model_9"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_8 (InputLayer)         [(None, 256, 256, 3)]     0         
_________________________________________________________________
conv2d_14 (Conv2D)           (None, 254, 254, 4)       112       
_________________________________________________________________
conv2d_15 (Conv2D)           (None, 252, 252, 16)      592       
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 126, 126, 16)      0         
_________________________________________________________________
flatten_4 (Flatten)          (None, 254016)            0         
_________________________________________________________________
dense_16 (Dense)             (None, 16)                4064272   
_________________________________________________________________
latent (Dense)               (None, 8)                 136       
_________________________________________________________________
dense_17 (Dense)             (None, 16)                144       
_________________________________________________________________
reshape_4 (Reshape)          (None, 256, 256, 3)       0         
_________________________________________________________________
conv2d_transpose_11 (Conv2DT (None, 258, 258, 16)      448       
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 129, 129, 16)      0         
_________________________________________________________________
conv2d_transpose_12 (Conv2DT (None, 131, 131, 3)       435       
=================================================================
Total params: 4,066,139
Trainable params: 4,066,139
Non-trainable params: 0
_________________________________________________________________

Answered by Tim on August 29, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP