Bases: torch.nn.Module
Implementation of the bridge architecture described in
Gradually Vanishing Bridge for Adversarial Domain Adaptation.
Source code in pytorch_adapt\layers\model_with_bridge.py
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 | class ModelWithBridge(torch.nn.Module):
"""
Implementation of the bridge architecture described in
[Gradually Vanishing Bridge for Adversarial Domain Adaptation](https://arxiv.org/abs/2003.13183).
"""
def __init__(self, model: torch.nn.Module, bridge: torch.nn.Module = None):
"""
Arguments:
model: Any pytorch model.
bridge: A model which has the same input/output sizes as ```model```.
If ```None```, then the bridge is formed by copying ```model```,
and randomly reinitialization all its parameters.
"""
super().__init__()
self.model = model
if bridge is None:
bridge = c_f.reinit(copy.deepcopy(model))
self.bridge = bridge
def forward(
self, x: torch.Tensor, return_bridge: bool = False
) -> Union[torch.Tensor, Tuple[torch.Tensor, torch.Tensor]]:
"""
Arguments:
x: The input to both ```self.model``` and ```self.bridge```.
return_bridge: Whether or not to return the bridge output
in addition to the ```model - bridge``` output
Returns:
If ```return_bridge = False```, then return just ```model - bridge```.
If ```return_bridge = True```, then return a tuple of ```(model - bridge), bridge```
"""
y = self.model(x)
z = self.bridge(x)
output = y - z
if return_bridge:
return output, z
return output
|
__init__(model, bridge=None)
Parameters:
| Name |
Type |
Description |
Default |
model |
torch.nn.Module
|
Any pytorch model. |
required
|
bridge |
torch.nn.Module
|
A model which has the same input/output sizes as model.
If None, then the bridge is formed by copying model,
and randomly reinitialization all its parameters. |
None
|
Source code in pytorch_adapt\layers\model_with_bridge.py
15
16
17
18
19
20
21
22
23
24
25
26
27 | def __init__(self, model: torch.nn.Module, bridge: torch.nn.Module = None):
"""
Arguments:
model: Any pytorch model.
bridge: A model which has the same input/output sizes as ```model```.
If ```None```, then the bridge is formed by copying ```model```,
and randomly reinitialization all its parameters.
"""
super().__init__()
self.model = model
if bridge is None:
bridge = c_f.reinit(copy.deepcopy(model))
self.bridge = bridge
|
forward(x, return_bridge=False)
Parameters:
| Name |
Type |
Description |
Default |
x |
torch.Tensor
|
The input to both self.model and self.bridge. |
required
|
return_bridge |
bool
|
Whether or not to return the bridge output
in addition to the model - bridge output |
False
|
Returns:
| Type |
Description |
Union[torch.Tensor, Tuple[torch.Tensor, torch.Tensor]]
|
If return_bridge = False, then return just model - bridge.
If return_bridge = True, then return a tuple of (model - bridge), bridge |
Source code in pytorch_adapt\layers\model_with_bridge.py
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 | def forward(
self, x: torch.Tensor, return_bridge: bool = False
) -> Union[torch.Tensor, Tuple[torch.Tensor, torch.Tensor]]:
"""
Arguments:
x: The input to both ```self.model``` and ```self.bridge```.
return_bridge: Whether or not to return the bridge output
in addition to the ```model - bridge``` output
Returns:
If ```return_bridge = False```, then return just ```model - bridge```.
If ```return_bridge = True```, then return a tuple of ```(model - bridge), bridge```
"""
y = self.model(x)
z = self.bridge(x)
output = y - z
if return_bridge:
return output, z
return output
|