예제 #1
0
def test_pynn():
    PyTorch.manualSeed(123)
    linear = nn.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 = nn.ClassNLLCriterion()
    print('criterion', criterion)

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

    mlp = nn.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 = nn.Sequential()

    mlp.add(nn.SpatialConvolutionMM(1, 16, 5, 5, 1, 1, 2, 2))
    res = mlp.add(nn.ReLU())
    print('res', res)
    mlp.add(nn.SpatialMaxPooling(3, 3, 3, 3))

    mlp.add(nn.SpatialConvolutionMM(16, 32, 3, 3, 1, 1, 1, 1))
    mlp.add(nn.ReLU())
    mlp.add(nn.SpatialMaxPooling(2, 2, 2, 2))

    mlp.add(nn.Reshape(32 * 4 * 4))
    mlp.add(nn.Linear(32 * 4 * 4, 150))
    mlp.add(nn.Tanh())
    mlp.add(nn.Linear(150, 10))
    mlp.add(nn.LogSoftMax())

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

    learningRate = 0.02

    mndata = MNIST('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))
    size = PyTorch.LongStorage(4)
    size[0] = N
    size[1] = 1
    size[2] = 28
    size[3] = 28
    imagesTensor.resize(size)
    imagesTensor /= 255.0
    imagesTensor -= 0.2
    print('imagesTensor.size()', imagesTensor.size())

    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)
    #        print('prediction', prediction)
            if prediction == label:
                numRight += 1
            criterion.forward(output, labelTensor)
            mlp.zeroGradParameters()
            gradOutput = criterion.backward(output, labelTensor)
            mlp.backward(input, gradOutput)
            mlp.updateParameters(learningRate)
    #        PyTorch.collectgarbage()
    #        if n % 100 == 0:
    #            print('n=', n)
        print('epoch ' + str(epoch) + ' accuracy: ' + str(numRight * 100.0 / N) + '%')
예제 #2
0
    print(PyTorch.{{Real}}Tensor(3, 4).normal())
    print(PyTorch.{{Real}}Tensor(3, 4).cauchy())
    print(PyTorch.{{Real}}Tensor(3, 4).exponential())
    print(PyTorch.{{Real}}Tensor(3, 4).logNormal())
    {% endif -%}
    print(PyTorch.{{Real}}Tensor(3, 4).bernoulli())
    print(PyTorch.{{Real}}Tensor(3, 4).geometric())
    print(PyTorch.{{Real}}Tensor(3, 4).geometric())
    PyTorch.manualSeed(3)
    print(PyTorch.{{Real}}Tensor(3, 4).geometric())
    PyTorch.manualSeed(3)
    print(PyTorch.{{Real}}Tensor(3, 4).geometric())

    print(type(PyTorch.{{Real}}Tensor(2, 3)))

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

    print('resize1d', PyTorch.{{Real}}Tensor().resize1d(3).fill(1))
    print('resize2d', PyTorch.{{Real}}Tensor().resize2d(2, 3).fill(1))
    print('resize', PyTorch.{{Real}}Tensor().resize(size).fill(1))

    D = PyTorch.{{Real}}Tensor(size).geometric()

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

#    def myexec(expr):
예제 #3
0
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')
    myeval('A')
    myexec('A -= 3')
    myeval('A')
    print('A /= 3')
    A /= 3
    myeval('A')

    myeval('A + 5')
    myeval('A - 5')
    myeval('A * 5')
    print('A / 2')
    A / 2
    B = FloatTensor().resizeAs(A).geometric(0.9)
    myeval('B')
    myeval('A + B')
    myeval('A - B')
    myexec('A += B')
    myeval('A')
    myexec('A -= B')
    myeval('A')
예제 #4
0
def test_pytorchByte():
    PyTorch.manualSeed(123)
    numpy.random.seed(123)

    ByteTensor = PyTorch.ByteTensor

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

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

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

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

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

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

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

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

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

    myeval('A + 5')
    myeval('A * 5')
    print('A // 2')
    A // 2
    B = ByteTensor().resizeAs(A).geometric(0.9)
    myeval('B')
    myeval('A + B')
    myexec('A += B')
    myeval('A')
예제 #5
0
def test_pytorchDouble():
    PyTorch.manualSeed(123)
    numpy.random.seed(123)

    DoubleTensor = PyTorch.DoubleTensor

    D = PyTorch.DoubleTensor(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.DoubleTensor(3, 4).uniform())
    print(PyTorch.DoubleTensor(3, 4).normal())
    print(PyTorch.DoubleTensor(3, 4).cauchy())
    print(PyTorch.DoubleTensor(3, 4).exponential())
    print(PyTorch.DoubleTensor(3, 4).logNormal())

    print(PyTorch.DoubleTensor(3, 4).bernoulli())
    print(PyTorch.DoubleTensor(3, 4).geometric())
    print(PyTorch.DoubleTensor(3, 4).geometric())
    PyTorch.manualSeed(3)
    print(PyTorch.DoubleTensor(3, 4).geometric())
    PyTorch.manualSeed(3)
    print(PyTorch.DoubleTensor(3, 4).geometric())

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

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

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

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

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

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

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

    A = DoubleTensor(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 = DoubleTensor().resizeAs(A).geometric(0.9)
    myeval('B')
    myeval('A + B')

    myeval('A - B')

    myexec('A += B')
    myeval('A')

    myexec('A -= B')
    myeval('A')