设为首页 加入收藏

TOP

单隐含层神经网络公式推导及C++实现 笔记(五)
2018-03-02 06:57:00 】 浏览:999
Tags:隐含 神经网络 公式 推导 实现 笔记
) { this->w1[i].resize(this->feature_length); } this->b1.resize(this->hidden_layer_node_num); this->w2.resize(this->output_layer_node_num); for (int i = 0; i < this->output_layer_node_num; ++i) { this->w2[i].resize(this->hidden_layer_node_num); } this->b2.resize(this->output_layer_node_num); int length = w1.size() * w1[0].size(); std::unique_ptr data1(new T[length]); T* p = data1.get(); file.read((char*)p, sizeof(T)* length); file.read((char*)this->b1.data(), sizeof(T)* b1.size()); int count{ 0 }; for (int i = 0; i < this->w1.size(); ++i) { for (int j = 0; j < this->w1[0].size(); ++j) { w1[i][j] = p[count++]; } } length = w2.size() * w2[0].size(); std::unique_ptr data2(new T[length]); p = data2.get(); file.read((char*)p, sizeof(T)* length); file.read((char*)this->b2.data(), sizeof(T)* b2.size()); count = 0; for (int i = 0; i < this->w2.size(); ++i) { for (int j = 0; j < this->w2[0].size(); ++j) { w2[i][j] = p[count++]; } } file.close(); return 0; } template T SingleHiddenLayer ::predict(const T* data, int feature_length) const { CHECK(feature_length == this->feature_length); CHECK(this->output_layer_node_num == 1); CHECK(this->hidden_layer_activation_type >= 0 && this->hidden_layer_activation_type < 4); CHECK(this->output_layer_activation_type >= 0 && this->output_layer_activation_type < 4); std::vector z1(this->hidden_layer_node_num, (T)0.), a1(this->hidden_layer_node_num, (T)0.), z2(this->output_layer_node_num, (T)0.), a2(this->output_layer_node_num, (T)0.); for (int p = 0; p < this->hidden_layer_node_num; ++p) { for (int q = 0; q < this->feature_length; ++q) { z1[p] += w1[p][q] * data[q]; } z1[p] += b1[p]; a1[p] = calculate_activation_function(z1[p], this->hidden_layer_activation_type); } for (int p = 0; p < this->output_layer_node_num; ++p) { for (int q = 0; q < this->hidden_layer_node_num; ++q) { z2[p] += w2[p][q] * a1[q]; } z2[p] += b2[p]; a2[p] = calculate_activation_function(z2[p], this->output_layer_activation_type); } return a2[0]; } template T SingleHiddenLayer ::calculate_activation_function(T value, ActivationFunctionType type) const { T result{ 0 }; switch (type) { case Sigmoid: result = (T)1. / ((T)1. + std::exp(-value)); break; case TanH: result = (T)(std::exp(value) - std::exp(-value)) / (std::exp(value) + std::exp(-value)); break; case ReLU: result = std::max((T)0., value); break; case Leaky_ReLU: result = std::max((T)0.01*value, value); break; default: CHECK(0); break; } return result; } template T SingleHiddenLayer ::calcuate_activation_function_derivative(T value, ActivationFunctionType type) const { T result{ 0 }; switch (type) { case Sigmoid: { T tmp = calculate_activation_function(value, Sigmoid); result = tmp * (1. - tmp); } break; case TanH: { T tmp = calculate_activation_function(value, TanH); result = 1 - tmp * tmp; } break; case ReLU: result = value < 0. ? 0. : 1.; break; case Leaky_ReLU: result = value < 0. ? 0.01 : 1.; break; default: CHECK(0); break; } return result; } template int SingleHiddenLayer ::store_model(const std::string& model) const { std::ofstream file; file.open(model.c_str(), std::ios::binary); if (!file.is_open()) { fprintf(stderr, "open file fail: %s\n", model.c_str()); return -1; } file.
首页 上一页 2 3 4 5 6 下一页 尾页 5/6/6
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇c++与C const变量的区别详解 下一篇C++中的引用(代码实例)

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目