'Autoencoder/Keras/Tensorflow: Dense layer is incompatible with the layer: expected axis -1 of input shape to have value 64

I just started with ML (Autoencoders in particular) and I having problems to make my code run.

I have built an input vector "x" as "artificial data", and I am trying to reduced the dimensionality of this "artificial data" using autoencoder.

import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
from tensorflow.keras import layers
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Lambda
import tensorflow.keras.backend as K
from keras.models import Input, Model, load_model
from keras.layers import Dense
from sklearn.model_selection import train_test_split 

N=64

z1=tf.linspace(0,1,N)
z2=tf.linspace(0,2,N)
z3=tf.linspace(0,3,N)
z4=tf.linspace(0,4,N)
z5=tf.linspace(0,5,N)


y1=np.sin(z1)
y2=np.sin(z2)
y3=np.sin(z3)
y4=np.sin(z4)
y5=np.sin(z5)


x=tf.concat([y1,y2,y3,y4,y5,z1,z2,z3,z4,z5],0)
x=np.matrix(x).T


main_input = layers.Input(shape=(N,), name='main_input')
encoded = Dense(32, activation='tanh')(main_input) 
decoded = Dense(N, activation='tanh')(encoded)

ae = Model(inputs=main_input, outputs=decoded)

print('Full autoencoder') 
print(ae.summary())
print('\n Encoder portion of autoencoder') # print(encoder.summary())


ae.compile(optimizer='adam', loss='mse', metrics=['mse'])
batch_size = 2
epochs = 100

x_train, x_test,  _, _ = train_test_split(x, x, test_size=0.33, random_state=42)



results = ae.fit(x_train,x_train,
                  batch_size = batch_size,
                  epochs = epochs,
                  validation_data = (x_train,x_train))

I am getting the following error:

 ValueError: Exception encountered when calling layer "model" (type Functional).
    
    Input 0 of layer "dense" is incompatible with the layer: expected axis -1 of input shape to have value 64, but received input with shape (2, 1)
    
    Call arguments received:
      • inputs=tf.Tensor(shape=(2, 1), dtype=float32)
      • training=True
      • mask=None

thanks a lot in advance!



Solution 1:[1]

The dense network is very accurate when using sin for the pattern matching.

from tensorflow.keras.models import Sequential
from tensorflow.keras import layers
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Lambda
import tensorflow.keras.backend as K
 from keras.models import Input, Model, load_model
from keras.layers import Dense
from sklearn.model_selection import train_test_split 

N=64
z1=np.linspace(0,1,N)
z2=np.linspace(0,2,N)
z3=np.linspace(0,3,N)
z4=np.linspace(0,4,N)
z5=np.linspace(0,5,N)

y1=np.sin(z1)**2
y2=np.sin(z2)**3
y3=np.sin(z3)
y4=np.sin(z4)
y5=np.sin(z5)


X=np.concatenate((z1,z2,z3,z4,z5))
y=np.concatenate((y1,y2,y3,y4,y5,))

#y=np.matrix(y).T
#plt.plot(X,y)
X_train, X_test, y_train, y_test= train_test_split(X,y,test_size=0.3)

model=Sequential()
model.add(layers.Input(shape=(1,), name='main_input'))
model.add(Dense(200, activation='tanh')) 
model.add(Dense(100, activation='tanh')) 
model.add(Dense(32, activation='tanh')) 
model.add(Dense(1))

model.compile(optimizer='adam', loss='mse', metrics=['mse'])

history=model.fit(X_train, y_train,  epochs=1000, verbose=0)

predictionResults=model.predict(X_test)

index=0
results=predictionResults.flatten()
for value in X_test:
    plt.scatter(value,results[index])
    index+=1
plt.plot(X,y)
plt.show()

plt.plot(history.history['loss'])
plt.title('loss accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()

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