Skip to content
Advertisement

Tensorflow – Dense and Convolutional layers connection

I’m new to Deep Learning and I can’t find anywhere how to do the bottleneck in my AE with convolutional and dense layers. The code below is the specific part where I’m struggling:

...
encoded = Conv2D(8, (3, 3), activation='relu', padding='same')(encoded)

# encoded = Dense(2)(encoded) # Linear activation function at the bottleneck

decoded = Conv2D(8, (3, 3), activation='relu', padding='same')(decoded)
...

I tried some solutions, like flatten and reshape, but nothing seems to work here. The point is that I need the latent space to be a dense layer of 2 because I need to sample points [x,y] from it. I did it with MLP following this link (https://www.kaggle.com/code/apapiu/manifold-learning-and-autoencoders/notebook) and it worked, but I can’t manage to do the same with my structure.

Thanks in advice, and best regards!

Advertisement

Answer

Convolution2D takes the input of a 4+ Dimension tensor, hence you need to reshape the input before passing it to Convolution2D layer. You can use a model like below.

input_img = Input(shape=(784,))
input_img1 = Reshape(target_shape=(28,28,1))(input_img)
encoded = Convolution2D(8, (3, 3), activation='relu', padding='same')(input_img1)
encoded = Dense(2)(encoded)
decoded1 = Convolution2D(8, (3, 3), activation='relu', padding='same')(encoded)
decoded2 = Flatten()(decoded1)
decoded = Dense(784,)(decoded2)

Please refer to this gist for complete code with random data.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement