mirror of
https://github.com/lordmathis/CUDANet.git
synced 2025-11-06 01:34:22 +00:00
Initial model implementation
This commit is contained in:
@@ -12,6 +12,7 @@ file(GLOB_RECURSE LIBRARY_SOURCES
|
||||
src/utils/*.cu
|
||||
src/kernels/*.cu
|
||||
src/layers/*.cu
|
||||
src/model/*.cpp
|
||||
)
|
||||
|
||||
set(LIBRARY_SOURCES
|
||||
@@ -32,6 +33,7 @@ target_include_directories(${PROJECT_NAME} PUBLIC
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/include/utils
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/include/kernels
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/include/layers
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/include/model
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src
|
||||
)
|
||||
|
||||
|
||||
@@ -1,20 +1,22 @@
|
||||
#ifndef CUDANET_INPUT_LAYER_H
|
||||
#define CUDANET_INPUT_LAYER_H
|
||||
|
||||
#include "layer.cuh"
|
||||
|
||||
namespace CUDANet::Layers {
|
||||
|
||||
/**
|
||||
* @brief Input layer, just copies the input to the device
|
||||
*
|
||||
*/
|
||||
class Input {
|
||||
class Input : public SequentialLayer {
|
||||
public:
|
||||
/**
|
||||
* @brief Create a new Input layer
|
||||
*
|
||||
* @param inputSize Size of the input vector
|
||||
*/
|
||||
Input(int inputSize);
|
||||
explicit Input(int inputSize);
|
||||
|
||||
/**
|
||||
* @brief Destroy the Input layer
|
||||
|
||||
34
include/model/model.hpp
Normal file
34
include/model/model.hpp
Normal file
@@ -0,0 +1,34 @@
|
||||
#ifndef CUDANET_MODEL_H
|
||||
#define CUDANET_MODEL_H
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include "layer.cuh"
|
||||
|
||||
namespace CUDANet {
|
||||
|
||||
class Model {
|
||||
public:
|
||||
Model(const int inputSize, const int inputChannels);
|
||||
~Model();
|
||||
|
||||
float* predict(const float* input);
|
||||
|
||||
void addLayer(const std::string& name, Layers::SequentialLayer* layer);
|
||||
|
||||
private:
|
||||
|
||||
int inputSize;
|
||||
int inputChannels;
|
||||
|
||||
int outputSize;
|
||||
|
||||
std::vector<Layers::SequentialLayer*> layers;
|
||||
std::map<std::string, Layers::WeightedLayer*> layerMap;
|
||||
|
||||
};
|
||||
|
||||
} // namespace CUDANet
|
||||
|
||||
#endif // CUDANET_MODEL_H
|
||||
36
src/model/model.cpp
Normal file
36
src/model/model.cpp
Normal file
@@ -0,0 +1,36 @@
|
||||
#include "model.hpp"
|
||||
#include "layer.cuh"
|
||||
#include "input.cuh"
|
||||
|
||||
using namespace CUDANet;
|
||||
|
||||
Model::Model(const int inputSize, const int inputChannels)
|
||||
: inputSize(inputSize), inputChannels(inputChannels) {
|
||||
|
||||
layerMap = std::map<std::string, Layers::WeightedLayer*>();
|
||||
layers = std::vector<Layers::SequentialLayer*>();
|
||||
|
||||
|
||||
const int inputLayerSize = inputSize * inputSize * inputChannels;
|
||||
Layers::Input* inputLayer = new Layers::Input(inputLayerSize);
|
||||
|
||||
layers.push_back(inputLayer);
|
||||
};
|
||||
|
||||
Model::~Model(){};
|
||||
|
||||
float* Model::predict(const float* input) {
|
||||
|
||||
for (auto& layer : layers) {
|
||||
input = layer->forward(input);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void Model::addLayer(const std::string& name, Layers::SequentialLayer* layer) {
|
||||
layers.push_back(layer);
|
||||
|
||||
if (dynamic_cast<Layers::WeightedLayer*>(layer) != nullptr) {
|
||||
layerMap[name] = dynamic_cast<Layers::WeightedLayer*>(layer);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user