# Generic way to change float precision in FluxML

**URL:** https://discourse.julialang.org/t/generic-way-to-change-float-precision-in-fluxml/102893
**Category:** Machine Learning
**Tags:** flux, fastai
**Created:** [August 17, 2023, 7:24am UTC](https://discourse.julialang.org/t/generic-way-to-change-float-precision-in-fluxml/102893 "2023-08-17T07:24:48Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![pat-alt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pat-alt/32/34264_2.png) [@pat-alt](https://discourse.julialang.org/u/pat-alt)
#### Post date: [August 17, 2023, 7:24am UTC](https://discourse.julialang.org/t/generic-way-to-change-float-precision-in-fluxml/102893/1 "2023-08-17T07:24:48Z")

</div>

I’ve recently started using [`FastAI.jl`](https://github.com/FluxML/FastAI.jl) and firstly want to say how much I appreciate the efforts of anyone involved here - this is really cool!

When going through chapter 5 of the fast.ai book, I just came across the point on how decreasing float precision can help speed up training (this is of course also mentioned in the Flux docs and additionally encouraged through warnings). To this end, the Python fast.ai package comes with a simple callback function:

```python
from fastai.callback.fp16 import *
learn = cnn_learner(dls, resnet50, metrics=error_rate).to_fp16()

```

I don’t think this is available in [`FastAI.jl`](https://github.com/FluxML/FastAI.jl) or [`FluxTraining.jl`](https://github.com/FluxML/FluxTraining.jl), probably because it’s easy enough to ensure lower precision further upstream. Still, it would be nice to be able to just take a `Learner()` and change the floating point precision. Is that possible?

---

<div class="post-metadata">

### Author: ![pat-alt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pat-alt/32/34264_2.png) [@pat-alt](https://discourse.julialang.org/u/pat-alt)
#### Post date: [August 17, 2023, 7:47am UTC](https://discourse.julialang.org/t/generic-way-to-change-float-precision-in-fluxml/102893/2 "2023-08-17T07:47:04Z")

</div>

**Edit** : The error below happens when calling `FastAI.showbatch` on the data loader, so is related to plotting only.

Relatedly, I just found that the following

```julia
task = BlockTask(
    blocks,
    (   
        ProjectiveTransforms(
            (_resize, _resize), 
            sharestate=false,
        ),
        ImagePreprocessing(T=Float16),
        OneHot(),
    )
)

```

leads to an error when calling `FastAI.showbatch`. The following

```julia
batchsize = 3
train_dl, val_dl = taskdataloaders(train_data, val_data, task, batchsize)
showbatch(task, first(train_dl))

```

throws this error:

```julia
ArgumentError: N0f8 is an 8-bit type representing 256 values from 0.0 to 1.0; cannot represent -0.00024414062

```

Is that expected behaviour?

---

<div class="post-metadata">

### Author: ![ToucheSir](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/touchesir/32/14411_2.png) [@ToucheSir](https://discourse.julialang.org/u/ToucheSir)
#### Post date: [August 17, 2023, 2:24pm UTC](https://discourse.julialang.org/t/generic-way-to-change-float-precision-in-fluxml/102893/3 "2023-08-17T14:24:48Z")

</div>

You were right to suspect something upstream. Calling [`f16`](https://fluxml.ai/Flux.jl/stable/utilities/#Flux.f16) on the model is probably the easiest way to go. The built-in `ToGPU` callback uses [FluxTraining.jl - ToDevice](https://fluxml.ai/FluxTraining.jl/dev/FluxTraining@dev/ref/FluxTraining.ToDevice.html) under the hood, so you should be able to use that to also convert precision. Something like `ToDevice(gpu∘f16, gpu∘f16)`. That could also save you from having to set the precision of the data too early in `ImagePreprocessing(T=Float16)`, though without a full stacktrace it’s hard to tell where the error is coming from.

---

<div class="post-metadata">

### Author: ![pat-alt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pat-alt/32/34264_2.png) [@pat-alt](https://discourse.julialang.org/u/pat-alt)
#### Post date: [August 18, 2023, 6:26am UTC](https://discourse.julialang.org/t/generic-way-to-change-float-precision-in-fluxml/102893/4 "2023-08-18T06:26:44Z")

</div>

> [@ToucheSir](#):
>
> f16

Just what I was looking for, thanks @ToucheSir, and sorry I missed this one in the docs
