# TorchScript: indexing question / filling NaNs

**URL:** <https://discuss.pytorch.org/t/torchscript-indexing-question-filling-nans/53100>\
**Category:** jit\
**Created:** [August 11, 2019, 11:45am UTC](https://discuss.pytorch.org/t/torchscript-indexing-question-filling-nans/53100 "2019-08-11T11:45:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![achim](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/achim/32/8037_2.png) [@achim](https://discuss.pytorch.org/u/achim)\
**Post date:** [August 11, 2019, 11:45am UTC](https://discuss.pytorch.org/t/torchscript-indexing-question-filling-nans/53100/1 "2019-08-11T11:45:26Z")

</div>

Hi!

I’m trying to `jit.script`-compile a model which uses the `t[t != t] = ...` trick to fill the nans of a tensor with a default value. However, torchscript does not seem to appreciate this kind of indexing. Does anyone know a solution/workaround?

Thank you!

Here’s a small example plus error message:

```python
 @torch.jit.script 
 def f(): 
     t = torch.tensor(0.) 
     t[t!=t] = 7 

```

```nohighlight
RuntimeError: 
Arguments for call are not valid.
The following operator variants are available:
  
  aten::index_put_(Tensor(a) self, Tensor?[] indices, Tensor values, bool accumulate=False) -> (Tensor(a)):
  Expected a value of type 'Tensor' for argument 'values' but instead found type 'int'.
  
  aten::index_put_(Tensor(a) self, Tensor[] indices, Tensor values, bool accumulate=False) -> (Tensor(a)):
  Expected a value of type 'List[Tensor]' for argument 'indices' but instead found type 'List[Optional[Tensor]]'.

The original call is:
at <ipython-input-21-45d08c8a21f7>:4:5
@torch.jit.script
def f():
    t = torch.tensor(0.)
    t[t!=t] = 7
    ~~~~~~~~~~~ <--- HERE

```

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [August 11, 2019, 7:10pm UTC](https://discuss.pytorch.org/t/torchscript-indexing-question-filling-nans/53100/2 "2019-08-11T19:10:15Z")

</div>

I’m not sure if it’s the indexing or rather the rhs of the assignment, since the error says:

> Expected a value of type ‘Tensor’ for argument ‘values’ but instead found type ‘int’.

This seems to work:

```python
@torch.jit.script 
def f(): 
    t = torch.tensor([0.])
    t[t!=t] = torch.tensor(7.)
    return t

f()

```

---

<div class="post-metadata">

**Author:** ![achim](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/achim/32/8037_2.png) [@achim](https://discuss.pytorch.org/u/achim)\
**Post date:** [August 12, 2019, 11:19am UTC](https://discuss.pytorch.org/t/torchscript-indexing-question-filling-nans/53100/3 "2019-08-12T11:19:22Z")

</div>

Your interpretation of the error msg makes a lot more sense. 😄  
Thank you!
