79 lines
2.4 KiB
C++
79 lines
2.4 KiB
C++
#ifndef CORE_H
|
|
#define CORE_H
|
|
|
|
#include "typedef.hpp"
|
|
#include <vector>
|
|
#include <cmath>
|
|
#include <cstdlib>
|
|
#include <omp.h>
|
|
#include <vulkan/vulkan.hpp>
|
|
#include <iostream>
|
|
#include <fstream>
|
|
|
|
class NeuralNetwork {
|
|
private:
|
|
// Параметры нейросети
|
|
int numLayers;
|
|
std::vector<int> sizes;
|
|
std::vector<std::vector<std::vector<double>>> weights;
|
|
std::vector<std::vector<double>> biases;
|
|
std::vector<std::vector<double>> outputs;
|
|
|
|
// Объекты Vulkan
|
|
bool useVulkan; // Сохраняем выбор пользователя
|
|
vk::Instance instance;
|
|
vk::PhysicalDevice physDev;
|
|
vk::Device device;
|
|
vk::Queue queue;
|
|
vk::CommandPool cmdPool;
|
|
uint32_t computeQueueFamilyIndex; // Индекс очереди для команд
|
|
|
|
// Вспомогательные методы Vulkan
|
|
uint32_t findMemoryType(uint32_t typeFilter, vk::MemoryPropertyFlags properties);
|
|
std::vector<char> readFile(const std::string& filename);
|
|
|
|
// Математика CPU
|
|
double sigmoid(double x) { return 1.0 / (1.0 + exp(-x)); }
|
|
double sigmoidDeriv(double x) { return x * (1.0 - x); }
|
|
|
|
struct TrainParams {
|
|
uint32_t prevLayerSize;
|
|
uint32_t nextLayerSize;
|
|
uint32_t weightOffset;
|
|
uint32_t biasOffset;
|
|
uint32_t outOffset;
|
|
uint32_t errOffset;
|
|
float lr;
|
|
};
|
|
|
|
vk::Buffer gpuW, gpuB, gpuO, gpuE;
|
|
vk::DeviceMemory memW, memB, memO, memE;
|
|
vk::DescriptorPool descriptorPool;
|
|
vk::DescriptorSet descriptorSet;
|
|
vk::DescriptorSetLayout dsLayout;
|
|
vk::PipelineLayout pipeLayout;
|
|
vk::Pipeline pipeline;
|
|
vk::ShaderModule shaderModule;
|
|
|
|
bool vulkanResourcesInitialized = false;
|
|
|
|
void initVulkanResources(); // Метод для разовой инициализации
|
|
void cleanupVulkanResources();
|
|
|
|
public:
|
|
int cpu_count = 1;
|
|
|
|
// Конструктор
|
|
NeuralNetwork(LayerStructure_t layers[], int count, bool useVulkan = false);
|
|
|
|
// Деструктор (ВАЖНО для очистки Vulkan)
|
|
~NeuralNetwork();
|
|
|
|
// Методы работы
|
|
std::vector<double> feedForward(const std::vector<double>& input);
|
|
double train(const std::vector<double>& input, const std::vector<double>& target, double lr);
|
|
|
|
double trainVulkan(const std::vector<double>& input, const std::vector<double>& target, double lr);
|
|
};
|
|
|
|
#endif |