Skip to content

Instantly share code, notes, and snippets.

@nticaric
Last active November 10, 2016 21:52
Show Gist options
  • Save nticaric/135475a00c0a5c9690bdf5e38d42783c to your computer and use it in GitHub Desktop.
Save nticaric/135475a00c0a5c9690bdf5e38d42783c to your computer and use it in GitHub Desktop.
A function that returns an array containing MNIST labels
<?php
public function loadMNISTLabels($filename)
{
$fp = fopen($filename, 'rb');
$array = unpack("N2", fread($fp, 8));
$magic = $array[1];
if($magic != 2049) {
throw new Exception("Bad magic number in $filename", 1);
}
$numLabels = $array[2];
$stream = stream_get_contents($fp);
$labels = unpack("C$numLabels", $stream);
fclose($fp);
return array_values($labels);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment