[英]TypeError: The added layer must be an instance of class Layer. Found: Tensor("input_2:0", shape=(?, 22), dtype=float32)
I am trying to add autoencoder layer to LSTM neural network.我正在尝试将自动编码器层添加到 LSTM 神经网络。 The input data is the pandas DataFrame with numerical features.输入数据是具有数值特征的pandas DataFrame。
To do this task, I am using Keras and Python My current code in Python is given below.为了完成这项任务,我使用了 Keras 和 Python 我在 Python 中的当前代码如下。
I cannot compile the model because I seem to mix Keras and Tensorflow:我无法编译模型,因为我似乎混合了 Keras 和 Tensorflow:
TypeError: The added layer must be an instance of class Layer. Found: Tensor("input_2:0", shape=(?, 22), dtype=float32)
I am quite new to both packages, and I'd appreciate if somebody could tell me how to fix this error.我对这两个软件包都很陌生,如果有人能告诉我如何解决这个错误,我将不胜感激。
nb_features = X_train.shape[2]
hidden_neurons = nb_classes*3
timestamps = X_train.shape[1]
NUM_CLASSES = 3
BATCH_SIZE = 32
input_size = len(col_names)
hidden_size = int(input_size/2)
code_size = int(input_size/4)
model = Sequential()
model.add(LSTM(
units=hidden_neurons,
return_sequences=True,
input_shape=(timestamps, nb_features),
dropout=0.15,
recurrent_dropout=0.20
)
)
input_vec = Input(shape=(input_size,))
# Encoder
hidden_1 = Dense(hidden_size, activation='relu')(input_vec)
code = Dense(code_size, activation='relu')(hidden_1)
# Decoder
hidden_2 = Dense(hidden_size, activation='relu')(code)
output_vec = Dense(input_size, activation='relu')(hidden_2)
model.add(input_vec)
model.add(hidden_1)
model.add(code)
model.add(hidden_2)
model.add(output_vec)
model.add(Dense(units=100,
kernel_initializer='normal'))
model.add(LeakyReLU(alpha=0.5))
model.add(Dropout(0.20))
model.add(Dense(units=200,
kernel_initializer='normal',
activation='relu'))
model.add(Flatten())
model.add(Dense(units=200,
kernel_initializer='uniform',
activation='relu'))
model.add(Dropout(0.10))
model.add(Dense(units=NUM_CLASSES,
activation='softmax'))
model.compile(loss="categorical_crossentropy",
metrics = ["accuracy"],
optimizer='adam')
The issue is that you are mixing Keras' sequential API with its functional API.问题是您将 Keras 的顺序 API 与其功能 API 混合在一起。 To fix your issue, you must replace:要解决您的问题,您必须更换:
input_vec = Input(shape=(input_size,))
# Encoder
hidden_1 = Dense(hidden_size, activation='relu')(input_vec)
code = Dense(code_size, activation='relu')(hidden_1)
# Decoder
hidden_2 = Dense(hidden_size, activation='relu')(code)
output_vec = Dense(input_size, activation='relu')(hidden_2)
With:和:
# Encoder
model.add(Dense(hidden_size, activation='relu'))
model.add(Dense(code_size, activation='relu'))
# Decoder
model.add(Dense(hidden_size, activation='relu'))
model.add(Dense(input_size, activation='relu'))
Or convert everything to the functional API或将所有内容转换为功能性 API
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.