What is the equivalent of tf.get_variable() in Pytorch?

I come from tensorflow and I know that tf.get_variable() (https://www.tensorflow.org/programmers_guide/variable_scope) is very useful because then I don’t have to have giant list of things were I define variables and instead they are included in the graph as I create more variables. i.e. I avoid:

D,H_l1 = 2,1
w_init = torch.zeros(H_l1,D).type(dtype)
W_l1 = Variable(w_init, requires_grad=True)

D,H_l2 = 2,1
w_init = torch.zeros(H_l2,D).type(dtype)
W_l2 = Variable(w_init, requires_grad=True)

what is the equivalent way to deal with this in pytorch?

Variable in tensorflow and variable in pytorch are different things. Variable in tensorflow is setting up a tensor and giving name and such.

When you declare a Variable in pytorch u already have a tensor made of set shape and you are just wrapping that tensor with pytorch wrapper so now you can auto compute gradients easily when backproping

but sometimes the networks are gigantic (resnet, vgg or something crazy new I want to try with LSTMs…maybe), do I have to really write 100 variables out in one script? Or maybe Im thinking about pytorch totally wrong but bare with me Im new to this :slight_smile:

Not at all

The main utility of a dynamic computation graph is that it allows you to process complex inputs and outputs, without worrying to convert every batch of input into a tensor. Tensorflow is define and run and as such write all placeholders beforehand. Pytorch define by run so graphs are created as you go

I would suggest not trying to compare tensorflow to pytorch. I assume you have built neural networks from scratch before and if so use that knowledge of how to, to how that can be done in pytorch.

The tutorials here will help a lot too:


I hope that helps:grin: