<jupyter_start><jupyter_text>Подготовка пакета PySyft.<jupyter_code>import os
from os import listdir, makedirs, remove
from os.path import exists, join, basename
import tarfile
import subprocess
from six.moves import urllib

import torch
import torchvision
import torch.nn as nn
import torch.optim as optim
import torch.nn.init as init
import torch.utils.data as data
import torchvision.utils as vutils
from torch.utils.data import DataLoader

import numpy as np<jupyter_output><empty_output><jupyter_text>Подключение к владельцу данных.<jupyter_code>import syft as sy
duet = sy.join_duet("80132c82b91f1a19df2660ead4b3c11a")
sy.logging(file_path="./syft_ds.log")
import torch
rem_torch = duet.torch<jupyter_output><empty_output><jupyter_text>Параметры Anaconda3 is already installed in /opt/anaconda3. Use 'conda update anaconda3' to update Anaconda3.<jupyter_code>args = {
    "batch_size": 1000,
    "log_batch_size": 1000,
    "epochs": 10,
    "lr": 1.0,
    "gamma": 0.7,
    "no_cuda": False,
    "dry_run": False,
    "seed": 42, # the meaning of life
    "log_interval": 10,
    "save_model": True,
}<jupyter_output><empty_output><jupyter_text>Список доступных тензоров<jupyter_code>duet.store.pandas<jupyter_output><empty_output><jupyter_text>Загрузим удаленные данные<jupyter_code>X_train = duet.store["Train X"]
y_train = duet.store["Train Y"]
train_num = duet.store["train_num"]
print(train_num)
class DatasetFromPointer(data.Dataset):
    def __init__(self, 
                 X_tensorpointer,
                 y_tensorpointer,
                 datanum_pointer,
                 ):
        super(DatasetFromPointer, self).__init__()
        self.X_tensorpointer = X_tensorpointer
        self.y_tensorpointer = y_tensorpointer
        self.datanum_pointer = datanum_pointer

    def __getitem__(self, index):
        input = self.X_tensorpointer[index]
        target = self.y_tensorpointer[index]
        return input, target

    def __len__(self):
        return self.datanum_pointer.get(
        request_block=True,
        reason="To write the training loop",
        timeout_secs=0,
        delete_obj=False,
    )
        
def batch_idx_fn(batch):
    return batch[0]
train_set = DatasetFromPointer(X_train, y_train, train_num)
len(train_set)
train_set = DatasetFromPointer(X_train, y_train, train_num)
training_data_loader = DataLoader(dataset=train_set, num_workers=0, batch_size=args["batch_size"], shuffle=True, collate_fn=batch_idx_fn)<jupyter_output><empty_output><jupyter_text>Зададим нашу сеть<jupyter_code>class SyNet(sy.Module):
    def __init__(self, torch_ref):
        super(SyNet, self).__init__(torch_ref=torch_ref)
        self.lin1 = self.torch_ref.nn.Linear(X_train.shape[1],X_train.shape[1]*2)
        self.lin2 = self.torch_ref.nn.Linear(X_train.shape[1]*2, 1)
       
    def forward(self, x):
        x = self.torch_ref.nn.functional.relu(self.lin1(x))
        output = self.torch_ref.nn.functional.sigmoid(self.lin2(x))
        return output
local_net = SyNet(rem_torch)
remote_net = model.send(duet)<jupyter_output><empty_output><jupyter_text>Проверка CUDA<jupyter_code>has_cuda = False
has_cuda_ptr = rem_torch.cuda.is_available()
has_cuda = bool(has_cuda_ptr.get(
    request_block=True,
    name="cuda_is_available",
    reason="To run test and inference locally",
    timeout_secs=5,  # change to something slower
))
print(has_cuda)

use_cuda = not args["no_cuda"] and has_cuda
# now we can set the seed
rem_torch.manual_seed(args["seed"])

device = rem_torch.device("cuda" if use_cuda else "cpu")
print(f"Data Owner device is {device.type.get()}")
# if we have CUDA lets send our model to the GPU
if has_cuda:
    model.cuda(device)
else:
    model.cpu()<jupyter_output><empty_output><jupyter_text>Обучение модели<jupyter_code>params = model.parameters()
optimizer = rem_torch.optim.SGD(params, lr=args["lr"])
scheduler = rem_torch.optim.lr_scheduler.StepLR(optimizer, step_size=1, gamma=args["gamma"])
criterion = rem_torch.nn.functional.binary_cross_entropy()
for epoch in range(args["epochs"]):
    
    remote_net.train()
    epoch_loss = 0
    for batch_idx, data_pointers in enumerate(training_data_loader):

        optimizer.zero_grad()
        data_ptr, target_ptr = data_pointers[0], data_pointers[1]
        data_ptr_reshape = remote_torch.unsqueeze(remote_torch.unsqueeze(data_ptr, 0), 0)
        target_ptr_reshape = remote_torch.unsqueeze(remote_torch.unsqueeze(target_ptr, 0), 0)
       
        output_ptr = remote_net(data_ptr_reshape)
        
        loss = criterion(output_ptr, target_ptr_reshape)
        loss.backward()
        optimizer.step()

        if batch_idx % args["log_batch_size"] == 0:
            loss_item = loss.item().get(
                reason="To evaluate training progress",
                request_block=True,
                timeout_secs=3,
                delete_obj=False,
                verbose=False
                )
            print(f"epoch {epoch}, batch_idx {batch_idx}, loss {loss_item}")

        if args["dry_run"]:
            break<jupyter_output><empty_output><jupyter_text>Сохранить модель<jupyter_code>remote_net.get(
    request_block=True,
    reason="test evaluation",
    timeout_secs=5
).save(f"model/small_signal_dataset.pt")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1060318664643399, 'fraction_numerical': 0.01270864946889226, 'mean_word_length': 3.955827067669173, 'pattern_counts': {'":': 10, '<': 37, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '113918', 'license': 'MIT', 'n_tokens_mistral': 1911, 'n_tokens_neox': 1889, 'n_words': 332, 'path': 'Small singnal dataset/PySyft_Exp_DS_small_signal_dataset.ipynb', 'repo_name': 'LasTAD/pysyft_experiments', 'size': '5272'}