Sample CodemacOSReviewed 2026-07-21View on Apple Developer

Training a neural network to recognize digits

At a glance

Item Summary
Purpose Build a simple neural network and train it to recognize randomly generated numbers.
App architecture A Swift sample centered on Digits, with direct use of Accelerate.
Main patterns No named application pattern supported by the extracted structure
Project style 10 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries.

Project structure

Source bundle/
├── BNNS-Training-Sample/
│   ├── main.swift
│   ├── Digits.swift
│   ├── TrainingSample.swift
│   ├── Evaluation.swift
│   ├── FullyConnected.swift
│   ├── FusedConvBatchNorm.swift
│   ├── InputAndLabelGeneration.swift
│   ├── Loss.swift
│   ├── Pooling.swift
│   └── ResourceDeallocation.swift
└── BNNS-Training-Sample.xcodeproj/
    ├── .xcodesamplecode.plist
    └── project.pbxproj

Structure observations

  • Architecturally prominent files are ranked from entry points and role-named declarations; resource-only paths are omitted.
  • Primary languages: Swift.
  • The verified tree contains 3 project/configuration file(s) and 2 source declaration(s).

Overall architecture

Reference code

BNNS-Training-Sample/Digits.swift:14 — architecture anchor

    struct Digits {
        // ...
    }

Interpretation

The arrows summarize the source-visible entry, role-named types or folders, and framework direction; when nodes come from structural folders, the sequence is a high-level interpretation rather than proof that every adjacent node calls the next. Ownership is claimed only where the next section cites a stored property or assignment. The diagram is intentionally limited to the dominant path into Accelerate.

Ownership and state

Ownership evidence

BNNS-Training-Sample/Digits.swift:18 — stored dependency or nearest verified ownership anchor

        static let zero: [Float] = [0, 0, 1, 1, 0, 0,
                                    0, 1, 0, 0, 1, 0,
                                    0, 1, 0, 0, 1, 0,
                                    0, 1, 0, 0, 1, 0,
                                    0, 1, 0, 0, 1, 0,
                                    0, 0, 1, 1, 0, 0]
Owner Object or state Relationship Mutation authority
Digits Array (zero) owns value state Initialized by the owner; the binding is immutable
Digits Array (one) owns value state Initialized by the owner; the binding is immutable
Digits Array (two) owns value state Initialized by the owner; the binding is immutable
Digits Array (three) owns value state Initialized by the owner; the binding is immutable

Composition arrows indicate a source-visible construction expression or locally owned value state; aggregation means the owner stores or receives a dependency without proving exclusive lifetime ownership.

Class and protocol design

BNNS-Training-Sample/Digits.swift:14 — representative type boundary

    struct Digits {
        // ...
    }
Type Responsibility Depends on or conforms to
Digits Represents a feature value or composable behavior Concrete collaborators/imported frameworks
TrainingSample Represents a feature value or composable behavior Concrete collaborators/imported frameworks

No local protocol conformance is claimed as protocol-oriented design; external framework conformances are listed only as dependencies.

Access control

Symbol Access Verified effect Likely rationale
Digits (BNNS-Training-Sample/Digits.swift:14) implicit internal No explicit modifier means the Swift declaration is internal to the module. Inference: app-target collaboration needs no exported library surface.
TrainingSample (BNNS-Training-Sample/TrainingSample.swift:10) implicit internal No explicit modifier means the Swift declaration is internal to the module. Inference: app-target collaboration needs no exported library surface.
main (BNNS-Training-Sample/main.swift:8) implicit internal No explicit modifier means the Swift declaration is internal to the module. Inference: app-target collaboration needs no exported library surface.

Reference code

BNNS-Training-Sample/Digits.swift:14 — representative boundary

    struct Digits {
        // ...
    }

Swift declarations without a modifier are internal; explicit private, fileprivate, private(set), public, or open entries above are interpreted by language semantics. Objective-C/C samples instead rely on header and implementation boundaries, which are not equivalent to Swift lexical privacy.

Logic ownership and placement

Logic Owning type or file Placement rationale
Feature and framework orchestration Digits The sample keeps the demonstrated path in its primary concrete type.

Design patterns

Pattern Source evidence Purpose or tradeoff
No named application pattern BNNS-Training-Sample/Digits.swift:14 The verified source directly composes concrete framework types; this document avoids forcing a pattern name.

Naming conventions

  • Types: feature-specific names rather than reusable layer suffixes.
  • Protocols: no local protocol declaration in the scanned source.
  • Methods: testExample, runTrainingExample, forwardPass, backwardPass, optimizerStep, test, backwardFully, backwardFused.
  • Files: BNNS-Training-Sample/Digits.swift, BNNS-Training-Sample/TrainingSample.swift.

Architecture takeaways

  • Digits is the main source-visible entry or composition anchor for this sample.
  • Framework work reaches Accelerate through a deliberately small high-level chain; the detailed API graph remains inside the cited implementation files.
  • Stored-property evidence identifies lifecycle collaboration; it does not by itself prove exclusive object ownership.
  • Access-control conclusions separate verified language visibility from the likely design rationale.
  • The source does not justify labeling the design protocol-oriented.

Source map

Source file Relevant symbols
BNNS-Training-Sample/main.swift Feature implementation
BNNS-Training-Sample/Digits.swift Digits
BNNS-Training-Sample/TrainingSample.swift TrainingSample
BNNS-Training-Sample/Evaluation.swift Feature implementation
BNNS-Training-Sample/FullyConnected.swift Feature implementation
BNNS-Training-Sample/FusedConvBatchNorm.swift Feature implementation
BNNS-Training-Sample/InputAndLabelGeneration.swift Feature implementation
BNNS-Training-Sample/Loss.swift Feature implementation
BNNS-Training-Sample/Pooling.swift Feature implementation
BNNS-Training-Sample/ResourceDeallocation.swift Feature implementation