Hi,

I’m trying to use the “Artificial Trust-Based Task Allocation (ATTA)” code from this github: Link

I am using Python 3.8.3 and PyTorch 1.7.1 as stated on github. All other packages were installed with pip and are the current available versions.

I’ve been attempting to debug the issue, but I’m unsure what the problem is and how to solve it.

Running file “*atta_caseII.py*” gives me an error in Line 527.

The code runs for some time until the error occurs.

Adding the full code here exceeds the character limit.

Full code of “atta_caseII.py”: https://github.com/arshaali/artificial-trust-task-allocation/blob/main/code/atta_caseII.py

Line 527:

```
loss.backward() #take deriv of loss function wrt the model parameters
```

Error:

```
Exception has occurred: RuntimeError
element 0 of tensors does not require grad and does not have a grad_fn
File "C:\[PATH_TO_FILE]\code\atta_caseII.py", line 527, in closure
loss.backward() #take deriv of loss function wrt the model parameters
File "C:\[PATH_TO_FILE]\code\atta_caseII.py", line 538, in <module>
optimizer.step(closure) #optimizer calculates the gradient and adjusts the parameters that will minimize the loss function
RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn
```

The code around Line 527:

```
def closure(): #closure function must be defined for pytorch
#this will calculate the gradients. this runs everytime.
#diff1 = model(bin_c, obs_probs_idxs)
#diff = torch.tensor(model(bin_c, obs_probs_idxs) - obs_probs_vect, requires_grad=True)
diff = model(bin_c, obs_probs_idxs) - obs_probs_vect #the diff between trust estimated from artificial trust model and trust approximation
#print("model diff = ", diff1)
#print("obs_probs_vect = ", obs_probs_vect)
#print("diff = ", diff)
#diff.retain_grad()
# loss = torch.tensor(torch.mean( torch.pow(diff, 2.0) ), requires_grad=True) #loss needs to be defined in pytorch
loss = torch.mean( torch.pow(diff, 2.0) ) #calculate the current loss
#loss.retain_grad() #Something to try if current implementation doesnt work
#loss = torch.mean( torch.pow( (model(bin_c, obs_probs_idxs) - obs_probs_vect), 2.0 ) )
#print("loss = ", loss)
#print("loss.grad_fn = ", loss.grad_fn)
optimizer.zero_grad() #standard command to give. Sets the gradients all to 0.
#print("ran zero grad")
loss.backward() #take deriv of loss function wrt the model parameters
#pytorch lets you choose a function to minimize. We are minimizing the loss function defined above.
#print("ran loss backward")
return loss
#print("_l1 = ", model.sigm( model.pre_l_1))
#print("_u1 = ", model.sigm( model.pre_u_1))
#print("_l2 = ", model.sigm( model.pre_l_2))
#print("_u2 = ", model.sigm( model.pre_u_2))
optimizer.step(closure) #optimizer calculates the gradient and adjusts the parameters that will minimize the loss function
#running the optimizer to update the parameters below
```