我正在尝试创建一个 CNN 模型,用于基于带有 ELMo 嵌入的推文数据的文本分类。在编译之前构建模型后,我将发布模型摘要,但模型摘要显示 0 个参数、0 个可训练参数和 0 个不可训练参数。我认为问题可能在于输入形状或缺少填充,但不确定如何继续。这是我实现的代码。
from sklearn.model_selection import train_test_split
x = seeker_df[['tweet']]
y = seeker_df['BinaryNumTarget']
#splitting into validation and test - 85% of dataset if train and validation, 15% for test
x_train_val, x_test, y_train_val, y_test = train_test_split(x,
y,
test_size =0.15,
stratify = y, # sampling used to ensure class distribution
random_state = 42)
#splitting into train and val - 70% for training, 15% for validation
x_train, x_val, y_train, y_val = train_test_split(x_train_val,
y_train_val,
test_size =0.1765,
stratify = y_train_val,
random_state=42)
#package installation
from keras.models import Sequential
from keras.layers import Conv1D, GlobalMaxPooling1D, Dense
from keras.preprocessing.sequence import pad_sequences
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
from keras.callbacks import EarlyStopping
#setting up parameters
no_of_filters = 250
kernel_size = 3
hidden_dims = 250
batch_size = 32
epochs = 2
early_stopping = EarlyStopping(monitor ='val_loss', patience = 10)
#obtaining elmo emmbedding
elmo = hub.KerasLayer("https://tfhub.dev/google/elmo/2",
trainable=True,
name = 'elmo_embedding',
input_shape=[],
dtype=(tf.string))
embedding = tf.keras.layers.Lambda(elmo)
model = Sequential()
model.add(embedding)
model.add(Conv1D(no_of_filters, kernel_size, activation ='relu'))
model.add(GlobalMaxPooling1D())
model.add(Dense(hidden_dims, activation = 'relu'))
model.add(Dense(1, activation = 'sigmoid'))
model.summary()
这是我目前获得的模型摘要。任何帮助将不胜感激。我知道纪元和提前停止目前没有意义,但首先要运行低纪元来进行测试。
Model: "sequential_5"
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┓
┃ Layer (type) ┃ Output Shape ┃ Param # ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━┩
│ lambda_3 (Lambda) │ ? │ 0 (unbuilt) │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ conv1d_3 (Conv1D) │ ? │ 0 (unbuilt) │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ global_max_pooling1d_3 │ ? │ 0 │
│ (GlobalMaxPooling1D) │ │ │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ dense_6 (Dense) │ ? │ 0 (unbuilt) │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ dense_7 (Dense) │ ? │ 0 (unbuilt) │
└──────────────────────────────────────┴─────────────────────────────┴─────────────────┘
Total params: 0 (0.00 B)
Trainable params: 0 (0.00 B)
Non-trainable params: 0 (0.00 B)
我对您的代码做了一些更改。为了保持预训练的 ELMo 模型固定, 我已将其可训练参数设置为 False。我还使用 tf.keras 添加 层到顺序模型并将 ELMo 输出重塑为 3D 格式, 与 Conv1D 层兼容。这些改变使得代码 操作。详细查看更新后的代码,请参阅 要点.