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


Python PyTorch.manualSeed方法代码示例

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


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

示例1: test_double_tensor

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

示例2: test_double_tensor

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

示例3: test_byte_tensor

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

示例4: test_float_tensor

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

示例5: test_pynn

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

示例6: test_pytorchLong

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

    LongTensor = PyTorch.LongTensor

    

    D = PyTorch.LongTensor(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.LongTensor(3,4).bernoulli())
    print(PyTorch.LongTensor(3,4).geometric())
    print(PyTorch.LongTensor(3,4).geometric())
    PyTorch.manualSeed(3)
    print(PyTorch.LongTensor(3,4).geometric())
    PyTorch.manualSeed(3)
    print(PyTorch.LongTensor(3,4).geometric())

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

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

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

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

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

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

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

    A = LongTensor(3,4).geometric(0.9)
    myeval('A')
    myexec('A += 3')
    myeval('A')
    myexec('A *= 3')
    myeval('A')
    
    myexec('A -= 3')
    
    myeval('A')
    myexec('A /= 3')
    myeval('A')

    myeval('A + 5')
    
    myeval('A - 5')
    
    myeval('A * 5')
    myeval('A / 2')

    B = LongTensor().resizeAs(A).geometric(0.9)
    myeval('B')
    myeval('A + B')
    
    myeval('A - B')
    
    myexec('A += B')
    myeval('A')
    
    myexec('A -= B')
    myeval('A')
开发者ID:dementrock,项目名称:pytorch,代码行数:87,代码来源:test_pytorch.py

示例7: test_pytorchFloat

# 需要导入模块: import PyTorch [as 别名]
# 或者: from PyTorch import manualSeed [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

示例8: test_cltorch

# 需要导入模块: import PyTorch [as 别名]
# 或者: from PyTorch import manualSeed [as 别名]
def test_cltorch():
    if "ALLOW_NON_GPUS" in os.environ:
        PyClTorch.setAllowNonGpus(True)

    # a = PyTorch.foo(3,2)
    # print('a', a)
    # print(PyTorch.FloatTensor(3,2))

    a = PyClTorch.ClTensor([3, 4, 9])
    assert a[0] == 3
    assert a[1] == 4
    assert a[2] == 9
    print("a", a)

    a = PyClTorch.ClTensor([[3, 5, 7], [9, 2, 4]])
    print("a", a)
    print("a[0]", a[0])
    print("a[0][0]", a[0][0])
    assert a[0][0] == 3
    assert a[1][0] == 9
    assert a[1][2] == 4

    PyTorch.manualSeed(123)
    a = PyTorch.FloatTensor(4, 3).uniform()
    print("a", a)
    a_cl = a.cl()
    print(type(a_cl))
    assert str(type(a_cl)) == "<class 'PyClTorch.ClTensor'>"
    print("a_cl[0]", a_cl[0])
    print("a_cl[0][0]", a_cl[0][0])
    assert a[0][0] == a_cl[0][0]
    assert a[0][1] == a_cl[0][1]
    assert a[1][1] == a_cl[1][1]

    print("a.dims()", a.dims())
    print("a.size()", a.size())
    print("a", a)
    assert a.dims() == 2
    assert a.size()[0] == 4
    assert a.size()[1] == 3

    a_sum = a.sum()
    a_cl_sum = a_cl.sum()
    assert abs(a_sum - a_cl_sum) < 1e-4
    a_cl2 = a_cl + 3.2
    assert abs(a_cl2[1][0] - a[1][0] - 3.2) < 1e-4

    b = PyClTorch.ClTensor()
    print("got b")
    myeval("b")
    assert b.dims() == -1
    b.resizeAs(a)
    myeval("b")
    assert b.dims() == 2
    assert b.size()[0] == 4
    assert b.size()[1] == 3
    print("run uniform")
    b.uniform()
    myeval("b")

    print("create new b")
    b = PyClTorch.ClTensor()
    print("b.dims()", b.dims())
    print("b.size()", b.size())
    print("b", b)

    c = PyTorch.FloatTensor().cl()
    print("c.dims()", c.dims())
    print("c.size()", c.size())
    print("c", c)
    assert b.dims() == -1
    assert b.size() is None

    print("creating Linear...")
    linear = nn.Linear(3, 5).float()
    print("created linear")
    print("linear:", linear)
    myeval("linear.output")
    myeval("linear.output.dims()")
    myeval("linear.output.size()")
    myeval("linear.output.nElement()")

    linear_cl = linear.clone().cl()
    print("type(linear.output)", type(linear.output))
    print("type(linear_cl.output)", type(linear_cl.output))
    assert str(type(linear.output)) == "<class 'PyTorch._FloatTensor'>"
    assert str(type(linear_cl.output)) == "<class 'PyClTorch.ClTensor'>"
    # myeval('type(linear)')
    # myeval('type(linear.output)')
    myeval("linear_cl.output.dims()")
    myeval("linear_cl.output.size()")
    # myeval('linear.output')
    assert str(type(linear)) == "<class 'PyTorchAug.Linear'>"
    assert str(type(linear_cl)) == "<class 'PyTorchAug.Linear'>"
    # assert str(type(linear.output)) == '<class \'PyClTorch.ClTensor\'>'
    # assert linear.output.dims() == -1  # why is this 0? should be -1???
    # assert linear.output.size() is None  # again, should be None?

    a_cl = PyClTorch.ClTensor(4, 3).uniform()
    # print('a_cl', a_cl)
#.........这里部分代码省略.........
开发者ID:hughperkins,项目名称:pycltorch,代码行数:103,代码来源:test_cltorch.py

示例9: print

# 需要导入模块: import PyTorch [as 别名]
# 或者: from PyTorch import manualSeed [as 别名]

{%- set types = [
    {'Real': 'Long','real': 'long'},
    {'Real': 'Float', 'real': 'float'}, 
    {'Real': 'Double', 'real': 'double'},
    {'Real': 'Byte', 'real': 'unsigned char'}
]
%}


{% for typedict in types -%}
{%- set Real = typedict['Real'] -%}
{%- 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')
开发者ID:hughperkins,项目名称:pytorch,代码行数:32,代码来源:jinja2.test_pytorch.py


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