10601-hws/HW 6/hw6/python/cnn_lenet.py
def init_convnet(layers):
  """Initialize parameters of each layer in LeNet

  Args:
    layers: a dictionary that defines LeNet

  Returns:
    params: a dictionary stores initialized parameters
  """

  params = {}

  h = layers[1]['height']
  w = layers[1]['width']
  c = layers[1]['channel']

  # Starts with second layer, first layer is data layer
  np.random.seed(100000)
  for i in range(2, len(layers)+1):
    params[i-1] = {}
    if layers[i]['type'] == 'CONV':
      scale = np.sqrt(3./(h*w*c))
      weight = np.random.rand(
        layers[i]['k'] * layers[i]['k'] * c / layers[i]['group'],
        layers[i]['num']
      )

      params[i-1]['w'] = 2*scale*weight - scale
      params[i-1]['b'] = np.zeros(layers[i]['num'])
      # update h, w and c, used in next layer
      h = (h + 2*layers[i]['pad'] - layers[i]['k']) / layers[i]['stride'] + 1
      w = (w + 2*layers[i]['pad'] - layers[i]['k']) / layers[i]['stride'] + 1
      c = layers[i]['num']
    elif layers[i]['type'] == 'POOLING':
      h = (h - layers[i]['k']) / layers[i]['stride'] + 1
      w = (w - layers[i]['k']) / layers[i]['stride'] + 1
      params[i-1]['w'] = np.array([])
      params[i-1]['b'] = np.array([])
    elif layers[i]['type'] == 'IP' and layers[i]['init_type'] == 'gaussian':
      scale = np.sqrt(3./(h*w*c))
      params[i-1]['w'] = scale*np.random.randn(h*w*c, layers[i]['num'])
      params[i-1]['b'] = np.zeros(layers[i]['num'])
      h = 1
      w = 1
      c = layers[i]['num']
    elif layers[i]['type'] == 'IP' and layers[i]['init_type'] == 'uniform':
      scale = np.sqrt(3./(h*w*c))
      params[i-1]['w'] = 2*scale*np.random.rand(h*w*c, layers[i]['num']) - scale
      params[i-1]['b'] = np.zeros(layers[i]['num'])
      h = 1
      w = 1
      c = layers[i]['num']
    elif layers[i]['type'] == 'RELU':
      params[i-1]['w'] = np.array([])
      params[i-1]['b'] = np.array([])
    elif layers[i]['type'] == 'ELU':
      params[i-1]['w'] = np.array([])
      params[i-1]['b'] = np.array([])
    elif layers[i]['type'] == 'LOSS':
      scale = np.sqrt(3./(h*w*c))
      num = layers[i]['num']
      # last layer is K-1
      params[i-1]['w'] = 2*scale*np.random.rand(h*w*c, num-1) - scale
      params[i-1]['b'] = np.zeros(num - 1)
      h = 1
      w = 1
      c = layers[i]['num']
  return params
Initialize parameters of each layer in LeNet

  Args:
    layers: a dictionary that defines LeNet

  Returns:
    params: a dictionary stores initialized parameters
  
Initialize parameters of each layer in LeNet
Initialize parameters of each layer in LeNet
def init_convnet(layers):
  params = {}
  h = layers[1]['height']
  w = layers[1]['width']
  c = layers[1]['channel']
  np.random.seed(100000)
  for i in range(2, len(layers)+1):
    params[i-1] = {}
    if layers[i]['type'] == 'CONV':
      scale = np.sqrt(3./(h*w*c))
      weight = np.random.rand(
        layers[i]['k'] * layers[i]['k'] * c / layers[i]['group'],
        layers[i]['num']
      )
      params[i-1]['w'] = 2*scale*weight - scale
      params[i-1]['b'] = np.zeros(layers[i]['num'])
      h = (h + 2*layers[i]['pad'] - layers[i]['k']) / layers[i]['stride'] + 1
      w = (w + 2*layers[i]['pad'] - layers[i]['k']) / layers[i]['stride'] + 1
      c = layers[i]['num']
    elif layers[i]['type'] == 'POOLING':
      h = (h - layers[i]['k']) / layers[i]['stride'] + 1
      w = (w - layers[i]['k']) / layers[i]['stride'] + 1
      params[i-1]['w'] = np.array([])
      params[i-1]['b'] = np.array([])
    elif layers[i]['type'] == 'IP' and layers[i]['init_type'] == 'gaussian':
      scale = np.sqrt(3./(h*w*c))
      params[i-1]['w'] = scale*np.random.randn(h*w*c, layers[i]['num'])
      params[i-1]['b'] = np.zeros(layers[i]['num'])
      h = 1
      w = 1
      c = layers[i]['num']
    elif layers[i]['type'] == 'IP' and layers[i]['init_type'] == 'uniform':
      scale = np.sqrt(3./(h*w*c))
      params[i-1]['w'] = 2*scale*np.random.rand(h*w*c, layers[i]['num']) - scale
      params[i-1]['b'] = np.zeros(layers[i]['num'])
      h = 1
      w = 1
      c = layers[i]['num']
    elif layers[i]['type'] == 'RELU':
      params[i-1]['w'] = np.array([])
      params[i-1]['b'] = np.array([])
    elif layers[i]['type'] == 'ELU':
      params[i-1]['w'] = np.array([])
      params[i-1]['b'] = np.array([])
    elif layers[i]['type'] == 'LOSS':
      scale = np.sqrt(3./(h*w*c))
      num = layers[i]['num']
      params[i-1]['w'] = 2*scale*np.random.rand(h*w*c, num-1) - scale
      params[i-1]['b'] = np.zeros(num - 1)
      h = 1
      w = 1
      c = layers[i]['num']
  return params
