text
stringlengths 0
4.99k
|
---|
A Keras model instance (uncompiled).
|
Raises
|
TypeError: if config is not a dictionary.
|
to_json method
|
Model.to_json(**kwargs)
|
Returns a JSON string containing the network configuration.
|
To load a network from a JSON save file, use keras.models.model_from_json(json_string, custom_objects={}).
|
Arguments
|
**kwargs: Additional keyword arguments to be passed to json.dumps().
|
Returns
|
A JSON string.
|
model_from_json function
|
tf.keras.models.model_from_json(json_string, custom_objects=None)
|
Parses a JSON model configuration string and returns a model instance.
|
Usage:
|
>>> model = tf.keras.Sequential([
|
... tf.keras.layers.Dense(5, input_shape=(3,)),
|
... tf.keras.layers.Softmax()])
|
>>> config = model.to_json()
|
>>> loaded_model = tf.keras.models.model_from_json(config)
|
Arguments
|
json_string: JSON string encoding a model configuration.
|
custom_objects: Optional dictionary mapping names (strings) to custom classes or functions to be considered during deserialization.
|
Returns
|
A Keras model instance (uncompiled).
|
clone_model function
|
tf.keras.models.clone_model(model, input_tensors=None, clone_function=None)
|
Clone a Functional or Sequential Model instance.
|
Model cloning is similar to calling a model on new inputs, except that it creates new layers (and thus new weights) instead of sharing the weights of the existing layers.
|
Note that clone_model will not preserve the uniqueness of shared objects within the model (e.g. a single variable attached to two distinct layers will be restored as two separate variables).
|
Arguments
|
model: Instance of Model (could be a Functional model or a Sequential model).
|
input_tensors: optional list of input tensors or InputLayer objects to build the model upon. If not provided, new Input objects will be created.
|
clone_function: Callable to be used to clone each layer in the target model (except InputLayer instances). It takes as argument the layer instance to be cloned, and returns the corresponding layer instance to be used in the model copy. If unspecified, this callable defaults to the following serialization/deserialization function: lambda layer: layer.__class__.from_config(layer.get_config()). By passing a custom callable, you can customize your copy of the model, e.g. by wrapping certain layers of interest (you might want to replace all LSTM instances with equivalent Bidirectional(LSTM(...)) instances, for example).
|
Returns
|
An instance of Model reproducing the behavior of the original model, on top of new inputs tensors, using newly instantiated weights. The cloned model may behave differently from the original model if a custom clone_function modifies the layer.
|
Example
|
# Create a test Sequential model.
|
model = keras.Sequential([
|
keras.Input(shape=(728,)),
|
keras.layers.Dense(32, activation='relu'),
|
keras.layers.Dense(1, activation='sigmoid'),
|
])
|
# Create a copy of the test model (with freshly initialized weights).
|
new_model = clone_model(model)
|
Note that subclassed models cannot be cloned, since their internal layer structure is not known. To achieve equivalent functionality as clone_model in the case of a subclassed model, simply make sure that the model class implements get_config() (and optionally from_config()), and call:
|
new_model = model.__class__.from_config(model.get_config())The Model class
|
Model class
|
tf.keras.Model()
|
Model groups layers into an object with training and inference features.
|
Arguments
|
inputs: The input(s) of the model: a keras.Input object or list of keras.Input objects.
|
outputs: The output(s) of the model. See Functional API example below.
|
name: String, the name of the model.
|
There are two ways to instantiate a Model:
|
1 - With the "Functional API", where you start from Input, you chain layer calls to specify the model's forward pass, and finally you create your model from inputs and outputs:
|
import tensorflow as tf
|
inputs = tf.keras.Input(shape=(3,))
|
x = tf.keras.layers.Dense(4, activation=tf.nn.relu)(inputs)
|
outputs = tf.keras.layers.Dense(5, activation=tf.nn.softmax)(x)
|
model = tf.keras.Model(inputs=inputs, outputs=outputs)
|
2 - By subclassing the Model class: in that case, you should define your layers in __init__ and you should implement the model's forward pass in call.
|
import tensorflow as tf
|
class MyModel(tf.keras.Model):
|
def __init__(self):
|
super(MyModel, self).__init__()
|
self.dense1 = tf.keras.layers.Dense(4, activation=tf.nn.relu)
|
self.dense2 = tf.keras.layers.Dense(5, activation=tf.nn.softmax)
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.