isayev / ReLeaSE

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

missing jak2 train #2

Closed Running-z closed 5 years ago

Running-z commented 5 years ago

I tried to run your JAK2-demo using my jak2 data, but when I execute to "train_model" here, I get an unexpected error

Execute the code in the following section to report an error:

rewards = []
n_to_generate = 1000
n_policy_replay = 10
n_policy = 5
n_transfer = 500
n_iterations = 5
prediction_log = []

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)

Get the following error:

RuntimeError                              Traceback (most recent call last)
<ipython-input-51-d78b3f91f2cc> in <module>()
     11 
     12     ## Transfer learning
---> 13     RL.transfer_learning(transfer_data, n_epochs=n_transfer)
     14 #     _, prediction = estimate_and_update(n_to_generate)
     15 #     prediction_log.append(prediction)

/project/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)

/project/ReLeaSE/stackRNN.py in fit(self, data, n_epochs, all_losses, print_every, plot_every, augment)
    332         for epoch in range(1, n_epochs + 1):
    333             inp, target = data.random_training_set(smiles_augmentation)
--> 334             loss = self.train_step(inp, target)
    335             loss_avg += loss
    336 

/project/ReLeaSE/stackRNN.py in train_step(self, inp, target)
    287         for c in range(len(inp)):
    288             output, hidden, stack = self(inp[c], hidden, stack)
--> 289             loss += self.criterion(output, target[c])
    290 
    291         loss.backward()

~/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    475             result = self._slow_forward(*input, **kwargs)
    476         else:
--> 477             result = self.forward(*input, **kwargs)
    478         for hook in self._forward_hooks.values():
    479             hook_result = hook(self, input, result)

~/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/nn/modules/loss.py in forward(self, input, target)
    860     def forward(self, input, target):
    861         return F.cross_entropy(input, target, weight=self.weight,
--> 862                                ignore_index=self.ignore_index, reduction=self.reduction)
    863 
    864 

~/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/nn/functional.py in cross_entropy(input, target, weight, size_average, ignore_index, reduce, reduction)
   1548     if size_average is not None or reduce is not None:
   1549         reduction = _Reduction.legacy_get_string(size_average, reduce)
-> 1550     return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction)
   1551 
   1552 

~/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/nn/functional.py in nll_loss(input, target, weight, size_average, ignore_index, reduce, reduction)
   1401         raise ValueError('Expected 2 or more dimensions (got {})'.format(dim))
   1402 
-> 1403     if input.size(0) != target.size(0):
   1404         raise ValueError('Expected input batch_size ({}) to match target batch_size ({}).'
   1405                          .format(input.size(0), target.size(0)))

RuntimeError: dimension specified as 0 but tensor has no dimensions

I didn't encounter any mistakes before this step, but this step got an unexpected error. Can you give me some guidance?

Mariewelt commented 5 years ago

@Running-z That is you due to updates in pytorch. You can either downgrade your pytorch to 0.3 version and keep using code from master branch, or you can checkout develop branch. Let me know if that works for you.

Running-z commented 5 years ago

@Mariewelt I tried to replace the pytorch0.3 environment, then run jak2_demo, and finally I got an unexpected error in "train_model", The errors obtained are as follows:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-27-9d34a040fb80> in <module>()
     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)

/deepchem/code/ReLeaSE_0.3.0/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)

/deepchem/code/ReLeaSE_0.3.0/stackRNN.py in fit(self, data, n_epochs, all_losses, print_every, plot_every, augment)
    329 
    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

/deepchem/code/ReLeaSE_0.3.0/data.py in random_training_set(self, smiles_augmentation)
     87 
     88     def random_training_set(self, smiles_augmentation):
---> 89         chunk = self.random_chunk()
     90         if smiles_augmentation is not None:
     91             chunk = '<' + smiles_augmentation.randomize_smiles(chunk[1:-1]) + '>'

/deepchem/code/ReLeaSE_0.3.0/data.py in random_chunk(self)
     67             random_smiles (str).
     68         """
---> 69         index = random.randint(0, self.file_len-1)
     70         return self.file[index]
     71 

/miniconda/lib/python3.6/random.py in randint(self, a, b)
    219         """
    220 
--> 221         return self.randrange(a, b+1)
    222 
    223     def _randbelow(self, n, int=int, maxsize=1<<BPF, type=type,

/miniconda/lib/python3.6/random.py in randrange(self, start, stop, step, _int)
    197             return istart + self._randbelow(width)
    198         if step == 1:
--> 199             raise ValueError("empty range for randrange() (%d,%d, %d)" % (istart, istop, width))
    200 
    201         # Non-unit step argument supplied.

ValueError: empty range for randrange() (0,0, 0)

Can this give some explanation?

Mariewelt commented 5 years ago

@Running-z It looks to me like your transfer pool is empty. You can make sure it's empty by printing it out: print(transfer_data.file) You transfer pool is empty, because all the probabilities returned by your predictive model are less than threshold, so there is nothing to put in the transfer pool. There are several things you can try to avoid this: