Skip to content

Instantly share code, notes, and snippets.

@franperezlopez
franperezlopez / langchain_otel.ipynb
Last active February 13, 2024 09:11
Open Telemetry for Langchain
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

Export fast.ai models to ONNX

fast.ai is an amazing library, allowing us to use a vast array of pre-trained models, but it also can be used as a foundation for new models.

The fast.ai tabular model is a great model that uses:

  • an embedding layer for representing categorical features
  • parametrized number of hidden layers modeling the continuous features
  • applies batch normalization, droput and weight decay to regularize the model (prevents overfitting and allows the model to be trained faster)

The code is so simple, that it takes an small amount of time to check it out. After reading the code, I wanted to make my "own" version of the tabular model. Mainly I wanted to try a new way to compose the hidden layers. This model uses Batch Norm + Linear + Dropout + Activation Function (ReLU), but some papers claim that the Activation Function should be placed before the Batch Norm the Dropout layers (activation functions used to be zero ce

from fastai import torch_core, layers
from fastai.basic_data import *
from fastai.core import *
from fastai.layers import embedding
from fastai.basic_train import Learner
from torch import nn, optim, as_tensor, Tensor
import torch
import logging
def tabularexperiment_learner(data:DataBunch, layers:Collection[int], emb_szs:Dict[str,int]=None, metrics=None,
@franperezlopez
franperezlopez / cache_remove.r
Created September 1, 2017 15:57
rMarkdown remove cache
unlink('housePrices_cache', recursive = TRUE)
@franperezlopez
franperezlopez / chunk_cache
Last active September 1, 2017 16:15
rMarkdown cache attributes
```{r rf, cache=T}
# train random forest model
```
```{r cache=T, dependson=”rf”}
# predict using random forest model
```
@franperezlopez
franperezlopez / chunk_eval
Created September 1, 2017 15:54
rMarkdown compilation flags using variables on eval
```{r}
eval.trainModels=T
eval.pcaOptimization=T
```
```{r eval=eval.pcaOptimization, include=eval.pcaOptimization}
# code applying PCA optimization
...
@franperezlopez
franperezlopez / chunk_save_error
Created September 1, 2017 15:52
rMarkdown save environment document on error
```{r}
options(error=function() {save.image(file="error.rmd.RData")})
```
@franperezlopez
franperezlopez / load.r
Created September 1, 2017 15:50
rMarkdown load environment from document
# reset current environment (optional)
rm(list=ls())
# load environment from file
load("housePrices.rmd.RData")
@franperezlopez
franperezlopez / load.r
Created September 1, 2017 15:50
rMarkdown load environment from document
# reset current environment (optional)
rm(list=ls())
# load environment from file
load("housePrices.rmd.RData")
@franperezlopez
franperezlopez / chunk_save
Last active September 1, 2017 16:16
rMarkdown save document environment
```{r}
# save document's environment (replaces existing one)
save.image(file="housePrices.rmd.RData")
# archive (optional)
save.image(file=paste0("housePrices_", format(Sys.time(),"%d%m%y_%H%M"), ".rmd.RData"))
```