首页 > 解决方案 > ValueError:输入 0 与层 conv1d_1 不兼容:预期 ndim=3,发现 ndim=2

问题描述

当我尝试将 Elmo 嵌入层输出提供给 conv1d 层输入时,它会给出错误

ValueError:输入 0 与层 conv1d_1 不兼容:预期 ndim=3,发现 ndim=2

我想从 Elmo 嵌入层的输出中添加一个卷积层

import tensorflow as tf
import tensorflow_hub as hub
import keras.backend as K
from keras import Model
from keras.layers import Input, Lambda, Conv1D, Flatten, Dense
from keras.utils import to_categorical
from sklearn.preprocessing import LabelEncoder
import pandas as pd
from sklearn.model_selection import train_test_split

df = pd.read_csv("/home/raju/Desktop/spam.csv", encoding='latin-1')
X = df['v2']
Y = df['v1']

le = LabelEncoder()
le.fit(Y)

Y = le.transform(Y)
Y = to_categorical(Y)

X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.25)

elmo = hub.Module('/home/raju/models/elmo')


def embeddings(x):
    return elmo(tf.squeeze(tf.cast(x, dtype=tf.string)), signature='default', as_dict=True)['default']


input_layer = Input(shape=(1,), dtype=tf.string)
embed_layer = Lambda(embeddings, output_shape=(1024,))(input_layer) 
conv_layer = Conv1D(4, 2, activation='relu')(embed_layer)
fcc_layer = Flatten()(conv_layer)
output_layer = Dense(2, activation='softmax')(fcc_layer)

model = Model(inputs=[input_layer], outputs=output_layer)

标签: tensorflowkeras

解决方案


一个Conv1D需要 shape 的输入 (batch, steps, channels)。您的情况下缺少通道维度,即使它等于 1,您也需要包含它。所以您的 elmo 模块的输出形状应该是(1024, 1)(这不包括批量大小)。您可以使用 为 elmo 模块的输出添加维度tf.expand_dims(x, axis=-1)


推荐阅读