(X_train, ncodebooks, init='gauss')
| 229 | |
| 230 | # @_memory.cache |
| 231 | def opq_initialize(X_train, ncodebooks, init='gauss'): |
| 232 | X = X_train |
| 233 | _, D = X.shape |
| 234 | |
| 235 | if init == 'gauss' or init == 'gauss_flat' or init == 'gauss_shuffle': |
| 236 | permute = (init == 'gauss_shuffle') |
| 237 | R = learn_opq_gaussian_rotation(X_train, ncodebooks, shuffle=permute) |
| 238 | R = R.astype(np.float32) |
| 239 | |
| 240 | if init == 'gauss_flat': |
| 241 | # assert R.shape[0] == R.shape[1] |
| 242 | D = R.shape[1] |
| 243 | d = D / ncodebooks |
| 244 | assert int(d) == d # need same number of dims in each subspace |
| 245 | # d = int(d) |
| 246 | local_r = random_rotation(int(d)) |
| 247 | tiled = np.zeros((D, D)) |
| 248 | for c in range(ncodebooks): |
| 249 | start = c * d |
| 250 | end = start + d |
| 251 | tiled[start:end, start:end] = local_r |
| 252 | |
| 253 | R = np.dot(R, tiled) |
| 254 | |
| 255 | X_rotated = opq_rotate(X, R) |
| 256 | elif init == 'identity': |
| 257 | R = np.identity(D, dtype=np.float32) # D x D |
| 258 | X_rotated = X |
| 259 | elif init == 'random': |
| 260 | R = np.random.randn(D, D).astype(np.float32) |
| 261 | R = orthonormalize_rows(R) |
| 262 | X_rotated = opq_rotate(X, R) |
| 263 | else: |
| 264 | raise ValueError("Unrecognized initialization method: ".format(init)) |
| 265 | |
| 266 | return X_rotated, R |
| 267 | |
| 268 | |
| 269 | # loosely based on: |
no test coverage detected