123 lines
4.5 KiB
C++
123 lines
4.5 KiB
C++
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
*
|
|
* PANIC
|
|
* Portable Algorithms and Numerics In C++
|
|
*
|
|
* Scientific computing from scratch, with feeling.
|
|
*
|
|
* Copyright (c) 2026 Michelle Bausager
|
|
*
|
|
* This file is part of PANIC.
|
|
*
|
|
* PANIC is free software licensed under the GNU General Public License v3.0 or later.
|
|
* You may redistribute and/or modify it under the terms of the GPL.
|
|
*
|
|
* PANIC is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
* See the LICENSE file for the full license text.
|
|
*
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
|
*
|
|
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
*
|
|
* Project Name: PANIC
|
|
* Module Name: neural_network
|
|
* File Name: spiral_data.cpp
|
|
* Revision: 0.1.0
|
|
* Date: 29-07-2026
|
|
* Author: Michelle Bausager
|
|
*
|
|
* Description:
|
|
* Functions to generate dataset for neural networks;
|
|
*
|
|
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
|
|
#include <neural_network/datasets/spiral_data.hpp>
|
|
|
|
#include <tensor/vector.hpp>
|
|
#include <tensor/matrix.hpp>
|
|
#include <config/types.hpp>
|
|
#include <config/omp.hpp>
|
|
|
|
|
|
#include <math/trigonometry/sin.hpp>
|
|
#include <math/trigonometry/cos.hpp>
|
|
#include <math/add.hpp>
|
|
#include <random/uniform.hpp>
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------------
|
|
// PRIVATE CONSTANTS
|
|
//---------------------------------------------------------------------------------------------------------------------------
|
|
/**
|
|
* @brief Minimum number of element operations before using the OpenMP-enabled loop.
|
|
*
|
|
* Small vectors and matrices are kept serial because the overhead of starting
|
|
* worker threads can be larger than the work itself.
|
|
*/
|
|
static const panic::types::uint_t spiral_data_omp_min_work = 500;
|
|
//---------------------------------------------------------------------------------------------------------------------------
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------------
|
|
// INPLEMENTATION
|
|
//---------------------------------------------------------------------------------------------------------------------------
|
|
|
|
namespace panic {
|
|
namespace neural_network {
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------------------------------
|
|
// Function Name : panic::neural_network::spiral_data
|
|
//
|
|
// Description:
|
|
// Generates dataset with spiral data
|
|
//--------------------------------------------------------------------------------------------------------------------------
|
|
template <typename T>
|
|
bool spiral_data(const panic::types::uint_t samples, const panic::types::uint_t classes, panic::tensor::matrix<T>& X, panic::tensor::uint_vector& y){
|
|
|
|
|
|
if ( !X.resize(samples*classes, 2) || !y.resize(samples*classes) ){
|
|
return false;
|
|
}
|
|
|
|
PANIC_OMP_PARALLEL_FOR_IF(samples*classes > spiral_data_omp_min_work)
|
|
for (panic::types::uint_t i = 0; i < classes; ++i){
|
|
for (panic::types::uint_t j = 0; j < samples; ++j){
|
|
|
|
const T radius = static_cast<T>(j)/static_cast<T>(samples-1);
|
|
|
|
const T angle = static_cast<T>(i)*T{4} + (T{4}*radius);
|
|
|
|
const panic::types::uint_t row_index = (i*samples) + j;
|
|
|
|
X(row_index, 0) = radius*panic::math::cos(angle*T{2.5});
|
|
X(row_index, 1) = radius*panic::math::sin(angle*T{2.5});
|
|
|
|
y[row_index] = i;
|
|
}
|
|
}
|
|
|
|
panic::tensor::matrix<T> random_matrix(samples*classes, 2);
|
|
panic::random::uniform(random_matrix, T{-0.0015}, T{0.0015});
|
|
|
|
if (!panic::math::add(X, random_matrix, X)){
|
|
return false;
|
|
}
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
//--------------------------------------------------------------------------------------------------------------------------
|
|
// EXPLICIT TEMPLATE INSTANTIATION
|
|
//
|
|
// The implementation is in this .cpp file.
|
|
// Build the overload for the official PANIC numeric types.
|
|
//--------------------------------------------------------------------------------------------------------------------------
|
|
template bool spiral_data<panic::types::real_t>(const panic::types::uint_t samples,
|
|
const panic::types::uint_t classes,
|
|
panic::tensor::matrix<panic::types::real_t>& X,
|
|
panic::tensor::uint_vector& y
|
|
);
|
|
|
|
|
|
} // namespace neural_network
|
|
} // namespace panic
|