UPDATE: @ptrblck, I’ve simplified the code much further. Intriguingly, now both versions show erratic gradients!
FILE 1: testgrad.py
import torch
from torch.autograd import Variable
import numpy as np

class SimBEN():

    def __init__(self):  # these parameters depend on the experiment
        self.num_cells = 3
        self.EdgeList = np.array([[0,2],[0,1],[1,2]])
        self.NumEdges = len(self.EdgeList)
        self.Weights = torch.ones(self.NumEdges,requires_grad=True)  # for the more nonlinear version of simulate
        self.W = torch.zeros(self.num_cells*self.num_cells).view(self.num_cells,self.num_cells)
        self.W[self.EdgeList[:,1],self.EdgeList[:,0]] = self.Weights
        self.OutputVolts = torch.FloatTensor([0.08])

    def Simulate(self):    
        self.cc_cells = torch.ones(self.num_cells)*10
        self.cc_cells = self.cc_cells + torch.sum(self.W)*0.001 # + self.Dm_array*0.001 
        self.vm = torch.sum(self.cc_cells).view(1)

    def MSELoss(self,vm,target_vm):
        self.err = (vm - target_vm).pow(2).mean() 

    def MasterSim(self):
        self.TotalWGrad = torch.zeros(1).resize_as_(self.Weights)
        self.Simulate()
        self.MSELoss(self.vm,self.OutputVolts)
        self.err.backward()
        self.TotalWGrad = self.TotalWGrad + self.Weights.grad
        print(self.TotalWGrad)

    def MasterLearn(self):
        self.MasterSim()

FILE 2: runtestgrad.py
import testgrad
from testgrad import SimBEN

for i in range(100):
	simBEN = SimBEN()
	simBEN.MasterLearn()

FILE 3: testgradproc.py
import torch
from torch.autograd import Variable
import numpy as np

def runtest():
    num_cells = 3
    EdgeList = np.array([[0,2],[0,1],[1,2]])
    NumEdges = len(EdgeList)
    Weights = torch.ones(NumEdges,requires_grad=True)  # for the more nonlinear version of simulate
    W = torch.zeros(num_cells*num_cells).view(num_cells,num_cells)
    W[EdgeList[:,1],EdgeList[:,0]] = Weights
    OutputVolts = torch.FloatTensor([30.08])

    def Simulate(W):    
        cc_cells = torch.ones(num_cells)*10
        cc_cells = cc_cells + torch.sum(W)*0.001
        vm = torch.sum(cc_cells).view(1)
        return(vm)

    def MSELoss(vm,target_vm):
        err = (vm - target_vm).pow(2).mean() 
        return(err)

    TotalWGrad = torch.zeros(1).resize_as_(Weights)
    vm = Simulate(W)
    err = MSELoss(vm,OutputVolts)
    err.backward()
    TotalWGrad = TotalWGrad + Weights.grad
    print(TotalWGrad)

FILE 4: runtestgradproc.py
from testgradproc import runtest

for i in range(100):
	runtest()