Skip to content

Instantly share code, notes, and snippets.

@jzstark
Last active August 16, 2017 17:15
Show Gist options
  • Select an option

  • Save jzstark/872a384a96431cc25e57c151736117ac to your computer and use it in GitHub Desktop.

Select an option

Save jzstark/872a384a96431cc25e57c151736117ac to your computer and use it in GitHub Desktop.
CIFAR10 with full dataset
#!/usr/bin/env owl
open Owl
open Owl_neural
open Algodiff.S
open Owl_neural_neuron
let model () =
let open Owl_neural_graph in
let nn = input [|28;28;3|]
|> conv2d [|3;3;3;32|] [|1;1|] ~act_typ:Activation.Relu
|> conv2d [|3;3;32;32|] [|1;1|] ~act_typ:Activation.Relu ~padding:Owl_dense_ndarray_generic.VALID
|> max_pool2d [|2;2|] [|2;2|] ~padding:Owl_dense_ndarray_generic.VALID
|> dropout 0.25
|> conv2d [|3;3;32;64|] [|1;1|] ~act_typ:Activation.Relu
|> conv2d [|3;3;64;64|] [|1;1|] ~act_typ:Activation.Relu ~padding:Owl_dense_ndarray_generic.VALID
|> max_pool2d [|2;2|] [|2;2|] ~padding:Owl_dense_ndarray_generic.VALID
|> dropout 0.25
|> fully_connected 1024 ~act_typ:Activation.Relu
|> linear 10 ~act_typ:Activation.Softmax
|> get_network
in print nn;
nn
let prepare_training_data () =
let datasets = Array.make 5 (Dense.Matrix.S.zeros 1 1) in
let labels = Array.make 5 (Dense.Matrix.S.zeros 1 1) in
for i = 1 to 5 do
let x, y = Dataset.load_cifar_train_data i in
Array.set datasets (i-1) x;
Array.set labels (i-1) y;
done;
let x = Dense.Matrix.S.concatenate datasets in
let y = Dense.Matrix.S.concatenate labels in
let m = Dense.Matrix.S.row_num x in
let x = Dense.Matrix.S.to_ndarray x in
let x = Dense.Ndarray.S.reshape x [|m;32;32;3|] in
(* let x = Dense.Ndarray.S.slice [[];[];[];[0]] x in *)
let classes = 10 in
let y' = Dense.Matrix.S.zeros m classes in
for i = 0 to m - 1 do
Dense.Matrix.S.set y' i (int_of_float y.{i,0}) 1.
done;
let [|s1;s2;s3;s4|] = Dense.Ndarray.S.shape x in
let wy, hy = Dense.Matrix.S.shape y' in
Printf.printf "data shape: (%d, %d, %d, %d)\nlabels shape: (%d, %d).\n" s1 s2 s3 s4 wy hy;
Dense.Ndarray.S.save x "cifar10_data";
Dense.Matrix.S.save y' "cifar10_labels";
()
let train_cifar10_keras_graph () =
let nn = model () in
(* let x, y = prepare_training_data () in *)
let x = Dense.Ndarray.S.load "cifar10_data" in
let y = Dense.Matrix.S.load "cifar10_labels" in
let params = Params.config
~batch:(Batch.Mini 32) ~learning_rate:(Learning_Rate.RMSprop (0.0001, 0.9)) ~checkpoint:0.1 1. in
Graph.train_cnn ~params nn x y
let _ =
(* prepare_training_data () *)
train_cifar10_keras_graph ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment