AI and ML with TensorFlow & PyTorch

Chapter 14
Responsible AI, Security and Governance

Bachelors Program in AI & ML
AI and ML with TensorFlow & PyTorch   •   Bachelor

Responsible AI, Security and Governance

Introduction and Context

Artificial intelligence systems increasingly influence employment, education, healthcare, finance, public services, transportation, and communication. Their benefits arise from automation, prediction, personalization, and decision support. Their risks arise when systems are inaccurate, discriminatory, insecure, opaque, invasive, inaccessible, or deployed without meaningful human accountability.

Responsible AI is the systematic design, development, deployment, and monitoring of AI so that it is lawful, ethical, safe, technically reliable, socially beneficial, and accountable. It is not a final compliance check. Responsibility must be considered throughout the AI life cycle: problem formulation, data collection, model development, evaluation, deployment, monitoring, retirement, and post-incident learning.

For AI and machine-learning practitioners using TensorFlow or PyTorch, responsible practice includes more than selecting an architecture and optimizing a loss function. It involves defining legitimate objectives, understanding data rights, testing performance across groups and conditions, protecting models and information, documenting decisions, and establishing human oversight.

Learning Objectives

By the end of this chapter, students should be able to:

1. Explain fairness, explainability, privacy, robustness, accessibility, and environmental responsibility in AI.

2. Distinguish common sources of bias and select suitable fairness metrics.

3. Describe adversarial threats and practical security controls.

4. Apply documentation, human-oversight, audit, and risk-management practices.

5. Relate AI development to data rights and major governance frameworks.

6. Analyze a responsible-AI case and recommend proportionate controls.

1. A Life-Cycle View of Responsible AI

A useful governance model divides an AI system into six stages:

1. Problem definition: Identify the decision, affected people, legitimate purpose, potential harms, and whether AI is necessary.

2. Data and design: Establish provenance, consent or another lawful basis, representativeness, labeling quality, retention rules, and security requirements.

3. Development: Select models, document assumptions, test performance and fairness, and apply privacy and security controls.

4. Validation: Conduct independent testing, subgroup analysis, robustness testing, usability assessment, and, where appropriate, impact assessment.

5. Deployment: Define authorized uses, human review, user notice, appeal routes, access controls, and monitoring.

6. Retirement and learning: Delete or archive data appropriately, revoke access, communicate system withdrawal, investigate incidents, and update controls.

This life-cycle perspective prevents a common error: treating a biased or harmful problem definition as if it could be repaired only by changing model weights.

Important Terminology

  • Stakeholder: A person or organization affected by, operating, procuring, or regulating an AI system.
  • Affected group: A population that may experience different benefits, burdens, or risks from a system.
  • Model card: Documentation describing a model’s intended use, limitations, evaluation, and risks.
  • Data sheet: Documentation describing a dataset’s motivation, composition, collection, processing, and recommended uses.
  • Impact assessment: A structured analysis of likely effects, risks, mitigations, and residual risk.
  • Residual risk: Risk remaining after controls are applied.
  • Human-in-the-loop: A process in which a person participates in a decision or operation.
  • Human-on-the-loop: A process in which a person supervises an automated system and can intervene.
  • Auditability: The ability to reconstruct, inspect, and evaluate system decisions and operations.

2. Fairness and Bias

Fairness asks whether an AI system distributes errors, opportunities, and treatment justly. Bias can enter at any stage:

  • Historical bias: Existing social inequalities are reproduced in data.
  • Representation bias: Some populations are missing or underrepresented.
  • Measurement bias: A proxy, label, or feature measures a construct differently across groups.
  • Aggregation bias: One model is applied to populations requiring different relationships between features and outcomes.
  • Evaluation bias: A benchmark does not reflect real deployment conditions.
  • Deployment bias: The system is used differently from its intended context.
  • Automation bias: Users over-trust machine recommendations.

Fairness is context-dependent. A medical triage system, a hiring tool, and a content moderation model may require different definitions of harm and fairness. No single metric can resolve every normative conflict.

Common Fairness Metrics

Let \(Y\) be the actual outcome, \(\hat{Y}\) the predicted outcome, and \(A\) a protected or socially relevant group attribute. For binary classification:

  • Demographic parity requires similar positive-prediction rates:

\[

P(\hat{Y}=1\mid A=a)=P(\hat{Y}=1\mid A=b).

\]

It may be inappropriate when groups have genuinely different base rates or when the prediction target is not equally distributed.

  • Equal opportunity requires similar true-positive rates:

\[

P(\hat{Y}=1\mid Y=1,A=a)=P(\hat{Y}=1\mid Y=1,A=b).

\]

This focuses on equal access to correct positive decisions.

  • Equalized odds requires both true-positive and false-positive rates to be similar across groups:

\[

P(\hat{Y}=1\mid Y=y,A=a)=P(\hat{Y}=1\mid Y=y,A=b),\quad y\in\{0,1\}.

\]

  • Calibration within groups requires predicted probabilities to have comparable meaning across groups. For example, among cases assigned a risk score of 0.7, the observed outcome rate should be approximately 0.7 in each group.

These criteria can be mathematically incompatible when groups have different base rates and predictions are not perfectly accurate. Consequently, fairness requires a documented choice of protected interests, error costs, legal obligations, and operational context rather than metric optimization alone.

Practical Fairness Assessment

A sound assessment should:

1. Define the decision and harm clearly.

2. Identify relevant groups, including intersectional groups such as age combined with disability.

3. Measure sample sizes, missingness, label quality, and subgroup performance.

4. Compare error rates, calibration, thresholds, and user outcomes.

5. Investigate causal mechanisms rather than assuming that removing a sensitive feature removes discrimination. Correlated variables may act as proxies.

6. Consider mitigation before, during, and after modeling.

Mitigation may involve improving representation, revising labels, reweighting observations, changing thresholds, constraining optimization, redesigning the decision process, or deciding not to automate. Fairness adjustments should be validated for unintended effects and documented.

3. Explainability and Contestability

Explainability concerns how understandable a system is to relevant audiences. These audiences differ:

  • Developers need diagnostic explanations.
  • Auditors need evidence and traceability.
  • Decision-makers need actionable reasons.
  • Affected individuals need understandable notice and routes for correction or appeal.

Interpretability often refers to a model being understandable in its own structure, as with a small decision tree. Post-hoc explanation provides an approximation of why a complex model produced an output. Feature attribution, counterfactual explanations, partial-dependence plots, and example-based explanations are common tools.

A local explanation describes one prediction; a global explanation describes general model behavior. Explanations can be unstable, incomplete, or misleading. An attribution method may identify correlation rather than causation, and a plausible explanation may not faithfully reflect the model’s actual computation. Therefore, explanation tools should be tested for fidelity, stability, and usefulness, not accepted merely because they produce attractive visualizations.

A meaningful explanation usually answers:

1. What decision or prediction was made?

2. What information materially influenced it?

3. What uncertainty or limitations apply?

4. What could be corrected or changed?

5. How can the decision be reviewed or challenged?

Explainability does not substitute for fairness, accuracy, privacy, or accountability. A perfectly explained harmful decision remains harmful.

4. Privacy, Data Rights, and Data Governance

Privacy is the appropriate control of information about people, not simply secrecy. AI systems create privacy risks through collection, inference, linkage, memorization, re-identification, unauthorized reuse, and excessive retention.

Important principles include purpose limitation, data minimization, accuracy, storage limitation, security, transparency, and accountability. Organizations should establish data provenance: where data came from, under what terms it was collected, what transformations occurred, who may use it, and when it must be deleted.

Depending on jurisdiction and context, individuals may have rights concerning notice, access, correction, deletion, objection, portability, or limitations on solely automated decisions. Copyright, database rights, confidentiality, biometric-information rules, and contractual restrictions may also apply. Legal requirements vary, so practitioners must involve qualified legal and privacy professionals rather than treating one framework as universally sufficient.

Privacy-Preserving Techniques

  • Access control: Restrict data and model access according to role and necessity.
  • Encryption: Protect data in transit and at rest; manage keys securely.
  • Pseudonymization: Replace direct identifiers, while recognizing that linkage may still re-identify people.
  • Anonymization: Remove or transform information so individuals cannot reasonably be identified under the relevant threat model; this is difficult to guarantee.
  • Differential privacy: Introduce calibrated randomness so the presence or absence of one individual has limited effect on released results. A common formal definition is that a mechanism \(M\) is \(\varepsilon\)-differentially private if, for neighboring datasets \(D\) and \(D'\), and every output set \(S\),

\[

P(M(D)\in S)\leq e^{\varepsilon}P(M(D')\in S).

\]

Smaller \(\varepsilon\) generally provides stronger privacy but can reduce utility. The definition assumes a clearly specified neighboring-dataset relation and a correctly implemented privacy mechanism.

  • Federated learning: Train across local devices or organizations while sharing model updates rather than raw data. Updates can still leak information, so secure aggregation and differential privacy may be needed.
  • Synthetic data: Generate artificial records to reduce direct exposure. Synthetic data is not automatically private and may preserve or amplify source-data bias.

Privacy protection must be assessed against realistic threats, including an attacker’s auxiliary information. Data minimization is often more reliable than collecting everything and attempting to secure it later.

5. Robustness, Reliability, and Adversarial Threats

A robust model maintains acceptable behavior when inputs, environments, or operating conditions vary. Robustness includes distribution shift, sensor noise, missing values, corrupted inputs, unexpected language, and deliberate attacks.

An evasion attack changes an input at inference time to cause an incorrect output. A poisoning attack corrupts training data or updates. A backdoor attack causes a model to behave normally except when a trigger is present. Model extraction attempts to reproduce a model through queries. Membership inference attempts to determine whether a particular record was used in training. Prompt injection manipulates an AI application, especially a system connected to tools or confidential context.

Security should be designed as defense in depth:

  • threat-model assets, attackers, entry points, and consequences;
  • validate and sanitize inputs;
  • separate training, evaluation, and production environments;
  • protect model files, credentials, and data pipelines;
  • use least-privilege identity and network controls;
  • log requests, model versions, approvals, and abnormal behavior;
  • rate-limit and monitor APIs;
  • test adversarially and red-team high-risk functions;
  • maintain rollback, incident response, and recovery procedures;
  • patch dependencies and verify supply-chain provenance.

Robustness testing must reflect deployment. A high test accuracy on an independent but identically distributed dataset does not establish safety under shift or attack. Report confidence intervals or uncertainty where appropriate, test failure modes, and define thresholds for suspension or human escalation.

6. Accessibility and Inclusion

An AI system can be statistically accurate yet inaccessible. Accessibility includes compatibility with assistive technologies, understandable communication, keyboard navigation, captioning, color contrast, language support, and alternatives for users unable to interact through the assumed modality.

Inclusive design should involve people with disabilities and linguistically or culturally diverse users early in development. Training and evaluation data should represent relevant variation, while avoiding the assumption that disability is an error to be corrected. Human alternatives and accommodation processes are important when automated interfaces fail.

Accessibility also concerns organizational access: users may lack reliable broadband, modern devices, digital literacy, or the language assumed by the system. A responsible deployment measures exclusion and abandonment, not merely model performance among successful users.

7. Environmental and Social Sustainability

AI has environmental costs from data collection, hardware production, training, inference, cooling, and electronic waste. Relevant measures include energy consumption, carbon intensity of electricity, hardware utilization, model size, training duration, and inference volume. Exact comparisons require a defined system boundary; reporting only model training energy can omit substantial operational costs.

Practical controls include using efficient architectures, transfer learning, pruning or quantization where quality permits, early stopping, caching, batching, renewable energy procurement, regional workload scheduling, and selecting an appropriately sized model. Efficiency must not be pursued by sacrificing accessibility, safety, or fairness. Social sustainability also includes labor conditions in data labeling and content moderation, affected communities, and the distribution of benefits and burdens.

8. Human Oversight and Risk Controls

Human oversight is meaningful only when the human has competence, time, authority, information, and a realistic ability to intervene. Merely placing a person after an automated recommendation can create rubber-stamping and automation bias.

Oversight arrangements should specify:

  • which decisions require human approval;
  • when confidence, uncertainty, or risk triggers escalation;
  • what evidence the reviewer receives;
  • how disagreements are recorded;
  • whether the human can override the system;
  • who is accountable for the final decision;
  • how affected people can request review;
  • what happens when the system is unavailable or unreliable.

Controls should be proportionate to risk. A low-risk spelling assistant may require basic privacy and quality checks. A system influencing medical treatment, access to housing, employment, credit, or public benefits requires stronger validation, documentation, independent review, monitoring, appeal, and potentially a prohibition on certain uses.

A practical risk register records the risk, cause, affected parties, likelihood, severity, existing controls, owner, mitigation, residual risk, and review date. Risk matrices are useful communication tools but do not replace evidence or judgment.

9. Documentation, Auditing, and Monitoring

Documentation turns informal intentions into inspectable evidence. A project should maintain, as appropriate:

  • purpose and intended-use statement;
  • system architecture and dependencies;
  • data sheet and provenance record;
  • labeling instructions and quality results;
  • model card and evaluation report;
  • fairness, privacy, security, robustness, and accessibility tests;
  • versioned code, data, configurations, and model artifacts;
  • decision logs and approval records;
  • user notices and operating procedures;
  • incident, complaint, and change-management records.

An audit may be internal, independent, regulatory, or community-based. Audit scope should include governance, data, technical behavior, security, human processes, and outcomes. Reproducibility requires version control, deterministic settings where feasible, documented random seeds, environment specifications, and preservation of evaluation datasets subject to privacy constraints.

Post-deployment monitoring should track accuracy, drift, subgroup outcomes, calibration, latency, availability, abuse indicators, complaints, override rates, and unexpected uses. Monitoring thresholds must have owners and actions. A dashboard without escalation authority is not a control.

10. Governance and Regulatory Frameworks

Governance frameworks provide principles, processes, and accountability structures; they do not remove the need for context-specific judgment. Important examples include:

  • NIST AI Risk Management Framework: Organizes activities around governing, mapping, measuring, and managing AI risk. It is a voluntary framework designed to support practical risk management.
  • ISO/IEC 42001: Specifies requirements for an AI management system, including organizational processes and continual improvement.
  • ISO/IEC 23894: Provides guidance on AI risk management.
  • OECD AI Principles: Emphasize inclusive growth, human-centered values, transparency, robustness, security, safety, and accountability.
  • UNESCO Recommendation on the Ethics of AI: Addresses human rights, environmental and social effects, governance, and human oversight.
  • EU AI Act: Establishes a risk-based legal framework in the European Union, including obligations for prohibited practices, high-risk systems, transparency, and general-purpose AI. Its obligations depend on the system’s role, risk classification, provider or deployer status, and applicable implementation details.
  • Data-protection laws: Instruments such as the EU General Data Protection Regulation may regulate personal data processing and automated decision-making. Other jurisdictions have their own privacy, consumer-protection, equality, employment, medical, and sectoral laws.

Frameworks should be mapped to organizational responsibilities. A developer may document evaluation and security controls; a product owner defines intended use and risk acceptance; a legal or privacy officer assesses obligations; an auditor tests evidence; and an accountable executive authorizes deployment. Responsibility cannot be transferred to a tool or model vendor without due diligence.

11. Worked Example: Fairness Assessment in Loan Triage

Consider an illustrative scenario in which a lender uses a classifier to prioritize applications for manual review. The positive class means “selected for review,” not “approved.” The system is evaluated on two groups.

Group A has 1,000 applications: 180 are truly high-risk, and 820 are not. The model selects 144 of the 180 high-risk applications and selects 164 of the 820 low-risk applications. Therefore, its true-positive rate is \(144/180=0.80\), and its false-positive rate is \(164/820=0.20\).

Group B has 500 applications: 100 are truly high-risk, and 400 are not. The model selects 70 high-risk applications and 120 low-risk applications. Its true-positive rate is \(70/100=0.70\), and its false-positive rate is \(120/400=0.30\).

The model therefore has a ten-percentage-point disadvantage in true-positive rate for Group B and a ten-percentage-point higher false-positive rate. The disparity may cause both missed intervention and unnecessary review. The analysis does not establish intentional discrimination or identify the cause. Analysts should investigate labels, historical lending processes, feature proxies, sample uncertainty, and operational consequences.

Possible responses include improving data quality, checking whether “high risk” is a valid and equitable target, testing calibrated alternative models, reviewing thresholds, providing human review, and measuring downstream outcomes. A decision to alter thresholds must consider legal requirements, error costs, and whether the review process itself distributes burdens unfairly. Results and residual risks should be documented before deployment, and subgroup monitoring should continue afterward.

12. Challenges and Limitations

Responsible AI has no universal checklist that guarantees ethical outcomes. Fairness metrics may conflict; privacy can reduce data utility; interpretability may conflict with model complexity; robust models can require greater computation; and human review can be inconsistent or unavailable. Protected attributes may be legally or ethically sensitive, yet measuring disparities without them can be impossible. Small samples make subgroup estimates uncertain.

There are also organizational limitations. Commercial pressure may encourage premature deployment, documentation may become performative, and responsibility may be fragmented across vendors and teams. A model can pass technical tests while the surrounding process creates harm. Governance therefore requires institutional authority, resources, incentives, and willingness to stop or redesign a system.

13. Best Practices for TensorFlow and PyTorch Projects

Students and practitioners should:

1. Create a project risk statement before coding.

2. Track data and model versions with reproducible pipelines.

3. Separate training, validation, test, and production data.

4. Evaluate relevant subgroups and intersections.

5. Log preprocessing, random seeds, hyperparameters, and dependencies.

6. Avoid exposing sensitive data in notebooks, logs, checkpoints, or error reports.

7. Scan dependencies and model artifacts for supply-chain risks.

8. Test corruptions, distribution shift, abuse cases, and unsafe inputs.

9. Use model cards, data sheets, and decision records.

10. Establish deployment gates, rollback criteria, incident contacts, and review dates.

11. Provide accessible interfaces, explanations, notices, and appeals.

12. Reassess the system after material changes in data, model, users, or context.

Chapter Summary

Responsible AI integrates fairness, explainability, privacy, security, robustness, accessibility, sustainability, and accountability across the complete AI life cycle. Fairness requires explicit consideration of affected groups and careful selection of metrics because fairness criteria can conflict. Explanations should be faithful, useful, and connected to contestability rather than treated as decoration. Privacy requires data minimization, lawful governance, security, and awareness that pseudonymized or synthetic data may still carry risk. Robustness requires testing under natural variation and deliberate attack, supported by defense-in-depth security.

Meaningful human oversight requires authority and capacity to intervene. Documentation, auditability, monitoring, incident response, and risk registers create evidence and accountability. Frameworks such as the NIST AI RMF, ISO standards, OECD principles, UNESCO guidance, data-protection law, and the EU AI Act provide important structures, but context and jurisdiction remain decisive. Responsible deployment is ultimately a socio-technical task: model quality matters, but so do institutions, users, affected communities, and the decisions surrounding the model.

Key Terms

Fairness: The just distribution of benefits, burdens, opportunities, and errors.

Historical bias: Existing social inequality reproduced in data or decisions.

Demographic parity: Similar positive-prediction rates across groups.

Equal opportunity: Similar true-positive rates across groups.

Calibration: Alignment between predicted probabilities and observed outcome frequencies.

Explainability: The extent to which an AI system’s behavior can be understood by relevant audiences.

Contestability: The ability to question, correct, appeal, or review an AI-assisted decision.

Differential privacy: A formal privacy guarantee limiting the effect of one person’s data on released outputs.

Federated learning: Collaborative model training in which raw data remains distributed.

Data provenance: A record of data origin, processing, ownership, and permitted use.

Robustness: The ability to maintain acceptable behavior under variation or attack.

Evasion attack: Manipulation of an input at inference time to cause an incorrect result.

Poisoning attack: Corruption of training data or updates.

Backdoor: Hidden behavior activated by a trigger.

Model card: Documentation of a model’s uses, limitations, evaluation, and risks.

Impact assessment: Structured analysis of effects, risks, mitigations, and residual risk.

Human-in-the-loop: Human participation in an automated decision or operation.

Auditability: The ability to inspect and reconstruct system behavior and governance.

Residual risk: Risk remaining after controls are applied.

AI management system: Organizational processes for governing and continually improving AI practices.

Review Questions

Short Answer Questions

1. Why must responsible AI be addressed throughout the system life cycle rather than only at deployment?

2. Distinguish demographic parity from equal opportunity.

3. What is the difference between an evasion attack and a poisoning attack?

4. Why is pseudonymization not equivalent to guaranteed anonymity?

5. State two conditions required for meaningful human oversight.

Long Answer Questions

1. Explain the major sources of bias in an AI project and describe a suitable assessment and mitigation process.

2. Compare local explanations, global explanations, and counterfactual explanations, including their limitations.

3. Describe a defense-in-depth security strategy for a machine-learning service.

4. Explain how privacy, data provenance, retention, and individual data rights should influence model development.

5. Compare the roles of documentation, auditing, and post-deployment monitoring in AI governance.

Analytical / Application Questions

1. A medical classifier has equal accuracy overall but substantially lower sensitivity for a minority group. Analyze possible causes and propose an evaluation and mitigation plan.

2. A university deploys an AI system to flag students at risk of dropping out. Design a proportionate governance plan covering notice, privacy, human review, fairness, accessibility, monitoring, and appeal.

3. An image model performs well in laboratory testing but fails when camera quality and lighting change in the field. Develop a robustness, security, and deployment decision framework, including criteria for rollback or restricted use.

Multiple Choice Questions with Answers

Question 1

Which fairness metric requires equal true-positive rates across groups?

A. Demographic parity

B. Equal opportunity

C. Calibration

D. Overall accuracy

Correct Answer: B. Equal opportunity

Question 2

What is the primary purpose of a model card?

A. To encrypt model parameters

B. To replace all legal review

C. To document intended uses, limitations, and evaluation

D. To guarantee demographic parity

Correct Answer: C. To document intended uses, limitations, and evaluation

Question 3

Which attack corrupts training data to influence a model?

A. Evasion attack

B. Model extraction

C. Poisoning attack

D. Prompt rendering

Correct Answer: C. Poisoning attack

Question 4

In differential privacy, a smaller epsilon generally indicates:

A. Stronger privacy protection

B. Higher model accuracy by definition

C. No privacy protection

D. A larger training dataset

Correct Answer: A. Stronger privacy protection

Question 5

Why can removing a protected attribute fail to eliminate discrimination?

A. Models cannot use numerical features

B. Correlated proxy variables may remain

C. Accuracy always becomes zero

D. Encryption recreates the attribute

Correct Answer: B. Correlated proxy variables may remain

Question 6

Which condition is essential for meaningful human oversight?

A. The reviewer must always accept the model output

B. The reviewer must have authority and ability to intervene

C. The system must hide uncertainty from the reviewer

D. The reviewer must be replaced after deployment

Correct Answer: B. The reviewer must have authority and ability to intervene

Question 7

What does data provenance primarily record?

A. Only the model’s prediction speed

B. The origin, processing, and permitted use of data

C. The color scheme of a dashboard

D. The number of neural-network layers

Correct Answer: B. The origin, processing, and permitted use of data

Question 8

Which practice best supports reproducibility?

A. Changing preprocessing without recording it

B. Deleting evaluation results after deployment

C. Versioning code, data references, configurations, and model artifacts

D. Using production data as the only test set

Correct Answer: C. Versioning code, data references, configurations, and model artifacts

Question 9

What is a backdoor attack?

A. A model that refuses every input

B. Hidden behavior activated by a trigger

C. A privacy notice shown to users

D. A hardware cooling method

Correct Answer: B. Hidden behavior activated by a trigger

Question 10

Why is post-deployment monitoring necessary?

A. Deployment conditions and data can change over time

B. It guarantees that all models become fair

C. It eliminates the need for testing before deployment

D. It makes documentation unnecessary

Correct Answer: A. Deployment conditions and data can change over time

AI and ML with TensorFlow & PyTorch
Chapter 14 of 15