# You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """Implementation of the Keras API, the high-level API of TensorFlow. Detailed documentation and user guides are available at [keras.io](https://keras.io). """ # pylint: disable=unused-import from tensorflow.python import tf2 from keras import distribute from keras import models from keras.engine.input_layer import Input from keras.engine.sequential import Sequential from keras.engine.training import Model from tensorflow.python.util.tf_export import keras_export __version__ = '2.9.0' keras_export('keras.__version__').export_constant(__name__, '__version__')
maxlen: Optional Int, maximum length of all sequences. If not provided, sequences will be padded to the length of the longest individual sequence. dtype: (Optional, defaults to int32). Type of the output sequences. To pad sequences with variable length strings, you can use `object`. padding: String, 'pre' or 'post' (optional, defaults to 'pre'): pad either before or after each sequence. truncating: String, 'pre' or 'post' (optional, defaults to 'pre'): remove values from sequences larger than `maxlen`, either at the beginning or at the end of the sequences. value: Float or String, padding value. (Optional, defaults to 0.) Returns: Numpy array with shape `(len(sequences), maxlen)` Raises: ValueError: In case of invalid values for `truncating` or `padding`, or in case of invalid shape for a `sequences` entry. """ return sequence.pad_sequences(sequences, maxlen=maxlen, dtype=dtype, padding=padding, truncating=truncating, value=value) keras_export('keras.preprocessing.sequence.make_sampling_table')( make_sampling_table) keras_export('keras.preprocessing.sequence.skipgrams')(skipgrams)
Arguments: input_text: Input text (string). n: int. Size of vocabulary. filters: list (or concatenation) of characters to filter out, such as punctuation. Default: ``` '!"#$%&()*+,-./:;<=>?@[\]^_`{|}~\t\n ```, includes basic punctuation, tabs, and newlines. lower: boolean. Whether to set the text to lowercase. split: str. Separator for word splitting. Returns: List of integers in `[1, n]`. Each integer encodes a word (unicity non-guaranteed). """ return text.one_hot(input_text, n, filters=filters, lower=lower, split=split) # text.tokenizer_from_json is only available if keras_preprocessing >= 1.1.0 try: tokenizer_from_json = text.tokenizer_from_json keras_export('keras.preprocessing.text.tokenizer_from_json', allow_multiple_exports=True)( tokenizer_from_json) except AttributeError: pass keras_export('keras.preprocessing.text.hashing_trick', allow_multiple_exports=True)(hashing_trick) keras_export('keras.preprocessing.text.Tokenizer', allow_multiple_exports=True)(Tokenizer)
width_shift_range=width_shift_range, height_shift_range=height_shift_range, brightness_range=brightness_range, shear_range=shear_range, zoom_range=zoom_range, channel_shift_range=channel_shift_range, fill_mode=fill_mode, cval=cval, horizontal_flip=horizontal_flip, vertical_flip=vertical_flip, rescale=rescale, preprocessing_function=preprocessing_function, data_format=data_format, validation_split=validation_split, **kwargs) keras_export('keras.preprocessing.image.random_rotation')(random_rotation) keras_export('keras.preprocessing.image.random_shift')(random_shift) keras_export('keras.preprocessing.image.random_shear')(random_shear) keras_export('keras.preprocessing.image.random_zoom')(random_zoom) keras_export( 'keras.preprocessing.image.apply_channel_shift')(apply_channel_shift) keras_export( 'keras.preprocessing.image.random_channel_shift')(random_channel_shift) keras_export( 'keras.preprocessing.image.apply_brightness_shift')(apply_brightness_shift) keras_export('keras.preprocessing.image.random_brightness')(random_brightness) keras_export( 'keras.preprocessing.image.apply_affine_transform')(apply_affine_transform) keras_export('keras.preprocessing.image.load_img')(load_img)
# pylint: disable=protected-access """Utilities related to loss functions.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function import tensorflow as tf from keras import backend as K from keras.engine import keras_tensor from tensorflow.python.ops.losses import loss_reduction from tensorflow.python.util.tf_export import keras_export # TODO(joshl/psv): Update references to ReductionV2 to point to its # new location. ReductionV2 = loss_reduction.ReductionV2 keras_export('keras.losses.Reduction', v1=[], allow_multiple_exports=True)(loss_reduction.ReductionV2) def remove_squeezable_dimensions(labels, predictions, expected_rank_diff=0, name=None): """Squeeze last dim if ranks differ from expected by exactly 1. In the common case where we expect shapes to match, `expected_rank_diff` defaults to 0, and we squeeze the last dimension of the larger rank if they differ by 1. But, for example, if `labels` contains class IDs and `predictions` contains 1 probability per class, we expect `predictions` to have 1 more dimension than `labels`, so `expected_rank_diff` would be 1. In this case, we'd squeeze
# Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """Utilities for text input preprocessing. """ # pylint: disable=invalid-name from __future__ import absolute_import from __future__ import division from __future__ import print_function from keras_preprocessing import text from tensorflow.python.util.tf_export import keras_export text_to_word_sequence = text.text_to_word_sequence one_hot = text.one_hot hashing_trick = text.hashing_trick Tokenizer = text.Tokenizer tokenizer_from_json = text.tokenizer_from_json keras_export( 'keras.preprocessing.text.text_to_word_sequence')(text_to_word_sequence) keras_export('keras.preprocessing.text.one_hot')(one_hot) keras_export('keras.preprocessing.text.hashing_trick')(hashing_trick) keras_export('keras.preprocessing.text.Tokenizer')(Tokenizer) keras_export('keras.preprocessing.text.tokenizer_from_json')( tokenizer_from_json)
from tensorflow.python.distribute import distribution_strategy_context from tensorflow.python.framework import ops from tensorflow.python.keras import backend as K from tensorflow.python.keras.engine import keras_tensor from tensorflow.python.ops import array_ops from tensorflow.python.ops import control_flow_ops from tensorflow.python.ops import math_ops from tensorflow.python.ops.losses import loss_reduction from tensorflow.python.util.tf_export import keras_export # TODO(joshl/psv): Update references to ReductionV2 to point to its # new location. ReductionV2 = loss_reduction.ReductionV2 keras_export('keras.losses.Reduction', v1=[])(loss_reduction.ReductionV2) def remove_squeezable_dimensions( labels, predictions, expected_rank_diff=0, name=None): """Squeeze last dim if ranks differ from expected by exactly 1. In the common case where we expect shapes to match, `expected_rank_diff` defaults to 0, and we squeeze the last dimension of the larger rank if they differ by 1. But, for example, if `labels` contains class IDs and `predictions` contains 1 probability per class, we expect `predictions` to have 1 more dimension than `labels`, so `expected_rank_diff` would be 1. In this case, we'd squeeze `labels` if `rank(predictions) - rank(labels) == 0`, and `predictions` if `rank(predictions) - rank(labels) == 2`.
sequences will be padded to the length of the longest individual sequence. dtype: (Optional, defaults to int32). Type of the output sequences. To pad sequences with variable length strings, you can use `object`. padding: String, 'pre' or 'post' (optional, defaults to 'pre'): pad either before or after each sequence. truncating: String, 'pre' or 'post' (optional, defaults to 'pre'): remove values from sequences larger than `maxlen`, either at the beginning or at the end of the sequences. value: Float or String, padding value. (Optional, defaults to 0.) Returns: Numpy array with shape `(len(sequences), maxlen)` Raises: ValueError: In case of invalid values for `truncating` or `padding`, or in case of invalid shape for a `sequences` entry. """ return sequence.pad_sequences(sequences, maxlen=maxlen, dtype=dtype, padding=padding, truncating=truncating, value=value) keras_export('keras.preprocessing.sequence.make_sampling_table', allow_multiple_exports=True)(make_sampling_table) keras_export('keras.preprocessing.sequence.skipgrams', allow_multiple_exports=True)(skipgrams)
# you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """Implementation of the Keras API, the high-level API of TensorFlow. Detailed documentation and user guides are available at [keras.io](https://keras.io). """ from keras import distribute from keras import models from keras.engine.input_layer import Input from keras.engine.sequential import Sequential from keras.engine.training import Model # isort: off from tensorflow.python import tf2 from tensorflow.python.util.tf_export import keras_export __version__ = "2.10.0" keras_export("keras.__version__").export_constant(__name__, "__version__")
filters: list (or concatenation) of characters to filter out, such as punctuation. Default: ``` '!"#$%&()*+,-./:;<=>?@[\]^_`{|}~\t\n ```, includes basic punctuation, tabs, and newlines. lower: boolean. Whether to set the text to lowercase. split: str. Separator for word splitting. Returns: List of integers in `[1, n]`. Each integer encodes a word (unicity non-guaranteed). """ return text.one_hot(input_text, n, filters=filters, lower=lower, split=split) # text.tokenizer_from_json is only available if keras_preprocessing >= 1.1.0 try: tokenizer_from_json = text.tokenizer_from_json keras_export('keras.preprocessing.text.tokenizer_from_json')( tokenizer_from_json) except AttributeError: pass keras_export('keras.preprocessing.text.hashing_trick')(hashing_trick) keras_export('keras.preprocessing.text.Tokenizer')(Tokenizer)
import tensorflow.compat.v2 as tf # isort: off from tensorflow.python.util.tf_export import keras_export _v1_zeros_initializer = tf.compat.v1.zeros_initializer _v1_ones_initializer = tf.compat.v1.ones_initializer _v1_constant_initializer = tf.compat.v1.constant_initializer _v1_variance_scaling_initializer = tf.compat.v1.variance_scaling_initializer _v1_orthogonal_initializer = tf.compat.v1.orthogonal_initializer _v1_identity = tf.compat.v1.initializers.identity _v1_glorot_uniform_initializer = tf.compat.v1.glorot_uniform_initializer _v1_glorot_normal_initializer = tf.compat.v1.glorot_normal_initializer keras_export( v1=["keras.initializers.Zeros", "keras.initializers.zeros"], allow_multiple_exports=True, )(_v1_zeros_initializer) keras_export( v1=["keras.initializers.Ones", "keras.initializers.ones"], allow_multiple_exports=True, )(_v1_ones_initializer) keras_export( v1=["keras.initializers.Constant", "keras.initializers.constant"], allow_multiple_exports=True, )(_v1_constant_initializer) keras_export(v1=["keras.initializers.VarianceScaling"], allow_multiple_exports=True)(_v1_variance_scaling_initializer) keras_export( v1=["keras.initializers.Orthogonal", "keras.initializers.orthogonal"], allow_multiple_exports=True, )(_v1_orthogonal_initializer)
# # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """Utilities for text input preprocessing. """ # pylint: disable=invalid-name from __future__ import absolute_import from __future__ import division from __future__ import print_function from keras_preprocessing import text from tensorflow.python.util.tf_export import keras_export text_to_word_sequence = text.text_to_word_sequence one_hot = text.one_hot hashing_trick = text.hashing_trick Tokenizer = text.Tokenizer keras_export( 'keras.preprocessing.text.text_to_word_sequence')(text_to_word_sequence) keras_export('keras.preprocessing.text.one_hot')(one_hot) keras_export('keras.preprocessing.text.hashing_trick')(hashing_trick) keras_export('keras.preprocessing.text.Tokenizer')(Tokenizer)
from tensorflow.python.keras import backend from tensorflow.python.keras import callbacks from tensorflow.python.keras import callbacks_v1 from tensorflow.python.keras import constraints from tensorflow.python.keras import datasets from tensorflow.python.keras import estimator from tensorflow.python.keras import initializers from tensorflow.python.keras import layers from tensorflow.python.keras import losses from tensorflow.python.keras import metrics from tensorflow.python.keras import models from tensorflow.python.keras import ops from tensorflow.python.keras import optimizers from tensorflow.python.keras import preprocessing from tensorflow.python.keras import regularizers from tensorflow.python.keras import utils from tensorflow.python.keras import wrappers from tensorflow.python.keras.layers import Input from tensorflow.python.keras.models import Model from tensorflow.python.keras.models import Sequential from tensorflow.python.util.tf_export import keras_export __version__ = '2.2.4-tf' keras_export('keras.__version__').export_constant(__name__, '__version__') del absolute_import del division del print_function
from tensorflow.python.distribute import distribution_strategy_context from tensorflow.python.framework import dtypes from tensorflow.python.framework import ops from tensorflow.python.keras import backend as K from tensorflow.python.ops import array_ops from tensorflow.python.ops import confusion_matrix from tensorflow.python.ops import control_flow_ops from tensorflow.python.ops import math_ops from tensorflow.python.ops import weights_broadcast_ops from tensorflow.python.ops.losses import loss_reduction from tensorflow.python.util.tf_export import keras_export # TODO(joshl/psv): Update references to ReductionV2 to point to its # new location. ReductionV2 = keras_export( # pylint: disable=invalid-name 'keras.losses.Reduction', v1=[])(loss_reduction.ReductionV2) def squeeze_or_expand_dimensions(y_pred, y_true, sample_weight): """Squeeze or expand last dimension if needed. 1. Squeezes last dim of `y_pred` or `y_true` if their rank differs by 1 (using `confusion_matrix.remove_squeezable_dimensions`). 2. Squeezes or expands last dim of `sample_weight` if its rank differs by 1 from the new rank of `y_pred`. If `sample_weight` is scalar, it is kept scalar. This will use static shape if available. Otherwise, it will add graph operations, which could result in a performance hit. Args:
# See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """Module for exporting TensorFlow ops under tf.keras.*.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from tensorflow.python.framework import ops from tensorflow.python.ops import init_ops from tensorflow.python.ops import init_ops_v2 from tensorflow.python.ops.losses import losses_impl from tensorflow.python.util.tf_export import keras_export # pylint: disable=bad-continuation keras_export(v1=["keras.initializers.Initializer"])(init_ops.Initializer) keras_export(v1=["keras.initializers.Zeros", "keras.initializers.zeros"])( init_ops.Zeros) keras_export(v1=["keras.initializers.Ones", "keras.initializers.ones"])( init_ops.Ones) keras_export( v1=["keras.initializers.Constant", "keras.initializers.constant"])( init_ops.Constant) keras_export(v1=["keras.initializers.VarianceScaling"])( init_ops.VarianceScaling) keras_export( v1=["keras.initializers.Orthogonal", "keras.initializers.orthogonal"])( init_ops.Orthogonal) keras_export( v1=["keras.initializers.Identity", "keras.initializers.identity"])( init_ops.Identity)
from __future__ import print_function from tensorflow.python.distribute import distribution_strategy_context from tensorflow.python.framework import ops from tensorflow.python.keras import backend as K from tensorflow.python.keras.engine import keras_tensor from tensorflow.python.ops import array_ops from tensorflow.python.ops import control_flow_ops from tensorflow.python.ops import math_ops from tensorflow.python.ops.losses import loss_reduction from tensorflow.python.ops.losses import util as tf_losses_utils from tensorflow.python.util.tf_export import keras_export # TODO(joshl/psv): Update references to ReductionV2 to point to its # new location. ReductionV2 = keras_export( # pylint: disable=invalid-name 'keras.losses.Reduction', v1=[])(loss_reduction.ReductionV2) def remove_squeezable_dimensions(labels, predictions, expected_rank_diff=0, name=None): """Squeeze last dim if ranks differ from expected by exactly 1. In the common case where we expect shapes to match, `expected_rank_diff` defaults to 0, and we squeeze the last dimension of the larger rank if they differ by 1. But, for example, if `labels` contains class IDs and `predictions` contains 1 probability per class, we expect `predictions` to have 1 more dimension than `labels`, so `expected_rank_diff` would be 1. In this case, we'd squeeze
target_size=target_size, color_mode=color_mode, classes=classes, class_mode=class_mode, data_format=self.data_format, batch_size=batch_size, shuffle=shuffle, seed=seed, save_to_dir=save_to_dir, save_prefix=save_prefix, save_format=save_format, subset=subset, interpolation=interpolation, validate_filenames=validate_filenames) keras_export('keras.preprocessing.image.random_rotation', allow_multiple_exports=True)(random_rotation) keras_export('keras.preprocessing.image.random_shift', allow_multiple_exports=True)(random_shift) keras_export('keras.preprocessing.image.random_shear', allow_multiple_exports=True)(random_shear) keras_export('keras.preprocessing.image.random_zoom', allow_multiple_exports=True)(random_zoom) keras_export( 'keras.preprocessing.image.apply_channel_shift', allow_multiple_exports=True)(apply_channel_shift) keras_export( 'keras.preprocessing.image.random_channel_shift', allow_multiple_exports=True)(random_channel_shift) keras_export( 'keras.preprocessing.image.apply_brightness_shift', allow_multiple_exports=True)(apply_brightness_shift) keras_export('keras.preprocessing.image.random_brightness', allow_multiple_exports=True)(random_brightness) keras_export( 'keras.preprocessing.image.apply_affine_transform', allow_multiple_exports=True)(apply_affine_transform) keras_export('keras.preprocessing.image.load_img', allow_multiple_exports=True)(load_img)
"""Keras initializers for TF 1.""" # pylint:disable=g-classes-have-attributes import tensorflow.compat.v2 as tf from tensorflow.python.util.tf_export import keras_export _v1_zeros_initializer = tf.compat.v1.zeros_initializer _v1_ones_initializer = tf.compat.v1.ones_initializer _v1_constant_initializer = tf.compat.v1.constant_initializer _v1_variance_scaling_initializer = tf.compat.v1.variance_scaling_initializer _v1_orthogonal_initializer = tf.compat.v1.orthogonal_initializer _v1_identity = tf.compat.v1.initializers.identity _v1_glorot_uniform_initializer = tf.compat.v1.glorot_uniform_initializer _v1_glorot_normal_initializer = tf.compat.v1.glorot_normal_initializer keras_export(v1=['keras.initializers.Zeros', 'keras.initializers.zeros'], allow_multiple_exports=True)(_v1_zeros_initializer) keras_export(v1=['keras.initializers.Ones', 'keras.initializers.ones'], allow_multiple_exports=True)(_v1_ones_initializer) keras_export(v1=['keras.initializers.Constant', 'keras.initializers.constant'], allow_multiple_exports=True)(_v1_constant_initializer) keras_export(v1=['keras.initializers.VarianceScaling'], allow_multiple_exports=True)(_v1_variance_scaling_initializer) keras_export( v1=['keras.initializers.Orthogonal', 'keras.initializers.orthogonal'], allow_multiple_exports=True)(_v1_orthogonal_initializer) keras_export(v1=['keras.initializers.Identity', 'keras.initializers.identity'], allow_multiple_exports=True)(_v1_identity) keras_export(v1=['keras.initializers.glorot_uniform'], allow_multiple_exports=True)(_v1_glorot_uniform_initializer) keras_export(v1=['keras.initializers.glorot_normal'], allow_multiple_exports=True)(_v1_glorot_normal_initializer)
# limitations under the License. # ============================================================================== """Module for exporting TensorFlow ops under tf.keras.*.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from tensorflow.python.framework import ops from tensorflow.python.ops import init_ops from tensorflow.python.ops import init_ops_v2 from tensorflow.python.ops.losses import losses_impl from tensorflow.python.util.tf_export import keras_export # pylint: disable=bad-continuation keras_export(v1=["keras.initializers.Initializer"])( init_ops.Initializer) keras_export(v1=["keras.initializers.Zeros", "keras.initializers.zeros"])( init_ops.Zeros) keras_export(v1=["keras.initializers.Ones", "keras.initializers.ones"])( init_ops.Ones) keras_export(v1=["keras.initializers.Constant", "keras.initializers.constant"])( init_ops.Constant) keras_export(v1=["keras.initializers.VarianceScaling"])( init_ops.VarianceScaling) keras_export(v1=["keras.initializers.Orthogonal", "keras.initializers.orthogonal"])( init_ops.Orthogonal) keras_export(v1=["keras.initializers.Identity", "keras.initializers.identity"])( init_ops.Identity) keras_export(v1=["keras.initializers.glorot_uniform"])(
height_shift_range=height_shift_range, brightness_range=brightness_range, shear_range=shear_range, zoom_range=zoom_range, channel_shift_range=channel_shift_range, fill_mode=fill_mode, cval=cval, horizontal_flip=horizontal_flip, vertical_flip=vertical_flip, rescale=rescale, preprocessing_function=preprocessing_function, data_format=data_format, validation_split=validation_split, **kwargs) keras_export('keras.preprocessing.image.random_rotation')(random_rotation) keras_export('keras.preprocessing.image.random_shift')(random_shift) keras_export('keras.preprocessing.image.random_shear')(random_shear) keras_export('keras.preprocessing.image.random_zoom')(random_zoom) keras_export('keras.preprocessing.image.apply_channel_shift')( apply_channel_shift) keras_export('keras.preprocessing.image.random_channel_shift')( random_channel_shift) keras_export('keras.preprocessing.image.apply_brightness_shift')( apply_brightness_shift) keras_export('keras.preprocessing.image.random_brightness')(random_brightness) keras_export('keras.preprocessing.image.apply_affine_transform')( apply_affine_transform) keras_export('keras.preprocessing.image.load_img')(load_img)
batch_size: Number of timeseries samples in each batch (except maybe the last one). # Returns A [Sequence](/utils/#sequence) instance. # Examples ```python from keras.preprocessing.sequence import TimeseriesGenerator import numpy as np data = np.array([[i] for i in range(50)]) targets = np.array([[i] for i in range(50)]) data_gen = TimeseriesGenerator(data, targets, length=10, sampling_rate=2, batch_size=2) assert len(data_gen) == 20 batch_0 = data_gen[0] x, y = batch_0 assert np.array_equal(x, np.array([[[0], [2], [4], [6], [8]], [[1], [3], [5], [7], [9]]])) assert np.array_equal(y, np.array([[10], [11]])) ``` """ pass keras_export('keras.preprocessing.sequence.pad_sequences')(pad_sequences) keras_export( 'keras.preprocessing.sequence.make_sampling_table')(make_sampling_table) keras_export('keras.preprocessing.sequence.skipgrams')(skipgrams)