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. cdh_hadoop下载地址

    http://archive.cloudera.com/cdh5/cdh/5/

  2. 论文翻译——R-CNN(目标检测开山之作)

    R-CNN论文翻译 <Rich feature hierarchies for accurate object detection and semantic segmentation> 用 ...

  3. C++项目使用的开源库记录

    日志库:log4cplus 网络库:libcurl xml库:tinyxml 多媒体库:lib555 视频解码库:ffmpeg 用opencv的VideoCapture类可以直接打开rtsp视频,所以 ...

  4. Hibernate继承注解

    hibernate应用中,继承的用途或目的主要有两点: 组件化:故明思义,把重复性的代码抽取成组件,以便重用和维护.hibernate应用中,一些重复的字段,重复的映射配置,就需要抽取成组件. 多态性 ...

  5. 使用poco再次封装redis

    为方便程序对redis操作,我对poco的redis进行了再次封装,主要是针对自己应用需要的部分. 开发工具:netbean 系统环境:centos7 poco版本: poco-1.9.0-all 其 ...

  6. 通过url方式传递中文参数时出现的中文乱码问题的处理

    1.通过url将参数[会有中文的情况]传递到另外一个页面: window.location.href = "/XXXX/XXXX?searchcontent=" + Content ...

  7. 2019-7-15-win10-uwp-在笔迹开始书写拿到书写移动事件

    title author date CreateTime categories win10 uwp 在笔迹开始书写拿到书写移动事件 lindexi 2019-7-15 8:58:5 +0800 201 ...

  8. Leetcode264. Ugly Number II丑数2

    编写一个程序,找出第 n 个丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例: 输入: n = 10 输出: 12 解释: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 ...

  9. 01Redis入门指南笔记(简介、安装、配置)

    一:简介 Redis是一个开源的高性能key-value数据库.Redis是Remote DIctionary Server(远程字典服务器)的缩写,它以字典结构存储数据,并允许其他应用通过TCP协议 ...

  10. c++设计模式:代理模式

    代理模式的实现和visitor实现有一曲同工之妙.这里就不多说了,也是实现set_delegate接口,以及回调函数处理接口. 主要区别在于: visitor主要是把信息回调之后就不管了. 代理模式主 ...