【问题标题】:TensorFlow Ranking Needs Functional API for DataFrame and TF.DatasetTensorFlow Ranking 需要 DataFrame 和 TF.Dataset 的功能 API
【发布时间】:2022-01-19 22:07:23
【问题描述】:

我正在尝试用我自己的数据帧中的数据(基本上是数字)来实现basic_rankingexample。我使用如下代码创建了一个 tf 数据集,使其类似于示例中描述的电影镜头数据集。

selected_features = ['feature1', 'feature2',...]
selected_target = 'target'
selected_features.append(selected_target)

tf_ds = tf.data.Dataset.from_tensor_slices(
    {
        f: tf.cast(df_agent_ranking_data[f].values, tf.float32) for f in selected_features
    }
)

创建排名模型如下:

class RankingModel(tf.keras.Model):
    def __init__(self):
        super().__init__()
        
        self.rank = tf.keras.Sequential([
            tf.keras.layers.Dense(128, activation='relu'),
            tf.keras.layers.Dense(128, activation='relu'),
            tf.keras.layers.Dense(1),
        ])
        
    def call(self, inputs):
        return self.rank(inputs)

class TestRankingModel(tfrs.models.Model):
    def __init__(self):
        super().__init__()
        self.ranking_model: tf.keras.Model = RankingModel()
            
        self.task: tf.keras.layers.Layer = tfrs.tasks.Ranking(
            loss = tf.keras.losses.MeanSquaredError(),
            metrics = [tf.keras.metrics.RootMeanSquaredError()]
        )
            
    def call(self, features: Dict[Text, tf.Tensor]) -> tf.Tensor:
        c = (features[f] for f in list(set(selected_features)-set(selected_target)))
        return self.ranking_model(c)
    
    def compute_loss(self, features: Dict[Text, tf.Tensor], training=False) -> tf.Tensor:
        labels = features.pop(selected_target)
        rank_predictions = self(features)
        return self.task(labels=labels, predictions=rank_predictions)

并运行模型

model = AgentRankingModel()
model.compile(optimizer=tf.keras.optimizers.Adagrad(learning_rate=0.01))
model.fit(tf_ds, epochs=5)

当我运行模型时,我收到警告和错误。警告说明顺序模式下的层应该只有一个输入张量,但有“生成器”类。考虑使用功能 API 重写此模型。
错误如下:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-24-148d79872ae7> in <module>
----> 1 model.fit(tf_ds, epochs=5)

/usr/local/lib/python3.8/site-packages/keras/utils/traceback_utils.py in error_handler(*args, **kwargs)
     65     except Exception as e:  # pylint: disable=broad-except
     66       filtered_tb = _process_traceback_frames(e.__traceback__)
---> 67       raise e.with_traceback(filtered_tb) from None
     68     finally:
     69       del filtered_tb

/usr/local/lib/python3.8/site-packages/tensorflow/python/framework/func_graph.py in autograph_handler(*args, **kwargs)
   1127           except Exception as e:  # pylint:disable=broad-except
   1128             if hasattr(e, "ag_error_metadata"):
-> 1129               raise e.ag_error_metadata.to_exception(e)
   1130             else:
   1131               raise

TypeError: in user code:

    File "/usr/local/lib/python3.8/site-packages/keras/engine/training.py", line 878, in train_function  *
        return step_function(self, iterator)
    File "/usr/local/lib/python3.8/site-packages/keras/engine/training.py", line 867, in step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "/usr/local/lib/python3.8/site-packages/keras/engine/training.py", line 860, in run_step  **
        outputs = model.train_step(data)
    File "/usr/local/lib/python3.8/site-packages/tensorflow_recommenders/models/base.py", line 68, in train_step
        loss = self.compute_loss(inputs, training=True)
    File "<ipython-input-21-4bfb14fa7fcd>", line 20, in compute_loss
        rank_predictions = self(features)
    File "/usr/local/lib/python3.8/site-packages/keras/utils/traceback_utils.py", line 67, in error_handler
        raise e.with_traceback(filtered_tb) from None

    TypeError: Exception encountered when calling layer "test_ranking_model" (type TestRankingModel).
    
    in user code:
    
        File "<ipython-input-21-4bfb14fa7fcd>", line 14, in call  *
            return self.ranking_model(c)
        File "/usr/local/lib/python3.8/site-packages/keras/utils/traceback_utils.py", line 67, in error_handler  **
            raise e.with_traceback(filtered_tb) from None
    
        TypeError: Exception encountered when calling layer "ranking_model_1" (type RankingModel).
        
        in user code:
        
            File "<ipython-input-15-c8a9586f4818>", line 12, in call  *
                return self.rank(inputs)
            File "/usr/local/lib/python3.8/site-packages/keras/utils/traceback_utils.py", line 67, in error_handler  **
                raise e.with_traceback(filtered_tb) from None
            File "/usr/local/lib/python3.8/site-packages/keras/engine/input_spec.py", line 196, in assert_input_compatibility
                raise TypeError(f'Inputs to a layer should be tensors. Got: {x}')
        
            TypeError: Exception encountered when calling layer "sequential_1" (type Sequential).
            
            Inputs to a layer should be tensors. Got: <generator object outer_factory.<locals>.inner_factory.<locals>.tf__call.<locals>.<genexpr> at 0x7f111c266c80>
            
            Call arguments received:
              • inputs=<generator object outer_factory.<locals>.inner_factory.<locals>.tf__call.<locals>.<genexpr> at 0x7f111c266c80>
              • training=None
              • mask=None
        
        
        Call arguments received:
          • inputs=<generator object outer_factory.<locals>.inner_factory.<locals>.tf__call.<locals>.<genexpr> at 0x7f111c266c80>
    
    
    Call arguments received:
      • features={'feature1': 'tf.Tensor(shape=(), dtype=float32)', 'feature2': 'tf.Tensor(shape=(), dtype=float32)'}

Python v3.8 TF2.7.0

【问题讨论】:

标签: python tensorflow keras ranking


【解决方案1】:

您现在可以忽略该警告。我认为这里的关键错误消息是:Inputs to a layer should be tensors。

我注意到您没有在类/函数中定义 selected_features...不知道这是否有帮助。

【讨论】:

    猜你喜欢
    • 2019-01-19
    • 1970-01-01
    • 1970-01-01
    • 2021-02-06
    • 2021-04-23
    • 1970-01-01
    • 2013-07-01
    • 1970-01-01
    • 2017-04-02
    相关资源
    最近更新 更多