Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Tensorflow线性层的Pyrotch等效物_Tensorflow_Pytorch - Fatal编程技术网

Tensorflow线性层的Pyrotch等效物

Tensorflow线性层的Pyrotch等效物,tensorflow,pytorch,Tensorflow,Pytorch,我试图使用PyTorch框架重新实现Tensorflow代码。下面我介绍了TF示例代码和我的PyT解释 TensorFlow实现: W1 = tf.Variable(xavier_init([135, 128])) b1 = tf.Variable(tf.zeros(shape=[128])) def fcn(x): z = tf.reshape(x, (-1, 135)) out1 = leaky_relu( tf.matmul(z, W1) + b1 ) retu

我试图使用PyTorch框架重新实现Tensorflow代码。下面我介绍了TF示例代码和我的PyT解释

TensorFlow实现:

W1 = tf.Variable(xavier_init([135, 128]))
b1 = tf.Variable(tf.zeros(shape=[128]))

def fcn(x):
    z = tf.reshape(x, (-1, 135))
    out1 = leaky_relu( tf.matmul(z, W1) + b1 )

    return out1
class decoder(nn.Module):
    def __init__(self):
        super(decoder, self).__init__()

        self.layer_10 = nn.Linear(135, 128, bias=True)
        self.leaky = nn.LeakyReLU(0.2, inplace=False)
        init.xavier_uniform(self.layer_10.weight) 

    def forward(self, x):
        z = x.view(-1, 135)
        h30 = self.leaky(self.layer_10(z))

        return h30
PyTorch实施:

W1 = tf.Variable(xavier_init([135, 128]))
b1 = tf.Variable(tf.zeros(shape=[128]))

def fcn(x):
    z = tf.reshape(x, (-1, 135))
    out1 = leaky_relu( tf.matmul(z, W1) + b1 )

    return out1
class decoder(nn.Module):
    def __init__(self):
        super(decoder, self).__init__()

        self.layer_10 = nn.Linear(135, 128, bias=True)
        self.leaky = nn.LeakyReLU(0.2, inplace=False)
        init.xavier_uniform(self.layer_10.weight) 

    def forward(self, x):
        z = x.view(-1, 135)
        h30 = self.leaky(self.layer_10(z))

        return h30
我想知道什么是实现matmul部分的正确方法,因为pytorch中的权重没有明确定义为TF中的权重,如果我错了,请纠正我

不需要显式调用:它在nn.Linear层的forward方法的实现中。通过调用self.layer_10z,您实际上是在幕后调用forward方法,该方法执行矩阵乘法并为您添加偏差

如果希望代码完全相同,可能需要使用相同的方法显式初始化权重。为此,您有实现各种权重初始化的nn.init。具体地说,您可能会发现它是相关的。

您不需要显式调用:它是在nn.Linear层的forward方法的实现中实现的。通过调用self.layer_10z,您实际上是在幕后调用forward方法,该方法执行矩阵乘法并为您添加偏差


如果希望代码完全相同,可能需要使用相同的方法显式初始化权重。为此,您有实现各种权重初始化的nn.init。具体来说,您可能会发现相关问题。

谢谢您的回答,权重初始化现在看起来好吗?@Blade为此,您需要了解xavier_init在tensor flow中的作用,并查看它是否匹配pytorch的默认值。我记不清确切的行为。谢谢。作为未来读者的参考,这就是中的第二个示例:itThanks是否需要答案,权重初始化现在看起来好吗?@Blade为此,您需要了解xavier_init在tensor flow中的作用,并查看它是否匹配pytorch的默认值。我记不清确切的行为。谢谢。作为未来读者的参考,这是第二个例子:DoIt