Пример #1
0

def dot(x,y):
  return sum(x*y)

def matmult_high_level(X,Y):
  return np.array([[np.dot(x,y) for y in Y.T] for x in X])

def matmult_loops(X,Y,Z):
  m, d = X.shape
  n = Y.shape[1]
  for i in xrange(m):
    for j in xrange(n):
      total = X[i,0] * Y[0,j] 
      for k in xrange(1,d):
        total += X[i,k] * Y[k,j]
      Z[i,j] = total 
  return Z
     

n, d = 250, 1000
m = 250 
X = np.random.randn(n,d)
Y = np.random.randn(d,m)
Z = np.zeros((m,n))
from timer import compare_perf

compare_perf(matmult_high_level, [X,Y],cpython=True)

compare_perf(matmult_loops, [X, Y, Z], cpython=False)
Пример #2
0
def clamp(i, offset, maxval):
    j = max(0, i + offset)
    return min(j, maxval)


def reflect(pos, offset, bound):
    idx = pos + offset
    return min(2 * (bound - 1) - idx, max(idx, -idx))


def conv(x, weights, mode=clamp):
    sx = x.shape
    sw = weights.shape
    result = np.zeros_like(x)
    for i in xrange(sx[0]):
        for j in xrange(sx[1]):
            for ii in xrange(sw[0]):
                for jj in xrange(sw[1]):
                    idx = mode(i, ii - sw[0] / 2,
                               sw[0]), mode(j, jj - sw[0] / 2, sw[0])
                    result[i, j] += x[idx] * weights[ii, jj]
    return result


xsize = (300, 300)
x = np.random.randn(*xsize)
wsize = (5, 5)
w = np.random.randn(*wsize)

compare_perf(conv, [x, w])
Пример #3
0
                for ii in xrange(max(i-window_radius, 0), min(i+window_radius+1, height)):
                    if ii != i or jj != j:
                        d = image[i, j, :] - image[ii, jj, :]
                        s = np.sum(d**2) 
                        gval = 1.0 - np.sqrt(s) / np.sqrt(3)
                        attack_strength = gval * state[ii, jj, 1]
                        if attack_strength > defense_strength:
                            defense_strength = attack_strength
                            winning_colony = state[ii, jj, 0]
                            changes += 1
            state_next[i, j, 0] = winning_colony
            state_next[i, j, 1] = defense_strength
    return changes
    
N = 50
dtype = np.double
image = np.zeros((N, N, 3), dtype=dtype)
state = np.zeros((N, N, 2), dtype=dtype)
state_next = np.empty_like(state)

# colony 1 is strength 1 at position 0,0
# colony 0 is strength 0 at all other positions
state[0, 0, 0] = 1
state[0, 0, 1] = 1

window_radius = 10

from timer import compare_perf 

compare_perf(growcut_python, [image, state, state_next, window_radius])
Пример #4
0
from parakeet import jit 

import timer  

def covariance(x,y):
  return ((x-x.mean()) * (y-y.mean())).mean()

def fit_simple_regression(x,y):
  slope = covariance(x,y) / covariance(x,x)
  offset = y.mean() - slope * x.mean() 
  return slope, offset

import numpy as np 

N = 10**7
x = np.random.randn(N).astype('float64')
slope = 903.29
offset = 102.1
y = slope * x + offset

jit(fit_simple_regression)(x,y)

timer.compare_perf(fit_simple_regression, (x,y))
Пример #5
0
def rosen_der_np(x):
  der = np.empty_like(x)
  der[1:-1] = (+ 200 * (x[1:-1] - x[:-2] ** 2)
               - 400 * (x[2:] - x[1:-1] ** 2) * x[1:-1]
               - 2 * (1 - x[1:-1]))
  der[0] = -400 * x[0] * (x[1] - x[0] ** 2) - 2 * (1 - x[0])
  der[-1] = 200 * (x[-1] - x[-2] ** 2)
  return der

