<commit_before>/*******************************************************
 * Copyright (c) 2015, ArrayFire
 * All rights reserved.
 *
 * This file is distributed under 3-clause BSD license.
 * The complete license agreement can be obtained at:
 * http://arrayfire.com/licenses/BSD-3-Clause
 ********************************************************/

#include <af/autograd.h>
#include <af/nn.h>

using namespace af;
using namespace af::nn;
using namespace af::autograd;

int main()
{
    const int inputSize  = 2;
    const int outputSize = 1;
    const int numSamples = 4;
    const double lr = 0.005;

    float hInput[] = {1, 1,
                      0, 0,
                      1, 0,
                      0, 1};

    float hOutput[] = {1,
                       0,
                       1,
                       1};

    auto in = af::array(inputSize, numSamples, hInput);
    auto out = af::array(outputSize, numSamples, hOutput);

    nn::Sequential perceptron;

    perceptron.add(nn::Linear(inputSize, outputSize));
    perceptron.add(nn::Sigmoid());

    Variable result;
    for (int i = 0; i < 10; i++) {

        // Forward propagation
        result = perceptron.forward(nn::input(in));

        // Calculate loss
        // TODO: Use loss function
        af::array diff = out - result.array();
        printf("Error at iteration(%d) : %lf\n", i + 1, af::max<float>(af::abs(diff)));

        // Backward propagation
        auto d_result = Variable(diff, false);
        result.backward(d_result);

        // Update parameters
        // TODO: Should use optimizer
        for (auto param : perceptron.parameters()) {
            param.array() += lr * param.grad().array();
            param.array().eval();
        }
    }
    af_print(result.array());
    return 0;
}<commit_msg>Fixing perceptron example to use smaller batch size<commit_after>/*******************************************************
 * Copyright (c) 2015, ArrayFire
 * All rights reserved.
 *
 * This file is distributed under 3-clause BSD license.
 * The complete license agreement can be obtained at:
 * http://arrayfire.com/licenses/BSD-3-Clause
 ********************************************************/

#include <af/autograd.h>
#include <af/nn.h>

using namespace af;
using namespace af::nn;
using namespace af::autograd;

int main()
{
    const int inputSize  = 2;
    const int outputSize = 1;
    const double lr = 0.005;
    const int numSamples = 4;

    float hInput[] = {1, 1,
                      0, 0,
                      1, 0,
                      0, 1};

    float hOutput[] = {1,
                       0,
                       1,
                       1};

    auto in = af::array(inputSize, numSamples, hInput);
    auto out = af::array(outputSize, numSamples, hOutput);

    nn::Sequential perceptron;

    perceptron.add(nn::Linear(inputSize, outputSize));
    perceptron.add(nn::Sigmoid());

    Variable result;
    for (int i = 0; i < 100; i++) {
        for (int j = 0; j < numSamples; j++) {

            af::array in_j = in(af::span, j);
            af::array out_j = out(af::span, j);

            // Forward propagation
            result = perceptron.forward(nn::input(in_j));

            // Calculate loss
            // TODO: Use loss function
            af::array diff = out_j - result.array();

            // Backward propagation
            auto d_result = Variable(diff, false);
            result.backward(d_result);

            // Update parameters
            // TODO: Should use optimizer
            for (auto param : perceptron.parameters()) {
                param.array() += lr * param.grad().array();
                param.array().eval();
            }
        }

        if ((i + 1) % 10 == 0) {
            // Forward propagation
            result = perceptron.forward(nn::input(in));

            // Calculate loss
            // TODO: Use loss function
            af::array diff = out - result.array();
            printf("Average Error at iteration(%d) : %lf\n", i + 1, af::mean<float>(af::abs(diff)));
            printf("Predicted\n");
            af_print(result.array());
            printf("Expected\n");
            af_print(out);
            printf("\n\n");
        }
    }
    return 0;
}
{'fraction_non_alphanumeric': 0.17766258246936853, 'fraction_numerical': 0.015551366635249765, 'mean_word_length': 1.9275862068965517, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 9, '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': '7425142', 'n_tokens_mistral': 1299, 'n_tokens_neox': 1171, 'n_words': 328}