The error only happens when i include the code for randomiser in the network, I have included all my code below
This is the code I used to generate the data
Nf = 2000 # the number of different functions f that we will generate
Npts = 40 # the number of x values that we will use to generate each fa
x = torch.zeros(Nf, Npts, 1)
for k in range(Nf):
x[k,:,0] = torch.linspace(-2, 2, Npts)
x += torch.rand_like(x)*0.1
a = -2 + 4*torch.rand(Nf).view(-1,1).repeat(1, Npts).unsqueeze(2)
y = a*torch.sin(x+a)
The code for the data loader
dataset = data.TensorDataset(x,y) #TensorDataset object
#print(dataset[0]) #First example in our dataset
batch_size = 200
data_iter = data.DataLoader(dataset, batch_size, shuffle=True) #DataLoader obj
x,y = next(iter(data_iter))
print(x.size())
print (y.shape)
The code defining the randomizer
def randomiser(n, data):
if n > 0:
rand_k = np.zeros(n)
elif n <= 0:
raise valueError("n shouldnt be zero or negative")
repeat_check=[]
ele = 0
while ele < n:
r = np.random.randint(len(data))
if r not in repeat_check:
rand_k[ele] = data[r]
repeat_check.append(r)
ele +=1
else:
ele = ele
return np.int32(rand_k)
The code defining the indexing function
i=0
Nc = randomiser(1, range(3, 37))[0]
def context_indexer (Nc, x, y):
indx = randomiser(Nc, range(39))
idx = torch.as_tensor(indx, dtype=torch.int64)
x_c = torch.index_select(x,1,idx)
y_c = torch.index_select(y,1,idx)
return x_c, y_c
x_c, y_c = context_indexer(Nc,x,y)
print(y_c)
The code for the encoder
class Enc(torch.nn.Module):
def __init__(self, input_dim, output_dim):
super(Enc, self).__init__()
self.input_dim = input_dim
self.output_dim = output_dim
hidden1 = 40
hidden2 = 10
self.Lin1 = torch.nn.Linear(self.input_dim,hidden1)
self.ReL = torch.nn.ReLU()
#self.ReLU = nn.Sigmoid()
self.Lin2 = torch.nn.Linear(hidden1, hidden2)
self.Linout = torch.nn.Linear(hidden2, self.output_dim)
def forward(self, x_c, y_c):
Nc = randomiser(1, range(3, 37)[0]
x_c, y_c = context_indexer(Nc, x, y)
x_c = x_c.view(-1, Nc)
y_c = y_c.view(-1, Nc)
pairs = torch.stack([x_c, y_c],2)
out = self.Lin1(pairs)
out = self.ReL(out)
out = self.Linout(out)
r_c = torch.mean(out, dim=1)
return out
code to run the encoder
encode=Enc(input_dim=2, output_dim=4)
out = encode(x,y)