Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/keras/src/premade_models/wide_deep.py: 22%
95 statements
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-03 07:57 +0000
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-03 07:57 +0000
1# Copyright 2019 The TensorFlow Authors. All Rights Reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ==============================================================================
15"""Built-in WideNDeep model classes."""
17import tensorflow.compat.v2 as tf
19from keras.src import activations
20from keras.src import backend
21from keras.src import layers as layer_module
22from keras.src.engine import base_layer
23from keras.src.engine import data_adapter
24from keras.src.engine import training as keras_training
25from keras.src.saving import serialization_lib
27# isort: off
28from tensorflow.python.util import deprecation
29from tensorflow.python.util.tf_export import keras_export
32@keras_export(
33 "keras.experimental.WideDeepModel",
34 v1=["keras.experimental.WideDeepModel", "keras.models.WideDeepModel"],
35)
36@deprecation.deprecated_endpoints("keras.experimental.WideDeepModel")
37class WideDeepModel(keras_training.Model):
38 r"""Wide & Deep Model for regression and classification problems.
40 This model jointly train a linear and a dnn model.
42 Example:
44 ```python
45 linear_model = LinearModel()
46 dnn_model = keras.Sequential([keras.layers.Dense(units=64),
47 keras.layers.Dense(units=1)])
48 combined_model = WideDeepModel(linear_model, dnn_model)
49 combined_model.compile(optimizer=['sgd', 'adam'],
50 loss='mse', metrics=['mse'])
51 # define dnn_inputs and linear_inputs as separate numpy arrays or
52 # a single numpy array if dnn_inputs is same as linear_inputs.
53 combined_model.fit([linear_inputs, dnn_inputs], y, epochs)
54 # or define a single `tf.data.Dataset` that contains a single tensor or
55 # separate tensors for dnn_inputs and linear_inputs.
56 dataset = tf.data.Dataset.from_tensors(([linear_inputs, dnn_inputs], y))
57 combined_model.fit(dataset, epochs)
58 ```
60 Both linear and dnn model can be pre-compiled and trained separately
61 before jointly training:
63 Example:
64 ```python
65 linear_model = LinearModel()
66 linear_model.compile('adagrad', 'mse')
67 linear_model.fit(linear_inputs, y, epochs)
68 dnn_model = keras.Sequential([keras.layers.Dense(units=1)])
69 dnn_model.compile('rmsprop', 'mse')
70 dnn_model.fit(dnn_inputs, y, epochs)
71 combined_model = WideDeepModel(linear_model, dnn_model)
72 combined_model.compile(optimizer=['sgd', 'adam'],
73 loss='mse', metrics=['mse'])
74 combined_model.fit([linear_inputs, dnn_inputs], y, epochs)
75 ```
77 """
79 def __init__(self, linear_model, dnn_model, activation=None, **kwargs):
80 """Create a Wide & Deep Model.
82 Args:
83 linear_model: a premade LinearModel, its output must match the output
84 of the dnn model.
85 dnn_model: a `tf.keras.Model`, its output must match the output of the
86 linear model.
87 activation: Activation function. Set it to None to maintain a linear
88 activation.
89 **kwargs: The keyword arguments that are passed on to
90 BaseLayer.__init__. Allowed keyword arguments include `name`.
91 """
92 super().__init__(**kwargs)
93 base_layer.keras_premade_model_gauge.get_cell("WideDeep").set(True)
94 self.linear_model = linear_model
95 self.dnn_model = dnn_model
96 self.activation = activations.get(activation)
98 def call(self, inputs, training=None):
99 if not isinstance(inputs, (tuple, list)) or len(inputs) != 2:
100 linear_inputs = dnn_inputs = inputs
101 else:
102 linear_inputs, dnn_inputs = inputs
103 linear_output = self.linear_model(linear_inputs)
105 if self.dnn_model._expects_training_arg:
106 if training is None:
107 training = backend.learning_phase()
108 dnn_output = self.dnn_model(dnn_inputs, training=training)
109 else:
110 dnn_output = self.dnn_model(dnn_inputs)
111 output = tf.nest.map_structure(
112 lambda x, y: (x + y), linear_output, dnn_output
113 )
114 if self.activation:
115 return tf.nest.map_structure(self.activation, output)
116 return output
118 # This does not support gradient scaling and LossScaleOptimizer.
119 def train_step(self, data):
120 x, y, sample_weight = data_adapter.unpack_x_y_sample_weight(data)
121 with tf.GradientTape() as tape:
122 y_pred = self(x, training=True)
123 loss = self.compiled_loss(
124 y, y_pred, sample_weight, regularization_losses=self.losses
125 )
126 self.compiled_metrics.update_state(y, y_pred, sample_weight)
128 if isinstance(self.optimizer, (list, tuple)):
129 linear_vars = self.linear_model.trainable_variables
130 dnn_vars = self.dnn_model.trainable_variables
131 linear_grads, dnn_grads = tape.gradient(
132 loss, (linear_vars, dnn_vars)
133 )
135 linear_optimizer = self.optimizer[0]
136 dnn_optimizer = self.optimizer[1]
137 linear_optimizer.apply_gradients(zip(linear_grads, linear_vars))
138 dnn_optimizer.apply_gradients(zip(dnn_grads, dnn_vars))
139 else:
140 trainable_variables = self.trainable_variables
141 grads = tape.gradient(loss, trainable_variables)
142 self.optimizer.apply_gradients(zip(grads, trainable_variables))
144 return {m.name: m.result() for m in self.metrics}
146 def _make_train_function(self):
147 # Only needed for graph mode and model_to_estimator.
148 has_recompiled = self._recompile_weights_loss_and_weighted_metrics()
149 self._check_trainable_weights_consistency()
150 # If we have re-compiled the loss/weighted metric sub-graphs then create
151 # train function even if one exists already. This is because
152 # `_feed_sample_weights` list has been updated on re-compile.
153 if getattr(self, "train_function", None) is None or has_recompiled:
154 # Restore the compiled trainable state.
155 current_trainable_state = self._get_trainable_state()
156 self._set_trainable_state(self._compiled_trainable_state)
158 inputs = (
159 self._feed_inputs
160 + self._feed_targets
161 + self._feed_sample_weights
162 )
163 if not isinstance(backend.symbolic_learning_phase(), int):
164 inputs += [backend.symbolic_learning_phase()]
166 if isinstance(self.optimizer, (list, tuple)):
167 linear_optimizer = self.optimizer[0]
168 dnn_optimizer = self.optimizer[1]
169 else:
170 linear_optimizer = self.optimizer
171 dnn_optimizer = self.optimizer
173 with backend.get_graph().as_default():
174 with backend.name_scope("training"):
175 # Training updates
176 updates = []
177 linear_updates = linear_optimizer.get_updates(
178 params=self.linear_model.trainable_weights,
179 loss=self.total_loss,
180 )
181 updates += linear_updates
182 dnn_updates = dnn_optimizer.get_updates(
183 params=self.dnn_model.trainable_weights,
184 loss=self.total_loss,
185 )
186 updates += dnn_updates
187 # Unconditional updates
188 updates += self.get_updates_for(None)
189 # Conditional updates relevant to this model
190 updates += self.get_updates_for(self.inputs)
192 metrics = self._get_training_eval_metrics()
193 metrics_tensors = [
194 m._call_result
195 for m in metrics
196 if hasattr(m, "_call_result")
197 ]
199 with backend.name_scope("training"):
200 # Gets loss and metrics. Updates weights at each call.
201 fn = backend.function(
202 inputs,
203 [self.total_loss] + metrics_tensors,
204 updates=updates,
205 name="train_function",
206 **self._function_kwargs
207 )
208 setattr(self, "train_function", fn)
210 # Restore the current trainable state
211 self._set_trainable_state(current_trainable_state)
213 def get_config(self):
214 linear_config = serialization_lib.serialize_keras_object(
215 self.linear_model
216 )
217 dnn_config = serialization_lib.serialize_keras_object(self.dnn_model)
218 config = {
219 "linear_model": linear_config,
220 "dnn_model": dnn_config,
221 "activation": activations.serialize(self.activation),
222 }
223 base_config = base_layer.Layer.get_config(self)
224 return dict(list(base_config.items()) + list(config.items()))
226 @classmethod
227 def from_config(cls, config, custom_objects=None):
228 linear_config = config.pop("linear_model")
229 linear_model = layer_module.deserialize(linear_config, custom_objects)
230 dnn_config = config.pop("dnn_model")
231 dnn_model = layer_module.deserialize(dnn_config, custom_objects)
232 activation = activations.deserialize(
233 config.pop("activation", None), custom_objects=custom_objects
234 )
235 return cls(
236 linear_model=linear_model,
237 dnn_model=dnn_model,
238 activation=activation,
239 **config
240 )