<commit_before>/*
 * This file is part of the CN24 semantic segmentation software,
 * copyright (C) 2015 <NAME> (ikosa dot de at gmail dot com).
 *
 * For licensing information, see the LICENSE file included with this project.
 */  
/**
 * @file Layer.h
 * @class Layer
 * @brief Abstract class representing a layer in the neural network.
 *
 * @author <NAME> (ikosa dot de at gmail dot com)
 *
 */

#ifndef CONV_LAYER_H
#define CONV_LAYER_H

#include <string>
#include <vector>

#include "../util/Tensor.h"
#include "../util/CombinedTensor.h"

namespace Conv {

  class NetStatus;
  class Trainer;
  class GradientTester;
	class NetGraphBuffer;
class Layer {
  friend class Trainer;
  friend class GradientTester;
public:
  virtual ~Layer() {};
  // Layer() : configuration_("") {};
  explicit Layer(std::string configuration) : configuration_(configuration) {};
  /**
   * @brief Creates a CombinedTensor vector given an input.
   *
   * Note that this does not connect the LayerOutputs to this layer.
   * You need to call Connect for this.
   *
   * @param inputs The inputs to the layer
   * @returns True on success, false for incompatible inputs
   */
  virtual bool CreateOutputs (const std::vector<CombinedTensor*>& inputs,
                              std::vector<CombinedTensor*>& outputs) = 0;

  /**
   * @brief Connects this Layer to the inputs and outputs.
   *
   * @param inputs The inputs to the layer
   * @param outputs The outputs to the layer
   */
  virtual bool Connect (const std::vector<CombinedTensor*>& inputs,
                        const std::vector<CombinedTensor*>& outputs,
                        const NetStatus* status) = 0;
                        
  /**
   * @brief Performs a forward pass
   */
  virtual void FeedForward() = 0;

  /**
   * @brief Performs a backward pass
   */
  virtual void BackPropagate() = 0;

  /**
   * @brief Returns a reference to the parameters.
   */
  inline const std::vector<CombinedTensor*>& parameters() const {
    return parameters_;
  }

  /**
   * @brief Sets the Layer's local learning rate.
   */
  inline void SetLocalLearningRate (const datum local_lr) {
    LOGDEBUG << "Setting local learning rate to " << local_lr;
    local_lr_ = local_lr;
  }

  /**
   * @brief Enables or disables backpropagation.
   */
  inline void SetBackpropagationEnabled (const bool backprop_enabled) {
    backprop_enabled_ = backprop_enabled;
  }

  /**
   * @brief This is called by the net when this layer has a child layer.
   */
  virtual void OnLayerConnect (const std::vector<Layer*> next_layers) {
		gain = 0;
		for (Layer* next_layer : next_layers)
			gain += next_layer->Gain();
  }

  /**
   * @brief Returns the layer's gain
   */
  virtual unsigned int Gain() { return gain; }

  /**
   * @brief Returns true if the layer is OpenCL aware
   * 
   * If this returns false, the net will make sure that any
   * input and output tensors are in the CPU's memory and not
   * the GPU's.
   */
  virtual bool IsOpenCLAware() { return false; }

  virtual std::string GetLayerConfiguration() { return configuration_; }<commit_msg>Layer: Added IsNotGradientSafe method to skip layers during gradient check (default: don't skip)<commit_after>/*
 * This file is part of the CN24 semantic segmentation software,
 * copyright (C) 2015 <NAME> (ikosa dot de at gmail dot com).
 *
 * For licensing information, see the LICENSE file included with this project.
 */  
/**
 * @file Layer.h
 * @class Layer
 * @brief Abstract class representing a layer in the neural network.
 *
 * @author <NAME> (ikosa dot de at gmail dot com)
 *
 */

#ifndef CONV_LAYER_H
#define CONV_LAYER_H

#include <string>
#include <vector>

#include "../util/Tensor.h"
#include "../util/CombinedTensor.h"

namespace Conv {

  class NetStatus;
  class Trainer;
  class GradientTester;
	class NetGraphBuffer;
class Layer {
  friend class Trainer;
  friend class GradientTester;
public:
  virtual ~Layer() {};
  // Layer() : configuration_("") {};
  explicit Layer(std::string configuration) : configuration_(configuration) {};
  /**
   * @brief Creates a CombinedTensor vector given an input.
   *
   * Note that this does not connect the LayerOutputs to this layer.
   * You need to call Connect for this.
   *
   * @param inputs The inputs to the layer
   * @returns True on success, false for incompatible inputs
   */
  virtual bool CreateOutputs (const std::vector<CombinedTensor*>& inputs,
                              std::vector<CombinedTensor*>& outputs) = 0;

  /**
   * @brief Connects this Layer to the inputs and outputs.
   *
   * @param inputs The inputs to the layer
   * @param outputs The outputs to the layer
   */
  virtual bool Connect (const std::vector<CombinedTensor*>& inputs,
                        const std::vector<CombinedTensor*>& outputs,
                        const NetStatus* status) = 0;
                        
  /**
   * @brief Performs a forward pass
   */
  virtual void FeedForward() = 0;

  /**
   * @brief Performs a backward pass
   */
  virtual void BackPropagate() = 0;

  /**
   * @brief Returns a reference to the parameters.
   */
  inline const std::vector<CombinedTensor*>& parameters() const {
    return parameters_;
  }

  /**
   * @brief Sets the Layer's local learning rate.
   */
  inline void SetLocalLearningRate (const datum local_lr) {
    LOGDEBUG << "Setting local learning rate to " << local_lr;
    local_lr_ = local_lr;
  }

  /**
   * @brief Enables or disables backpropagation.
   */
  inline void SetBackpropagationEnabled (const bool backprop_enabled) {
    backprop_enabled_ = backprop_enabled;
  }

  /**
   * @brief This is called by the net when this layer has a child layer.
   */
  virtual void OnLayerConnect (const std::vector<Layer*> next_layers) {
		gain = 0;
		for (Layer* next_layer : next_layers)
			gain += next_layer->Gain();
  }

  /**
   * @brief Returns the layer's gain
   */
  virtual unsigned int Gain() { return gain; }

  /**
   * @brief Returns true if the layer is OpenCL aware
   * 
   * If this returns false, the net will make sure that any
   * input and output tensors are in the CPU's memory and not
   * the GPU's.
   */
  virtual bool IsOpenCLAware() { return false; }
  
  /**
   * @brief Returns true if the layer should be ignored during gradient checks
   */
  virtual bool IsNotGradientSafe() { return false; }

  virtual std::string GetLayerConfiguration() { return configuration_; }
{'fraction_non_alphanumeric': 0.10165006226650063, 'fraction_numerical': 0.003424657534246575, 'mean_word_length': 2.941717791411043, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 25, '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': '322787', 'n_tokens_mistral': 1933, 'n_tokens_neox': 1825, 'n_words': 689}