earmingol / cell2cell

User-friendly tool to infer cell-cell interactions and communication from gene expression of interacting proteins
BSD 3-Clause "New" or "Revised" License
56 stars 12 forks source link

tensor mask causes gpu tensor factorization to fail #6

Closed ckmah closed 3 years ago

ckmah commented 3 years ago

Input code

# Create mask for np.nan values
tensor_mask = ~np.isnan(tensor)

# Set nans to 0 otherwise loss is always nan
tensor[~tensor_mask] = 0

# Create tensor, set device to gpu
tensor_c2c = c2c.tensor.PreBuiltTensor(
    tensor,
    order_names=[...],
    order_labels=[...],
    mask=tensor_mask,
    device='cuda:0'
)

# Perform elbow analysis to choose best rank
fig, error = tensor_c2c.elbow_rank_selection(
    upper_rank=27,
    runs=4,
    init="random",
    automatic_elbow=True,
    random_state=888,
)

Output

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/tmp/ipykernel_2254180/4264400354.py in <module>
----> 1 fig, error = tensor_c2c.elbow_rank_selection(
      2     upper_rank=27,
      3     runs=4,
      4     init="random",
      5     automatic_elbow=True,

~/miniconda3/envs/py3.8/lib/python3.8/site-packages/cell2cell/tensor/tensor.py in elbow_rank_selection(self, upper_rank, runs, tf_type, init, random_state, automatic_elbow, mask, ci, figsize, fontsize, filename, verbose, **kwargs)
    252                              filename=filename)
    253         elif runs > 1:
--> 254             all_loss = _multiple_runs_elbow_analysis(tensor=self.tensor,
    255                                                      upper_rank=upper_rank,
    256                                                      runs=runs,

~/miniconda3/envs/py3.8/lib/python3.8/site-packages/cell2cell/tensor/factorization.py in _multiple_runs_elbow_analysis(tensor, upper_rank, runs, tf_type, init, random_state, mask, verbose, **kwargs)
    210                                                       verbose=verbose,
    211                                                       **kwargs)
--> 212             run_errors.append(_compute_norm_error(tensor, tl_object, mask))
    213         all_loss.append(run_errors)
    214 

~/miniconda3/envs/py3.8/lib/python3.8/site-packages/cell2cell/tensor/factorization.py in _compute_norm_error(tensor, tl_object, mask)
    257             mask_ = mask
    258         diff = tl.tensor(np.ones(mask.shape), **context) - mask_,
--> 259         tensor_ = tensor * mask_ + rec_ * (diff)
    260     else:
    261         tensor_ = tensor

TypeError: only integer tensors of a single element can be converted to an index
earmingol commented 3 years ago

Thanks @ckmah. This is now fixed in the version 0.5.2