Пример #1
0
 def test_fwd_same_as_numpy_3D3D_memsave(self):
     a = Variable(torch.FloatTensor(np.random.random((10, 5, 20))))
     b = Variable(torch.FloatTensor(np.random.random((10, 4, 20))))
     dist = ForwardDistance(20, 20, 10, activation=None, use_bias=False)
     dist.memsave = True
     d = dist(a, b).data.numpy()
     lw, rw, aggw = dist.lblock.weight.data.numpy(
     ), dist.rblock.weight.data.numpy(), dist.agg.data.numpy()
     a = a.data.numpy()
     b = b.data.numpy()
     npd = np.zeros((10, 5, 4))
     for i in range(a.shape[0]):
         for j in range(a.shape[1]):
             for k in range(b.shape[1]):
                 x = np.dot(np.dot(lw, a[i, j]) + np.dot(rw, b[i, k]), aggw)
                 npd[i, j, k] = x
     print(np.argwhere(abs(d - npd) > 1e-8))
     #print(d[np.argwhere(abs(d - npd) > 1e-8)])
     #print(npd[np.argwhere(abs(d - npd) > 1e-8)])
     print(np.argwhere(abs(d - npd) > 1e-7))
     print(np.argwhere(abs(d - npd) > 1e-6))
     print(np.argwhere(abs(d - npd) > 1e-5))
     print(d[1])
     print(npd[1])
     print((d - npd)[1])
     self.assertTrue(np.allclose(d, npd, atol=1e-6))
Пример #2
0
 def test_all_dist_shape_3D3D(self):
     self.m = DotDistance()
     self.dorun_shape_tst_3D3D()
     self.m = CosineDistance()
     self.dorun_shape_tst_3D3D()
     self.m = ForwardDistance(4, 4, 8)
     self.dorun_shape_tst_3D3D()
     self.m = BilinearDistance(4, 4)
     self.dorun_shape_tst_3D3D()
     self.m = TrilinearDistance(4, 4, 8)
     self.dorun_shape_tst_3D3D()
Пример #3
0
 def forward_gen(self,
                 ldim,
                 rdim,
                 aggdim,
                 activation="tanh",
                 use_bias=True):
     self.attgen.dist = ForwardDistance(ldim,
                                        rdim,
                                        aggdim,
                                        activation=activation,
                                        use_bias=use_bias)
     return self
Пример #4
0
 def test_fwd_same_as_numpy_2D2D(self):
     a = Variable(torch.FloatTensor(np.random.random((100, 200))))
     b = Variable(torch.FloatTensor(np.random.random((100, 200))))
     dist = ForwardDistance(200, 200, 100, activation=None, use_bias=False)
     d = dist(a, b).data.numpy()
     lw, rw, aggw = dist.lblock.weight.data.numpy(
     ), dist.rblock.weight.data.numpy(), dist.agg.data.numpy()
     a = a.data.numpy()
     b = b.data.numpy()
     npd = np.zeros((100, ))
     for i in range(a.shape[0]):
         x = np.dot(np.dot(lw, a[i]) + np.dot(rw, b[i]), aggw)
         npd[i] = x
     self.assertTrue(np.allclose(d, npd, atol=1e-6))