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. jmeter汉化

    测试需要许多工具辅助工作,我们在使用这些工具之前需要对工具有一定的了解,第一步就是需要安装特定的软件. 因为工具基本上都是英文版的,所以菜鸟使用就面临汉化的问题,下面介绍几种汉化的办法: 一.万事不决 ...

  2. TensorFlow Python2.7环境下的源码编译(一)环境准备

    参考: https://blog.csdn.net/yhily2008/article/details/79967118 https://tensorflow.google.cn/install/in ...

  3. 《杜增强讲Unity之Tanks坦克大战》3-添加坦克

    3 添加坦克 3.1 本节效果预览   3.2 另存新场景 首先打开上次的场景s1,另存为s2,放到同一个文件夹下面.   3.3 添加坦克模型 在Model文件夹下面找到Tank模型   将Tank ...

  4. Python创建虚拟环境

    用于创建和管理虚拟环境的模块称为 venv.venv 通常会安装你可用的最新版本的 Python.如果您的系统上有多个版本的 Python,您可以通过运行 python3 或您想要的任何版本来选择特定 ...

  5. trampoline蹦床函数解决递归调用栈问题

    递归函数的调用栈太多,造成溢出,那么只要减少调用栈,就不会溢出.怎么做可以减少调用栈呢?就是采用"循环"换掉"递归". 下面是一个正常的递归函数. functi ...

  6. java基础---JDK、JRE、JVM的区别和联系

    当我们学习java语言时,首先需要安装到我们电脑上的就是jdk.jdk是java语言的开发环境,只有安装了jdk,我们才能使用java语言开发程序. JDK=JRE+开发工具包 JRE=JVM+核心类 ...

  7. 微信小程序-帝国cms会员系统调用

    在用户->管理会员字段,增加如下字段:openidsession_keylsktokennicknameheadimg设置用户名长度然后,在系统,系统变最设置,用户设置,将注册用户名设置长度改成 ...

  8. SQL Operations Studio的安装和使用

    之前管理和访问SQL SERVER使用的自然是SSMS,功能确实很强大的一个数据库图形化管理软件,但是SSMS有个问题就是体积超级大,启动速度也就比较慢.今天我正好要学习一些T-SQL的内容,在微软的 ...

  9. Hyperledger Fabric Capabilities——超级账本功能汇总

    Hyperledger Fabric是一种模块化的区块链架构,是分布式记账技术(DLT)的一种独特的实现,它提供了可供企业运用的网络,具备安全.可伸缩.加密和可执行等特性.Hyperledger Fa ...

  10. bcd引导Ubuntu

    下面步骤就是创建Windows的启动项了. 以管理员身份打开CMD, 然后输入 bcdedit /create /d "ubuntu" /application bootsecto ...