tensorflow - 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)
解决方案
一个Conv1D
层需要 shape 的输入 (batch, steps, channels)
。您的情况下缺少通道维度,即使它等于 1,您也需要包含它。所以您的 elmo 模块的输出形状应该是(1024, 1)
(这不包括批量大小)。您可以使用 为 elmo 模块的输出添加维度tf.expand_dims(x, axis=-1)
。
推荐阅读
- mysql - 带有 if 条件的 MySQL 子查询
- reactjs - 使用 reactjs Material ui 在 3 列中显示我的卡片
- r - 为什么我的二项式 GLM 有多个预测线?
- c++ - 如何在凯撒密码解密作业中使用英语字母频率?
- javascript - 全局作用域和对象作用域中的上下文和词法环境
- cryptography - 在 Solidity 中进行加密
- postgresql - postgres的主要作用不是postgres,如何设置为postgress
- github-api - Octokit:PUT /repos/{owner}/{repo}/contents/{path} 状态:404
- xml - MS Access 导出到特定的 XML 格式
- javascript - 如何为 Google 地图的“未加载地图”部分设置样式