<jupyter_start><jupyter_text><jupyter_code>!pip install syft
import torch as th
import syft as sy
hook = sy.TorchHook(th)
bob = sy.VirtualWorker(hook, id='bob')
bob._objects
data = th.tensor([1, 2, 3, 4, 5])
data = data.send(bob)
bob._objects
data.location
print(data.location == bob)
print(data.id_at_location, data.id, data.owner)
hook.local_worker
data = data.get()
data
bob._objects
alice = sy.VirtualWorker(hook, id='alice')
new_data = th.tensor([1, 2, 3, 4, 5]).send(bob, alice)
new_data
new_data.get(sum_results=True)
x = th.tensor([1, 2, 3]).send(bob)
y = th.tensor([1, 1, 1]).send(bob)
z = x + y
z.get()
x = th.tensor([1., 2, 3], requires_grad=True).send(bob)
y = th.tensor([1., 1, 1], requires_grad=True).send(bob)
z = (x+y).sum() 
z.backward()
x = x.get()
x.grad
## Learn a simple linear model

input = th.tensor([[1., 1], [0, 1], [1, 0], [0, 0]], requires_grad=True).send(bob)
targets = th.tensor([[1.], [1], [0], [0]], requires_grad=True).send(bob)
weights = th.tensor([[0.], [0]], requires_grad=True).send(bob)
loss1 = 10
rounds=0
while rounds < 30 or loss1 > 1e-5:

  pred = th.mm(input, weights)
  loss = ((pred-targets)**2).sum()

  loss.backward()  

  weights.data.sub_(weights.grad*0.1)
  weights.grad*=0
  loss1 = loss.get().data
  print(loss1, rounds)
  rounds+=1


bob.clear_objects()
bob._objects
x = th.tensor([1., 1]).send(bob)
print(bob._objects, x.child.garbage_collect_data)
del x
print(bob._objects)
for indx in range(1000):
  x = th.tensor([1, 1]).send(bob)
bob._objects
## Learning a toy FL on two different machines

from torch import nn, optim

input = th.tensor([[1., 1], [0, 1], [1, 0], [0, 0]], requires_grad=True)
targets = th.tensor([[1.], [1], [0], [0]], requires_grad=True)
weights = th.tensor([[0.], [0]], requires_grad=True)

model = nn.Linear(2, 1)
opt = optim.SGD(params=model.parameters(), lr=0.1)
for<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.20404901438465636, 'fraction_numerical': 0.04155567394778902, 'mean_word_length': 4.832298136645963, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '514951', 'license': 'MIT', 'n_tokens_mistral': 824, 'n_tokens_neox': 757, 'n_words': 182, 'path': 'FederatedLearning.ipynb', 'repo_name': 'thehimalayanleo/Private-Machine-Learning', 'size': '1877'}