示例#1
0
def getData(file_path):
    sperm_d = scipy.io.loadmat(file_path)['cnn_data']
    pre_data = []
    normal_data = []
    for i in sperm_d:
        for j in i[0]:
            pre_data.append(np.float64(j[0]))
            if j[1][0] == 3:
                normal_data.append(np.float64(j[0]))
    pre_data = np.asarray(
        [cv2.resize(pre_data, (64, 64)) for pre_data in pre_data])
    pre_data = pre_data.astype(np.float32, copy=False) / (255.0 / 2) - 1.0

    normal_data = np.asarray(
        [cv2.resize(normal_data, (64, 64)) for normal_data in normal_data])
    normal_data = normal_data.astype(np.float32,
                                     copy=False) / (255.0 / 2) - 1.0
    #    plt.imshow(pre_data[0])
    pre_data = nd.array(pre_data).expand_dims(axis=1)
    pre_data = nd.tile(pre_data, (1, 3, 1, 1))

    normal_data = nd.array(normal_data).expand_dims(axis=1)
    normal_data = nd.tile(normal_data, (1, 3, 1, 1))

    return pre_data.asnumpy(), normal_data.asnumpy()
示例#2
0
    def embedding(self, observed_arr):
        '''
        Embedding. In default, this method does the positional encoding.

        Args:
            observed_arr:       `mxnet.ndarray` of observed data points.

        Returns:
            `mxnet.ndarray` of embedded data points.
        '''
        batch_size, seq_len, depth_dim = observed_arr.shape
        self.batch_size = batch_size
        self.seq_len = seq_len
        self.depth_dim = depth_dim

        if self.embedding_flag is False:
            return observed_arr

        depth_arr = nd.tile(nd.expand_dims((nd.arange(depth_dim) / 2).astype(int) * 2, 0), [seq_len, 1])

        depth_arr = depth_arr / depth_dim
        depth_arr = nd.power(10000.0, depth_arr).astype(np.float32)

        phase_arr = nd.tile(nd.expand_dims((nd.arange(depth_dim) % 2) * np.pi / 2, 0), [seq_len, 1])
        positional_arr = nd.tile(nd.expand_dims(nd.arange(seq_len), 1), [1, depth_dim])

        sin_arr = nd.sin(positional_arr / depth_arr + phase_arr)

        positional_encoded_arr = nd.tile(nd.expand_dims(sin_arr, 0), [batch_size, 1, 1])

        positional_encoded_arr = positional_encoded_arr.as_in_context(observed_arr.context)

        result_arr = observed_arr + (positional_encoded_arr * self.embedding_weignt)
        return result_arr
示例#3
0
def convert_xy(XY):
    B,H,W,A,N = XY.shape
    dy = nd.tile( nd.arange(0,H,repeat=(W*A), ctx = XY.context).reshape((1,H,W,A,1)), (B,1,1,1,1) )
    dx = nd.tile( nd.arange(0,W,repeat=(A),ctx = XY.context).reshape((1,1,W,A,1)), (B,H,1,1,1) )
    x,y = XY.split(num_outputs=2,axis=-1)
    x = (x + dx) / W
    y = (y + dy) / H
    return x,y
示例#4
0
def test_jitter_synthetic(
    jitter_method, float_type, ctx=mx.Context('cpu')
) -> None:
    # Initialize problem parameters
    batch_size = 1
    prediction_length = 50
    context_length = 5
    num_samples = 3

    # Initialize test data to generate Gaussian Process from
    lb = -5
    ub = 5
    dx = (ub - lb) / (prediction_length - 1)
    x_test = nd.arange(lb, ub + dx, dx, ctx=ctx, dtype=float_type).reshape(
        -1, 1
    )
    x_test = nd.tile(x_test, reps=(batch_size, 1, 1))

    # Define the GP hyper parameters
    amplitude = nd.ones((batch_size, 1, 1), ctx=ctx, dtype=float_type)
    length_scale = math.sqrt(0.4) * nd.ones_like(amplitude)
    sigma = math.sqrt(1e-5) * nd.ones_like(amplitude)

    # Instantiate desired kernel object and compute kernel matrix
    rbf_kernel = RBFKernel(amplitude, length_scale)

    # Generate samples from 0 mean Gaussian process with RBF Kernel and plot it
    gp = GaussianProcess(
        sigma=sigma,
        kernel=rbf_kernel,
        prediction_length=prediction_length,
        context_length=context_length,
        num_samples=num_samples,
        ctx=ctx,
        float_type=float_type,
        jitter_method=jitter_method,
        sample_noise=False,  # Returns sample without noise
    )

    # Generate training set on subset of interval using the sine function
    x_train = nd.array([-4, -3, -2, -1, 1], ctx=ctx, dtype=float_type).reshape(
        context_length, 1
    )
    x_train = nd.tile(x_train, reps=(batch_size, 1, 1))
    y_train = nd.sin(x_train.squeeze(axis=2))

    # Predict exact GP using the GP predictive mean and covariance using the same fixed hyper-parameters
    samples, predictive_mean, predictive_std = gp.exact_inference(
        x_train, y_train, x_test
    )

    assert (
        np.sum(np.isnan(samples.asnumpy())) == 0
    ), 'NaNs in predictive samples!'
示例#5
0
文件: yolotrain.py 项目: zfxu/tests
def convert_xy(XY):
    B, H, W, A, N = XY.shape
    dy = nd.tile(
        nd.arange(0, H, repeat=(W * A), ctx=XY.context).reshape(
            (1, H, W, A, 1)), (B, 1, 1, 1, 1))
    dx = nd.tile(
        nd.arange(0, W, repeat=(A), ctx=XY.context).reshape((1, 1, W, A, 1)),
        (B, H, 1, 1, 1))
    x, y = XY.split(num_outputs=2, axis=-1)
    x = (x + dx) / W
    y = (y + dy) / H
    return x, y
示例#6
0
def verify_loaded_model(net):
    """Run inference using ten random images.
    Print both input and output of the model"""

    def transform(data, label):
        return data.astype(np.float32)/255, label.astype(np.float32)

    # Load ten random images from the test dataset
    sample_data = mx.gluon.data.DataLoader(mx.gluon.data.vision.MNIST(train=False, transform=transform),
                                  10, shuffle=True)

    for data, label in sample_data:

        # Display the images
        img = nd.transpose(data, (1,0,2,3))
        img = nd.reshape(img, (28,10*28,1))
        imtiles = nd.tile(img, (1,1,3))
        plt.imshow(imtiles.asnumpy())
        plt.show()

        # Display the predictions
        data = nd.transpose(data, (0, 3, 1, 2))
        out = net(data.as_in_context(ctx))
        predictions = nd.argmax(out, axis=1)
        print('Model predictions: ', predictions.asnumpy())

        break
def transform(data, shape):
    data = mx.image.imresize(data, shape[1], shape[0])
    data = nd.transpose(data, (2, 0, 1))
    data = data.astype(np.float32) / 127.5 - 1.0
    if data.shape[0] == 1:
        data = nd.tile(data, (3, 1, 1))
    return data.reshape((1, ) + data.shape)
def verify_broadcast_like_fixed(xshp, wshp, lhs_axes, rhs_axes):
    x_np = np.random.uniform(size=xshp)
    w_np = np.random.uniform(size=wshp)
    x = nd.array(x_np)
    w = nd.array(w_np)

    # org op
    y = nd.broadcast_like(x, w, lhs_axes, rhs_axes)

    # rewrite op
    lndims = len(lhs_axes)
    rndims = len(rhs_axes)
    assert lndims == rndims

    xndims = len(xshp)
    lhs_axes = tuple([v+xndims if v<0 else v for v in lhs_axes])
    assert all([0<=v<xndims for v in list(lhs_axes)])

    wndims = len(wshp)
    rhs_axes = tuple([v+wndims if v<0 else v for v in rhs_axes])
    assert all([0<=v<wndims for v in list(rhs_axes)])

    assert all([xshp[lhs_axes[i]]==1 for i in range(lndims)])

    cnts = {v: wshp[rhs_axes[i]] for i, v in enumerate(lhs_axes)}
    reps = tuple([cnts[v] if v in lhs_axes else 1 for v in range(xndims)])
    z = nd.tile(x, reps=reps)

    # compare
    assert z.shape == y.shape
    zn, zp = get_norm(z)
    yn, yp = get_norm(y)
    rn = np.linalg.norm(zp-yp)
    print(zn, yn, rn)
示例#9
0
def verify_l2normalization_rewrite_tile(shape, eps, mode):
    assert len(shape) == 4  # NCHW
    data_np = np.random.uniform(size=shape)
    x = nd.array(data_np)

    # org op
    y = nd.L2Normalization(x, eps, mode=mode)

    # rewrite op
    z = nd.broadcast_mul(x, x)
    if mode == "channel":
        axis = [1]
    elif mode == "instance":
        axis = [1, 2, 3]
    elif mode == "spatial":
        axis = [2, 3]
    else:
        assert "not valid `mode` type: %s" % mode
    reps = tuple(
        [shp if i in axis else 1 for i, shp in enumerate(list(shape))])
    z = nd.sum(z, axis=axis, keepdims=True)
    eps_tensor = nd.array([eps])
    z = nd.sqrt(z)
    z = nd.tile(z, reps=reps)
    z = nd.broadcast_div(x, z)

    # compare
    assert z.shape == y.shape
    zn, zp = get_norm(z)
    yn, yp = get_norm(y)
    rn = np.linalg.norm(zp - yp)
    print(zn, yn, rn)
示例#10
0
 def transform(img, dims):
     data = mx.image.imread(img)
     data = mx.image.imresize(data, dims, dims)
     data = nd.transpose(data, (2, 0, 1))
     data = data.astype(np.float32) / 127.5 - 1
     if data.shape[0] == 1:
         data = nd.tile(data, (3, 1, 1))
     return data.reshape((1, ) + data.shape)
示例#11
0
文件: yolotest.py 项目: zfxu/tests
def convert_xy(XY):
    B,H,W,A,N = XY.shape
    dy = nd.tile( nd.arange(0,H,repeat=(W*A), ctx = XY.context).reshape((1,H,W,A,1)), (B,1,1,1,1) )
    dx = nd.tile( nd.arange(0,W,repeat=(A),ctx = XY.context).reshape((1,1,W,A,1)), (B,H,1,1,1) )
    x,y = XY.split(num_outputs=2,axis=-1)
    x = (x + dx) / W
    y = (y + dy) / H
    if 0:
        for b in range(B):
            for h in range(H):
                for w in range(W):
                    for a in range(A):
                        for n in range(1):
                            xx = dx[b,h,w,a,n].asnumpy()[0]
                            yy = dy[b,h,w,a,n].asnumpy()[0]
                            #pdb.set_trace()
                            print '(%.3f,%.3f)'%(xx,yy)
                    print ''
    return x,y
示例#12
0
def transform(data, target_wd, target_ht):
    # resize to target_wd * target_ht
    data = mx.image.imresize(data, target_wd, target_ht)
    # transpose from (target_wd, target_ht, 3)
    # to (3, target_wd, target_ht)
    data = nd.transpose(data, (2, 0, 1))
    # normalize to [-1, 1]
    data = data.astype(np.float32) / 127.5 - 1
    # if image is greyscale, repeat 3 times to get RGB image.
    if data.shape[0] == 1:
        data = nd.tile(data, (3, 1, 1))
    return data.reshape((1, ) + data.shape)
