def __init__( self, input_shape, output_shape, input_queue_capacity=1, input_queue_name="input", output_queue_capacity=1, output_queue_name="output", ): self.input_shape = input_shape self.output_shape = output_shape # input input_queue = tfe.queue.FIFOQueue( capacity=input_queue_capacity, shape=input_shape, shared_name=input_queue_name) self.input_placeholder = tfe.define_private_placeholder(shape=input_shape) self.input_op = input_queue.enqueue(self.input_placeholder) # output output_queue = tfe.queue.FIFOQueue( capacity=output_queue_capacity, shape=output_shape, shared_name=output_queue_name) output = output_queue.dequeue() self.output0 = output.share0 self.output1 = output.share1 # fixedpoint config self.modulus = output.backing_dtype.modulus self.bound = tfe.get_protocol().fixedpoint_config.bound_single_precision self.scaling_factor = tfe.get_protocol().fixedpoint_config.scaling_factor
def binary_crossentropy(y_true, y_pred): batch_size = y_true.shape.as_list()[0] batch_size_inv = 1 / batch_size out = y_true * get_protocol().log(y_pred) out += (1 - y_true) * get_protocol().log(1 - y_pred) out = out.negative() bce = out.reduce_sum(axis=0) * batch_size_inv return bce
def _rsqrt(converter, node: Any, inputs: List[str]) -> Any: x_in = converter.outputs[inputs[0]] if isinstance(x_in, tf.NodeDef): tensor = x_in.attr["value"].tensor shape = [i.size for i in tensor.tensor_shape.dim] dtype = x_in.attr["dtype"].type if dtype == tf.float32: nums = array.array("f", tensor.tensor_content) elif dtype == tf.float64: nums = array.array("d", tensor.tensor_content) else: raise TypeError("Unsupported dtype for rsqrt") def inputter_fn(): return tf.constant(1 / np.sqrt(np.array(nums).reshape(shape))) else: # XXX this is a little weird but the input into rsqrt is public and # being used only for batchnorm at the moment prot = tfe.get_protocol() # pylint: disable=protected-access decoded = prot._decode(x_in.value_on_0, True) # pylint: enable=protected-access def inputter_fn(): return tf.rsqrt(decoded) x = tfe.define_public_input(converter.model_provider, inputter_fn) return x
def test_assign_synchronization(self): # from https://github.com/tf-encrypted/tf-encrypted/pull/665 tf.reset_default_graph() tfe.get_protocol().clear_initializers() prot = tfe.protocol.Pond() tfe.set_protocol(prot) def poc(x, y): x_shares = x.unwrapped y_shares = y.unwrapped z_shares = [None, None] with tf.name_scope("fabricated_test"): with tf.device(prot.server_0.device_name): z_shares[0] = x_shares[1] + y_shares[1] with tf.device(prot.server_1.device_name): z_shares[1] = x_shares[0] + y_shares[0] return tfe.protocol.pond.PondPrivateTensor( prot, z_shares[0], z_shares[1], x.is_scaled) a = prot.define_private_variable(tf.ones(shape=(1, 1))) b = prot.define_private_variable(tf.ones(shape=(1, 1))) op = prot.assign(a, poc(a, b)) with tfe.Session() as sess: sess.run(tfe.global_variables_initializer()) for _ in range(100): sess.run(op) result = sess.run(a.reveal()) assert result == np.array([101.])
def sigmoid(x): """Computes sigmoid of x element-wise""" return get_protocol().sigmoid(x)
def relu(x): """Computes relu of x element-wise""" return get_protocol().relu(x)
def tanh(x): """Computes tanh of x element-wise""" return get_protocol().tanh(x)
def prot(self): return get_protocol()
def __enter__(self) -> "Protocol": self.last_protocol = tfe.get_protocol() tfe.set_protocol(self) return self
@tfe.local_computation(name_scope='provide_input') def provide_threshold() -> tf.Tensor: t = 0.5 a = np.array([t] * 10) # print (a) b = tf.constant(a) return b @tfe.local_computation('result-receiver', name_scope='receive_output') def receive_output(average: tf.Tensor) -> tf.Operation: # simply print average return tf.print("Result:", average) inputs = provide_input(player_name='inputter-0') threshold = provide_threshold(player_name='threshold_inputter') # result = inputs - threshold result = inputs from tf_encrypted import get_protocol result = get_protocol().relu(result) result_op = receive_output(result) # mask = with tfe.Session() as sess: sess.run(result_op, tag='prune')