Skip to content

Instantly share code, notes, and snippets.

@hughperkins
Created September 21, 2016 12:10
Show Gist options
  • Select an option

  • Save hughperkins/fdf8e27daec983e69767ef6bbaf1db9f to your computer and use it in GitHub Desktop.

Select an option

Save hughperkins/fdf8e27daec983e69767ef6bbaf1db9f to your computer and use it in GitHub Desktop.
from __future__ import print_function
import numpy as np
import PyTorch
import PyTorchHelpers
TorchClass = PyTorchHelpers.load_lua_class('TorchClass.lua', 'TorchClass')
cl = TorchClass()
v = list(range(10))
npv = np.asarray(v, dtype=np.float32)
cl.set(npv)
# cl.write()
npv2 = npv * 2
cl.set(npv2)
# cl.write()
cl.set(npv * 3)
# cl.write()
print('')
print('before allocating a simple array of zeros: cl.v\n', cl.v)
print(np.zeros(10, dtype=np.float32))
print('')
print('after: cl.v\n', cl.v)
torch.setdefaulttensortype('torch.FloatTensor')
local TorchClass = torch.class('TorchClass')
function TorchClass:__init()
print("<TorchClass> initialization")
end
function TorchClass:write()
if self.v then
print("<TorchClass> This is the value:")
print(self.v)
else
print("<TorchClass> Value not defined")
end
end
function TorchClass:set(vin)
self.v = vin
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment