Hi everyone,
I am trying to convert the following code in TensorFlow to PyTorch. But I couldn’t get the point of how to write the placeholders and tf.variable_scope() in PyTorch. Is there any equivalent function for those?
class Dense(Layer):
“”“Dense layer.”""
def init(self, input_dim, output_dim, placeholders, dropout=0., sparse_inputs=False,
act=nn.ReLU(), bias=False, featureless=False, **kwargs):
super(Dense, self).init(**kwargs)
if dropout:
self.dropout = placeholders['dropout']
else:
self.dropout = 0.
self.act = act
self.sparse_inputs = sparse_inputs
self.featureless = featureless
self.bias = bias
# helper variable for sparse dropout
self.num_features_nonzero = placeholders['num_features_nonzero']
with tf.variable_scope(self.name + '_vars'):
self.vars['weights'] = glorot([input_dim, output_dim],
name='weights')
if self.bias:
self.vars['bias'] = zeros([output_dim], name='bias')
if self.logging:
self._log_vars()
def _call(self, inputs):
x = inputs
# # dropout
if self.sparse_inputs:
x = SparseDropout(x, 1-self.dropout, self.num_features_nonzero)
else:
x = nn.Dropout(x, 1-self.dropout)
# transform
output = dot(x, self.vars['weights'], sparse=self.sparse_inputs)
# bias
if self.bias:
output += self.vars['bias']
return self.act(output)