<jupyter_start><jupyter_text>Train<jupyter_code>use_cuda = torch.cuda.is_available()
# use_cuda = False

# torch.manual_seed(args.seed)

device = torch.device("cuda" if use_cuda else "cpu")

# train_kwargs = {'batch_size': args.batch_size}
# test_kwargs = {'batch_size': args.test_batch_size}
if use_cuda:
    cuda_kwargs = {'num_workers': 1,
#                    'shuffle': True,
                   'pin_memory': True}
#     train_kwargs.update(cuda_kwargs)
#     test_kwargs.update(cuda_kwargs)

# load data (will need to be adapted as well)
# 1) load A_test
# 2) load labels
# 3) Perform train-test-split
dataset = HindroidDataset(
    'data/out/all-apps/hindroid-train-set/A_test.npz', 
    'data/out/all-apps/hindroid-train-set/predictions.csv',
    'ABAT',
)
# weights = torch.Tensor([dataset.labels.mean() / (1-dataset.labels.mean()), 1]).to(device).double()
weights = None
train_loader = torch.utils.data.DataLoader(
    dataset, 
    batch_size=5,
    shuffle = False,
    sampler = ImbalancedDatasetSampler(dataset, callback_get_label = hindroid_custom_get_label),
    **cuda_kwargs)
test_loader = torch.utils.data.DataLoader(
    dataset, 
    batch_size=5,
    shuffle = False,
    sampler = ImbalancedDatasetSampler(dataset, callback_get_label = hindroid_custom_get_label),
    **cuda_kwargs)
# train_loader = torch.utils.data.DataLoader(dataset, batch_size=10)
# test_loader = torch.utils.data.DataLoader(dataset, batch_size=10)

model = HindroidSubstitute(dataset.feature_width).double().to(device)
optimizer = optim.Adadelta(model.parameters(), lr=0.005)
scheduler = StepLR(optimizer, step_size=1)
for epoch in range(1, 20 + 1):
    train(model, device, train_loader, optimizer, epoch, weights)
    test(model, device, test_loader, weights)
    scheduler.step()
test_loader = torch.utils.data.DataLoader(
    dataset, 
    batch_size=10,
    **cuda_kwargs)
model.eval()
all_preds = []
with torch.no_grad():
    for data, target in test_loader:
        data, target = data.to(device), target.to(device)
        output = model(data)
        output = F.log_softmax(output, dim=1)
        pred = output.argmax(dim=1, keepdim=True)  # get the index of the max log-probability
        all_preds.extend(pred.tolist())

print(classification_report(dataset.labels, all_preds))
print(confusion_matrix(dataset.labels, all_preds))
with open(os.path.join(outfolder, 'NN_sub.pkl'), 'wb') as file:
    torch.save(model, file)
outfolder = 'data/out/all-apps/hindroid-train-set-ABAT/'
with open(os.path.join(outfolder, 'NN_sub.pkl'), 'rb') as file:
    model = torch.load(file).to(device)

batch_size = 10
test_loader = torch.utils.data.DataLoader(
    dataset, 
    batch_size=batch_size,
    shuffle = True,
#     sampler = ImbalancedDatasetSampler(dataset, callback_get_label = hindroid_custom_get_label),
    **cuda_kwargs)
from src.attack.cw import to_tanh_space, from_tanh_space, L2Adversary

%load_ext autoreload
%autoreload 2
cw_attack = L2Adversary(
    targeted=False, confidence=0.0, c_range=(1e-1, 1e10),
    search_steps=3, max_steps=1000, abort_early=True,
    box=(0., 1.), optimizer_lr=1e-2, init_rand=True
)
advxs = None
all_inputs = []
all_advx = []

for data, target in test_loader:
    if len(all_advx) * batch_size >= 500:
        break
    
    print("OG Labels: ", target)
    target = F.softmax(model(data.to(device)), dim=1)
    print("Model pred: ", target)
    target = torch.argmax(target, dim=1)
    print(target)
    data = data.view(batch_size, -1)
    with open(os.path.join(outfolder, 'input.npz'), 'wb') as file:
        all_inputs.append(sparse.csr_matrix(data))
        sparse.save_npz(file, sparse.csr_matrix(data))
    data, target = data.to(device), target.to(device)
    advxs = cw_attack(model, data, target, to_numpy=False)
    sparse_advxs = sparse.csr_matrix(torch.round(advxs), dtype='i1')
    all_advx.append(sparse_advxs)
    
    with open(os.path.join(outfolder, 'advxs.npz'), 'wb') as file:
        sparse.save_npz(file, sparse_advxs)
        
all_inputs = sparse.vstack(all_inputs)
all_advx = sparse.vstack(all_advx)
# attack_folder = os.path.join('data', 'out', 'all-apps', 'hind')
# os.makedirs(attack_folder, exist_ok=True)

with open(os.path.join(outfolder, 'inputs.npz'), 'wb') as file:
    sparse.save_npz(file, sparse.csr_matrix(all_inputs))

with open(os.path.join(outfolder, 'advxs.npz'), 'wb') as file:
    sparse.save_npz(file, sparse.csr_matrix(all_advx))
with open('advxs.npz', 'wb') as file:
    sparse.save_npz(file, sparse.csr_matrix(advxs))
torch.min(torch.abs(advxs - 0.5)) 
torch.argmax(F.softmax(model(torch.round(advxs.double().to(device))), dim=1), dim=1)
mat = sparse.csr_matrix(torch.round(advxs))
density = mat.getnnz() / np.prod(mat.shape)
density
mat.data.nbytes + mat.indptr.nbytes + mat.indices.nbytes
2535703 * 1e-5
with open('advxs.npz', 'wb') as file:
    sparse.save_npz(file, sparse.csr_matrix(advxs))
