本文整理匯總了Python中numpy.testing.assert_array_almost_equal方法的典型用法代碼示例。如果您正苦於以下問題:Python testing.assert_array_almost_equal方法的具體用法?Python testing.assert_array_almost_equal怎麽用?Python testing.assert_array_almost_equal使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類numpy.testing
的用法示例。
在下文中一共展示了testing.assert_array_almost_equal方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_binary_crossentropy
# 需要導入模塊: from numpy import testing [as 別名]
# 或者: from numpy.testing import assert_array_almost_equal [as 別名]
def test_binary_crossentropy(self):
y_pred = tf.constant([[0.5, 0., 1.], [2., 0., -1.]])
y_pred_2 = tf.constant([[0.5, 2., 1.], [2., 2., -1.]])
y_true = tf.constant([[1., MAGIC_NUMBER, 1.], [1., MAGIC_NUMBER, 0.]])
y_pred_sigmoid = tf.nn.sigmoid(y_pred)
y_pred_2_sigmoid = tf.nn.sigmoid(y_pred_2)
# Truth with Magic number is wrong
npt.assert_array_almost_equal(binary_crossentropy(y_true, y_pred_sigmoid).numpy(),
binary_crossentropy(y_true, y_pred, from_logits=True).numpy(), decimal=3)
# make sure neural network prediction won't matter for magic number term
npt.assert_array_almost_equal(
binary_crossentropy(y_true, y_pred_2, from_logits=True).numpy(),
binary_crossentropy(y_true, y_pred, from_logits=True).numpy()
, decimal=3)
npt.assert_array_almost_equal(binary_crossentropy(y_true, y_pred_sigmoid).numpy(),
binary_crossentropy(y_true, y_pred_2_sigmoid).numpy(), decimal=3)
示例2: test_logsol
# 需要導入模塊: from numpy import testing [as 別名]
# 或者: from numpy.testing import assert_array_almost_equal [as 別名]
def test_logsol(self):
# Test conversion tools related to log solar luminosity
from astroNN.gaia import fakemag_to_logsol, absmag_to_logsol, logsol_to_absmag, logsol_to_fakemag
self.assertEqual(logsol_to_fakemag(fakemag_to_logsol(100.)), 100.)
npt.assert_array_equal(logsol_to_fakemag(fakemag_to_logsol([100., 100.])), [100., 100.])
npt.assert_array_equal(logsol_to_fakemag(fakemag_to_logsol(np.array([100, 100, 100]))), [100., 100., 100.])
self.assertEqual(fakemag_to_logsol(MAGIC_NUMBER), MAGIC_NUMBER)
self.assertEqual(logsol_to_fakemag(fakemag_to_logsol(MAGIC_NUMBER)), MAGIC_NUMBER)
self.assertEqual(np.any(fakemag_to_logsol([MAGIC_NUMBER, 1000]) == MAGIC_NUMBER), True)
self.assertEqual(logsol_to_absmag(absmag_to_logsol(99.)), 99.)
self.assertAlmostEqual(logsol_to_absmag(absmag_to_logsol(-99.)), -99.)
npt.assert_array_equal(logsol_to_absmag(absmag_to_logsol([99., 99.])), [99., 99.])
npt.assert_array_almost_equal(logsol_to_absmag(absmag_to_logsol([-99., -99.])), [-99., -99.])
npt.assert_array_almost_equal(logsol_to_absmag(absmag_to_logsol(np.array([99., 99., 99.]))), [99., 99., 99.])
self.assertEqual(absmag_to_logsol(MAGIC_NUMBER), MAGIC_NUMBER)
self.assertEqual(logsol_to_absmag(absmag_to_logsol(MAGIC_NUMBER)), MAGIC_NUMBER)
self.assertEqual(np.any(absmag_to_logsol([MAGIC_NUMBER, 1000]) == MAGIC_NUMBER), True)
示例3: test_regularizator
# 需要導入模塊: from numpy import testing [as 別名]
# 或者: from numpy.testing import assert_array_almost_equal [as 別名]
def test_regularizator(self):
# make sure its the same as tensorflow
x = np.array([-1., 2., 3., 4.])
reg = 0.2
astroNN_x = l1(x, l1=reg)
astroNN_x_2 = l2(x, l2=reg)
with tf.device("/cpu:0"), context.eager_mode():
l1_reg = tf.keras.regularizers.l1(l=reg)
l2_reg = tf.keras.regularizers.l2(l=reg)
tf_x = l1_reg(tf.convert_to_tensor(x))
tf_x_2 = l2_reg(tf.convert_to_tensor(x))
npt.assert_array_almost_equal(tf_x.numpy(), astroNN_x)
npt.assert_array_almost_equal(tf_x_2.numpy(), astroNN_x_2)
示例4: test_ODEbadprecision
# 需要導入模塊: from numpy import testing [as 別名]
# 或者: from numpy.testing import assert_array_almost_equal [as 別名]
def test_ODEbadprecision(self): # make sure float32 is not enough for very precise integration
t = tf.constant(np.linspace(0, 10, 1000), dtype=tf.float32)
# initial condition
true_y0 = tf.constant([0., 5.], dtype=tf.float32)
true_func = lambda y, t: np.sin(5*t)
ode_func = lambda y, t: tf.cast(tf.stack([5*tf.cos(5*t), -25*tf.sin(5*t)]), tf.float32)
true_y = odeint(ode_func, true_y0, t, method='dop853', precision=tf.float32)
self.assertRaises(AssertionError, npt.assert_array_almost_equal, true_y.numpy()[:, 0], true_func(true_y0, t))
true_y0_pretend_multidims = [[0., 5.]] # to introduce a mix of list, np array, tensor to make sure no issue
true_y_pretend_multidims = odeint(ode_func, true_y0_pretend_multidims, t, method='dop853', precision=tf.float32)
# assert equal pretendinging multidim or not
np.testing.assert_array_almost_equal(true_y_pretend_multidims[0], true_y)
true_y0_multidims = tf.constant([[1., 2.], [0., 5.]], dtype=tf.float32)
t = np.linspace(0, 10, 1000)
true_y_multidims = odeint(ode_func, true_y0_multidims, t, method='dop853', precision=tf.float32)
# assert equal in multidim or not
np.testing.assert_array_almost_equal(true_y_multidims[1], true_y)
示例5: test_attention_softmax
# 需要導入模塊: from numpy import testing [as 別名]
# 或者: from numpy.testing import assert_array_almost_equal [as 別名]
def test_attention_softmax(self):
vector_in = tf.constant([
[1., 2., 1., 2.0],
[.3, .2, .9, .3]
])
padding = tf.constant([
[1., 1., 1., 0.],
[1., 1., 0., 0.]
])
result = self.sess.run(attention_softmax(vector_in, padding))
reference_value = np.array([
[0.21194156, 0.57611692, 0.21194156, 0.],
[0.52497919, 0.47502081, 0., 0.]
])
npt.assert_array_almost_equal(result, reference_value)
示例6: test__impute
# 需要導入模塊: from numpy import testing [as 別名]
# 或者: from numpy.testing import assert_array_almost_equal [as 別名]
def test__impute():
expected = numpy.array([
3.11279729, 3.60634338, 4.04602788, 4.04602788,
4.71008116, 6.14010906, 6.97841457, 2. ,
4.2 , 4.62 , 5.57 , 5.66 ,
5.86 , 6.65 , 6.78 , 6.79 ,
7.5 , 7.5 , 7.5 , 8.63 ,
8.71 , 8.99 , 9.85 , 10.82 ,
11.25 , 11.25 , 12.2 , 14.92 ,
16.77 , 17.81 , 19.16 , 19.19 ,
19.64 , 20.18 , 22.97
])
df = load_advanced_data()
df = ros._impute(df, 'conc', 'censored', numpy.log, numpy.exp)
result = df['final'].values
npt.assert_array_almost_equal(result, expected)
示例7: test__do_ros
# 需要導入模塊: from numpy import testing [as 別名]
# 或者: from numpy.testing import assert_array_almost_equal [as 別名]
def test__do_ros():
expected = numpy.array([
3.11279729, 3.60634338, 4.04602788, 4.04602788,
4.71008116, 6.14010906, 6.97841457, 2. ,
4.2 , 4.62 , 5.57 , 5.66 ,
5.86 , 6.65 , 6.78 , 6.79 ,
7.5 , 7.5 , 7.5 , 8.63 ,
8.71 , 8.99 , 9.85 , 10.82 ,
11.25 , 11.25 , 12.2 , 14.92 ,
16.77 , 17.81 , 19.16 , 19.19 ,
19.64 , 20.18 , 22.97
])
df = load_basic_data()
df = ros._do_ros(df, 'conc', 'censored', numpy.log, numpy.exp)
result = df['final'].values
npt.assert_array_almost_equal(result, expected)
示例8: test_feasibility_problem
# 需要導入模塊: from numpy import testing [as 別名]
# 或者: from numpy.testing import assert_array_almost_equal [as 別名]
def test_feasibility_problem(self):
# Solve problem
res = self.model.solve()
# Assert close
nptest.assert_array_almost_equal(
res.x,
np.array([-0.0656074, 1.04194398, 0.4756959, -1.64036689,
-0.34180168, -0.81696303, -1.06389178, 0.44944554,
-0.44829675, -1.01289944, -0.12513655, 0.02267293,
-1.15206474, 1.06817424, 1.18143313, 0.01690332,
-0.11373645, -0.48115767, 0.25373436, 0.81369707,
0.18883475, 0.47000419, -0.24932451, 0.09298623,
1.88381076, 0.77536814, -1.35971433, 0.51511176,
0.03317466, 0.90226419]), decimal=3)
nptest.assert_array_almost_equal(res.y, np.zeros(self.m), decimal=3)
nptest.assert_array_almost_equal(res.info.obj_val, 0., decimal=3)
示例9: test_update_P_allind
# 需要導入模塊: from numpy import testing [as 別名]
# 或者: from numpy.testing import assert_array_almost_equal [as 別名]
def test_update_P_allind(self):
import mat_emosqp
# Update matrix P
Px = self.P_new.data
mat_emosqp.update_P(Px, None, 0)
x, y, _, _, _ = mat_emosqp.solve()
# Assert close
nptest.assert_array_almost_equal(x, np.array([0., 5.]), decimal=5)
nptest.assert_array_almost_equal(
y, np.array([0., 0., 3., 0., 0.]), decimal=5)
# Update matrix P to the original value
Px_idx = np.arange(self.P.nnz)
mat_emosqp.update_P(Px, Px_idx, len(Px))
示例10: test_update_A
# 需要導入模塊: from numpy import testing [as 別名]
# 或者: from numpy.testing import assert_array_almost_equal [as 別名]
def test_update_A(self):
import mat_emosqp
# Update matrix A
Ax = self.A_new.data
Ax_idx = np.arange(self.A_new.nnz)
mat_emosqp.update_A(Ax, Ax_idx, len(Ax))
# Solve problem
x, y, _, _, _ = mat_emosqp.solve()
# Assert close
nptest.assert_array_almost_equal(x,
np.array([0.15765766, 7.34234234]), decimal=5)
nptest.assert_array_almost_equal(
y, np.array([0., 0., 2.36711712, 0., 0.]), decimal=5)
# Update matrix A to the original value
Ax = self.A.data
Ax_idx = np.arange(self.A.nnz)
mat_emosqp.update_A(Ax, Ax_idx, len(Ax))
示例11: test_update_A_allind
# 需要導入模塊: from numpy import testing [as 別名]
# 或者: from numpy.testing import assert_array_almost_equal [as 別名]
def test_update_A_allind(self):
import mat_emosqp
# Update matrix A
Ax = self.A_new.data
mat_emosqp.update_A(Ax, None, 0)
x, y, _, _, _ = mat_emosqp.solve()
# Assert close
nptest.assert_array_almost_equal(x,
np.array([0.15765766, 7.34234234]), decimal=5)
nptest.assert_array_almost_equal(
y, np.array([0., 0., 2.36711712, 0., 0.]), decimal=5)
# Update matrix A to the original value
Ax = self.A.data
Ax_idx = np.arange(self.A.nnz)
mat_emosqp.update_A(Ax, Ax_idx, len(Ax))
示例12: test_update_P_A_indP_indA
# 需要導入模塊: from numpy import testing [as 別名]
# 或者: from numpy.testing import assert_array_almost_equal [as 別名]
def test_update_P_A_indP_indA(self):
import mat_emosqp
# Update matrices P and A
Px = self.P_new.data
Px_idx = np.arange(self.P_new.nnz)
Ax = self.A_new.data
Ax_idx = np.arange(self.A_new.nnz)
mat_emosqp.update_P_A(Px, Px_idx, len(Px), Ax, Ax_idx, len(Ax))
# Solve problem
x, y, _, _, _ = mat_emosqp.solve()
# Assert close
nptest.assert_array_almost_equal(x, np.array([4.25, 3.25]), decimal=5)
nptest.assert_array_almost_equal(
y, np.array([0., 0., 3.625, 0., 0.]), decimal=5)
# Update matrices P and A to the original values
Px = self.P.data
Ax = self.A.data
mat_emosqp.update_P_A(Px, None, 0, Ax, None, 0)
示例13: test_update_P_A_indP
# 需要導入模塊: from numpy import testing [as 別名]
# 或者: from numpy.testing import assert_array_almost_equal [as 別名]
def test_update_P_A_indP(self):
import mat_emosqp
# Update matrices P and A
Px = self.P_new.data
Px_idx = np.arange(self.P_new.nnz)
Ax = self.A_new.data
mat_emosqp.update_P_A(Px, Px_idx, len(Px), Ax, None, 0)
x, y, _, _, _ = mat_emosqp.solve()
# Assert close
nptest.assert_array_almost_equal(x, np.array([4.25, 3.25]), decimal=5)
nptest.assert_array_almost_equal(
y, np.array([0., 0., 3.625, 0., 0.]), decimal=5)
# Update matrices P and A to the original values
Px = self.P.data
Ax = self.A.data
mat_emosqp.update_P_A(Px, None, 0, Ax, None, 0)
示例14: test_update_P_A_allind
# 需要導入模塊: from numpy import testing [as 別名]
# 或者: from numpy.testing import assert_array_almost_equal [as 別名]
def test_update_P_A_allind(self):
import mat_emosqp
# Update matrices P and A
Px = self.P_new.data
Ax = self.A_new.data
mat_emosqp.update_P_A(Px, None, 0, Ax, None, 0)
x, y, _, _, _ = mat_emosqp.solve()
# Assert close
nptest.assert_array_almost_equal(x, np.array([4.25, 3.25]), decimal=5)
nptest.assert_array_almost_equal(y, np.array([0., 0., 3.625, 0., 0.]), decimal=5)
# Update matrices P and A to the original values
Px = self.P.data
Ax = self.A.data
mat_emosqp.update_P_A(Px, None, 0, Ax, None, 0)
示例15: test_unconstrained_problem
# 需要導入模塊: from numpy import testing [as 別名]
# 或者: from numpy.testing import assert_array_almost_equal [as 別名]
def test_unconstrained_problem(self):
# Solve problem
res = self.model.solve()
# Assert close
nptest.assert_array_almost_equal(
res.x, np.array([
-0.61981415, -0.06174194, 0.83824061, -0.0595013, -0.17810828,
2.90550031, -1.8901713, -1.91191741, -3.73603446, 1.7530356,
-1.67018181, 3.42221944, 0.61263403, -0.45838347, -0.13194248,
2.95744794, 5.2902277, -1.42836238, -8.55123842, -0.79093815,
0.43418189, -0.69323554, 1.15967924, -0.47821898, 3.6108927,
0.03404309, 0.16322926, -2.17974795, 0.32458796, -1.97553574]))
nptest.assert_array_almost_equal(res.y, np.array([]))
nptest.assert_array_almost_equal(res.info.obj_val, -35.020288603855825)