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

Tensorflow ValueError:输入0与层conv1d_1不兼容:预期ndim=3,发现ndim=2,tensorflow,keras,Tensorflow,Keras,当我尝试将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

当我尝试将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)

一个
Conv1D
(批处理、步骤、通道)
。在您的案例中缺少通道维度,即使它等于1,也需要包含它。因此,elmo模块的输出形状应该是
(1024,1)
(这不包括批量大小)。您可以使用
tf.expand_dims(x,axis=-1)
向elmo模块的输出添加维度