Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[MRG+1] ENH: only call clock() if verbosity level warrants it #10091

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 28 additions & 18 deletions sklearn/manifold/_barnes_hut_tsne.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ cdef float compute_gradient(float[:] val_P,
long n_samples = pos_reference.shape[0]
int n_dimensions = qt.n_dimensions
double[1] sum_Q
clock_t t1, t2
clock_t t1 = 0, t2 = 0
float sQ, error
int take_timing = 1 if qt.verbose > 15 else 0

if qt.verbose > 11:
printf("[t-SNE] Allocating %li elements in force arrays\n",
Expand All @@ -71,19 +72,22 @@ cdef float compute_gradient(float[:] val_P,
cdef float* pos_f = <float*> malloc(sizeof(float) * n_samples * n_dimensions)

sum_Q[0] = 0.0
t1 = clock()
if take_timing:
t1 = clock()
compute_gradient_negative(pos_reference, neg_f, qt, sum_Q,
dof, theta, start, stop)
t2 = clock()
if qt.verbose > 15:
if take_timing:
t2 = clock()
printf("[t-SNE] Computing negative gradient: %e ticks\n", ((float) (t2 - t1)))
sQ = sum_Q[0]
t1 = clock()

if take_timing:
t1 = clock()
error = compute_gradient_positive(val_P, pos_reference, neighbors, indptr,
pos_f, n_dimensions, dof, sQ, start,
qt.verbose)
t2 = clock()
if qt.verbose > 15:
if take_timing:
t2 = clock()
printf("[t-SNE] Computing positive gradient: %e ticks\n", ((float) (t2 - t1)))
for i in range(start, n_samples):
for ax in range(n_dimensions):
Expand Down Expand Up @@ -118,9 +122,10 @@ cdef float compute_gradient_positive(float[:] val_P,
float C = 0.0
float exponent = (dof + 1.0) / -2.0
float[3] buff
clock_t t1, t2
clock_t t1 = 0, t2 = 0

t1 = clock()
if verbose > 10:
t1 = clock()
for i in range(start, n_samples):
# Init the gradient vector
for ax in range(n_dimensions):
Expand All @@ -140,9 +145,9 @@ cdef float compute_gradient_positive(float[:] val_P,
/ max(qij, FLOAT32_TINY))
for ax in range(n_dimensions):
pos_f[i * n_dimensions + ax] += dij * buff[ax]
t2 = clock()
dt = ((float) (t2 - t1))
if verbose > 10:
t2 = clock()
dt = ((float) (t2 - t1))
printf("[t-SNE] Computed error=%1.4f in %1.1e ticks\n", C, dt)
return C

Expand Down Expand Up @@ -170,7 +175,8 @@ cdef void compute_gradient_negative(float[:, :] pos_reference,
double qijZ
float[1] iQ
float[3] force, neg_force, pos
clock_t t1, t2, t3
clock_t t1 = 0, t2 = 0, t3 = 0
int take_timing = 1 if qt.verbose > 20 else 0

summary = <float*> malloc(sizeof(float) * n * offset)

Expand All @@ -183,9 +189,11 @@ cdef void compute_gradient_negative(float[:, :] pos_reference,
iQ[0] = 0.0
# Find which nodes are summarizing and collect their centers of mass
# deltas, and sizes, into vectorized arrays
t1 = clock()
if take_timing:
t1 = clock()
idx = qt.summarize(pos, summary, theta*theta)
t2 = clock()
if take_timing:
t2 = clock()
# Compute the t-SNE negative force
# for the digits dataset, walking the tree
# is about 10-15x more expensive than the
Expand All @@ -200,12 +208,14 @@ cdef void compute_gradient_negative(float[:, :] pos_reference,
mult = size * qijZ * qijZ
for ax in range(n_dimensions):
neg_force[ax] += mult * summary[j * offset + ax]
t3 = clock()
if take_timing:
t3 = clock()
for ax in range(n_dimensions):
neg_f[i * n_dimensions + ax] = neg_force[ax]
dta += t2 - t1
dtb += t3 - t2
if qt.verbose > 20:
if take_timing:
dta += t2 - t1
dtb += t3 - t2
if take_timing:
printf("[t-SNE] Tree: %li clock ticks | ", dta)
printf("Force computation: %li clock ticks\n", dtb)

Expand Down