or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

base-exceptions.mdcontrib.mddistributed.mdengine.mdhandlers.mdindex.mdmetrics.mdutils.md

base-exceptions.mddocs/

0

# Base Classes and Exceptions

1

2

Core base classes and exception types used throughout PyTorch Ignite. These provide fundamental functionality and error handling for the library's components.

3

4

## Capabilities

5

6

### Base Classes

7

8

Foundation classes that provide common functionality across Ignite components.

9

10

```python { .api }

11

class Serializable:

12

"""

13

Mixin class for objects that can be serialized and deserialized.

14

15

Provides state_dict() and load_state_dict() methods for saving and loading

16

object state, following PyTorch conventions.

17

"""

18

def state_dict(self):

19

"""

20

Get object state dictionary for serialization.

21

22

Returns:

23

dict: State dictionary containing object state

24

"""

25

26

def load_state_dict(self, state_dict):

27

"""

28

Load object state from state dictionary.

29

30

Parameters:

31

- state_dict: dictionary containing object state

32

"""

33

```

34

35

### Exceptions

36

37

Custom exception types for specific error conditions in PyTorch Ignite.

38

39

```python { .api }

40

class NotComputableError(RuntimeError):

41

"""

42

Raised when a metric cannot be computed due to insufficient data or invalid state.

43

44

This exception is typically raised by metrics when:

45

- No data has been accumulated yet

46

- Invalid data types or shapes are encountered

47

- Mathematical operations cannot be completed (e.g., division by zero)

48

"""

49

pass

50

```

51

52

## Usage Examples

53

54

### Using Serializable Mixin

55

56

```python

57

from ignite.base import Serializable

58

59

class CustomHandler(Serializable):

60

def __init__(self, param1, param2):

61

self.param1 = param1

62

self.param2 = param2

63

self.internal_state = 0

64

65

def state_dict(self):

66

return {

67

'param1': self.param1,

68

'param2': self.param2,

69

'internal_state': self.internal_state

70

}

71

72

def load_state_dict(self, state_dict):

73

self.param1 = state_dict['param1']

74

self.param2 = state_dict['param2']

75

self.internal_state = state_dict['internal_state']

76

77

# Save handler state

78

handler = CustomHandler("value1", 42)

79

state = handler.state_dict()

80

81

# Load handler state

82

new_handler = CustomHandler("", 0)

83

new_handler.load_state_dict(state)

84

```

85

86

### Handling NotComputableError

87

88

```python

89

from ignite.metrics import Accuracy

90

from ignite.exceptions import NotComputableError

91

92

# Create metric

93

accuracy = Accuracy()

94

95

try:

96

# Try to compute without any data

97

result = accuracy.compute()

98

except NotComputableError:

99

print("Cannot compute accuracy - no data accumulated yet")

100

101

# Add some data first

102

accuracy.update((torch.tensor([1, 0]), torch.tensor([1, 1])))

103

result = accuracy.compute() # Now it works

104

print(f"Accuracy: {result}")

105

```

106

107

## Types

108

109

```python { .api }

110

class SerializableStateDict:

111

"""Type hint for serializable state dictionaries."""

112

pass

113

```