Hello, I’m a beginner at using pytorch, so I’m not sure if I’m approaching this correctly. I have a layer at the beginning of my network to apply an acoustic filter on the magnitude of a FFT input of an audio signal (see equation C.171 from here for more details).
Here’s what I have for my layer implementation:
class ConeFilter(torch.nn.Module):
def __init__(self,
n_fft=512,
sample_rate=44100.0,
steps=100):
super().__init__()
self.x0 = torch.nn.Parameter(torch.zeros(1))
self.angle = torch.nn.Parameter(torch.zeros(1))
self.depth = torch.nn.Parameter(torch.zeros(1))
self.freq_map = torch.from_numpy(librosa.fft_frequencies(sr=sample_rate, n_fft=n_fft)).float()
self.c = 343 # Speed of sound in air m/s
self.rho = 1.293 # Air density kg/m^3
self.steps = 100
self.pi = 3.1415927
def forward(self, noisy):
scalar = self.c*self.rho
result = noisy
self.freq_map = self.freq_map.to('cuda:0')
tot_impedance = None
myx0 = torch.relu(self.x0) # x0 must be non-negative
mydepth = torch.relu(self.depth) # depth must be non-negative
myangle = ((torch.sigmoid(self.angle) + 1.0)/2.0) * (self.pi/2.0)
for i in range(self.steps):
x = (myx0 + (i * mydepth/self.steps)).float()
r = torch.tan(myangle) * x
numer = torch.mul(self.freq_map, x)
denom = torch.add(numer, self.c)
frac = torch.div(numer,denom)
impedance = frac * self.rho * self.c / (self.pi * r * r)
if (tot_impedance == None):
tot_impedance = impedance
else:
tot_impedance = torch.mul(tot_impedance, impedance)
tot_impedance = tot_impedance.unsqueeze(1)
tot_impedance = tot_impedance.unsqueeze(0)
result = torch.mul(result, tot_impedance)
self.print_parameters()
return result
def print_parameters(self):
print("x0: " + str(self.x0.data))
print("angle: " + str(self.angle.data * self.pi / 180.0))
print("depth: " + str(self.depth.data))
The idea is that I want the model to converge on the optimal cone that is expressed by the three terms: x0
, angle
, and depth
. My problem is that the output of the print_parameters()
function shows that the data values are always stuck at 0.0
. I read on another post that accessing the .data
field may break the autograd computation graph, but I’m not sure how else to verify that the weights are changing appropriately through training. Any insight would be greatly appreciated.