mirror of
https://github.com/lordmathis/CUDANet.git
synced 2025-11-06 01:34:22 +00:00
Use IDX2C macro properly
This commit is contained in:
@@ -13,6 +13,7 @@ set(LIBRARY_SOURCES
|
|||||||
src/layers/dense.cpp
|
src/layers/dense.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
|
set(CMAKE_CUDA_ARCHITECTURES 75)
|
||||||
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||||
set(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS} -arch=sm_75)
|
set(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS} -arch=sm_75)
|
||||||
|
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ namespace Layers {
|
|||||||
float* d_weights;
|
float* d_weights;
|
||||||
float* d_biases;
|
float* d_biases;
|
||||||
|
|
||||||
std::vector<std::vector<float>> weights;
|
std::vector<float> weights;
|
||||||
std::vector<float> biases;
|
std::vector<float> biases;
|
||||||
|
|
||||||
void initializeWeights();
|
void initializeWeights();
|
||||||
|
|||||||
@@ -4,15 +4,13 @@
|
|||||||
#include <cuda_runtime.h>
|
#include <cuda_runtime.h>
|
||||||
#include <cublas_v2.h>
|
#include <cublas_v2.h>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <stdexcept>
|
#include <random>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Layers::Dense::Dense(int inputSize, int outputSize, cublasHandle_t cublasHandle)
|
Layers::Dense::Dense(int inputSize, int outputSize, cublasHandle_t cublasHandle)
|
||||||
: inputSize(inputSize), outputSize(outputSize), cublasHandle(cublasHandle) {
|
: inputSize(inputSize), outputSize(outputSize), cublasHandle(cublasHandle) {
|
||||||
|
|
||||||
// Allocate memory for weights and biases
|
// Allocate memory for weights and biases
|
||||||
weights.resize(outputSize, std::vector<float>(inputSize));
|
weights.resize(outputSize * inputSize);
|
||||||
biases.resize(outputSize);
|
biases.resize(outputSize);
|
||||||
|
|
||||||
initializeWeights();
|
initializeWeights();
|
||||||
@@ -32,17 +30,22 @@ Layers::Dense::~Dense() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Layers::Dense::initializeWeights() {
|
void Layers::Dense::initializeWeights() {
|
||||||
for (auto& row : weights) {
|
int numWeights = inputSize * outputSize;
|
||||||
for (float& weight : row) {
|
|
||||||
weight = 0.0f;
|
std::random_device rd;
|
||||||
|
std::mt19937 gen(rd());
|
||||||
|
std::normal_distribution<float> dist(0.0f, 0.01f); // Xavier initialization
|
||||||
|
|
||||||
|
for (int i = 0; i < outputSize; ++i) {
|
||||||
|
for (int j = 0; j < inputSize; ++j) {
|
||||||
|
int idx = IDX2C(i, j, inputSize);
|
||||||
|
weights[idx] = dist(gen);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Layers::Dense::initializeBiases() {
|
void Layers::Dense::initializeBiases() {
|
||||||
for (float& bias : biases) {
|
std::fill(biases.begin(), biases.end(), 0.1f);
|
||||||
bias = 0.0f;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Layers::Dense::forward(const float* d_input, float* d_output) {
|
void Layers::Dense::forward(const float* d_input, float* d_output) {
|
||||||
@@ -58,12 +61,20 @@ void Layers::Dense::toCuda() {
|
|||||||
CUBLAS_CHECK(cublasSetVector(biases.size(), sizeof(float), biases.data(), 1, d_biases, 1));
|
CUBLAS_CHECK(cublasSetVector(biases.size(), sizeof(float), biases.data(), 1, d_biases, 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Layers::Dense::setWeights(const std::vector<std::vector<float>>& weights) {
|
void Layers::Dense::setWeights(const std::vector<std::vector<float>>& weights_input) {
|
||||||
this->weights = weights;
|
int numWeights = inputSize * outputSize;
|
||||||
|
|
||||||
|
for (int i = 0; i < outputSize; ++i) {
|
||||||
|
for (int j = 0; j < inputSize; ++j) {
|
||||||
|
int idx = IDX2C(i, j, inputSize);
|
||||||
|
weights[idx] = weights_input[i][j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
toCuda();
|
toCuda();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Layers::Dense::setBiases(const std::vector<float>& biases) {
|
void Layers::Dense::setBiases(const std::vector<float>& biases_input) {
|
||||||
this->biases = biases;
|
std::copy(biases_input.begin(), biases_input.end(), biases.begin());
|
||||||
toCuda();
|
toCuda();
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user