def __init__(self, input_size, hidden_size_list, output_size, activation='relu', weight_init_std='relu', weight_decay_lambda=0): self.input_size = input_size self.hidden_size_list = hidden_size_list self.hidden_layer_num = len(hidden_size_list) self.weight_decay_lambda = weight_decay_lambda self.params = {} # Initialize weights self.__init_weight(weight_init_std) # Generate layers activation_layer = {'sigmoid': Sigmoid, 'relu': ReLU} self.layers = OrderedDict() for idx in range(1, self.hidden_layer_num + 1): self.layers['Affine' + str(idx)] = 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)] = Affine(self.params['W' + str(idx)], self.params['b' + str(idx)]) self.last_layer = SoftmaxWithLoss()
def initialize(input_size, hidden_size, output_size, init_weight=0.01, init_params=None): hidden_count = len(hidden_size) if init_params is None: params['w1'] = init_weight * np.random.randn(input_size, hidden_size[0]) params['b1'] = np.zeros(hidden_size[0]) for idx in range(1, hidden_count): params[f'w{idx+1}'] = init_weight * np.random.randn( hidden_size[idx - 1], output_size[idx]) params[f'b{idx+1}'] = np.zeros(hidden_size[idx]) params[f'w{hidden_count+1}'] = init_weight * np.random.randn( hidden_size[idx - 1], output_size[idx]) params[f'b{hidden_count+1}'] = np.zeros(output_size) else: globals()['params'] = init_params #전역, 네임테이블(전역은 쓰지 않는게 좋다..) layers.append(Affine(params['w1'], params['b1'])) layers.append(ReLU()) layers.append(Affine(params['w2'], params['b2']))
def initialize(input_size, hidden_size, output_size, init_weight=0.01): params['w1'] = init_weight * np.random.randn(input_size, hidden_size) params['b1'] = np.zeros(hidden_size) params['w2'] = init_weight * np.random.randn(hidden_size, output_size) params['b2'] = np.zeros(output_size) layers.append(Affine(params['w1'], params['b1'])) layers.append(ReLU()) layers.append(Affine(params['w2'], params['b2'])) layers.append(SoftmaxWithLoss())
def forward(self, x, hidden_state_prev, params): assert len(x.shape) == 2 affine_hidden, affine_input, affine_output, tanh = Affine(), Affine( ), Affine(), Tanh() hidden_state_raw = affine_hidden(hidden_state_prev, params['h2h'], params['h2h_b']) hidden_state_raw += affine_input(x, params['i2h'], params['i2h_b']) hidden_state = tanh(hidden_state_raw) logits = affine_output(hidden_state, params['h2o'], params['h2o_b']) self.cache = (affine_hidden, affine_input, affine_output, tanh, params) return hidden_state, logits
def initialize(input_size, hidden_size, output_size, init_weight=0.01, init_params=None): if init_params is None: params['w1'] = init_weight * np.random.randn(input_size, hidden_size) params['b1'] = np.zeros(hidden_size) params['w2'] = init_weight * np.random.randn(hidden_size, output_size) params['b2'] = np.zeros(output_size) else: globals()['params'] = init_params layers.append(Affine(params['w1'], params['b1'])) layers.append(ReLU()) layers.append(Affine(params['w2'], params['b2'])) layers.append(SoftmaxWithLoss())
def __init__(self, input_size: int, hidden_size: int, output_size: int): W1 = 0.01 * np.random.randn(input_size, hidden_size) b1 = np.zeros(hidden_size) W2 = 0.01 * np.random.randn(hidden_size, output_size) b2 = np.zeros(output_size) self.layers = [Affine(W1, b1), Sigmoid(), Affine(W2, b2)] self.loss_layer = SoftmaxWithLoss() self.params, self.grads = [], [] for layer in self.layers: self.params += layer.params self.grads += layer.grads
def __init__(self, input_dim=(1, 28, 28), # (C, W, H) filter_num=30, filter_size=5, filter_pad=0, filter_stride=1, hidden_size=100, output_size=10, weight_init_std=0.01 ): # input(N, C, W, H) # -> Conv(N, FN, conv_out_h, conv_out_w) -> ReLu # -> Pooling(N, FN , pool_out_h, pool_out_w) # -> Affine[flatten行う](N, hidden_layer) -> ReLu # -> Affine(N, output_layer) -> SoftMax # input_sizeは動的に決定(正方形を前提) input_size = input_dim[1] conv_output_size = (input_size + 2 * filter_pad - filter_size) / filter_stride + 1 # FN * pool_out_h * pool_out_w pool_output_size = int(filter_num * (conv_output_size / 2) * (conv_output_size / 2)) self.params = {} # Conv # (input_size, C, W, H) -> (N, FilterNum, out_h, out_w) 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) # ReLu # Pool # Affine self.params['W2'] = weight_init_std * np.random.randn(pool_output_size, hidden_size) self.params['b2'] = np.zeros(hidden_size) # Relu # Affine 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'] = Convolution(self.params['W1'], self.params['b1'], filter_stride, filter_pad) self.layers['ReLu1'] = ReLu() self.layers['Pool1'] = Pooling(pool_h=2, pool_w=2, stride=2) self.layers['Affine1'] = Affine(self.params['W2'], self.params['b2']) self.layers['ReLu2'] = ReLu() self.layers['Affine2'] = Affine(self.params['W3'], self.params['b3']) self.last_layer = SoftmaxWithLoss()
def __init__(self, input_size, hidden_size, output_size): I, H, O = input_size, hidden_size, output_size w1 = np.random.randn(I, H) * 0.01 b1 = np.zeros(H) #np.random.randn(H) w2 = np.random.randn(H, O) * 0.01 b2 = np.zeros(O) #np.random.randn(O) self.layers = [Affine(w1, b1), Sigmoid(), Affine(w2, b2)] self.loss_layer = SoftmaxWithLoss() self.params, self.grads = [], [] for l in self.layers: self.params += l.params self.grads += l.grads # 勾配まとめはこのときだけ。-> 各layerの勾配更新は参照場所を動かさないようにする。
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.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': Sigmoid, 'relu': Relu} self.layers = OrderedDict() for idx in range(1, self.hidden_layer_num + 1): self.layers['Affine' + str(idx)] = 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)] = 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)] = Dropout(dropout_ratio) idx = self.hidden_layer_num + 1 self.layers['Affine' + str(idx)] = Affine(self.params['W' + str(idx)], self.params['b' + str(idx)]) self.last_layer = SoftmaxWithLoss()
def __init__(self, hidden_size, weight_init_std=0.01): super().__init__() self.params = dict() self.params['W1'] = weight_init_std * np.random.randn( self.x_train.shape[1], hidden_size) self.params['b1'] = np.zeros(hidden_size) self.params['W2'] = weight_init_std * np.random.randn( hidden_size, self.t_train.shape[1]) self.params['b2'] = np.zeros(self.t_train.shape[1]) # 生成层 self.layers = OrderedDict() self.layers['Affine1'] = Affine(self.params['W1'], self.params['b1']) self.layers['Relu'] = Relu() self.layers['Affine2'] = Affine(self.params['W2'], self.params['b2']) self.last_layer = SoftmaxWithLoss()
def __init__(self, input_size, hidden_size, output_size): I, H, O = input_size, hidden_size, output_size W1 = 0.01 * np.random.randn(I, H) b1 = np.zeros(H) W2 = 0.01 * np.random.randn(H, O) b2 = np.zeros(O) self.layers = [Affine(W1, b1), Sigmoid(), Affine(W2, b2)] self.loss_layer = SoftmaxWithLoss() self.params, self.grads = [], [] for layer in self.layer: self.params += layer.params self.grads += layer.grads
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['W1'] = np.ones((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['W2'] = np.ones((hidden_size, output_size)) self.params['b2'] = np.zeros(output_size) # 生成层 self.layers = OrderedDict() self.layers['Affine1'] = Affine(self.params['W1'], self.params['b1']) self.layers['Relu1'] = Relu() self.layers['Affine2'] = Affine(self.params['W2'], self.params['b2']) self.lastLayer = SoftmaxWithLoss()
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'] = Convolution(self.params['W1'], self.params['b1'], conv_param['stride'], conv_param['pad']) self.layers['Relu1'] = Relu() self.layers['Pool1'] = Pooling(pool_h=2, pool_w=2, stride=2) self.layers['Affine1'] = Affine(self.params['W2'], self.params['b2']) self.layers['Relu2'] = Relu() self.layers['Affine2'] = Affine(self.params['W3'], self.params['b3']) self.last_layer = SoftmaxWithLoss()
def __init__(self, input_size, hidden_size, output_size, weight_init_std=0.01): # Initialize weights. 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) # Generate layers. self.layers = OrderedDict() # Ordered dictionary self.layers['Affine1'] = Affine(self.params['W1'], self.params['b1']) self.layers['Relu'] = Relu() self.layers['Affine2'] = Affine(self.params['W2'], self.params['b2']) self.lastLayer = SoftmaxWithLoss()
def __init__(self, input_size, hidden_size, output_size): I, H, O = input_size, hidden_size, output_size # 重みとバイアスの初期化 W1 = 0.01 * np.random.randn(I, H) b1 = np.random.randn(H) W2 = 0.01 * np.random.randn(H, O) b2 = np.random.randn(O) # レイヤの生成 self.layers = [Affine(W1, b1), Sigmoid(), Affine(W2, b2)] self.loss_layer = SoftmaxWithLoss() # すべての重みをリストにまとめる self.params, self.grads = [], [] for layer in self.layers: self.params += layer.params self.grads += layer.grads
def __init__(self, input_size, hidden_size, output_size, weight_init_std=0.01): self.params = {} self.params['w1'] = np.random.randn(input_size, hidden_size) / weight_init_std self.params['b1'] = np.zeros(hidden_size) self.params['w2'] = np.random.randn(hidden_size, output_size) / weight_init_std self.params['b2'] = np.zeros(output_size) self.layers = OrderedDict() self.layers['Affine1'] = Affine(self.params['w1'], self.params['b1']) self.layers['Relu1'] = Relu() self.layers['Affine2'] = Affine(self.params['w2'], self.params['b2']) self.lastlayer = 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"] = Affine(self.params["W1"], self.params["b1"]) self.layers["Relu1"] = Relu() self.layers["Affine2"] = Affine(self.params["W2"], self.params["b2"]) self.lastLayer = SoftmaxLoss()
def __init__(self, input_size, hidden_size, output_size): I, H, O = input_size, hidden_size, output_size # initialize weight and bias W1 = 0.01 * cp.random.randn(I, H) b1 = cp.zeros(H) W2 = 0.01 * cp.random.randn(H, O) b2 = cp.zeros(O) # create layer self.layers = [Affine(W1, b1), Sigmoid(), Affine(W2, b2)] self.loss_layer = SoftmaxWithLoss() # combine all weight and grads into list self.params, self.grads = [], [] for layer in self.layers: self.params += layer.params self.grads += layer.grads
def __init_layer(self): activation_layer = {'sigmoid': Sigmoid, 'relu': Relu} self.layers = OrderedDict() for idx in range(1, self.hidden_layer_num + 1): self.layers['Affine' + str(idx)] = Affine(self.params['W' + str(idx)], self.params['b' + str(idx)]) if self.use_batchnorm: self.params['gamma' + str(idx)] = np.ones(self.hidden_size_list[idx - 1]) self.params['beta' + str(idx)] = np.zeros(self.hidden_size_list[idx - 1]) self.layers['BatchNorm' + str(idx)] = BatchNormalization(self.params['gamma' + str(idx)], self.params['beta' + str(idx)]) self.layers['activation_function' + str(idx)] = activation_layer[self.activation]() if self.use_dropout: self.layers['Dropout' + str(idx)] = Dropout(self.dropout_ration) idx += 1 self.layers['Affine' + str(idx)] = Affine(self.params['W' + str(idx)], self.params['b' + str(idx)]) self.last_layer = SoftmaxWithLoss()
def _make_layers(self): self.layers = [] for i in range(self.layer_num): self.layers.append(Affine(self.weights[i], self.bias[i])) if i == self.layer_num - 1: pass else: self.layers.append(Relu()) self.lastLayer = SoftmaxWithLoss()
def _init_layers(self): self.layers = OrderedDict() all_layers = [self.input_size ] + self.hidden_size_list + [self.output_size] activation_dict = {'relu': Relu, 'sigmoid': Sigmoid} for i in range(1, len(self.hidden_size_list) + 2): self.layers['Affine%d' % i] = Affine(self.params['W%d' % i], self.params['b%d' % i]) self.layers['Activation%d' % i] = activation_dict[self.activation]() self.last_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) # f = open("./db/param_result/784x50x10-0.99162.json", 'r') # self.params = json.load(f) # f.close() # for key in ('W1', 'b1', 'W2', 'b2'): # self.params[key] = np.array(self.params[key]) # 创建各层的对象 self.layers = OrderedDict() self.layers['Affine1'] = Affine(self.params['W1'], self.params['b1']) self.layers['Relu1'] = Relu() self.layers['Affine2'] = Affine(self.params['W2'], self.params['b2']) self.last_layer = SoftmaxWithLoss()
#=============================== # 1. Load training/tet data # 배치테스트 N=2 _x, _t = np.array([[2.6, 3.9, 5.6], [1.76, 2.19, 0.6]]), np.array([[0, 0, 1], [0, 1, 0]]) # 2. hyperparameter #==Numerical Gradient============================= # 3. initialize network network.initialize(3, 2, 3) _layers = [ Affine(network.params['w1'], network.params['b1']), ReLU(), Affine(network.params['w2'], network.params['b2']), SoftmaxWithLoss() ] grad = network.numerical_gradient_net(_x, _t) print(grad) #== Backpropagation Gradient============================= # 3. initialize network np.random.seed(3) network.initialize(3, 2, 3) _layers = [ Affine(network.params['w1'], network.params['b1']),
train_y = convert_to_one_hot(train_y, num_classes) test_x = np.reshape(test_x, (len(test_x), 1, img_rows, img_cols)).astype(skml_config.config.i_type) test_y = convert_to_one_hot(test_y, num_classes) train_x, valid_x, train_y, valid_y = train_test_split(train_x, train_y) filters = 64 model = Sequential() model.add(Convolution(filters, 3, input_shape=input_shape)) model.add(BatchNormalization()) model.add(ReLU()) model.add(MaxPooling(2)) model.add(Convolution(filters, 3)) model.add(BatchNormalization()) model.add(ReLU()) model.add(GlobalAveragePooling()) model.add(Affine(num_classes)) model.compile(SoftmaxCrossEntropy(), Adam()) train_batch_size = 100 valid_batch_size = 1 print("訓練開始: {}".format(datetime.now().strftime("%Y/%m/%d %H:%M"))) model.fit(train_x, train_y, train_batch_size, 20, validation_data=(valid_batch_size, valid_x, valid_y), validation_steps=1) print("訓練終了: {}".format(datetime.now().strftime("%Y/%m/%d %H:%M"))) model.save(save_path) loss, acc = model.evaluate(test_x, test_y) print("Test loss: {}".format(loss)) print("Test acc: {}".format(acc))
num_episodes = 1000 num_steps = 200 success_steps = 100 reward_fail = -1 reward_success = 1 reward_none = 0 env = gym.make("CartPole-v0") env = wrappers.Monitor(env, "videos", (lambda ep: ep % 100 == 0), True) num_actions = env.action_space.n num_observations = env.observation_space.shape[0] input_shape = (num_observations, ) hidden_size = 32 model = Sequential() model.add(Affine(hidden_size, input_shape=input_shape)) model.add(ReLU()) model.add(Affine(hidden_size)) model.add(ReLU()) model.add(Affine(hidden_size)) model.add(ReLU()) model.add(Affine(num_actions)) model.compile(MeanSquaredError(), Adam()) explorer = LinearDecayEpsilonGreedy(1.0, 0.0, 500, env.action_space.sample) replay_buffer = ReplayBuffer(10**6) agent = DQN(model, replay_buffer, explorer, sync_target_interval=5, replay_size=32,
x = np.array([[1.0, 0.5], [-2.0, 3.0]]) dout = np.array([[1.0, 0.5], [-2.0, 3.0]]) # # Pass! # print(sigmoid.forward(x)) # print(sigmoid.backward(dout)) ############################# # Test class Affine. ############################# # Define parameters of class Affline W = np.array([[1, 1, 1], [2, 2, 2]]) # (2, 3) b = np.array([0, 0, 0]) # (3, ) # Instantiate class Affine. affine = Affine(W, b) # Define x and dout with np.array data type. x = np.array([[1, 2], [3, 4]]) # (2, 2) dout = np.array([[1, 2, 3], [4, 5, 6]]) # (2, 3) # # Pass! # print(affine.forward(x)) # print(affine.backward(dout)) # print(affine.dW) # print(affine.db) ############################# # Test class SoftmaxWithLoss. ############################# # Instantiate class SoftmaxWithLoss.
def __init__(self, input_dim, conv_params=[ { 'filter_num': 32, 'filter_size': 9, 'pad': 0, 'stride': 3 }, { 'filter_num': 64, 'filter_size': 5, 'pad': 2, 'stride': 1 }, { 'filter_num': 128, 'filter_size': 7, 'pad': 0, 'stride': 1 }, ], hidden_size=128, dropout_ratio=[0.2, 0.5], output_size=5): self.params = {} self.layers = {} pre_shape = input_dim for idx, conv_param in enumerate(conv_params): # init parameters self.params['W' + str(idx + 1)] = init_he(pre_shape[0] * conv_param['filter_size']**2) *\ np.random.randn( conv_param['filter_num'], pre_shape[0], conv_param['filter_size'], conv_param['filter_size']) self.params['b' + str(idx + 1)] = np.zeros( conv_param['filter_num']) # set layers self.layers['Conv' + str(idx + 1)] = Convolution( self.params['W' + str(idx + 1)], self.params['b' + str(idx + 1)], conv_param['stride'], conv_param['pad']) self.layers['Relu' + str(idx + 1)] = Relu() # calc output image size of conv layers pre_shape = self.layers['Conv' + str(idx + 1)].output_size(pre_shape) idx = len(conv_params) # init parameters and set layers Affine self.params['W' + str(idx + 1)] = init_he(pre_shape[0] * pre_shape[1]**2) *\ np.random.randn(pre_shape[0] * pre_shape[1]**2, hidden_size) self.params['b' + str(idx + 1)] = np.zeros(hidden_size) self.layers['Affine' + str(idx + 1)] = Affine( self.params['W' + str(idx + 1)], self.params['b' + str(idx + 1)]) self.layers['Relu' + str(idx + 1)] = Relu() idx += 1 # init parameters and set layers output self.params['W' + str(idx + 1)] = init_he(hidden_size) * np.random.randn( hidden_size, output_size) self.params['b' + str(idx + 1)] = np.zeros(output_size) self.layers['Affine' + str(idx + 1)] = Affine( self.params['W' + str(idx + 1)], self.params['b' + str(idx + 1)]) # set loss function layer self.loss_layer = SoftmaxWithLoss()
model.add_layer( Convolution(32, (3, 3), input_shape=(batch_size, X_tr.shape[1], X_tr.shape[2], X_tr.shape[3]), weight_initializer=NormalInitializer(std))) model.add_layer(ReLuActivation()) model.add_layer(BatchNormalization()) model.add_layer( Convolution(32, (3, 3), weight_initializer=NormalInitializer(std), padding='same')) model.add_layer(ReLuActivation()) model.add_layer(MaxPool((2, 2))) model.add_layer(Flatten()) model.add_layer( Affine(100, weight_initializer=NormalInitializer(std), reg=reg)) model.add_layer(ReLuActivation()) model.add_layer(DropoutLayer(drop_rate=0.3)) model.add_layer( Affine(n_classes, weight_initializer=NormalInitializer(std), reg=reg)) model.initialize(loss=CrossEntropyLoss(), optimizer=Adam(learning_rate=0.001, decay_fst_mom=0.9, decay_sec_mom=0.999)) # with open('model_90_49.14262959724404', 'rb') as file: # model = pickle.load(file) model.fit(batch_size, X_tr, y_tr, n_epochs=100, metric=accuracy_metric)