示例#13
0
def transform(data, label):

    data = data.asnumpy()
    data = cv2.resize(src=data, dsize=(64, 64), interpolation=cv2.INTER_CUBIC)

    data = nd.array(data)
    if len(data.shape) == 2:
        data = data.reshape((64, 64, 1))
    data = nd.transpose(data.astype(np.float32), (2, 0, 1))
    data = data / 127.5 - 1
    if data.shape[0] == 1:
        data = nd.tile(data, (3, 1, 1))
    return data, label.astype(np.float32)
def get_data(file_path, rand_seed, batch_size, train_augs):
    #從 mat 檔獲得 data資訊
    sperm_data = scipy.io.loadmat(file_path)['cnn_data']
    pre_data = []
    pre_label = []
    for i in sperm_data:
        for j in i[0]:
            pre_data.append(np.float64(j[0]))
            pre_label.append(np.int32(j[1][0][0]))


#            tic = time.time()
#            pre_data = np.append(pre_data,nd.array(cv2.resize(j[0],(94,94))).expand_dims(axis=0).asnumpy(),axis = 0)
#            pre_label = np.append(pre_label,np.int32(j[1][0][0]))
#            print(time.time()-tic)
    pre_data = np.asarray(
        [cv2.resize(pre_data, (128, 128)) for pre_data in pre_data])
    pre_data = pre_data.astype(np.float32, copy=False) / (255.0 / 2) - 1.0
    pre_label = np.array(pre_label)

    class_value = [0, 3, 4]  #所需要流下的種類編號
    data_num = [1536, 1536, 1024]  #
    pre_data, pre_label, new_label, new_data = data_class_process(
        pre_data, pre_label, data_num, train_augs, class_value)
    #    plt.imshow(pre_data[0])
    pre_data = nd.array(new_data).expand_dims(axis=1)
    pre_data = nd.tile(pre_data, (1, 3, 1, 1))

    pre_label = np.array(new_label)
    np.random.seed(rand_seed)
    rand_index = np.random.permutation(np.shape(pre_data)[0])

    #    plt.figure(2)
    #    plt.imshow(((pre_data[0][0]+1)*(255/2)).asnumpy())

    pre_train = [
        pre_data[rand_index[0:-np.int32(rand_index.shape[0] / 4)]],
        pre_label[rand_index[0:-np.int32(rand_index.shape[0] / 4)]]
    ]
    pre_test = [
        pre_data[rand_index[-np.int32(rand_index.shape[0] / 4):]],
        pre_label[rand_index[-np.int32(rand_index.shape[0] / 4):]]
    ]
    train_iter = mx.io.NDArrayIter(data=pre_train[0],
                                   label=pre_train[1],
                                   batch_size=batch_size)
    test_iter = mx.io.NDArrayIter(data=pre_test[0],
                                  label=pre_test[1],
                                  batch_size=batch_size)

    return train_iter, test_iter, data_num
