REVISED: Wednesday, October 8, 2025
1. OCAML SINGLE-LAYER NEURAL NETWORK
(*
============================
ocaml C:\AI2025\lesson5.ml
Lesson 5: Single-Layer Neural Network
============================
Objective:
1. Implement a single-layer neural network (no training yet)
2. Apply weights, biases, and activation functions
3. Compute the network output for a batch of inputs
*)
(* -----------------------------
1. Vector and Matrix Utilities
----------------------------- *)
type vector = float list
type matrix = float list list
(* Vector addition *)
let vector_add v1 v2 =
List.map2 ( +. ) v1 v2
(* Adds two vectors element-wise *)
(* Dot product of two vectors *)
let dot_product v1 v2 =
List.fold_left (+.) 0.0 (List.map2 ( *. ) v1 v2)
(* Computes sum of products of corresponding elements *)
(* Matrix-vector multiplication *)
let mat_vec_mul m v =
List.map (fun row -> dot_product row v) m
(* Each row of the matrix is dotted with the input vector *)
(* -----------------------------
2. Activation Functions
----------------------------- *)
let relu x = if x > 0. then x else 0.
let sigmoid x = 1. /. (1. +. exp (-.x))
let vector_map f v = List.map f v
(* Apply function f to each element of vector v *)
(* -----------------------------
3. Single-Layer Neural Network
----------------------------- *)
(* Network parameters: weights and biases *)
(* 2 inputs, 3 neurons in the hidden layer *)
let weights : matrix = [
[0.5; -0.2]; (* neuron 1 weights *)
[0.3; 0.8]; (* neuron 2 weights *)
[-0.7; 0.1] (* neuron 3 weights *)
]
let biases : vector = [0.1; -0.1; 0.05]
(* Each neuron has a bias *)
(* Forward pass *)
let forward_pass input =
let z = vector_add (mat_vec_mul weights input) biases in
vector_map relu z
(* Applies linear transformation and ReLU activation *)
(* -----------------------------
4. Example usage
----------------------------- *)
let inputs = [
[1.0; 2.0];
[0.5; -1.0];
[0.0; 0.0]
]
let () =
List.iter (fun inp ->
let out = forward_pass inp in
Printf.printf "Input: [%s] -> Output: [%s]\n"
(String.concat "; " (List.map string_of_float inp))
(String.concat "; " (List.map string_of_float out))
) inputs
2. CONCLUSION
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
Install the latest PowerShell for new features and improvements! https://aka.ms/PSWindows
OCaml version: The OCaml toplevel, version 5.3.0
Coq-LSP version: 0.2.3
Loading personal and system profiles took 1141ms.
PS C:\Users\User> ocaml C:\AI2025\lesson5.ml
Input: [1.; 2.] -> Output: [0.2; 1.8; 0.]
Input: [0.5; -1.] -> Output: [0.55; 0.; 0.]
Input: [0.; 0.] -> Output: [0.1; 0.; 0.05]
PS C:\Users\User>
3. REFERENCES
Bird, R. (2015). Thinking Functionally with Haskell. Cambridge, England: Cambridge University Press.
Davie, A. (1992). Introduction to Functional Programming Systems Using Haskell. Cambridge, England: Cambridge University Press.
Goerzen, J. & O'Sullivan, B. & Stewart, D. (2008). Real World Haskell. Sebastopol, CA: O'Reilly Media, Inc.
Hutton, G. (2007). Programming in Haskell. New York: Cambridge University Press.
Lipovača, M. (2011). Learn You a Haskell for Great Good!: A Beginner's Guide. San Francisco, CA: No Starch Press, Inc.
Thompson, S. (2011). The Craft of Functional Programming. Edinburgh Gate, Harlow, England: Pearson Education Limited.