ValueError:输入0与层conv1d_1不兼容:预期ndim = 3,找到的ndim = 2

时间:2019-01-09 20:44:34

标签: 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, 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)

1 个答案:

答案 0 :(得分:1)

Conv1Dexpects input of the shape (batch, steps, channels)。根据您的情况,通道尺寸会丢失,即使它等于1,也需要包括它。因此,elmo模块的输出形状应为(1024, 1)(不包括批次大小)。您可以使用tf.expand_dims(x, axis=-1)向elmo模块的输出添加尺寸。