Skip to content

Instantly share code, notes, and snippets.

View andreaskoepf's full-sized avatar

Andreas Köpf andreaskoepf

View GitHub Profile
using Microsoft.Extensions.DependencyModel;
using System;
using System.Linq;
using System.Runtime.Loader;
using System.Reflection;
using System.Collections.Generic;
namespace ConsoleApp1
{
class Program
@andreaskoepf
andreaskoepf / torch_chunk_alternative.lua
Last active April 17, 2016 14:33
Alternative implementation of torch.chunk() that always returns exactly nChunks.
--[[
torch.chunk2()
returns a table with nChunk entries, even in the case that the tensor has < nChunk entries in the specified dimension.
Behaviour of the originial torch.chunk() function:
th> torch.rand(11):chunk(5)
{
1 : DoubleTensor - size: 3
2 : DoubleTensor - size: 3
function YUV422toYUV(input, w, h)
local yuv_result = torch.ByteTensor(3, h, w)
local x = input:view(h, w, 2)
yuv_result[1] = x[{{},{},1}] -- copy Y part
local u = yuv_result[2]
local v = yuv_result[3]
local uv = x[{{},{},2}]:reshape(h,w/2,2)
local u_ = uv[{{},{},1}]
local v_ = uv[{{},{},2}]
u:view(h,w/2, 2)[{{},{},1}] = u_
local function pca(X)
-- PCA -------------------------------------------------------------------------
-- X is m x n
local mean = torch.mean(X, 1) -- 1 x n
local m = X:size(1)
local Xm = X - torch.ones(m, 1) * mean
Xm:div(math.sqrt(m - 1))
local v,s,_ = torch.svd(Xm:t())
s:cmul(s) -- n
require 'cunn'
--test cuda & non-cuda version
function testVolumetricFullConvolution()
local input = torch.rand(1,2,10,10,10) * 2 - 1
local a = nn.VolumetricFullConvolution(2,3, 3,3,3, 1,1,1)
local b = nn.VolumetricFullConvolution(2,3, 3,3,3, 1,1,1)
b:cuda()
b.weight = a.weight:cuda()
@andreaskoepf
andreaskoepf / callbenchmark.lua
Created December 12, 2015 20:46
Performance comparison between tensor type string lookup and metatable-function selector...
x = torch.DoubleTensor()
y = torch.FloatTensor()
function testfunc(a,b)
return a + b
end
f = {}
f[x:type()] = {}
f[y:type()] = {}
@andreaskoepf
andreaskoepf / SpatialUnpooling.lua
Last active December 3, 2015 08:25
A simple lena-denoising auto-encoder SpatialUnpooling test.
local SpatialUnpooling, parent = torch.class('nn.SpatialUnpooling', 'nn.Module')
function SpatialUnpooling:__init(kW, kH, dW, dH, padW, padH)
parent.__init(self)
self.dW = dW or kW
self.dH = dH or kH
self.padW = padW or 0
self.padH = padH or 0
self.indices = torch.LongTensor()
self._indexTensor = torch.LongTensor()
local SpatialUnpooling, parent = torch.class('nn.SpatialUnpooling', 'nn.Module')
function SpatialUnpooling:__init(kW, kH, dW, dH, padW, padH)
parent.__init(self)
self.dW = dW or kW
self.dH = dH or kH
self.padW = padW or 0
self.padH = padH or 0
self.indices = torch.LongTensor()
self._indexTensor = torch.LongTensor()
@andreaskoepf
andreaskoepf / 2d_conv_test.lua
Last active December 24, 2015 20:38
Shows that forwad and backward operations of SpatialConvolution and SpatialFullConvolution are swapped...
require 'nn'
x = torch.rand(1,5,5)
a = nn.SpatialConvolution(1,1,3,3)
a.bias:zero()
ay1 =torch.xcorr2(x,a.weight,'V')
ay2 = a:forward(x)
b = nn.SpatialFullConvolution(1,1,3,3)
b.bias:zero()
@andreaskoepf
andreaskoepf / torch_obj_versioning.lua
Last active November 27, 2015 23:03
Demo for custom 'read' function and versioning of torch objects.
--
local Foo, parent = torch.class('Foo')
Foo.__version = 1
function Foo:__init()
parent.__init(self)
end
-- serialize 'old' object
old = Foo.new()