当前位置: 首页>>代码示例>>Python>>正文


Python PyTorch类代码示例

本文整理汇总了Python中PyTorch的典型用法代码示例。如果您正苦于以下问题:Python PyTorch类的具体用法?Python PyTorch怎么用?Python PyTorch使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了PyTorch类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_double_tensor

def test_double_tensor():
    PyTorch.manualSeed(123)
    LongTensor = PyTorch.LongTensor
    DoubleTensor = PyTorch.DoubleTensor
    LongStorage = PyTorch.LongStorage
    print('LongStorage', LongStorage)
    print('LongTensor', LongTensor)
    print('DoubleTensor', DoubleTensor)
    print('dir(G)', dir())
    print('test_double_tensor')
    a = PyTorch.DoubleTensor(3, 2)
    print('got double a')
    myeval('a.dims()')
    a.uniform()
    myeval('a')
    myexec('a[1][1] = 9')
    myeval('a')
    myeval('a.size()')
    myeval('a + 2')
    myexec('a.resize2d(3,3).fill(1)')
    myeval('a')
    myexec('size = LongStorage(2)')
    myexec('size[0] = 4')
    myexec('size[1] = 2')
    myexec('a.resize(size)')
    myeval('a')
    myeval('DoubleTensor(3,4).uniform()')
    myeval('DoubleTensor(3,4).bernoulli()')
    myeval('DoubleTensor(3,4).normal()')
    myeval('DoubleTensor(3,4).cauchy()')
    myeval('DoubleTensor(3,4).exponential()')
    myeval('DoubleTensor(3,4).logNormal()')
    myeval('DoubleTensor(3,4).geometric()')
开发者ID:hughperkins,项目名称:pytorch,代码行数:33,代码来源:testDoubleTensor.py

示例2: test_double_tensor

def test_double_tensor():
    PyTorch.manualSeed(123)
    LongTensor = PyTorch.LongTensor
    DoubleTensor = PyTorch.DoubleTensor
    print("dir(G)", dir())
    print("test_double_tensor")
    a = PyTorch.DoubleTensor(3, 2)
    print("got double a")
    myeval("a.dims()")
    a.uniform()
    myeval("a")
    myexec("a[1][1] = 9")
    myeval("a")
    myeval("a.size()")
    myeval("a + 2")
    myexec("a.resize2d(3,3).fill(1)")
    myeval("a")
    myexec("size = LongTensor(2)")
    myexec("size[0] = 4")
    myexec("size[1] = 2")
    myexec("a.resize(size)")
    myeval("a")
    myeval("DoubleTensor(3,4).uniform()")
    myeval("DoubleTensor(3,4).bernoulli()")
    myeval("DoubleTensor(3,4).normal()")
    myeval("DoubleTensor(3,4).cauchy()")
    myeval("DoubleTensor(3,4).exponential()")
    myeval("DoubleTensor(3,4).logNormal()")
    myeval("DoubleTensor(3,4).geometric()")
开发者ID:SalemAmeen,项目名称:pytorch,代码行数:29,代码来源:testDoubleTensor.py

示例3: test_nnx

def test_nnx():
    # net = nn.Minus()
    inputTensor = PyTorch.DoubleTensor(2, 3).uniform()
    print("inputTensor", inputTensor)

    PyTorch.require("nnx")
    net = nn.Minus()
    print(net.forward(inputTensor))
开发者ID:hughperkins,项目名称:pytorch,代码行数:8,代码来源:test_nnx.py

示例4: test_byte_tensor

def test_byte_tensor():
    PyTorch.manualSeed(123)
    print('test_byte_tensor')
    a = PyTorch.ByteTensor(3,2).geometric()
    myeval('a')
    myexec('a[1][1] = 9')
    myeval('a')
    myeval('a.size()')
    myeval('a + 2')
    myexec('a.resize2d(3,3).fill(1)')
    myeval('a')
开发者ID:3upperm2n,项目名称:pytorch,代码行数:11,代码来源:testByteTensor.py

示例5: test_float_tensor

def test_float_tensor():
    PyTorch.manualSeed(123)
    print('dir(G)', dir())
    print('test_float_tensor')
    a = PyTorch.FloatTensor(3, 2)
    print('got float a')
    myeval('a.dims()')
    a.uniform()
    myeval('a')
    myexec('a[1][1] = 9')
    myeval('a')
    myeval('a.size()')
开发者ID:hughperkins,项目名称:pytorch,代码行数:12,代码来源:testFloatTensor.py

示例6: pushSomething

def pushSomething(lua, something):
    if isinstance(something, int):
        lua.pushNumber(something)
        return

    if isinstance(something, float):
        lua.pushNumber(something)
        return

    if isinstance(something, str):
        lua.pushString(something)
        return

    if isinstance(something, dict):
        pushTable(lua, something)
        return

    if isinstance(something, list):
        pushArray(lua, something)
        return

    for pythonClass in pushFunctionByPythonClass:
        if isinstance(something, pythonClass):
            pushFunctionByPythonClass[pythonClass](something)
            return

    if type(something) in luaClassesReverse:
        pushObject(lua, something)
        return

    typestring = str(type(something))
    if typestring in ["<class 'numpy.ndarray'>", "<type 'numpy.ndarray'>"]:
      dtypestr = str(something.dtype)
      if dtypestr == 'float32':
        pushSomething(lua, PyTorch._asFloatTensor(something))
        return
      if dtypestr == 'float64':
        pushSomething(lua, PyTorch._asDoubleTensor(something))
        return
      if dtypestr == 'uint8':
        pushSomething(lua, PyTorch._asByteTensor(something))
        return
      raise Exception('pushing numpy array with elements of type ' + dtypestr + ' it not currently implemented')

    raise Exception('pushing type ' + str(type(something)) + ' not implemented, value ', something)
开发者ID:benglard,项目名称:pytorch,代码行数:45,代码来源:PyTorchAug.py

示例7: load_lua_class

def load_lua_class(lua_filename, lua_classname):
    module = lua_filename.replace('.lua', '')
    PyTorch.require(module)
    splitName = lua_classname.split('.')
    class LuaWrapper(PyTorchAug.LuaClass):
        def __init__(self, *args):
            _fromLua = False
            if len(args) >= 1:
                if args[0] == '__FROMLUA__':
                   _fromLua = True
                   args = args[1:]
#            print('LuaWrapper.__init__', lua_classname, 'fromLua', _fromLua, 'args', args)
            self.luaclass = lua_classname
            if not _fromLua:
                PyTorchAug.LuaClass.__init__(self, splitName, *args)
            else:
                self.__dict__['__objectId'] = PyTorchAug.getNextObjectId()
    renamedClass = PyTorchLua.renameClass(LuaWrapper, module, lua_classname)
    return renamedClass
开发者ID:3upperm2n,项目名称:pytorch,代码行数:19,代码来源:PyTorchHelpers.py

示例8: test_call_lua

def test_call_lua():
    TestCallLua = PyTorchHelpers.load_lua_class('test/test_call_lua.lua', 'TestCallLua')

    batchSize = 2
    numFrames = 4
    inSize = 3
    outSize = 3
    kernelSize = 3

    luabit = TestCallLua('green')
    print(luabit.getName())
    assert luabit.getName() == 'green'
    print('type(luabit)', type(luabit))
    assert str(type(luabit)) == '<class \'PyTorchLua.TestCallLua\'>'

    np.random.seed(123)
    inTensor = np.random.randn(batchSize, numFrames, inSize).astype('float32')
    luain = PyTorch.asFloatTensor(inTensor)

    luaout = luabit.getOut(luain, outSize, kernelSize)

    outTensor = luaout.asNumpyTensor()
    print('outTensor', outTensor)
    # I guess we just assume if we got to this point, with no exceptions, then thats a good thing...
    # lets add some new test...

    outTensor = luabit.addThree(luain).asNumpyTensor()
    assert isinstance(outTensor, np.ndarray)
    assert inTensor.shape == outTensor.shape
    assert np.abs((inTensor + 3) - outTensor).max() < 1e-4

    res = luabit.printTable({'color': 'red', 'weather': 'sunny', 'anumber': 10, 'afloat': 1.234}, 'mistletoe', {
        'row1': 'col1', 'meta': 'data'})
    print('res', res)
    assert res == {'foo': 'bar', 'result': 12.345, 'bear': 'happy'}

    # List and tuple support by conversion to dictionary
    reslist = luabit.modifyList([3.1415, r'~Python\omega', 42])
    restuple = luabit.modifyList((3.1415, r'~Python\omega', 42))
    assert len(reslist) == len(restuple) == 4
    assert list(reslist.keys()) == list(restuple.keys()) == [1, 2, 3, 4]
    assert reslist[1] == restuple[1]
    assert (reslist[1] - 3.1415) < 1e-7
    reslist.pop(1)
    restuple.pop(1)
    assert reslist == restuple == {2: r'~Python\omega', 3: 42, 4: 'Lorem Ipsum'}

    # Get an object created from scratch by Lua
    res = luabit.getList()
    assert res[1] == 3.1415
    res.pop(1)
    assert res == {2: 'Lua', 3: 123}
开发者ID:hughperkins,项目名称:pytorch,代码行数:52,代码来源:test_call_lua.py

示例9: test_save_load

def test_save_load():
    np.random.seed(123)
    a_np = np.random.randn(3, 2).astype(np.float32)
    a = PyTorch.asFloatTensor(a_np)
    print('a', a)

    filename = '/tmp/foo.t7'  # TODO: should use tempfile to get this
    PyTorchAug.save(filename, a)

    b = PyTorchAug.load(filename)
    print('type(b)', type(b))
    print('b', b)

    assert np.abs(a_np - b.asNumpyTensor()).max() < 1e-4
开发者ID:hughperkins,项目名称:pytorch,代码行数:14,代码来源:test_save_load.py

示例10: save

def save(filepath, target):
    lua = PyTorch.getGlobalState().getLua()

    topStart = lua.getTop()

    pushGlobal(lua, 'torch', 'saveobj')
    pushSomething(lua, filepath)
    pushSomething(lua, target)
    res = lua.pcall(2, 0, 1)
    if res != 0:
        errorMessage = popString(lua)
        raise Exception(errorMessage)

    topEnd = lua.getTop()
    assert topStart == topEnd
开发者ID:hughperkins,项目名称:pytorch,代码行数:15,代码来源:PyTorchAug.py

示例11: __init__

    def __init__(self, nameList, *args):
        lua = PyTorch.getGlobalState().getLua()
        self.__dict__['__objectId'] = getNextObjectId()
        topStart = lua.getTop()
        pushGlobalFromList(lua, nameList)
        for arg in args:
            pushSomething(lua, arg)
#        print('nameList', nameList)
#        print('args', args)
        res = lua.pcall(len(args), 1)
        if res != 0:
          errorMessage = popString(lua)
          raise Exception(errorMessage)
#        lua.call(len(args), 1)
        registerObject(lua, self)

        topEnd = lua.getTop()
        assert topStart == topEnd
开发者ID:benglard,项目名称:pytorch,代码行数:18,代码来源:PyTorchAug.py

示例12: load

def load(filepath):
    lua = PyTorch.getGlobalState().getLua()
    topStart = lua.getTop()

    pushGlobal(lua, 'torch', 'loadobj')
    pushSomething(lua, filepath)

    res = lua.pcall(1, 1, 1)
    if res != 0:
        errorMessage = popString(lua)
        raise Exception(errorMessage)

    res = popSomething(lua)

    topEnd = lua.getTop()
    assert topStart == topEnd

    return res
开发者ID:hughperkins,项目名称:pytorch,代码行数:18,代码来源:PyTorchAug.py

示例13: __init__

    def __init__(self, nameList, *args):
        lua = PyTorch.getGlobalState().getLua()
        self.__dict__['__objectId'] = getNextObjectId()
        topStart = lua.getTop()
        pushGlobalFromList(lua, nameList)
        for arg in args:
            if isinstance(arg, int):
                lua.pushNumber(arg)
            else:
                raise Exception('arg type ' + str(type(arg)) + ' not implemented')
        lua.call(len(args), 1)
        registerObject(lua, self)

#        nameList = nameList[:]
#        nameList.append('float')
#        pushGlobalFromList(lua, nameList)
#        pushObject(lua, self)
#        lua.call(1, 0)

        topEnd = lua.getTop()
        assert topStart == topEnd
开发者ID:dementrock,项目名称:pytorch,代码行数:21,代码来源:PyTorchAug.py

示例14: Luabit

import sys
import os
import PyTorch
import PyTorchHelpers
import numpy as np

Luabit = PyTorchHelpers.load_lua_class('luabit.lua', 'Luabit')

batchSize = 2
numFrames = 4
inSize = 3
outSize = 3
kernelSize = 3

luabit = Luabit('green')
print(luabit.getName())
print('type(luabit)', type(luabit))

inTensor = np.random.randn(batchSize, numFrames, inSize).astype('float32')
luain = PyTorch.asFloatTensor(inTensor)

luaout = luabit.getOut(luain, outSize, kernelSize)

outTensor = luaout.asNumpyTensor()
print('outTensor', outTensor)

res = luabit.printTable({'color': 'red', 'weather': 'sunny', 'anumber': 10, 'afloat': 1.234}, 'mistletoe', {
  'row1': 'col1', 'meta': 'data'})
print('res', res)

开发者ID:3upperm2n,项目名称:pytorch,代码行数:29,代码来源:pybit.py

示例15: test_pynn

def test_pynn():
    PyTorch.manualSeed(123)
    linear = Linear(3, 5)
    linear
    print('linear', linear)
    print('linear.weight', linear.weight)
    print('linear.output', linear.output)
    print('linear.gradInput', linear.gradInput)

    input = PyTorch.DoubleTensor(4, 3).uniform()
    print('input', input)
    output = linear.updateOutput(input)
    print('output', output)

    gradInput = linear.updateGradInput(input, output)
    print('gradInput', gradInput)

    criterion = ClassNLLCriterion()
    print('criterion', criterion)

    print('dir(linear)', dir(linear))

    mlp = Sequential()
    mlp.add(linear)

    output = mlp.forward(input)
    print('output', output)

    import sys
    sys.path.append('thirdparty/python-mnist')
    from mnist import MNIST
    import numpy
    import array

    numpy.random.seed(123)

    mlp = Sequential()
    linear = Linear(784, 10)
    mlp.add(linear)
    logSoftMax = LogSoftMax()
    mlp.add(logSoftMax)
    mlp

    criterion = ClassNLLCriterion()
    print('got criterion')

    learningRate = 0.0001

    mndata = MNIST('/norep/data/mnist')
    imagesList, labelsB = mndata.load_training()
    images = numpy.array(imagesList).astype(numpy.float64)
    #print('imagesArray', images.shape)

    #print(images[0].shape)

    labelsf = array.array('d', labelsB.tolist())
    imagesTensor = PyTorch.asDoubleTensor(images)

    #imagesTensor = PyTorch.FloatTensor(100,784)
    #labels = numpy.array(20,).astype(numpy.int32)
    #labelsTensor = PyTorch.FloatTensor(100).fill(1)
    #print('labels', labels)
    #print(imagesTensor.size())

    def printStorageAddr(name, tensor):
        print('printStorageAddr START')
        storage = tensor.storage()
        if storage is None:
            print(name, 'storage is None')
        else:
            print(name, 'storage is ', hex(storage.dataAddr()))
        print('printStorageAddr END')

    labelsTensor = PyTorch.asDoubleTensor(labelsf)
    labelsTensor += 1
    #print('calling size on imagestensor...')
    #print('   (called size)')

    desiredN = 128
    maxN = int(imagesTensor.size()[0])
    desiredN = min(maxN, desiredN)
    imagesTensor = imagesTensor.narrow(0, 0, desiredN)
    labelsTensor = labelsTensor.narrow(0, 0, desiredN)
    print('imagesTensor.size()', imagesTensor.size())
    print('labelsTensor.size()', labelsTensor.size())
    N = int(imagesTensor.size()[0])
    print('type(imagesTensor)', type(imagesTensor))

    print('start training...')
    for epoch in range(4):
        numRight = 0
        for n in range(N):
    #        print('n', n)
            input = imagesTensor[n]
            label = labelsTensor[n]
            labelTensor = PyTorch.DoubleTensor(1)
            labelTensor[0] = label
    #        print('label', label)
            output = mlp.forward(input)
            prediction = PyTorch.getDoublePrediction(output)
#.........这里部分代码省略.........
开发者ID:SalemAmeen,项目名称:pytorch,代码行数:101,代码来源:test_pynn.py


注:本文中的PyTorch类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。