Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
DonaldChung-HK opened this issue Sep 25, 2018 · 1 comment
Labels
duplicate This issue or pull request already exists

Comments

@DonaldChung-HK
Copy link

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 Mariewelt added the duplicate This issue or pull request already exists label Sep 25, 2018
@Mariewelt
Copy link
Collaborator

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants