'Input 0 of layer "conv1d_5" is incompatible with the layer: expected min_ndim=3, found ndim=1. Full shape received: (None,) when predicting new values

I have trained a VAE and now I am trying to predict new data using this dataset. I have taken the predict code line from this tutorial. However, when I run the line I get: Input 0 of layer "conv1d_5" is incompatible with the layer: expected min_ndim=3, found ndim=1. Full shape received: (None,). I have used keras VAE tutorial and adapted it myself.

data:

df = pd.read_csv('local path')
data, data_test = train_test_split(df, test_size=0.20)
data = np.asarray(data).astype(np.float32)
data = tf.expand_dims(data, axis=-1)
latent_dim = 10
leaky_relu = tf.nn.leaky_relu
data.shape #TensorShape([820007, 11, 1])

decoder:

latent_inputs = keras.Input(shape=(latent_dim, 1))
x = layers.Conv1D(32, 3, activation=leaky_relu)(latent_inputs)
x = layers.BatchNormalization(momentum=0.8)(x)
x = layers.Activation('relu')(x)
x = layers.Dropout(0.20)(x)
x = layers.Conv1D(32, 3, activation=leaky_relu, padding='same')(x)
x = layers.BatchNormalization(momentum=0.8)(x)
x = layers.Activation('relu')(x)
x = layers.Dropout(0.20)(x)
x = layers.Conv1D(64, 3, activation=leaky_relu, padding='same')(x)
x = layers.BatchNormalization(momentum=0.8)(x)
x = layers.Activation('relu')(x)
x = layers.Dropout(0.20)(x)
x = layers.Conv1D(64, 3, activation=leaky_relu, padding='same')(x)
x = layers.BatchNormalization(momentum=0.8)(x)
x = layers.Activation('relu')(x)
x = layers.Dropout(0.20)(x)
x = layers.Conv1D(128, 3, activation=leaky_relu, padding='same')(x)
x = layers.BatchNormalization(momentum=0.8)(x)
x = layers.Activation('relu')(x)
x = layers.Dropout(0.20)(x)
x = layers.Conv1D(256, 3, activation=leaky_relu, padding='same')(x)
x = layers.BatchNormalization(momentum=0.8)(x)
x = layers.Activation('relu')(x)
x = layers.Dropout(0.20)(x)
x = layers.Flatten()(x)
x = layers.BatchNormalization(momentum=0.8)(x)
x = layers.Activation('relu')(x)
x = layers.Dropout(0.20)(x)
x = layers.Dense(data.shape[1], activation='sigmoid')(x)
x = Reshape((data.shape[1], 1))(x)
decoder_outputs = layers.Dense(data.shape[1], activation='sigmoid')(x)
decoder = keras.Model(latent_inputs, x, name='decoder')
decoder.summary()

training:

earlystopper = EarlyStopping(monitor='kl_loss', mode='min', min_delta=0.005, patience=20, verbose=0, restore_best_weights=True)

vae = VAE(encoder, decoder)  

vae.compile(optimizer=keras.optimizers.Adam(learning_rate=0.0000002))
hist = vae.fit(data, epochs=50, batch_size=128, callbacks=[earlystopper])

New data prediction:

new_data = vae.decoder.predict(np.random.normal(0,1,size=(latent_dim))) #Where I get the error

I am pretty new to VAEs so I don't know how to generate new data. Help is much appreciated



Solution 1:[1]

tf.keras.layers.Conv1D expects input of 3+D tensor with shape: batch_shape + (steps, input_dim)

Add extra dimension to your input data shape.

tf.expand_dims(input_shape, axis=0).shape.as_list()

(1,10,1)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 TFer