簡體   English   中英

如何將 numpy 數組輸入到 pytorch 中的神經網絡?

[英]How to input a numpy array to a neural network in pytorch?

這是我定義的神經網絡

class generator(nn.Module):
def __init__(self, n_dim, io_dim):
    super().__init__()
    self.gen = nn.Sequential(
        nn.Linear(n_dim,64),
        nn.LeakyReLU(.01),
        nn.Linear(64, io_dim),
    )

def forward(self, x):
    return self.gen(x)
#The input x is:
x = numpy.random.dirichlet([10,6,3],3)

現在我希望神經網絡將 dirichlet 分布式樣本(使用 numpy.random.dirichlet([10,6,3],10) 采樣)作為輸入。 怎么做?

您需要將numpy.array轉換為torch.Tensor

input_tensor = torch.from_numpy(x)

要將 NumPy 數組輸入到 PyTorch 中的神經網絡,您需要將numpy.array轉換為torch.Tensor 為此,您需要鍵入以下代碼。

input_tensor = torch.from_numpy(x)

在此之后,您的numpy.array將轉換為torch.Tensor

不要使用 numpy 從狄利克雷分布中采樣,而是使用 pytorch。 這是代碼:

y = torch.Tensor([[10,6,3]])
m = torch.distributions.dirichlet.Dirichlet(y)
z=m.sample()

gen = generator(3,3)
gen(z)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM