Giter VIP home page Giter VIP logo

fml's Introduction

FML (Francis' Machine-Learnin' Library)

This repository is a collection of PyTorch tools for machine learning. Currently it includes

  • A numerically stable implementation of the Sinkhorn Algorithm for point sets in any dimension
  • A vectorized implementation of the Chamfer Distance between point sets in any dimension

Installation Instructions

With conda (recommended)

On Linux, Simply run:

conda install -c conda-forge fml

On Windows and Mac, you may need to add the PyTorch Channel before installing:

conda config --add channels pytorch
conda install -c conda-forge fml

With pip (not recommended)

Simply run:

pip install git+https://github.com/fwilliams/fml

Library Structure

The structure of the library is similar to PyTorch. There is a fml.functional module which includes a functional interface for utilities and an fml.nn which includes PyTorch module implementations of utilities.

Examples

Computing the loss between two evenly weighted point sets

import torch
from fml.nn import SinkhornLoss

minibatch_size = 3
set_size = 10
point_dim = 4

# Create two minibatches of point sets where each batch item set_a[k, :, :] is a set of `set_size` points
set_a = torch.rand([minibatch_size, set_size, point_dim])
set_b = torch.rand([minibatch_size, set_size, point_dim])

# Create a loss function module with default parameters. See the class documentation for optional parameters.
loss_fun = SinkhornLoss()

# Compute the loss between each pair of sets in the minibatch
# loss is a tensor with [minibatch_size] elements which can be backpropagated through
loss = loss_fun(set_a, set_b)

Computing the loss between two non evenly weighted point sets

import torch
from fml.nn import SinkhornLoss

minibatch_size = 3
set_size = 10
point_dim = 4

# Create two minibatches of point sets where each batch item set_a[k, :, :] is a set of `set_size` points
set_a = torch.rand([minibatch_size, set_size, point_dim])
set_b = torch.rand([minibatch_size, set_size, point_dim])

# Generate weights which sum to 1 for each set
# Note that zero weights are the same as not including a set element
weights_a = torch.rand([minibatch_size, set_size])
weights_a /= torch.sum(weights_a, axis=1) 

weights_b = torch.rand([minibatch_size, set_size])
weights_b /= torch.sum(weights_b, axis=1) 

# Create a loss function module with default parameters. See the class documentation for optional parameters.
loss_fun = SinkhornLoss()

# Compute the loss between each pair of sets in the minibatch
# loss is a tensor with [minibatch_size] elements which can be backpropagated through
loss = loss_fun(set_a, set_b)

Computing the Chamfer Distance between point sets

import torch
from fml.nn import ChamferLoss

minibatch_size = 3
set_size = 10
point_dim = 4

# Create two minibatches of point sets where each batch item set_a[k, :, :] is a set of `set_size` points
set_a = torch.rand([minibatch_size, set_size, point_dim])
set_b = torch.rand([minibatch_size, set_size, point_dim])

# Create a loss function module.
loss_fun = ChamferLoss()

# Compute the loss between each pair of sets in the minibatch
# loss is a tensor with [minibatch_size] elements which can be backpropagated through
loss = loss_fun(set_a, set_b)

Computing pairwise distances between point sets

import torch
from fml.functional import pairwise_distances

minibatch_size = 3
set_size = 10
point_dim = 4

# Create two minibatches of point sets where each batch item set_a[k, :, :] is a set of `set_size` points
set_a = torch.rand([minibatch_size, set_size, point_dim])
set_b = torch.rand([minibatch_size, set_size, point_dim])

# Compute the pairwise distances between each pair of sets in the minibatch
# distances is a tensor of shape [minibatch_size, set_size, set_size] where each
# disances[k, i, j] = ||set_a[k, i] - set_b[k, j]||^2
distances = pairwise_distances(set_a, set_b)

fml's People

Contributors

fwilliams avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

fml's Issues

Sinkhorn algorithm questions

hi @fwilliams nice lib ! :)

I have a couple of questions about the sinkhorn algorithm you have implemented:

1- Do you have any paper reference about the original algorithm?
2- The a, b dirac weights shouldn't be 1/num_samples ? Instead in your examples you initialize to ones, and in practice I have found that with other values the algorithm doesn't converge properly.

Thanks,

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.