首页 > 其他分享 >深度学习的始祖框架,grandfather级别的框架 —— Theano —— 示例代码学习(3)

深度学习的始祖框架,grandfather级别的框架 —— Theano —— 示例代码学习(3)

时间:2024-02-12 18:36:55浏览次数:29  
标签:框架 示例 nb grandfather filter shape image TT self

实战:写一个卷积层ConvolutionLayer



二维卷积的前向操作:

代码:

import theano.tensor as TT
import theano
import numpy as np
# from theano.tensor.shared_randomstreams import RandomStreams


Identity = lambda x: x
ReLU = lambda x: TT.maximum(x, 0.0)
Sigmoid = lambda x: TT.nnet.sigmoid(x)
Tanh = lambda x: TT.tanh(x)


"""
input:  (batchsize, nb_channel, nb_i_row, nb_i_column)
filter: (nb_filters,nb_channel, nb_f_row, nb_f_column)
output: (batchsize, nb_filters, nb_o_row, nb_o_column)
"""
class ConvLayer(object):
    def __init__(self, image_shape, filter_shape, activation):
        self.activation = eval(activation)
        self.image_shape = image_shape
        self.filter_shape = filter_shape
        rng = np.random.RandomState(seed=42)
        w_init = np.asarray(rng.uniform(low=-1, high=1,  size=filter_shape), dtype='float32')
        b_init = np.zeros(filter_shape[0], dtype='float32')
        self.w = theano.shared(value=w_init, borrow=True)
        self.b = theano.shared(value=b_init, borrow=True)

    def feedforward(self, x):
        cout = TT.nnet.conv.conv2d(x, self.w, self.image_shape, self.filter_shape)

        # dimshuffle后,shape在'x'位置都被补1
        output = self.activation(cout + self.b.dimshuffle('x', 0, 'x', 'x'))
        return output

x = TT.tensor4('x', dtype="float32")  # 维度为4的tensor, tensor4

con = ConvLayer((32, 3, 224, 224), (64, 3, 5, 5), "ReLU")
out_put = con.feedforward(x)
con_run = theano.function([x, ], out_put, allow_input_downcast=True)


image_input = np.array( np.random.rand(32, 3, 224, 224), dtype="float32" )
# print( con_run(image_input) )
print( con_run(image_input).shape )

运行结果:

image



参考:

https://zhuanlan.zhihu.com/p/24275551



标签:框架,示例,nb,grandfather,filter,shape,image,TT,self
From: https://www.cnblogs.com/devilmaycry812839668/p/18013992

相关文章

  • 深度学习的始祖框架,grandfather级别的框架 —— Theano —— 示例代码学习(2)
    代码1:(ifelse判断结构)importtheanofromtheanoimporttensorfromtheano.ifelseimportifelsex=tensor.fscalar('x')y=tensor.fscalar('y')z=ifelse(x>0,2*y,3*y)#x>0的返回值是int8类型f=theano.function([x,y],z,allow_input_down......
  • 深度学习的始祖框架,grandfather级别的框架 —— Theano —— 示例代码学习(1)
    示例代码1:importtheanofromtheanoimporttensorx=tensor.vector("x")y=tensor.vector("y")w=tensor.vector("w")z=tensor.vector("z")z=x+y+wf=theano.function([x,theano.In(y,value=[1,1,1]),theano.In(......
  • PHP项目&TP框架&SQL&XSS&架构&路由&调试&写法
    开发基础-TP框架-入口&调试&路由&写法等参考手册-TP5开发手册-为了掌握了解框架首页文件看APP_PATH定义-为了后期分析核心代码全局搜索:THINK_VERSION,为了后期分析此版本是否存在漏洞。参考手册-本地代码案例对比,为了后期分析定位代码块或测试漏洞。配置文件开关(app_debug,a......
  • extism 基于rust 开发的强大webassembly 框架
    extism基于rust开发的强大webassembly框架包含的特性使用简单 可以方便的开发基于webassembly的插件系统安全方便运行 包含了灵活的架构可以可以方便与多种语言进行通信(基本覆盖了主流的编程语言)说明目前基于webassembly的语言集成热度是越来越高了,webassembly很值......
  • 第 7章 Python 爬虫框架 Scrapy(上)
    第7章Python爬虫框架Scrapy(上)编写爬虫可以看成行军打仗,基本的角色有两个:士兵和将军,士兵冲锋陷阵,而将军更多地是调兵遣将。框架就像一个将军,里面包含了爬虫的全部流程、异常处理和任务调度等。除了可以让我们少写一些烦琐的代码,学习框架还可以学到编程思想和提升编程能力。Pyt......
  • Java之泛型系列--继承父类与实现多个接口(有示例)
    原文网址:​​Java之泛型系列--继承父类与实现多个接口(有示例)_IT利刃出鞘的博客-CSDN博客​​简介本文介绍java如何用泛型表示继承父类并实现多个接口。用泛型表示某个类是某个类的子类或者实现了接口的方法为:<TextendsA&B&C> 用法1:全都是接口。对于本例来说:A、B......
  • nim静态编译capstone示例代码
    capstone.c代码:/*CapstoneDisassemblerEngine*//*ByNguyenAnhQuynh<[email protected]>,2013*/#include<stdio.h>#include<stdlib.h>#include<capstone/capstone.h>#include<capstone/platform.h>staticcshhandle;s......
  • 深度学习框架Theano停止维护
    Theano停止开发的声明地址:https://groups.google.com/g/theano-users/c/7Poq8BZutbY/m/rNCIfvAEAwAJ原文内容:Dearusersanddevelopers,Afteralmosttenyearsofdevelopment,wehavetheregrettoannouncethatwewillputanendtoourTheanodevelopmentafter......
  • 策略模式的代码实践示例
    一、定义策略模式,针对每一个不同的类型,调用具有共同接口的不同实现类,从而使得它们可以相互替换。策略模式,针对实现同一接口的不同的类,采用不同的策略。比如,面对高级会员、初级会员会采用不同的折扣。策略模式,可以避免大量的if和else。二、角色策略模式涉及到三个角色:●......
  • Asp-Net-Core学习笔记:3.使用SignalR实时通信框架开发聊天室
    SignalR牛刀小试在MVP杨老师的博客里看到这么个东西,我还以为是NetCore3才推出的新玩意,原来是已经有很多年的历史了,那看来还是比较成熟的一个技术了。简介SignalR是一个.NETCore/.NETFramework的开源实时框架,SignalR的可使用WebSocket,ServerSentEvents和LongPolling......