def init_convnet ( layers ) : params = { } h = layers [ 1 ] [ 'height' ] w = layers [ 1 ] [ 'width' ] c = layers [ 1 ] [ 'channel' ] np . random . seed ( 100000 ) for i in range ( 2 , len ( layers ) + 1 ) : params [ i - 1 ] = { } if layers [ i ] [ 'type' ] == 'CONV' : scale = np . sqrt ( 3. / ( h * w * c ) ) weight = np . random . rand ( layers [ i ] [ 'k' ] * layers [ i ] [ 'k' ] * c / layers [ i ] [ 'group' ] , layers [ i ] [ 'num' ] ) params [ i - 1 ] [ 'w' ] = 2 * scale * weight - scale params [ i - 1 ] [ 'b' ] = np . zeros ( layers [ i ] [ 'num' ] ) h = ( h + 2 * layers [ i ] [ 'pad' ] - layers [ i ] [ 'k' ] ) / layers [ i ] [ 'stride' ] + 1 w = ( w + 2 * layers [ i ] [ 'pad' ] - layers [ i ] [ 'k' ] ) / layers [ i ] [ 'stride' ] + 1 c = layers [ i ] [ 'num' ] elif layers [ i ] [ 'type' ] == 'POOLING' : h = ( h - layers [ i ] [ 'k' ] ) / layers [ i ] [ 'stride' ] + 1 w = ( w - layers [ i ] [ 'k' ] ) / layers [ i ] [ 'stride' ] + 1 params [ i - 1 ] [ 'w' ] = np . array ( [ ] ) params [ i - 1 ] [ 'b' ] = np . array ( [ ] ) elif layers [ i ] [ 'type' ] == 'IP' and layers [ i ] [ 'init_type' ] == 'gaussian' : scale = np . sqrt ( 3. / ( h * w * c ) ) params [ i - 1 ] [ 'w' ] = scale * np . random . randn ( h * w * c , layers [ i ] [ 'num' ] ) params [ i - 1 ] [ 'b' ] = np . zeros ( layers [ i ] [ 'num' ] ) h = 1 w = 1 c = layers [ i ] [ 'num' ] elif layers [ i ] [ 'type' ] == 'IP' and layers [ i ] [ 'init_type' ] == 'uniform' : scale = np . sqrt ( 3. / ( h * w * c ) ) params [ i - 1 ] [ 'w' ] = 2 * scale * np . random . rand ( h * w * c , layers [ i ] [ 'num' ] ) - scale params [ i - 1 ] [ 'b' ] = np . zeros ( layers [ i ] [ 'num' ] ) h = 1 w = 1 c = layers [ i ] [ 'num' ] elif layers [ i ] [ 'type' ] == 'RELU' : params [ i - 1 ] [ 'w' ] = np . array ( [ ] ) params [ i - 1 ] [ 'b' ] = np . array ( [ ] ) elif layers [ i ] [ 'type' ] == 'ELU' : params [ i - 1 ] [ 'w' ] = np . array ( [ ] ) params [ i - 1 ] [ 'b' ] = np . array ( [ ] ) elif layers [ i ] [ 'type' ] == 'LOSS' : scale = np . sqrt ( 3. / ( h * w * c ) ) num = layers [ i ] [ 'num' ] params [ i - 1 ] [ 'w' ] = 2 * scale * np . random . rand ( h * w * c , num - 1 ) - scale params [ i - 1 ] [ 'b' ] = np . zeros ( num - 1 ) h = 1 w = 1 c = layers [ i ] [ 'num' ] return params
Initialize parameters of each layer in LeNet
Initialize parameters of each layer in LeNet
"""Initialize parameters of each layer in LeNet

  Args:
    layers: a dictionary that defines LeNet

  Returns:
    params: a dictionary stores initialized parameters
  """ # Starts with second layer, first layer is data layer # update h, w and c, used in next layer # last layer is K-1
a dictionary stores initialized parameters
a dictionary stores initialized parameters
params
layers
a dictionary that defines LeNet
a dictionary that defines LeNet