neulab/docprompting-conala huggingface dataset:
tf.keras.backend.clear_session     View source on GitHub    Resets all state generated by Keras.  View aliases  Compat aliases for migration 
See Migration guide for more details. tf.compat.v1.keras.backend.clear_session  
tf.keras.backend.clear_session()
 Keras manages a global state, which it uses to implement the Functional model-building API and to uniquify autogenerated layer names. If you are creating many models in a loop, this global state will consume an increasing amount of memory over time, and you may want to clear it. Calling clear_session() releases the global state: this helps avoid clutter from old models and layers, especially when memory is limited. Example 1: calling clear_session() when creating models in a loop for _ in range(100):
  # Without `clear_session()`, each iteration of this loop will
  # slightly increase the size of the global state managed by Keras
  model = tf.keras.Sequential([tf.keras.layers.Dense(10) for _ in range(10)])

for _ in range(100):
  # With `clear_session()` called at the beginning,
  # Keras starts with a blank state at each iteration
  # and memory consumption is constant over time.
  tf.keras.backend.clear_session()
  model = tf.keras.Sequential([tf.keras.layers.Dense(10) for _ in range(10)])
 Example 2: resetting the layer name generation counter 
import tensorflow as tf
layers = [tf.keras.layers.Dense(10) for _ in range(10)]
new_layer = tf.keras.layers.Dense(10)
print(new_layer.name)
dense_10
tf.keras.backend.set_learning_phase(1)
print(tf.keras.backend.learning_phase())
1
tf.keras.backend.clear_session()
new_layer = tf.keras.layers.Dense(10)
print(new_layer.name)
dense
tf.keras.backend.epsilon     View source on GitHub    Returns the value of the fuzz factor used in numeric expressions.  View aliases  Compat aliases for migration 
See Migration guide for more details. tf.compat.v1.keras.backend.epsilon  
tf.keras.backend.epsilon()

 


 Returns   A float.  
 Example: 
tf.keras.backend.epsilon()
1e-07
tf.keras.backend.floatx     View source on GitHub    Returns the default float type, as a string.  View aliases  Compat aliases for migration 
See Migration guide for more details. tf.compat.v1.keras.backend.floatx  
tf.keras.backend.floatx()
 E.g. 'float16', 'float32', 'float64'.
 


 Returns   String, the current default float type.  
 Example: 
tf.keras.backend.floatx()
'float32'
tf.keras.backend.get_uid     View source on GitHub    Associates a string prefix with an integer counter in a TensorFlow graph.  View aliases  Compat aliases for migration 
See Migration guide for more details. tf.compat.v1.keras.backend.get_uid  
tf.keras.backend.get_uid(
    prefix=''
)

 


 Arguments
  prefix   String prefix to index.   
 


 Returns   Unique integer ID.  
 Example: 
get_uid('dense')
1
get_uid('dense')
2
tf.keras.backend.image_data_format     View source on GitHub    Returns the default image data format convention.  View aliases  Compat aliases for migration 
See Migration guide for more details. tf.compat.v1.keras.backend.image_data_format  
tf.keras.backend.image_data_format()

 


 Returns   A string, either 'channels_first' or 'channels_last'  
 Example: 
tf.keras.backend.image_data_format()
'channels_last'
tf.keras.backend.is_keras_tensor Returns whether x is a Keras tensor.  View aliases  Compat aliases for migration 
See Migration guide for more details. tf.compat.v1.keras.backend.is_keras_tensor  
tf.keras.backend.is_keras_tensor(
    x
)
 A "Keras tensor" is a tensor that was returned by a Keras layer, (Layer class) or by Input.
 


 Arguments
  x   A candidate tensor.   
 


 Returns   A boolean: Whether the argument is a Keras tensor.  

 


 Raises
  ValueError   In case x is not a symbolic tensor.    Examples: 
np_var = np.array([1, 2])
# A numpy array is not a symbolic tensor.
tf.keras.backend.is_keras_tensor(np_var)
Traceback (most recent call last):

ValueError: Unexpectedly found an instance of type `<class 'numpy.ndarray'>`.
Expected a symbolic tensor instance.
keras_var = tf.keras.backend.variable(np_var)
# A variable created with the keras backend is not a Keras tensor.
tf.keras.backend.is_keras_tensor(keras_var)
False
keras_placeholder = tf.keras.backend.placeholder(shape=(2, 4, 5))
# A placeholder is a Keras tensor.
tf.keras.backend.is_keras_tensor(keras_placeholder)
True
keras_input = tf.keras.layers.Input([10])
# An Input is a Keras tensor.
tf.keras.backend.is_keras_tensor(keras_input)
True
keras_layer_output = tf.keras.layers.Dense(10)(keras_input)
# Any Keras layer output is a Keras tensor.
tf.keras.backend.is_keras_tensor(keras_layer_output)
True
tf.keras.backend.reset_uids     View source on GitHub    Resets graph identifiers.  View aliases  Compat aliases for migration 
See Migration guide for more details. tf.compat.v1.keras.backend.reset_uids  
tf.keras.backend.reset_uids()
tf.keras.backend.rnn     View source on GitHub    Iterates over the time dimension of a tensor.  View aliases  Compat aliases for migration 
See Migration guide for more details. tf.compat.v1.keras.backend.rnn  
tf.keras.backend.rnn(
    step_function, inputs, initial_states, go_backwards=False, mask=None,
    constants=None, unroll=False, input_length=None, time_major=False,
    zero_output_for_mask=False
)

 


 Arguments
  step_function   RNN step function. Args; input; Tensor with shape (samples, ...) (no time dimension), representing input for the batch of samples at a certain time step. states; List of tensors. Returns; output; Tensor with shape (samples, output_dim) (no time dimension). new_states; List of tensors, same length and shapes as 'states'. The first state in the list must be the output tensor at the previous timestep.  
  inputs   Tensor of temporal data of shape (samples, time, ...) (at least 3D), or nested tensors, and each of which has shape (samples, time, ...).  
  initial_states   Tensor with shape (samples, state_size) (no time dimension), containing the initial values for the states used in the step function. In the case that state_size is in a nested shape, the shape of initial_states will also follow the nested structure.  
  go_backwards   Boolean. If True, do the iteration over the time dimension in reverse order and return the reversed sequence.  
  mask   Binary tensor with shape (samples, time, 1), with a zero for every element that is masked.  
  constants   List of constant values passed at each step.  
  unroll   Whether to unroll the RNN or to use a symbolic while_loop.  
  input_length   An integer or a 1-D Tensor, depending on whether the time dimension is fixed-length or not. In case of variable length input, it is used for masking in case there's no mask specified.  
  time_major   Boolean. If true, the inputs and outputs will be in shape (timesteps, batch, ...), whereas in the False case, it will be (batch, timesteps, ...). Using time_major = True is a bit more efficient because it avoids transposes at the beginning and end of the RNN calculation. However, most TensorFlow data is batch-major, so by default this function accepts input and emits output in batch-major form.  
  zero_output_for_mask   Boolean. If True, the output for masked timestep will be zeros, whereas in the False case, output from previous timestep is returned.   
 


 Returns   A tuple, (last_output, outputs, new_states). last_output: the latest output of the rnn, of shape (samples, ...) outputs: tensor with shape (samples, time, ...) where each entry outputs[s, t] is the output of the step function at time t for sample s. new_states: list of tensors, latest states returned by the step function, of shape (samples, ...).  

 


 Raises
  ValueError   if input dimension is less than 3.  
  ValueError   if unroll is True but input timestep is not a fixed number.  
  ValueError   if mask is provided (not None) but states is not provided (len(states) == 0).
tf.keras.backend.set_epsilon     View source on GitHub    Sets the value of the fuzz factor used in numeric expressions.  View aliases  Compat aliases for migration 
See Migration guide for more details. tf.compat.v1.keras.backend.set_epsilon  
tf.keras.backend.set_epsilon(
    value
)

 


 Arguments
  value   float. New value of epsilon.    Example: 
tf.keras.backend.epsilon()
1e-07
tf.keras.backend.set_epsilon(1e-5)
tf.keras.backend.epsilon()
1e-05
tf.keras.backend.set_epsilon(1e-7)
tf.keras.backend.set_floatx     View source on GitHub    Sets the default float type.  View aliases  Compat aliases for migration 
See Migration guide for more details. tf.compat.v1.keras.backend.set_floatx  
tf.keras.backend.set_floatx(
    value
)
 
Note: It is not recommended to set this to float16 for training, as this will likely cause numeric stability issues. Instead, mixed precision, which is using a mix of float16 and float32, can be used by calling tf.keras.mixed_precision.experimental.set_policy('mixed_float16'). See the mixed precision guide for details.

 


 Arguments
  value   String; 'float16', 'float32', or 'float64'.    Example: 
tf.keras.backend.floatx()
'float32'
tf.keras.backend.set_floatx('float64')
tf.keras.backend.floatx()
'float64'
tf.keras.backend.set_floatx('float32')

 


 Raises
  ValueError   In case of invalid value.
