def fit(self, A, y, group_ids): x = np.zeros(A.shape[1]) theta = x.copy() gamma = x.copy() inv_matrix = np.linalg.inv(A.T @ A + self.rho) group_ids = LabelBinarizer().fit_transform(group_ids) group_ids = group_ids.astype(np.float64) for _ in range(self.max_iter): x = self.update_x(A, y, gamma, theta, inv_matrix) theta_hat = self.update_theta_hat(x, gamma) w = self.update_w(theta_hat) theta = self.update_theta(w, theta_hat, group_ids) gamma = self.update_gamma(A, x, gamma, theta) self.coef_ = theta return self
def fit(self, A, y, group_ids): # 標準化 if self.normalize == True: sscaler = StandardScaler() sscaler.fit(A) A = sscaler.transform(A) # 初期値、ハイパラ n_samples = A.shape[0] w = np.zeros(A.shape[1]) rho = self.supermum_eigen(A.T @ A) alpha = self.alpha * n_samples # group-ids -> one-hot vector group_ids = LabelBinarizer().fit_transform(group_ids) group_ids = group_ids.astype(np.float64) group_id_sum = np.sum(group_ids, axis=0) for it in range(self.max_iter): w = self.update(w, A, y, rho, alpha, group_ids, group_id_sum) self.coef = w return self
def one_hot_encode_labels(data_y): data_y = LabelBinarizer().fit_transform(data_y) data_y = data_y.astype('float32') return data_y
train_x = train_x.reshape(-1, 3, 32, 32) train_y = train_y.reshape(-1, 1) test_x = test_x.reshape(-1, 3, 32, 32) test_y = test_y.reshape(-1, 1) train_x = train_x / 255. test_x = test_x / 255. # Binalize labels_train = LabelBinarizer().fit_transform(train_y) labels_test = LabelBinarizer().fit_transform(test_y) # Change types. train_x = train_x.astype(np.float32) test_x = test_x.astype(np.float32) labels_train = labels_train.astype(np.float32) labels_test = labels_test.astype(np.float32) N = len(train_x) class Cifar10(rm.Model): def __init__(self): super(Cifar10, self).__init__() self._l1 = rm.Conv2d(channel=32) self._l2 = rm.Conv2d(channel=32) self._l3 = rm.Conv2d(channel=64) self._l4 = rm.Conv2d(channel=64) self._l5 = rm.Dense(512) self._l6 = rm.Dense(10) self._sd = rm.SpatialDropout(dropout_ratio=0.25)