Skip to content

Instantly share code, notes, and snippets.

View minhlab's full-sized avatar

Minh Le minhlab

  • FPT Software
  • Vietanm
View GitHub Profile
@minhlab
minhlab / ntensor.py
Last active August 29, 2015 14:10
neural tensor network
'''
Created on Nov 17, 2014
@author: Minh Ngoc Le
'''
from pylearn2.models.mlp import Layer
from pylearn2.space import IndexSpace, VectorSpace
from pylearn2.utils import sharedX, wraps
from theano import tensor as T
import theano
import numpy as np
floatX = 'float32'
num_blocks = 2
block_dims = 3
hidden_dims = 5
output_dims = 7
@minhlab
minhlab / results.txt
Last active November 5, 2015 16:02
I compare the performance of Theano and Torch by two scripts performing multi-classification based on categorical input. These kind of model is important for NLP, similar to Chen & Manning (2014). Surprisingly, Theano is much slower and less accurate than Torch.
Running Torch...
Last cost: 0.01029977761209
Time: 0
Using gpu device 0: GeForce GTX 980
Compiling function...
Running Theano...
Last cost: 3.135782
Time: 6.343242
@minhlab
minhlab / torch-index-collision.txt
Created December 9, 2015 09:25
We should avoid index x into itself since doing so risks collision.
th> i = torch.LongTensor(10)
[0.0001s]
th> i:random(10)
8
2
2
2
7
2
4
@minhlab
minhlab / maskedCopy.txt
Last active December 11, 2015 23:51
Use maskedCopy to select elements in a tensor
th> mask=torch.ByteTensor(10, 3):random(2):add(-1)
[0.0001s]
th> mask
1 0 1
0 1 1
1 1 1
1 1 0
0 1 1
1 1 0
1 0 1
@minhlab
minhlab / masked_softmax.txt
Created December 11, 2015 17:04
Training with masked matrix as input to softmax. Instead of setting inactive elements to zero, we need to set them to -inf.
th> x=torch.Tensor(2,3)
[0.0000s]
th> x:uniform()
0.2714 0.8744 0.5518
0.3207 0.5189 0.8901
[torch.DoubleTensor of size 2x3]
[0.0001s]
th> x[1][2] = -math.huge
[0.0000s]
local MaskedLogSoftMax, Parent = torch.class('nn.MaskedLogSoftMax', 'nn.Module')
function MaskedLogSoftMax:__init(masks, filler)
Parent.__init(self)
self.masks = masks
self.minvals = torch.Tensor()
self.mininds = torch.LongTensor()
self.temp1 = torch.Tensor()
self.temp2 = torch.Tensor()
@minhlab
minhlab / cube.java
Last active April 7, 2016 13:37
Multiplication is 100 times faster than Math.pow()
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
double a = Math.random();
@minhlab
minhlab / ecb_plus_stats.py
Created December 9, 2016 13:36
Print some statistics of ECB+ (Cybulska and Vossen, 2014)
import os
import re
count = 0
for root, dir_names, file_names in os.walk('ECB+'):
for fname in file_names:
if 'plus' in fname:
path = os.path.join(root, fname)
with open(path) as f:
content = f.read()
print list(m.group() for m in re.finditer('<token', content))
@minhlab
minhlab / ecb_stats.py
Created December 9, 2016 13:42
Perform some statistics on ECB given ECB+ directory which include the ECB files (Cybulska and Vossen, 2014)
import os
import re
count = 0
for root, dir_names, file_names in os.walk('ECB+'):
for fname in file_names:
if 'plus' not in fname:
path = os.path.join(root, fname)
with open(path) as f:
content = f.read()
print list(m.group() for m in re.finditer('<token', content))