• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

yeliudev / nncore / 5595037279

pending completion
5595037279

push

github

yeliudev
Add contrastive losses support

61 of 61 new or added lines in 4 files covered. (100.0%)

648 of 4026 relevant lines covered (16.1%)

3.13 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/nncore/nn/bundle/bundle.py
1
# Copyright (c) Ye Liu. Licensed under the MIT License.
2

3
from collections import OrderedDict
×
4

5
import torch
×
6
import torch.nn as nn
×
7

8
import nncore
×
9

10

11
class Sequential(nn.Sequential):
×
12
    """
13
    An :obj:`nn.Sequential` class that supports multiple inputs.
14
    """
15

16
    def __init__(self, *args):
×
17
        super(nn.Sequential, self).__init__()
×
18

19
        idx = 0
×
20
        for arg in args:
×
21
            if isinstance(arg, OrderedDict):
×
22
                for key, mod in arg.items():
×
23
                    if mod is not None:
×
24
                        self.add_module(key, mod)
×
25
                continue
×
26
            elif not isinstance(arg, (list, tuple)):
×
27
                arg = [arg]
×
28

29
            for mod in [a for a in arg if a is not None]:
×
30
                self.add_module(str(idx), mod)
×
31
                idx += 1
×
32

33
    def forward(self, *args, **kwargs):
×
34
        for mod in self:
×
35
            if isinstance(args, (list, tuple)):
×
36
                args = mod(*args, **kwargs)
×
37
            else:
38
                args = mod(args, **kwargs)
×
39
        return args
×
40

41

42
class ModuleList(nn.ModuleList):
×
43
    """
44
    An :obj:`nn.ModuleList` class that supports multiple inputs.
45
    """
46

47
    def __init__(self, *args):
×
48
        super(nn.ModuleList, self).__init__()
×
49
        self += [a for a in nncore.flatten(args) if a is not None]
×
50

51

52
class ModuleDict(nn.ModuleDict):
×
53
    """
54
    An :obj:`nn.ModuleDict` class that supports multiple inputs.
55
    """
56

57
    def __init__(self, *args, **kwargs):
×
58
        super(ModuleDict, self).__init__()
×
59
        for arg in args:
×
60
            self.update(arg)
×
61
        self.update(kwargs)
×
62

63

64
class Parameter(nn.Parameter):
×
65
    """
66
    An :obj:`nn.Parameter` class that supports multiple inputs initializes the
67
    parameters using a scaled normal distribution.
68
    """
69

70
    def __new__(cls, *args, requires_grad=True, **kwargs):
×
71
        if torch.is_tensor(args[0]):
×
72
            data = args[0]
×
73
        elif isinstance(args[0], float):
×
74
            data = torch.Tensor([args[0]])
×
75
        elif isinstance(args[0], (list, tuple)):
×
76
            data = torch.randn(args[0], **kwargs) / args[0][-1]**0.5
×
77
        else:
78
            data = torch.randn(args, **kwargs) / args[-1]**0.5
×
79

80
        return torch.Tensor._make_subclass(cls, data, requires_grad)
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc