Skip to content

Instantly share code, notes, and snippets.

@0xnurl
0xnurl / non_null_accuracy.py
Created April 24, 2017 11:51
Keras metric for non-null targets accuracy, mainly for Named Entity Recognition models
def non_null_label_accuracy(y_true, y_pred):
"""Calculate accuracy excluding targets that are the null label (at index 0).
Useful when the null target is over-represented in the data, like in Named Entity Recognition tasks.
typical y shape: (batch_size, sentence_length, num_labels)
"""
y_true_argmax = K.argmax(y_true, -1) # ==> (batch_size, sentence_length, 1)
y_pred_argmax = K.argmax(y_pred, -1) # ==> (batch_size, sentence_length, 1)
@0xnurl
0xnurl / openfst_python_on_macos.md
Last active October 23, 2021 17:35
Installing OpenFST Native Python Extension on MacOS

Installing OpenFst Native Python Extension on MacOS

Starting from version 1.5, OpenFst has offered a native Python module, making the use of external wrappers like PyFst unnecessary. This has been greatly helpful since PyFst doesn't support Python 3.

1. Install OpenFst

@0xnurl
0xnurl / iterate_grid_combinations.py
Last active January 11, 2023 11:44
Configuration dictionary grid search iterator with support for nested dictionaries
def iterate_grid_combinations(
grid: dict[str : Union[Iterable[Any], dict]]
) -> Iterator[dict[str, Any]]:
# language=rst
"""
:param grid: A dictionary specifying arguments and their values to iterate over in a grid-search way. Values can be nested dictionaries of same structure.
:return: A (lazy) generator iterating over all value combinations, including nested dicts.
Example:
def get_free_gpu():
max_free = 0
max_idx = 0
rows = (
subprocess.check_output(
["nvidia-smi", "--format=csv", "--query-gpu=memory.free"]
)
.decode("utf-8")
.split("\n")
@0xnurl
0xnurl / Dockerfile
Created March 14, 2018 13:27
Compile OpenFST Python Extension for Python 3
FROM ubuntu:16.04
# Use bash (Ubuntu default is dash)
RUN rm /bin/sh && ln -s /bin/bash /bin/sh
# Ubuntu packages
RUN apt-get update && apt-get install -y ssh gcc=4:5.3.1-1ubuntu1 g++=4:5.3.1-1ubuntu1 make vim zlib1g-dev libbz2-dev libssl-dev python-dev man libreadline-dev build-essential libreadline-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev python3 python3-dev
# Install Python 3.6 from source for optimizations
RUN cd /tmp && wget https://www.python.org/ftp/python/3.6.0/Python-3.6.0.tgz && tar xzvf Python-3.6.0.tgz && cd Python-3.6.0 && ./configure && make && make install && cd /tmp && wget https://bootstrap.pypa.io/get-pip.py && python3 get-pip.py