tf.keras.backend.set_image_data_format     View source on GitHub    Sets the value of the image data format convention.  View aliases  Compat aliases for migration 
See Migration guide for more details. tf.compat.v1.keras.backend.set_image_data_format  
tf.keras.backend.set_image_data_format(
    data_format
)

 


 Arguments
  data_format   string. 'channels_first' or 'channels_last'.    Example: 
tf.keras.backend.image_data_format()
'channels_last'
tf.keras.backend.set_image_data_format('channels_first')
tf.keras.backend.image_data_format()
'channels_first'
tf.keras.backend.set_image_data_format('channels_last')

 


 Raises
  ValueError   In case of invalid data_format value.
Module: tf.keras.callbacks Callbacks: utilities called at certain points during model training. Modules experimental module: Public API for tf.keras.callbacks.experimental namespace. Classes class BaseLogger: Callback that accumulates epoch averages of metrics. class CSVLogger: Callback that streams epoch results to a CSV file. class Callback: Abstract base class used to build new callbacks. class CallbackList: Container abstracting a list of callbacks. class EarlyStopping: Stop training when a monitored metric has stopped improving. class History: Callback that records events into a History object. class LambdaCallback: Callback for creating simple, custom callbacks on-the-fly. class LearningRateScheduler: Learning rate scheduler. class ModelCheckpoint: Callback to save the Keras model or model weights at some frequency. class ProgbarLogger: Callback that prints metrics to stdout. class ReduceLROnPlateau: Reduce learning rate when a metric has stopped improving. class RemoteMonitor: Callback used to stream events to a server. class TensorBoard: Enable visualizations for TensorBoard. class TerminateOnNaN: Callback that terminates training when a NaN loss is encountered.
tf.keras.callbacks.BaseLogger     View source on GitHub    Callback that accumulates epoch averages of metrics. Inherits From: Callback  View aliases  Compat aliases for migration 
See Migration guide for more details. tf.compat.v1.keras.callbacks.BaseLogger  
tf.keras.callbacks.BaseLogger(
    stateful_metrics=None
)
 This callback is automatically applied to every Keras model.
 


 Arguments
  stateful_metrics   Iterable of string names of metrics that should not be averaged over an epoch. Metrics in this list will be logged as-is in on_epoch_end. All others will be averaged in on_epoch_end.    Methods set_model View source 
set_model(
    model
)
 set_params View source 
set_params(
    params
)
tf.keras.callbacks.CSVLogger     View source on GitHub    Callback that streams epoch results to a CSV file. Inherits From: Callback  View aliases  Compat aliases for migration 
See Migration guide for more details. tf.compat.v1.keras.callbacks.CSVLogger  
tf.keras.callbacks.CSVLogger(
    filename, separator=',', append=False
)
 Supports all values that can be represented as a string, including 1D iterables such as np.ndarray. Example: csv_logger = CSVLogger('training.log')
model.fit(X_train, Y_train, callbacks=[csv_logger])

 


 Arguments
  filename   Filename of the CSV file, e.g. 'run/log.csv'.  
  separator   String used to separate elements in the CSV file.  
  append   Boolean. True: append if file exists (useful for continuing training). False: overwrite existing file.    Methods set_model View source 
set_model(
    model
)
 set_params View source 
set_params(
    params
)
tf.keras.callbacks.EarlyStopping     View source on GitHub    Stop training when a monitored metric has stopped improving. Inherits From: Callback  View aliases  Compat aliases for migration 
See Migration guide for more details. tf.compat.v1.keras.callbacks.EarlyStopping  
tf.keras.callbacks.EarlyStopping(
    monitor='val_loss', min_delta=0, patience=0, verbose=0,
    mode='auto', baseline=None, restore_best_weights=False
)
 Assuming the goal of a training is to minimize the loss. With this, the metric to be monitored would be 'loss', and mode would be 'min'. A model.fit() training loop will check at end of every epoch whether the loss is no longer decreasing, considering the min_delta and patience if applicable. Once it's found no longer decreasing, model.stop_training is marked True and the training terminates. The quantity to be monitored needs to be available in logs dict. To make it so, pass the loss or metrics at model.compile().
 


 Arguments
  monitor   Quantity to be monitored.  
  min_delta   Minimum change in the monitored quantity to qualify as an improvement, i.e. an absolute change of less than min_delta, will count as no improvement.  
  patience   Number of epochs with no improvement after which training will be stopped.  
  verbose   verbosity mode.  
  mode   One of {"auto", "min", "max"}. In min mode, training will stop when the quantity monitored has stopped decreasing; in "max" mode it will stop when the quantity monitored has stopped increasing; in "auto" mode, the direction is automatically inferred from the name of the monitored quantity.  
  baseline   Baseline value for the monitored quantity. Training will stop if the model doesn't show improvement over the baseline.  
  restore_best_weights   Whether to restore model weights from the epoch with the best value of the monitored quantity. If False, the model weights obtained at the last step of training are used.    Example: 
callback = tf.keras.callbacks.EarlyStopping(monitor='loss', patience=3)
# This callback will stop the training when there is no improvement in
# the validation loss for three consecutive epochs.
model = tf.keras.models.Sequential([tf.keras.layers.Dense(10)])
model.compile(tf.keras.optimizers.SGD(), loss='mse')
history = model.fit(np.arange(100).reshape(5, 20), np.zeros(5),
                    epochs=10, batch_size=1, callbacks=[callback],
                    verbose=0)
len(history.history['loss'])  # Only 4 epochs are run.
4
 Methods get_monitor_value View source 
get_monitor_value(
    logs
)
 set_model View source 
set_model(
    model
)
 set_params View source 
set_params(
    params
)
Module: tf.keras.callbacks.experimental Public API for tf.keras.callbacks.experimental namespace. Classes class BackupAndRestore: Callback to back up and restore the training state.
tf.keras.callbacks.experimental.BackupAndRestore Callback to back up and restore the training state. Inherits From: Callback 
tf.keras.callbacks.experimental.BackupAndRestore(
    backup_dir
)
 BackupAndRestore callback is intended to recover from interruptions that happened in the middle of a model.fit execution by backing up the training states in a temporary checkpoint file (based on TF CheckpointManager) at the end of each epoch. If training restarted before completion, the training state and model are restored to the most recently saved state at the beginning of a new model.fit() run. Note that user is responsible to bring jobs back up. This callback is important for the backup and restore mechanism for fault tolerance purpose. And the model to be restored from an previous checkpoint is expected to be the same as the one used to back up. If user changes arguments passed to compile or fit, the checkpoint saved for fault tolerance can become invalid. Note:  This callback is not compatible with disabling eager execution. A checkpoint is saved at the end of each epoch, when restoring we'll redo any partial work from an unfinished epoch in which the training got restarted (so the work done before a interruption doesn't affect the final model state). This works for both single worker and multi-worker mode, only MirroredStrategy and MultiWorkerMirroredStrategy are supported for now.  Example: 
class InterruptingCallback(tf.keras.callbacks.Callback):
  def on_epoch_begin(self, epoch, logs=None):
    if epoch == 4:
      raise RuntimeError('Interrupting!')
callback = tf.keras.callbacks.experimental.BackupAndRestore(
backup_dir="/tmp")
model = tf.keras.models.Sequential([tf.keras.layers.Dense(10)])
model.compile(tf.keras.optimizers.SGD(), loss='mse')
try:
  model.fit(np.arange(100).reshape(5, 20), np.zeros(5), epochs=10,
            batch_size=1, callbacks=[callback, InterruptingCallback()],
            verbose=0)
except:
  pass
history = model.fit(np.arange(100).reshape(5, 20), np.zeros(5), epochs=10,
            batch_size=1, callbacks=[callback], verbose=0)
# Only 6 more epochs are run, since first trainning got interrupted at
# zero-indexed epoch 4, second training will continue from 4 to 9.
len(history.history['loss'])
6

 


 Arguments
  backup_dir   String, path to save the model file. This is the directory in which the system stores temporary files to recover the model from jobs terminated unexpectedly. The directory cannot be reused elsewhere to store other checkpoints, e.g. by BackupAndRestore callback of another training, or by another callback (ModelCheckpoint) of the same training.    Methods set_model View source 
set_model(
    model
)
 set_params View source 
set_params(
    params
)
tf.keras.callbacks.History     View source on GitHub    Callback that records events into a History object. Inherits From: Callback  View aliases  Compat aliases for migration 
See Migration guide for more details. tf.compat.v1.keras.callbacks.History  
tf.keras.callbacks.History()
 This callback is automatically applied to every Keras model. The History object gets returned by the fit method of models. Methods set_model View source 
set_model(
    model
)
 set_params View source 
