n

import numpy as np

from cs231n.layers import *
from cs231n.fast_layers import *
from cs231n.layer_utils import * class ThreeLayerConvNet(object):
"""
A three-layer convolutional network with the following architecture: conv - relu - 2x2 max pool - affine - relu - affine - softmax The network operates on minibatches of data that have shape (N, C, H, W)
consisting of N images, each with height H and width W and with C input
channels.
""" def __init__(self, input_dim=(3, 32, 32), num_filters=32, filter_size=7,
hidden_dim=100, num_classes=10, weight_scale=1e-3, reg=0.0,
dtype=np.float32):
"""
Initialize a new network. Inputs:
- input_dim: Tuple (C, H, W) giving size of input data
- num_filters: Number of filters to use in the convolutional layer
- filter_size: Size of filters to use in the convolutional layer
- hidden_dim: Number of units to use in the fully-connected hidden layer
- num_classes: Number of scores to produce from the final affine layer.
- weight_scale: Scalar giving standard deviation for random initialization
of weights.
- reg: Scalar giving L2 regularization strength
- dtype: numpy datatype to use for computation.
"""
C,H,W=input_dim self.params = {}
self.reg = reg
self.dtype = dtype
self.params['W1']=np.random.randn(num_filters,C,filter_size,filter_size)*weight_scale
self.params['b1']=np.zeros(num_filters,)
self.params['W2']=np.random.randn(num_filters*H*W/4,hidden_dim)*weight_scale
self.params['b2']=np.zeros(hidden_dim,)
self.params['W3']=np.random.randn(hidden_dim,num_classes)*weight_scale
self.params['b3']=np.zeros(num_classes,)
# why randn needs int while seros needs tuple!!!!
for k, v in self.params.iteritems():
self.params[k] = v.astype(dtype) def loss(self, X, y=None):
"""
Evaluate loss and gradient for the three-layer convolutional network. Input / output: Same API as TwoLayerNet in fc_net.py.
"""
W1, b1 = self.params['W1'], self.params['b1']
W2, b2 = self.params['W2'], self.params['b2']
W3, b3 = self.params['W3'], self.params['b3'] # pass conv_param to the forward pass for the convolutional layer
filter_size = W1.shape[2]
conv_param = {'stride': 1, 'pad': (filter_size - 1) / 2} # pass pool_param to the forward pass for the max-pooling layer
pool_param = {'pool_height': 2, 'pool_width': 2, 'stride': 2} scores = None
out1,cache1=conv_relu_pool_forward(X,W1,b1,conv_param,pool_param) out=out1.reshape(out1.shape[0],-1) out,cache2=affine_relu_forward(out,W2,b2) scores,cache3=affine_forward(out,W3,b3) if y is None:
return scores loss, grads = 0, {}
loss,dout=softmax_loss(scores,y) loss+=self.reg*0.5*np.sum(W3**2)
loss+=self.reg*0.5*np.sum(W2**2)
loss+=self.reg*0.5*np.sum(W1**2) dout,grads['W3'],grads['b3']=affine_backward(dout,cache3)
grads['W3']+=W3*self.reg dout,grads['W2'],grads['b2']=affine_relu_backward(dout,cache2)
grads['W2']+=W2*self.reg dout=dout.reshape(*out1.shape)
dout,grads['W1'],grads['b1']=conv_relu_pool_backward(dout,cache1)
grads['W1']+=W1*self.reg ############################################################################
# END OF YOUR CODE #
############################################################################ return loss, grads pass

  

n

