You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Keras调用TF Hub的ELMO层训练报错Unsupported object type int

问题修复方案

错误根因

  • x_data初始化时用np.zeros生成了int类型的默认填充值,若样本文件行数不足51行,未被赋值的位置仍然是int类型的0,Elmo模块要求输入全为字符串类型,遇到int值就抛出该报错。
  • TensorFlow 1.x版本下Keras模型未绑定当前创建的会话,模型和会话不共享上下文,也可能触发类型解析异常。
  • Lambda层的output_shape参数配置错误,Elmo输出的是三维张量(batch_size, sequence_length, 1024),你配置的(1024, )和实际输出形状不匹配,会导致后续LSTM层接收的形状异常。

修复步骤

  1. 动态根据样本文件的行数生成x_data和y_data,不要固定max_cells为51,避免出现未赋值的int填充值。
  2. 把Keras的后端会话设置为你创建的tf.Session,保证模型和会话共享上下文。
  3. 修正Lambda层的output_shape配置,匹配Elmo的实际输出形状。
  4. 增加空行过滤逻辑,避免无效的空字符串输入。

修改后可运行代码

import tensorflow_hub as hub
import tensorflow as tf
from tensorflow.keras import backend as K
from tensorflow.keras.layers import Input, Lambda, Bidirectional, Dense, Dropout, LSTM
from tensorflow.keras.models import Model
import numpy as np
import io
import re

# 初始化Elmo模块
elmo = hub.Module("https://tfhub.dev/google/elmo/3", trainable=True)

def ELMoEmbedding(input_text):
    return elmo(tf.reshape(tf.cast(input_text, tf.string), [-1]), signature="default", as_dict=True)["elmo"]

def build_model():
    input_layer = Input(shape=(1,), dtype="string", name="Input_layer")    
    # 修正output_shape配置,匹配Elmo输出的三维结构
    embedding_layer = Lambda(ELMoEmbedding, output_shape=(None, 1024, ), name="Elmo_Embedding")(input_layer)
    BiLSTM = Bidirectional(LSTM(128, return_sequences= False, recurrent_dropout=0.2, dropout=0.2), name="BiLSTM")(embedding_layer)
    Dense_layer_1 = Dense(64, activation='relu')(BiLSTM)
    Dropout_layer_1 = Dropout(0.5)(Dense_layer_1)
    Dense_layer_2 = Dense(32, activation='relu')(Dropout_layer_1)
    Dropout_layer_2 = Dropout(0.5)(Dense_layer_2)
    output_layer = Dense(1, activation='sigmoid')(Dropout_layer_2)
    model = Model(inputs=[input_layer], outputs=output_layer, name="BiLSTM with ELMo Embeddings")
    model.summary()
    model.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])
    return model

elmo_BiDirectional_model = build_model()

# 动态读取样本,不固定长度避免填充int值
x_data = []
y_data = []
with io.open('./data/names-sample.txt', encoding='utf-8') as f:
    content = f.readlines()
    for line in content:        
        line = re.sub("\n", " ", line).strip()
        if line: # 跳过空行
            x_data.append(line)
            y_data.append(0.1) # 测试用标签

# 转为符合输入要求的numpy数组
x_data = np.array(x_data, dtype=object).reshape(-1, 1)
y_data = np.array(y_data, dtype='float32').reshape(-1, 1)

# 绑定会话到Keras后端
with tf.Session() as session:
    K.set_session(session) # 关键配置,让keras使用当前创建的会话
    session.run(tf.global_variables_initializer()) 
    session.run(tf.tables_initializer())
    model_elmo = elmo_BiDirectional_model.fit(x_data, y_data, epochs=100, batch_size=5)
    train_prediction = elmo_BiDirectional_model.predict(x_data)

内容的提问来源于stack exchange,提问作者webber

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 14:15:01