set_params(
    params
)
tf.keras.callbacks.LambdaCallback     View source on GitHub    Callback for creating simple, custom callbacks on-the-fly. Inherits From: Callback  View aliases  Compat aliases for migration 
See Migration guide for more details. tf.compat.v1.keras.callbacks.LambdaCallback  
tf.keras.callbacks.LambdaCallback(
    on_epoch_begin=None, on_epoch_end=None, on_batch_begin=None, on_batch_end=None,
    on_train_begin=None, on_train_end=None, **kwargs
)
 This callback is constructed with anonymous functions that will be called at the appropriate time. Note that the callbacks expects positional arguments, as:  
on_epoch_begin and on_epoch_end expect two positional arguments: epoch, logs
 
on_batch_begin and on_batch_end expect two positional arguments: batch, logs
 
on_train_begin and on_train_end expect one positional argument: logs
 
 


 Arguments
  on_epoch_begin   called at the beginning of every epoch.  
  on_epoch_end   called at the end of every epoch.  
  on_batch_begin   called at the beginning of every batch.  
  on_batch_end   called at the end of every batch.  
  on_train_begin   called at the beginning of model training.  
  on_train_end   called at the end of model training.    Example: # Print the batch number at the beginning of every batch.
batch_print_callback = LambdaCallback(
    on_batch_begin=lambda batch,logs: print(batch))

# Stream the epoch loss to a file in JSON format. The file content
# is not well-formed JSON but rather has a JSON object per line.
import json
json_log = open('loss_log.json', mode='wt', buffering=1)
json_logging_callback = LambdaCallback(
    on_epoch_end=lambda epoch, logs: json_log.write(
        json.dumps({'epoch': epoch, 'loss': logs['loss']}) + '\n'),
    on_train_end=lambda logs: json_log.close()
)

# Terminate some processes after having finished model training.
processes = ...
cleanup_callback = LambdaCallback(
    on_train_end=lambda logs: [
        p.terminate() for p in processes if p.is_alive()])

model.fit(...,
          callbacks=[batch_print_callback,
                     json_logging_callback,
                     cleanup_callback])
 Methods set_model View source 
set_model(
    model
)
 set_params View source 
set_params(
    params
)
tf.keras.callbacks.LearningRateScheduler     View source on GitHub    Learning rate scheduler. Inherits From: Callback  View aliases  Compat aliases for migration 
See Migration guide for more details. tf.compat.v1.keras.callbacks.LearningRateScheduler  
tf.keras.callbacks.LearningRateScheduler(
    schedule, verbose=0
)
 At the beginning of every epoch, this callback gets the updated learning rate value from schedule function provided at __init__, with the current epoch and current learning rate, and applies the updated learning rate on the optimizer.
 


 Arguments
  schedule   a function that takes an epoch index (integer, indexed from 0) and current learning rate (float) as inputs and returns a new learning rate as output (float).  
  verbose   int. 0: quiet, 1: update messages.    Example: 
# This function keeps the initial learning rate for the first ten epochs
# and decreases it exponentially after that.
def scheduler(epoch, lr):
  if epoch < 10:
    return lr
  else:
    return lr * tf.math.exp(-0.1)

model = tf.keras.models.Sequential([tf.keras.layers.Dense(10)])
model.compile(tf.keras.optimizers.SGD(), loss='mse')
round(model.optimizer.lr.numpy(), 5)
0.01
 
callback = tf.keras.callbacks.LearningRateScheduler(scheduler)
history = model.fit(np.arange(100).reshape(5, 20), np.zeros(5),
                    epochs=15, callbacks=[callback], verbose=0)
round(model.optimizer.lr.numpy(), 5)
0.00607
 Methods set_model View source 
set_model(
    model
)
 set_params View source 
set_params(
    params
)
tf.keras.callbacks.ProgbarLogger     View source on GitHub    Callback that prints metrics to stdout. Inherits From: Callback  View aliases  Compat aliases for migration 
See Migration guide for more details. tf.compat.v1.keras.callbacks.ProgbarLogger  
tf.keras.callbacks.ProgbarLogger(
    count_mode='samples', stateful_metrics=None
)

 


 Arguments
  count_mode   One of "steps" or "samples". Whether the progress bar should count samples seen or steps (batches) seen.  
  stateful_metrics   Iterable of string names of metrics that should not be averaged over an epoch. Metrics in this list will be logged as-is. All others will be averaged over time (e.g. loss, etc). If not provided, defaults to the Model's metrics.   
 


 Raises
  ValueError   In case of invalid count_mode.    Methods set_model View source 
set_model(
    model
)
 set_params View source 
set_params(
    params
)