首页 > 解决方案 > ValueError:无法将 NumPy 数组转换为张量(不支持的对象类型 numpy.ndarray)

问题描述

tfidf_Train 和 features_Train 是包含浮点数的列表列表,即 [[0.14, 0.22...],[0.52,0.34]] 我尝试使用 np.asarray() 将变量转换为 np 数组,但底部仍然出现错误拟合我的模型时在代码下方。感谢任何帮助。

    inp = Input(shape=(sen_Len,))
    embed = Embedding(len(term_Index)+1, emb_Dim, weights=[emb_Mat], 
    input_length=sen_Len, trainable=False)(inp)
    emb_input = LSTM(60, dropout=0.1, recurrent_dropout=0.1)(embed)
    tfidf_i = Input(shape=(1,))
    conc = Concatenate()([emb_input, tfidf_i])
    drop = Dropout(0.2)(conc)
    dens = Dense(2)(drop)
    acti = Activation('sigmoid')(dens)

    model = Model([inp, tfidf_i], acti)

    model.compile(optimizer='adam', loss='binary_crossentropy', metrics = 
    ['accuracy'])

    history = model.fit([features_Train,tfidf_Train], target_Train, epochs = 50, 
    batch_size=128, validation_split=0.2)

错误:

    x = _process_numpy_inputs(x)
    File "/home/stud/henrikm/anaconda3/lib/python3.7/site- 
    packages/tensorflow_core/python/keras/engine/data_adapter.py", line 1048, in 
    _process_numpy_inputs
    inputs = nest.map_structure(_convert_non_tensor, inputs)
    File "/home/stud/henrikm/anaconda3/lib/python3.7/site- 
    packages/tensorflow_core/python/util/nest.py", line 568, in map_structure
    structure[0], [func(*x) for x in entries],
    File "/home/stud/henrikm/anaconda3/lib/python3.7/site- 
    packages/tensorflow_core/python/util/nest.py", line 568, in <listcomp>
    structure[0], [func(*x) for x in entries],
    File "/home/stud/henrikm/anaconda3/lib/python3.7/site- 
    packages/tensorflow_core/python/keras/engine/data_adapter.py", line 1045, in 
    _convert_non_tensor
    return ops.convert_to_tensor(x)
    File "/home/stud/henrikm/anaconda3/lib/python3.7/site- 
    packages/tensorflow_core/python/framework/ops.py", line 1314, in convert_to_tensor
    ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
    File "/home/stud/henrikm/anaconda3/lib/python3.7/site- 
    packages/tensorflow_core/python/framework/tensor_conversion_registry.py", line 52, 
    in _default_conversion_function
    return constant_op.constant(value, dtype, name=name)
    File "/home/stud/henrikm/anaconda3/lib/python3.7/site- 
    packages/tensorflow_core/python/framework/constant_op.py", line 258, in constant
    allow_broadcast=True)
    File "/home/stud/henrikm/anaconda3/lib/python3.7/site- 
    packages/tensorflow_core/python/framework/constant_op.py", line 266, in 
    _constant_impl
    t = convert_to_eager_tensor(value, ctx, dtype)
    File "/home/stud/henrikm/anaconda3/lib/python3.7/site- 
    packages/tensorflow_core/python/framework/constant_op.py", line 96, in 
    convert_to_eager_tensor
    return ops.EagerTensor(value, ctx.device_name, dtype)
    ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type 
    numpy.ndarray).

标签: tensorflowkeraslstmtf-idfembedding

解决方案


我通过使用 Sequential 模型解决了这个问题,删除了第 5 行和第 6 行(我只使用了一个输入层)并使用 np.concatenate 而不是 Concatenate 层将 tfidf_Train 连接到 features_Train。


推荐阅读