Lambda Layer Tensorflow的多个输入



我试图在keras/tensorflow中的lambda层中创建一个非常简单的加法器(将两个标量张量相加)的示例。下面是我的最小示例:

import tensorflow as tf
from tensorflow import keras
def adder(x1,x2):
return tf.math.add(x1,x2)

我可以执行我的加法函数的快速测试:

# Check that the adder function works well for two scaler tensors
scaler1 = tf.convert_to_tensor(21.1)
scaler2 = tf.convert_to_tensor(12.0)
print(adder(scaler1,scaler2))

得到正确的输出:

tf.Tensor(33.1, shape=(), dtype=float32)

所以我现在定义了一个多输入keras网络,其中一个lambda层试图将两个输入加在一起:

inpAdder1 = keras.layers.Input((1), name="Input1")
inpAdder2 = keras.layers.Input((1), name="Input2")
doAdd = keras.layers.Lambda(lambda x: adder(x[0],x[1]),name="LambdaLayer")([inpAdder1,inpAdder2])
modelAdder = keras.Model([inpAdder1,inpAdder2],doAdd)

网络摘要显示正确:

Model: "model_12"
__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
Input1 (InputLayer)             [(None, 1)]          0                                            
__________________________________________________________________________________________________
Input2 (InputLayer)             [(None, 1)]          0                                            
__________________________________________________________________________________________________
LambdaLayer (Lambda)            (None, 1)            0           Input1[0][0]                     
Input2[0][0]                     
==================================================================================================
Total params: 0
Trainable params: 0
Non-trainable params: 0

但是,如果我试图调用一个简单的预测与相同的标量张量,我得到一个错误,我不知道如何调试:

modelAdder.predict([scaler1, scaler2])

错误回溯:

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<command-4319124675560319> in <module>
16 print(adder(scaler1,scaler2))
17 
---> 18 modelAdder.predict([scaler1, scaler2])
/databricks/python/lib/python3.8/site-packages/keras/engine/training.py in predict(self, x, batch_size, verbose, steps, callbacks, max_queue_size, workers, use_multiprocessing)
1718                         '. Consider setting it to AutoShardPolicy.DATA.')
1719 
-> 1720       data_handler = data_adapter.get_data_handler(
1721           x=x,
1722           batch_size=batch_size,
/databricks/python/lib/python3.8/site-packages/keras/engine/data_adapter.py in get_data_handler(*args, **kwargs)
1381   if getattr(kwargs["model"], "_cluster_coordinator", None):
1382     return _ClusterCoordinatorDataHandler(*args, **kwargs)
-> 1383   return DataHandler(*args, **kwargs)
1384 
1385 
/databricks/python/lib/python3.8/site-packages/keras/engine/data_adapter.py in __init__(self, x, y, sample_weight, batch_size, steps_per_epoch, initial_epoch, epochs, shuffle, class_weight, max_queue_size, workers, use_multiprocessing, model, steps_per_execution, distribute)
1136 
1137     adapter_cls = select_data_adapter(x, y)
-> 1138     self._adapter = adapter_cls(
1139         x,
1140         y,
/databricks/python/lib/python3.8/site-packages/keras/engine/data_adapter.py in __init__(self, x, y, sample_weights, sample_weight_modes, batch_size, epochs, steps, shuffle, **kwargs)
238     inputs = pack_x_y_sample_weight(x, y, sample_weights)
239 
--> 240     num_samples = set(int(i.shape[0]) for i in tf.nest.flatten(inputs)).pop()
241     _check_data_cardinality(inputs)
242 
/databricks/python/lib/python3.8/site-packages/keras/engine/data_adapter.py in <genexpr>(.0)
238     inputs = pack_x_y_sample_weight(x, y, sample_weights)
239 
--> 240     num_samples = set(int(i.shape[0]) for i in tf.nest.flatten(inputs)).pop()
241     _check_data_cardinality(inputs)
242 
/databricks/python/lib/python3.8/site-packages/tensorflow/python/framework/tensor_shape.py in __getitem__(self, key)
894       else:
895         if self._v2_behavior:
--> 896           return self._dims[key].value
897         else:
898           return self._dims[key]
IndexError: list index out of range

我有一种感觉,我在这里错过了一些容易的东西,但我试着看类似的问题(例如:如何将两个输入放入tensorflow lambda层),找不到我做错了什么。

经过进一步的调试,我已经解决了这个问题。这个问题是因为我在调用模型时没有包含批处理维度。预测功能。

将上面的预测行更改为:

modelAdder.predict([tf.expand_dims(scaler1,0), tf.expand_dims(scaler2,0)])

通过为批处理的每个张量添加一个额外的维度来解决这个问题。上述命令生成的张量为:

Out[48]: array([[33.1]], dtype=float32)

相关内容

  • 没有找到相关文章

最新更新