tensorflow - Tensorflow 的 tf.keras.layers.Dense 和 PyTorch 的 torch.nn.Linear 的区别?

标签 tensorflow pytorch

我有一个关于 Tensorflow 如何定义它的线性层的快速(并且可能是愚蠢的)问题。在 PyTorch 中,线性(或密集)层被定义为 y = x A^T + b,其中 A 和 b 是线性层的权重矩阵和偏置向量(参见 here)。
但是,我无法准确找到 Tensorflow 的等效方程!它与 PyTorch 相同还是只是 y = x A + b ?
先感谢您!

最佳答案

如果我们将激活设置为 Nonekeras的致密层中API,那么它们在技术上是等效的。
TensorFlow 的

tf.keras.layers.Dense(..., activation=None) 
根据doc , 更多学习 here .

activation: Activation function to use. If you don't specify anything, no activation is applied (ie. "linear" activation: a(x) = x).


在 PyTorch 的 src 中.
torch.nn.Linear
他们现在在这一点上是平等的。对传入数据的线性变换:y = x*W^T + b .请参阅下面这两个的更具体的等效实现。在 PyTorch , 我们的确是
class Network(torch.nn.Module):
    def __init__(self):
        super(Network, self).__init__()
        self.fc1 = torch.nn.Linear(5, 30)
    def forward(self, state):
        return self.fc1(state)
或者,
trd = torch.nn.Linear(in_features = 3, out_features = 30)
y = trd(torch.ones(5, 3))
print(y.size())
# torch.Size([5, 30])
相当于 tf实现将是
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Dense(30, input_shape=(5,), activation=None)) 
或者,
tfd = tf.keras.layers.Dense(30, input_shape=(3,), activation=None)
x = tfd(tf.ones(shape=(5, 3)))
print(x.shape)
# (5, 30)

关于tensorflow - Tensorflow 的 tf.keras.layers.Dense 和 PyTorch 的 torch.nn.Linear 的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66626700/

相关文章:

python - pytorch运行时错误: CUDA error: device-side assert triggered

python - 在 Pytorch 卷积神经网络中展平张量(大小不匹配错误)

machine-learning - model_dir 在 TensorFlow 中指什么?

python - 卡住神经网络的权重,使其输出在特定点取特定值( tensorflow )

python - PyTorch : How to apply the same random transformation to multiple image?

python - 使用 Pytorch 实现 FFT

python - 是否可以只加载 TensorFlow 数据集的一部分?

python - TensorFlow ValueError : Cannot feed value of shape (64, 64, 3) for Tensor u'Placeholder : 0', which has shape ' (? , 64, 64, 3)'

python - tf.data.Dataset - map() 和 cache() 方法的行为

deep-learning - a.sub(or*a.grad) 实际上做了什么?