MCPcopy Create free account
hub / github.com/Newmu/dcgan_code / gpu_nnc_predict

Function gpu_nnc_predict

lib/metrics.py:33–59  ·  view source on GitHub ↗
(trX, trY, teX, metric='cosine', batch_size=4096)

Source from the content-addressed store, hash-verified

31 return models[best]
32
33def gpu_nnc_predict(trX, trY, teX, metric='cosine', batch_size=4096):
34 if metric == 'cosine':
35 metric_fn = cosine_dist
36 else:
37 metric_fn = euclid_dist
38 idxs = []
39 for i in range(0, len(teX), batch_size):
40 mb_dists = []
41 mb_idxs = []
42 for j in range(0, len(trX), batch_size):
43 dist = metric_fn(floatX(teX[i:i+batch_size]), floatX(trX[j:j+batch_size]))
44 if metric == 'cosine':
45 mb_dists.append(np.max(dist, axis=1))
46 mb_idxs.append(j+np.argmax(dist, axis=1))
47 else:
48 mb_dists.append(np.min(dist, axis=1))
49 mb_idxs.append(j+np.argmin(dist, axis=1))
50 mb_idxs = np.asarray(mb_idxs)
51 mb_dists = np.asarray(mb_dists)
52 if metric == 'cosine':
53 i = mb_idxs[np.argmax(mb_dists, axis=0), np.arange(mb_idxs.shape[1])]
54 else:
55 i = mb_idxs[np.argmin(mb_dists, axis=0), np.arange(mb_idxs.shape[1])]
56 idxs.append(i)
57 idxs = np.concatenate(idxs, axis=0)
58 nearest = trY[idxs]
59 return nearest
60
61def gpu_nnd_score(trX, teX, metric='cosine', batch_size=4096):
62 if metric == 'cosine':

Callers 1

nnc_scoreFunction · 0.85

Calls 1

floatXFunction · 0.90

Tested by

no test coverage detected