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

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

微博黄锦池-hjimce   qq:1393852684

一、C++实现

 //y=x*w+bstatic void CFullyconnecteLayer::forward(const Eigen::MatrixXf &inputs, const Eigen::MatrixXf &weights, const Eigen::VectorXf &bais, Eigen::MatrixXf &outputs) {outputs = inputs*weights;outputs.rowwise() += bais.transpose();//每一行加上b}//y=x*w+b,反向求导后dw=x.T*dy,dx=dy*w.T,db=dystatic void CFullyconnecteLayer::backward(const Eigen::MatrixXf &inputs, const Eigen::MatrixXf &weights, const Eigen::VectorXf &bais,const Eigen::MatrixXf &d_outputs,Eigen::MatrixXf &d_inputs, Eigen::MatrixXf &d_weights, Eigen::VectorXf &d_bais) {d_weights = inputs.transpose()*d_outputs;d_inputs = d_outputs*weights.transpose();d_bais = d_outputs.colwise().sum();}static void CFullyconnecteLayer::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 outputs;//全连接层forward(inputs, weights, bais, outputs);Eigen::MatrixXf d_input, d_weights,d_output;float loss;CSoftmaxLayer::softmax_loss_forward_backward(outputs, label, d_output, loss);std::cout << loss << std::endl;Eigen::VectorXf d_bais;backward(inputs, weights, bais, d_output, d_input, d_weights, d_bais);std::cout << "outputs" << outputs << std::endl;std::cout << "d_output" << d_output << std::endl;std::cout << "d_input" << d_input << std::endl;std::cout << "d_weights" << d_weights << std::endl;std::cout << "d_bais"<<d_bais << 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])output=tf.matmul(inputs,weights)+bais
one_hot=tf.one_hot(label,2)
predicts=tf.nn.softmax(output)
loss =-tf.reduce_mean(one_hot * tf.log(predicts))d_output,d_inputs,d_weights,d_bais=tf.gradients(loss,[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,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)

从零开始编写深度学习库(二)FullyconnecteLayer CPU编写相关推荐

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

    从零开始编写深度学习库(三)ActivationLayer网络层CPU实现 博客:http://blog.csdn.net/hjimce 微博:黄锦池-hjimce   qq:1393852684 一 ...

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

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

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

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

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

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

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

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

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

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

  7. 【从零开始学深度学习编译器】十二,MLIR Toy Tutorials学习笔记一

    本笔记由学习MLIR Tutorials总结而成,欢迎批评指正. Chapter1: Toy语言和AST MLIR提供了一种Toy语言来说明MLIR的定义和执行的流程.Toy语言是一种基于张量的语言, ...

  8. 【干货小铺】各种编程语言的深度学习库整理

    点击上方"小白学视觉",选择加"星标"或"置顶" 重磅干货,第一时间送达本文转自|新机器视觉 本文总结了Python.Matlab.CPP. ...

  9. 30个深度学习库:按Python、C++、Java、JavaScript、R等10种语言分类

    30个深度学习库:按Python.C++.Java.JavaScript.R等10种语言分类 包括 Python.C++.Java.JavaScript.R.Haskell等在内的一系列编程语言的深度 ...

最新文章

  1. 【基础篇】DatePickerDialog日期控件的基本使用(一)
  2. SpringBoot 2 + Spring Security 5 + JWT 的单页应用 Restful 解决方案
  3. 关于ARP、MAC、IP欺骗以及TCP劫持
  4. BZOJ2662[BeiJing wc2012]冻结——分层图最短路
  5. scss的使用方式(环境搭建)
  6. 基于Tablestore的Wifi设备监管系统架构实现
  7. 第十一届蓝桥杯省赛C++组试题 第4题 选择题判定
  8. [ACTF2020 新生赛]Include
  9. Linux Ubuntu下Jupyter Notebook的安装
  10. 内核并发控制---互斥量(来自网易)
  11. elementUI使用之table表格如何给行元素添加点击事件
  12. 潭州课堂25班:Ph201805201 第十课 类的定义,属性和方法 (课堂笔记)
  13. UVA11309 Counting Chaos【Ad Hoc】
  14. usb 测试软件,usb端口测试(USB端口测试工具)
  15. 环境工程微生物学练习题
  16. OpenGL混合功能与抗锯齿
  17. C语言main函数参数[转:C语言中文网]
  18. java实现12306查票_GitHub - HendSame/J12306: 12306抢票程序JAVA版
  19. 在数据库使用期间创建OMF(Oracle Managed Files,Oracle管理的文件)
  20. sas html5,什么是sas?

热门文章

  1. csv格式清洗与转换python123,Python Pandas 清理错误格式数据
  2. ubuntu下mysql5.7安装教程_Ubuntu 16.04 上安装 MySQL 5.7 教程
  3. java 能重写构成函数_java函数重载和函数重写
  4. 前端怎么使用jsessionid_前端搞微前端 | 侑夕 - 如何落地微前端一体化运营工作台...
  5. c语言case多种情况,switch语句中有多种情况
  6. SQL性能优化前期准备-清除缓存、开启IO统计
  7. SQL基础:数据表的创建
  8. SVN代码回滚命令之---merge的使用
  9. 随机生成彩票的shell脚本
  10. php获取网页内容方法总结