示例#15
0
    def var(array,W=_W,B=None,square=0,sqrt=0,V=False,order='NCHW',sizz=0):
        arrs=array.shape
        ashp=W.shape
        xi=(-2,-1)
        x2=(-2,-1,-3)
        sb=(ashp[1],1,1)
        WV=ashp[-2:]
        print(sb)

        mnc=mnd.tile(mnd.reshape(mnd.array([WV[0]*WV[1]]), shape=(1,1,1)),ashp[1])
        print(mnc)

        if V:
            print(W.eval())
        print(arrs,ashp)
        mul=(mnd.broadcast_mul(array,W))
        if V:
            print('Wsamp',W[-1,-1])
            print('array*w',mul[0,-1])
        size=mnd.sum(W,axis=xi,keepdims=True)#shape=(outputs, channel)
        if V:
            print("sizesamp",size.shape,size)
        if B is None:
            B=mnd.zeros(W.shape[0:2],dtype=np.float32)#channel
        B=mnd.reshape(B,(*B.shape,*[1 for _ in range(len(ashp)-len(B.shape))]))
        if sizz==1:
            mean=mnd.sum(mul,axis=xi,keepdims=True)/size
        else:
            mean=mnd.sum(mul,axis=xi,keepdims=True)/mnc
        if V:
            print("meansamp",mean[0,-1])
        if square:
            i=mnd.square(mnd.broadcast_add(mnd.broadcast_minus(mul,mean),B))
        else:
            i=mnd.broadcast_add(mnd.broadcast_minus(mul,mean),B)
        di=i/size
        if V==2:
            print("i",i,"i")
            print("di",di,"di")
        if V:
            print('isamp',i.shape,i[-1,-1,])
        out=mnd.sum(mnd.broadcast_add(i,B),axis=x2)
        #out=np.rollaxis(np.sum(i+B,axis=x2),-1,1)
        #print(out.shape)
        if sqrt:
            out=mnd.sqrt(out)
        out=mnd.swapaxes(out, 3, 1)
        #print(out.shape,(arrs[0],ashp[0],arrs[1],arrs[2]))
        assert out.shape==(arrs[0],ashp[0],arrs[1],arrs[2])
        return(out)
示例#16
0
    def forward(self, X):
        X = nd.expand_dims(X, axis=-2)
        reps = [1] * len(X.shape)
        reps[-2] = self.num_perspective
        X = nd.tile(X, reps=tuple(reps))

        W = self.weight.data(X.context)

        delta_dim = len(X.shape) - len(W.shape)
        for i in range(delta_dim):
            W = nd.expand_dims(W, axis=0)

        Y = X * W

        return Y
示例#17
0
    def cvt_output_for_predict(self,pred): #how to interprete net output according format_groundtruth()
        predCls,predObj, XYWH = self.format_net_output(pred)
        batchSize,height,width,boxNum,_= XYWH.shape
        X,Y,W,H = XYWH.split(num_outputs=4, axis=-1)
        #pdb.set_trace()
        DY = nd.tile(nd.arange(0,height,repeat=width*boxNum, ctx=XYWH.context).reshape((1,height,width,boxNum,1)), (batchSize,1,1,1,1) )
        DX = nd.tile(nd.arange(0,width,repeat=boxNum,ctx=XYWH.context).reshape((1,1,width,boxNum,1)),(batchSize,height,1,1,1))
        X = (X + DX) / width
        Y = (Y + DY) / height
        #pdb.set_trace()
        W = nd.exp(W) - 1
        H = nd.exp(H) - 1

        
        W = nd.clip(W,0,1)
        H = nd.clip(H,0,1)
        X = nd.clip(X,0,1)
        Y = nd.clip(Y,0,1)
        left = X
        top = Y
        right = nd.clip(left + W,0,1)
        bottom = nd.clip(top + H, 0, 1)
        corners = nd.concat(left,top,right,bottom,dim=-1) #nms requiring corner format
        return predCls, predObj, corners
def test():
    x_np = np.random.uniform(size=(1,1,1,3))
    x = nd.array(x_np)
    w_np = np.random.uniform(size=(3,1,4))
    w = nd.array(w_np)

    y = nd.broadcast_like(x, w, lhs_axes=(1,1), rhs_axes=(2,0))
    print(y.shape)

    z = nd.tile(x, reps=(1,3,1,1))
    print(z.shape)

    assert z.shape == y.shape
    zn, zp = get_norm(z)
    yn, yp = get_norm(y)
    rn = np.linalg.norm(zp-yp)
    print(zn, yn, rn)
示例#19
0
def get_sample_point():
    """Grabs a single input/label pair from MNIST"""
    def transform(data, label):
        return data.astype(np.float32) / 255.0, label.astype(np.float32)

    # Load ten random images from the test dataset
    sample_data = mx.gluon.data.DataLoader(mx.gluon.data.vision.MNIST(
        train=False, transform=transform),
                                           1,
                                           shuffle=True)
    for data, label in sample_data:
        img = nd.transpose(data, (1, 0, 2, 3))
        img = nd.reshape(img, (28, 28, 1))
        imtiles = nd.tile(img, (1, 1, 3))
        plt.imshow(imtiles.asnumpy())
        plt.savefig("test_input.png")

        data = nd.transpose(data, (0, 3, 1, 2))
        data = data.as_in_context(ctx).asnumpy()
        label = int(label.asnumpy()[0])
        return data, label
def test_evaluate(data_path, net):
    sperm_d = scipy.io.loadmat(data_path)['cnn_data']
    t_data = []
    for i in sperm_d:
        for j in i[0]:
            t_data.append(np.float64(j[0]))

    t_data = np.asarray([cv2.resize(t_data, (128, 128)) for t_data in t_data])
    t_data = t_data.astype(np.float32, copy=False) / (255.0 / 2) - 1.0
    t_data = nd.array(t_data).expand_dims(axis=1)
    t_data = nd.tile(t_data, (1, 3, 1, 1))

    t_iter = mx.io.NDArrayIter(data=t_data)

    t_iter.reset()
    y = nd.zeros(len(t_iter.data[0][1]))
    for i, batch in enumerate(t_iter):
        data = batch.data
        for X in data:
            y[i] = net(X).argmax(axis=1)

    return y
def verify_broadcast_like_dynamic(xshp, wshp, lhs_axes, rhs_axes):
    x_np = np.random.uniform(size=xshp)
    w_np = np.random.uniform(size=wshp)
    x = nd.array(x_np)
    w = nd.array(w_np)

    # org op
    y = nd.broadcast_like(x, w,
        lhs_axes=lhs_axes, rhs_axes=rhs_axes)
    print(y.shape)

    # rewrite op
    xndims, wndims = len(xshp), len(wshp)
    if lhs_axes is None or rhs_axes is None:
        assert xndims == wndims and lhs_axes is None \
            and rhs_axes is None
        z = _broadcast_like(x, w)
    else:
        lhs_axes, lndims = list(lhs_axes), len(lhs_axes)
        rhs_axes, rndims = list(rhs_axes), len(rhs_axes)
        assert lndims == rndims > 0

        lhs_axes = tuple([v+xndims if v<0 else v for v in lhs_axes])
        assert all([0<=v<xndims for v in list(lhs_axes)])

        rhs_axes = tuple([v+wndims if v<0 else v for v in rhs_axes])
        assert all([0<=v<wndims for v in list(rhs_axes)])

        assert all([xshp[lhs_axes[i]] == 1 for i in range(lndims)])

        batch_axes = [0]
        flg = all([batch_axis not in rhs_axes \
            for batch_axis in batch_axes])
        if flg:
            cnts = {v: wshp[rhs_axes[i]] \
                for i, v in enumerate(lhs_axes)}
            reps = tuple([cnts[v] if v in lhs_axes else 1 \
                for v in range(xndims)])
            z = nd.tile(x, reps=reps)
        else:
            axis_map = {}
            for i, v in enumerate(lhs_axes):
                axis_map[v] = rhs_axes[i]
            for batch_axis in batch_axes:
                assert sum([1 if v == batch_axis else 0 \
                    for k, v in axis_map.items()]) <= 1, \
                    "multiple broadcast on batch_axis: %s, " + \
                    "which is not support by dynamic shape fusion." % \
                    batch_axis
            assert wndims < 6, \
                "slice can manipulate at most 5d"

            # reduce shape to 1 for non-broadcast dimensions
            begin = tuple([0]*wndims)
            end = tuple([wshp[v] if v in axis_map.values() else 1 \
                for v in range(wndims)])
            w = nd.slice(w, begin=begin, end=end)

            # decompose k1->v, k2->v into k1->v, k2->v2
            # which make axis
            while True:
                vs, flag, paxis_map = set(), True, axis_map
                for pk, pv in paxis_map.items():
                    if pv not in vs:
                        vs.add(pv)
                        continue
                    flag = False
                    axis_map = {k: (v+1 if v>pv or k==pk else v) \
                        for k, v in axis_map.items()}
                    w = nd.expand_dims(w, axis=pv)
                    w = nd.repeat(w, axis=pv, repeats=wshp[pv])
                    wshp = wshp[:pv] + (wshp[pv],) + wshp[pv:]
                    break
                if flag:
                    break
            wndims = len(wshp)

            # trim wndims if not equal to xndims
            v = 0
            while wndims > xndims:
                while v in axis_map.values():
                    v += 1
                w = nd.squeeze(w, axis=v)
                wndims -= 1
                axis_map = {k: (nv-1 if nv > v else nv) \
                    for k, nv in axis_map.items()}
            while wndims < xndims:
                w = nd.expand_dims(w, axis=wndims)
                wndims += 1
            axes = list(range(wndims))
            while True:
                dels = [k for k, v in axis_map.items() if k==v]
                for k in dels:
                    del axis_map[k]
                if not axis_map:
                    break
                keys = list(axis_map.keys())
                k, v = keys[0], axis_map[keys[0]]
                axes[k], axes[v] = axes[v], axes[k]
                for nk in keys:
                    nv = axis_map[nk]
                    if nv == k:
                        axis_map[nk] = v
                    elif nv == v:
                        axis_map[nk] = k
            axes = tuple(axes)
            if axes != tuple(range(wndims)):
                assert wndims < 7, \
                    "slice can manipulate at most 6d"
                w = nd.transpose(w, axes=axes)
            z = _broadcast_like(x, w)
    print(z.shape)

    # compare
    assert z.shape == y.shape
    zn, zp = get_norm(z)
    yn, yp = get_norm(y)
    rn = np.linalg.norm(zp-yp)
    print(zn, yn, rn)
示例#22
0
    def hybrid_forward(self, F, X):
        # (batch_size, num_channel_prev, h, w, dim_vector)
        # -->(batch_size,num_capsule_prev,1,1,dim_vector)
        X = X.reshape((0, -1, 1, 1, 0))

        self.num_capsules_prev = X.shape[1]
        self.batch_size = X.shape[0]
        # (batch_size,num_capsule_prev,out_channels,1,dim_vector)
        X_tile = nd.tile(X, reps=(1, 1, self.out_channels, 1, 1))

        if self.routing_weight_initial:
            self.routing_weight = nd.random_normal(
                shape=(1, self.num_capsules_prev, self.out_channels,
                       self.dim_input_vector, self.dim_vector),
                name='routing_weight').as_in_context(mx.gpu(0))
            self.routing_weight_initial = False
        # (batch_size,num_capsule_prev,out_channels,dim_input_vector,dim_vector)
        # (64, 1152, 10, 8, 16)
        W_tile = nd.tile(self.routing_weight,
                         reps=(self.batch_size, 1, 1, 1, 1))
        linear_combination_3d = nd.batch_dot(
            X_tile.reshape((-1, X_tile.shape[-2], X_tile.shape[-1])),
            W_tile.reshape((-1, W_tile.shape[-2], W_tile.shape[-1])))
        # (64, 1152, 10, 1, 16)
        linear_combination = linear_combination_3d.reshape(
            (self.batch_size, self.num_capsules_prev, self.out_channels, 1,
             self.dim_vector))

        # b_ij (1, 1152, 10, 1, 1)
        priors = nd.zeros((1, self.num_capsules_prev, self.out_channels, 1, 1))

        ############################################################################
        ##                                Rounting                                ##
        ############################################################################
        for iter_index in range(self.num_routing_iter):
            # NOTE: RoutingAlgorithm-line 4
            # b_ij (1, 1152, 10, 1, 1)
            softmax_prior = nd.softmax(priors,
                                       axis=2)  # on num_capsule dimension
            # NOTE: RoutingAlgorithm-line 5
            # (64, 1152, 10, 1, 16)
            # output = torch.mul(softmax_prior, linear_combination)
            output = softmax_prior * linear_combination

            # (64, 1, 10, 1, 16)
            output_sum = output.sum(axis=1, keepdims=True)  # s_J

            # NOTE: RoutingAlgorithm-line 6
            # (64, 1, 10, 1, 16)
            output_squashed = self.squash(output_sum)  # v_J

            # NOTE: RoutingAlgorithm-line 7
            # (64, 1152, 10, 1, 16)
            output_tile = nd.tile(output_squashed,
                                  reps=(1, self.num_capsules_prev, 1, 1, 1))
            # (64, 1152, 10, 1, 16) x (64, 1152, 10, 1, 16) (transpose on last two axis)
            # ==> (64, 1152, 10, 1, 1)
            U_times_v = nd.batch_dot(linear_combination.reshape(
                (-1, 1, self.dim_vector)),
                                     output_tile.reshape(
                                         (-1, 1, self.dim_vector)),
                                     transpose_b=True)
            U_times_v = U_times_v.reshape(
                (self.batch_size, self.num_capsules_prev, self.out_channels, 1,
                 1))

            priors = priors + U_times_v.sum(axis=0).expand_dims(axis=0)

        return output_squashed  # v_J
示例#23
0
def main():
    # Initialize problem parameters
    batch_size = 1
    prediction_length = 50
    context_length = 5
    axis = [-5, 5, -3, 3]
    float_type = np.float64
    ctx = mx.Context("gpu")

    num_samples = 3
    ts_idx = 0

    # Initialize test data to generate Gaussian Process from
    lb = -5
    ub = 5
    dx = (ub - lb) / (prediction_length - 1)
    x_test = nd.arange(lb, ub + dx, dx, ctx=ctx,
                       dtype=float_type).reshape(-1, 1)
    x_test = nd.tile(x_test, reps=(batch_size, 1, 1))

    # Define the GP hyper parameters
    amplitude = nd.ones((batch_size, 1, 1), ctx=ctx, dtype=float_type)
    length_scale = math.sqrt(0.4) * nd.ones_like(amplitude)
    sigma = math.sqrt(1e-5) * nd.ones_like(amplitude)

    # Instantiate desired kernel object and compute kernel matrix
    rbf_kernel = RBFKernel(amplitude, length_scale)

    # Generate samples from 0 mean Gaussian process with RBF Kernel and plot it
    gp = GaussianProcess(
        sigma=sigma,
        kernel=rbf_kernel,
        prediction_length=prediction_length,
        context_length=context_length,
        num_samples=num_samples,
        ctx=ctx,
        float_type=float_type,
        sample_noise=False,  # Returns sample without noise
    )
    mean = nd.zeros((batch_size, prediction_length), ctx=ctx, dtype=float_type)
    covariance = rbf_kernel.kernel_matrix(x_test, x_test)
    gp.plot(x_test=x_test, samples=gp.sample(mean, covariance), ts_idx=ts_idx)

    # Generate training set on subset of interval using the sine function
    x_train = nd.array([-4, -3, -2, -1, 1], ctx=ctx,
                       dtype=float_type).reshape(context_length, 1)
    x_train = nd.tile(x_train, reps=(batch_size, 1, 1))
    y_train = nd.sin(x_train.squeeze(axis=2))

    # Predict exact GP using the GP predictive mean and covariance using the same fixed hyper-parameters
    samples, predictive_mean, predictive_std = gp.exact_inference(
        x_train, y_train, x_test)

    assert (np.sum(np.isnan(
        samples.asnumpy())) == 0), "NaNs in predictive samples!"

    gp.plot(
        x_train=x_train,
        y_train=y_train,
        x_test=x_test,
        ts_idx=ts_idx,
        mean=predictive_mean,
        std=predictive_std,
        samples=samples,
        axis=axis,
    )