def testEmpty(self): x = np.random.rand(2, 2).astype(np.float32) coeffs = [] np_val = np.polyval(coeffs, x) with self.cached_session(): tf_val = math_ops.polyval(coeffs, x) self.assertAllClose(np_val, self.evaluate(tf_val))
def _runtest(self, dtype, degree): x = np.random.rand(2, 2).astype(dtype) coeffs = [np.random.rand(2, 2).astype(dtype) for _ in range(degree + 1)] np_val = np.polyval(coeffs, x) with self.cached_session(): tf_val = math_ops.polyval(coeffs, x) self.assertAllClose(np_val, self.evaluate(tf_val))
def f(p, x): if p.shape.rank == 0: p = array_ops.reshape(p, [1]) p = array_ops.unstack(p) # TODO(wangpeng): Make tf version take a tensor for p instead of a list. y = math_ops.polyval(p, x) # If the polynomial is 0-order, numpy requires the result to be broadcast to # `x`'s shape. if len(p) == 1: y = array_ops.broadcast_to(y, x.shape) return y
def testBroadcast(self): dtype = np.float32 degree = 3 shapes = [(1,), (2, 1), (1, 2), (2, 2)] for x_shape in shapes: for coeff_shape in shapes: x = np.random.rand(*x_shape).astype(dtype) coeffs = [ np.random.rand(*coeff_shape).astype(dtype) for _ in range(degree + 1) ] np_val = np.polyval(coeffs, x) with self.cached_session(): tf_val = math_ops.polyval(coeffs, x) self.assertAllClose(np_val, self.evaluate(tf_val))
def test_coeffs_raise(self): x = np.random.rand(2, 2).astype(np.float32) coeffs = {} with self.assertRaisesRegexp(ValueError, "Argument coeffs must be list"): math_ops.polyval(coeffs, x)