设为首页 加入收藏

TOP

单隐含层神经网络公式推导及C++实现 笔记(六)
2018-03-02 06:57:00 】 浏览:1000
Tags:隐含 神经网络 公式 推导 实现 笔记
write((char*)&this->hidden_layer_node_num, sizeof(int)); file.write((char*)&this->output_layer_node_num, sizeof(int)); int type = this->hidden_layer_activation_type; file.write((char*)&type, sizeof(int)); type = this->output_layer_activation_type; file.write((char*)&type, sizeof(int)); file.write((char*)&this->feature_length, sizeof(int)); int length = w1.size() * w1[0].size(); std::unique_ptr data1(new T[length]); T* p = data1.get(); for (int i = 0; i < w1.size(); ++i) { for (int j = 0; j < w1[0].size(); ++j) { p[i * w1[0].size() + j] = w1[i][j]; } } file.write((char*)p, sizeof(T)* length); file.write((char*)this->b1.data(), sizeof(T)* this->b1.size()); length = w2.size() * w2[0].size(); std::unique_ptr data2(new T[length]); p = data2.get(); for (int i = 0; i < w2.size(); ++i) { for (int j = 0; j < w2[0].size(); ++j) { p[i * w2[0].size() + j] = w2[i][j]; } } file.write((char*)p, sizeof(T)* length); file.write((char*)this->b2.data(), sizeof(T)* this->b2.size()); file.close(); return 0; } template class SingleHiddenLayer ; template class SingleHiddenLayer ; } // namespace ANN main.cpp:
#include "funset.hpp"
#include 
  
   
#include "perceptron.hpp"
#include "BP.hpp""
#include "CNN.hpp"
#include "linear_regression.hpp"
#include "naive_bayes_classifier.hpp"
#include "logistic_regression.hpp"
#include "common.hpp"
#include "knn.hpp"
#include "decision_tree.hpp"
#include "pca.hpp"
#include 
   
     #include "logistic_regression2.hpp" #include "single_hidden_layer.hpp" // ====================== single hidden layer(two categories) =============== int test_single_hidden_layer_train() { const std::string image_path{ "E:/GitCode/NN_Test/data/images/digit/handwriting_0_and_1/" }; cv::Mat data, labels; for (int i = 1; i < 11; ++i) { const std::vector
    
      label{ "0_", "1_" }; for (const auto& value : label) { std::string name = std::to_string(i); name = image_path + value + name + ".jpg"; cv::Mat image = cv::imread(name, 0); if (image.empty()) { fprintf(stderr, "read image fail: %s\n", name.c_str()); return -1; } data.push_back(image.reshape(0, 1)); } } data.convertTo(data, CV_32F); std::unique_ptr
     
       tmp(new float[20]); for (int i = 0; i < 20; ++i) { if (i % 2 == 0) tmp[i] = 0.f; else tmp[i] = 1.f; } labels = cv::Mat(20, 1, CV_32FC1, tmp.get()); ANN::SingleHiddenLayer
      
        shl; const float learning_rate{ 0.00001f }; const int iterations{ 10000 }; const int hidden_layer_node_num{ static_cast
       
        (std::log2(data.cols)) }; const int hidden_layer_activation_type{ ANN::SingleHiddenLayer
        
         ::ReLU }; const int output_layer_activation_type{ ANN::SingleHiddenLayer
         
          ::Sigmoid }; int ret = shl.init((float*)data.data, (float*)labels.data, data.rows, data.cols, hidden_layer_node_num, learning_rate, iterations, hidden_layer_activation_type, output_layer_activation_type); if (ret != 0) { fprintf(stderr, "single_hidden_layer(two categories) init fail: %d\n", ret); return -1; } const std::string model{ "E:/GitCode/NN_Test/data/single_hidden_layer.model" }; ret = shl.train(model); i
首页 上一页 3 4 5 6 下一页 尾页 6/6/6
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇c++与C const变量的区别详解 下一篇C++中的引用(代码实例)

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目