def test_reciprocal(input_data):
expected_output = np.reciprocal(input_data)
node = onnx.helper.make_node('Reciprocal', inputs=['x'], outputs=['y'])
ng_results = convert_and_calculate(node, [input_data], [expected_output])
assert np.allclose(ng_results, [expected_output])
python类reciprocal()的实例源码
def testCplxReciprocalGPU(self):
shapes = [(5,4,3), (5,4), (5,), (1,)]
for sh in shapes:
x = ((np.random.randn(*sh) +
1j*np.random.randn(*sh)).astype(np.complex64))
self._compareGpu(x, np.reciprocal, tf.reciprocal)
def testCplxReciprocalGradGPU(self):
shapes = [(5,4,3), (5,4), (5,), (1,)]
for sh in shapes:
x = ((np.random.randn(*sh) +
1j*np.random.randn(*sh)).astype(np.complex64))
self._compareGpuGrad(x, np.reciprocal, tf.reciprocal)
def test_blocked(self):
# test alignments offsets for simd instructions
# alignments for vz + 2 * (vs - 1) + 1
for dt, sz in [(np.float32, 11), (np.float64, 7)]:
for out, inp1, inp2, msg in _gen_alignment_data(dtype=dt,
type='binary',
max_size=sz):
exp1 = np.ones_like(inp1)
inp1[...] = np.ones_like(inp1)
inp2[...] = np.zeros_like(inp2)
assert_almost_equal(np.add(inp1, inp2), exp1, err_msg=msg)
assert_almost_equal(np.add(inp1, 1), exp1 + 1, err_msg=msg)
assert_almost_equal(np.add(1, inp2), exp1, err_msg=msg)
np.add(inp1, inp2, out=out)
assert_almost_equal(out, exp1, err_msg=msg)
inp2[...] += np.arange(inp2.size, dtype=dt) + 1
assert_almost_equal(np.square(inp2),
np.multiply(inp2, inp2), err_msg=msg)
assert_almost_equal(np.reciprocal(inp2),
np.divide(1, inp2), err_msg=msg)
inp1[...] = np.ones_like(inp1)
inp2[...] = np.zeros_like(inp2)
np.add(inp1, 1, out=out)
assert_almost_equal(out, exp1 + 1, err_msg=msg)
np.add(1, inp2, out=out)
assert_almost_equal(out, exp1, err_msg=msg)
def BX(Y,index_PQ, index_P, n_PQ, n_P):
case_number, _ = np.shape(Y)
Y_p = Y.copy()
X_p = np.zeros((case_number, case_number))
B_p = np.zeros((n_P,n_P))
B_pp = np.zeros((n_PQ,n_PQ))
#-------------------------------------------------
for i in xrange(case_number):
Y_p[i][i] = complex(0,0)
for j in xrange(case_number):
if i != j:
Y_p[i][i] -= Y_p[i][j]
B = np.imag(Y_p)
for i in xrange(n_P):
for j in xrange(n_P):
B_p[i][j] = B[index_P[i]][index_P[j]]
#-------------------------------------------------
g_b_round = np.zeros(case_number)
for i in xrange(case_number):
a = np.sum(Y[i])
if LA.norm(a) > 1e-5:
g_b_round[i] = np.reciprocal(np.imag(np.reciprocal(a)))
for i in xrange(case_number):
for j in xrange(case_number):
if LA.norm(Y[i][j]) > 1e-5 and i!=j:
X_p[i][j] = np.reciprocal(np.imag(np.reciprocal(Y[i][j])))
for i in xrange(case_number):
X_p[i][i] = g_b_round[i]
for j in xrange(case_number):
if i != j:
X_p[i][i] -= X_p[i][j]
for i in xrange(0, n_PQ):
for j in xrange(0, n_PQ):
B_pp[i][j] = X_p[index_PQ[i]][index_PQ[j]]
return B_p, - B_pp
# Stott ================================================================================================================
# Stott Original--------------------------------------------------------------------------------------------------------
def XB(Y,index_PQ, index_P, n_PQ, n_P):
case_number, _ = np.shape(Y)
_, B = util.get_G_B(Y)
X = np.zeros((case_number, case_number))
B_p = np.zeros((n_P,n_P))
B_pp = np.zeros((n_PQ,n_PQ))
for i in xrange(case_number):
for j in xrange(case_number):
if LA.norm(Y[i][j]) > 1e-5 and i != j:
X[i][j] = np.reciprocal(np.imag(np.reciprocal(Y[i][j])))
for i in xrange(case_number):
for j in xrange(case_number):
if i != j:
X[i][i] -= X[i][j]
for i in xrange(0, n_P):
for j in xrange(0, n_P):
B_p[i][j] = X[index_P[i]][index_P[j]]
#---------------------------------------------------
for i in xrange(0, n_PQ):
for j in xrange(0, n_PQ):
B_pp[i][j] = B[index_PQ[i]][index_PQ[j]]
return - B_p, B_pp
# Stott count r in B'---------------------------------------------------------------------------------------------------
def XB_ground(Y,index_PQ, index_P, n_PQ, n_P):
case_number, _ = np.shape(Y)
_, B = util.get_G_B(Y)
X_p = np.zeros((case_number, case_number))
B_p = np.zeros((n_P, n_P))
B_pp = np.zeros((n_PQ, n_PQ))
g_b_round = np.zeros(case_number)
for i in xrange(case_number):
a = np.sum(Y[i])
if LA.norm(a) > 1e-5:
g_b_round[i] = np.reciprocal(np.imag(np.reciprocal(a)))
for i in xrange(case_number):
for j in xrange(case_number):
if LA.norm(Y[i][j]) > 1e-5 and i!=j:
X_p[i][j] = np.reciprocal(np.imag(np.reciprocal(Y[i][j])))
for i in xrange(case_number):
X_p[i][i] = g_b_round[i]
for j in xrange(case_number):
if i != j:
X_p[i][i] -= X_p[i][j]
for i in xrange(0, n_P):
for j in xrange(0, n_P):
B_p[i][j] = X_p[index_P[i]][index_P[j]]
# ---------------------------------------------------
for i in xrange(0, n_PQ):
for j in xrange(0, n_PQ):
B_pp[i][j] = B[index_PQ[i]][index_PQ[j]]
return - B_p, B_pp
# Stott ================================================================================================================
# XX--------------------------------------------------------------------------------------------------------------------
def XX(Y,index_PQ, index_P, n_PQ, n_P):
case_number, _ = np.shape(Y)
X_p = np.zeros((case_number, case_number))
B_p = np.zeros((n_P,n_P))
B_pp = np.zeros((n_PQ,n_PQ))
for i in xrange(case_number):
for j in xrange(case_number):
if LA.norm(Y[i][j]) > 1e-5 and i != j:
X_p[i][j] = np.reciprocal(np.imag(np.reciprocal(Y[i][j])))
for i in xrange(case_number):
for j in xrange(case_number):
if i != j:
X_p[i][i] -= X_p[i][j]
for i in xrange(0, n_P):
for j in xrange(0, n_P):
B_p[i][j] = X_p[index_P[i]][index_P[j]]
#-------------------------------------------------
g_b_round = np.zeros(case_number)
for i in xrange(case_number):
a = np.sum(Y[i])
if LA.norm(a) > 1e-5:
g_b_round[i] = np.reciprocal(np.imag(np.reciprocal(a)))
for i in xrange(case_number):
for j in xrange(case_number):
if LA.norm(Y[i][j]) > 1e-5 and i!=j:
X_p[i][j] = np.reciprocal(np.imag(np.reciprocal(Y[i][j])))
for i in xrange(case_number):
X_p[i][i] = g_b_round[i]
for j in xrange(case_number):
if i != j:
X_p[i][i] -= X_p[i][j]
for i in xrange(0, n_PQ):
for j in xrange(0, n_PQ):
B_pp[i][j] = X_p[index_PQ[i]][index_PQ[j]]
return - B_p, - B_pp
def BX(Y,index_PQ, index_P, n_PQ, n_P):
case_number, _ = np.shape(Y)
Y_p = Y.copy()
# print Y
X_p = np.zeros((case_number, case_number))
B_p = np.zeros((n_P,n_P))
B_pp = np.zeros((n_PQ,n_PQ))
#-------------------------------------------------
for i in xrange(case_number):
Y_p[i][i] = complex(0,0)
for j in xrange(case_number):
if i != j:
Y_p[i][i] -= Y_p[i][j]
B = np.imag(Y_p)
for i in xrange(n_P):
for j in xrange(n_P):
B_p[i][j] = B[index_P[i]][index_P[j]]
#-------------------------------------------------
g_b_round = np.zeros(case_number)
for i in xrange(case_number):
a = np.sum(Y[i])
if LA.norm(a) > 1e-5:
g_b_round[i] = np.reciprocal(np.imag(np.reciprocal(a)))
for i in xrange(case_number):
for j in xrange(case_number):
if LA.norm(Y[i][j]) > 1e-5 and i!=j:
X_p[i][j] = np.reciprocal(np.imag(np.reciprocal(Y[i][j])))
for i in xrange(case_number):
X_p[i][i] = g_b_round[i]
for j in xrange(case_number):
if i != j:
X_p[i][i] -= X_p[i][j]
for i in xrange(0, n_PQ):
for j in xrange(0, n_PQ):
B_pp[i][j] = X_p[index_PQ[i]][index_PQ[j]]
return B_p, B_pp
def __init__(self, parameters, language):
assert language in ["en", "nl"]
self.language = language
# load frequency list
pathtofrequencies = 'frequencies_' + language + '.json'
# load trained fasttext model
pathtomodel = 'embeddings_' + language + '.bin'
# give path to fasttext vectors
pathtovectors = 'embeddings_' + language + '.vec'
# PHASE 1
self.comp_function = parameters['comp_function'] # item from ["sum", "mult", "max"]
self.include_misspelling = parameters['include_misspelling'] # boolean
self.include_oov_candidates = parameters['include_oov_candidates'] # boolean
self.pathtovectors = pathtovectors # path to fasttext vectors
self.model = fasttext.load_model(pathtomodel) # path to fasttext model
# PHASE 2
self.window_size = parameters['window_size'] # number in range(0,11)
self.reciprocal = parameters['reciprocal'] # boolean
self.remove_stopwords = parameters['remove_stopwords'] # boolean
self.stopwords = frozenset(json.load(open('stopwords_' + str(self.language) + '.json', 'r')))
# PHASE 3
self.edit_distance = parameters['edit_distance'] # item from [1, 2, 3, 4]
# PHASE 4
self.oov_penalty = parameters['oov_penalty'] # oov penalty tuned with self.tune_oov()
# OUTPUT
self.ranking_method = parameters['ranking_method'] # item from ["context", "noisy_channel", "frequency",
# "ensemble"]
self.frequency_dict = json.load(open(pathtofrequencies, 'r')) # path to frequency list
self.k = parameters['k-best'] # positive natural number
test_scalarmath.py 文件源码
项目:PyDataLondon29-EmbarrassinglyParallelDAWithAWSLambda
作者: SignalMedia
项目源码
文件源码
阅读 36
收藏 0
点赞 0
评论 0
def test_blocked(self):
# test alignments offsets for simd instructions
# alignments for vz + 2 * (vs - 1) + 1
for dt, sz in [(np.float32, 11), (np.float64, 7)]:
for out, inp1, inp2, msg in _gen_alignment_data(dtype=dt,
type='binary',
max_size=sz):
exp1 = np.ones_like(inp1)
inp1[...] = np.ones_like(inp1)
inp2[...] = np.zeros_like(inp2)
assert_almost_equal(np.add(inp1, inp2), exp1, err_msg=msg)
assert_almost_equal(np.add(inp1, 1), exp1 + 1, err_msg=msg)
assert_almost_equal(np.add(1, inp2), exp1, err_msg=msg)
np.add(inp1, inp2, out=out)
assert_almost_equal(out, exp1, err_msg=msg)
inp2[...] += np.arange(inp2.size, dtype=dt) + 1
assert_almost_equal(np.square(inp2),
np.multiply(inp2, inp2), err_msg=msg)
assert_almost_equal(np.reciprocal(inp2),
np.divide(1, inp2), err_msg=msg)
inp1[...] = np.ones_like(inp1)
inp2[...] = np.zeros_like(inp2)
np.add(inp1, 1, out=out)
assert_almost_equal(out, exp1 + 1, err_msg=msg)
np.add(1, inp2, out=out)
assert_almost_equal(out, exp1, err_msg=msg)
def KernelRadial(self, r):
return np.reciprocal(np.sqrt(1+self.gamma*r))
def KernelRadial(self, r):
return np.reciprocal(1+self.gamma*r)
def predict_probas(X, w, intercept, multinomial=True):
"""
Predict probabilities for each class using either a multinomial or a
one-vs-rest approach
"""
#print X.shape
#print w.shape
#print intercept.shape
p = safe_sparse_dot(X, w.T, dense_output=True) + intercept
if multinomial:
return softmax(p, copy=False)
else:
p = p.ravel() if p.shape[1] == 1 else p
p *= -1
np.exp(p, p)
p += 1
np.reciprocal(p, p)
if p.ndim == 1:
return np.vstack([1 - p, p]).T
else:
# OvR normalization, like LibLinear's predict_probability
p /= p.sum(axis=1).reshape((p.shape[0], -1))
return p
def test_blocked(self):
# test alignments offsets for simd instructions
# alignments for vz + 2 * (vs - 1) + 1
for dt, sz in [(np.float32, 11), (np.float64, 7)]:
for out, inp1, inp2, msg in _gen_alignment_data(dtype=dt,
type='binary',
max_size=sz):
exp1 = np.ones_like(inp1)
inp1[...] = np.ones_like(inp1)
inp2[...] = np.zeros_like(inp2)
assert_almost_equal(np.add(inp1, inp2), exp1, err_msg=msg)
assert_almost_equal(np.add(inp1, 1), exp1 + 1, err_msg=msg)
assert_almost_equal(np.add(1, inp2), exp1, err_msg=msg)
np.add(inp1, inp2, out=out)
assert_almost_equal(out, exp1, err_msg=msg)
inp2[...] += np.arange(inp2.size, dtype=dt) + 1
assert_almost_equal(np.square(inp2),
np.multiply(inp2, inp2), err_msg=msg)
assert_almost_equal(np.reciprocal(inp2),
np.divide(1, inp2), err_msg=msg)
inp1[...] = np.ones_like(inp1)
inp2[...] = np.zeros_like(inp2)
np.add(inp1, 1, out=out)
assert_almost_equal(out, exp1 + 1, err_msg=msg)
np.add(1, inp2, out=out)
assert_almost_equal(out, exp1, err_msg=msg)
def test_blocked(self):
# test alignments offsets for simd instructions
# alignments for vz + 2 * (vs - 1) + 1
for dt, sz in [(np.float32, 11), (np.float64, 7)]:
for out, inp1, inp2, msg in _gen_alignment_data(dtype=dt,
type='binary',
max_size=sz):
exp1 = np.ones_like(inp1)
inp1[...] = np.ones_like(inp1)
inp2[...] = np.zeros_like(inp2)
assert_almost_equal(np.add(inp1, inp2), exp1, err_msg=msg)
assert_almost_equal(np.add(inp1, 1), exp1 + 1, err_msg=msg)
assert_almost_equal(np.add(1, inp2), exp1, err_msg=msg)
np.add(inp1, inp2, out=out)
assert_almost_equal(out, exp1, err_msg=msg)
inp2[...] += np.arange(inp2.size, dtype=dt) + 1
assert_almost_equal(np.square(inp2),
np.multiply(inp2, inp2), err_msg=msg)
assert_almost_equal(np.reciprocal(inp2),
np.divide(1, inp2), err_msg=msg)
inp1[...] = np.ones_like(inp1)
inp2[...] = np.zeros_like(inp2)
np.add(inp1, 1, out=out)
assert_almost_equal(out, exp1 + 1, err_msg=msg)
np.add(1, inp2, out=out)
assert_almost_equal(out, exp1, err_msg=msg)
def sigmoid(x, out):
if out is not x:
out[:] = x
np.negative(out, out)
np.exp(out, out)
out += 1
np.reciprocal(out, out)
return out
def calc_pmi(counts, cds):
sum_w = np.array(counts.sum(axis=1))[:, 0]
sum_c = np.array(counts.sum(axis=0))[0, :]
if cds != 1:
sum_c = sum_c ** cds
sum_total = sum_c.sum()
sum_w = np.reciprocal(sum_w)
sum_c = np.reciprocal(sum_c)
pmi = csr_matrix(counts)
pmi = multiply_by_rows(pmi, sum_w)
pmi = multiply_by_columns(pmi, sum_c)
pmi = pmi * sum_total
return pmi
def normalize(self):
m2 = self.m.copy()
m2.data **= 2
norm = np.reciprocal(np.sqrt(np.array(m2.sum(axis=1))[:, 0]))
normalizer = dok_matrix((len(norm), len(norm)))
normalizer.setdiag(norm)
self.m = normalizer.tocsr().dot(self.m)
def numerator(center_coords, data_i):
"""
"""
return np.reciprocal(np.linalg.norm(center_coords - data_i))