예제 #1
0
    def test_shapes(self):

        # test shape tuple
        shape1 = dc.lvec([2, 3, 4, 5])
        shape2 = dc.lvec([5, 4, 3, 2])
        a = dc.random(2, 3, 4, 5).astype('int')
        assert a.rank() == 4
        assert a.shape() == (2, 3, 4, 5)

        # reshape to new dimensions
        a.reshape(shape2)
        assert a.shape() == (5, 4, 3, 2)

        # return a new tensor with flattened dimensions.
        b = a.flatten()
        assert a.shape() == (5, 4, 3, 2)
        assert b.shape() == (120, )

        # flatten the same tensor
        a.flatteninplace()
        assert a.shape() == (120, )
        shape3 = dc.lvec([8, 15, 1, 1])

        # new shape
        a.reshape(shape3)

        # confirm new shape
        assert a.shape() == (8, 15, 1, 1)

        # dnnc method to reshape.
        a = dc.random(2, 3, 4, 5)
        dc.reshape(a, (8, 15, 1, 1))
        assert a.shape() == (8, 15, 1, 1)
        dc.reshape(a, (120, ))
        assert a.shape() == (120, )
예제 #2
0
    def test_data(self):

        # confirm type as class tuple.
        a = dc.zeros(2, 3).astype('int')
        adata = a.data()
        assert type(adata) == type((1, ))

        # load new data
        new_data_list = [10, 11, 12, 13, 14, 15]
        a.load(dc.ivec(new_data_list))
        assert a[0] == 10

        # load one element with flat index
        a[0] = 777
        assert a[0] == 777

        # reshape, fetch and load with multi indices
        a = dc.arange(12).astype('int')
        a.reshape(dc.lvec([2, 2, 3]))
        assert a[0, 1, 1] == 4

        a[1, 1, 1] = 200
        assert a[1, 1, 1] == 200

        # negative test
        try:
            # This throws ValueError
            print(a[0, 0, 9, 9, 9])
        except ValueError as e:
            assert e
예제 #3
0
import os, sys
sys.path.append(os.path.abspath('.'))

import dnnc as dc
t1 = dc.array(2, 3)
t2 = dc.array(3, 2)

mul = dc.matmul(t1, t2)
#print ("multiplication : " , mul.to_string())
add = dc.add(t1, t1)
#print ("addition : " , add.to_string())

t3 = dc.array(2, 3, 4)
#print("old shape", t1.shape())
new_shape = dc.lvec([2, 12])
t3.reshape(new_shape)
#print("new shape", t1.shape())

#t4 = dc.thresholded_relu(t1);
#print("relu", t4.to_string())

#replace first few values in tensor with new values.
data = dc.fvec([1.0, 2.0, 3.0, 4.0])
t3.load(data)
#print(t3.to_string())

arr = dc.array([1, 2])
#print(arr)
arr2D = dc.array([[1, 2], [10, 20]]).astype('int')
#print(arr2D)
arrRand = dc.random(2, 3)