Skip to content

Instantly share code, notes, and snippets.

View Steboss89's full-sized avatar

Stefano Bosisio Steboss89

View GitHub Profile
@Steboss89
Steboss89 / download_mnist.rs
Created August 12, 2022 15:58
Download the MNIST with mnist
use mnist::*;
fn main()-> Result<(), Box<dyn, Error>>{
// Deconstruct the returned Mnist struct.
let Mnist {
trn_img,
trn_lbl,
val_img,
val_lbl,
tst_img,
@Steboss89
Steboss89 / Cargo.toml
Created August 12, 2022 15:44
Cargo dependencies for all our projects
[package]
name = "simple_neural_networks"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
tch = "0.8.0"
mnist = {version = "0.5.0", features = ["download"]}
@Steboss89
Steboss89 / linear2.rs
Created August 4, 2022 17:14
Introduce lifetime and Borrow<super>
pub fn linear<'a, T: Borrow<super::Path<'a>>>(
vs: T,
in_dim: i64,
out_dim: i64,
c: LinearConfig,
) -> Linear
@Steboss89
Steboss89 / linear1.rs
Created August 3, 2022 20:29
Start defining the default for a Linear NN
pub struct LinearConfig {
pub ws_init: super::Init,
pub bs_init: Option<super::Init>,
pub bias: bool,
}
impl Default for LinearConfig {
fn default() -> Self {
LinearConfig { ws_init: super::Init::KaimingUniform, bs_init: None, bias: true }
}
@Steboss89
Steboss89 / implinit.rs
Created July 27, 2022 14:20
reinitialised an existing tensor
impl Init {
/// Re-initializes an existing tensor with the specified initialization
pub fn set(self, tensor: &mut Tensor) {
match self {
Init::Const(cst) => {
let _ = tensor.fill_(cst);
}
@Steboss89
Steboss89 / init_match.rs
Created July 7, 2022 15:29
Initialisation, match case
match i {
Init::Const(cst) => {
// Optimize the case for which a single C++ code can be done.
if cst == 0. {
Tensor::f_zeros(dims, (Kind::Float, device))
} else if (cst - 1.).abs() <= std::f64::EPSILON {
Tensor::f_ones(dims, (Kind::Float, device))
} else {
Tensor::f_ones(dims, (Kind::Float, device)).map(|t| t * cst)
}
@Steboss89
Steboss89 / init_constant.rs
Created July 6, 2022 21:11
Constant method to initialize weights and biases
Init::Const(cst) => {
// Optimize the case for which a single C++ code can be done.
if cst == 0. {
Tensor::f_zeros(dims, (Kind::Float, device))
} else if (cst - 1.).abs() <= std::f64::EPSILON {
Tensor::f_ones(dims, (Kind::Float, device))
} else {
Tensor::f_ones(dims, (Kind::Float, device)).map(|t| t * cst)
}
}
@Steboss89
Steboss89 / transformer.py
Created June 2, 2022 21:36
measure text similarity with Roberta
!pip install sentence_transformers
from sentence_transformers import SentenceTransformer, util
# use roberta
model = SentenceTransformer('stsb-roberta-large')
def create_heatmap(similarity, cmap = "YlGnBu"):
df = pd.DataFrame(similarity)
df.columns = ['john', 'luke','mark', 'matt'] #ohn 0 mark 2 matt 3 luke 1
df.index = ['john', 'luke','mark', 'matt']
def create_heatmap(similarity, cmap = "YlGnBu"):
df = pd.DataFrame(similarity)
df.columns = ['john', 'luke','mark', 'matt'] #ohn 0 mark 2 matt 3 luke 1
df.index = ['john', 'luke','mark', 'matt']
fig, ax = plt.subplots(figsize=(5,5))
sns.heatmap(df, cmap=cmap)
from sklearn.metrics.pairwise import cosine_similarity
from sklearn.feature_extraction.text import TfidfVectorizer
import seaborn as sns
def create_heatmap(similarity, cmap = "YlGnBu"):
df = pd.DataFrame(similarity)
df.columns = ['john', 'luke','mark', 'matt'] #ohn 0 mark 2 matt 3 luke 1
df.index = ['john', 'luke','mark', 'matt']
fig, ax = plt.subplots(figsize=(5,5))
sns.heatmap(df, cmap=cmap)
from sklearn.metrics.pairwise import cosine_similarity
from sklearn.feature_extraction.text import TfidfVectorizer
import seaborn as sns