function IAS_CNN_digits_f(W1)
%
% see https://www.mathworks.com/solutions/deep-learning/examples/training-a-model-from-scratch.html
%

% create image data store
digitDatasetPath = fullfile(matlabroot,"toolbox","nnet","nndemos", ...
    "nndatasets","DigitDataset");
imds = imageDatastore(digitDatasetPath, ...
    IncludeSubfolders=true,LabelSource="foldernames");

% divide into training and validation sets
numTrainFiles = 750; [imdsTrain,imdsValidation] = splitEachLabel(imds,...
    numTrainFiles,"randomize");

% create network layers
layers = [
    imageInputLayer([28 28 1])
	
    convolution2dLayer(5,4,'Weights',W1,'Bias',zeros(1,1,4));
    batchNormalizationLayer
    reluLayer
	
    maxPooling2dLayer(2,Stride=2)
	
    convolution2dLayer(5,4,Padding="same")
    batchNormalizationLayer
    reluLayer
	
    maxPooling2dLayer(2,Stride=2)
	
	
    fullyConnectedLayer(10)
    softmaxLayer
    classificationLayer
    ];

% train the network
options = trainingOptions("sgdm", ...
    InitialLearnRate=0.01, ...
    MaxEpochs=8, ...
    Plots="training-progress");    

net = trainNetwork(imdsTrain,layers,options);

% Check accuracy
YPred = classify(net,imdsValidation);
YValidation = imdsValidation.Labels;
accuracy = sum(YPred == YValidation)/numel(YValidation)

tch = 0;
