简体   繁体   English

类型错误:添加的图层必须是类图层的实例。 找到:Tensor("input_2:0", shape=(?, 22), dtype=float32)

[英]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.

相关问题 类型错误:添加的图层必须是类图层的实例。 找到:Tensor(&quot;concatenate_6/concat:0&quot;, shape=(None, 4608), dtype=float32) - TypeError: The added layer must be an instance of class Layer. Found: Tensor("concatenate_6/concat:0", shape=(None, 4608), dtype=float32) TypeError:添加的层必须是 class 层的实例。 找到:Tensor(“input_1:0”, shape=(None, 64, 64, 3), dtype=float32) -Python - TypeError: The added layer must be an instance of class Layer. Found: Tensor(“input_1:0”, shape=(None, 64, 64, 3), dtype=float32) -Python 添加的层必须是 class 层的实例。 找到:张量(“conv1d_12/Relu:0”, shape=(?, 41, 64), dtype=float32) - The added layer must be an instance of class Layer. Found: Tensor(“conv1d_12/Relu:0”, shape=(?, 41, 64), dtype=float32) raise TypeError(&#39;The added layer must be &#39; TypeError: The added layer must be an instance of class Layer.Found: tf.Tensor - raise TypeError('The added layer must be ' TypeError: The added layer must be an instance of class Layer. Found: tf.Tensor 来自“tf.keras.layers.concatenate”的类型错误:添加的层必须是类层的实例。 发现:张量 - TypeError from "tf.keras.layers.concatenate": The added layer must be an instance of class Layer. Found: Tensor 添加的图层必须是类图层的实例。 成立:<tensorflow.python.keras.engine.input_layer.InputLayer> - The added layer must be an instance of class Layer. Found: <tensorflow.python.keras.engine.input_layer.InputLayer> 图断开连接:无法在“input_1”层获取张量 Tensor("input_1:0", shape=(None, 299, 299, 3), dtype=float32) 的值 - Graph disconnected: cannot obtain value for tensor Tensor("input_1:0", shape=(None, 299, 299, 3), dtype=float32) at layer "input_1" 类型错误:添加的图层必须是类图层的实例。 成立:<keras.engine.input_layer.InputLayer object at 0x7fc6f1b92240> - TypeError: The added layer must be an instance of class Layer. Found: <keras.engine.input_layer.InputLayer object at 0x7fc6f1b92240> 类型错误:添加的图层必须是类图层的实例。 成立:<keras.layers.core.Dropout object at 0x000001622999A5F8> - TypeError: The added layer must be an instance of class Layer. Found: <keras.layers.core.Dropout object at 0x000001622999A5F8> 图表断开:无法在“input_5”层获取张量 Tensor(“input_5:0”, shape=(None, None, None, 128), dtype=float32) 的值 - Graph disconnected: cannot obtain value for tensor Tensor(“input_5:0”, shape=(None, None, None, 128), dtype=float32) at layer “input_5”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM