isayev / ReLeaSE

Deep Reinforcement Learning for de-novo Drug Design
MIT License
344 stars 134 forks source link

RuntimeError: dimension specified as 0 but tensor has no dimensions #13

Open DonaldChung-HK opened 5 years ago

DonaldChung-HK commented 5 years ago

when I am running train the model

for _ in range(n_iterations):

### Transfer learning 
RL.transfer_learning(transfer_data, n_epochs=n_transfer)
_, prediction = estimate_and_update(n_to_generate)
prediction_log.append(prediction)
if len(np.where(prediction >= threshold)[0])/len(prediction) > 0.15:
    threshold = min(threshold + 0.05, 0.8)

### Policy gtadient with experience replay 
for _ in range(n_policy_replay):
    rewards.append(RL.policy_gradient_replay(gen_data, replay, threshold=threshold, n_batch=10))
    print(rewards[-1])

_, prediction = estimate_and_update(n_to_generate)
prediction_log.append(prediction)
if len(np.where(prediction >= threshold)[0])/len(prediction) > 0.15:
    threshold = min(threshold + 0.05, 0.8)

### Policy graient without experinece replay 
for _ in range(n_policy):
    rewards.append(RL.policy_gradient(gen_data, threshold=threshold, n_batch=10))
    print(rewards[-1]) 

_, prediction = estimate_and_update(n_to_generate)
prediction_log.append(prediction)
if len(np.where(prediction >= threshold)[0])/len(prediction) > 0.15:
    threshold = min(threshold + 0.05, 0.8)

I experience this error


RuntimeError Traceback (most recent call last)

in () 10 11 ### Transfer learning ---> 12 RL.transfer_learning(transfer_data, n_epochs=n_transfer) 13 _, prediction = estimate_and_update(n_to_generate) 14 prediction_log.append(prediction) ~/ReLeaSE/reinforcement.py in transfer_learning(self, data, n_epochs, augment) 131 132 def transfer_learning(self, data, n_epochs, augment=False): --> 133 _ = self.generator.fit(data, n_epochs, augment=augment) ~/ReLeaSE/stackRNN.py in fit(self, data, n_epochs, all_losses, print_every, plot_every, augment) 330 for epoch in range(1, n_epochs + 1): 331 inp, target = data.random_training_set(smiles_augmentation) --> 332 loss = self.train_step(inp, target) 333 loss_avg += loss 334 ~/ReLeaSE/stackRNN.py in train_step(self, inp, target) 285 for c in range(len(inp)): 286 output, hidden, stack = self(inp[c], hidden, stack) --> 287 loss += self.criterion(output, target[c]) 288 289 loss.backward() ~/anaconda3/envs/ReLeaSE/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs) 489 result = self._slow_forward(*input, **kwargs) 490 else: --> 491 result = self.forward(*input, **kwargs) 492 for hook in self._forward_hooks.values(): 493 hook_result = hook(self, input, result) ~/anaconda3/envs/ReLeaSE/lib/python3.6/site-packages/torch/nn/modules/loss.py in forward(self, input, target) 757 _assert_no_grad(target) 758 return F.cross_entropy(input, target, self.weight, self.size_average, --> 759 self.ignore_index, self.reduce) 760 761 ~/anaconda3/envs/ReLeaSE/lib/python3.6/site-packages/torch/nn/functional.py in cross_entropy(input, target, weight, size_average, ignore_index, reduce) 1440 >>> loss.backward() 1441 """ -> 1442 return nll_loss(log_softmax(input, 1), target, weight, size_average, ignore_index, reduce) 1443 1444 ~/anaconda3/envs/ReLeaSE/lib/python3.6/site-packages/torch/nn/functional.py in nll_loss(input, target, weight, size_average, ignore_index, reduce) 1326 raise ValueError('Expected 2 or more dimensions (got {})'.format(dim)) 1327 -> 1328 if input.size(0) != target.size(0): 1329 raise ValueError('Expected input batch_size ({}) to match target batch_size ({}).' 1330 .format(input.size(0), target.size(0))) RuntimeError: dimension specified as 0 but tensor has no dimensions
Mariewelt commented 5 years ago

Hi @Donald954732

Looks like you are using pytorch 0.4. The error you got was due to updates in pytorch between versions 0.3 and 0.4. I updated the code, now everything should work just fine. Let me know if you have any issues!