Hi Samuel!



 Samue1:

My version is 1.9.0+cpu. Any idea why the results are different


Apparently there has been a change in how Sequentials (and
presumably other Modules) are stored sometime between my
prehistoric 0.3.0 version and the modern era.
Here is a script that illustrates the difference:
import torch
print (torch.__version__)

seq = torch.nn.Sequential (torch.nn.Linear (2, 3), torch.nn.ReLU())
print ('seq = ...')
print (seq)
print ('dir (seq) = ...')
print (dir (seq))

Here is its output for version 0.3.0:
0.3.0b0+591e73e
seq = ...
Sequential(
  (0): Linear(in_features=2, out_features=3)
  (1): ReLU()
)
dir (seq) = ...
['0', '1', '__call__', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattr__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__len__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_all_buffers', '_apply', '_backend', '_backward_hooks', '_buffers', '_forward_hooks', '_forward_pre_hooks', '_modules', '_parameters', 'add_module', 'apply', 'children', 'cpu', 'cuda', 'double', 'dump_patches', 'eval', 'float', 'forward', 'half', 'load_state_dict', 'modules', 'named_children', 'named_modules', 'named_parameters', 'parameters', 'register_backward_hook', 'register_buffer', 'register_forward_hook', 'register_forward_pre_hook', 'register_parameter', 'share_memory', 'state_dict', 'train', 'training', 'type', 'zero_grad']

Note that it contains dir() entries for its submodules, namely ‘0’ and ‘1’.
Here is the script output for a reasonably up-to-date 1.7.1 version:
1.7.1
seq = ...
Sequential(
  (0): Linear(in_features=2, out_features=3, bias=True)
  (1): ReLU()
)
dir (seq) = ...
['T_destination', '__annotations__', '__call__', '__class__', '__delattr__', '__delitem__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattr__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_apply', '_backward_hooks', '_buffers', '_call_impl', '_forward_hooks', '_forward_pre_hooks', '_get_item_by_idx', '_get_name', '_load_from_state_dict', '_load_state_dict_pre_hooks', '_modules', '_named_members', '_non_persistent_buffers_set', '_parameters', '_register_load_state_dict_pre_hook', '_register_state_dict_hook', '_replicate_for_data_parallel', '_save_to_state_dict', '_slow_forward', '_state_dict_hooks', '_version', 'add_module', 'apply', 'bfloat16', 'buffers', 'children', 'cpu', 'cuda', 'double', 'dump_patches', 'eval', 'extra_repr', 'float', 'forward', 'half', 'load_state_dict', 'modules', 'named_buffers', 'named_children', 'named_modules', 'named_parameters', 'parameters', 'register_backward_hook', 'register_buffer', 'register_forward_hook', 'register_forward_pre_hook', 'register_parameter', 'requires_grad_', 'share_memory', 'state_dict', 'to', 'train', 'training', 'type', 'zero_grad']

Note that the explicit submodule dir() entries no longer appear.


what I can do the get the same result you got?


To get the same result as me, run with a prehistoric pytorch version.
More usefully:
The idea of iterating over the attributes returned by dir() strikes me
as asking for trouble.
I would imagine that working with the official pytorch api, rather than
rummaging around in dir(), would be a more robust solution.  I would
suggest iterating over  .modules() or .children() or something.
Best.
K. Frank