ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray)

ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type 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).

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