Hi I’m trying to adjust the VAE tutorial with my own dataset, this is a 1d dataset of the type:
tensor( [ 1.4570, -0.3061, -1.0579, 0.5670, .... ], dtype=torch.float64)
I’ve slightly modified the tutorial code to try make it work with here but with the code below
def train(svi, train_loader, use_cuda=False):
# initialize loss accumulator
epoch_loss = 0.
# do a training epoch over each mini-batch x returned
# by the data loader
for i, (x) in enumerate(train_loader):
# if on GPU put mini-batch into CUDA memory
if use_cuda:
x = x.cuda()
print(x[i])
# do ELBO gradient and accumulate loss
epoch_loss += svi.step(x[i])
# return epoch loss
normalizer_train = len(train_loader.dataset)
total_epoch_loss_train = epoch_loss / normalizer_train
return total_epoch_loss_train
vae = VAE() optimizer = Adam({"lr": 1.0e-3}) svi = SVI(vae.model, vae.guide, optimizer, loss=Trace_ELBO()) train_loss = train(svi, train_loader, use_cuda=False)
On this line
epoch_loss += svi.step(x[i])
I get the error :
’
TypeError: ‘builtin_function_or_method’ object cannot be interpreted as an integer
`
Thanks
Rhys
I can’t be sure without looking at the full code, but my guess is that x[i]
is just an integer and when you pass that into your model, and it tries to do any tensor operations, it fails. Why don’t you pass your full batch x
from the train_loader
instead?
Hi Neeraj Thanks for getting back to me, I made the change you suggested so it now reads
def train(svi, training_data):
# initialize loss accumulator
epoch_loss = 0.
# do a training epoch over each mini-batch x returned
# by the data loader
for i, (x) in enumerate(training_data) :
x = torch.tensor(x)
print(x.shape)
# do ELBO gradient and accumulate loss
epoch_loss += svi.step(x)
#epoch_loss = svi.step(training_data)
# return epoch loss
normalizer_train = len(train_loader.dataset)
total_epoch_loss_train = epoch_loss / normalizer_train
return total_epoch_loss_train
but still getting the same error. I’ve attached a link to the notebook on github below if you need to see the code in full:
[ Probabilistic-Programming/GW_VAE.ipynb at master · rgreen1995/Probabilistic-Programming · GitHub ]
Cheers
Rhys
I think you may need to change this line train(svi, x_train, use_cuda=False) to train(svi, train_loader, use_cuda=False).
for i, (x) in enumerate(training_data) to for i, (x) in enumerate(train_loader)
On a side note, it is not clear to me why enumerate is used at all in the tutorial. We are not making use of the batch index anyways. Why not just: for (x,_) in train_loader:
1 Like
I think the issue is in this line:
with pyro.iarange("data", x.size):
x.size
is just a method (you’ll need to call it with the dim arg, or alternatively use x.shape[0]
). You want to vectorize the first dimension by declaring it a data dimension as follows:
with pyro.iarange("data", x.size(0)):
Hi Both,
Thanks for the help, Neeraj you were right that was the problem and it’s working now
Thanks again
Rhys