gpt4 book ai didi

python - PyTorch:传递 numpy 数组以进行权重初始化

转载 作者:行者123 更新时间:2023-12-04 12:16:18 24 4
gpt4 key购买 nike

我想用 np 数组初始化 RNN 的参数。

在下面的例子中,我想通过 wrnn的参数.我知道 pytorch 提供了许多初始化方法,如 Xavier、uniform 等,但是有没有办法通过传递 numpy 数组来初始化参数?

import numpy as np
import torch as nn
rng = np.random.RandomState(313)
w = rng.randn(input_size, hidden_size).astype(np.float32)

rnn = nn.RNN(input_size, hidden_size, num_layers)

最佳答案

首先,让我们注意 nn.RNN有多个权重变量,c.f. documentation :

Variables:

  • weight_ih_l[k] – the learnable input-hidden weights of the k-th layer, of shape (hidden_size * input_size) for k = 0. Otherwise, the shape is (hidden_size * hidden_size)
  • weight_hh_l[k] – the learnable hidden-hidden weights of the k-th layer, of shape (hidden_size * hidden_size)
  • bias_ih_l[k] – the learnable input-hidden bias of the k-th layer, of shape (hidden_size)
  • bias_hh_l[k] – the learnable hidden-hidden bias of the k-th layer, of shape (hidden_size)


现在,这些变量中的每一个( Parameter 实例)都是您的 nn.RNN 的属性。实例。您可以通过两种方式访问​​和编辑它们,如下所示:
  • 方案一:访问所有RNN Parameter按名称的属性( rnn.weight_hh_lKrnn.weight_ih_lK 等):

  • import torch
    from torch import nn
    import numpy as np

    input_size, hidden_size, num_layers = 3, 4, 2
    use_bias = True
    rng = np.random.RandomState(313)

    rnn = nn.RNN(input_size, hidden_size, num_layers, bias=use_bias)

    def set_nn_parameter_data(layer, parameter_name, new_data):
    param = getattr(layer, parameter_name)
    param.data = new_data

    for i in range(num_layers):
    weights_hh_layer_i = rng.randn(hidden_size, hidden_size).astype(np.float32)
    weights_ih_layer_i = rng.randn(hidden_size, hidden_size).astype(np.float32)
    set_nn_parameter_data(rnn, "weight_hh_l{}".format(i),
    torch.from_numpy(weights_hh_layer_i))
    set_nn_parameter_data(rnn, "weight_ih_l{}".format(i),
    torch.from_numpy(weights_ih_layer_i))

    if use_bias:
    bias_hh_layer_i = rng.randn(hidden_size).astype(np.float32)
    bias_ih_layer_i = rng.randn(hidden_size).astype(np.float32)
    set_nn_parameter_data(rnn, "bias_hh_l{}".format(i),
    torch.from_numpy(bias_hh_layer_i))
    set_nn_parameter_data(rnn, "bias_ih_l{}".format(i),
    torch.from_numpy(bias_ih_layer_i))
  • 方案二:访问所有RNN Parameter属性通过 rnn.all_weights列表属性:

  • import torch
    from torch import nn
    import numpy as np

    input_size, hidden_size, num_layers = 3, 4, 2
    use_bias = True
    rng = np.random.RandomState(313)

    rnn = nn.RNN(input_size, hidden_size, num_layers, bias=use_bias)

    for i in range(num_layers):
    weights_hh_layer_i = rng.randn(hidden_size, hidden_size).astype(np.float32)
    weights_ih_layer_i = rng.randn(hidden_size, hidden_size).astype(np.float32)
    rnn.all_weights[i][0].data = torch.from_numpy(weights_ih_layer_i)
    rnn.all_weights[i][1].data = torch.from_numpy(weights_hh_layer_i)

    if use_bias:
    bias_hh_layer_i = rng.randn(hidden_size).astype(np.float32)
    bias_ih_layer_i = rng.randn(hidden_size).astype(np.float32)
    rnn.all_weights[i][2].data = torch.from_numpy(bias_ih_layer_i)
    rnn.all_weights[i][3].data = torch.from_numpy(bias_hh_layer_i)

    关于python - PyTorch:传递 numpy 数组以进行权重初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51628607/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com