def test_approximate_range_finder(rows, cols, rank, dtype, piter_normalizer, rgen):
# only guaranteed to work for low-rank matrices
if rank is 'fullrank':
return
rf_size = rank + 10
assert min(rows, cols) > rf_size
A = mptest.random_lowrank(rows, cols, rank, randstate=rgen, dtype=dtype)
A /= np.linalg.norm(A, ord='fro')
Q = em.approx_range_finder(A, rf_size, 7, randstate=rgen,
piter_normalizer=piter_normalizer)
Q = np.asmatrix(Q)
assert Q.shape == (rows, rf_size)
normdist = np.linalg.norm(A - Q * (Q.H * A), ord='fro')
assert normdist < 1e-7
python类asmatrix()的实例源码
def train(self):
eps = 1e-10
for i in range(self.epo):
if i % 1 == 0:
self.show_error()
A = np.asarray(self.A.copy())
Z = np.asarray(self.Z.copy())
start = time.time()
Z1 = np.multiply(Z, np.asarray(self.A.transpose() * self.Y))
Z = np.divide(Z1, eps + np.asarray(self.A.transpose() * self.A * self.Z)) # + eps to avoid divided by 0
self.Z = np.asmatrix(Z)
A1 = np.multiply(A, np.asarray( self.Y * self.Z.transpose()))
A = np.divide(A1, eps + np.asarray( self.A * self.Z * self.Z.transpose()))
end = time.time()
self.A = np.asmatrix(A)
self.time = self.time + end - start
def extractVecs():
## Pandas read_csv breaks while reading text file. Very buggy. Manually read each line.
t0 = time.clock()
with open(options.pretrained,'r') as f:
content = [item.rstrip().lower().split(' ') for item in f.readlines()]
globalWordFile = np.asmatrix(content,dtype = str)
globalWordTokens = globalWordFile[:,0].astype('str')
globalWordVectors = globalWordFile[:,1:].astype(np.float)
globalWordFile = None
### Pandas read_csv implementation - Broken
#globalWordFile = pd.read_csv(options.pretrained,delimiter = ' ', header = None)
#globalWordVectors = globalWordFile.ix[:,1:]
#globalWordTokens = globalWordFile.ix[:,0]
#globalWordFile = None
print time.clock() - t0, " seconds taken for loading and slicing gLoVe Word Vectors"
return globalWordTokens,globalWordVectors
def random_portfolio(returns):
def rand_weights(n):
# Produces n random weights that sum to 1
k = np.random.rand(n)
return k / sum(k)
'''
Returns the mean and standard deviation of returns for a random portfolio
'''
p = np.asmatrix(np.mean(returns, axis=1))
w = np.asmatrix(rand_weights(returns.shape[0]))
C = np.asmatrix(np.cov(returns))
mu = w * p.T
sigma = np.sqrt(w * C * w.T)
# This recursion reduces outliers to keep plots pretty
if sigma > 2:
return random_portfolio(returns)
return mu, sigma
def shared_mnist():
def shared_dataset(data_xy):
data_x, data_y = data_xy
np_y = np.zeros((len(data_y), 10), dtype=theano.config.floatX)
for i in xrange(len(data_y)):
np_y[i, data_y[i]] = 1
shared_x = theano.shared(np.asmatrix(data_x, dtype=theano.config.floatX))
shared_y = theano.shared(np.asmatrix(np_y, dtype=theano.config.floatX))
return shared_x, shared_y
f = gzip.open(curr_path + "/data/mnist.pkl.gz", "rb")
train_set, valid_set, test_set = cPickle.load(f)
f.close()
test_set_x, test_set_y = shared_dataset(test_set)
valid_set_x, valid_set_y = shared_dataset(valid_set)
train_set_x, train_set_y = shared_dataset(train_set)
return [train_set_x, train_set_y], [valid_set_x, valid_set_y], [test_set_x, test_set_y]
def testHorzConvWithVaryingImage(self):
image = np.asmatrix(('1.0 2.0 3.0;'
'1.1 2.0 4.0;'
'-4.3 0.0 8.9'))
expected = np.asmatrix(('-1.0 -1.0;'
'-0.9 -2.0;'
'-4.3 -8.9'))
expected = np.reshape(np.asarray(expected), (1, 3, 2, 1))
tf_image = tf.constant(image, shape=(1, 3, 3, 1), dtype=tf.float32)
horz_gradients = tf.contrib.layers.conv2d_in_plane(
tf_image,
weights_initializer=tf.constant_initializer([1, -1]),
kernel_size=[1, 2],
padding='VALID',
activation_fn=None)
init_op = tf.initialize_all_variables()
with self.test_session() as sess:
sess.run(init_op)
result = sess.run(horz_gradients)
self.assertAllClose(result, expected, rtol=1e-5, atol=1e-5)
def testVertConvWithVaryingImage(self):
image = np.asmatrix(('1.0 2.0 3.0;'
'1.1 2.0 4.0;'
'-4.3 0.0 8.9'))
expected = np.asmatrix(('-0.1 0.0 -1.0;'
' 5.4 2.0 -4.9'))
expected = np.reshape(np.asarray(expected), (1, 2, 3, 1))
tf_image = tf.constant(image, shape=(1, 3, 3, 1), dtype=tf.float32)
vert_gradients = tf.contrib.layers.conv2d_in_plane(
tf_image,
weights_initializer=tf.constant_initializer([1, -1]),
kernel_size=[2, 1],
padding='VALID',
activation_fn=None)
init_op = tf.initialize_all_variables()
with self.test_session() as sess:
sess.run(init_op)
result = sess.run(vert_gradients)
self.assertAllClose(result, expected, rtol=1e-5, atol=1e-5)
def testHorzConvWithVaryingImage(self):
image = np.asmatrix(('1.0 2.0 3.0;'
'1.1 2.0 4.0;'
'-4.3 0.0 8.9'))
expected = np.asmatrix(('-1.0 -1.0;'
'-0.9 -2.0;'
'-4.3 -8.9'))
expected = np.reshape(np.asarray(expected), (1, 3, 2, 1))
tf_image = tf.constant(image, shape=(1, 3, 3, 1), dtype=tf.float32)
horz_gradients = tf.contrib.layers.conv2d_in_plane(
tf_image,
weights_initializer=tf.constant_initializer([1, -1]),
kernel_size=[1, 2],
padding='VALID',
activation_fn=None)
init_op = tf.global_variables_initializer()
with self.test_session() as sess:
sess.run(init_op)
result = sess.run(horz_gradients)
self.assertAllClose(result, expected, rtol=1e-5, atol=1e-5)
def testVertConvWithVaryingImage(self):
image = np.asmatrix(('1.0 2.0 3.0;'
'1.1 2.0 4.0;'
'-4.3 0.0 8.9'))
expected = np.asmatrix(('-0.1 0.0 -1.0;'
' 5.4 2.0 -4.9'))
expected = np.reshape(np.asarray(expected), (1, 2, 3, 1))
tf_image = tf.constant(image, shape=(1, 3, 3, 1), dtype=tf.float32)
vert_gradients = tf.contrib.layers.conv2d_in_plane(
tf_image,
weights_initializer=tf.constant_initializer([1, -1]),
kernel_size=[2, 1],
padding='VALID',
activation_fn=None)
init_op = tf.global_variables_initializer()
with self.test_session() as sess:
sess.run(init_op)
result = sess.run(vert_gradients)
self.assertAllClose(result, expected, rtol=1e-5, atol=1e-5)
def fkine(self, stance, unit='rad', apply_stance=False, actor_list=None, timer=None):
"""
Calculates forward kinematics for a list of joint angles.
:param stance: stance is list of joint angles.
:param unit: unit of input angles.
:param apply_stance: If True, then applied tp actor_list.
:param actor_list: Passed to apply transformations computed by fkine.
:param timer: internal use only (for animation).
:return: homogeneous transformation matrix.
"""
if type(stance) is np.ndarray:
stance = np.asmatrix(stance)
if unit == 'deg':
stance = stance * pi / 180
if timer is None:
timer = 0
t = self.base
for i in range(self.length):
if apply_stance:
actor_list[i].SetUserMatrix(transforms.np2vtk(t))
t = t * self.links[i].A(stance[timer, i])
t = t * self.tool
if apply_stance:
actor_list[self.length].SetUserMatrix(transforms.np2vtk(t))
return t
def rotx(theta, unit="rad"):
"""
ROTX gives rotation about X axis
:param theta: angle for rotation matrix
:param unit: unit of input passed. 'rad' or 'deg'
:return: rotation matrix
rotx(THETA) is an SO(3) rotation matrix (3x3) representing a rotation
of THETA radians about the x-axis
rotx(THETA, "deg") as above but THETA is in degrees
"""
check_args.unit_check(unit)
if unit == "deg":
theta = theta * math.pi / 180
ct = math.cos(theta)
st = math.sin(theta)
mat = np.matrix([[1, 0, 0], [0, ct, -st], [0, st, ct]])
mat = np.asmatrix(mat.round(15))
return mat
# ---------------------------------------------------------------------------------------#
def roty(theta, unit="rad"):
"""
ROTY Rotation about Y axis
:param theta: angle for rotation matrix
:param unit: unit of input passed. 'rad' or 'deg'
:return: rotation matrix
roty(THETA) is an SO(3) rotation matrix (3x3) representing a rotation
of THETA radians about the y-axis
roty(THETA, "deg") as above but THETA is in degrees
"""
check_args.unit_check(unit)
if unit == "deg":
theta = theta * math.pi / 180
ct = math.cos(theta)
st = math.sin(theta)
mat = np.matrix([[ct, 0, st], [0, 1, 0], [-st, 0, ct]])
mat = np.asmatrix(mat.round(15))
return mat
# ---------------------------------------------------------------------------------------#
def trotx(theta, unit="rad", xyz=[0, 0, 0]):
"""
TROTX Rotation about X axis
:param theta: rotation in radians or degrees
:param unit: "rad" or "deg" to indicate unit being used
:param xyz: the xyz translation, if blank defaults to [0,0,0]
:return: homogeneous transform matrix
trotx(THETA) is a homogeneous transformation (4x4) representing a rotation
of THETA radians about the x-axis.
trotx(THETA, 'deg') as above but THETA is in degrees
trotx(THETA, 'rad', [x,y,z]) as above with translation of [x,y,z]
"""
check_args.unit_check(unit)
tm = rotx(theta, unit)
tm = np.r_[tm, np.zeros((1, 3))]
mat = np.c_[tm, np.array([[xyz[0]], [xyz[1]], [xyz[2]], [1]])]
mat = np.asmatrix(mat.round(15))
return mat
# ---------------------------------------------------------------------------------------#
def troty(theta, unit="rad", xyz=[0, 0, 0]):
"""
TROTY Rotation about Y axis
:param theta: rotation in radians or degrees
:param unit: "rad" or "deg" to indicate unit being used
:param xyz: the xyz translation, if blank defaults to [0,0,0]
:return: homogeneous transform matrix
troty(THETA) is a homogeneous transformation (4x4) representing a rotation
of THETA radians about the y-axis.
troty(THETA, 'deg') as above but THETA is in degrees
troty(THETA, 'rad', [x,y,z]) as above with translation of [x,y,z]
"""
check_args.unit_check(unit)
tm = roty(theta, unit)
tm = np.r_[tm, np.zeros((1, 3))]
mat = np.c_[tm, np.array([[xyz[0]], [xyz[1]], [xyz[2]], [1]])]
mat = np.asmatrix(mat.round(15))
return mat
# ---------------------------------------------------------------------------------------#
def trotz(theta, unit="rad", xyz=[0, 0, 0]):
"""
TROTZ Rotation about Z axis
:param theta: rotation in radians or degrees
:param unit: "rad" or "deg" to indicate unit being used
:param xyz: the xyz translation, if blank defaults to [0,0,0]
:return: homogeneous transform matrix
trotz(THETA) is a homogeneous transformation (4x4) representing a rotation
of THETA radians about the z-axis.
trotz(THETA, 'deg') as above but THETA is in degrees
trotz(THETA, 'rad', [x,y,z]) as above with translation of [x,y,z]
"""
check_args.unit_check(unit)
tm = rotz(theta, unit)
tm = np.r_[tm, np.zeros((1, 3))]
mat = np.c_[tm, np.array([[xyz[0]], [xyz[1]], [xyz[2]], [1]])]
mat = np.asmatrix(mat.round(15))
return mat
# ---------------------------------------------------------------------------------------#
def m8_motif(A):
B, U, G = directional_breakup(A)
W = np.zeros(G.shape)
W = np.asmatrix(W)
N = G.shape[0]
# print('U\n', U)
for i in range(N):
J = np.nonzero(U[i, :])[1]
# print(J)
for j1 in range(len(J)):
for j2 in range(j1 + 1, len(J)):
k1 = J[j1]
k2 = J[j2]
# print(k1, k2)
if A[k1, k2] == 0 and A[k2, k1] == 0:
W[i, k1] = W[i, k1] + 1
W[i, k2] = W[i, k2] + 1
W[k1, k2] = W[k1, k2] + 1
W = W + W.T
# matlab use W = sparse(W + W')
# I think it is properly use W = W+W'T
return W
def __init__(self, shape, optimizer='seq', lr=0.01, clip_min=0, clip_max=1081, is_round=True):
'''
param shape: phi_x shape
'''
self.optimizer = optimizer
self.lr = lr
self.shape = shape
self.learning_rate_decay = 0.99
# w is Mx1 column vector
self.w = np.asmatrix(self._init_weight(shape + (1,)))
# Setup model
self._setup_model()
# Setup optimizer (vary from models)
self._setup_optimizer()
def back_propagation(self,gradient):
gradient_activation = self.cal_gradient() # i * i ?
gradient = np.asmatrix(np.dot(gradient.T,gradient_activation))
self._gradient_weight = np.asmatrix(self.xdata)
self._gradient_bias = -1
self._gradient_x = self.weight
self.gradient_weight = np.dot(gradient.T,self._gradient_weight.T)
self.gradient_bias = gradient * self._gradient_bias
self.gradient = np.dot(gradient,self._gradient_x).T
# ----------------------upgrade
# -----------the Negative gradient direction --------
self.weight = self.weight - self.learn_rate * self.gradient_weight
self.bias = self.bias - self.learn_rate * self.gradient_bias.T
return self.gradient
def pooling(self,featuremaps,size_pooling,type='average_pool'):
#pooling process
size_map = len(featuremaps[0])
size_pooled = int(size_map/size_pooling)
featuremap_pooled = []
for i_map in range(len(featuremaps)):
map = featuremaps[i_map]
map_pooled = []
for i_focus in range(0,size_map,size_pooling):
for j_focus in range(0, size_map, size_pooling):
focus = map[i_focus:i_focus + size_pooling, j_focus:j_focus + size_pooling]
if type == 'average_pool':
#average pooling
map_pooled.append(np.average(focus))
elif type == 'max_pooling':
#max pooling
map_pooled.append(np.max(focus))
map_pooled = np.asmatrix(map_pooled).reshape(size_pooled,size_pooled)
featuremap_pooled.append(map_pooled)
return featuremap_pooled
def make_symmetric_lower(mat):
'''
Copies the matrix entries below the main diagonal to the upper triangle
half of the matrix. Leaves the diagonal unchanged. Returns a `NumPy` matrix
object.
**mat** : `numpy.matrix`
A lower diagonal matrix.
returns : `numpy.matrix`
The lower triangle matrix.
'''
# extract lower triangle from matrix (including diagonal)
tmp_mat = np.tril(mat)
# if the matrix given wasn't a lower triangle matrix, raise an error
if (mat != tmp_mat).all():
raise Exception('Matrix to symmetrize is not a lower diagonal matrix.')
# add its transpose to itself, zeroing the diagonal to avoid doubling
tmp_mat += np.triu(tmp_mat.transpose(), 1)
return np.asmatrix(tmp_mat)
def get_error_matrix(self, correlation=False): # VIEWED TODO
'''Retrieves the parameter error matrix from iminuit.
correlation : boolean (optional, default ``False``)
If ``True``, return correlation matrix, else return
covariance matrix.
return : `numpy.matrix`
'''
# get parameter covariance matrix from iminuit
# FIX_UPSTREAM we need skip_fixed=False, but this is unsupported
#_mat = self.__iminuit.matrix(correlation, skip_fixed=False)
# ... so use skip_fixed=False instead and fill in the gaps
_mat = self.__iminuit.matrix(correlation, skip_fixed=True)
_mat = np.asmatrix(_mat) # reshape into numpy matrix
_mat = self._fill_in_zeroes_for_fixed(_mat) # fill in fixed par 'gaps'
return _mat
def _mvee(self, points, tolerance):
# Taken from: http://stackoverflow.com/questions/14016898/port-matlab-bounding-ellipsoid-code-to-python
points = np.asmatrix(points)
n, d = points.shape
q = np.column_stack((points, np.ones(n))).T
err = tolerance + 1.0
u = np.ones(n) / n
while err > tolerance:
# assert u.sum() == 1 # invariant
x = q * np.diag(u) * q.T
m = np.diag(q.T * la.inv(x) * q)
jdx = np.argmax(m)
step_size = (m[jdx] - d - 1.0) / ((d + 1) * (m[jdx] - 1.0))
new_u = (1 - step_size) * u
new_u[jdx] += step_size
err = la.norm(new_u - u)
u = new_u
c = u * points
a = la.inv(points.T * np.diag(u) * points - c.T * c) / d
return np.asarray(a), np.squeeze(np.asarray(c))
def word_sequence(f_path, batch_size = 1, i2w, w2i):
test_seqs = []
lines = []
tf = {}
f = open(curr_path + "/" + f_path, "r")
for line in f:
line = line.strip('\n').lower()
words = ['<soss>']+line_x.split()+["<eoss>"]
lines.append(words)
f.close()
for i in range(0, len(lines)):
words = lines[i]
x = np.zeros((len(words), len(w2i)), dtype = theano.config.floatX)
for j in range(0, len(words)):
if words[j] in w2i:
x[j, w2i[words[j]]] = 1
test_seqs.append(np.asmatrix(x))
test_data_x = batch_sequences(test_seqs, i2w, w2i, batch_size)
return test_seqs, i2w, w2i, test_data_x
def sim(A, B, time, x0, controller):
x = np.asmatrix(x0)
prev_y = np.dot(H, x)
x_out = []
x_hat_out = []
u_out = []
time_out = []
for t in xrange(time):
x_hat = x + ((np.random.random((2,1)) - 0.5) * 0.05)
y = np.dot(H, x_hat)
u = np.clip(controller(y, prev_y), -40, 40)
x = np.dot(A, x) + np.dot(B, u)
x_out.append(x)
x_hat_out.append(x_hat)
u_out.append(u)
time_out.append(t*dt)
prev_y = np.copy(y)
return (np.asarray(time_out), np.asarray(x_out), np.asarray(x_hat_out), np.asarray(u_out))
def _beta_cal(self,observations,c_scale):
# Calculate Beta maxtrix
num_states = self.em_prob.shape[0]
total_stages = len(observations)
# Initialize values
ob_ind = self.obs_map[ observations[total_stages-1] ]
beta = np.asmatrix(np.zeros((num_states,total_stages)))
# Handle beta base case
beta[:,total_stages-1] = c_scale[total_stages-1]
# Iteratively calculate beta(t) for all 't'
for curr_t in range(total_stages-1,0,-1):
ob_ind = self.obs_map[observations[curr_t]]
beta[:,curr_t-1] = np.multiply( beta[:,curr_t] , self.em_prob[:,ob_ind] )
beta[:,curr_t-1] = np.dot( self.trans_prob, beta[:,curr_t-1] )
beta[:,curr_t-1] = np.multiply( beta[:,curr_t-1] , c_scale[curr_t -1 ] )
# return the computed beta
return beta
def _train_emission(self,observations):
# Initialize matrix
new_em_prob = np.asmatrix(np.zeros(self.em_prob.shape))
# Indexing position of unique observations in the observation sequence
selectCols=[]
for i in range(self.em_prob.shape[1]):
selectCols.append([])
for i in range(len(observations)):
selectCols[ self.obs_map[observations[i]] ].append(i)
# Calculate delta matrix
delta = self._forward_backward(observations)
# Sum the rowise of delta matrix, which gives probability of a particular state
totalProb = np.sum(delta,axis=1)
# Re-estimate emission matrix
for i in range(self.em_prob.shape[0]):
for j in range(self.em_prob.shape[1]):
new_em_prob[i,j] = np.sum(delta[i,selectCols[j]])/totalProb[i]
return new_em_prob
def _train_transition(self,observations):
# Initialize transition matrix
new_trans_prob = np.asmatrix(np.zeros(self.trans_prob.shape))
# Find alpha and beta
alpha,c = self._alpha_cal(observations)
beta = self._beta_cal(observations,c)
# calculate transition matrix values
for t in range(len(observations)-1):
temp1 = np.multiply(alpha[:,t],beta[:,t+1].transpose())
temp1 = np.multiply(self.trans_prob,temp1)
new_trans_prob = new_trans_prob + np.multiply(temp1,self.em_prob[:,self.obs_map[observations[t+1]]].transpose())
# Normalize values so that sum of probabilities is 1
for i in range(self.trans_prob.shape[0]):
new_trans_prob[i,:] = new_trans_prob[i,:]/np.sum(new_trans_prob[i,:])
return new_trans_prob
def add_pixels(self, uv_px, img1d, weight=None):
# Lookup row & column for each in-bounds coordinate.
mask = self.get_mask(uv_px)
xx = uv_px[0,mask]
yy = uv_px[1,mask]
# Update matrix according to assigned weight.
if weight is None:
img1d[mask] = self.img[yy,xx]
elif np.isscalar(weight):
img1d[mask] += self.img[yy,xx] * weight
else:
w1 = np.asmatrix(weight, dtype='float32')
w3 = w1.transpose() * np.ones((1,3))
img1d[mask] += np.multiply(self.img[yy,xx], w3[mask])
# A panorama image made from several FisheyeImage sources.
# TODO: Add support for supersampled anti-aliasing filters.
def coherence_of_columns(A):
"""Mutual coherence of columns of A.
Parameters
----------
A : array_like
Input matrix.
p : int, optional
p-th norm.
Returns
-------
array_like
Mutual coherence of columns of A.
"""
A = np.asmatrix(A)
_, N = A.shape
A = A * np.asmatrix(np.diag(1/norm_of_columns(A)))
Gram_A = A.H*A
for j in range(N):
Gram_A[j, j] = 0
return np.max(np.abs(Gram_A))
layers_test.py 文件源码
项目:DeepLearning_VirtualReality_BigData_Project
作者: rashmitripathi
项目源码
文件源码
阅读 29
收藏 0
点赞 0
评论 0
def testHorzConvWithVaryingImage(self):
image = np.asmatrix(('1.0 2.0 3.0;' '1.1 2.0 4.0;' '-4.3 0.0 8.9'))
expected = np.asmatrix(('-1.0 -1.0;' '-0.9 -2.0;' '-4.3 -8.9'))
expected = np.reshape(np.asarray(expected), (1, 3, 2, 1))
tf_image = constant_op.constant(
image, shape=(1, 3, 3, 1), dtype=dtypes.float32)
horz_gradients = layers_lib.conv2d_in_plane(
tf_image,
weights_initializer=init_ops.constant_initializer([1, -1]),
kernel_size=[1, 2],
padding='VALID',
activation_fn=None)
init_op = variables_lib.global_variables_initializer()
with self.test_session() as sess:
sess.run(init_op)
result = sess.run(horz_gradients)
self.assertAllClose(result, expected, rtol=1e-5, atol=1e-5)