def test_polynomial_piecewise(self): tf.reset_default_graph() prot = ABY3() tfe.set_protocol(prot) x = tfe.define_private_variable(tf.constant([[-1, -0.5, -0.25], [0, 0.25, 2]])) # This is the approximation of the sigmoid function by using a piecewise function: # f(x) = (0 if x<-0.5), (x+0.5 if -0.5<=x<0.5), (1 if x>=0.5) z1 = tfe.polynomial_piecewise( x, (-0.5, 0.5), ((0,), (0.5, 1), (1,)), # use tuple because list is not hashable ) # Or, simply use the pre-defined sigmoid API which includes a different approximation z2 = tfe.sigmoid(x) with tfe.Session() as sess: # initialize variables sess.run(tfe.global_variables_initializer()) # reveal result result = sess.run(z1.reveal()) np.testing.assert_allclose( result, np.array([[0, 0, 0.25], [0.5, 0.75, 1]]), rtol=0.0, atol=0.01 ) result = sess.run(z2.reveal()) np.testing.assert_allclose( result, np.array([[0.33, 0.415, 0.4575], [0.5, 0.5425, 0.84]]), rtol=0.0, atol=0.01, )
def test_polynomial_piecewise(): tf.reset_default_graph() import time start = time.time() prot = ABY3() tfe.set_protocol(prot) x = tfe.define_private_variable( tf.constant([[-1, -0.5, -0.25], [0, 0.25, 2]])) # This is the approximation of the sigmoid function by using a piecewise function: # f(x) = (0 if x<-0.5), (x+0.5 if -0.5<=x<0.5), (1 if x>=0.5) z1 = tfe.polynomial_piecewise( x, (-0.5, 0.5), ( (0, ), (0.5, 1), (1, ) ) # Should use tuple because list is not hashable for the memoir cache key ) # Or, simply use the pre-defined sigmoid API which includes a different approximation z2 = tfe.sigmoid(x) with tfe.Session() as sess: # initialize variables sess.run(tfe.global_variables_initializer()) # reveal result result = sess.run(z1.reveal()) close(result, np.array([[0, 0, 0.25], [0.5, 0.75, 1]])) result = sess.run(z2.reveal()) close(result, np.array([[0.33, 0.415, 0.4575], [0.5, 0.5425, 0.84]])) print("test_polynomial_piecewise succeeds") end = time.time() print("Elapsed time: {} seconds".format(end - start))