繁体   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

错误读取:

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

在 LSTM 层中,输入tf.nn.embedding_lookup(embedding, neighbor)的形状 =(15,12),一个None是批量大小,它的大小是怎么来的 [None, None, 15,12]? 如何处理这个错误? 下面是我创建的虚拟 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)

当使用 Keras 功能 API 时,不要将 None 用于批处理维度。 例如,如果您的输入尺寸为 (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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM