http://hihocoder.com/contest/tupu2018/problem/2

题目2 : ​Standard 2D Convolution

时间限制:5000ms
单点时限:1000ms
内存限制:256MB

描述

Implement such below standard convolution

conv2d(input tensor, filters = 8, kernel size=[5,5], stride = 2, zero padding = 'SAME', activation = relu)

The shape of input tensor:  [height = 32, width = 32, channels = 3]

输入

The first line will contain an image. The value of pixels is [0, 255]. The image should be preprocessed (/127.5 - 1) before fed into the convolution function. Weights[kernel height, kernel width, input channels, output channels] and biases[output channels] are followed in the next two lines respectively.

输出

Print the result tensor in one line printed in the same way as input file. The precision is (1E-4).

注意

All data are arranged into one line using C-like order, with the last axis index changing fastest, back to the first axis index changing slowest.

样例输入
Download the sample input from:
https://media.hihocoder.com/contests/tupu-campus-hiring-2017/conv_sample_input.txt 
样例输出
Download the sample output from:
https://media.hihocoder.com/contests/tupu-campus-hiring-2017/conv_sample_output.txt 

题意:实现conv2d()卷积函数。

卷积神经网络

#input channels = 3, output channels = 8

#input_tensor = 32 * 32 * 3

#weights = 5 * 5 * 3 * 8

#biases = 1*8

参数解释:stride是步长参数;zero padding表示是否用零填充边缘进行,same表示在stride = 1的时候输出矩阵大小不变; activation是激励函数;ReLU函数为f(x) = max(x, 0)。

 #include <iostream>
 #include <stdio.h>
 #include <cmath>
 #include <vector>
 #include <string>
 using namespace std;
 #define Height 32
 #define Width 32
 #define Channels 3
 #define Filters 8
 #define kernel 5
 #define Eps 1e-5
 float weight[kernel][kernel][Channels][Filters];
 float biases[Filters];
 , , , string padding = "SAME", string activation = "relu")
 {
     int feaMapH = ceil(Height * 1.0 / stride);
     ) * stride + kernelSize;
     ;
     int paddingR = (HeightAfterPadding - Height) - paddingL;
     vector<vector< vector<float> > >a;

     ;i < HeightAfterPadding;i++){
         vector< vector<float> >b;
         ; j < HeightAfterPadding;j++){
             vector<float>c;
             ;k < Channels;k++){
                 c.push_back();
             }
             b.push_back(c);
         }
         a.push_back(b);
     }
     //cout << HeightAfterPadding << endl;

     ;i < Height;i++){
         ; j < Width;j++){
             ;k < Channels;k++){
                 a[paddingL + i][paddingL + j][k] = ((float)(inputTensor[i][j][k])) / 127.5 - 1.0;
             }
         }
     }

     ;i <= HeightAfterPadding - kernelSize;i += stride){
         ; j <= HeightAfterPadding - kernelSize;j += stride){

             ;nn < filters;nn++){
                 ;
                 ;k < kernelSize;k++){
                     ;l < kernelSize;l++){
                         ;mm < Channels;mm++){
                             sum += a[i + k][j + l][mm] * weight[k][l][mm][nn];
                         }
                     }
                 }
                 sum += biases[nn];
                 if(sum < Eps) cout << "0.0000 " ;
                 else printf("%.4f ",sum);
             }
             //cout << endl;
         }
     }
     return ;
 }
 int main()
 {
     int a[Height][Width][Channels];
     //freopen("conv_sample_input.txt","r",stdin);
     ;i < Height;i++){
         ;j < Width;j++){
             ;k < Channels;k++){
                 cin >> a[i][j][k];
             }
         }
     }

     ;i < kernel;i++){
         ;j < kernel;j++){
             ;k < Channels;k++){
                 ;l < Filters;l++){
                     cin >> weight[i][j][k][l];
                 }
             }
         }
     }

     ;l < Filters;l++){
         cin >> biases[l];
     }
     conv2d(a,Filters,kernel);
     ;
 }

附:FFT矩阵卷积的快速乘法

Hihocoder之conv2d()的更多相关文章

  1. hihocoder -1121-二分图的判定

    hihocoder -1121-二分图的判定 1121 : 二分图一•二分图判定 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 大家好,我是小Hi和小Ho的小伙伴Net ...

  2. Hihocoder 太阁最新面经算法竞赛18

    Hihocoder 太阁最新面经算法竞赛18 source: https://hihocoder.com/contest/hihointerview27/problems 题目1 : Big Plus ...

  3. hihoCoder太阁最新面经算法竞赛15

    hihoCoder太阁最新面经算法竞赛15 Link: http://hihocoder.com/contest/hihointerview24 题目1 : Boarding Passes 时间限制: ...

  4. 【hihoCoder 1454】【hiho挑战赛25】【坑】Rikka with Tree II

    http://hihocoder.com/problemset/problem/1454 调了好长时间,谜之WA... 等我以后学好dp再来看为什么吧,先弃坑(╯‵□′)╯︵┻━┻ #include& ...

  5. 【hihocoder#1413】Rikka with String 后缀自动机 + 差分

    搞了一上午+接近一下午这个题,然后被屠了个稀烂,默默仰慕一晚上学会SAM的以及半天4道SAM的hxy大爷. 题目链接:http://hihocoder.com/problemset/problem/1 ...

  6. 【hihoCoder】1148:2月29日

    问题:http://hihocoder.com/problemset/problem/1148 给定两个日期,计算这两个日期之间有多少个2月29日(包括起始日期). 思路: 1. 将问题转换成求两个日 ...

  7. 【hihoCoder】1288 : Font Size

    题目:http://hihocoder.com/problemset/problem/1288 手机屏幕大小为 W(宽) * H(长),一篇文章有N段,每段有ai个字,要求使得该文章占用的页数不超过P ...

  8. 【hihoCoder】1082: 然而沼跃鱼早就看穿了一切

      题目:http://hihocoder.com/problemset/problem/1082 输入一个字符串,将其中特定的单词替换成另一个单词   代码注意点: 1. getline(istre ...

  9. 【hihoCoder】1121:二分图一·二分图判定

      题目   http://hihocoder.com/problemset/problem/1121 无向图上有N个点,两两之间可以有连线,共有M条连线. 如果对所有点进行涂色(白/黑),判定是否存 ...

随机推荐

  1. Qt-网易云音乐界面实现-6 迷你个人中心实现

    这个界面除了麻烦耗时,没有啥技术含量.暂时我也就把它称为迷你个人中心,因为后面还有一个个人中心了. 先看下完成品 左侧是我的,右侧是原生 个人感觉还可以吧,哈哈哈.给我自己奖励一个鸡腿. 看下头文件 ...

  2. HT7A6312—— 离线开关电源小功率初级转换开关IC 记录总结

    1. 芯片特性 a. 固定60KHz开关频率: b. 宽Vcc输出电压范围:9V - 38V: c. 宽交流输入电压范围:85Vac - 265Vac: d. 电流模式PWM控制: e. 带迟滞的辅助 ...

  3. php常用的魔术方法

    __construct:构造函数,一旦创建对象都就会自动调用 __call:当调用了未定义的方法时会自动触发 __set:当给类外部不可访问的属性设置值时会自动触发 __get:当获取类外部不可访问的 ...

  4. 二级域名 cookie session 共享

    setcookie('login','12345',0,'/','.abc.com'); session_set_cookie_params(0,'/','.abc.com');session_sta ...

  5. jenkins 多任务串行执行

    摘要 今天在新创建自动化部署项目的时候遇到了一个问题:我们的项目是maven聚合的所以在构建maven项目的时候要从parent开始build,但是这样会造成一个问题,我每次添加此parent项目下的 ...

  6. MySQL基础架构之查询语句执行流程

    这篇笔记主要记录mysql的基础架构,一条查询语句是如何执行的. 比如,在我们从student表中查询一个id=2的信息 select * from student where id=2; 在解释这条 ...

  7. 用编程方式编写Babylon格式的宇宙飞船3D模型

    使用上一篇文章(https://www.cnblogs.com/ljzc002/p/9353101.html)中提出的方法,编写一个简单的宇宙飞船3D模型,在这篇文章中对模型制作流程和数学计算步骤进行 ...

  8. 教你如何编写、保存与运行 Python 程序

    第一步 接下来我们将看见如何在 Python 中运行一个传统的“Hello World”程序.Python教程本章将会教你如何编写.保存与运行 Python 程序. 通过 Python 来运行的你的程 ...

  9. 从零开始的Python学习Episode 20——面向对象(3)

    面向对象之封装 封装,即隐藏对象的属性和实现细节,仅对外公开接口,控制在程序中属性的读和修改的访问级别:将抽象得到的数据和行为(或功能)相结合,形成一个有机的整体. 隐藏 在python中用双下划线开 ...

  10. 君学,佳一tvodp文件破解

    tvodp文件破解的意思就是,越过加密部分直接提取内部原始文件,难度较大,方法用U盘刻老毛桃pe,然后电脑启动pe,在pe中打开文件,做提取工作, 本人淘宝破解:https://item.taobao ...