Home > Software engineering >  From multidimensional array to other shape in Keras
From multidimensional array to other shape in Keras

Time:11-12

I have 4 inputs with shape (batch_train, 128, 30000). I want to have as output a softmax layer of all 4 inputs. This is my code:

inp1.shape = (batch_train, 128, 30000)
inp2.shape = (batch_train, 128, 30000)
inp3.shape = (batch_train, 128, 30000)
inp4.shape = (batch_train, 128, 30000)


conc = tf.keras.layers.Concatenate(axis=0)([inp1[tf.newaxis], inp2[tf.newaxis], inp3[tf.newaxis], inp4[tf.newaxis]])
dense = tf.keras.layers.Dense(num_x)(concat)
drp= tf.keras.layers.Dropout(0.1)(dense)
output = tf.keras.layers.Dense(4)(drp)

However, my output shape is (4, batch_train, 128, 30000, 4). My desired output is (train_batch, 4). What am I doing wrong?

CodePudding user response:

I am not sure on which axis you want to concatenate, but you will have to flatten your tensor if you want a 1D output:

import tensorflow as tf

inp1 = tf.keras.layers.Input((128, 5))
inp2 = tf.keras.layers.Input((128, 5))
inp3 = tf.keras.layers.Input((128, 5))
inp4 = tf.keras.layers.Input((128, 5))

conc = tf.keras.layers.Concatenate(axis=1)([inp1, inp2, inp3, inp4])
flatten = tf.keras.layers.Flatten()(conc)
dense = tf.keras.layers.Dense(100)(flatten)
drp= tf.keras.layers.Dropout(0.1)(dense)
output = tf.keras.layers.Dense(4)(drp)
model = tf.keras.Model([inp1, inp2, inp3, inp4], output)

batch_size = 5
inputs1, inputs2, inputs3, inputs4 = tf.random.normal((5, 128, 5)), tf.random.normal((5, 128, 5)), tf.random.normal((5, 128, 5)), tf.random.normal((5, 128, 5))
print(model([inputs1, inputs2, inputs3, inputs4]).shape)
(5, 4)

You will, however, probably run into an "Out Of Memory" error using such large input shapes.

  • Related