cnn.py cs231n

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

时间: 2024-10-12 20:35:20

cnn.py cs231n的相关文章

layers.py cs231n

如果有错误,欢迎指出,不胜感激. import numpy as np def affine_forward(x, w, b): 第一个最简单的 affine_forward简单的前向传递,返回 out,cache """ Computes the forward pass for an affine (fully-connected) layer. The input x has shape (N, d_1, ..., d_k) and contains a minibat

optim.py cs231n

n如果有错误,欢迎指出,不胜感激 import numpy as np """ This file implements various first-order update rules that are commonly used for training neural networks. Each update rule accepts current weights and the gradient of the loss with respect to those w

[Keras] mnist with cnn

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

DeepLearning tutorial(6)易用的深度学习框架Keras简介

之前我一直在使用Theano,前面五篇Deeplearning相关的文章也是学习Theano的一些笔记,当时已经觉得Theano用起来略显麻烦,有时想实现一个新的结构,就要花很多时间去编程,所以想过将代码模块化,方便重复使用,但因为实在太忙没有时间去做.最近发现了一个叫做Keras的框架,跟我的想法不谋而合,用起来特别简单,适合快速开发.(其实还有很多其他的深度学习框架都是比较容易用的.) 1. Keras简介 Keras是基于Theano的一个深度学习框架,它的设计参考了Torch,用Pyth

DeepLearning tutorial(7)深度学习框架Keras的使用-进阶

上一篇文章总结了Keras的基本使用方法,相信用过的同学都会觉得不可思议,太简洁了.十多天前,我在github上发现这个框架的时候,关注Keras的人还比较少,这两天无论是github还是微薄,都看到越来越多的人关注和使用Keras.所以这篇文章就简单地再介绍一下Keras的使用,方便各位入门. 主要包括以下三个内容: 训练CNN并保存训练好的模型. 将CNN用于特征提取,用提取出来的特征训练SVM. 可视化CNN卷积层后的特征图. 仍然以Mnist为例,代码中用的Mnist数据到这里下载 ht

卷积层,池化层等,前向/反向传播原理讲解

今天闲来无事,考虑到以前都没有好好研究过卷积层.池化层等等的前向/反向传播的原理,所以今天就研究了一下,参考了一篇微信好文,讲解如下: 参考链接:https://www.zybuluo.com/hanbingtao/note/485480 https://github.com/hanbt/learn_dl/blob/master/cnn.py 一.卷积层 (1)首先是卷积神经网络中的卷积操作: 计算公式为: 注意上式的使用场景:stride = 1 , channel = 1 我们可以将其扩展到

[Tensorflow]激励函数tf.nn.relu样例

代码: import tensorflow as tf import numpy as np ### 定义添加神经网络层函数 START ### def add_layer(inputs,in_size,out_size,activation_function=None): """描述: 添加神经网络层函数. :param inputs: 输入神经层 :param in_size: 输入神经层的神经元个数 :param out_size: 输出神经层的神经元个数 :param

ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (60000, 28, 28)

报错 Traceback (most recent call last): File "D:/PyCharm 5.0.3/WorkSpace/3.Keras/3.构建各种神经网络/3.CNN.py", line 46, in <module> model.fit(x_train, y_train, epochs=1, batch_size=32) File "D:\Anaconda3\lib\site-packages\keras\engine\training.

可变形卷积 deformable convolution 学习记录

Deformable ConvNets v1: 论文地址:https://github.com/chengdazhi/Deformable-Convolution-V2-PyTorch 工程地址:https://github.com/felixlaumon/deform-conv 论文地址: Deformable ConvNets v2: More Deformable, Better Results 工程地址:https://github.com/chengdazhi/Deformable-C