我正在尝试在keras中连接两个并行模型,每个模型具有不同的输入。相关代码如下。
# model 1
model1_in = Input(shape=(train_x_1.shape[1], train_x_1.shape[2]))
model1_out = LSTM(50, activation='relu',return_sequences=False, name='layer_1')(model1_in)
model1 = Model(model1_in, model1_out)
# model 2
model2_in = Input(shape=(1))
model2_out = Dense(8, activation='relu', name='layer_2')(model2_in)
model2 = Model(model2_in, model2_out)
concatenated = concatenate(inputs=[model1.output, model2.output])
out = Dense(1, activation='relu', name='output_layer')(concatenated)
model = Model([model1_in, model2_in], out)
model.compile(loss='mean_absolute_error', optimizer='adam')
# fit network
history = model.fit([train_x_1,train_x_2], train_y, epochs=100, batch_size=72, validation_data=([test_x_1,test_x_2], test_y), verbose=2, shuffle=False)
我得到的错误是
ValueError:无法将NumPy数组转换为张量(不受支持的对象类型float)。
并出现在model.fit行。
我在IDLE中跑步。训练和测试值都是数组,我检查了所有训练输入的长度是否相同:
#train_x_1.shape[0]
15465
#train_y.shape[0]
15465
#train_x_2.shape[0]
15465
#test_x_1.shape[0]
1719
#test_x_2.shape[0]
1719
#test_y.shape[0]
1719
#test_x_1
array([[[0.6243922 ],
[0.5463666 ],
[0.7083546 ], ... etc ...
任何帮助将不胜感激-预先感谢!
完整的错误跟踪如下:
回溯(最近一次通话最近):文件“ filepath .py”,第220行, 在 历史= model.fit([train_x_1,train_x_2],train_y,epochs = 100,batch_size = 72,validation_data =([[test_x_1,test_x_2],test_y), verbose = 2,shuffle = False)文件 “ /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training.py”, 728行,适合 use_multiprocessing = use_multiprocessing)文件“ /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training_v2.py”,第224行,适合 distribution_strategy = strategy)文件“ /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training_v2.py”,第547行,位于_process_training_inputs中 use_multiprocessing = use_multiprocessing)文件“ /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training_v2.py”,行606,在_process_inputs中 use_multiprocessing = use_multiprocessing)文件“ /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/data_adapter.py”, 第217行,在 init 中 x = _process_numpy_inputs(x)文件“ /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/data_adapter.py”, _process_numpy_inputs中的第703行 输入= nest.map_structure(_convert_non_tensor,输入)文件“ /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow_core/python/util/nest.py”, map_structure中的第535行 结构[0],[用于x的func(* x)条目],文件“ /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow_core/python/util/nest。 py”, 535行,在 结构[0],[用于x的func(* x)条目],文件“ /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/ data_adapter.py”, 第700行,_convert_non_tensor中 返回ops.convert_to_tensor(x)文件“ /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py”, 第1184行,在convert_to_tensor中 返回convert_to_tensor_v2(值,dtype,preferred_dtype,名称)文件 “ /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py”, 第1242行,在convert_to_tensor_v2中 as_ref = False)文件“ /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py”, 第1296行,位于internal_convert_to_tensor中 ret = conversion_func(值,dtype = dtype,名称=名称,as_ref = as_ref)文件 “ /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow_core/python/framework/tensor_conversion_registry.py”, _default_conversion_function中的第52行 返回constant_op.constant(value,dtype,name = name)文件“ /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow_core/python/framework/constant_op.py”, 第227行,常量 allow_broadcast = True)文件“ /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow_core/python/framework/constant_op.py”, _constant_impl中的第235行 t = convert_to_eager_tensor(value,ctx,dtype)文件“ /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow_core/python/framework/constant_op.py”, 第96行,在convert_to_eager_tensor中 返回ops.EagerTensor(value,ctx.device_name,dtype)ValueError:无法将NumPy数组转换为张量(不支持的对象类型 浮动)。
答案 0 :(得分:1)
为了社区的利益,在“答案”部分中指定解决方案(即使它在“评论”部分中也存在)。
x_2的值都为 float
类型,而 x_1 values
则为 float32
强>。
使用 x2
将 float32
修改为 x2.astype('float32')
已解决了该问题。