n

  1. import numpy as np
  2.  
  3. from cs231n.layers import *
  4. from cs231n.fast_layers import *
  5. from cs231n.layer_utils import *
  6.  
  7. class ThreeLayerConvNet(object):
  8. """
  9. A three-layer convolutional network with the following architecture:
  10.  
  11. conv - relu - 2x2 max pool - affine - relu - affine - softmax
  12.  
  13. The network operates on minibatches of data that have shape (N, C, H, W)
  14. consisting of N images, each with height H and width W and with C input
  15. channels.
  16. """
  17.  
  18. def __init__(self, input_dim=(3, 32, 32), num_filters=32, filter_size=7,
  19. hidden_dim=100, num_classes=10, weight_scale=1e-3, reg=0.0,
  20. dtype=np.float32):
  21. """
  22. Initialize a new network.
  23.  
  24. Inputs:
  25. - input_dim: Tuple (C, H, W) giving size of input data
  26. - num_filters: Number of filters to use in the convolutional layer
  27. - filter_size: Size of filters to use in the convolutional layer
  28. - hidden_dim: Number of units to use in the fully-connected hidden layer
  29. - num_classes: Number of scores to produce from the final affine layer.
  30. - weight_scale: Scalar giving standard deviation for random initialization
  31. of weights.
  32. - reg: Scalar giving L2 regularization strength
  33. - dtype: numpy datatype to use for computation.
  34. """
  35. C,H,W=input_dim
  36.  
  37. self.params = {}
  38. self.reg = reg
  39. self.dtype = dtype
  40. self.params['W1']=np.random.randn(num_filters,C,filter_size,filter_size)*weight_scale
  41. self.params['b1']=np.zeros(num_filters,)
  42. self.params['W2']=np.random.randn(num_filters*H*W/4,hidden_dim)*weight_scale
  43. self.params['b2']=np.zeros(hidden_dim,)
  44. self.params['W3']=np.random.randn(hidden_dim,num_classes)*weight_scale
  45. self.params['b3']=np.zeros(num_classes,)
  46. # why randn needs int while seros needs tuple!!!!
  47. for k, v in self.params.iteritems():
  48. self.params[k] = v.astype(dtype)
  49.  
  50. def loss(self, X, y=None):
  51. """
  52. Evaluate loss and gradient for the three-layer convolutional network.
  53.  
  54. Input / output: Same API as TwoLayerNet in fc_net.py.
  55. """
  56. W1, b1 = self.params['W1'], self.params['b1']
  57. W2, b2 = self.params['W2'], self.params['b2']
  58. W3, b3 = self.params['W3'], self.params['b3']
  59.  
  60. # pass conv_param to the forward pass for the convolutional layer
  61. filter_size = W1.shape[2]
  62. conv_param = {'stride': 1, 'pad': (filter_size - 1) / 2}
  63.  
  64. # pass pool_param to the forward pass for the max-pooling layer
  65. pool_param = {'pool_height': 2, 'pool_width': 2, 'stride': 2}
  66.  
  67. scores = None
  68. out1,cache1=conv_relu_pool_forward(X,W1,b1,conv_param,pool_param)
  69.  
  70. out=out1.reshape(out1.shape[0],-1)
  71.  
  72. out,cache2=affine_relu_forward(out,W2,b2)
  73.  
  74. scores,cache3=affine_forward(out,W3,b3)
  75.  
  76. if y is None:
  77. return scores
  78.  
  79. loss, grads = 0, {}
  80. loss,dout=softmax_loss(scores,y)
  81.  
  82. loss+=self.reg*0.5*np.sum(W3**2)
  83. loss+=self.reg*0.5*np.sum(W2**2)
  84. loss+=self.reg*0.5*np.sum(W1**2)
  85.  
  86. dout,grads['W3'],grads['b3']=affine_backward(dout,cache3)
  87. grads['W3']+=W3*self.reg
  88.  
  89. dout,grads['W2'],grads['b2']=affine_relu_backward(dout,cache2)
  90. grads['W2']+=W2*self.reg
  91.  
  92. dout=dout.reshape(*out1.shape)
  93. dout,grads['W1'],grads['b1']=conv_relu_pool_backward(dout,cache1)
  94. grads['W1']+=W1*self.reg
  95.  
  96. ############################################################################
  97. # END OF YOUR CODE #
  98. ############################################################################
  99.  
  100. return loss, grads
  101.  
  102. 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. OSG实现正八面体剖分成球

    #include<Windows.h> #include<osg/Node> #include<osg/Geode> #include<osg/Group&g ...

  2. 为互联网业务而生:阿里云全球首发云Cassandra服务!

    引言:十年沉淀.全球宽表排名第一.阿里云首发云Cassandra服务 ApsaraDB for Cassandra是基于开源Apache Cassandra,融合阿里云数据库DBaaS能力的分布式No ...

  3. centos7 盘符变动 绑定槽位

    服务器下的硬盘主有机械硬盘.固态硬盘以及raid阵列,通常内核分配盘符的顺序是/dev/sda./dev/sdb… ….在系统启动过程中,内核会按照扫描到硬盘的顺序分配盘符(先分配直通的,再分配阵列) ...

  4. jeecms v9.3 has a stroed xss vulnerability

    转载:https://blog.csdn.net/libieme/article/details/83588929 jeecms v9.3 has a stroed xss vulnerability ...

  5. ajax返回后台编译时都对,返回error

    首先看看你的dataType:‘json’  类型是否与后台获取的类型一直.特别是json的格式对不对. 第二: 红括号里的有没有加

  6. 浅谈Python小数据池

    什么是小数据池 小数据池是python中提高效率的一种方式,固定数据类型的相同值使用同一内存地址. id 用于获取开辟空间的内存地址 代码块 一个文件,一个模块,一个函数,一个类,终端中的每一行代码都 ...

  7. 未加星标 Linux磁盘下查看I/O磁盘的性能

    iostat查看linux硬盘IO性能 rrqm/s:每秒进行merge的读操作数目.即delta(rmerge)/s wrqm/s:每秒进行merge的写操作数目.即delta(wmerge)/s ...

  8. spring源码学习之容器的扩展(二)

    六 BeanFactory的后处理BeanFactory作为spring容器的基础,用于存放所有已经加载的bean,为了保证程序上的高扩展性,spring针对BeanFactory做了大量的扩展,比如 ...

  9. Django项目:CRM(客户关系管理系统)--39--31PerfectCRM实现King_admin编辑多对多限制

    readonly_fields = ('qq', 'consultant','tags',) # 不可修改 # forms.py # ————————19PerfectCRM实现King_admin数 ...

  10. bzoj 1034 [ZJOI2008]泡泡堂BNB——贪心

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1034 原来觉得和 bzoj4977跳伞求生 有点像(虽然还没做). 所以对于a[ ]从小到大 ...