<commit_before>#include "cnn/nodes.h"
#include "cnn/cnn.h"
#include "cnn/training.h"
#include "cnn/gpu-ops.h"
#include "cnn/expr.h"
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>

#include <iostream>
#include <fstream>

using namespace std;
using namespace cnn;
using namespace cnn::expr;


// This is a sample class which implements the xor model from xor.cc
// Everything in this class is just as you would do the usual except for
// parts with provided comments.
class XORModel {
public:
  unsigned hidden_size;

  Expression W, b, V, a;
  Parameters *pW, *pb, *pV, *pa;

  // It is important to have a null default constructor for the class, as
  // we would first need to read the class object from the file, followed by
  // the cnn model which has saved parameters.
  XORModel() {}

  XORModel(const unsigned& hidden_len, Model *m) {
    hidden_size = hidden_len;
    InitParams(m);
  }

  void InitParams(Model *m) {
    pW = m->add_parameters({hidden_size, 2});
    pb = m->add_parameters({hidden_size});
    pV = m->add_parameters({1, hidden_size});
    pa = m->add_parameters({1});
  }

  void AddParamsToCG(ComputationGraph *cg) {
    W = parameter(*cg, pW);
    b = parameter(*cg, pb);
    V = parameter(*cg, pV);
    a = parameter(*cg, pa);
  }

  float Train(vector<cnn::real> &input, cnn::real &gold_output,
              SimpleSGDTrainer *sgd) {
    ComputationGraph cg;
    AddParamsToCG(&cg);

    Expression x = cnn::expr::input(cg, {input.size()}, &input);
    Expression y = cnn::expr::input(cg, &gold_output);

    Expression h = tanh(W*x + b);
    Expression y_pred = V*h + a;
    Expression loss = squared_distance(y_pred, y);
    float return_loss = as_scalar(cg.forward());
    cg.backward();
    sgd->update(1.0);
    return return_loss;
  }

  float Decode(vector<cnn::real> &input) {
    ComputationGraph cg;
    AddParamsToCG(&cg);

    Expression x = cnn::expr::input(cg, {input.size()}, &input);
    Expression h = tanh(W*x + b);
    Expression y_pred = V*h + a;
    return as_scalar(cg.forward());
  }

  // This function should save all those variables in the archive, which
  // determine the size of other members of the class, here: hidden_size
  friend class boost::serialization::access;
  template<class Archive> void serialize(Archive& ar, const unsigned int) {
<commit_msg>Fixed clang compile error wrt mismatched types<commit_after>#include "cnn/nodes.h"
#include "cnn/cnn.h"
#include "cnn/training.h"
#include "cnn/gpu-ops.h"
#include "cnn/expr.h"
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>

#include <iostream>
#include <fstream>

using namespace std;
using namespace cnn;
using namespace cnn::expr;


// This is a sample class which implements the xor model from xor.cc
// Everything in this class is just as you would do the usual except for
// parts with provided comments.
class XORModel {
public:
  unsigned hidden_size;

  Expression W, b, V, a;
  Parameters *pW, *pb, *pV, *pa;

  // It is important to have a null default constructor for the class, as
  // we would first need to read the class object from the file, followed by
  // the cnn model which has saved parameters.
  XORModel() {}

  XORModel(const unsigned& hidden_len, Model *m) {
    hidden_size = hidden_len;
    InitParams(m);
  }

  void InitParams(Model *m) {
    pW = m->add_parameters({hidden_size, 2});
    pb = m->add_parameters({hidden_size});
    pV = m->add_parameters({1, hidden_size});
    pa = m->add_parameters({1});
  }

  void AddParamsToCG(ComputationGraph *cg) {
    W = parameter(*cg, pW);
    b = parameter(*cg, pb);
    V = parameter(*cg, pV);
    a = parameter(*cg, pa);
  }

  float Train(vector<cnn::real> &input, cnn::real &gold_output,
              SimpleSGDTrainer *sgd) {
    ComputationGraph cg;
    AddParamsToCG(&cg);

    Expression x = cnn::expr::input(cg, {(long)input.size()}, &input);
    Expression y = cnn::expr::input(cg, &gold_output);

    Expression h = tanh(W*x + b);
    Expression y_pred = V*h + a;
    Expression loss = squared_distance(y_pred, y);
    float return_loss = as_scalar(cg.forward());
    cg.backward();
    sgd->update(1.0);
    return return_loss;
  }

  float Decode(vector<cnn::real> &input) {
    ComputationGraph cg;
    AddParamsToCG(&cg);

    Expression x = cnn::expr::input(cg, {(long)input.size()}, &input);
    Expression h = tanh(W*x + b);
    Expression y_pred = V*h + a;
    return as_scalar(cg.forward());
  }

  // This function should save all those variables in the archive, which
  // determine the size of other members of the class, here: hidden_size
  friend class boost::serialization::access;
  template<class Archive> void serialize(Archive& ar, const unsigned int) {

{'fraction_non_alphanumeric': 0.1320715036803365, 'fraction_numerical': 0.002103049421661409, 'mean_word_length': 3.6857142857142855, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 27, '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': '4082773', 'n_tokens_mistral': 1620, 'n_tokens_neox': 1584, 'n_words': 476}