Skip to content

Instantly share code, notes, and snippets.

@paduvi
paduvi / FlatCnnLayer.py
Last active May 10, 2024 16:59
Hierarchical Softmax CNN Classification
import torch
import torch.nn as nn
import torch.nn.init as init
dropout_prob = 0.5
class FlatCnnLayer(nn.Module):
def __init__(self, embedding_size, sequence_length, filter_sizes=[3, 4, 5], out_channels=128):
super(FlatCnnLayer, self).__init__()
@lmclupr
lmclupr / race-car-CV2-NN-network-TD0-15-possible-actions.ipynb
Created August 30, 2017 18:18
Python notebook for the Box2D Race car reinforce learning problem.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@bkj
bkj / pytorch_lifted_loss.py
Last active April 28, 2022 07:12
pytorch lifted loss
#!/usr/bin/env python
"""
pytorch_lifted_loss.py
"""
import torch
import torch.nn as nn
from torch.autograd import Variable
@chrischoy
chrischoy / smoothed_triplet_loss.py
Last active March 13, 2021 11:50
Smoothed triplet loss from Deep Metric Learning via Lifted Structured Feature Embedding, CVPR 2016
import tensorflow as tf
import numpy as np
def smoothed_metric_loss(input_tensor, name='smoothed_triplet_loss', margin=1):
'''
input_tensor: require a tensor with predefined dimensions (No None dimension)
Every two consecutive vectors must be a positive pair. There
should not be more than one pair from each class.
'''
with tf.variable_scope(name):
@cbaziotis
cbaziotis / AttentionWithContext.py
Last active April 25, 2022 14:37
Keras Layer that implements an Attention mechanism, with a context/query vector, for temporal data. Supports Masking. Follows the work of Yang et al. [https://www.cs.cmu.edu/~diyiy/docs/naacl16.pdf] "Hierarchical Attention Networks for Document Classification"
def dot_product(x, kernel):
"""
Wrapper for dot product operation, in order to be compatible with both
Theano and Tensorflow
Args:
x (): input
kernel (): weights
Returns:
"""
if K.backend() == 'tensorflow':
@lawliet89
lawliet89 / mergesort.cpp
Created November 12, 2012 22:54
C++ Mergesort with in place merging
// http://en.literateprograms.org/Merge_sort_(C_Plus_Plus)
#include <algorithm>
#include <iterator>
#include <iostream>
// Note: InputIterator must allow the minus operator -- i.e. RandomAccess
// The end element is AFTER the last element
// output refers to the iterator for the first output item
// You can mix and match different input and output iterators (i.e. vectors, arrays etc.)
template <typename InputIterator, typename OutputIterator> void mergesort(InputIterator start, InputIterator end, OutputIterator output){
@bwhite
bwhite / rank_metrics.py
Created September 15, 2012 03:23
Ranking Metrics
"""Information Retrieval metrics
Useful Resources:
http://www.cs.utexas.edu/~mooney/ir-course/slides/Evaluation.ppt
http://www.nii.ac.jp/TechReports/05-014E.pdf
http://www.stanford.edu/class/cs276/handouts/EvaluationNew-handout-6-per.pdf
http://hal.archives-ouvertes.fr/docs/00/72/67/60/PDF/07-busa-fekete.pdf
Learning to Rank for Information Retrieval (Tie-Yan Liu)
"""
import numpy as np