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


Python PyTorch.asFloatTensor方法代码示例

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


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

示例1: test_call_lua

# 需要导入模块: import PyTorch [as 别名]
# 或者: from PyTorch import asFloatTensor [as 别名]
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,代码行数:54,代码来源:test_call_lua.py

示例2: test_save_load

# 需要导入模块: import PyTorch [as 别名]
# 或者: from PyTorch import asFloatTensor [as 别名]
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,代码行数:16,代码来源:test_save_load.py

示例3: Luabit

# 需要导入模块: import PyTorch [as 别名]
# 或者: from PyTorch import asFloatTensor [as 别名]
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,代码行数:31,代码来源:pybit.py

示例4: test_pytorchFloat

# 需要导入模块: import PyTorch [as 别名]
# 或者: from PyTorch import asFloatTensor [as 别名]
def test_pytorchFloat():
    PyTorch.manualSeed(123)
    numpy.random.seed(123)

    FloatTensor = PyTorch.FloatTensor

    
    A = numpy.random.rand(6).reshape(3,2).astype(numpy.float32)
    B = numpy.random.rand(8).reshape(2,4).astype(numpy.float32)

    C = A.dot(B)
    print('C', C)

    print('calling .asTensor...')
    tensorA = PyTorch.asFloatTensor(A)
    tensorB = PyTorch.asFloatTensor(B)
    print(' ... asTensor called')

    print('tensorA', tensorA)

    tensorA.set2d(1, 1, 56.4)
    tensorA.set2d(2, 0, 76.5)
    print('tensorA', tensorA)
    print('A', A)

    print('add 5 to tensorA')
    tensorA += 5
    print('tensorA', tensorA)
    print('A', A)

    print('add 7 to tensorA')
    tensorA2 = tensorA + 7
    print('tensorA2', tensorA2)
    print('tensorA', tensorA)

    tensorAB = tensorA * tensorB
    print('tensorAB', tensorAB)

    print('A.dot(B)', A.dot(B))

    print('tensorA[2]', tensorA[2])
    

    D = PyTorch.FloatTensor(5,3).fill(1)
    print('D', D)

    D[2][2] = 4
    print('D', D)

    D[3].fill(9)
    print('D', D)

    D.narrow(1,2,1).fill(0)
    print('D', D)

    
    print(PyTorch.FloatTensor(3,4).uniform())
    print(PyTorch.FloatTensor(3,4).normal())
    print(PyTorch.FloatTensor(3,4).cauchy())
    print(PyTorch.FloatTensor(3,4).exponential())
    print(PyTorch.FloatTensor(3,4).logNormal())
    
    print(PyTorch.FloatTensor(3,4).bernoulli())
    print(PyTorch.FloatTensor(3,4).geometric())
    print(PyTorch.FloatTensor(3,4).geometric())
    PyTorch.manualSeed(3)
    print(PyTorch.FloatTensor(3,4).geometric())
    PyTorch.manualSeed(3)
    print(PyTorch.FloatTensor(3,4).geometric())

    print(type(PyTorch.FloatTensor(2,3)))

    size = PyTorch.LongStorage(2)
    size[0] = 4
    size[1] = 3
    D.resize(size)
    print('D after resize:\n', D)

    print('resize1d', PyTorch.FloatTensor().resize1d(3).fill(1))
    print('resize2d', PyTorch.FloatTensor().resize2d(2, 3).fill(1))
    print('resize', PyTorch.FloatTensor().resize(size).fill(1))

    D = PyTorch.FloatTensor(size).geometric()

#    def myeval(expr):
#        print(expr, ':', eval(expr))

#    def myexec(expr):
#        print(expr)
#        exec(expr)

    myeval('FloatTensor(3,2).nElement()')
    myeval('FloatTensor().nElement()')
    myeval('FloatTensor(1).nElement()')

    A = FloatTensor(3,4).geometric(0.9)
    myeval('A')
    myexec('A += 3')
    myeval('A')
    myexec('A *= 3')
#.........这里部分代码省略.........
开发者ID:dementrock,项目名称:pytorch,代码行数:103,代码来源:test_pytorch.py

示例5: print

# 需要导入模块: import PyTorch [as 别名]
# 或者: from PyTorch import asFloatTensor [as 别名]
{%- set real = typedict['real'] -%}
def test_pytorch{{Real}}():
    PyTorch.manualSeed(123)
    numpy.random.seed(123)

    {{Real}}Tensor = PyTorch.{{Real}}Tensor

    {% if Real == 'Float' -%}
    A = numpy.random.rand(6).reshape(3, 2).astype(numpy.float32)
    B = numpy.random.rand(8).reshape(2, 4).astype(numpy.float32)

    C = A.dot(B)
    print('C', C)

    print('calling .asTensor...')
    tensorA = PyTorch.asFloatTensor(A)
    tensorB = PyTorch.asFloatTensor(B)
    print(' ... asTensor called')

    print('tensorA', tensorA)

    tensorA.set2d(1, 1, 56.4)
    tensorA.set2d(2, 0, 76.5)
    print('tensorA', tensorA)
    print('A', A)

    print('add 5 to tensorA')
    tensorA += 5
    print('tensorA', tensorA)
    print('A', A)
开发者ID:hughperkins,项目名称:pytorch,代码行数:32,代码来源:jinja2.test_pytorch.py


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