2024-10-21 16:42:03 -04:00
|
|
|
#pragma once
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2024 Brett Terpstra
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef COSC_4P80_ASSIGNMENT_2_LAYER_H
|
|
|
|
#define COSC_4P80_ASSIGNMENT_2_LAYER_H
|
|
|
|
|
|
|
|
#include <blt/std/types.h>
|
2024-10-21 19:25:00 -04:00
|
|
|
#include <assign2/initializers.h>
|
2024-10-21 16:42:03 -04:00
|
|
|
|
|
|
|
namespace assign2
|
|
|
|
{
|
|
|
|
class layer_t
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
layer_t(const blt::i32 in, const blt::i32 out): in_size(in), out_size(out)
|
|
|
|
{}
|
|
|
|
|
2024-10-21 19:25:00 -04:00
|
|
|
template<typename WeightsFunc = empty_init, typename BiasFunc = empty_init>
|
|
|
|
void init(WeightsFunc weightFunc = empty_init{}, BiasFunc biasFunc = empty_init{})
|
|
|
|
{
|
|
|
|
weights.resize(in_size, out_size);
|
|
|
|
bias.resize(out_size);
|
|
|
|
|
|
|
|
weightFunc(weights);
|
|
|
|
biasFunc(bias);
|
|
|
|
}
|
|
|
|
|
2024-10-21 16:42:03 -04:00
|
|
|
template<typename ActFunction>
|
2024-10-21 19:25:00 -04:00
|
|
|
vector_t call(const vector_t& in, ActFunction func = ActFunction{})
|
2024-10-21 16:42:03 -04:00
|
|
|
{
|
|
|
|
vector_t out;
|
|
|
|
out.resize(out_size, Eigen::NoChange_t{});
|
|
|
|
out.noalias() = weights.transpose() * in;
|
|
|
|
out.colwise() += bias;
|
2024-10-21 19:25:00 -04:00
|
|
|
return func(std::move(out));
|
2024-10-21 16:42:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const blt::i32 in_size, out_size;
|
|
|
|
matrix_t weights{};
|
2024-10-21 19:25:00 -04:00
|
|
|
matrix_t dweights{}; // derivative of weights
|
2024-10-21 16:42:03 -04:00
|
|
|
vector_t bias{};
|
2024-10-21 19:25:00 -04:00
|
|
|
vector_t dbias{}; // derivative of bias
|
2024-10-21 16:42:03 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif //COSC_4P80_ASSIGNMENT_2_LAYER_H
|