Ejemplo n.º 1
0
    def test_concat(self):
        tf.reset_default_graph()

        prot = ABY3()
        tfe.set_protocol(prot)

        x1 = tfe.define_private_variable(tf.constant([[1, 2], [4, 5]]))
        x2 = tfe.define_private_variable(tf.constant([[3], [6]]))
        y1 = tfe.define_constant(np.array([[1, 2, 3]]))
        y2 = tfe.define_constant(np.array([[4, 5, 6]]))

        z1 = tfe.concat([x1, x2], axis=1)
        z2 = tfe.concat([y1, y2], axis=0)

        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([[1, 2, 3], [4, 5, 6]]), rtol=0.0, atol=0.01
            )

            result = sess.run(z2)
            np.testing.assert_allclose(
                result, np.array([[1, 2, 3], [4, 5, 6]]), rtol=0.0, atol=0.01
            )
Ejemplo n.º 2
0
def main(server):

    num_rows = 7000
    num_features = 32
    num_epoch = 5
    batch_size = 200
    num_batches = (num_rows // batch_size) * num_epoch

    #who shall receive the output
    model_owner = ModelOwner('alice')

    data_schema0 = DataSchema([tf.float64] * 16, [0.0] * 16)
    data_schema1 = DataSchema([tf.int64] + [tf.float64] * 16, [0] + [0.0] * 16)
    data_owner_0 = DataOwner('alice',
                             'aliceTrainFile.csv',
                             data_schema0,
                             batch_size=batch_size)
    data_owner_1 = DataOwner('bob',
                             'bobTrainFileWithLabel.csv',
                             data_schema1,
                             batch_size=batch_size)

    tfe.set_protocol(
        tfe.protocol.Pond(
            tfe.get_config().get_player(data_owner_0.player_name),
            tfe.get_config().get_player(data_owner_1.player_name)))

    x_train_0 = tfe.define_private_input(data_owner_0.player_name,
                                         data_owner_0.provide_data)
    x_train_1 = tfe.define_private_input(data_owner_1.player_name,
                                         data_owner_1.provide_data)
    y_train = tfe.gather(x_train_1, 0, axis=1)
    y_train = tfe.reshape(y_train, [batch_size, 1])

    #Remove bob's first column (which is label)
    x_train_1 = tfe.strided_slice(x_train_1, [0, 1],
                                  [x_train_1.shape[0], x_train_1.shape[1]],
                                  [1, 1])

    x_train = tfe.concat([x_train_0, x_train_1], axis=1)

    model = LogisticRegression(num_features)
    reveal_weights_op = model_owner.receive_weights(model.weights)
    with tfe.Session() as sess:
        sess.run(tfe.global_variables_initializer(), tag='init')
        start_time = time.time()
        model.fit(sess, x_train, y_train, num_batches)
        end_time = time.time()
        # TODO(Morten)
        # each evaluation results in nodes for a forward pass being added to the graph;
        # maybe there's some way to avoid this, even if it means only if the shapes match
        model.evaluate(sess, x_train, y_train, data_owner_0)
        model.evaluate(sess, x_train, y_train, data_owner_1)

        print(sess.run(reveal_weights_op, tag='reveal'),
              ((end_time - start_time) * 1000))
Ejemplo n.º 3
0
                         training_set_size,
                         test_set_size,
                         batch_size // 2)

tfe.set_protocol(tfe.protocol.Pond(
    tfe.get_config().get_player(data_owner_0.player_name),
    tfe.get_config().get_player(data_owner_1.player_name)
))

x_train_0, y_train_0 = data_owner_0.provide_training_data()
x_train_1, y_train_1 = data_owner_1.provide_training_data()

x_test_0, y_test_0 = data_owner_0.provide_testing_data()
x_test_1, y_test_1 = data_owner_1.provide_testing_data()

x_train = tfe.concat([x_train_0, x_train_1], axis=0)
y_train = tfe.concat([y_train_0, y_train_1], axis=0)

model = LogisticRegression(num_features)
reveal_weights_op = model_owner.receive_weights(model.weights)

with tfe.Session() as sess:
  sess.run([tfe.global_variables_initializer(),
            data_owner_0.initializer,
            data_owner_1.initializer],
           tag='init')

  model.fit(sess, x_train, y_train, num_batches)
  # TODO(Morten)
  # each evaluation results in nodes for a forward pass being added to the graph;
  # maybe there's some way to avoid this, even if it means only if the shapes match
Ejemplo n.º 4
0
def _concat(converter, node: Any, inputs: List[str]) -> Any:
  input_list = [converter.outputs[inputs[i]] for i in range(len(inputs) - 1)]
  axis = converter.outputs[inputs[-1]]
  axis_int = axis.attr["value"].tensor.int_val[0]

  return tfe.concat(input_list, axis_int)
Ejemplo n.º 5
0
import tf_encrypted as tfe

from common import LogisticRegression, PredictionClient

num_features = 10

model = LogisticRegression(num_features)
prediction_client_0 = PredictionClient('prediction-client-0',
                                       num_features // 2)
prediction_client_1 = PredictionClient('prediction-client-1',
                                       num_features // 2)
result_receiver = prediction_client_0

x_0 = tfe.define_private_input(prediction_client_0.player_name,
                               prediction_client_0.provide_input)
x_1 = tfe.define_private_input(prediction_client_1.player_name,
                               prediction_client_1.provide_input)
x = tfe.concat([x_0, x_1], axis=1)

y = model.forward(x)

reveal_output = tfe.define_output(result_receiver.player_name, y,
                                  result_receiver.receive_output)

with tfe.Session() as sess:
    sess.run(tfe.global_variables_initializer(), tag='init')

    sess.run(reveal_output, tag='predict')
Ejemplo n.º 6
0
        tfe.protocol.Pond(
            tfe.get_config().get_player(data_owner_0.player_name),
            tfe.get_config().get_player(data_owner_1.player_name)))

    x_train_0 = tfe.define_private_input(data_owner_0.player_name,
                                         data_owner_0.provide_data)
    x_train_1 = tfe.define_private_input(data_owner_1.player_name,
                                         data_owner_1.provide_data)
    y_train = tfe.gather(x_train_1, 0, axis=1)
    y_train = tfe.reshape(y_train, [batch_size, 1])

    # Remove bob's first column (which is label)
    x_train_1 = tfe.strided_slice(x_train_1, [0, 1],
                                  [x_train_1.shape[0], x_train_1.shape[1]],
                                  [1, 1])
    x_train = tfe.concat([x_train_0, x_train_1], axis=1)

    model = LinearRegression(num_features)
    # model = LogisticRegression(num_features)
    fit_forward_op = model.fit_forward(x_train,
                                       y_train,
                                       learning_rate=case['l'])
    reveal_weights_op = model_owner.receive_weights(model.weights)

    # prepare test data
    test_x_data, test_y_data = load_test_data(f'{name}/tfe_features_test.csv',
                                              f'{name}/tfe_label_test.csv')

    with tfe.Session() as sess:
        sess.run(tfe.global_variables_initializer(), tag='init')
        # sess.run(tf.initialize_local_variables())