How to check if a tensor is a float/double?

tueboesen: <p>How do I check whether a tensor is a float object without checking for all the specific dtypes (float16,float32,double). All I want, is to check that the object is some kind of float such that I can perform floating operations on it. I was expecting there to be some kind of abstract base class above all float types that I could check. Something like:</p>
<pre><code class="lang-auto">
isinstance(x,torch.FloatTensor)
</code></pre>
<p>But I haven’t found anything.</p>

KFrank: <p>Hi Tue!</p>
<aside class="quote no-group" data-username="tueboesen" data-post="1" data-topic="174945" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/tueboesen/48/17097_2.png" class="avatar"> tueboesen:</div>
<blockquote>
<p>How do I check whether a tensor is a float object</p>
</blockquote>
</aside>
<p>The best I can think of is some hackery using the string representation<br>
of the tensor’s <code>dtype</code>.  (Tensors of different <code>dtype</code>s are instances of<br>
the same class, namely <code>torch.Tensor</code>, so you can’t use the type of<br>
the tensor – in the sense of the class type of the tensor instance – to<br>
determine the tensor’s <code>dtype</code>.)</p>
<aside class="quote no-group">
<blockquote>
<pre><code class="lang-auto">isinstance(x,torch.FloatTensor)
</code></pre>
</blockquote>
</aside>
<p>Classes like <code>torch.FloatTensor</code> and <code>torch.DoubleTensor</code> aren’t really<br>
used anymore*.</p>
<p>Consider:</p>
<pre><code class="lang-plaintext">&gt;&gt;&gt; import torch
&gt;&gt;&gt; print (torch.__version__)
1.13.1
&gt;&gt;&gt;
&gt;&gt;&gt; tf = torch.FloatTensor ((5.6,))
&gt;&gt;&gt; td = torch.DoubleTensor ((5.6,))
&gt;&gt;&gt; tl = torch.LongTensor ((5.6,))
&gt;&gt;&gt;
&gt;&gt;&gt; type (tf)
&lt;class 'torch.Tensor'&gt;
&gt;&gt;&gt; type (tf).mro()
[&lt;class 'torch.Tensor'&gt;, &lt;class 'torch._C._TensorBase'&gt;, &lt;class 'object'&gt;]
&gt;&gt;&gt; tf.dtype
torch.float32
&gt;&gt;&gt; 'float' in str (tf.dtype)
True
&gt;&gt;&gt;
&gt;&gt;&gt; type (td)
&lt;class 'torch.Tensor'&gt;
&gt;&gt;&gt; type (td).mro()
[&lt;class 'torch.Tensor'&gt;, &lt;class 'torch._C._TensorBase'&gt;, &lt;class 'object'&gt;]
&gt;&gt;&gt; td.dtype
torch.float64
&gt;&gt;&gt; 'float' in str (td.dtype)
True
&gt;&gt;&gt;
&gt;&gt;&gt; type (tl)
&lt;class 'torch.Tensor'&gt;
&gt;&gt;&gt; type (tl).mro()
[&lt;class 'torch.Tensor'&gt;, &lt;class 'torch._C._TensorBase'&gt;, &lt;class 'object'&gt;]
&gt;&gt;&gt; tl.dtype
torch.int64
&gt;&gt;&gt; 'float' in str (tl.dtype)
False
</code></pre>
<p>*)  I suspect that in older versions of pytorch things like <code>FloatTensor</code> did<br>
exist as bona fide classes and presumably were subclasses of some sort<br>
of <code>torch.BaseTensor</code>.  I further suspect that for reasons of backward<br>
compatibility when in current versions of pytorch you “construct” a<br>
<code>FloatTensor</code> (as I did in the above example), the operation gets<br>
delegated to some sort of factory function such as <code>torch.tensor()</code><br>
(note the lower-case <code>tensor</code>).</p>
<p>Best.</p>
<p>K. Frank</p>

ptrblck: <p>Besides <a class="mention" href="/u/kfrank">@KFrank</a>’s checks you could use <code>torch.is_floating_point</code> as seen here:</p>
<pre><code class="lang-python">for dtype in [torch.float16, torch.bfloat16, torch.float32, torch.float64, torch.complex32, torch.complex64, torch.complex128, torch.int32, torch.int64]:
    x = torch.randn(1).to(dtype)
    print("dtype {} is_floating_point {}".format(dtype, torch.is_floating_point(x)))

# dtype torch.float16 is_floating_point True
# dtype torch.bfloat16 is_floating_point True
# dtype torch.float32 is_floating_point True
# dtype torch.float64 is_floating_point True
# dtype torch.complex32 is_floating_point False
# dtype torch.complex64 is_floating_point False
# dtype torch.complex128 is_floating_point False
# dtype torch.int32 is_floating_point False
# dtype torch.int64 is_floating_point False
</code></pre>

tueboesen: <p>I ended up using <a class="mention" href="/u/ptrblck">@ptrblck</a> suggestion, but your suggestion helped me understand the context, so thank you very much.</p>
<p>I guess all this also means that there is no good way to give typehints for torch tensors to be torch.floats or torch.ints, except maybe making a custom type union of all the float types manually and having that saved somewhere in the code. I’m surprised if this is the case, is there a particular reason why type-hinting like that isn’t used in pytorch?</p>