Module phiml.backend.tensorflow.nets
TensorFlow implementation of the unified machine learning API. Equivalent functions also exist for the other frameworks.
For API documentation, see phiml.nn
.
Functions
def adagrad(net: keras.src.engine.training.Model, learning_rate: float = 0.001, lr_decay=0.0, weight_decay=0.0, initial_accumulator_value=0.0, eps=1e-10)
def adam(net: keras.src.engine.training.Model, learning_rate: float = 0.001, betas=(0.9, 0.999), epsilon=1e-07)
def conv_classifier(in_features: int, in_spatial: Union[tuple, list], num_classes: int, blocks=(64, 128, 256, 256, 512, 512), block_sizes=(2, 2, 3, 3, 3), dense_layers=(4096, 4096, 100), batch_norm=True, activation='ReLU', softmax=True, periodic=False)
def conv_net(in_channels: int, out_channels: int, layers: Sequence[int], batch_norm: bool = False, activation: Union[str, Callable] = 'ReLU', periodic=False, in_spatial: Union[int, tuple] = 2) ‑> keras.src.engine.training.Model
def double_conv(x, d: int, out_channels: int, mid_channels: int, batch_norm: bool, activation: Callable, periodic: bool, kernel_size=3)
def get_mask(inputs, reverse_mask, data_format='NHWC')
-
Compute mask for slicing input feature map for Invertible Nets
def get_parameters(model: keras.src.engine.training.Model, wrap=True) ‑> dict
def invertible_net(num_blocks: int, construct_net: Union[str, Callable], **construct_kwargs)
def load_state(obj: Union[keras.src.engine.training.Model, keras.src.optimizers.optimizer.Optimizer], path: str)
def mlp(in_channels: int, out_channels: int, layers: Sequence[int], batch_norm=False, activation='ReLU', softmax=False) ‑> keras.src.engine.training.Model
def pad_periodic(x: tensorflow.python.framework.ops.Tensor)
def res_net(in_channels: int, out_channels: int, layers: Sequence[int], batch_norm: bool = False, activation: Union[str, Callable] = 'ReLU', periodic=False, in_spatial: Union[int, tuple] = 2)
def resnet_block(in_channels: int, out_channels: int, periodic: bool, batch_norm: bool = False, activation: Union[str, Callable] = 'ReLU', in_spatial: Union[int, tuple] = 2, kernel_size=3)
def rmsprop(net: keras.src.engine.training.Model, learning_rate: float = 0.001, alpha=0.99, eps=1e-08, weight_decay=0.0, momentum=0.0, centered=False)
def save_state(obj: Union[keras.src.engine.training.Model, keras.src.optimizers.optimizer.Optimizer], path: str)
def sgd(net: keras.src.engine.training.Model, learning_rate: float = 0.001, momentum=0.0, dampening=0.0, weight_decay=0.0, nesterov=False)
def u_net(in_channels: int, out_channels: int, levels: int = 4, filters: Union[int, Sequence[+T_co]] = 16, batch_norm: bool = True, activation: Union[str, Callable] = 'ReLU', in_spatial: Union[int, tuple] = 2, periodic=False, use_res_blocks: bool = False, down_kernel_size=3, up_kernel_size=3) ‑> keras.src.engine.training.Model
def update_weights(net: keras.src.engine.training.Model, optimizer: keras.src.optimizers.optimizer.Optimizer, loss_function: Callable, *loss_args, **loss_kwargs)
Classes
class CouplingLayer (construct_net: Callable, construction_kwargs: dict, reverse_mask)
-
A model grouping layers into an object with training/inference features.
Args
inputs
- The input(s) of the model: a
keras.Input
object or a combination ofkeras.Input
objects in a dict, list or tuple. outputs
- The output(s) of the model: a tensor that originated from
keras.Input
objects or a combination of such tensors in a dict, list or tuple. 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)
Note: Only dicts, lists, and tuples of input tensors are supported. Nested inputs are not supported (e.g. lists of list or dicts of dict).
A new Functional API model can also be created by using the intermediate tensors. This enables you to quickly extract sub-components of the model.
Example:
inputs = keras.Input(shape=(None, None, 3)) processed = keras.layers.RandomCrop(width=32, height=32)(inputs) conv = keras.layers.Conv2D(filters=2, kernel_size=3)(processed) pooling = keras.layers.GlobalAveragePooling2D()(conv) feature = keras.layers.Dense(10)(pooling) full_model = keras.Model(inputs, feature) backbone = keras.Model(processed, conv) activations = keras.Model(conv, feature)
Note that the
backbone
andactivations
models are not created withkeras.Input
objects, but with the tensors that are originated fromkeras.Input
objects. Under the hood, the layers and weights will be shared across these models, so that user can train thefull_model
, and usebackbone
oractivations
to do feature extraction. The inputs and outputs of the model can be nested structures of tensors as well, and the created models are standard Functional API models that support all the existing APIs.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 incall()
.import tensorflow as tf class MyModel(tf.keras.Model): def __init__(self): super().__init__() self.dense1 = tf.keras.layers.Dense(4, activation=tf.nn.relu) self.dense2 = tf.keras.layers.Dense(5, activation=tf.nn.softmax) def call(self, inputs): x = self.dense1(inputs) return self.dense2(x) model = MyModel()
If you subclass
Model
, you can optionally have atraining
argument (boolean) incall()
, which you can use to specify a different behavior in training and inference:import tensorflow as tf class MyModel(tf.keras.Model): def __init__(self): super().__init__() self.dense1 = tf.keras.layers.Dense(4, activation=tf.nn.relu) self.dense2 = tf.keras.layers.Dense(5, activation=tf.nn.softmax) self.dropout = tf.keras.layers.Dropout(0.5) def call(self, inputs, training=False): x = self.dense1(inputs) if training: x = self.dropout(x, training=training) return self.dense2(x) model = MyModel()
Once the model is created, you can config the model with losses and metrics with
model.compile()
, train the model withmodel.fit()
, or use the model to do prediction withmodel.predict()
.Expand source code
class CouplingLayer(keras.Model): def __init__(self, construct_net: Callable, construction_kwargs: dict, reverse_mask): super().__init__() self.reverse_mask = reverse_mask self.s1 = construct_net(**construction_kwargs) self.t1 = construct_net(**construction_kwargs) self.s2 = construct_net(**construction_kwargs) self.t2 = construct_net(**construction_kwargs) def call(self, x, invert=False): mask = tf.cast(get_mask(x, self.reverse_mask, 'NCHW'), x.dtype) if invert: v1 = x * mask v2 = x * (1 - mask) u2 = (1 - mask) * (v2 - self.t1(v1)) * tf.math.exp(tf.tanh(-self.s1(v1))) u1 = mask * (v1 - self.t2(u2)) * tf.math.exp(tf.tanh(-self.s2(u2))) return u1 + u2 else: u1 = x * mask u2 = x * (1 - mask) v1 = mask * (u1 * tf.math.exp(tf.tanh(self.s2(u2))) + self.t2(u2)) v2 = (1 - mask) * (u2 * tf.math.exp(tf.tanh(self.s1(v1))) + self.t1(v1)) return v1 + v2
Ancestors
- keras.src.engine.training.Model
- keras.src.engine.base_layer.Layer
- tensorflow.python.module.module.Module
- tensorflow.python.trackable.autotrackable.AutoTrackable
- tensorflow.python.trackable.base.Trackable
- keras.src.utils.version_utils.LayerVersionSelector
- keras.src.utils.version_utils.ModelVersionSelector
Methods
def call(self, x, invert=False)
-
Calls the model on new inputs and returns the outputs as tensors.
In this case
call()
just reapplies all ops in the graph to the new inputs (e.g. build a new computational graph from the provided inputs).Note: This method should not be called directly. It is only meant to be overridden when subclassing
tf.keras.Model
. To call a model on an input, always use the__call__()
method, i.e.model(inputs)
, which relies on the underlyingcall()
method.Args
inputs
- Input tensor, or dict/list/tuple of input tensors.
training
- Boolean or boolean scalar tensor, indicating whether to
run the
Network
in training mode or inference mode. mask
- A mask or list of masks. A mask can be either a boolean tensor or None (no mask). For more details, check the guide here.
Returns
A tensor if there is a single output, or a list of tensors if there are more than one outputs.
class InvertibleNet (num_blocks: int, construct_net, construction_kwargs: dict)
-
A model grouping layers into an object with training/inference features.
Args
inputs
- The input(s) of the model: a
keras.Input
object or a combination ofkeras.Input
objects in a dict, list or tuple. outputs
- The output(s) of the model: a tensor that originated from
keras.Input
objects or a combination of such tensors in a dict, list or tuple. 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)
Note: Only dicts, lists, and tuples of input tensors are supported. Nested inputs are not supported (e.g. lists of list or dicts of dict).
A new Functional API model can also be created by using the intermediate tensors. This enables you to quickly extract sub-components of the model.
Example:
inputs = keras.Input(shape=(None, None, 3)) processed = keras.layers.RandomCrop(width=32, height=32)(inputs) conv = keras.layers.Conv2D(filters=2, kernel_size=3)(processed) pooling = keras.layers.GlobalAveragePooling2D()(conv) feature = keras.layers.Dense(10)(pooling) full_model = keras.Model(inputs, feature) backbone = keras.Model(processed, conv) activations = keras.Model(conv, feature)
Note that the
backbone
andactivations
models are not created withkeras.Input
objects, but with the tensors that are originated fromkeras.Input
objects. Under the hood, the layers and weights will be shared across these models, so that user can train thefull_model
, and usebackbone
oractivations
to do feature extraction. The inputs and outputs of the model can be nested structures of tensors as well, and the created models are standard Functional API models that support all the existing APIs.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 incall()
.import tensorflow as tf class MyModel(tf.keras.Model): def __init__(self): super().__init__() self.dense1 = tf.keras.layers.Dense(4, activation=tf.nn.relu) self.dense2 = tf.keras.layers.Dense(5, activation=tf.nn.softmax) def call(self, inputs): x = self.dense1(inputs) return self.dense2(x) model = MyModel()
If you subclass
Model
, you can optionally have atraining
argument (boolean) incall()
, which you can use to specify a different behavior in training and inference:import tensorflow as tf class MyModel(tf.keras.Model): def __init__(self): super().__init__() self.dense1 = tf.keras.layers.Dense(4, activation=tf.nn.relu) self.dense2 = tf.keras.layers.Dense(5, activation=tf.nn.softmax) self.dropout = tf.keras.layers.Dropout(0.5) def call(self, inputs, training=False): x = self.dense1(inputs) if training: x = self.dropout(x, training=training) return self.dense2(x) model = MyModel()
Once the model is created, you can config the model with losses and metrics with
model.compile()
, train the model withmodel.fit()
, or use the model to do prediction withmodel.predict()
.Expand source code
class InvertibleNet(keras.Model): def __init__(self, num_blocks: int, construct_net, construction_kwargs: dict): super(InvertibleNet, self).__init__() self.num_blocks = num_blocks self.layer_dict = {} for i in range(num_blocks): self.layer_dict[f'coupling_block{i + 1}'] = CouplingLayer(construct_net, construction_kwargs, (i % 2 == 0)) def call(self, x, backward=False): if backward: for i in range(self.num_blocks, 0, -1): x = self.layer_dict[f'coupling_block{i}'](x, backward) else: for i in range(1, self.num_blocks + 1): x = self.layer_dict[f'coupling_block{i}'](x) return x
Ancestors
- keras.src.engine.training.Model
- keras.src.engine.base_layer.Layer
- tensorflow.python.module.module.Module
- tensorflow.python.trackable.autotrackable.AutoTrackable
- tensorflow.python.trackable.base.Trackable
- keras.src.utils.version_utils.LayerVersionSelector
- keras.src.utils.version_utils.ModelVersionSelector
Methods
def call(self, x, backward=False)
-
Calls the model on new inputs and returns the outputs as tensors.
In this case
call()
just reapplies all ops in the graph to the new inputs (e.g. build a new computational graph from the provided inputs).Note: This method should not be called directly. It is only meant to be overridden when subclassing
tf.keras.Model
. To call a model on an input, always use the__call__()
method, i.e.model(inputs)
, which relies on the underlyingcall()
method.Args
inputs
- Input tensor, or dict/list/tuple of input tensors.
training
- Boolean or boolean scalar tensor, indicating whether to
run the
Network
in training mode or inference mode. mask
- A mask or list of masks. A mask can be either a boolean tensor or None (no mask). For more details, check the guide here.
Returns
A tensor if there is a single output, or a list of tensors if there are more than one outputs.