facebookresearch / CrypTen

A framework for Privacy Preserving Machine Learning
MIT License
1.52k stars 278 forks source link

X.grad is none #488

Closed ReemAlsharabi closed 1 year ago

ReemAlsharabi commented 1 year ago

I'm trying to get the gradients of cryptensor but it's returning None:

images_enc = crypten.cryptensor(images, requires_grad=True)
labels_enc = crypten.cryptensor(labels, requires_grad=True)

def train_model(model, X, y, epochs=10, learning_rate=0.05):
    criterion = crypten.nn.CrossEntropyLoss()

    for epoch in range(epochs):
        model.zero_grad()
        output = model(X)
        loss = criterion(output, y)
        print(f"epoch {epoch} loss: {loss.get_plain_text()}")
        loss.backward()
        grads = X.grad
        print(grads)
        model.update_parameters(learning_rate)
    return model, grads

model, grads = train_model(model, images_enc[:10, ], labels_enc[:10,], epochs=3)

Output of print(grads): None

ReemAlsharabi commented 1 year ago

The problem was using [:10, ] indexing. See #210 Sol.: replace model, grads = train_model(model, images_enc[:10, ], labels_enc[:10,], epochs=3) with model, grads = train_model(model, images_enc, labels_enc, epochs=3)