<commit_before># Lint as: python3
# Copyright 2018 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# 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.
# ==============================================================================
"""Image classification models."""

import lingvo.compat as tf
from lingvo.core import base_model
from lingvo.core import layers
from lingvo.core import metrics
from lingvo.core import plot
from lingvo.core import py_utils
from lingvo.core import schedule
import numpy as np


class BaseClassifier(base_model.BaseTask):
  """Base class for image classifier."""

  @classmethod
  def Params(cls):
    p = super().Params()
    p.Define('softmax', layers.SimpleFullSoftmax.Params(), 'Softmax layer.')
    p.Define('add_image_summary', True, 'If True, adds image summary in '
             'evaler/decoder jobs.')
    return p

  def _AddSummary(self, batch, prediction):
    """Adds image summaries for the batch."""
    p = self.params
    if not self.do_eval or not p.add_image_summary:
      # Image summaries only works in evaler/decoder.
      return

    def Draw(fig, axes, img, label, pred):
      plot.AddImage(
          fig=fig,
          axes=axes,
          data=img[:, :, 0] / 256.,
          show_colorbar=False,
          suppress_xticks=True,
          suppress_yticks=True)
      axes.text(
          x=0.5,
          y=0,
          s=u'%d vs. %d' % (label, pred),
          transform=axes.transAxes,
          horizontalalignment='center')

    with plot.MatplotlibFigureSummary(
        'examples', figsize=(1, 1), max_outputs=10) as fig:
      fig.AddSubplot([batch.raw, batch.label, prediction], Draw)

  def _Accuracy(self, k, logits, labels, weights):
    """Compute top-k accuracy.

    Args:
      k: An int scalar. Top-k.
      logits: A [N, C] float tensor.
      labels: A [N] int vector.
      weights: A [N] float vector.

    Returns:
      A float scalar. The accuracy at precision k.
    """
    logits = py_utils.HasRank(logits, 2)
    n, _ = tf.unstack(tf.shape(logits), 2)
    labels = py_utils.HasShape(labels, [n])
    weights = py_utils.HasShape(weights, [n])
    correct = tf.nn.in_top_k(targets=labels, predictions=logits, k=k)
    return tf.reduce_sum(tf.cast(correct, weights.dtype) *
                         weights) / tf.maximum(1e-8, tf.reduce_sum(weights))


class ModelV1(BaseClassifier):
  """CNNs with maxpooling followed by a softmax."""

  @classmethod
  def Params(cls):<commit_msg>Move _Accuracy function in classifier.py model into standalone fn.<commit_after># Lint as: python3
# Copyright 2018 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# 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.
# ==============================================================================
"""Image classification models."""

import lingvo.compat as tf
from lingvo.core import base_model
from lingvo.core import layers
from lingvo.core import metrics
from lingvo.core import plot
from lingvo.core import py_utils
from lingvo.core import schedule
import numpy as np


def TopKAccuracy(k, logits, labels, weights):
  """Compute top-k accuracy.

  Args:
    k: An int scalar. Top-k.
    logits: A [N, C] float tensor.
    labels: A [N] int vector.
    weights: A [N] float vector.

  Returns:
    A float scalar. The accuracy at precision k.
  """
  logits = py_utils.HasRank(logits, 2)
  n, _ = tf.unstack(tf.shape(logits), 2)
  labels = py_utils.HasShape(labels, [n])
  weights = py_utils.HasShape(weights, [n])
  correct = tf.nn.in_top_k(targets=labels, predictions=logits, k=k)
  return tf.reduce_sum(tf.cast(correct, weights.dtype) * weights) / tf.maximum(
      1e-8, tf.reduce_sum(weights))


class BaseClassifier(base_model.BaseTask):
  """Base class for image classifier."""

  @classmethod
  def Params(cls):
    p = super().Params()
    p.Define('softmax', layers.SimpleFullSoftmax.Params(), 'Softmax layer.')
    p.Define('add_image_summary', True, 'If True, adds image summary in '
             'evaler/decoder jobs.')
    return p

  def _AddSummary(self, batch, prediction):
    """Adds image summaries for the batch."""
    p = self.params
    if not self.do_eval or not p.add_image_summary:
      # Image summaries only works in evaler/decoder.
      return

    def Draw(fig, axes, img, label, pred):
      plot.AddImage(
          fig=fig,
          axes=axes,
          data=img[:, :, 0] / 256.,
          show_colorbar=False,
          suppress_xticks=True,
          suppress_yticks=True)
      axes.text(
          x=0.5,
          y=0,
          s=u'%d vs. %d' % (label, pred),
          transform=axes.transAxes,
          horizontalalignment='center')

    with plot.MatplotlibFigureSummary(
        'examples', figsize=(1, 1), max_outputs=10) as fig:
      fig.AddSubplot([batch.raw, batch.label, prediction], Draw)

  def _Accuracy(self, k, logits, labels, weights):
    """Compute top-k accuracy.

    Args:
      k: An int scalar. Top-k.
      logits: A [N, C] float tensor.
      labels: A [N] int vector.
      weights: A [N] float vector.

    Returns:
      A float scalar. The accuracy at precision k.
    """
    return TopKAccuracy(k, logits, labels, weights)


class ModelV1(BaseClassifier):
  """CNNs with maxpooling followed by a softmax."""

  @classmethod
  def Params(cls):
{'fraction_non_alphanumeric': 0.13084410403701932, 'fraction_numerical': 0.007978299026647519, 'mean_word_length': 3.156498673740053, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1188743', 'n_tokens_mistral': 1956, 'n_tokens_neox': 1867, 'n_words': 630}