def kron(a,b): """kronecker product of a and b Kronecker product of two arrays is block array [[ a[ 0 ,0]*b, a[ 0 ,1]*b, ... , a[ 0 ,n-1]*b ], [ ... ... ], [ a[m-1,0]*b, a[m-1,1]*b, ... , a[m-1,n-1]*b ]] """ wrapper = get_array_wrap(a, b) b = asanyarray(b) a = array(a,copy=False,subok=True,ndmin=b.ndim) ndb, nda = b.ndim, a.ndim if (nda == 0 or ndb == 0): return _nx.multiply(a,b) as_ = a.shape bs = b.shape if not a.flags.contiguous: a = reshape(a, as_) if not b.flags.contiguous: b = reshape(b, bs) nd = ndb if (ndb != nda): if (ndb > nda): as_ = (1,)*(ndb-nda) + as_ else: bs = (1,)*(nda-ndb) + bs nd = nda result = outer(a,b).reshape(as_+bs) axis = nd-1 for _ in xrange(nd): result = concatenate(result, axis=axis) if wrapper is not None: result = wrapper(result) return result
def colorfulness(provider): """ colorfulness see: TODO """ img = provider.img if 3 != img.ndim: return None s = img.shape len = s[0] * s[1] r = reshape(img[...,0] , (len,)) / 255.0 g = reshape(img[...,1] , (len,)) / 255.0 b = reshape(img[...,2] , (len,)) / 255.0 rg = r - g yb = 0.5 * ( r + g ) - b sd_rgyb = ( rg.std() ** 2 + yb.std() ** 2 ) ** 0.5 m_rgyb = ( rg.mean() **2 + yb.mean() ** 2 ) ** 0.5 return sd_rgyb + 3.0 * m_rgyb
def quantize(img, palette, density=True, size=0.10): # resize image img = imresize(img, size) #img = imresize(img, size) # reshape to array of points pixels = reshape(img,(img.shape[0]*img.shape[1],3)) #print pixels.shape #whitened = whiten(pixels) #print whitened.shape #print whitened # custom pallete or adaptive #if colors is None: # centroids, _ = kmeans(pixels, 8) #print "centroid shape" #print centroids.shape #else: # centroids = colors # quantize qnt, _ = vq(pixels, palette) #print qnt # reshape back to image centers_idx = reshape(qnt,(img.shape[0], img.shape[1])) clustered = palette[centers_idx] #if False: # print "centroids" # print centroids.shape # print centroids # # print "quantize" # print qnt.shape # print qnt # # print "centers_idx" # print centers_idx.shape # print centers_idx # # print "clusters" # print clustered.shape # print clustered h, _ = np.histogram(qnt, len(palette), range=(0,len(palette)-1), normed=density) #print h return clustered, h
def _entropy_rgb(img): """ http://en.wikipedia.org/wiki/Entropy_estimation#Histogram_estimator http://stackoverflow.com/questions/5524179/how-to-detect-motion-between-two-pil-images-wxpython-webcam-integration-exampl """ if 3 == img.ndim: print "shape", img.shape pixels = reshape(img, (img.shape[0] * img.shape[1], 3)) h,e = np.histogramdd(pixels, bins=(16,)*3, range=((0,256),)*3) prob = h/np.sum(h) # normalize prob = prob[prob>0] # remove zeros return -np.sum(prob*np.log2(prob)) else: return None
def entropy_sv(provider): """ entropy of the saturation and value channels in HSV space """ if 3 != provider.img.ndim: return None hsv = provider.as_hsv() if 3 == hsv.ndim: pixels = hsv[...,1:] pixels = reshape(pixels, (pixels.shape[0] * pixels.shape[1], 2)) h,e = np.histogramdd(pixels, bins=(16,)*2, range=((0,1.0),)*2) prob = h/np.sum(h) # normalize prob = prob[prob>0] # remove zeros return -np.sum(prob*np.log2(prob)) else: return None
def entropy_ab(provider): """ entropy of the AB channels in the LAB space """ if 3 != provider.img.ndim: return None lab = provider.as_lab() if 3 == lab.ndim: pixels = lab[...,1:] + 127 pixels = reshape(pixels, (pixels.shape[0] * pixels.shape[1], 2)) h,e = np.histogramdd(pixels, bins=(16,)*2, range=((0,255),)*2) prob = h/np.sum(h) # normalize prob = prob[prob>0] # remove zeros return -np.sum(prob*np.log2(prob)) else: return None
def entropy(img): """ entropy for a single channel http://en.wikipedia.org/wiki/Entropy_estimation#Histogram_estimator http://stackoverflow.com/questions/5524179/how-to-detect-motion-between-two-pil-images-wxpython-webcam-integration-exampl """ # reshape to single dim array pixels = reshape(img, (img.shape[0] * img.shape[1], 1)) # histogram with 16 bins h, _ = np.histogram(pixels, bins=16, range=(0.0,1.0)) # normalize prob = 1.0 * h / np.sum(h) # remove zeroes prob = prob[prob > 0.0] # calc entropy return -np.sum(prob * np.log2(prob))
def kron(a,b): """ Kronecker product of two arrays. Computes the Kronecker product, a composite array made of blocks of the second array scaled by the first. Parameters ---------- a, b : array_like Returns ------- out : ndarray See Also -------- outer : The outer product Notes ----- The function assumes that the number of dimenensions of `a` and `b` are the same, if necessary prepending the smallest with ones. If `a.shape = (r0,r1,..,rN)` and `b.shape = (s0,s1,...,sN)`, the Kronecker product has shape `(r0*s0, r1*s1, ..., rN*SN)`. The elements are products of elements from `a` and `b`, organized explicitly by:: kron(a,b)[k0,k1,...,kN] = a[i0,i1,...,iN] * b[j0,j1,...,jN] where:: kt = it * st + jt, t = 0,...,N In the common 2-D case (N=1), the block structure can be visualized:: [[ a[0,0]*b, a[0,1]*b, ... , a[0,-1]*b ], [ ... ... ], [ a[-1,0]*b, a[-1,1]*b, ... , a[-1,-1]*b ]] Examples -------- >>> np.kron([1,10,100], [5,6,7]) array([ 5, 6, 7, 50, 60, 70, 500, 600, 700]) >>> np.kron([5,6,7], [1,10,100]) array([ 5, 50, 500, 6, 60, 600, 7, 70, 700]) >>> np.kron(np.eye(2), np.ones((2,2))) array([[ 1., 1., 0., 0.], [ 1., 1., 0., 0.], [ 0., 0., 1., 1.], [ 0., 0., 1., 1.]]) >>> a = np.arange(100).reshape((2,5,2,5)) >>> b = np.arange(24).reshape((2,3,4)) >>> c = np.kron(a,b) >>> c.shape (2, 10, 6, 20) >>> I = (1,3,0,2) >>> J = (0,2,1) >>> J1 = (0,) + J # extend to ndim=4 >>> S1 = (1,) + b.shape >>> K = tuple(np.array(I) * np.array(S1) + np.array(J1)) >>> c[K] == a[I]*b[J] True """ b = asanyarray(b) a = array(a,copy=False,subok=True,ndmin=b.ndim) ndb, nda = b.ndim, a.ndim if (nda == 0 or ndb == 0): return _nx.multiply(a,b) as_ = a.shape bs = b.shape if not a.flags.contiguous: a = reshape(a, as_) if not b.flags.contiguous: b = reshape(b, bs) nd = ndb if (ndb != nda): if (ndb > nda): as_ = (1,)*(ndb-nda) + as_ else: bs = (1,)*(nda-ndb) + bs nd = nda result = outer(a,b).reshape(as_+bs) axis = nd-1 for _ in range(nd): result = concatenate(result, axis=axis) wrapper = get_array_prepare(a, b) if wrapper is not None: result = wrapper(result) wrapper = get_array_wrap(a, b) if wrapper is not None: result = wrapper(result) return result
def nnCostFunction(nn_params, input_layer_size, hidden_layer_size, num_labels, X, y, Lambda): print nn_params[0:hidden_layer_size * input_layer_size].shape # Reshape the unrolled parameter vector. Remember the bias nodes. theta1 = reshape(nn_params[0:hidden_layer_size * (input_layer_size + 1)], (hidden_layer_size, input_layer_size + 1)) theta2 = reshape(nn_params[hidden_layer_size * (input_layer_size + 1):], (num_labels, hidden_layer_size + 1)) # Number of training sets m = X.shape[0] # Return this J = 0 theta1_grad = zeros(theta1.shape) theta2_grad = zeros(theta2.shape) # Substitute all ys as the data presumes indexing starts at 1 y = y - 1 # From y, craete a matrix with zeros and ones Y = zeros((y.shape[0], num_labels)) for i in range(0, Y.shape[0]): Y[i,y[i]] = 1.0 # Calculate the hypothesis, h (or a3, the activation in layer 3) a1 = concatenate((ones((X.shape[0],1)),X), axis=1) z2 = dot(a1, theta1.transpose()) a2 = concatenate((ones((X.shape[0],1)),sigmoid(z2)), axis=1) z3 = dot(a2, theta2.transpose()) h = sigmoid(z3) # or a3 # s - sum term of J s = (-Y*log(h) - (1.0-Y)*log(1.0-h)).sum() # Don't regularize the bias terms (on index 0) t1 = theta1[:,1:] t2 = theta2[:,1:] # reg - Regularization term of J reg = Lambda/(2*m) * (square(t1).sum() + square(t2).sum()).sum() # J - Cost J = 1.0/m*s + reg # Calculate gradients d3 = h - Y d2 = dot(d3, t2) * sigmoidGradient(z2) grad2 = 1.0/m * dot(d3.transpose(), a2) grad1 = 1.0/m * dot(d2.transpose(), a1) # Calculate the regularization term of the gradients theta1_grad_reg = theta1 theta2_grad_reg = theta2 theta1_grad_reg[:,0] = 0 theta2_grad_reg[:,0] = 0 theta1_grad = grad1 + Lambda/m*theta1_grad_reg theta2_grad = grad2 + Lambda/m*theta2_grad_reg # Unroll gradients grad = concatenate((theta1_grad.flatten(), theta2_grad.flatten()), axis=1) return J, grad
def kron(a, b): """ Kronecker product of two arrays. Computes the Kronecker product, a composite array made of blocks of the second array scaled by the first. Parameters ---------- a, b : array_like Returns ------- out : ndarray See Also -------- outer : The outer product Notes ----- The function assumes that the number of dimensions of `a` and `b` are the same, if necessary prepending the smallest with ones. If `a.shape = (r0,r1,..,rN)` and `b.shape = (s0,s1,...,sN)`, the Kronecker product has shape `(r0*s0, r1*s1, ..., rN*SN)`. The elements are products of elements from `a` and `b`, organized explicitly by:: kron(a,b)[k0,k1,...,kN] = a[i0,i1,...,iN] * b[j0,j1,...,jN] where:: kt = it * st + jt, t = 0,...,N In the common 2-D case (N=1), the block structure can be visualized:: [[ a[0,0]*b, a[0,1]*b, ... , a[0,-1]*b ], [ ... ... ], [ a[-1,0]*b, a[-1,1]*b, ... , a[-1,-1]*b ]] Examples -------- >>> np.kron([1,10,100], [5,6,7]) array([ 5, 6, 7, 50, 60, 70, 500, 600, 700]) >>> np.kron([5,6,7], [1,10,100]) array([ 5, 50, 500, 6, 60, 600, 7, 70, 700]) >>> np.kron(np.eye(2), np.ones((2,2))) array([[ 1., 1., 0., 0.], [ 1., 1., 0., 0.], [ 0., 0., 1., 1.], [ 0., 0., 1., 1.]]) >>> a = np.arange(100).reshape((2,5,2,5)) >>> b = np.arange(24).reshape((2,3,4)) >>> c = np.kron(a,b) >>> c.shape (2, 10, 6, 20) >>> I = (1,3,0,2) >>> J = (0,2,1) >>> J1 = (0,) + J # extend to ndim=4 >>> S1 = (1,) + b.shape >>> K = tuple(np.array(I) * np.array(S1) + np.array(J1)) >>> c[K] == a[I]*b[J] True """ b = asanyarray(b) a = array(a, copy=False, subok=True, ndmin=b.ndim) ndb, nda = b.ndim, a.ndim if (nda == 0 or ndb == 0): return _nx.multiply(a, b) as_ = a.shape bs = b.shape if not a.flags.contiguous: a = reshape(a, as_) if not b.flags.contiguous: b = reshape(b, bs) nd = ndb if (ndb != nda): if (ndb > nda): as_ = (1, ) * (ndb - nda) + as_ else: bs = (1, ) * (nda - ndb) + bs nd = nda result = outer(a, b).reshape(as_ + bs) axis = nd - 1 for _ in range(nd): result = concatenate(result, axis=axis) wrapper = get_array_prepare(a, b) if wrapper is not None: result = wrapper(result) wrapper = get_array_wrap(a, b) if wrapper is not None: result = wrapper(result) return result
def kron(a, b): """ Kronecker product of two arrays. Computes the Kronecker product, a composite array made of blocks of the second array scaled by the first. Parameters ---------- a, b : array_like Returns ------- out : ndarray See Also -------- outer : The outer product Notes ----- The function assumes that the number of dimensions of `a` and `b` are the same, if necessary prepending the smallest with ones. If ``a.shape = (r0,r1,..,rN)`` and ``b.shape = (s0,s1,...,sN)``, the Kronecker product has shape ``(r0*s0, r1*s1, ..., rN*SN)``. The elements are products of elements from `a` and `b`, organized explicitly by:: kron(a,b)[k0,k1,...,kN] = a[i0,i1,...,iN] * b[j0,j1,...,jN] where:: kt = it * st + jt, t = 0,...,N In the common 2-D case (N=1), the block structure can be visualized:: [[ a[0,0]*b, a[0,1]*b, ... , a[0,-1]*b ], [ ... ... ], [ a[-1,0]*b, a[-1,1]*b, ... , a[-1,-1]*b ]] Examples -------- >>> np.kron([1,10,100], [5,6,7]) array([ 5, 6, 7, ..., 500, 600, 700]) >>> np.kron([5,6,7], [1,10,100]) array([ 5, 50, 500, ..., 7, 70, 700]) >>> np.kron(np.eye(2), np.ones((2,2))) array([[1., 1., 0., 0.], [1., 1., 0., 0.], [0., 0., 1., 1.], [0., 0., 1., 1.]]) >>> a = np.arange(100).reshape((2,5,2,5)) >>> b = np.arange(24).reshape((2,3,4)) >>> c = np.kron(a,b) >>> c.shape (2, 10, 6, 20) >>> I = (1,3,0,2) >>> J = (0,2,1) >>> J1 = (0,) + J # extend to ndim=4 >>> S1 = (1,) + b.shape >>> K = tuple(np.array(I) * np.array(S1) + np.array(J1)) >>> c[K] == a[I]*b[J] True """ b = asanyarray(b) a = array(a, copy=False, subok=True, ndmin=b.ndim) ndb, nda = b.ndim, a.ndim nd = max(ndb, nda) if (nda == 0 or ndb == 0): return _nx.multiply(a, b) as_ = a.shape bs = b.shape if not a.flags.contiguous: a = reshape(a, as_) if not b.flags.contiguous: b = reshape(b, bs) # Equalise the shapes by prepending smaller one with 1s as_ = (1, ) * max(0, ndb - nda) + as_ bs = (1, ) * max(0, nda - ndb) + bs # Compute the product a_arr = a.reshape(a.size, 1) b_arr = b.reshape(1, b.size) is_any_mat = isinstance(a_arr, matrix) or isinstance(b_arr, matrix) # In case of `mat`, convert result to `array` result = _nx.multiply(a_arr, b_arr, subok=(not is_any_mat)) # Reshape back result = result.reshape(as_ + bs) transposer = _nx.arange(nd * 2).reshape([2, nd]).ravel(order='f') result = result.transpose(transposer) result = result.reshape(_nx.multiply(as_, bs)) return result if not is_any_mat else matrix(result, copy=False)
from numpy.core.fromnumeric import reshape import tensorflow as tf import numpy as np from matplotlib import pyplot as plt N = 100 w_true = 5 b_true = 2 noise_scale = .1 x_np = np.random.rand(N, 1) noise = np.random.normal(scale=noise_scale, size=(N, 1)) y_np = np, reshape(w_true*x_np + b_true + noise, (-1)) plt.title("toy") plt.xlabel("hi") plt.ylabel("hell") plt.scatter(x_np,y_np) plt.show()