MATLAB Answers


How are the functions "train" and "trainNetwork" different underneath?

How are the functions "train" and "trainNetwork" different underneath? When should I use "train" instead of "trainNetwork" or vice-versa?




1 Answer

Answer by MathWorks Support Team on 27 Feb 2018
 Accepted Answer

"train" and "trainNetwork" and their associated functions correspond to two totally independent universes of network learning. "train" and associated functions are used to create and train a "Shallow Neural Network" which is useful for function approximation and clustering. On the other hand, "trainNetwork" and its associated functions are used to create and train a "Deep Neural Network"* *which* *is predominantly used for image classification. 

The functions “train” and “trainNetwork” sit on top of totally independent code bases. They are part of the same toolbox (Neural Networks Toolbox), but independent of each other.

While you can train deep network or shallow networks with both functions, there are a few reasons that make training deep networks with “trainNetworks” easier:

  • Deep network usually require a lot of training data, which usually don’t fit in RAM (or in the GPU if training on GPU). The function “trainNetwork” was designed with that into account using algorithms like stochastic gradient descend, and ADAM optimization algorithm, that work on mini-batches, while keeping the rest of the training data out of memory.
  • The function “trainNetwork” (and all the associated functions) is more tightly integrated with cuDNN (NVIDIA’s low-level library to do NN on their GPUs) (e.g., to support fast operations like convolutions).
  • The ecosystem around “trainNetwork” closely follows new trends in deep learning, while the one for “train” focuses on the classic algorithms used for training neural networks.


If you set the same network architecture for both functions, the codebase optimizing the network parameters will be different, and the algorithms doing so will also be different. The function “train” offers more variety of algorithms. The function “trainNetwork” offers algorithms used in recent state of the art research on deep learning.



Sign in to comment.

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today