def __init__(self, input_dim=(1, 28, 28), conv_param={'filter_num': 30, 'filter_size': 5, 'pad': 0, 'stride': 1}, hidden_size=100, output_size=10, weight_init_std=0.01): filter_num = conv_param ['filter_num'] filter_size = conv_param ['filter_size'] filter_pad = conv_param ['pad'] filter_stride = conv_param ['stride'] input_size = input_dim [1] conv_output_size = (input_size - filter_size + 2 * filter_pad) / filter_stride + 1 pool_output_size = int (filter_num * (conv_output_size / 2) * (conv_output_size / 2)) # 重みの初期化 self.params = {} self.params ['W1'] = weight_init_std * np.random.randn (filter_num, input_dim [0], filter_size, filter_size) self.params ['b1'] = np.zeros (filter_num) self.params ['W2'] = weight_init_std * np.random.randn (pool_output_size, hidden_size) self.params ['b2'] = np.zeros (hidden_size) self.params ['W3'] = weight_init_std * np.random.randn (hidden_size, output_size) self.params ['b3'] = np.zeros (output_size) # レイヤの生成 self.layers = OrderedDict () self.layers ['Conv1'] = layers.Convolution (self.params ['W1'], self.params ['b1'], conv_param ['stride'], conv_param ['pad']) self.layers ['Relu1'] = layers.Relu () self.layers ['Pool1'] = layers.Pooling (pool_h=2, pool_w=2, stride=2) self.layers ['Affine1'] = layers.Affine (self.params ['W2'], self.params ['b2']) self.layers ['Relu2'] = layers.Relu () self.layers ['Affine2'] = layers.Affine (self.params ['W3'], self.params ['b3']) self.last_layer = layers.SoftmaxWithLoss ()
def __init__(self, input_dim=(1, 28, 28), conv_param_1={ 'filter_num': 10, 'filter_size': 7, 'pad': 1, 'stride': 1 }, conv_param_2={ 'filter_num': 20, 'filter_size': 3, 'pad': 1, 'stride': 1 }, hidden_size=50, output_size=10, weight_init_std=0.01): conv_output_size_1 = (input_dim[1] - conv_param_1['filter_size'] + 2 * conv_param_1['pad']) / conv_param_1['stride'] + 1 conv_output_size_2 = ( conv_output_size_1 - conv_param_2['filter_size'] + 2 * conv_param_2['pad']) / conv_param_2['stride'] + 1 pool_output_size = int(conv_param_2['filter_num'] * (conv_output_size_2 / 2) * (conv_output_size_2 / 2)) # 重みの初期化 self.params = {} self.params['W1'] = weight_init_std * np.random.randn( conv_param_1['filter_num'], input_dim[0], conv_param_1['filter_size'], conv_param_1['filter_size']) self.params['b1'] = np.zeros(conv_param_1['filter_num']) self.params['W2'] = weight_init_std * np.random.randn( conv_param_2['filter_num'], conv_param_1['filter_num'], conv_param_2['filter_size'], conv_param_2['filter_size']) self.params['b2'] = np.zeros(conv_param_2['filter_num']) self.params['W3'] = weight_init_std * np.random.randn( pool_output_size, hidden_size) self.params['b3'] = np.zeros(hidden_size) self.params['W4'] = weight_init_std * np.random.randn( hidden_size, output_size) self.params['b4'] = np.zeros(output_size) # レイヤの生成 self.layers = OrderedDict() self.layers['Conv1'] = layers.Convolution(self.params['W1'], self.params['b1'], conv_param_1['stride'], conv_param_1['pad']) self.layers['Relu1'] = layers.Relu() self.layers['Conv2'] = layers.Convolution(self.params['W2'], self.params['b2'], conv_param_2['stride'], conv_param_2['pad']) self.layers['Relu2'] = layers.Relu() self.layers['Pool1'] = layers.Pooling(pool_h=2, pool_w=2, stride=2) self.layers['Affine1'] = layers.Affine(self.params['W3'], self.params['b3']) self.layers['Relu3'] = layers.Relu() self.layers['Affine2'] = layers.Affine(self.params['W4'], self.params['b4']) self.last_layer = layers.SoftmaxWithLoss()
def __init__(self, input_size, hidden_size, output_size, weight_init_std=0.01): # 가중치 초기화 self.params = {} self.params['W1'] = weight_init_std * np.random.randn( input_size, hidden_size) self.params['b1'] = np.zeros(hidden_size) self.params['W2'] = weight_init_std * np.random.randn( hidden_size, output_size) self.params['b2'] = np.zeros(output_size) # print(self.params) # 계층 생성 (Affine - Relu - Affine - SoftmaxWithLoss) # 신경망 계층을 OrderedDict(순서가 있는 딕셔너리)에 보관하는 점이 중요 self.layers = OrderedDict() self.layers['Affine1'] = layers.Affine(self.params['W1'], self.params['b1']) self.layers['Relu1'] = layers.Relu() self.layers['Affine2'] = layers.Affine(self.params['W2'], self.params['b2']) # 마지막 출력 층 생성 self.lastLayer = layers.SoftmaxWithLoss()
def __init__(self, size_in, list_size_hidden, size_out, lambda_l2=0., ratio_dropout=None): if isinstance(list_size_hidden, int): list_size_hidden = [list_size_hidden] self.size_units = [size_in] + list_size_hidden + [size_out] self.lambda_l2 = lambda_l2 self.params = {} self.layers = OrderedDict() for i in range(len(self.size_units) - 1): affine, weight, bias, relu, dropout =\ [k + str(i + 1) for k in ('Affine', 'W', 'b', 'Relu', 'Dropout')] scale = np.sqrt(2.0 / self.size_units[i]) self.params[weight] =\ scale * np.random.randn(self.size_units[i], self.size_units[i+1]) self.params[bias] = np.zeros(self.size_units[i + 1]) self.layers[affine] = clay.Affine(self.params[weight], self.params[bias]) if ratio_dropout is not None: self.layers[dropout] = clay.Dropout(ratio_dropout) if i < len(self.size_units) - 2: self.layers[relu] = clay.Relu() else: self.lastLayer = clay.SoftmaxWithLoss()
def __init__(self, input_size, hidden_size_list, output_size, activation='relu', weight_init_std='relu', weight_decay_lambda=0, use_dropout=False, dropout_ratio=0.5, use_batchnorm=False): self.input_size = input_size self.output_size = output_size self.hidden_size_list = hidden_size_list self.hidden_layer_num = len(hidden_size_list) self.use_dropout = use_dropout self.weight_decay_lambda = weight_decay_lambda self.use_batchnorm = use_batchnorm self.params = {} # 重みの初期化 self.__init_weight(weight_init_std) # レイヤの生成 activation_layer = {'sigmoid': layers.Sigmoid, 'relu': layers.Relu} self.layers = OrderedDict() for idx in range(1, self.hidden_layer_num + 1): self.layers['Affine' + str(idx)] = layers.Affine( self.params['W' + str(idx)], self.params['b' + str(idx)]) if self.use_batchnorm: self.params['gamma' + str(idx)] = np.ones( hidden_size_list[idx - 1]) self.params['beta' + str(idx)] = np.zeros( hidden_size_list[idx - 1]) self.layers['BatchNorm' + str(idx)] = layers.BatchNormalization( self.params['gamma' + str(idx)], self.params['beta' + str(idx)]) self.layers['Activation_function' + str(idx)] = activation_layer[activation]() if self.use_dropout: self.layers['Dropout' + str(idx)] = layers.Dropout(dropout_ratio) idx = self.hidden_layer_num + 1 self.layers['Affine' + str(idx)] = layers.Affine( self.params['W' + str(idx)], self.params['b' + str(idx)]) self.last_layer = layers.SoftmaxWithLoss()
def __init__(self, size_in, size_hidden, size_out, std_init_weight=0.01): self.params = {} self.params['W1'] = std_init_weight * np.random.randn( size_in, size_hidden) self.params['b1'] = np.zeros(size_hidden) self.params['W2'] = std_init_weight * np.random.randn( size_hidden, size_out) self.params['b2'] = np.zeros(size_out) self.layers = OrderedDict() self.layers['Affine1'] = clay.Affine(self.params['W1'], self.params['b1']) self.layers['Relu1'] = clay.Relu() self.layers['Affine2'] = clay.Affine(self.params['W2'], self.params['b2']) self.lastLayer = clay.SoftmaxWithLoss()
def __init__(self, input_size, hidden_size_list, output_size, activation='relu', weight_init_std='relu'): self.input_size = input_size self.output_size = output_size self.hidden_size_list = hidden_size_list self.hidden_layer_num = len(hidden_size_list) self.params = {} # 重みの初期化 self.__init_weight(weight_init_std) # レイヤの生成, sigmoidとreluのみ扱う activation_layer = {'sigmoid': layers.Sigmoid, 'relu': layers.Relu} self.layers = OrderedDict() # 追加した順番に格納 for idx in range(1, self.hidden_layer_num+1): self.layers['Affine' + str(idx)] = layers.Affine(self.params['W' + str(idx)], self.params['b' + str(idx)]) self.layers['Activation_function' + str(idx)] = activation_layer[activation]() idx = self.hidden_layer_num + 1 self.layers['Affine' + str(idx)] = layers.Affine(self.params['W' + str(idx)], self.params['b' + str(idx)]) self.last_layer = layers.SoftmaxWithLoss()
def __init__(self, input_size, hidden_size, output_size, weight_init_std=0.01): # 重みの初期化 self.params = {} self.params['W1'] = weight_init_std * np.random.randn( input_size, hidden_size) self.params['W2'] = weight_init_std * np.random.randn( hidden_size, output_size) self.params['b1'] = np.zeros(hidden_size) self.params['b2'] = np.zeros(output_size) # レイヤの生成 self.layers = OrderedDict() self.layers['Affine1'] = layers.Affine(self.params['W1'], self.params['b1']) self.layers['Relu1'] = layers.Relu() self.layers['Affine2'] = layers.Affine(self.params['W2'], self.params['b2']) self.lastLayer = layers.SoftmaxWithLoss()
def __init__(self, input_size, hidden_size_list, output_size, activation='relu', weight_init_std='relu', weight_decay_lambda=0, use_dropout=False, dropout_ration=0.5, use_batchnorm=False): self.input_size = input_size self.hidden_size_list = hidden_size_list self.hidden_layer_num = len(hidden_size_list) self.output_size = output_size self.weight_decay_lambda = weight_decay_lambda self.use_dropout = use_dropout self.use_batchnorm = use_batchnorm self.params = {} # 가중치 초기화 self.__init_weight(weight_init_std) # 계층 생성 activation_layer = {'sigmoid': layers.Sigmoid, 'relu': layers.Relu} self.layers = OrderedDict() # < 은닉층 생성 > # self.hidden_layer_num 개수만큼 for idx in range(1, self.hidden_layer_num + 1): # (1) Affine 계층 self.layers['Affine' + str(idx)] = layers.Affine( self.params['W' + str(idx)], self.params['b' + str(idx)]) # (2) BatchNormalization 계층 if self.use_batchnorm: # 각 계층별 배치 정규화 계층에서 사용할 매개변수 최기화 # 원본 그대로에서 시작하는 것으로 초기화. 1배 확대(gamma), 이동 0(beta) self.params['gamma' + str(idx)] = np.ones( hidden_size_list[idx - 1]) # 1 self.params['beta' + str(idx)] = np.zeros( hidden_size_list[idx - 1]) # 0 self.layers['BatchNorm' + str(idx)] = layers.BatchNormalization( self.params['gamma' + str(idx)], self.params['beta' + str(idx)]) # (3) 활성화 함수 self.layers['Activation_function' + str(idx)] = activation_layer[activation]() # (4) Dropout 계층 if self.use_dropout: self.layers['Dropout' + str(idx)] = layers.Dropout(dropout_ration) # < 출력층 Affine 생성 > idx = self.hidden_layer_num + 1 self.layers['Affine' + str(idx)] = layers.Affine( self.params['W' + str(idx)], self.params['b' + str(idx)]) # < 출력층 생성 > # 출력층 활성화 함수로 Softmax, 손실함수로 cross_entropy_error 사용 self.last_layer = layers.SoftmaxWithLoss()
def __init__(self, input_dim=(1, 28, 28), conv_param_1={ 'filter_num': 16, 'filter_size': 3, 'pad': 1, 'stride': 1 }, conv_param_2={ 'filter_num': 16, 'filter_size': 3, 'pad': 1, 'stride': 1 }, conv_param_3={ 'filter_num': 32, 'filter_size': 3, 'pad': 1, 'stride': 1 }, conv_param_4={ 'filter_num': 32, 'filter_size': 3, 'pad': 2, 'stride': 1 }, conv_param_5={ 'filter_num': 64, 'filter_size': 3, 'pad': 1, 'stride': 1 }, conv_param_6={ 'filter_num': 64, 'filter_size': 3, 'pad': 1, 'stride': 1 }, hidden_size=50, output_size=10): # 重みの初期化=========== # 各層のニューロンひとつあたりが、前層のニューロンといくつのつながりがあるか pre_node_nums = np.array([ 1 * 3 * 3, 16 * 3 * 3, 16 * 3 * 3, 32 * 3 * 3, 32 * 3 * 3, 64 * 3 * 3, 64 * 4 * 4, hidden_size ]) wight_init_scales = np.sqrt(2.0 / pre_node_nums) # Heの初期値 self.params = {} pre_channel_num = input_dim[0] for idx, conv_param in enumerate([ conv_param_1, conv_param_2, conv_param_3, conv_param_4, conv_param_5, conv_param_6 ]): self.params[ 'W' + str(idx + 1)] = wight_init_scales[idx] * np.random.randn( conv_param['filter_num'], pre_channel_num, conv_param['filter_size'], conv_param['filter_size']) self.params['b' + str(idx + 1)] = np.zeros( conv_param['filter_num']) pre_channel_num = conv_param['filter_num'] self.params['W7'] = wight_init_scales[6] * np.random.randn( pre_node_nums[6], hidden_size) print(self.params['W7'].shape) self.params['b7'] = np.zeros(hidden_size) self.params['W8'] = wight_init_scales[7] * np.random.randn( pre_node_nums[7], output_size) self.params['b8'] = np.zeros(output_size) # レイヤの生成=========== self.layers = [] self.layers.append( layers.Convolution(self.params['W1'], self.params['b1'], conv_param_1['stride'], conv_param_1['pad'])) self.layers.append(layers.Relu()) self.layers.append( layers.Convolution(self.params['W2'], self.params['b2'], conv_param_2['stride'], conv_param_2['pad'])) self.layers.append(layers.Relu()) self.layers.append(layers.Pooling(pool_h=2, pool_w=2, stride=2)) self.layers.append( layers.Convolution(self.params['W3'], self.params['b3'], conv_param_3['stride'], conv_param_3['pad'])) self.layers.append(layers.Relu()) self.layers.append( layers.Convolution(self.params['W4'], self.params['b4'], conv_param_4['stride'], conv_param_4['pad'])) self.layers.append(layers.Relu()) self.layers.append(layers.Pooling(pool_h=2, pool_w=2, stride=2)) self.layers.append( layers.Convolution(self.params['W5'], self.params['b5'], conv_param_5['stride'], conv_param_5['pad'])) self.layers.append(layers.Relu()) self.layers.append( layers.Convolution(self.params['W6'], self.params['b6'], conv_param_6['stride'], conv_param_6['pad'])) self.layers.append(layers.Relu()) self.layers.append(layers.Pooling(pool_h=2, pool_w=2, stride=2)) self.layers.append(layers.Affine(self.params['W7'], self.params['b7'])) self.layers.append(layers.Relu()) self.layers.append(layers.Dropout(0.5)) self.layers.append(layers.Affine(self.params['W8'], self.params['b8'])) self.layers.append(layers.Dropout(0.5)) self.last_layer = layers.SoftmaxWithLoss()