tensorflow - 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).
解决方案
我通过使用 Sequential 模型解决了这个问题,删除了第 5 行和第 6 行(我只使用了一个输入层)并使用 np.concatenate 而不是 Concatenate 层将 tfidf_Train 连接到 features_Train。
推荐阅读
- python - 如何打印字典中单词的频率
- r - 如何通过R中的多个嵌套块汇总数据框列中的唯一值
- json - 如何创建自定义 JSON 字符串/结构?
- google-apps-script - 反正有没有给匿名用户UserProperties?
- php - Laravel 忽略特定路线。改为打开另一个代码库
- java - 具有不同日期的 Spring Boot 应用程序
- three.js - 带有 alphaMap 的 SpriteMaterial
- sparql - RDF4j ParsedQuery 或 TupleExpr 到字符串表示
- python - Python 3.7 - Moviepy 将音频修剪为视频的长度
- android - 在空对象引用上调用的虚拟方法 kotlin