<commit_before>// Copyright (C) 2015 <NAME> <<EMAIL>>

#ifndef LOGISTIC_H
#define LOGISTIC_H

#include <cmath>

template<typename T>
struct Logistic {
  inline static T f(T x) {
    return (T)1 / ((T)1 + exp(-x));
  }
  inline static T df(T y) {
    return y * ((T)1 - y);
  }
};
<commit_msg>Added batch functions for logistic<commit_after>// Copyright (C) 2015 <NAME> <<EMAIL>>

#ifndef LOGISTIC_H
#define LOGISTIC_H

#include <cmath>
#include <array>

template<typename T>
struct Logistic;

template<typename T>
struct Logistic {
  /* One at a time */
  inline static T f(T x) {
    return (T)1 / ((T)1 + exp(-x));
  }
  inline static T df(T y) {
    return y * ((T)1 - y);
  }

  /* All neurons on a layer */
  template<typename LayerSize>
  using Neurons = std::array<T, LayerSize::length>;

  template<typename LayerSize>
  inline static void
  f_layer(const Neurons<LayerSize>& Z, Neurons<LayerSize>& A) {
    for (size_t j = 0; j < LayerSize::length; j++) {
      A[j] = f(Z[j]);
    }
  }

  template<typename LayerSize>
  inline static void
  df_layer(const Neurons<LayerSize>& A, Neurons<LayerSize>& Err){
    for (size_t j = 0; j < LayerSize::length; j++) {
      Err[j] *= A[j] * ((T)1 - A[j]);
    }
  }

  /* All neurons in a batch */
  template<typename LayerSize, size_t batch_size>
  using Batch = std::array<Neurons<LayerSize>, batch_size>;

  template<typename LayerSize, size_t batch_size>
  inline static void
  f_batch(const Batch<LayerSize, batch_size>& Z,
          Batch<LayerSize, batch_size>& A) {
    constexpr size_t full_length = LayerSize::length * batch_size;
    using LinearBatch = std::array<T, LayerSize::length * batch_size>;

    const LinearBatch& z = *reinterpret_cast<const LinearBatch*>(Z.data());
    LinearBatch& a = *reinterpret_cast<LinearBatch*>(A.data());

    for (size_t j = 0; j < full_length; j++)
      a[j] = f(z[j]);
  }

  template<typename LayerSize, size_t batch_size>
  inline static void
  df_batch(const Batch<LayerSize, batch_size>& A,
           Batch<LayerSize, batch_size>& Err) {
    constexpr size_t full_length = LayerSize::length * batch_size;
    using LinearBatch = std::array<T, LayerSize::length * batch_size>;

    const LinearBatch& a = *reinterpret_cast<const LinearBatch*>(A.data());
    LinearBatch& err = *reinterpret_cast<LinearBatch*>(Err.data());

    for (size_t j = 0; j < full_length; j++)
      err[j] *= a[j] * ((T)1 - a[j]);
  }
};

{'fraction_non_alphanumeric': 0.15, 'fraction_numerical': 0.008264462809917356, 'mean_word_length': 3.691860465116279, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6078693', 'n_tokens_mistral': 910, 'n_tokens_neox': 861, 'n_words': 230}