![]() |
![]() |
Linear Model for regression and classification problems.
Inherits From: Model
, Layer
, Module
tf.keras.experimental.LinearModel(
units=1, activation=None, use_bias=True, kernel_initializer='zeros',
bias_initializer='zeros', kernel_regularizer=None,
bias_regularizer=None, **kwargs
)
This model approximates the following function:
where
is the bias and
is the weight for each feature.
Example:
model = LinearModel()
model.compile(optimizer='sgd', loss='mse')
model.fit(x, y, epochs=epochs)
This model accepts sparse float inputs as well:
Example:
model = LinearModel()
opt = tf.keras.optimizers.Adam()
loss_fn = tf.keras.losses.MeanSquaredError()
with tf.GradientTape() as tape:
output = model(sparse_input)
loss = tf.reduce_mean(loss_fn(target, output))
grads = tape.gradient(loss, model.weights)
opt.apply_gradients(zip(grads, model.weights))
Args | |
---|---|
units
|
Positive integer, output dimension without the batch size. |
activation
|
Activation function to use. If you don't specify anything, no activation is applied. |
use_bias
|
whether to calculate the bias/intercept for this model. If set to False, no bias/intercept will be used in calculations, e.g., the data is already centered. |
kernel_initializer
|
Initializer for the kernel weights matrices.
|
bias_initializer
|
Initializer for the bias vector. |
kernel_regularizer
|
regularizer for kernel vectors. |
bias_regularizer
|
regularizer for bias vector. |
**kwargs
|
The keyword arguments that are passed on to BaseLayer.init. |
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. |
Methods
compile
compile(
optimizer='rmsprop', loss=None, metrics=None, loss_weights=None,
weighted_metrics=None, run_eagerly=None, steps_per_execution=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.
|
weighted_metrics
|
List of metrics to be evaluated and weighted by sample_weight or class_weight during training and testing. |
run_eagerly
|
Bool. Defaults to False . If True , this Model 's
logic will not be wrapped in a tf.function . Recommended to leave
this as None unless your Model cannot be run inside a
tf.function .
|
steps_per_execution
|
Int. Defaults to 1. The number of batches to
run during each tf.function call. Running multiple batches
inside a single tf.function call can greatly improve performance
on TPUs or small models with a large Python overhead.
At most, one full epoch will be run each
execution. If a number larger than the size of the epoch is passed,
the execution will be truncated to the size of the epoch.
Note that if steps_per_execution is set to N ,
Callback.on_batch_begin and Callback.on_batch_end methods
will only be called every N batches
(i.e. before/after each tf.function execution).
|
**kwargs
|
Arguments supported for backwards compatibility only. |
Raises | |
---|---|
ValueError
|
In case of invalid arguments for
optimizer , loss or metrics .
|
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 (see the batch_size
arg.)
Arguments | |
---|---|
x
|
Input data. It could be:
|
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 batch of
computation. 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. 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 | |
---|---|
RuntimeError
|
If model.evaluate is wrapped in tf.function .
|
ValueError
|
in case of invalid arguments. |
evaluate_generator
evaluate_generator(
generator, steps=None, callbacks=None, max_queue_size=10, workers=1,
use_multiprocessing=False, verbose=0
)
Evaluates the model on a data generator.
DEPRECATED:
Model.evaluate
now supports generators, so there is no longer any need
to use this endpoint.
fit
fit(
x=None, y=None, batch_size=None, epochs=1, verbose=1, callbacks=None,
validation_split=0.0, validation_data=None, shuffle=True, class_weight=None,
sample_weight=None, initial_epoch=0, steps_per_epoch=None,
validation_steps=None, validation_batch_size=None, validation_freq=1,
max_queue_size=10, workers=1, use_multiprocessing=False
)
Trains the model for a fixed number of epochs (iterations on a dataset).
Arguments | |
---|---|
x
|
Input data. It could be: |