Repository: NegatioN/OnlineMiningTripletLoss
Branch: master
Commit: 4c22eac01ee8
Files: 67
Total size: 135.4 KB
Directory structure:
gitextract_53kablsc/
├── .github/
│ └── workflows/
│ └── main.yml
├── .gitignore
├── CONTRIBUTING.md
├── LICENSE
├── Makefile
├── README.md
├── docs/
│ ├── .gitignore
│ ├── Gemfile
│ ├── _config.yml
│ ├── _data/
│ │ ├── alerts.yml
│ │ ├── definitions.yml
│ │ ├── glossary.yml
│ │ ├── sidebars/
│ │ │ └── home_sidebar.yml
│ │ ├── tags.yml
│ │ ├── terms.yml
│ │ └── topnav.yml
│ ├── _includes/
│ │ ├── archive.html
│ │ ├── callout.html
│ │ ├── footer.html
│ │ ├── google_analytics.html
│ │ ├── head.html
│ │ ├── head_print.html
│ │ ├── image.html
│ │ ├── important.html
│ │ ├── initialize_shuffle.html
│ │ ├── inline_image.html
│ │ ├── links.html
│ │ ├── note.html
│ │ ├── search_google_custom.html
│ │ ├── search_simple_jekyll.html
│ │ ├── sidebar.html
│ │ ├── tip.html
│ │ ├── toc.html
│ │ ├── topnav.html
│ │ └── warning.html
│ ├── _layouts/
│ │ ├── default.html
│ │ ├── default_print.html
│ │ ├── none.html
│ │ ├── page.html
│ │ └── page_print.html
│ ├── css/
│ │ ├── boxshadowproperties.css
│ │ ├── customstyles.css
│ │ ├── fonts/
│ │ │ └── FontAwesome.otf
│ │ ├── modern-business.css
│ │ ├── printstyles.css
│ │ ├── syntax.css
│ │ ├── theme-blue.css
│ │ └── theme-green.css
│ ├── feed.xml
│ ├── fonts/
│ │ └── FontAwesome.otf
│ ├── index.html
│ ├── js/
│ │ ├── customscripts.js
│ │ ├── jekyll-search.js
│ │ └── toc.js
│ ├── licenses/
│ │ ├── LICENSE
│ │ └── LICENSE-BSD-NAVGOCO.txt
│ ├── sidebar.json
│ ├── sitemap.xml
│ ├── tooltips.json
│ └── triplet_loss.html
├── index.ipynb
├── online_triplet_loss/
│ ├── __init__.py
│ ├── _nbdev.py
│ └── losses.py
├── settings.ini
├── setup.py
└── triplet_loss.ipynb
================================================
FILE CONTENTS
================================================
================================================
FILE: .github/workflows/main.yml
================================================
name: CI
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/setup-python@v1
with:
python-version: '3.6'
architecture: 'x64'
- name: Install the library
run: |
pip install nbdev jupyter
pip install -e .
- name: Read all notebooks
run: |
nbdev_read_nbs
- name: Check if all notebooks are cleaned
run: |
echo "Check we are starting with clean git checkout"
if [ -n "$(git status -uno -s)" ]; then echo "git status is not clean"; false; fi
echo "Trying to strip out notebooks"
nbdev_clean_nbs
echo "Check that strip out was unnecessary"
git status -s # display the status to see which nbs need cleaning up
if [ -n "$(git status -uno -s)" ]; then echo -e "!!! Detected unstripped out notebooks\n!!!Remember to run nbdev_install_git_hooks"; false; fi
- name: Check if there is no diff library/notebooks
run: |
if [ -n "$(nbdev_diff_nbs)" ]; then echo -e "!!! Detected difference between the notebooks and the library"; false; fi
- name: Run tests
run: |
nbdev_test_nbs
================================================
FILE: .gitignore
================================================
.last_checked
.idea/
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
.gitconfig
.gitattributes
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
target/
# Jupyter Notebook
.ipynb_checkpoints
# pyenv
.python-version
# celery beat schedule file
celerybeat-schedule
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
================================================
FILE: CONTRIBUTING.md
================================================
# How to contribute
## How to get started
Before anything else, please install the git hooks that run automatic scripts during each commit and merge to strip the notebooks of superfluous metadata (and avoid merge conflicts). After cloning the repository, run the following command inside it:
```
nbdev_install_git_hooks
```
## Did you find a bug?
* Ensure the bug was not already reported by searching on GitHub under Issues.
* If you're unable to find an open issue addressing the problem, open a new one. Be sure to include a title and clear description, as much relevant information as possible, and a code sample or an executable test case demonstrating the expected behavior that is not occurring.
* Be sure to add the complete error messages.
#### Did you write a patch that fixes a bug?
* Open a new GitHub pull request with the patch.
* Ensure that your PR includes a test that fails without your patch, and pass with it.
* Ensure the PR description clearly describes the problem and solution. Include the relevant issue number if applicable.
## PR submission guidelines
* Keep each PR focused. While it's more convenient, do not combine several unrelated fixes together. Create as many branches as needing to keep each PR focused.
* Do not mix style changes/fixes with "functional" changes. It's very difficult to review such PRs and it most likely get rejected.
* Do not add/remove vertical whitespace. Preserve the original style of the file you edit as much as you can.
* Do not turn an already submitted PR into your development playground. If after you submitted PR, you discovered that more work is needed - close the PR, do the required work and then submit a new PR. Otherwise each of your commits requires attention from maintainers of the project.
* If, however, you submitted a PR and received a request for changes, you should proceed with commits inside that PR, so that the maintainer can see the incremental fixes and won't need to review the whole PR again. In the exception case where you realize it'll take many many commits to complete the requests, then it's probably best to close the PR, do the work and then submit it again. Use common sense where you'd choose one way over another.
## Do you want to contribute to the documentation?
* Docs are automatically created from the notebooks in the nbs folder.
================================================
FILE: LICENSE
================================================
MIT License
Copyright (c) [2019] [Joakim Rishaug]
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
================================================
FILE: Makefile
================================================
SRC = $(wildcard lib/*.ipynb)
all: "online_triplet_loss" docs
"online_triplet_loss": $(SRC)
nbdev_build_lib
touch "online_triplet_loss"
docs_serve: docs
cd docs && bundle exec jekyll serve
docs: $(SRC)
nbdev_build_docs
touch docs
test:
nbdev_test_nbs
release: pypi
nbdev_bump_version
pypi: dist
twine upload --repository pypi dist/*
dist: clean
python setup.py sdist bdist_wheel
clean:
rm -rf dist
================================================
FILE: README.md
================================================
# online_triplet_loss
> PyTorch conversion of the excellent post on the same topic in Tensorflow. Simply an implementation of a triple loss with online mining of candidate triplets used in semi-supervised learning.
## Install
`pip install online_triplet_loss`
Then import with:
`from online_triplet_loss.losses import *`
PS: Requires Pytorch version 1.1.0 or above to use.
## How to use
In these examples I use a really large margin, since the embedding space is so small. A more realistic margins seems to be between `0.1 and 2.0`
```
from torch import nn
import torch
model = nn.Embedding(10, 10)
```
```
#from online_triplet_loss.losses import *
labels = torch.randint(high=10, size=(5,)) # our five labels
embeddings = model(labels)
print('Labels:', labels)
print('Embeddings:', embeddings)
loss = batch_hard_triplet_loss(labels, embeddings, margin=100)
print('Loss:', loss)
loss.backward()
```
Labels: tensor([6, 1, 3, 6, 6])
Embeddings: tensor([[-1.1335, 0.3364, -3.0174, -0.8732, -0.9301, 1.3619, 0.3746, 0.0457,
0.0180, -0.4500],
[ 1.0757, -0.8420, -0.7630, -0.0746, 1.1545, 0.4017, 0.5587, 1.7947,
0.1992, -2.2288],
[ 0.2646, 1.2383, 0.1949, 0.5743, -0.8460, -0.9929, -2.0350, 0.2095,
0.2129, -0.4855],
[-1.1335, 0.3364, -3.0174, -0.8732, -0.9301, 1.3619, 0.3746, 0.0457,
0.0180, -0.4500],
[-1.1335, 0.3364, -3.0174, -0.8732, -0.9301, 1.3619, 0.3746, 0.0457,
0.0180, -0.4500]], grad_fn=)
Loss: tensor(95.1271, grad_fn=)
```
#from online_triplet_loss.losses import *
embeddings = model(labels)
print('Labels:', labels)
print('Embeddings:', embeddings)
loss, fraction_pos = batch_all_triplet_loss(labels, embeddings, squared=False, margin=100)
print('Loss:', loss)
loss.backward()
```
Labels: tensor([6, 1, 3, 6, 6])
Embeddings: tensor([[-1.1335, 0.3364, -3.0174, -0.8732, -0.9301, 1.3619, 0.3746, 0.0457,
0.0180, -0.4500],
[ 1.0757, -0.8420, -0.7630, -0.0746, 1.1545, 0.4017, 0.5587, 1.7947,
0.1992, -2.2288],
[ 0.2646, 1.2383, 0.1949, 0.5743, -0.8460, -0.9929, -2.0350, 0.2095,
0.2129, -0.4855],
[-1.1335, 0.3364, -3.0174, -0.8732, -0.9301, 1.3619, 0.3746, 0.0457,
0.0180, -0.4500],
[-1.1335, 0.3364, -3.0174, -0.8732, -0.9301, 1.3619, 0.3746, 0.0457,
0.0180, -0.4500]], grad_fn=)
tensor(94.9947, grad_fn=) tensor(1.)
Loss: tensor(94.9947, grad_fn=)
## References
* [Triplet Loss and Online Triplet Mining in Tensorflow](https://github.com/omoindrot/tensorflow-triplet-loss)
* [Facenet paper](https://arxiv.org/abs/1503.03832)
* [adambielski's nice implementation](https://github.com/adambielski/siamese-triplet) (unfortunately context switches between CPU / GPU)
================================================
FILE: docs/.gitignore
================================================
_site/
================================================
FILE: docs/Gemfile
================================================
source "https://rubygems.org"
gem 'github-pages', group: :jekyll_plugins
# Added at 2019-11-25 10:11:40 -0800 by jhoward:
gem "jekyll", "~> 3.7"
================================================
FILE: docs/_config.yml
================================================
repository: "NegatioN/OnlineMiningTripletLoss"
output: web
topnav_title: "online_triplet_loss"
site_title: "online_triplet_loss"
company_name: "Joakim Rishaug"
description: "Online mining triplet losses for Pytorch"
# Set to false to disable KaTeX math
use_math: true
# Add Google analytics id if you have one and want to use it here
google_analytics:
# See http://nbdev.fast.ai/search for help with adding Search
google_search:
host: 127.0.0.1
# the preview server used. Leave as is.
port: 4000
# the port where the preview is rendered.
exclude:
- .idea/
- .gitignore
- vendor
exclude: [vendor]
highlighter: rouge
markdown: kramdown
kramdown:
input: GFM
auto_ids: true
hard_wrap: false
syntax_highlighter: rouge
collections:
tooltips:
output: false
defaults:
-
scope:
path: ""
type: "pages"
values:
layout: "page"
comments: true
search: true
sidebar: home_sidebar
topnav: topnav
-
scope:
path: ""
type: "tooltips"
values:
layout: "page"
comments: true
search: true
tooltip: true
sidebars:
- home_sidebar
permalink: pretty
theme: jekyll-theme-cayman
baseurl: /OnlineMiningTripletLoss/
================================================
FILE: docs/_data/alerts.yml
================================================
tip: '
================================================
FILE: docs/js/customscripts.js
================================================
$('#mysidebar').height($(".nav").height());
$( document ).ready(function() {
//this script says, if the height of the viewport is greater than 800px, then insert affix class, which makes the nav bar float in a fixed
// position as your scroll. if you have a lot of nav items, this height may not work for you.
var h = $(window).height();
//console.log (h);
if (h > 800) {
$( "#mysidebar" ).attr("class", "nav affix");
}
// activate tooltips. although this is a bootstrap js function, it must be activated this way in your theme.
$('[data-toggle="tooltip"]').tooltip({
placement : 'top'
});
/**
* AnchorJS
*/
anchors.add('h2,h3,h4,h5');
});
// needed for nav tabs on pages. See Formatting > Nav tabs for more details.
// script from http://stackoverflow.com/questions/10523433/how-do-i-keep-the-current-tab-active-with-twitter-bootstrap-after-a-page-reload
$(function() {
var json, tabsState;
$('a[data-toggle="pill"], a[data-toggle="tab"]').on('shown.bs.tab', function(e) {
var href, json, parentId, tabsState;
tabsState = localStorage.getItem("tabs-state");
json = JSON.parse(tabsState || "{}");
parentId = $(e.target).parents("ul.nav.nav-pills, ul.nav.nav-tabs").attr("id");
href = $(e.target).attr('href');
json[parentId] = href;
return localStorage.setItem("tabs-state", JSON.stringify(json));
});
tabsState = localStorage.getItem("tabs-state");
json = JSON.parse(tabsState || "{}");
$.each(json, function(containerId, href) {
return $("#" + containerId + " a[href=" + href + "]").tab('show');
});
$("ul.nav.nav-pills, ul.nav.nav-tabs").each(function() {
var $this = $(this);
if (!json[$this.attr("id")]) {
return $this.find("a[data-toggle=tab]:first, a[data-toggle=pill]:first").tab("show");
}
});
});
================================================
FILE: docs/js/jekyll-search.js
================================================
!function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a="function"==typeof require&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}for(var i="function"==typeof require&&require,o=0;o=0}var self=this;self.matches=function(string,crit){return"string"!=typeof string?!1:(string=string.trim(),doMatch(string,crit))}}module.exports=new LiteralSearchStrategy},{}],4:[function(require,module){module.exports=function(){function findMatches(store,crit,strategy){for(var data=store.get(),i=0;i{title}',noResultsText:"No results found",limit:10,fuzzy:!1};self.init=function(_opt){validateOptions(_opt),assignOptions(_opt),isJSON(opt.dataSource)?initWithJSON(opt.dataSource):initWithURL(opt.dataSource)}}var Searcher=require("./Searcher"),Templater=require("./Templater"),Store=require("./Store"),JSONLoader=require("./JSONLoader"),searcher=new Searcher,templater=new Templater,store=new Store,jsonLoader=new JSONLoader;window.SimpleJekyllSearch=new SimpleJekyllSearch}(window,document)},{"./JSONLoader":1,"./Searcher":4,"./Store":5,"./Templater":6}]},{},[7]);
================================================
FILE: docs/js/toc.js
================================================
// https://github.com/ghiculescu/jekyll-table-of-contents
// this library modified by fastai to:
// - update the location.href with the correct anchor when a toc item is clicked on
(function($){
$.fn.toc = function(options) {
var defaults = {
noBackToTopLinks: false,
title: '',
minimumHeaders: 3,
headers: 'h1, h2, h3, h4',
listType: 'ol', // values: [ol|ul]
showEffect: 'show', // values: [show|slideDown|fadeIn|none]
showSpeed: 'slow' // set to 0 to deactivate effect
},
settings = $.extend(defaults, options);
var headers = $(settings.headers).filter(function() {
// get all headers with an ID
var previousSiblingName = $(this).prev().attr( "name" );
if (!this.id && previousSiblingName) {
this.id = $(this).attr( "id", previousSiblingName.replace(/\./g, "-") );
}
return this.id;
}), output = $(this);
if (!headers.length || headers.length < settings.minimumHeaders || !output.length) {
return;
}
if (0 === settings.showSpeed) {
settings.showEffect = 'none';
}
var render = {
show: function() { output.hide().html(html).show(settings.showSpeed); },
slideDown: function() { output.hide().html(html).slideDown(settings.showSpeed); },
fadeIn: function() { output.hide().html(html).fadeIn(settings.showSpeed); },
none: function() { output.html(html); }
};
var get_level = function(ele) { return parseInt(ele.nodeName.replace("H", ""), 10); }
var highest_level = headers.map(function(_, ele) { return get_level(ele); }).get().sort()[0];
//var return_to_top = '';
// other nice icons that can be used instead: glyphicon-upload glyphicon-hand-up glyphicon-chevron-up glyphicon-menu-up glyphicon-triangle-top
var level = get_level(headers[0]),
this_level,
html = settings.title + " <"+settings.listType+">";
headers.on('click', function() {
if (!settings.noBackToTopLinks) {
var pos = $(window).scrollTop();
window.location.hash = this.id;
$(window).scrollTop(pos);
}
})
.addClass('clickable-header')
.each(function(_, header) {
base_url = window.location.href;
base_url = base_url.replace(/#.*$/, "");
this_level = get_level(header);
//if (!settings.noBackToTopLinks && this_level > 1) {
// $(header).addClass('top-level-header').before(return_to_top);
//}
txt = header.textContent.split('¶')[0].split(/\[(test|source)\]/)[0];
if (!txt) {return;}
if (this_level === level) // same level as before; same indenting
html += "
" + txt + "";
else if (this_level <= level){ // higher level than before; end parent ol
for(i = this_level; i < level; i++) {
html += "
"+settings.listType+">"
}
html += "
" + txt + "";
}
else if (this_level > level) { // lower level than before; expand the previous to contain a ol
for(i = this_level; i > level; i--) {
html += "<"+settings.listType+">"+((i-level == 2) ? "
" : "
")
}
html += "" + txt + "";
}
level = this_level; // update for the next one
});
html += ""+settings.listType+">";
if (!settings.noBackToTopLinks) {
$(document).on('click', '.back-to-top', function() {
$(window).scrollTop(0);
window.location.hash = '';
});
}
render[settings.showEffect]();
};
})(jQuery);
================================================
FILE: docs/licenses/LICENSE
================================================
/* This license pertains to the docs template, except for the Navgoco jQuery component. */
The MIT License (MIT)
Original theme: Copyright (c) 2016 Tom Johnson
Modifications: Copyright (c) 2017 onwards fast.ai, Inc
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
================================================
FILE: docs/licenses/LICENSE-BSD-NAVGOCO.txt
================================================
/* This license pertains to the Navgoco jQuery component used for the sidebar. */
Copyright (c) 2013, Christodoulos Tsoulloftas, http://www.komposta.net
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the nor the names of its
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
OF THE POSSIBILITY OF SUCH DAMAGE.
================================================
FILE: docs/sidebar.json
================================================
{
"online_triplet_loss": {
"Overview": "/",
"Losses": "/triplet_loss"
}
}
================================================
FILE: docs/sitemap.xml
================================================
---
layout: none
search: exclude
---
{% for post in site.posts %}
{% unless post.search == "exclude" %}
{{site.url}}{{post.url}}
{% endunless %}
{% endfor %}
{% for page in site.pages %}
{% unless page.search == "exclude" %}
{{site.url}}{{ page.url}}
{% endunless %}
{% endfor %}
================================================
FILE: docs/tooltips.json
================================================
---
layout: null
search: exclude
---
{
"entries":
[
{% for page in site.tooltips %}
{
"doc_id": "{{ page.doc_id }}",
"body": "{{ page.content | strip_newlines | replace: '\', '\\\\' | replace: '"', '\\"' }}"
} {% unless forloop.last %},{% endunless %}
{% endfor %}
]
}
================================================
FILE: docs/triplet_loss.html
================================================
---
title: Losses
keywords: fastai
sidebar: home_sidebar
summary: "Where all of the losses are situated."
description: "Where all of the losses are situated."
---
Build the triplet loss over a batch of embeddings.
For each anchor, we get the hardest positive and hardest negative to form a triplet.
Args:
labels: labels of the batch, of size (batch_size,)
embeddings: tensor of shape (batch_size, embed_dim)
margin: margin for triplet loss
squared: Boolean. If true, output is the pairwise squared euclidean distance matrix.
If false, output is the pairwise euclidean distance matrix.
Returns:
triplet_loss: scalar tensor containing the triplet loss
Build the triplet loss over a batch of embeddings.
We generate all the valid triplets and average the loss over the positive ones.
Args:
labels: labels of the batch, of size (batch_size,)
embeddings: tensor of shape (batch_size, embed_dim)
margin: margin for triplet loss
squared: Boolean. If true, output is the pairwise squared euclidean distance matrix.
If false, output is the pairwise euclidean distance matrix.
Returns:
triplet_loss: scalar tensor containing the triplet loss