我有一个关于 Tensorflow 如何定义它的线性层的快速(并且可能是愚蠢的)问题。在 PyTorch 中,线性(或密集)层被定义为 y = x A^T + b,其中 A 和 b 是线性层的权重矩阵和偏置向量(参见 here)。
但是,我无法准确找到 Tensorflow 的等效方程!它与 PyTorch 相同还是只是 y = x A + b ?
先感谢您!
最佳答案
如果我们将激活设置为 None
在keras
的致密层中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/