Change:
it'd be great to pick out elements of of a sequential with array slicing:
I agree this idea!
However,
this has come up again (last in ). Shall we reconsider?
I attempted getting a to work. Is this fine? If this is fine, I can issue a PR too. Let me know what you think. I believe this modification needs to be done in the class.
cc: This issue has been open for quite sometime.

Discussion:
self.assertEqual(n[1], l2) self.assertEqual(n[2], l3) self.assertEqual(n[3], l4) <ins> self.assertEqual(n[1:], nn.Sequential(l2, l3, l4)) self.assertEqual(n[3:], nn.Sequential(l4)) self.assertEqual(n[:-1], nn.Sequential(l1, l2, l3)) self.assertEqual(n[:-3], nn.Sequential(l1)) self.assertEqual(n[::-1], nn.Sequential(l4, l3, l2, l1)) </ins> def test_ModuleList(self): modules = [nn.ReLU(), nn.Linear(5, 5)]

modules[2] = nn.Conv2d(5, 3, 2) module_list[2] = modules[2] check() <ins> self.assertEqual(module_list[1:], nn.ModuleList(modules[1:])) self.assertEqual(module_list[3:], nn.ModuleList(modules[3:])) self.assertEqual(module_list[:-1], nn.ModuleList(modules[:-1])) self.assertEqual(module_list[:-3], nn.ModuleList(modules[:-3])) self.assertEqual(module_list[::-1], nn.ModuleList(modules[::-1])) </ins> with self.assertRaises(TypeError): module_list += nn.ReLU()

parameters[2] = make_param() param_list[2] = parameters[2] check() <ins> self.assertEqual(param_list[1:], nn.ParameterList(parameters[1:])) self.assertEqual(param_list[3:], nn.ParameterList(parameters[3:])) self.assertEqual(param_list[:-1], nn.ParameterList(parameters[:-1])) self.assertEqual(param_list[:-3], nn.ParameterList(parameters[:-3])) self.assertEqual(param_list[::-1], nn.ParameterList(parameters[::-1])) </ins> with self.assertRaises(TypeError): param_list += make_param()

self.add_module(str(idx), module) def __getitem__(self, idx): <del> if not (-len(self) <= idx < len(self)): raise IndexError('index {} is out of range'.format(idx)) if idx < 0: idx += len(self) it = iter(self._modules.values()) for i in range(idx): next(it) return next(it) </del> <ins> if isinstance(idx, slice): return Sequential(OrderedDict(list(self._modules.items())[idx])) else: if not (-len(self) <= idx < len(self)): raise IndexError('index {} is out of range'.format(idx)) if idx < 0: idx += len(self) it = iter(self._modules.values()) for i in range(idx): next(it) return next(it) </ins> def __len__(self): return len(self._modules)

self += modules def __getitem__(self, idx): <del> if not (-len(self) <= idx < len(self)): raise IndexError('index {} is out of range'.format(idx)) if idx < 0: idx += len(self) return self._modules[str(idx)] </del> <ins> if isinstance(idx, slice): return ModuleList(list(self._modules.values())[idx]) else: if not (-len(self) <= idx < len(self)): raise IndexError('index {} is out of range'.format(idx)) if idx < 0: idx += len(self) return self._modules[str(idx)] </ins> def __setitem__(self, idx, module): return setattr(self, str(idx), module)

self += parameters def __getitem__(self, idx): <del> if not (-len(self) <= idx < len(self)): raise IndexError('index {} is out of range'.format(idx)) if idx < 0: idx += len(self) return self._parameters[str(idx)] </del> <ins> if isinstance(idx, slice): return ParameterList(list(self._parameters.values())[idx]) else: if not (-len(self) <= idx < len(self)): raise IndexError('index {} is out of range'.format(idx)) if idx < 0: idx += len(self) return self._parameters[str(idx)] </ins> def __setitem__(self, idx, param): return self.register_parameter(str(idx), param)