![]() |
![]() |
Wide & Deep Model for regression and classification problems.
Inherits From: Model
tf.keras.experimental.WideDeepModel(
linear_model, dnn_model, activation=None, **kwargs
)
This model jointly train a linear and a dnn model.
Example:
linear_model = LinearModel()
dnn_model = keras.Sequential([keras.layers.Dense(units=64),
keras.layers.Dense(units=1)])
combined_model = WideDeepModel(dnn_model, linear_model)
combined_model.compile(optimizer=['sgd', 'adam'], 'mse', ['mse'])
# define dnn_inputs and linear_inputs as separate numpy arrays or
# a single numpy array if dnn_inputs is same as linear_inputs.
combined_model.fit([dnn_inputs, linear_inputs], y, epochs)
# or define a single `tf.data.Dataset` that contains a single tensor or
# separate tensors for dnn_inputs and linear_inputs.
dataset = tf.data.Dataset.from_tensors(([dnn_inputs, linear_inputs], y))
combined_model.fit(dataset, epochs)
Both linear and dnn model can be pre-compiled and trained separately before jointly training:
Example:
linear_model = LinearModel()
linear_model.compile('adagrad', 'mse')
linear_model.fit(linear_inputs, y, epochs)
dnn_model = keras.Sequential([keras.layers.Dense(units=1)])
dnn_model.compile('rmsprop', 'mse')
dnn_model.fit(dnn_inputs, y, epochs)
combined_model = WideDeepModel(dnn_model, linear_model)
combined_model.compile(optimizer=['sgd', 'adam'], 'mse', ['mse'])
combined_model.fit([dnn_inputs, linear_inputs], y, epochs)
Args | |
---|---|
linear_model
|
a premade LinearModel, its output must match the output of the dnn model. |
dnn_model
|
a tf.keras.Model , its output must match the output of the
linear model.
|
activation
|
Activation function. Set it to None to maintain a linear activation. |
**kwargs
|
The keyword arguments that are passed on to BaseLayer.init.
Allowed keyword arguments include name .
|
Attributes | |
---|---|
distribute_strategy
|
The tf.distribute.Strategy this model was created under.
|
layers
|
|
metrics_names
|
Returns the model's display labels for all outputs.
|
run_eagerly
|
Settable attribute indicating whether the model should run eagerly.
Running eagerly means that your model will be run step by step, like Python code. Your model might run slower, but it should become easier for you to debug it by stepping into individual layer calls. By default, we will attempt to compile your model to a static graph to deliver the best execution performance. |
state_updates
|
Returns the updates from all layers that are stateful.
This is useful for separating training updates and state updates, e.g. when we need to update a layer's internal state during prediction. |
Methods
compile
compile(
optimizer='rmsprop', loss=None, metrics=None, loss_weights=None,
sample_weight_mode=None, weighted_metrics=None, **kwargs
)
Configures the model for training.
Arguments | |
---|---|
optimizer
|
String (name of optimizer) or optimizer instance.
See tf.keras.optimizers .
|
loss
|
String (name of objective function), objective function or
tf.keras.losses.Loss instance. See tf.keras.losses .
An objective function is any callable with the signature
loss = fn(y_true, y_pred) , where
y_true = ground truth values with shape = [batch_size, d0, .. dN] ,
except sparse loss functions such as sparse categorical crossentropy
where shape = [batch_size, d0, .. dN-1] .
y_pred = predicted values with shape = [batch_size, d0, .. dN] .
It returns a weighted loss float tensor.
If a custom Loss instance is used and reduction is set to NONE,
return value has the shape [batch_size, d0, .. dN-1] ie. per-sample
or per-timestep loss values; otherwise, it is a scalar.
If the model has multiple outputs, you can use a different loss on
each output by passing a dictionary or a list of losses. The loss
value that will be minimized by the model will then be the sum of
all individual losses.
|
metrics
|
List of metrics to be evaluated by the model during training
and testing.
Each of this can be a string (name of a built-in function), function
or a tf.keras.metrics.Metric instance. See tf.keras.metrics .
Typically you will use metrics=['accuracy'] . A function is any
callable with the signature result = fn(y_true, y_pred) .
To specify different metrics for different outputs of a
multi-output model, you could also pass a dictionary, such as
metrics={'output_a': 'accuracy', 'output_b': ['accuracy', 'mse']} .
You can also pass a list (len = len(outputs)) of lists of metrics
such as metrics=[['accuracy'], ['accuracy', 'mse']] or
metrics=['accuracy', ['accuracy', 'mse']] .
When you pass the strings 'accuracy' or 'acc', we convert this to
one of tf.keras.metrics.BinaryAccuracy ,
tf.keras.metrics.CategoricalAccuracy ,
tf.keras.metrics.SparseCategoricalAccuracy based on the loss
function used and the model output shape. We do a similar conversion
for the strings 'crossentropy' and 'ce' as well.
|
loss_weights
|
Optional list or dictionary specifying scalar
coefficients (Python floats) to weight the loss contributions
of different model outputs.
The loss value that will be minimized by the model
will then be the weighted sum of all individual losses,
weighted by the loss_weights coefficients.
If a list, it is expected to have a 1:1 mapping
to the model's outputs. If a dict, it is expected to map
output names (strings) to scalar coefficients.
|
sample_weight_mode
|
If you need to do timestep-wise
sample weighting (2D weights), set this to "temporal" .
None defaults to sample-wise weights (1D).
If the model has multiple outputs, you can use a different
sample_weight_mode on each output by passing a
dictionary or a list of modes.
|
weighted_metrics
|
List of metrics to be evaluated and weighted by sample_weight or class_weight during training and testing. |
**kwargs
|
Any additional arguments. For eager execution, pass
run_eagerly=True .
|
Raises | |
---|---|
ValueError
|
In case of invalid arguments for
optimizer , loss , metrics or sample_weight_mode .
|
evaluate
evaluate(
x=None, y=None, batch_size=None, verbose=1, sample_weight=None, steps=None,
callbacks=None, max_queue_size=10, workers=1, use_multiprocessing=False,
return_dict=False
)
Returns the loss value & metrics values for the model in test mode.
Computation is done in batches.
Arguments | |
---|---|
x
|
Input data. It could be: - A Numpy array (or array-like), or a list
of arrays (in case the model has multiple inputs). - A TensorFlow
tensor, or a list of tensors (in case the model has multiple inputs).
|
y
|
Target data. Like the input data x , it could be either Numpy
array(s) or TensorFlow tensor(s). It should be consistent with x
(you cannot have Numpy inputs and tensor targets, or inversely). If
x is a dataset, generator or keras.utils.Sequence instance, y
should not be specified (since targets will be obtained from the
iterator/dataset).
|
batch_size
|
Integer or None . Number of samples per gradient update. If
unspecified, batch_size will default to 32. Do not specify the
batch_size if your data is in the form of a dataset, generators,
or keras.utils.Sequence instances (since they generate batches).
|
verbose
|
0 or 1. Verbosity mode. 0 = silent, 1 = progress bar. |
sample_weight
|
Optional Numpy array of weights for the test samples,
used for weighting the loss function. You can either pass a flat (1D)
Numpy array with the same length as the input samples
(1:1 mapping between weights and samples), or in the case of
temporal data, you can pass a 2D array with shape (samples,
sequence_length) , to apply a different weight to every timestep
of every sample. In this case you should make sure to specify
sample_weight_mode="temporal" in compile() . This argument is
not supported when x is a dataset, instead pass sample weights
as the third element of x .
|
steps
|
Integer or None . Total number of steps (batches of samples)
before declaring the evaluation round finished. Ignored with the
default value of None . If x is a tf.data dataset and steps is
None, 'evaluate' will run until the dataset is exhausted. This
argument is not supported with array inputs.
|
callbacks
|
List of keras.callbacks.Callback instances. List of
callbacks to apply during evaluation. See
callbacks.
|
max_queue_size
|
Integer. Used for generator or keras.utils.Sequence
input only. Maximum size for the generator queue. If unspecified,
max_queue_size will default to 10.
|
workers
|
Integer. Used for generator or keras.utils.Sequence input
only. Maximum number of processes to spin up when using process-based
threading. If unspecified, workers will default to 1. If 0, will
execute the generator on the main thread.
|
use_multiprocessing
|
Boolean. Used for generator or
keras.utils.Sequence input only. If True , use process-based
threading. If unspecified, use_multiprocessing will default to
False . Note that because this implementation relies on
multiprocessing, you should not pass non-picklable arguments to the
generator as they can't be passed easily to children processes.
|
return_dict
|
If True , loss and metric results are returned as a dict,
with each key being the name of the metric. If False , they are
returned as a list.
|
See the discussion of Unpacking behavior for iterator-like inputs
for
Model.fit
.
Returns | |
---|---|
Scalar test loss (if the model has a single output and no metrics)
or list of scalars (if the model has multiple outputs
and/or metrics). The attribute model.metrics_names will give you
the display labels for the scalar outputs.
|
Raises | |
---|---|
ValueError
|
in case of invalid arguments. |
evaluate_generator
evaluate_generator(
generator, steps=None, callbacks=None, max_queue_size=10, workers=1,
u