The following issues were found
keras/applications/efficientnet_weight_update_util.py
160 issues
Line: 39
Column: 1
--ckpt noisy_student_efficientnet-b3/model.ckpt --o efficientnetb3_new.h5
"""
import tensorflow.compat.v2 as tf
import argparse
import warnings
from tensorflow.keras.applications import efficientnet
Reported by Pylint.
Line: 43
Column: 1
import argparse
import warnings
from tensorflow.keras.applications import efficientnet
def write_ckpt_to_h5(path_h5, path_ckpt, keras_model, use_ema=True):
"""Map the weights in checkpoint file (tf) to h5 file (keras).
Reported by Pylint.
Line: 109
Column: 9
warnings.warn('Fail to load top layer variable {}'
'from {} because of {}.'.format(w.name, tf_name, e))
else:
raise ValueError('Fail to load {} from {}'.format(w.name, tf_name))
total_weights = len(keras_model.weights)
print('{}/{} weights updated'.format(changed_weights, total_weights))
keras_model.save_weights(path_h5)
Reported by Pylint.
Line: 41
Column: 1
import tensorflow.compat.v2 as tf
import argparse
import warnings
from tensorflow.keras.applications import efficientnet
def write_ckpt_to_h5(path_h5, path_ckpt, keras_model, use_ema=True):
Reported by Pylint.
Line: 42
Column: 1
import tensorflow.compat.v2 as tf
import argparse
import warnings
from tensorflow.keras.applications import efficientnet
def write_ckpt_to_h5(path_h5, path_ckpt, keras_model, use_ema=True):
"""Map the weights in checkpoint file (tf) to h5 file (keras).
Reported by Pylint.
Line: 46
Column: 1
from tensorflow.keras.applications import efficientnet
def write_ckpt_to_h5(path_h5, path_ckpt, keras_model, use_ema=True):
"""Map the weights in checkpoint file (tf) to h5 file (keras).
Args:
path_h5: str, path to output hdf5 file to write weights loaded from ckpt
files.
Reported by Pylint.
Line: 47
Column: 1
def write_ckpt_to_h5(path_h5, path_ckpt, keras_model, use_ema=True):
"""Map the weights in checkpoint file (tf) to h5 file (keras).
Args:
path_h5: str, path to output hdf5 file to write weights loaded from ckpt
files.
path_ckpt: str, path to the ckpt files (e.g. 'efficientnet-b0/model.ckpt')
Reported by Pylint.
Line: 59
Column: 1
functions (e.g. EfficientNetB0)
use_ema: Bool, whether to use ExponentialMovingAverage result or not
"""
model_name_keras = keras_model.name
model_name_tf = model_name_keras.replace('efficientnet', 'efficientnet-')
keras_weight_names = [w.name for w in keras_model.weights]
tf_weight_names = get_variable_names_from_ckpt(path_ckpt)
Reported by Pylint.
Line: 60
Column: 1
use_ema: Bool, whether to use ExponentialMovingAverage result or not
"""
model_name_keras = keras_model.name
model_name_tf = model_name_keras.replace('efficientnet', 'efficientnet-')
keras_weight_names = [w.name for w in keras_model.weights]
tf_weight_names = get_variable_names_from_ckpt(path_ckpt)
keras_blocks = get_keras_blocks(keras_weight_names)
Reported by Pylint.
Line: 62
Column: 1
model_name_keras = keras_model.name
model_name_tf = model_name_keras.replace('efficientnet', 'efficientnet-')
keras_weight_names = [w.name for w in keras_model.weights]
tf_weight_names = get_variable_names_from_ckpt(path_ckpt)
keras_blocks = get_keras_blocks(keras_weight_names)
tf_blocks = get_tf_blocks(tf_weight_names)
Reported by Pylint.
keras/preprocessing/image.py
157 issues
Line: 16
Column: 1
# limitations under the License.
# ==============================================================================
# pylint: disable=invalid-name
# pylint: disable=g-import-not-at-top
# pylint: disable=g-classes-have-attributes
"""Set of tools for real-time data augmentation on image data."""
import tensorflow.compat.v2 as tf
Reported by Pylint.
Line: 17
Column: 1
# ==============================================================================
# pylint: disable=invalid-name
# pylint: disable=g-import-not-at-top
# pylint: disable=g-classes-have-attributes
"""Set of tools for real-time data augmentation on image data."""
import tensorflow.compat.v2 as tf
from keras_preprocessing import image
Reported by Pylint.
Line: 20
Column: 1
# pylint: disable=g-classes-have-attributes
"""Set of tools for real-time data augmentation on image data."""
import tensorflow.compat.v2 as tf
from keras_preprocessing import image
import numpy as np
try:
from scipy import linalg # pylint: disable=unused-import
Reported by Pylint.
Line: 22
Column: 1
import tensorflow.compat.v2 as tf
from keras_preprocessing import image
import numpy as np
try:
from scipy import linalg # pylint: disable=unused-import
from scipy import ndimage # pylint: disable=unused-import
except ImportError:
Reported by Pylint.
Line: 33
Column: 1
from keras.preprocessing.image_dataset import image_dataset_from_directory # pylint: disable=unused-import
from keras.utils import data_utils
from keras.utils import tf_inspect
from tensorflow.python.platform import tf_logging
from tensorflow.python.util.tf_export import keras_export
random_rotation = image.random_rotation
random_shift = image.random_shift
random_shear = image.random_shear
Reported by Pylint.
Line: 34
Column: 1
from keras.utils import data_utils
from keras.utils import tf_inspect
from tensorflow.python.platform import tf_logging
from tensorflow.python.util.tf_export import keras_export
random_rotation = image.random_rotation
random_shift = image.random_shift
random_shear = image.random_shear
random_zoom = image.random_zoom
Reported by Pylint.
Line: 318
Column: 1
@keras_export('keras.preprocessing.image.Iterator')
class Iterator(image.Iterator, data_utils.Sequence):
pass
@keras_export('keras.preprocessing.image.DirectoryIterator')
class DirectoryIterator(image.DirectoryIterator, Iterator): # pylint: disable=inconsistent-mro
Reported by Pylint.
Line: 318
Column: 1
@keras_export('keras.preprocessing.image.Iterator')
class Iterator(image.Iterator, data_utils.Sequence):
pass
@keras_export('keras.preprocessing.image.DirectoryIterator')
class DirectoryIterator(image.DirectoryIterator, Iterator): # pylint: disable=inconsistent-mro
Reported by Pylint.
Line: 323
Column: 1
@keras_export('keras.preprocessing.image.DirectoryIterator')
class DirectoryIterator(image.DirectoryIterator, Iterator): # pylint: disable=inconsistent-mro
"""Iterator capable of reading images from a directory on disk.
Args:
directory: Path to the directory to read images from.
Each subdirectory in this directory will be
Reported by Pylint.
Line: 323
Column: 1
@keras_export('keras.preprocessing.image.DirectoryIterator')
class DirectoryIterator(image.DirectoryIterator, Iterator): # pylint: disable=inconsistent-mro
"""Iterator capable of reading images from a directory on disk.
Args:
directory: Path to the directory to read images from.
Each subdirectory in this directory will be
Reported by Pylint.
keras/saving/saving_utils.py
157 issues
Line: 17
Column: 1
# ==============================================================================
"""Utils related to keras model saving."""
# pylint: disable=g-bad-import-order, g-direct-tensorflow-import
import tensorflow.compat.v2 as tf
import copy
import os
from keras import backend as K
Reported by Pylint.
Line: 17
Column: 1
# ==============================================================================
"""Utils related to keras model saving."""
# pylint: disable=g-bad-import-order, g-direct-tensorflow-import
import tensorflow.compat.v2 as tf
import copy
import os
from keras import backend as K
Reported by Pylint.
Line: 18
Column: 1
"""Utils related to keras model saving."""
# pylint: disable=g-bad-import-order, g-direct-tensorflow-import
import tensorflow.compat.v2 as tf
import copy
import os
from keras import backend as K
from keras import losses
Reported by Pylint.
Line: 30
Column: 1
from keras.utils import generic_utils
from keras.utils import version_utils
from keras.utils.io_utils import ask_to_proceed_with_overwrite
from tensorflow.python.platform import tf_logging as logging
# pylint: enable=g-bad-import-order, g-direct-tensorflow-import
def extract_model_metrics(model):
"""Convert metrics from a Keras model `compile` API to dictionary.
Reported by Pylint.
Line: 31
Column: 1
from keras.utils import version_utils
from keras.utils.io_utils import ask_to_proceed_with_overwrite
from tensorflow.python.platform import tf_logging as logging
# pylint: enable=g-bad-import-order, g-direct-tensorflow-import
def extract_model_metrics(model):
"""Convert metrics from a Keras model `compile` API to dictionary.
Reported by Pylint.
Line: 31
Column: 1
from keras.utils import version_utils
from keras.utils.io_utils import ask_to_proceed_with_overwrite
from tensorflow.python.platform import tf_logging as logging
# pylint: enable=g-bad-import-order, g-direct-tensorflow-import
def extract_model_metrics(model):
"""Convert metrics from a Keras model `compile` API to dictionary.
Reported by Pylint.
Line: 130
Column: 1
# Outputs always has to be a flat dict.
output_names = model.output_names # Functional Model.
if output_names is None: # Subclassed Model.
from keras.engine import compile_utils # pylint: disable=g-import-not-at-top
output_names = compile_utils.create_pseudo_output_names(outputs)
outputs = tf.nest.flatten(outputs)
return {name: output for name, output in zip(output_names, outputs)}
return _wrapped_model.get_concrete_function(*model_args, **model_kwargs)
Reported by Pylint.
Line: 140
Column: 1
def model_metadata(model, include_optimizer=True, require_config=True):
"""Returns a dictionary containing the model metadata."""
from keras import __version__ as keras_version # pylint: disable=g-import-not-at-top
from keras.optimizer_v2 import optimizer_v2 # pylint: disable=g-import-not-at-top
model_config = {'class_name': model.__class__.__name__}
try:
model_config['config'] = model.get_config()
Reported by Pylint.
Line: 141
Column: 1
def model_metadata(model, include_optimizer=True, require_config=True):
"""Returns a dictionary containing the model metadata."""
from keras import __version__ as keras_version # pylint: disable=g-import-not-at-top
from keras.optimizer_v2 import optimizer_v2 # pylint: disable=g-import-not-at-top
model_config = {'class_name': model.__class__.__name__}
try:
model_config['config'] = model.get_config()
except NotImplementedError as e:
Reported by Pylint.
Line: 276
Column: 1
def _deserialize_metric(metric_config):
"""Deserialize metrics, leaving special strings untouched."""
from keras import metrics as metrics_module # pylint:disable=g-import-not-at-top
if metric_config in ['accuracy', 'acc', 'crossentropy', 'ce']:
# Do not deserialize accuracy and cross-entropy strings as we have special
# case handling for these in compile, based on model output shape.
return metric_config
return metrics_module.deserialize(metric_config)
Reported by Pylint.
keras/layers/pooling_test.py
156 issues
Line: 17
Column: 1
# ==============================================================================
"""Tests for pooling layers."""
import tensorflow.compat.v2 as tf
from absl.testing import parameterized
import numpy as np
import keras
Reported by Pylint.
Line: 19
Column: 1
import tensorflow.compat.v2 as tf
from absl.testing import parameterized
import numpy as np
import keras
from keras import combinations
from keras import testing_utils
Reported by Pylint.
Line: 269
Column: 3
# to be properly assigned to a GPU when running in eager mode.
if not tf.executing_eagerly():
# Only runs on GPU with CUDA, channels_first is not supported on CPU.
# TODO(b/62340061): Support channels_first on CPU.
if tf.test.is_gpu_available(cuda_only=True):
testing_utils.layer_test(
keras.layers.AveragePooling2D,
kwargs={
'strides': (1, 1),
Reported by Pylint.
Line: 29
Column: 1
@combinations.generate(combinations.combine(mode=['graph', 'eager']))
class GlobalPoolingTest(tf.test.TestCase, parameterized.TestCase):
@testing_utils.enable_v2_dtype_behavior
def test_mixed_float16_policy(self):
with policy.policy_scope('mixed_float16'):
inputs1 = keras.Input(shape=(36, 512), dtype='float16')
Reported by Pylint.
Line: 31
Column: 1
@combinations.generate(combinations.combine(mode=['graph', 'eager']))
class GlobalPoolingTest(tf.test.TestCase, parameterized.TestCase):
@testing_utils.enable_v2_dtype_behavior
def test_mixed_float16_policy(self):
with policy.policy_scope('mixed_float16'):
inputs1 = keras.Input(shape=(36, 512), dtype='float16')
inputs2 = keras.Input(shape=(36,), dtype='bool')
average_layer = keras.layers.pooling.GlobalAveragePooling1D()
Reported by Pylint.
Line: 32
Column: 3
class GlobalPoolingTest(tf.test.TestCase, parameterized.TestCase):
@testing_utils.enable_v2_dtype_behavior
def test_mixed_float16_policy(self):
with policy.policy_scope('mixed_float16'):
inputs1 = keras.Input(shape=(36, 512), dtype='float16')
inputs2 = keras.Input(shape=(36,), dtype='bool')
average_layer = keras.layers.pooling.GlobalAveragePooling1D()
_ = average_layer(inputs1, inputs2)
Reported by Pylint.
Line: 32
Column: 1
class GlobalPoolingTest(tf.test.TestCase, parameterized.TestCase):
@testing_utils.enable_v2_dtype_behavior
def test_mixed_float16_policy(self):
with policy.policy_scope('mixed_float16'):
inputs1 = keras.Input(shape=(36, 512), dtype='float16')
inputs2 = keras.Input(shape=(36,), dtype='bool')
average_layer = keras.layers.pooling.GlobalAveragePooling1D()
_ = average_layer(inputs1, inputs2)
Reported by Pylint.
Line: 32
Column: 3
class GlobalPoolingTest(tf.test.TestCase, parameterized.TestCase):
@testing_utils.enable_v2_dtype_behavior
def test_mixed_float16_policy(self):
with policy.policy_scope('mixed_float16'):
inputs1 = keras.Input(shape=(36, 512), dtype='float16')
inputs2 = keras.Input(shape=(36,), dtype='bool')
average_layer = keras.layers.pooling.GlobalAveragePooling1D()
_ = average_layer(inputs1, inputs2)
Reported by Pylint.
Line: 33
Column: 1
@testing_utils.enable_v2_dtype_behavior
def test_mixed_float16_policy(self):
with policy.policy_scope('mixed_float16'):
inputs1 = keras.Input(shape=(36, 512), dtype='float16')
inputs2 = keras.Input(shape=(36,), dtype='bool')
average_layer = keras.layers.pooling.GlobalAveragePooling1D()
_ = average_layer(inputs1, inputs2)
Reported by Pylint.
Line: 34
Column: 1
@testing_utils.enable_v2_dtype_behavior
def test_mixed_float16_policy(self):
with policy.policy_scope('mixed_float16'):
inputs1 = keras.Input(shape=(36, 512), dtype='float16')
inputs2 = keras.Input(shape=(36,), dtype='bool')
average_layer = keras.layers.pooling.GlobalAveragePooling1D()
_ = average_layer(inputs1, inputs2)
def test_globalpooling_1d(self):
Reported by Pylint.
keras/utils/tf_utils_test.py
153 issues
Line: 17
Column: 1
# ==============================================================================
"""Tests for Keras TF utils."""
import tensorflow.compat.v2 as tf
from absl.testing import parameterized
import keras
from keras import combinations
Reported by Pylint.
Line: 19
Column: 1
import tensorflow.compat.v2 as tf
from absl.testing import parameterized
import keras
from keras import combinations
from keras.utils import tf_utils
Reported by Pylint.
Line: 26
Column: 1
from keras.utils import tf_utils
try:
import attr # pylint:disable=g-import-not-at-top
except ImportError:
attr = None
@combinations.generate(combinations.combine(mode=['graph', 'eager']))
Reported by Pylint.
Line: 26
Column: 1
from keras.utils import tf_utils
try:
import attr # pylint:disable=g-import-not-at-top
except ImportError:
attr = None
@combinations.generate(combinations.combine(mode=['graph', 'eager']))
Reported by Pylint.
Line: 28
Column: 1
try:
import attr # pylint:disable=g-import-not-at-top
except ImportError:
attr = None
@combinations.generate(combinations.combine(mode=['graph', 'eager']))
class TestIsSymbolicTensor(tf.test.TestCase, parameterized.TestCase):
Reported by Pylint.
Line: 32
Column: 1
@combinations.generate(combinations.combine(mode=['graph', 'eager']))
class TestIsSymbolicTensor(tf.test.TestCase, parameterized.TestCase):
def test_default_behavior(self):
if tf.executing_eagerly():
self.assertFalse(tf_utils.is_symbolic_tensor(
tf.Variable(name='blah', initial_value=0.)))
Reported by Pylint.
Line: 34
Column: 3
@combinations.generate(combinations.combine(mode=['graph', 'eager']))
class TestIsSymbolicTensor(tf.test.TestCase, parameterized.TestCase):
def test_default_behavior(self):
if tf.executing_eagerly():
self.assertFalse(tf_utils.is_symbolic_tensor(
tf.Variable(name='blah', initial_value=0.)))
self.assertFalse(
tf_utils.is_symbolic_tensor(
Reported by Pylint.
Line: 34
Column: 1
@combinations.generate(combinations.combine(mode=['graph', 'eager']))
class TestIsSymbolicTensor(tf.test.TestCase, parameterized.TestCase):
def test_default_behavior(self):
if tf.executing_eagerly():
self.assertFalse(tf_utils.is_symbolic_tensor(
tf.Variable(name='blah', initial_value=0.)))
self.assertFalse(
tf_utils.is_symbolic_tensor(
Reported by Pylint.
Line: 35
Column: 1
class TestIsSymbolicTensor(tf.test.TestCase, parameterized.TestCase):
def test_default_behavior(self):
if tf.executing_eagerly():
self.assertFalse(tf_utils.is_symbolic_tensor(
tf.Variable(name='blah', initial_value=0.)))
self.assertFalse(
tf_utils.is_symbolic_tensor(
tf.convert_to_tensor(0.)))
Reported by Pylint.
Line: 36
Column: 1
def test_default_behavior(self):
if tf.executing_eagerly():
self.assertFalse(tf_utils.is_symbolic_tensor(
tf.Variable(name='blah', initial_value=0.)))
self.assertFalse(
tf_utils.is_symbolic_tensor(
tf.convert_to_tensor(0.)))
self.assertFalse(tf_utils.is_symbolic_tensor(
Reported by Pylint.
keras/layers/dense_attention.py
152 issues
Line: 21
Column: 1
Attention is formed by three tensors: Query, Key and Value.
"""
import tensorflow.compat.v2 as tf
from keras import backend
from keras.engine.base_layer import Layer
from keras.utils import control_flow_util
from tensorflow.python.util.tf_export import keras_export
Reported by Pylint.
Line: 25
Column: 1
from keras import backend
from keras.engine.base_layer import Layer
from keras.utils import control_flow_util
from tensorflow.python.util.tf_export import keras_export
class BaseDenseAttention(Layer):
"""Base Attention class for Dense networks.
Reported by Pylint.
Line: 77
Column: 38
self.dropout = dropout
self.supports_masking = True
def _calculate_scores(self, query, key):
"""Calculates attention scores.
Args:
query: Query tensor of shape `[batch_size, Tq, dim]`.
key: Key tensor of shape `[batch_size, Tv, dim]`.
Reported by Pylint.
Line: 77
Column: 31
self.dropout = dropout
self.supports_masking = True
def _calculate_scores(self, query, key):
"""Calculates attention scores.
Args:
query: Query tensor of shape `[batch_size, Tq, dim]`.
key: Key tensor of shape `[batch_size, Tv, dim]`.
Reported by Pylint.
Line: 135
Column: 3
lambda: tf.identity(weights))
return tf.matmul(weights, value), weights
# TODO(b/125916026): Consider exposing a __call__ method with named args.
def call(self,
inputs,
mask=None,
training=None,
return_attention_scores=False):
Reported by Pylint.
Line: 136
Column: 3
return tf.matmul(weights, value), weights
# TODO(b/125916026): Consider exposing a __call__ method with named args.
def call(self,
inputs,
mask=None,
training=None,
return_attention_scores=False):
self._validate_call_args(inputs=inputs, mask=mask)
Reported by Pylint.
Line: 324
Column: 7
def build(self, input_shape):
"""Creates scale variable if use_scale==True."""
if self.use_scale:
self.scale = self.add_weight(
name='scale',
shape=(),
initializer='ones',
dtype=self.dtype,
trainable=True)
Reported by Pylint.
Line: 331
Column: 7
dtype=self.dtype,
trainable=True)
else:
self.scale = None
super(Attention, self).build(input_shape)
def _calculate_scores(self, query, key):
"""Calculates attention scores as a query-key dot product.
Reported by Pylint.
Line: 467
Column: 7
dim = v_shape[-1]
dim = tf.compat.dimension_value(dim)
if self.use_scale:
self.scale = self.add_weight(
name='scale',
shape=[dim],
initializer='glorot_uniform',
dtype=self.dtype,
trainable=True)
Reported by Pylint.
Line: 474
Column: 7
dtype=self.dtype,
trainable=True)
else:
self.scale = None
super(AdditiveAttention, self).build(input_shape)
def _calculate_scores(self, query, key):
"""Calculates attention scores as a nonlinear sum of query and key.
Reported by Pylint.
keras/benchmarks/keras_examples_benchmarks/mnist_conv_custom_training_benchmark_test.py
152 issues
Line: 20
Column: 1
from __future__ import division
from __future__ import print_function
import tensorflow as tf
import timeit
import numpy as np
from keras.benchmarks import benchmark_util
Reported by Pylint.
Line: 25
Column: 1
import timeit
import numpy as np
from keras.benchmarks import benchmark_util
from keras.benchmarks import distribution_util
class CustomMnistBenchmark(tf.test.Benchmark):
"""Benchmarks for custom training loop using `tf.test.Benchmark`."""
Reported by Pylint.
Line: 26
Column: 1
import numpy as np
from keras.benchmarks import benchmark_util
from keras.benchmarks import distribution_util
class CustomMnistBenchmark(tf.test.Benchmark):
"""Benchmarks for custom training loop using `tf.test.Benchmark`."""
Reported by Pylint.
Line: 22
Column: 1
import tensorflow as tf
import timeit
import numpy as np
from keras.benchmarks import benchmark_util
from keras.benchmarks import distribution_util
Reported by Pylint.
Line: 30
Column: 1
class CustomMnistBenchmark(tf.test.Benchmark):
"""Benchmarks for custom training loop using `tf.test.Benchmark`."""
def __init__(self):
super(CustomMnistBenchmark, self).__init__()
self.num_classes = 10
self.input_shape = (28, 28, 1)
Reported by Pylint.
Line: 32
Column: 1
class CustomMnistBenchmark(tf.test.Benchmark):
"""Benchmarks for custom training loop using `tf.test.Benchmark`."""
def __init__(self):
super(CustomMnistBenchmark, self).__init__()
self.num_classes = 10
self.input_shape = (28, 28, 1)
self.epochs = 15
(x_train, y_train), _ = tf.keras.datasets.mnist.load_data()
Reported by Pylint.
Line: 33
Column: 5
"""Benchmarks for custom training loop using `tf.test.Benchmark`."""
def __init__(self):
super(CustomMnistBenchmark, self).__init__()
self.num_classes = 10
self.input_shape = (28, 28, 1)
self.epochs = 15
(x_train, y_train), _ = tf.keras.datasets.mnist.load_data()
x_train = x_train.astype('float32') / 255
Reported by Pylint.
Line: 33
Column: 1
"""Benchmarks for custom training loop using `tf.test.Benchmark`."""
def __init__(self):
super(CustomMnistBenchmark, self).__init__()
self.num_classes = 10
self.input_shape = (28, 28, 1)
self.epochs = 15
(x_train, y_train), _ = tf.keras.datasets.mnist.load_data()
x_train = x_train.astype('float32') / 255
Reported by Pylint.
Line: 34
Column: 1
def __init__(self):
super(CustomMnistBenchmark, self).__init__()
self.num_classes = 10
self.input_shape = (28, 28, 1)
self.epochs = 15
(x_train, y_train), _ = tf.keras.datasets.mnist.load_data()
x_train = x_train.astype('float32') / 255
x_train = np.expand_dims(x_train, -1)
Reported by Pylint.
Line: 35
Column: 1
def __init__(self):
super(CustomMnistBenchmark, self).__init__()
self.num_classes = 10
self.input_shape = (28, 28, 1)
self.epochs = 15
(x_train, y_train), _ = tf.keras.datasets.mnist.load_data()
x_train = x_train.astype('float32') / 255
x_train = np.expand_dims(x_train, -1)
y_train = tf.keras.utils.to_categorical(y_train, self.num_classes)
Reported by Pylint.
keras/distribute/keras_metrics_test.py
151 issues
Line: 17
Column: 1
# ==============================================================================
"""Tests for Keras metrics."""
from absl.testing import parameterized
from keras import metrics
from keras.engine import base_layer
import tensorflow.compat.v2 as tf
combinations = tf.__internal__.distribute.combinations
Reported by Pylint.
Line: 20
Column: 1
from absl.testing import parameterized
from keras import metrics
from keras.engine import base_layer
import tensorflow.compat.v2 as tf
combinations = tf.__internal__.distribute.combinations
def _labeled_dataset_fn():
Reported by Pylint.
Line: 156
Column: 7
else:
self.sum_var = tf.Variable(1.0)
def call(self, inputs):
self.add_metric(self.sum(inputs))
self.add_metric(
tf.reduce_mean(inputs), name="mean", aggregation="mean")
self.sum_var.assign(self.sum.result())
return inputs
Reported by Pylint.
Line: 176
Column: 8
def run():
return distribution.run(func)
if distribution._should_use_with_coordinator:
coord = tf.distribute.experimental.coordinator.ClusterCoordinator(
distribution)
coord.schedule(run)
coord.join()
else:
Reported by Pylint.
Line: 31
Column: 1
# 4: 4, 1 -> False; 5: 0, 2 -> False; 6: 1, 0 -> False; 7: 2, 1 -> False
# 8: 3, 2 -> False; 9: 4, 0 -> False; 10: 0, 1 -> False; 11: 1, 2 -> False
# 12: 2, 0 -> False; 13: 3, 1 -> False; 14: 4, 2 -> False; 15: 0, 0 -> True
return tf.data.Dataset.range(1000).map(
lambda x: {"labels": x % 5, "predictions": x % 3}).batch(
4, drop_remainder=True)
def _boolean_dataset_fn():
Reported by Pylint.
Line: 43
Column: 1
# F, F -> TN; T, T -> TP; F, T -> FP
# T, F -> FN; F, F -> TN; T, T -> TP
# F, T -> FP; T, F -> FN; F, F -> TN
return tf.data.Dataset.from_tensor_slices({
"labels": [True, False, True, False],
"predictions": [True, True, False, False]}).repeat().batch(
3, drop_remainder=True)
Reported by Pylint.
Line: 56
Column: 1
# False, 0.0 -> TN; True, 1.0 -> TP; False, .75 -> FP
# True, .25 -> FN; False, 0.0 -> TN; True, 1.0 -> TP
# False, .75 -> FP; True, .25 -> FN; False, 0.0 -> TN
return tf.data.Dataset.from_tensor_slices({
"labels": [True, False, True, False],
"predictions": [1.0, 0.75, 0.25, 0.]}).repeat().batch(
3, drop_remainder=True)
Reported by Pylint.
Line: 63
Column: 1
def _regression_dataset_fn():
return tf.data.Dataset.from_tensor_slices({
"labels": [1., .5, 1., 0.],
"predictions": [1., .75, .25, 0.]}).repeat()
def all_combinations():
Reported by Pylint.
Line: 68
Column: 1
"predictions": [1., .75, .25, 0.]}).repeat()
def all_combinations():
return tf.__internal__.test.combinations.combine(
distribution=[
combinations.default_strategy, combinations.one_device_strategy,
combinations.mirrored_strategy_with_gpu_and_cpu,
combinations.mirrored_strategy_with_two_gpus
Reported by Pylint.
Line: 69
Column: 1
def all_combinations():
return tf.__internal__.test.combinations.combine(
distribution=[
combinations.default_strategy, combinations.one_device_strategy,
combinations.mirrored_strategy_with_gpu_and_cpu,
combinations.mirrored_strategy_with_two_gpus
],
Reported by Pylint.
keras/mixed_precision/policy.py
151 issues
Line: 17
Column: 1
# ==============================================================================
"""Contains the Policy class for mixed precision training."""
import tensorflow.compat.v2 as tf
import contextlib
from keras import backend
from keras.engine import base_layer_utils
from keras.mixed_precision import device_compatibility_check
Reported by Pylint.
Line: 25
Column: 1
from keras.mixed_precision import device_compatibility_check
from keras.mixed_precision import loss_scale as keras_loss_scale_module
from keras.utils import generic_utils
from tensorflow.python.platform import tf_logging
from tensorflow.python.util.tf_export import keras_export
# pylint: disable=g-classes-have-attributes
@keras_export('keras.mixed_precision.Policy', v1=[])
Reported by Pylint.
Line: 26
Column: 1
from keras.mixed_precision import loss_scale as keras_loss_scale_module
from keras.utils import generic_utils
from tensorflow.python.platform import tf_logging
from tensorflow.python.util.tf_export import keras_export
# pylint: disable=g-classes-have-attributes
@keras_export('keras.mixed_precision.Policy', v1=[])
class Policy:
Reported by Pylint.
Line: 29
Column: 1
from tensorflow.python.util.tf_export import keras_export
# pylint: disable=g-classes-have-attributes
@keras_export('keras.mixed_precision.Policy', v1=[])
class Policy:
"""A dtype policy for a Keras layer.
A dtype policy determines a layer's computation and variable dtypes. Each
Reported by Pylint.
Line: 242
Column: 7
error = ("Cannot convert value %s to a mixed precision Policy. "
"Valid policies include 'mixed_float16', 'mixed_bfloat16', "
"and the name of any dtype such as 'float32'." % (name,))
raise ValueError(error)
return dtype, dtype
@property
def variable_dtype(self):
"""The variable dtype of this policy.
Reported by Pylint.
Line: 407
Column: 3
# The current global policy in effect. If None, it means the current value of
# floatx should be used as the policy if the V2 dtype behavior is enabled,
# or "_infer" otherwise.
# TODO(reedwm): Make this thread local?
_global_policy = None
@keras_export('keras.mixed_precision.global_policy',
'keras.mixed_precision.experimental.global_policy', v1=[])
Reported by Pylint.
Line: 498
Column: 3
be None, in which case the global policy will be constructed from
`tf.keras.backend.floatx()`
"""
global _global_policy
if not base_layer_utils.v2_dtype_behavior_enabled():
raise ValueError('The global policy can only be set in TensorFlow 2 or if '
'V2 dtype behavior has been set. To enable V2 dtype '
'behavior, call '
'"tf.compat.v1.keras.layers.enable_v2_dtype_behavior()"')
Reported by Pylint.
Line: 520
Column: 3
tf.__internal__.train.set_using_mixed_precision_policy(is_mixed_policy)
# TODO(reedwm): Make this thread local
@contextlib.contextmanager
def policy_scope(policy):
"""A context manager that sets the global Policy under it.
Args:
Reported by Pylint.
Line: 19
Column: 1
import tensorflow.compat.v2 as tf
import contextlib
from keras import backend
from keras.engine import base_layer_utils
from keras.mixed_precision import device_compatibility_check
from keras.mixed_precision import loss_scale as keras_loss_scale_module
from keras.utils import generic_utils
Reported by Pylint.
Line: 32
Column: 1
# pylint: disable=g-classes-have-attributes
@keras_export('keras.mixed_precision.Policy', v1=[])
class Policy:
"""A dtype policy for a Keras layer.
A dtype policy determines a layer's computation and variable dtypes. Each
layer has a policy. Policies can be passed to the `dtype` argument of layer
constructors, or a global policy can be set with
`tf.keras.mixed_precision.set_global_policy`.
Reported by Pylint.
keras/engine/base_preprocessing_layer_test.py
150 issues
Line: 24
Column: 1
from keras import testing_utils
from keras.engine import base_preprocessing_layer
import numpy as np
import tensorflow.compat.v2 as tf
# Define a test-only implementation of BasePreprocessingLayer to validate
# its correctness directly.
class AddingPreprocessingLayer(base_preprocessing_layer.PreprocessingLayer):
Reported by Pylint.
Line: 33
Column: 5
def build(self, input_shape):
super(AddingPreprocessingLayer, self).build(input_shape)
self.sum = tf.Variable(0., dtype=tf.float32)
def update_state(self, data):
self.sum.assign_add(tf.reduce_sum(tf.cast(data, tf.float32)))
def reset_state(self): # pylint: disable=method-hidden
Reported by Pylint.
Line: 49
Column: 3
"""
self.sum.assign(sum_value)
def call(self, inputs):
return inputs + self.sum
@keras_parameterized.run_all_keras_modes(always_skip_v1=True)
class PreprocessingLayerTest(keras_parameterized.TestCase):
Reported by Pylint.
Line: 88
Column: 5
layer = AddingPreprocessingLayer()
output = layer(input_data)
model = keras.Model(input_data, output)
model._run_eagerly = testing_utils.should_run_eagerly()
layer.set_total(15)
self.assertAllEqual([[16], [17], [18]], model.predict([1., 2., 3.]))
Reported by Pylint.
Line: 104
Column: 5
input_data = keras.Input(shape=(1,))
output = layer(input_data)
model = keras.Model(input_data, output)
model._run_eagerly = testing_utils.should_run_eagerly()
self.assertAllEqual([[16], [17], [18]], model.predict([1., 2., 3.]))
def test_post_build_adapt_update_numpy(self):
"""Test that preproc layers can adapt() after build() is called."""
Reported by Pylint.
Line: 116
Column: 5
layer = AddingPreprocessingLayer()
output = layer(input_data)
model = keras.Model(input_data, output)
model._run_eagerly = testing_utils.should_run_eagerly()
layer.adapt(input_dataset)
self.assertAllEqual([[16], [17], [18]], model.predict([1., 2., 3.]))
Reported by Pylint.
Line: 133
Column: 5
input_data = keras.Input(shape=(1,))
output = layer(input_data)
model = keras.Model(input_data, output)
model._run_eagerly = testing_utils.should_run_eagerly()
self.assertAllEqual([[16], [17], [18]], model.predict([1., 2., 3.]))
def test_post_build_adapt_update_dataset(self):
"""Test that preproc layers can adapt() after build() is called."""
Reported by Pylint.
Line: 146
Column: 5
layer = AddingPreprocessingLayer()
output = layer(input_data)
model = keras.Model(input_data, output)
model._run_eagerly = testing_utils.should_run_eagerly()
layer.adapt(input_dataset)
self.assertAllEqual([[16], [17], [18]], model.predict([1., 2., 3.]))
Reported by Pylint.
Line: 160
Column: 7
layer = AddingPreprocessingLayer()
output = layer(input_data)
model = keras.Model(input_data, output)
model._run_eagerly = testing_utils.should_run_eagerly()
return (model, layer)
input_dataset = np.array([1, 2, 3, 4, 5])
model, layer = get_model()
layer.adapt(input_dataset)
Reported by Pylint.
Line: 29
Column: 1
# Define a test-only implementation of BasePreprocessingLayer to validate
# its correctness directly.
class AddingPreprocessingLayer(base_preprocessing_layer.PreprocessingLayer):
def build(self, input_shape):
super(AddingPreprocessingLayer, self).build(input_shape)
self.sum = tf.Variable(0., dtype=tf.float32)
Reported by Pylint.