cnn.py cs231n的更多相关文章

  1. fc_net.py cs231n

    n如果有错误,欢迎指出,不胜感激 import numpy as np from cs231n.layers import * from cs231n.layer_utils import * cla ...

  2. layers.py cs231n

    如果有错误,欢迎指出,不胜感激. import numpy as np def affine_forward(x, w, b): 第一个最简单的 affine_forward简单的前向传递,返回 ou ...

  3. optim.py cs231n

    n如果有错误,欢迎指出,不胜感激 import numpy as np """ This file implements various first-order upda ...

  4. [Keras] mnist with cnn

    典型的卷积神经网络. Keras傻瓜式读取数据:自动下载,自动解压,自动加载. # X_train: array([[[[ 0., 0., 0., ..., 0., 0., 0.], [ 0., 0. ...

  5. 卷积神经网络CNN(Convolutional Neural Networks)没有原理只有实现

    零.说明: 本文的所有代码均可在 DML 找到,欢迎点星星. 注.CNN的这份代码非常慢,基本上没有实际使用的可能,所以我只是发出来,代表我还是实践过而已 一.引入: CNN这个模型实在是有些年份了, ...

  6. 深度学习之卷积神经网络(CNN)详解与代码实现(一)

    卷积神经网络(CNN)详解与代码实现 本文系作者原创,转载请注明出处:https://www.cnblogs.com/further-further-further/p/10430073.html 目 ...

  7. python,tensorflow,CNN实现mnist数据集的训练与验证正确率

    1.工程目录 2.导入data和input_data.py 链接:https://pan.baidu.com/s/1EBNyNurBXWeJVyhNeVnmnA 提取码:4nnl 3.CNN.py i ...

  8. 基于MNIST数据的卷积神经网络CNN

    基于tensorflow使用CNN识别MNIST 参数数量:第一个卷积层5x5x1x32=800个参数,第二个卷积层5x5x32x64=51200个参数,第三个全连接层7x7x64x1024=3211 ...

  9. 【转载】 深度学习之卷积神经网络(CNN)详解与代码实现(一)

    原文地址: https://www.cnblogs.com/further-further-further/p/10430073.html ------------------------------ ...

随机推荐

  1. [转]Visual Studio 2010单元测试(2)--运行测试并查看代码覆盖率

    Visual Studio 2010 单元测试--运行测试并查看代码覆盖率 运行测试并查看代码覆盖率对程序集中的代码运行测试时,可以通过收集代码覆盖率数据来查看正在测试的项目代码部分. 运行测试并查看 ...

  2. 模板:数位DP

    第一次听说dp还有模板的... 当然你要是记忆化搜索的话,就可以有一些套路 这是一个伪代码: LL Dfs(LL now,限制,LL top){ if(!now) return 判断条件; if(!t ...

  3. 2、node服务器

    一.简单的node服务器搭建 1.首先新建一个名为server.js的文件(文件名随意,后缀名必须是.js) 2.粘贴进文件以下内容 //引入http模块 const http = require(& ...

  4. SQLSERVER 数据库管理员的专用连接DAC

    DAC:Dedicated Admin Connection 当SQL Server因系统资源不足,或其它异常导致无法建立数据库连接时, 可以使用系统预留的DAC连接到数据库,进行一些问题诊断和故障排 ...

  5. Jeecms6中后台控制层Action如何将值传入前台视图层模板中的?

    转载:https://blog.csdn.net/wsm201005030226/article/details/44343069     Jeecms后台控制层如何传值到前台freemarker的? ...

  6. hibernate4多对多Use of @OneToMany or @ManyToMany targeting an unmapped class:

    出错之后先查了一下,大家有 @Entity 导错包的,不过我这里没错 import javax.persistence.Entity; 就是这个 还有的是没有注解@Table的,我这里也是没问题的 我 ...

  7. 008-python绘制五个五角星

    操纵海龟绘图有着许多的命令,这些命令可以划分为两种:一种为运动命令,一种为画笔控制命令 1. 运动命令: forward(degree)  #向前移动距离degree代表距离 backward(deg ...

  8. Linux中如何安装mysql数据库

    安装mysql 1.解压源码压缩包 如果服务器可以上网也可以采用在线安装方式,在线安装操作简单具体见下面在线安装步骤 进入源码压缩包所在目录输入#tar -zxvf mysql-5.6.17-linu ...

  9. 通过游戏学python 3.6 第一季 第四章 实例项目 猜数字游戏--核心代码--猜测次数--随机函数和屏蔽错误代码--优化代码及注释 可复制直接使用 娱乐 可封装 函数

    #猜数字--核心代码--猜测次数--随机函数和屏蔽错误代码---优化代码及注释 #!usr/bin/env python #-*-coding:utf-8-*- #QQ124111294 import ...

  10. TZ_03_mybatis的注解开发

    1.一对多的注解开发 1>需求通过查询所有的用户,并且找到该用户的所有账户(使用延迟加载模式) @Select("select * from user") //sql语句查询 ...