to_tanh_space(torch.Tensor(np.arange(-1,1,.001)).double(), [0., 1.])
pd.Series(to_tanh_space(torch.Tensor(np.arange(-1,1,.001)).double(), [0., 1.]).tolist(), 
          index=np.arange(-1,1,.001)).plot()
inputs = torch.Tensor(np.random.randint(0,2, (3,4)))
inputs
to_tanh_space(inputs, [0., 1.])
torch.clamp(to_tanh_space(inputs, [0., 1.]), min=-1e4)
from_tanh_space(torch.clamp(to_tanh_space(inputs, [0., 1.]), min=-1e4), [0., 1.])
torch.clamp(inputs + from_tanh_space(pert_tanh*1000, [0., 1.]), max=1.0)
pert_tanh = torch.zeros((3,4))  # type: torch.FloatTensor
nn.init.normal_(pert_tanh, mean=0, std=1)
pert_tanh
from_tanh_space(pert_tanh*1e4, [0., 1.])
torch.clamp(inputs + from_tanh_space(pert_tanh*1e4, [0., 1.]), max=1.0)
pd.Series(from_tanh_space(
    (to_tanh_space(torch.Tensor(np.arange(-1,1,.001)).double(), [-1., 1.])*1000), [-1., 1]).tolist(),
    index=to_tanh_space(torch.Tensor(np.arange(-1,1,.001)).double(), [-1., 1.]).tolist()).plot()
pd.Series(from_tanh_space(torch.Tensor(np.arange(-1000, 1000, 1)), box=[0,1]).tolist(),
    index=np.arange(-1000, 1000, 1)).plot()
from_tanh_space(to_tanh_space(torch.Tensor([0,.999]).double(), [-1., 1.]), [-1., 1])
# def main():
#     # Training settings
#     parser = argparse.ArgumentParser(description='PyTorch MNIST Example')
#     parser.add_argument('--batch-size', type=int, default=64, metavar='N',
#                         help='input batch size for training (default: 64)')
#     parser.add_argument('--test-batch-size', type=int, default=1000, metavar='N',
#                         help='input batch size for testing (default: 1000)')
#     parser.add_argument('--epochs', type=int, default=14, metavar='N',
#                         help='number of epochs to train (default: 14)')
#     parser.add_argument('--lr', type=float, default=1.0, metavar='LR',
#                         help='learning rate (default: 1.0)')
#     parser.add_argument('--gamma', type=float, default=0.7, metavar='M',
#                         help='Learning rate step gamma (default: 0.7)')
#     parser.add_argument('--no-cuda', action='store_true', default=False,
#                         help='disables CUDA training')
#     parser.add_argument('--dry-run', action='store_true', default=False,
#                         help='quickly check a single pass')
#     parser.add_argument('--seed', type=int, default=1, metavar='S',
#                         help='random seed (default: 1)')
#     parser.add_argument('--log-interval', type=int, default=10, metavar='N',
#                         help='how many batches to wait before logging training status')
#     parser.add_argument('--save-model', action='store_true', default=False,
#                         help='For Saving the current Model')
#     args = parser.parse_args()
    use_cuda = not args.no_cuda and torch.cuda.is_available()

    torch.manual_seed(args.seed)

    device = torch.device("cuda" if use_cuda else "cpu")

    train_kwargs = {'batch_size': args.batch_size}
    test_kwargs = {'batch_size': args.test_batch_size}
    if use_cuda:
        cuda_kwargs = {'num_workers': 1,
                       'pin_memory': True,
                       'shuffle': True}
        train_kwargs.update(cuda_kwargs)
        test_kwargs.update(cuda_kwargs)

    # load data (will need to be adapted as well)
    # 1) load A_test
    # 2) load labels
    # 3) Perform train-test-split
#     transform=transforms.Compose([
#         transforms.ToTensor(),
#         transforms.Normalize((0.1307,), (0.3081,))
#         ])
#     dataset1 = datasets.MNIST('../data', train=True, download=True,
#                        transform=transform)
#     dataset2 = datasets.MNIST('../data', train=False,
#                        transform=transform)
    dataset = HindroidDataset(
        'data/out/all-apps/hindroid-train-half/A_test.npz', 
        'data/out/all-apps/hindroid-train-half/predictions.csv',
        'AAT'
    )
    train_loader = torch.utils.data.DataLoader(dataset,**train_kwargs)
    test_loader = torch.utils.data.DataLoader(dataset, **test_kwargs)

    model = HindroidSubstitute().to(device)
    optimizer = optim.Adadelta(model.parameters(), lr=args.lr)

    scheduler = StepLR(optimizer, step_size=1, gamma=args.gamma)
    for epoch in range(1, args.epochs + 1):
        train(args, model, device, train_loader, optimizer, epoch)
        test(model, device, test_loader)
        scheduler.step()

    if args.save_model:
        torch.save(model.state_dict(), "mnist_cnn.pt")


# if __name__ == '__main__':
#     main()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15765527457411915, 'fraction_numerical': 0.023066342186012063, 'mean_word_length': 3.612981942410932, 'pattern_counts': {'":': 0, '<': 5, '<?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': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '505049', 'license': 'MIT', 'n_tokens_mistral': 3530, 'n_tokens_neox': 3381, 'n_words': 592, 'path': 'notebooks/Attack.ipynb', 'repo_name': 'rcgonzalez9061/m2v-adversarial-hindoird', 'size': '9451'}