def rosen_der_loops(x):
  n = x.shape[0]
  der = np.empty_like(x)

  for i in range(1, n - 1):
    der[i] = (+ 200 * (x[i] - x[i - 1] ** 2)
              - 400 * (x[i + 1]
              - x[i] ** 2) * x[i]
              - 2 * (1 - x[i]))
  der[0] = -400 * x[0] * (x[1] - x[0] ** 2) - 2 * (1 - x[0])
  der[-1] = 200 * (x[-1] - x[-2] ** 2)
  return der

if __name__ == '__main__':
  N = 10**5
  x = np.arange(N) / float(N)
  jit(rosen_der_np)(x) 
  from timer import compare_perf
  # numba still crashes on negative indexing
  compare_perf(rosen_der_np, [x.copy()], numba=False)
  compare_perf(rosen_der_loops, [x.copy()], numba=False)
Пример #6
0
import numpy as np 
from timer import compare_perf

# Simple convolution of 3x3 patches from a given array x
# by a 3x3 array of filter weights
 
def conv_3x3_trim(x, weights):
  return np.array([[(x[i-1:i+2, j-1:j+2]*weights).sum() 
                    for j in xrange(1, x.shape[1] -2)]
                    for i in xrange(1, x.shape[0] -2)])
 

x = np.random.randn(500,500)
w = np.random.randn(3,3)

compare_perf(conv_3x3_trim, [x,w])

Пример #7
0
def clamp(i, offset, maxval):
    j = max(0, i + offset)
    return min(j, maxval)


def reflect(pos, offset, bound):
    idx = pos+offset
    return min(2*(bound-1)-idx,max(idx,-idx))
 

def conv(x, weights, mode=clamp):
    sx = x.shape
    sw = weights.shape
    result = np.zeros_like(x)
    for i in xrange(sx[0]):
        for j in xrange(sx[1]):
            for ii in xrange(sw[0]):
                for jj in xrange(sw[1]):
                    idx = mode(i,ii-sw[0]/2,sw[0]), mode(j,jj-sw[0]/2,sw[0])
                    result[i,j] += x[idx] * weights[ii,jj] 
    return result


xsize = (300,300)
x = np.random.randn(*xsize)
wsize = (5,5)
w = np.random.randn(*wsize)

compare_perf(conv, [x,w])
Пример #8
0
import numpy as np

def dists(X,Y):
  return np.array([[np.sum( (x-y) ** 2) for x in X] for y in Y])

d = 100
X = np.random.randn(1000,d)
Y = np.random.randn(200,d)

from timer import compare_perf
compare_perf(dists, [X,Y])


Пример #9
0
    return distance_matrix


def arc_distance_numpy_broadcast(a, b):
    """
    Calculates the pairwise arc distance between all points in vector a and b.
    """
    theta_1 = a[:, 0][:, None]
    theta_2 = b[:, 0][None, :]
    phi_1 = a[:, 1][:, None]
    phi_2 = b[:, 1][None, :]

    temp = (np.sin((theta_2 - theta_1) / 2)**2
            +
            np.cos(theta_1) * np.cos(theta_2)
            * np.sin((phi_2 - phi_1) / 2)**2)
    distance_matrix = 2 * (np.arctan2(np.sqrt(temp), np.sqrt(1 - temp)))
    return distance_matrix

from timer import compare_perf 

n = 1000 
import numpy as np
a = np.random.rand(n, 2)
b = np.random.rand(n, 2)

compare_perf(arc_distance_python_nested_for_loops, [a,b])
compare_perf(arc_distance_numpy_broadcast, [a,b])
compare_perf(arc_distance_numpy_tile, [a,b])
Пример #10
0
# Simple convolution of 3x3 patches from a given array x
# by a 3x3 array of filter weights
 
def conv_3x3_trim(x, weights):
  return np.array([[(x[i-1:i+2, j-1:j+2]*weights).sum() 
                    for j in xrange(1, x.shape[1] -2)]
                    for i in xrange(1, x.shape[0] -2)])
 

x = np.random.randn(500,500)
w = np.random.randn(3,3)
# compare_perf(conv_3x3_trim, [x,w])


w = np.random.randn(5,5)
# Simple convolution of 5x5 patches from a given array x
# by a 5x5 array of filter weights
 
def conv_3x3_trim_loops(image, weights):
  result = np.zeros_like(image)
  for i in xrange(3,x.shape[0]-3):
    for j in xrange(3,x.shape[1]-3):
      for ii in xrange(5): 
        for jj in xrange(5):
          result[i,j] += image[i-ii+2, j-jj+2] * weights[ii, jj] 
  return result


compare_perf(conv_3x3_trim_loops, [x,w])
Пример #11
0
      min_dist = dist(x, C[0, :]) 
      min_idx = 0 
      for cidx in xrange(1,k):
        centroid = C[cidx,:]
        curr_dist = np.sum(x-centroid)**2
        if curr_dist < min_dist:
          min_dist = curr_dist
          min_idx = cidx
      A[i] = min_idx
    # recompute the clusters by averaging data points 
    # assigned to them 
    for cidx in xrange(k):
      # reset centroids
      for dim_idx in xrange(ndims):
        C[cidx, dim_idx] = 0
      # add each data point only to its assigned centroid
      cluster_count = 0
      for i in xrange(n):
        if A[i] == cidx:
          C[cidx, :] += X[i, :]
          cluster_count += 1
      C[cidx, :] /= cluster_count 
  return C      

n, d = 10**3, 100
X = np.random.randn(n,d)
k = 5

from timer import compare_perf
compare_perf(kmeans_loops, [X, k, 10])
Пример #12
0
import numpy as np

def dists(X,Y):
  result = np.zeros( (X.shape[0], Y.shape[0]), dtype=X.dtype)
  for i in xrange(X.shape[0]):
    for j in xrange(Y.shape[0]):
      result[i,j] = np.sum( (X[i,:] - Y[j,:]) ** 2)
  return result 

d = 100
X = np.random.randn(1000,d)
Y = np.random.randn(200,d)

import timer 
timer.compare_perf(dists, [X,Y])


Пример #13
0
def clamp(i, offset, maxval):
    j = max(0, i + offset)
    return min(j, maxval)


def reflect(pos, offset, bound):
    idx = pos+offset
    return min(2*(bound-1)-idx,max(idx,-idx))
 

def conv(x, weights, mode=clamp):
    sx = x.shape
    sw = weights.shape
    result = np.zeros_like(x)
    for i in xrange(sx[0]):
        for j in xrange(sx[1]):
            for ii in xrange(sw[0]):
                for jj in xrange(sw[1]):
                    idx = mode(i,ii-sw[0]/2,sw[0]), mode(j,jj-sw[0]/2,sw[0])
                    result[i,j] += x[idx] * weights[ii,jj] 
    return result


xsize = (300,300)
x = np.random.randn(*xsize)
wsize = (5,5)
w = np.random.randn(*wsize)

# Nmba can't run this benchmark yet 
compare_perf(conv, [x,w], numba=False)
Пример #14
0
        for xidx in xrange(ndims):
          curr_dist += (x[xidx] - centroid[xidx])**2
        if curr_dist < min_dist:
          min_dist = curr_dist
          min_idx = cidx
      A[i] = min_idx
    # recompute the clusters by averaging data points 
    # assigned to them 
    for cidx in xrange(k):
      # reset centroids
      for dim_idx in xrange(ndims):
        C[cidx, dim_idx] = 0
      # add each data point only to its assigned centroid
      cluster_count = 0
      for i in xrange(n):
        if A[i] == cidx:
          C[cidx, :] += X[i, :]
          cluster_count += 1
      C[cidx, :] /= cluster_count 
  return C      

n, d = 10**4, 50
X = np.random.randn(n,d)
k = 25

from timer import compare_perf

compare_perf(kmeans_comprehensions, [X, k, 5],cpython=False)

compare_perf(kmeans_loops, [X, k, 5], cpython=True)