简体   繁体   English

我尝试使用功能 API 在 tensorflow 2.x 中创建 model,但出现 LSTM 层不兼容错误

[英]I tried to create model in tensorflow 2.x using functional API, but got LSTM layers incompatible error

Error reads:错误读取:

Input 0 of layer lstm_28 is incompatible with the layer: expected ndim=3, found ndim=4. Full shape received: [None, None, 15, 12]

In LSTM layer, input tf.nn.embedding_lookup(embedding, neighbor) have shape =(15,12) and one None is for batch size, how it comes the size of [None, None, 15,12]?在 LSTM 层中,输入tf.nn.embedding_lookup(embedding, neighbor)的形状 =(15,12),一个None是批量大小,它的大小是怎么来的 [None, None, 15,12]? How to deal with this error?如何处理这个错误? Below is the dummy model that I created.下面是我创建的虚拟 model。

    def create_model(embedding, embedding_dim, samp_size):
        
        
        node = Input(shape=(None,), dtype=tf.int64)
        neighbor = Input(shape=(None, samp_size), dtype=tf.int64)
        label = Input(shape=(None,), dtype=tf.int64)
        
        cell = LSTMCell(embedding_dim,)
        _,h,c = LSTM(embedding_size, return_sequences=True, return_state=True)(tf.nn.embedding_lookup(embedding, neighbor))
        predict_info = tf.squeeze(Dense(1, activation='relu'))(h)
        
        return h
    
    
    
    node_size = 1000
    embedding_dim = 12
    sampling_size = 15
    embedding = tf.random.uniform([node_size, embedding_dim])
    
    model = create_model (embedding, embedding_dim, sampling_size)

when using Keras functional API, do not include the None for batch dimension.当使用 Keras 功能 API 时,不要将 None 用于批处理维度。 For example, if your input is of dimension (batch_size, image_w, image_h, image_channels) do it like this:例如,如果您的输入尺寸为 (batch_size, image_w, image_h, image_channels),请执行以下操作:

inp = tf.keras.Input(shape=(IMG_W, IMG_H, IMG_CH))

暂无
暂无

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

相关问题 如何使用功能 API 模型实现 CNN 并解决 keras 层中的“_keras_shape”错误 - How do I implement CNN using Functional API model and resolve '_keras_shape' error in keras layers 如何在 tensorflow 2.x 中应用等效的 LSTM? - How to apply equivalent LSTM in tensorflow 2.x? 当我在张量流中得到卷积层的变量时出现错误 - I got a error when I got the variables of convolution layers in tensorflow 如何在 Keras 功能 API 中创建具有多个共享层的 model? - How to create a model with multiple shared layers in Keras Functional API? 从 TensorFlow 2.x 中的特定层移除层/提取特征 - Removing layers/extracting features from specific layers in TensorFlow 2.x 在功能 API keras model 中使用 datagen.flow_from_directory() 出现不兼容的形状错误 - incompatible shapes error using datagen.flow_from_directory() in a functional API keras model ValueError:使用 RandomizedSearchCV 的 Tensorflow LSTM 中的形状不兼容 - ValueError: Shapes are incompatible in Tensorflow LSTM using RandomizedSearchCV TensorFlow 2.x:使用嵌入列时无法加载 h5 格式的训练模型(ValueError: Shapes (101, 15) and (57218, 15) is incompatible) - TensorFlow 2.x: Cannot load trained model in h5 format when using embedding columns (ValueError: Shapes (101, 15) and (57218, 15) are incompatible) 通过for循环使用功能api创建keras输入层? - create keras input layers using the functional api through a for loop? Keras LSTM (TensorFlow 2.x) 中的动态批量大小 - Dynamic batch size in Keras LSTM (TensorFlow 2.x)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM