MCPcopy Create free account
hub / github.com/dblalock/bolt / opq_initialize

Function opq_initialize

experiments/python/product_quantize.py:231–266  ·  view source on GitHub ↗
(X_train, ncodebooks, init='gauss')

Source from the content-addressed store, hash-verified

229
230# @_memory.cache
231def 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:

Callers 1

learn_opqFunction · 0.85

Calls 6

random_rotationFunction · 0.90
orthonormalize_rowsFunction · 0.90
opq_rotateFunction · 0.85
formatMethod · 0.80
dotMethod · 0.45

Tested by

no test coverage detected