ddhostallero / BiG-DRP

1 stars 3 forks source link

AttributeError and RunTimeError #3

Closed PascalIversen closed 2 years ago

PascalIversen commented 2 years ago

Hi again :) I am running python main.py --mode=train --dataroot=.\ and I am getting the error:

Traceback (most recent call last):
  File "main.py", line 40, in <module>
    main(args)
  File "main.py", line 15, in main
    main_fxn.main(FLAGS)
  File ".../BiG-DRP/bigdrp/main_cv.py", line 237, in main
    test_metrics = nested_cross_validation(FLAGS, drug_feats, cell_lines, labels, label_matrix, normalizer)
  File ".../BiG-DRP/bigdrp/main_cv.py", line 169, in nested_cross_validation
    epoch=hp['num_epoch'], final=False)
  File ".../BiG-DRP/bigdrp/main_cv.py", line 57, in fold_validation
    trainer = Trainer(n_genes, cell_lines, drug_feats, network, hyperparams)
  File ".../BiG-DRP/bigdrp/trainer.py", line 35, in __init__
    self.blocks = [b.to(self.device) for b in graph_sampler.sample_blocks(self.network, {'drug': range(len(drug_feats))})]
  File ".../miniconda3/envs/bigdrpp37/lib/python3.7/site-packages/dgl/dataloading/dataloader.py", line 324, in sample_blocks
    for ntype, nodes in seed_nodes_in.items()}
  File ".../miniconda3/envs/bigdrpp37/lib/python3.7/site-packages/dgl/dataloading/dataloader.py", line 324, in <dictcomp>
    for ntype, nodes in seed_nodes_in.items()}
AttributeError: 'range' object has no attribute 'to'

I guess in self.blocks = [b.to(self.device) for b in graph_sampler.sample_blocks(self.network, {'drug': range(len(drug_feats))})] I need to cast the range to a tensor as: torch.tensor(list(range(len(drug_feats)))).

However, running this gives me:

Traceback (most recent call last):
  File "main.py", line 40, in <module>
    main(args)
  File "main.py", line 15, in main
    main_fxn.main(FLAGS)
  File ".../BiG-DRP/bigdrp/main_cv.py", line 237, in main
    test_metrics = nested_cross_validation(FLAGS, drug_feats, cell_lines, labels, label_matrix, normalizer)
  File ".../BiG-DRP/bigdrp/main_cv.py", line 169, in nested_cross_validation
    epoch=hp['num_epoch'], final=False)
  File ".../BiG-DRP/bigdrp/main_cv.py", line 62, in fold_validation
    tuning=tuning)
  File ".../BiG-DRP/bigdrp/trainer.py", line 133, in fit
    train_metrics = self.train_step(train_loader, self.device)
  File ".../BiG-DRP/bigdrp/trainer.py", line 50, in train_step
    pred = self.model(self.blocks, self.drug_feats, self.cell_feats, x, d1)
  File ".../miniconda3/envs/bigdrpp37/lib/python3.7/site-packages/torch/nn/modules/module.py", line 1130, in _call_impl
    return forward_call(*input, **kwargs)
  File ".../BiG-DRP/bigdrp/model.py", line 88, in forward
    h2 = {k: F.leaky_relu(v + self.alpha*h1[k]) for k, v in h2.items()}
  File ".../BiG-DRP/bigdrp/model.py", line 88, in <dictcomp>
    h2 = {k: F.leaky_relu(v + self.alpha*h1[k]) for k, v in h2.items()}
RuntimeError: The size of tensor a (0) must match the size of tensor b (405) at non-singleton dimension 0

Seems like the cell_line tensor in h2 in the forward() after h2 = self.conv2(blocks[1], h1) of BiGDRP is empty:

{'cell_line': tensor([], size=(0, 512), grad_fn=<SumBackward1>), 'drug': tensor([[ 0.5645, -0.3377, -0.6438,
  ..., -0.3254, -0.1739, -0.5733],
        [ 0.3454, -0.0190, -0.3623,  ..., -0.1903, -0.1232, -0.4309],
        [ 0.5056, -0.2610, -0.3448,  ..., -0.2536, -0.1260, -0.4715],
        ...,
        [ 0.6368,  0.0211, -0.0124,  ..., -0.3359, -0.4111, -0.2941],
        [ 0.4178,  0.1202, -0.1740,  ..., -0.3778, -0.1694, -0.5124],
        [ 0.3049, -0.1588, -0.3898,  ..., -0.2842, -0.2146, -0.3605]],
       grad_fn=<SumBackward1>)}

(This is not the case for h1.) Any idea what could be the issue here? I run the code with your data and my dgl version is '0.7.2' since '0.7.1' is not available for cuda 11.4. but I guess that should not make any difference.

Thanks!

ddhostallero commented 2 years ago

Hi,

The 'cell_line' entry in the dictionary should not exist in 0.7.1 version. I suggest you delete all the lines with:

h2 = {k: F.leaky_relu(v + self.alpha*h1[k]) for k, v in h2.items()}

and replace it with:

h2['drug'] = F.leaky_relu(h2['drug'] + self.alpha*h1['drug'])

in the model.py

PascalIversen commented 2 years ago

great, that works, thanks!