Ejemplo n.º 1
0
 def init_weights(self):
   self.param_count = 0
   for module in self.modules():
     if (isinstance(module, nn.Conv2d)
         or isinstance(module, nn.Linear)
         or isinstance(module, nn.Embedding)):
       if self.init == 'ortho':
         init.orthogonal_(module.weight)
       elif self.init == 'N02':
         init.normal_(module.weight, 0, 0.02)
       elif self.init in ['glorot', 'xavier']:
         init.xavier_uniform_(module.weight)
       else:
         print('Init style not recognized...')
       self.param_count += sum([p.data.nelement() for p in module.parameters()])
   print('Param count for D''s initialized parameters: %d' % self.param_count)
Ejemplo n.º 2
0
from paddle import fluid
import paddorch as torch
import paddle
from scipy.sparse import csr_matrix
from paddorch.nn.init import xavier_uniform_
from paddorch.sparse import  FloatTensor
import numpy as np
place = fluid.CPUPlace()
with fluid.dygraph.guard(place=place):
    i = torch.from_numpy(np.array([[0, 2], [1, 0], [1, 2]]) ).astype("int32")
    v = paddle.Tensor(np.array([3, 4, 5])).astype("float32")
    x=FloatTensor(i,v,(2,3))
    print(x)

    A = csr_matrix([[1, 2, 0], [0, 0, 3], [4, 0, 5]]).todense().astype("float32")
    x= paddle.Tensor(A)
    # x = torch.randn((4, 23, 16))
    print(xavier_uniform_(x))