def load_Iris(test_size=0.3, seed=42): X, y = load_iris(return_X_y=True) X_train, X_test, y_train, y_test = train_test_split(X, y, shuffle=True, test_size=test_size, random_state=seed) X_train = F.normalize(X_train) X_test = F.normalize(X_test) num_classes = 3 return X_train, y_train, X_test, y_test, num_classes
def generate_3d(data, noise, samples, shuffle=False): if data == 1: centers = [(1, 0, 0), (0, 1, 0), (0, 0, 1)] elif data == 2: centers = [(np.cos(np.pi / 4), np.sin(np.pi / 4), 1), (np.cos(2 * np.pi / 3), np.sin(2 * np.pi / 3), 1), (np.cos(np.pi), np.sin(np.pi), 1)] elif data == 3: centers = [(np.cos(np.pi / 4), np.sin(np.pi / 4), 1), (np.cos(2 * np.pi / 3), np.sin(2 * np.pi / 3), 1), (np.cos(5 * np.pi / 6), np.cos(5 * np.pi / 6), 1)] else: raise NameError('Data not found.') X, Y = [], [] for c, center in enumerate(centers): _X = np.random.normal(center, scale=(noise, noise, noise), size=(samples, 3)) _Y = np.ones(samples, dtype=np.int32) * c X.append(_X) Y.append(_Y) X = F.normalize(np.vstack(X)) Y = np.hstack(Y) if shuffle: idx_arr = np.random.choice(np.arange(len(X)), len(X), replace=False) X, Y = X[idx_arr], Y[idx_arr] return X.astype(np.float32), Y.astype(np.int32), 3
def forward(self, layer, V, y=None): if y is not None: self.init(V, y) self.save_weights(layer) self.save_gam(layer) else: self.load_weights(layer) self.load_gam(layer) expd = np.einsum("bi...,ih...->bh...", V, self.E.conj(), optimize=True) comp = np.stack([np.einsum("bi...,ih...->bh...", V, C_j.conj(), optimize=True) \ for C_j in self.Cs]) clus, y_approx = self.nonlinear(comp) V = V + self.eta * (expd - clus) V = F.normalize(V) return V, y_approx
def load_Mice(root, test_size=0.3, seed=42): """Download data by running the following command in shell `curl https://archive.ics.uci.edu/ml/machine-learning-databases/00342/Data_Cortex_Nuclear.xls --output ./data/mice.xls` """ df_data = pd.read_excel(os.path.join(root, 'mice.xls'), sheet_name='Hoja1') df_data = df_data.fillna(df_data.mean()) df_data['class'] = df_data['class'].astype('category').cat.codes X = df_data.to_numpy()[:, 1:78].astype(np.float32) X = F.normalize(X) y = df_data['class'].to_numpy().astype(np.int32) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_size, random_state=seed) num_classes = 8 return X_train, y_train, X_test, y_test, num_classes
def generate_2d(data, noise, samples, shuffle=False): if data == 1: centers = [(1, 0), (0, 1)] elif data == 2: centers = [(np.cos(np.pi / 3), np.sin(np.pi / 3)), (1, 0)] elif data == 3: centers = [(np.cos(np.pi / 4), np.sin(np.pi / 4)), (1, 0)] elif data == 4: centers = [(np.cos(3 * np.pi / 4), np.sin(3 * np.pi / 4)), (1, 0)] elif data == 5: centers = [(np.cos(2 * np.pi / 3), np.sin(2 * np.pi / 3)), (1, 0)] elif data == 6: centers = [(np.cos(3 * np.pi / 4), np.sin(3 * np.pi / 4)), (np.cos(4 * np.pi / 3), np.sin(4 * np.pi / 3)), (1, 0)] elif data == 7: centers = [(np.cos(3 * np.pi / 4), np.sin(3 * np.pi / 4)), (np.cos(4 * np.pi / 3), np.sin(4 * np.pi / 3)), (np.cos(np.pi / 4), np.sin(np.pi / 4))] elif data == 8: centers = [(np.cos(np.pi / 6), np.sin(np.pi / 6)), (np.cos(np.pi / 2), np.sin(np.pi / 2)), (np.cos(3 * np.pi / 4), np.sin(3 * np.pi / 4)), (np.cos(5 * np.pi / 4), np.sin(5 * np.pi / 4)), (np.cos(7 * np.pi / 4), np.sin(7 * np.pi / 4)), (np.cos(3 * np.pi / 2), np.sin(3 * np.pi / 2))] else: raise NameError('data not found.') data = [] targets = [] for lbl, center in enumerate(centers): X = np.random.normal(loc=center, scale=noise, size=(samples, 2)) y = np.repeat(lbl, samples).tolist() data.append(X) targets += y data = np.concatenate(data) data = F.normalize(data) targets = np.array(targets) if shuffle: idx_arr = np.random.choice(np.arange(len(data)), len(data), replace=False) data, targets = data[idx_arr], targets[idx_arr] return data, targets, len(centers)
utils.save_features(model_dir, "X_train", X_train, y_train) utils.save_features(model_dir, "X_test", X_test, y_test) utils.save_features(model_dir, "X_translate_train", X_translate_train, y_translate_train) utils.save_features(model_dir, "X_translate_test", X_translate_test, y_translate_test) utils.save_features(model_dir, "Z_train", Z_train, y_train) utils.save_features(model_dir, "Z_test", Z_test, y_test) utils.save_features(model_dir, "Z_translate_train", Z_translate_train, y_translate_train) utils.save_features(model_dir, "Z_translate_test", Z_translate_test, y_translate_test) np.save(os.path.join(model_dir, 'features', 'kernel.npy'), kernels) # normalize X_train = F.normalize(X_train.reshape(X_train.shape[0], -1)) X_test = F.normalize(X_test.reshape(X_test.shape[0], -1)) X_translate_train = F.normalize( X_translate_train.reshape(X_translate_train.shape[0], -1)) X_translate_test = F.normalize( X_translate_test.reshape(X_translate_test.shape[0], -1)) Z_train = F.normalize(Z_train.reshape(Z_train.shape[0], -1)) Z_test = F.normalize(Z_test.reshape(Z_test.shape[0], -1)) Z_translate_train = F.normalize( Z_translate_train.reshape(Z_translate_train.shape[0], -1)) Z_translate_test = F.normalize( Z_translate_test.reshape(Z_translate_test.shape[0], -1)) # plot plot.plot_combined_loss(model_dir) plot.plot_heatmap(X_train, y_train, "X_train", model_dir)
def postprocess(self, X): Z = np.fft.ifft(X, norm='ortho', axis=2) return F.normalize(Z)
def preprocess(self, X): Z = F.normalize(X) return np.fft.fft(Z, norm='ortho', axis=2)
def postprocess(self, X): Z = np.fft.ifft2(X, norm='ortho', axes=(2, 3)) return F.normalize(Z)
def preprocess(self, X): Z = F.normalize(X) return np.fft.fft2(Z, norm='ortho', axes=(2, 3))