从零开始编写深度学习库(三)ActivationLayer网络层CPU实现

博客http://blog.csdn.net/hjimce

微博黄锦池-hjimce   qq:1393852684

一、C++实现:

 //relu=max(x,0)static void CActivationLayer::relu_forward(const Eigen::MatrixXf &bottom,Eigen::MatrixXf &top){top = bottom.cwiseMax(0);}/* if x>0 dx=dreluelse dx=0  */static void CActivationLayer::relu_backward(const Eigen::MatrixXf &top, const Eigen::MatrixXf &d_top,Eigen::MatrixXf &d_bottom) {d_bottom = (top.array() <=0).select(0, d_top);}//y=1/(1+exp(-z))static void CActivationLayer::sigmoid_forward(const Eigen::MatrixXf &bottom, Eigen::MatrixXf &top) {top = 1 / (1 + (-bottom).array().exp());}//dz=y(1-y)static void CActivationLayer::sigmoid_backward(const Eigen::MatrixXf &top, const Eigen::MatrixXf &d_top, Eigen::MatrixXf &d_bottom) {d_bottom = d_top.array()*top.array()*(1 - top.array());}static void CActivationLayer::test() {//验证测试函数int batch_size = 4;int input_size = 3;int output_size = 2;Eigen::MatrixXf inputs(batch_size, input_size);inputs << 1, -2, 3, 4, -5, 6, 7, -8, -9, 10, 11, -12;Eigen::MatrixXf weights(input_size, output_size);weights << 0.55,-0.88, 0.75, -1.1, -0.11, 0.002;Eigen::VectorXf bais(output_size);bais << 3, -2;Eigen::VectorXf label(batch_size);label << 1, 0, 1, 1;Eigen::MatrixXf fc_outputs;//全连接层-》forwardCFullyconnecteLayer::forward(inputs, weights, bais, fc_outputs);Eigen::MatrixXf relu_output;//relu层->forwardsigmoid_forward(fc_outputs, relu_output);Eigen::MatrixXf d_input, d_fc_weights, d_relu_output,d_fc_output;//softmax层—forward_backwardfloat loss;CSoftmaxLayer::softmax_loss_forward_backward(relu_output, label, d_relu_output, loss);sigmoid_backward(relu_output, d_relu_output, d_fc_output);Eigen::VectorXf d_fc_bais;CFullyconnecteLayer::backward(inputs, weights, bais, d_fc_output, d_input, d_fc_weights, d_fc_bais);std::cout << loss << std::endl;std::cout << "relu_outputs" << relu_output << std::endl;std::cout << "d_relu_output" << d_relu_output << std::endl;std::cout << "d_input" << d_input << std::endl;std::cout << "d_fc_weights" << d_fc_weights << std::endl;std::cout << "d_fc_bais" << d_fc_bais << std::endl;std::cout << "d_fc_output" << d_fc_output << std::endl;}

二、tensorflow 代码验证:

import  tensorflow as tf
inputs=tf.constant([[1,-2,3],[4,-5,6],[7,-8,-9],[10,11,-12]],shape=(4,3),dtype=tf.float32)
weights=tf.constant([0.55, -0.88, 0.75, -1.1, -0.11, 0.002],shape=(3,2),dtype=tf.float32)
bais=tf.constant([3, -2],dtype=tf.float32)
label=tf.constant([1,0,1,1])relu_output=tf.nn.relu(tf.matmul(inputs,weights)+bais)
one_hot=tf.one_hot(label,2)
predicts=tf.nn.softmax(relu_output)
loss =-tf.reduce_mean(one_hot * tf.log(predicts))#打印相关变量,梯度等,验证是否与c++结果相同
d_output,d_inputs,d_weights,d_bais=tf.gradients(loss,[relu_output,inputs,weights,bais])with tf.Session() as sess:sess.run(tf.global_variables_initializer())loss_np,output_np,d_output_np, d_inputs_np, d_weights_np, d_bais_np=sess.run([loss,relu_output,d_output,d_inputs,d_weights,d_bais])print (loss_np)print ('output',output_np)print('d_output', d_output_np)print ("d_inputs",d_inputs_np)print("d_weights", d_weights_np)print("d_bais", d_bais_np)

从零开始编写深度学习库(三)ActivationLayer网络层CPU实现相关推荐

  1. 从零开始编写深度学习库(四)Eigen::Tensor学习使用及代码重构

    从零开始编写深度学习库(四)Eigen::Tensor学习使用及代码重构 博客:http://blog.csdn.net/hjimce 微博:黄锦池-hjimce   qq:1393852684 一. ...

  2. 从零开始编写深度学习库(二)FullyconnecteLayer CPU编写

    从零开始编写深度学习库(二)FullyconnecteLayer CPU编写 博客:http://blog.csdn.net/hjimce 微博:黄锦池-hjimce   qq:1393852684 ...

  3. 从零开始编写深度学习库(一)SoftmaxWithLoss CPU编写

    从零开始编写深度学习库(一)SoftmaxWithLoss CPU编写 博客:http://blog.csdn.net/hjimce 微博:黄锦池-hjimce   qq:1393852684 一.C ...

  4. 从零开始编写深度学习库(五)PoolingLayer 网络层CPU编写

    记录:编写卷积层和池化层,比较需要注意的细节就是边界问题,还有另外一个就是重叠池化的情况,这两个小细节比较重要,边界问题pad在反向求导的时候,由于tensorflow是没有计算的,另外一个比较烦人的 ...

  5. 从零开始编写深度学习库(五)Eigen Tensor学习笔记2.0

    1.extract_image_patches函数的使用: 假设Eigen::Tensor形状为(3,8,8,9),现在要对第二维.第三维根据size大小为(2,2),stride=(2,2),那么如 ...

  6. 从零开始编写深度学习库(五)ConvolutionLayer CPU编写

    对于池化层来说,需要注意的问题是:重叠池化,还有边界处理模式:valid.same模式这两个细节.由于我采用的边界处理方式,与tensorflow 在same模式下边界处理方式不同,valid模式下是 ...

  7. python深度学习库keras——各类网络层

    全栈工程师开发手册 (作者:栾鹏) python教程全解 一.网络层 keras的层主要包括: 常用层(Core).卷积层(Convolutional).池化层(Pooling).局部连接层.递归层( ...

  8. 如何从零开始构建深度学习项目?这里有一份详细的教程

    点击上方"迈微AI研习社",选择"星标★"公众号 重磅干货,第一时间送达 来源丨机器之心 在学习了有关深度学习的理论课程之后,很多人都会有兴趣尝试构建一个属于自 ...

  9. 手把手教 | 深度学习库PyTorch(附代码)

    原文标题: An Introduction to PyTorch – A Simple yet Powerful Deep LearningLibrary 作者:FAIZAN SHAIKH 翻译:和中 ...

最新文章

  1. 编写程序将字符串中最长的单词输出
  2. js调试控制台使用详解图解
  3. Java虚拟机内存模型初步学习
  4. Systemd 入门教程之命令篇
  5. (王道408考研数据结构)第六章图-第四节2:最小生成树之克鲁斯卡尔算法(思想、代码、演示、答题规范)
  6. iOS 面试题整理(带答案)二
  7. 爬虫实战(一)之爬取房天下新房数据
  8. Sophix热修复的简单使用
  9. 應用程式中發生伺服器錯誤
  10. Django重置管理后台模板
  11. 宅男也可变形男-我是如何在11个月零27天减掉80斤的
  12. J2ME 发送彩信问题,请个位高手帮忙,长时间在线等待
  13. 利用Excel自带的数据分析工具进行回归分析
  14. 新生学大学计算机心得,大学生信息技术心得体会怎么写
  15. python做web后端_最简易的python web框架的后端实现
  16. 配置secureCRT
  17. 2021中国IC封装基板市场现状及未来发展趋势
  18. D3.js音乐可视化
  19. ImportError: cannot import name ‘SAVE_STATE_WARNING‘ from ‘torch.optim.lr_scheduler‘ (/home/jsj/anac
  20. 互联网应用基础第三课:初识搜索引擎和常见网站类型

热门文章

  1. linux 脚本 变量为空,Shell判断一个变量是否为空
  2. javascript乘法和加法_前端基础:JavaScript
  3. 新装oracle密码文件,oracle密码文件的重建
  4. Visual Studio Code快速删除空行及几个常用快捷键总结
  5. SpringBoot 2.x (3):文件上传
  6. @RequestBody配合@JsonFormat注解实现字符串自动转换成Date
  7. 2017-2018 Northwestern European Regional Contest (NWERC 2017)
  8. Teamviewer 手机端怎么使用右键-已解决
  9. pycharm 如何自动添加头注释,比如时间,作者信息等
  10. 嵌入式linux和pc机的linux对照