如何将自定义树添加到自定义的Keras层?
我正在尝试将以下教程汇总在一起:
- a href =“ https://www.tensorflow.org/decision_forests/tutorials/advanced_colab#creating_a_model_model_model_model_by_by_by_by_handhande
- < a href =“ https://www.tensorflow.org/guide/keras/custom_layers_and_models” rel =“ nofollow noreferrer”>通过subclassing
- 组成决策森林和神经网络模型
目标是1。创建一个自定义树,2。将其嵌入自定义层,并将其组合到3中。与其他层的模型。
问题在于,在步骤1中,通过使用RandomForestBuilder,该模型被序列化并进行了序列化,从而导致对象的keras.saving.saved.saved_model.load.coremodel.coremodel,
但是,该教程在步骤3中。通过tfdf.keras嵌入了树层。 理想情况下, RandomForestModel
理想情况下,自定义层将通过在其构造函数中调用Random ForestBuilder创建自定义树,但是,鉴于模型的导出和加载,这并不简单。
则会出现错误的错误
下面给出了输入层的结构的错误,如果省略了前者的错误,
builder = tfdf.builder.RandomForestBuilder(
path="/tmp/manual_model",
objective = tfdf.py_tree.objective.RegressionObjective(label='tree_result')
)
Tree = tfdf.py_tree.tree.Tree
SimpleColumnSpec = tfdf.py_tree.dataspec.SimpleColumnSpec
ColumnType = tfdf.py_tree.dataspec.ColumnType
RegressionValue = tfdf.py_tree.value.RegressionValue
NonLeafNode = tfdf.py_tree.node.NonLeafNode
LeafNode = tfdf.py_tree.node.LeafNode
NumericalHigherThanCondition = tfdf.py_tree.condition.NumericalHigherThanCondition
CategoricalIsInCondition = tfdf.py_tree.condition.CategoricalIsInCondition
tree = Tree(
NonLeafNode(
condition=CategoricalIsInCondition(
feature=SimpleColumnSpec(name='feature_name', type=ColumnType.CATEGORICAL),
mask=['class_1'],
missing_evaluation=False
),
pos_child = LeafNode(value=RegressionValue(value=0.5)),
neg_child = LeafNode(value=RegressionValue(value=0.6))
)
)
builder.add_tree(tree)
builder.close()
custom_tree = tf.keras.models.load_model("/tmp/manual_model")
。
class CustomTree(tf.keras.layers.Layer):
def __init__(self, custom_tree):
super(CustomTree, self).__init__()
self.custom_tree = custom_tree
def call(self, inputs):
return self.custom_tree(inputs)
input_layer = tf.keras.layers.Input(shape=(None,), name='feature_name', dtype=tf.string)
output_layer = CustomTree(custom_tree)(input_layer)
model = tf.keras.models.Model(input_layer, output_layer, name='SomeModel')
model.predict(tf.data.Dataset.from_tensor_slices(
{'feature_name': ['class_1','class_2']}
).batch(1))
I am trying to bring together the following tutorials:
- Creating decision tree by hand
- Custom layers via subclassing
- Composing Decision Forest and Neural Network models
The goal is to 1. Create a custom tree, 2. Embed it into a custom layer and 3. combine it in a model with other layers.
The problem is that in step 1. by using the RandomForestBuilder, the model is serialized and deserialized resulting in object of type keras.saving.saved_model.load.CoreModel
However, the tutorial in step 3. embeds the tree layer via tfdf.keras.RandomForestModel
Ideally, the custom layer would create the custom tree by calling RandomForestBuilder in its constructor, however, this is not straightforward given the exporting and loading of the model.
The below gives error for the structure of the input layer and if the former is omitted gives error for no matching concrete function to call loaded from the SavedModel:
Step 1:
builder = tfdf.builder.RandomForestBuilder(
path="/tmp/manual_model",
objective = tfdf.py_tree.objective.RegressionObjective(label='tree_result')
)
Tree = tfdf.py_tree.tree.Tree
SimpleColumnSpec = tfdf.py_tree.dataspec.SimpleColumnSpec
ColumnType = tfdf.py_tree.dataspec.ColumnType
RegressionValue = tfdf.py_tree.value.RegressionValue
NonLeafNode = tfdf.py_tree.node.NonLeafNode
LeafNode = tfdf.py_tree.node.LeafNode
NumericalHigherThanCondition = tfdf.py_tree.condition.NumericalHigherThanCondition
CategoricalIsInCondition = tfdf.py_tree.condition.CategoricalIsInCondition
tree = Tree(
NonLeafNode(
condition=CategoricalIsInCondition(
feature=SimpleColumnSpec(name='feature_name', type=ColumnType.CATEGORICAL),
mask=['class_1'],
missing_evaluation=False
),
pos_child = LeafNode(value=RegressionValue(value=0.5)),
neg_child = LeafNode(value=RegressionValue(value=0.6))
)
)
builder.add_tree(tree)
builder.close()
custom_tree = tf.keras.models.load_model("/tmp/manual_model")
Step 2:
class CustomTree(tf.keras.layers.Layer):
def __init__(self, custom_tree):
super(CustomTree, self).__init__()
self.custom_tree = custom_tree
def call(self, inputs):
return self.custom_tree(inputs)
input_layer = tf.keras.layers.Input(shape=(None,), name='feature_name', dtype=tf.string)
output_layer = CustomTree(custom_tree)(input_layer)
model = tf.keras.models.Model(input_layer, output_layer, name='SomeModel')
model.predict(tf.data.Dataset.from_tensor_slices(
{'feature_name': ['class_1','class_2']}
).batch(1))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
找到以下解决方案:
以yggdrasil格式导出自定义树:
通过关闭建筑商保存模型后,将树加载为Coremodel:
创建一个带有自定义拟合功能的tfdf.keras.cartmodel的模型子类,将自定义树结构分配给模型:
Found the following solution:
Export the custom tree in yggdrasil format:
After saving the model by closing the builder load the tree as CoreModel:
Create a model subclass of tfdf.keras.CartModel with custom fit function that assigns the custom tree structure to the model: