-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
254 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import Lean | ||
-- import SciLean | ||
import SciLean.Data.DataArray | ||
import SciLean.Core.FloatAsReal | ||
|
||
open SciLean | ||
open IO FS System | ||
|
||
def checkFileExists (path : FilePath) : IO Unit := do | ||
|
||
if ¬(← path.pathExists) then | ||
throw (IO.userError s!"MNIST data file '{path}' not found. Please download binary version from https://git-disl.github.io/GTDLBench/datasets/mnist_datasets/ and extract it in 'data' directory") | ||
|
||
def toFloatRepr (b : ByteArray) (ι : Type _) [Index ι] (_ : b.size = sizeOf ι) : Float^ι := Id.run do | ||
let mut idx : USize := 0 | ||
let mut x : Float^ι := 0 | ||
for i in fullRange ι do | ||
let val := b.uget idx sorry_proof | ||
x[i] := val.toNat.toFloat / 256.0 | ||
idx := idx + 1 | ||
x | ||
|
||
open IO FS System in | ||
def loadImages (path : FilePath) (maxImages : Nat) : IO (Array (Float^[28,28])) := do | ||
|
||
checkFileExists path | ||
|
||
if maxImages = 0 then | ||
return #[] | ||
|
||
let start ← IO.monoMsNow | ||
IO.print s!"loading images from {path} ... " | ||
let data ← | ||
IO.FS.withFile path .read fun m => do | ||
let _header ← m.read 16 -- discart 16 byte header | ||
let mut data : Array ByteArray := #[] | ||
for _ in [0:maxImages] do | ||
let n : Nat := 28 | ||
let nums ← m.read (n*n).toUSize | ||
if nums.size = 0 then | ||
break | ||
data := data.push nums | ||
pure data | ||
|
||
if data.size ≠ maxImages then | ||
throw <| IO.userError s!"file {path} contains only {data.size} images" | ||
|
||
IO.println s!"loaded in {(← IO.monoMsNow) - start}ms" | ||
|
||
let start ← IO.monoMsNow | ||
IO.print "converting to float format ... " | ||
let data := data.map (toFloatRepr · (Idx 28 × Idx 28) sorry_proof) | ||
IO.println s!"converted in {(← IO.monoMsNow) - start}ms" | ||
|
||
return data | ||
|
||
|
||
def loadLabels (path : FilePath) (maxLabels : Nat) : IO (Array (Float^[10])) := do | ||
checkFileExists path | ||
|
||
if maxLabels = 0 then | ||
return #[] | ||
|
||
let start ← IO.monoMsNow | ||
IO.print s!"loading labels from {path} ... " | ||
let data ← IO.FS.withFile path .read fun m => do | ||
let _header ← m.read 8 -- discart 8 byte header | ||
m.read maxLabels.toUSize | ||
if data.size ≠ maxLabels then | ||
throw <| IO.userError s!"file {path} contains only {data.size} labels" | ||
IO.println s!"loaded in {(← IO.monoMsNow) - start}ms" | ||
|
||
let start ← IO.monoMsNow | ||
IO.print "converting to float format ... " | ||
let mut labels : Array (Float^[10]) := .mkEmpty data.size | ||
for b in data do | ||
let i : Idx 10 := ⟨b.toUSize, sorry_proof⟩ | ||
labels := labels.push (oneHot i 1) | ||
IO.println s!"converted in {(← IO.monoMsNow) - start}ms" | ||
|
||
return labels | ||
|
||
|
||
def printDigit (digit : Float^[28,28]) : IO Unit := do | ||
|
||
for i in fullRange (Idx 28) do | ||
IO.print "|" | ||
for j in fullRange (Idx 28) do | ||
let val := digit[(i,j)] | ||
if (val > 0.8) then | ||
IO.print "#" | ||
else if (val > 0.6) then | ||
IO.print "$" | ||
else if (val > 0.4) then | ||
IO.print "o" | ||
else if (val > 0.1) then | ||
IO.print "." | ||
else | ||
IO.print " " | ||
|
||
IO.println "|" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import examples.MNISTClassifier.DataUtil | ||
import examples.MNISTClassifier.Model | ||
|
||
open SciLean | ||
|
||
|
||
def main : IO Unit := do | ||
|
||
let trainNum := 1000 | ||
let trainImages ← loadImages "data/train-images.idx3-ubyte" trainNum | ||
let trainLabels ← loadLabels "data/train-labels.idx1-ubyte" trainNum | ||
|
||
let testNum := 0 | ||
let testImages ← loadImages "data/t10k-images.idx3-ubyte" testNum | ||
let testLabels ← loadLabels "data/t10k-labels.idx1-ubyte" testNum | ||
|
||
IO.println "" | ||
|
||
IO.println s!"label: {trainLabels[2]!}" | ||
IO.println "+----------------------------+" | ||
printDigit trainImages[2]! | ||
IO.println "+----------------------------+" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import Lean | ||
-- import SciLean | ||
import SciLean.Core.FloatAsReal | ||
import SciLean.Modules.ML.Dense | ||
import SciLean.Modules.ML.Convolution | ||
import SciLean.Modules.ML.Pool | ||
|
||
open SciLean | ||
open IO FS System | ||
|
||
open NotationOverField | ||
set_default_scalar Float | ||
set_option synthInstance.maxSize 2000 | ||
|
||
open ML ArrayType in | ||
def model (w x) := | ||
(fun ((w₁,b₁),(w₂,b₂),(w₃,b₃)) (x : Float^[1,28,28]) => | ||
x |> conv2d 32 1 w₁ b₁ | ||
|> map (fun x => x^2) | ||
|> avgPool | ||
|> dense 100 w₂ b₂ | ||
|> map (fun x => x^2) | ||
|> dense 10 w₃ b₃) w x | ||
-- |> softMax | ||
|
||
|
||
#generate_revDeriv model w x | ||
prop_by unfold model; simp[model.match_1]; fprop | ||
trans_by unfold model; simp[model.match_1]; ftrans | ||
|
||
|
||
def batchLoss (w) (images : Float^[1,28,28]^[batchSize]) (labels : Float^[10]^[batchSize]) := | ||
∑ i, ‖(model w images[i] - labels[i])‖₂ |