|
28 | 28 |
|
29 | 29 | @dataclasses.dataclass |
30 | 30 | class Version: |
31 | | - """The information about verions of a model.""" |
32 | | - # The name of the version. |
| 31 | + """The information about verions of a model. |
| 32 | +
|
| 33 | + If there are multiple versions of the model, or there may be in the future, |
| 34 | + it’s useful for your audience to know which version of the model is discussed |
| 35 | + in the Model Card. If there are previous versions of this model, briefly |
| 36 | + describe how this version is different. If no more than one version of the |
| 37 | + model will be released, this field may be omitted. |
| 38 | +
|
| 39 | + ## Attributes |
| 40 | +
|
| 41 | + * `name`: The name of the version. |
| 42 | + * `date`: The date this version was released. |
| 43 | + * `diff`: The changes from the previous version. |
| 44 | + """ |
33 | 45 | name: Optional[Text] = None |
34 | | - # The date the version was released. |
35 | 46 | date: Optional[Text] = None |
36 | | - # The changes from the previous version. |
37 | 47 | diff: Optional[Text] = None |
38 | 48 |
|
39 | 49 |
|
40 | 50 | @dataclasses.dataclass |
41 | 51 | class Owner: |
42 | | - """The information about owners of a model.""" |
43 | | - # The name of the owner. |
| 52 | + """The information about owners of a model. |
| 53 | +
|
| 54 | + ## Attributes |
| 55 | +
|
| 56 | + * `name`: The name of the model owner. |
| 57 | + * `contact`: The contact information for the model owner or owners. These |
| 58 | + could be individual email addresses, a team mailing list expressly, or a |
| 59 | + monitored feedback form. |
| 60 | + """ |
44 | 61 | name: Optional[Text] = None |
45 | | - # The contact information of the owner. |
46 | 62 | contact: Optional[Text] = None |
47 | 63 |
|
48 | 64 |
|
49 | 65 | @dataclasses.dataclass |
50 | 66 | class ModelDetails: |
51 | | - """Metadata about the model.""" |
52 | | - # The name of the model. |
| 67 | + """This section provides a general, high-level description of the model. |
| 68 | +
|
| 69 | + ## Attributes |
| 70 | +
|
| 71 | + * `name`: The name of the model. |
| 72 | + * `overview`: A description of the model card. |
| 73 | + * `owners`: The individuals or teams who own the model. |
| 74 | + * `version`: The version of the model. If there are previous versions of this |
| 75 | + model, briefly describe how this version is different. |
| 76 | + * `license`: The license information for the model. If the model is licensed |
| 77 | + for use by others, include the license type. If the model is not licensed for |
| 78 | + future use, you may state that here as well. |
| 79 | + * `references`: Provide any additional links the reader may need. You can |
| 80 | + link to foundational research, technical documentation, or other materials |
| 81 | + that may be useful to your audience. |
| 82 | + * `citation`: How should the model be cited? If the model is based on |
| 83 | + published academic research, cite the research. |
| 84 | + """ |
53 | 85 | name: Optional[Text] = None |
54 | | - # A description of the model card. |
55 | 86 | overview: Optional[Text] = None |
56 | | - # The individuals or teams who own the model. |
57 | 87 | owners: List[Owner] = dataclasses.field(default_factory=list) |
58 | | - # The version of the model. |
59 | | - version: Version = dataclasses.field(default_factory=Version) |
60 | | - # The model's license for use. |
| 88 | + version: Optional[Version] = dataclasses.field(default_factory=Version) |
61 | 89 | license: Optional[Text] = None |
62 | | - # Links providing more information about the model. |
63 | 90 | references: List[Text] = dataclasses.field(default_factory=list) |
64 | | - # How to reference this model card. |
65 | 91 | citation: Optional[Text] = None |
66 | 92 |
|
67 | 93 |
|
68 | 94 | @dataclasses.dataclass |
69 | 95 | class Graphic: |
70 | | - """A named inline plot.""" |
71 | | - # The name of the graphic. |
| 96 | + """A named inline plot. |
| 97 | +
|
| 98 | + ## Attributes |
| 99 | +
|
| 100 | + * `name`: The name of the graphic. |
| 101 | + * `image`: The image string encoded as a base64 string. |
| 102 | + """ |
72 | 103 | name: Text |
73 | | - # The image string encoded as a base64 string. |
74 | 104 | image: Text |
75 | 105 |
|
76 | 106 |
|
77 | 107 | @dataclasses.dataclass |
78 | 108 | class Graphics: |
79 | | - """A collection of graphics.""" |
80 | | - # A description of this collection of graphics. |
| 109 | + """A collection of graphics. |
| 110 | +
|
| 111 | + Each ```graphic``` in the ```collection``` field has both a ```name``` and |
| 112 | + an ```image```. For instance, you might want to display a graph showing the |
| 113 | + number of examples belonging to each class in your training dataset: |
| 114 | +
|
| 115 | + ```python |
| 116 | +
|
| 117 | + model_card.model_parameters.data.train.graphics.collection = [ |
| 118 | + {'name': 'Training Set Size', 'image': training_set_size_barchart}, |
| 119 | + ] |
| 120 | + ``` |
| 121 | +
|
| 122 | + Then, provide a description of the graph: |
| 123 | +
|
| 124 | + ```python |
| 125 | +
|
| 126 | + model_card.model_parameters.data.train.graphics.description = ( |
| 127 | + 'This graph displays the number of examples belonging to each class ', |
| 128 | + 'in the training dataset. ') |
| 129 | + ``` |
| 130 | +
|
| 131 | + ## Attributes |
| 132 | +
|
| 133 | + * `description`: The name of the dataset. |
| 134 | + * `collection`: A collection of graphics. |
| 135 | + """ |
81 | 136 | description: Optional[Text] = None |
82 | | - # A collection of graphics. |
83 | 137 | collection: List[Graphic] = dataclasses.field(default_factory=list) |
84 | 138 |
|
85 | 139 |
|
86 | 140 | @dataclasses.dataclass |
87 | 141 | class Dataset: |
88 | | - """The information about a dataset used to generate a model.""" |
89 | | - # The name of the dataset. |
| 142 | + """Provide some information about a dataset used to generate a model. |
| 143 | +
|
| 144 | + ## Attributes |
| 145 | +
|
| 146 | + * `name`: The name of the dataset. |
| 147 | + * `link`: A link to the dataset. |
| 148 | + * `sensitive`: Does this dataset contain human or other sensitive data? |
| 149 | + * `graphics`: Visualizations of the dataset. |
| 150 | + """ |
90 | 151 | name: Optional[Text] = None |
91 | | - # The contact information of the owner |
92 | 152 | link: Optional[Text] = None |
93 | | - # Does this dataset contain human or other sensitive data? |
94 | 153 | sensitive: Optional[bool] = None |
95 | | - # Visualizations of the dataset. |
96 | 154 | graphics: Graphics = dataclasses.field(default_factory=Graphics) |
97 | 155 |
|
98 | 156 |
|
99 | 157 | @dataclasses.dataclass |
100 | 158 | class Data: |
101 | | - """The related datasets used to train and evaluate the model.""" |
102 | | - # The training dataset |
| 159 | + """The related datasets used to train and evaluate the model. |
| 160 | +
|
| 161 | + ## Attributes |
| 162 | +
|
| 163 | + * `train`: The training dataset |
| 164 | + * `eval`: The evaluation dataset |
| 165 | + """ |
103 | 166 | train: Dataset = dataclasses.field(default_factory=Dataset) |
104 | | - # The evaluation dataset |
105 | 167 | eval: Dataset = dataclasses.field(default_factory=Dataset) |
106 | 168 |
|
107 | 169 |
|
108 | 170 | @dataclasses.dataclass |
109 | 171 | class ModelParameters: |
110 | | - """Parameters for construction of the model.""" |
111 | | - # The architecture of the model. |
| 172 | + """Parameters for construction of the model. |
| 173 | +
|
| 174 | + ## Attributes |
| 175 | +
|
| 176 | + * `model_architecture`: specifies the architecture of your model. |
| 177 | + * `data`: specifies the datasets used to train and evaluate your model. |
| 178 | + * `input_format`: describes the data format for inputs to your model. |
| 179 | + * `output_format`: describes the data format for outputs from your model. |
| 180 | + """ |
112 | 181 | model_architecture: Optional[Text] = None |
113 | | - # The datasets used to train and evaluate the model. |
114 | 182 | data: Data = dataclasses.field(default_factory=Data) |
115 | | - # The data format for inputs to the model. |
116 | 183 | input_format: Optional[Text] = None |
117 | | - # The data format for outputs from the model. |
118 | 184 | output_format: Optional[Text] = None |
119 | 185 |
|
120 | 186 |
|
121 | 187 | @dataclasses.dataclass |
122 | 188 | class ConfidenceInterval: |
123 | | - """The confidence interval of the metric.""" |
124 | | - # The lower bound of the confidence interval. |
| 189 | + """The confidence interval of the metric. |
| 190 | +
|
| 191 | + ## Attributes |
| 192 | +
|
| 193 | + * `lower_bound`: The lower bound of the confidence interval. |
| 194 | + * `upper_bound`: The upper bound of the confidence interval. |
| 195 | + """ |
125 | 196 | lower_bound: float |
126 | | - # The upper bound of the confidence interval. |
127 | 197 | upper_bound: float |
128 | 198 |
|
129 | 199 |
|
130 | 200 | @dataclasses.dataclass |
131 | 201 | class PerformanceMetric: |
132 | | - """The details of the performance metric.""" |
133 | | - # The type of performance metric. |
| 202 | + """The details of the performance metric. |
| 203 | +
|
| 204 | + ## Attributes |
| 205 | +
|
| 206 | + * `type`: What performance metric are you reporting on? |
| 207 | + * `value`: What is the value of this performance metric? |
| 208 | + * `confidence_interval`: What is the confidence interval for this |
| 209 | + performance metric's value? |
| 210 | + * `threshold`: At what threshold was this metric computed? |
| 211 | + * `slice`: What slice of your data was this metric computed on? |
| 212 | + """ |
134 | 213 | type: Text |
135 | | - # The value of the performance metric. |
136 | 214 | value: Union[int, float, Text] |
137 | | - # The confidence interval of the metric. |
138 | 215 | confidence_interval: Optional[ConfidenceInterval] = None |
139 | | - # The decision threshold the metric was computed on. |
140 | 216 | threshold: Optional[float] = None |
141 | | - # The name of the slice this metric was computed on. |
142 | 217 | slice: Optional[Text] = None |
143 | 218 |
|
144 | 219 |
|
145 | 220 | @dataclasses.dataclass |
146 | 221 | class QuantitativeAnalysis: |
147 | | - """The quantitative analysis of a model.""" |
148 | | - # The model performance metrics being reported. |
| 222 | + """The quantitative analysis of a model. |
| 223 | +
|
| 224 | + Identify relevant performance metrics and display values. Let’s say you’re |
| 225 | + interested in displaying the accuracy and false positive rate (FPR) of a |
| 226 | + cat vs. dog classification model. Assuming you have already computed both |
| 227 | + metrics, both overall and per-class, you can specify metrics like so: |
| 228 | +
|
| 229 | + ```python |
| 230 | + model_card.quantitative_analysis.performance_metrics = [ |
| 231 | + {'type': 'accuracy', 'value': computed_accuracy}, |
| 232 | + {'type': 'accuracy', 'value': cat_accuracy, 'slice': 'cat'}, |
| 233 | + {'type': 'accuracy', 'value': dog_accuracy, 'slice': 'dog'}, |
| 234 | + {'type': 'fpr', 'value': computed_fpr}, |
| 235 | + {'type': 'fpr', 'value': cat_fpr, 'slice': 'cat'}, |
| 236 | + {'type': 'fpr', 'value': dog_fpr, 'slice': 'dog'}, |
| 237 | + ] |
| 238 | + ``` |
| 239 | +
|
| 240 | + ## Attributes |
| 241 | +
|
| 242 | + * `performance_metrics`: The performance metrics being reported. |
| 243 | + * `graphics`: A collection of visualizations of model performance. |
| 244 | + """ |
149 | 245 | performance_metrics: List[PerformanceMetric] = dataclasses.field( |
150 | 246 | default_factory=list) |
151 | | - # Visualizations of model performance. |
152 | 247 | graphics: Graphics = dataclasses.field(default_factory=Graphics) |
153 | 248 |
|
154 | 249 |
|
155 | 250 | @dataclasses.dataclass |
156 | 251 | class Risk: |
157 | | - """The information about risks when using the model.""" |
158 | | - # The name of the risk. |
| 252 | + """Information about risks involved when using the model. |
| 253 | +
|
| 254 | + ## Attributes |
| 255 | +
|
| 256 | + * `name`: The name of the risk. |
| 257 | + * `mitigation_strategy`: A mitigation strategy that you've implemented, or |
| 258 | + one that you suggest to users. |
| 259 | + """ |
159 | 260 | name: Text |
160 | | - # Strategy used to address this risk. |
161 | 261 | mitigation_strategy: Text |
162 | 262 |
|
163 | 263 |
|
164 | 264 | @dataclasses.dataclass |
165 | 265 | class Considerations: |
166 | | - """Considerations related to model construction, training, and application.""" |
167 | | - # Who are the intended users of the model? |
| 266 | + """Considerations related to model construction, training, and application. |
| 267 | +
|
| 268 | + The considerations section includes qualitative information about your model, |
| 269 | + including some analysis of its risks and limitations. As such, this section |
| 270 | + usually requires careful consideration, and conversations with many relevant |
| 271 | + stakeholders, including other model developers, dataset producers, and |
| 272 | + downstream users likely to interact with your model, or be affected by its |
| 273 | + outputs. |
| 274 | +
|
| 275 | + ## Attributes |
| 276 | +
|
| 277 | + * `users`: Who are the intended users of the model? This may include |
| 278 | + researchers, developers, and/or clients. You might also include information |
| 279 | + about the downstream users you expect to interact with your model. |
| 280 | + * `user_cases`: What are the intended use cases of the model? What use cases |
| 281 | + are out-of-scope? |
| 282 | + * `limitations`: What are the known limitations of the model? This may |
| 283 | + include technical limitations, or conditions that may degrade model |
| 284 | + performance. |
| 285 | + * `tradeoffs`: What are the known accuracy/performance tradeoffs for the |
| 286 | + model? |
| 287 | + * `ethical_considerations`: What are the ethical risks involved in |
| 288 | + application of this model? For each risk, you may also provide a mitigation |
| 289 | + strategy that you've implemented, or one that you suggest to users. |
| 290 | + """ |
168 | 291 | users: List[Text] = dataclasses.field(default_factory=list) |
169 | | - # What are the intended use cases of the model. |
170 | 292 | use_cases: List[Text] = dataclasses.field(default_factory=list) |
171 | | - # What are the known technical limitations of the model. |
172 | 293 | limitations: List[Text] = dataclasses.field(default_factory=list) |
173 | | - # What are the known tradeoffs in accuracy/performance of the model |
174 | 294 | tradeoffs: List[Text] = dataclasses.field(default_factory=list) |
175 | | - # What are the ethical risks involved in the application of this model. |
176 | 295 | ethical_considerations: List[Risk] = dataclasses.field(default_factory=list) |
177 | 296 |
|
178 | 297 |
|
179 | 298 | @dataclasses.dataclass |
180 | 299 | class ModelCard: |
181 | | - """Fields used to generate the Model Card.""" |
182 | | - # The json schema version of the ModelCard |
| 300 | + """Fields used to generate the Model Card. |
| 301 | +
|
| 302 | + ## Attributes |
| 303 | +
|
| 304 | + * `schema_version`: The Model Card JSON schema version. |
| 305 | + * `model_details`: Descriptive metadata for the model. |
| 306 | + * `model_parameters`: Technical metadata for the model. |
| 307 | + * `quantitative_analysis`: Quantitative analysis of model performance. |
| 308 | + * `considerations`: Any considerations related to model construction, |
| 309 | + training, and application. |
| 310 | + """ |
183 | 311 | schema_version: Optional[Text] = None |
184 | | - # Descriptive metadata for the model. |
185 | 312 | model_details: ModelDetails = dataclasses.field(default=ModelDetails()) |
186 | | - # Parameters used when generating the model. |
187 | 313 | model_parameters: ModelParameters = dataclasses.field( |
188 | 314 | default_factory=ModelParameters) |
189 | | - # The quantitative analysis of the ModelCard |
190 | 315 | quantitative_analysis: QuantitativeAnalysis = dataclasses.field( |
191 | 316 | default_factory=QuantitativeAnalysis) |
192 | | - # The considerations related to model construction, training, and application. |
193 | 317 | considerations: Considerations = dataclasses.field( |
194 | 318 | default_factory=Considerations) |
195 | 319 |
|
196 | 320 | def to_dict(self) -> Dict[Text, Any]: |
| 321 | + """Convert your model card to a python dictionary.""" |
197 | 322 | # ignore None properties recusively to allow missing values. |
198 | 323 | ignore_none = lambda properties: {k: v for k, v in properties if v} |
199 | 324 | return dataclasses.asdict(self, dict_factory=ignore_none) |
200 | 325 |
|
201 | 326 | def to_json(self) -> Text: |
| 327 | + """Convert your model card to json.""" |
202 | 328 | return json.dumps(self.to_dict(), indent=2) |
0 commit comments