4
$\begingroup$

I'd like to train a neural network for only a specified number of batches, that is for only part of an epoch (or TrainingRound). I've tried using the NetTrain[net,{f,"RoundLength"->n}] form, but this only produces approximately n examples.

n = 10000;
trainingData = RandomReal[1, {n, 4}] -> RandomReal[1, {n, 4}];
net = NetChain[{8, Tanh, 2048, Tanh, 2048, Tanh, 4}];
trained = NetTrain[net, trainingData, BatchSize-> 24, 
             MaxTrainingRounds -> 0.1 (* I'd like 'TrainingBatches -> 6' *)]
$\endgroup$
2
  • 2
    $\begingroup$ Maybe something like this TrainingProgressFunction -> {"StopTraining" &, "Interval" -> Quantity[6, "Batches"]}. But if you wish to stop after some number of batches at each round then this wouldn't work. Unless there is a hidden command similar to "StopTraining", like "StopRound" or "NextRound". $\endgroup$
    – swish
    Commented Jul 11, 2018 at 19:24
  • 1
    $\begingroup$ @swish that’d be a fun thing to try to dig up. Try using GeneralUtilities`PrintDefinitionsLocal and searching for strings like that. $\endgroup$
    – b3m2a1
    Commented Jul 11, 2018 at 21:19

1 Answer 1

3
$\begingroup$

The function NetTrain will build a NetTrainer first.

Then NeuralNetworks`TrainerUpdate[trainer, DataBatch] will update the net.

Let's see the options of NeuralNetworks`ToNetTrainer

Options[NeuralNetworks`ToNetTrainer]

{
    "BatchSize" -> Automatic, "MaxBatchSize" -> None,
    "TotalBatches" -> 4096, "UpdatesPerBatch" -> 1,
    "Context" -> 1, "DataType" -> 0, "GradientScale" -> 1,
    "Optimizer" -> "ADAM", "LearningRates" -> Automatic
}

"TotalBatches" is an optional item, so I think your needs are in development.

See if the next version will add this option.


Well, in fact, we do not need to know these.

Use "StopTraining", and if you want to store each of the 6 batches, using "Checkpointing".

n = 1000;
trainingData = RandomReal[1, {n, 4}] -> RandomReal[1, {n, 4}];
net = NetChain[{8, Tanh, 2048, Tanh, 2048, Tanh, 4}];
trained = NetTrain[net, trainingData, All, TargetDevice -> "GPU",
    BatchSize -> 24, MaxTrainingRounds -> 10000,
    TrainingProgressFunction -> {"StopTraining"&, "Interval" -> Quantity[60, "Batches"]},
    TrainingProgressCheckpointing -> {"Directory", $HomeDirectory, "Interval" -> Quantity[6, "Batches"]}
]

If you want to store the specific {2, 3, 5, 7} batch's training results.

Using NetTrainResultsObject, and extract the final result and repeat the first step.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.