简体   繁体   English

自动编码器/Keras/Tensorflow:密集层与层不兼容:输入形状的预期轴 -1 的值为 64

[英]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.我刚开始使用 ML(尤其是自动编码器),但在运行代码时遇到了问题。

I have built an input vector "x" as "artificial data", and I am trying to reduced the dimensionality of this "artificial data" using autoencoder.我已经构建了一个输入向量“x”作为“人工数据”,并且我正在尝试使用自动编码器来降低这个“人工数据”的维数。

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!非常感谢!

The dense.network is very accurate when using sin for the pattern matching. dense.network 在使用 sin 进行模式匹配时非常准确。

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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 层密集的输入 0 与层不兼容:输入形状的预期轴 -1 具有值 3,但收到的输入形状为 (None, 1) - Input 0 of layer dense is incompatible with the layer: expected axis -1 of input shape to have value 3 but received input with shape (None, 1) ValueError:密集层的输入 0 与层不兼容:预期轴 -1 的值为 8,但接收到形状为 [None, 1] 的输入 - ValueError: Input 0 of layer dense is incompatible with the layer: expected axis -1 to have value 8 but received input with shape [None, 1] 密集层的输入 0 与该层不兼容:输入形状的预期轴 -1 具有值 8192,但接收到的输入具有形状(无,61608) - Input 0 of layer dense is incompatible with the layer: expected axis -1 of input shape to have value 8192 but received input with shape (None, 61608) 密集层的输入 0 与该层不兼容:输入形状的预期轴 -1 的值为 2700,但接收到的输入形状为 [None, 900] - Input 0 of layer dense is incompatible with the layer: expected axis -1 of input shape to have value 2700 but received input with shape [None, 900] 层密集_18 的输入 0 与层不兼容:输入形状的预期轴 -1 具有值 3500,但接收到形状为 [None, 7] 的输入 - Input 0 of layer dense_18 is incompatible with the layer: expected axis -1 of input shape to have value 3500 but received input with shape [None, 7] conv2d 层的输入 0 与层不兼容:输入形状的预期轴 -1 具有值 1,但接收到形状为 [None, 64, 64, 3] 的输入 - Input 0 of layer conv2d is incompatible with layer: expected axis -1 of input shape to have value 1 but received input with shape [None, 64, 64, 3] ValueError:密集层的输入 0 与层不兼容:预期轴 -1。 tensorflow - ValueError: Input 0 of layer dense is incompatible with the layer: expected axis -1. tensorflow ValueError:dense_24 层的输入 0 不兼容:输入形状的预期轴 -1 具有值 1024,但接收到形状为 [16, 512] 的输入 - ValueError: Input 0 of layer dense_24 is incompatible: expected axis -1 of input shape to have value 1024 but received input with shape [16, 512] 层序的输入 0 与层不兼容:输入形状的预期轴 -1 具有值 3,但收到的输入具有形状 - Input 0 of layer sequential is incompatible with the layer: expected axis -1 of input shape to have value 3 but received input with shape 层顺序的输入 0 与层不兼容:输入形状的预期轴 -1 具有值 8,但收到的输入具有形状(无,71) - Input 0 of layer sequential is incompatible with the layer: expected axis -1 of input shape to have value 8 but received input with shape (None, 71)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM