Skip to content

Instantly share code, notes, and snippets.

@blgnksy
Last active October 21, 2020 11:36
Show Gist options
  • Save blgnksy/e666b720990508d73c00d9eec1ee7269 to your computer and use it in GitHub Desktop.
Save blgnksy/e666b720990508d73c00d9eec1ee7269 to your computer and use it in GitHub Desktop.
Machine/Deep Learning

Torch

Describe Torch Tensors

def describe(x):     
  print("Type: {}".format(x.type()))     
  print("Shape/size: {}".format(x.shape))     
  print("Values: \n{}".format(x))

GPU vs CPU Models

C++

torch::DeviceType device_type;
if (torch::cuda::is_available()) {
    std::cout << "Cuda available, running on GPU" << "\n";
    device_type = torch::kCUDA;
} else {
    std::cout << "Cuda NOT available, running on CPU" << "\n";
    device_type = torch::kCPU;
}
torch::Device device(device_type);
module->to(torch::Device(device));

Python

if torch.cuda.is_available(): 
 dev = "cuda:0" 
else: 
 dev = "cpu" 
device = torch.device(dev)
model.to(device)

NLP

N-gram

def n_grams(textn):     
   '''     takes tokens or text, returns a list of ngrams     '''
   return [text[i:i+nfor i in range(len(text)-n+1)]

Tokenization

Sentence Tokenization

from nltk.tokenize import sent_tokenize

data = "Sentence one. Sentence two."
sent_tokenize(data)

Word Tokenization

from nltk.tokenize import word_tokenize

data = "word one two three, four."
word_tokenize(data)

Lemmatization

  • Add lemma for Turkish
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment