File I/O

So far we discussed how to process data and how to build, train, and test deep learning models. However, at some point, we will hopefully be happy enough with the learned models that we will want to save the results for later use in various contexts (perhaps even to make predictions in deployment). Additionally, when running a long training process, the best practice is to periodically save intermediate results (checkpointing) to ensure that we do not lose several days worth of computation if we trip over the power cord of our server. Thus it is time to learn how to load and store both individual weight vectors and entire models. This section addresses both issues.

Loading and Saving Tensors

For individual tensors, we can directly invoke the load and save functions to read and write them respectively. Both functions require that we supply a name, and save requires as input the variable to be saved.

```{.python .input} from mxnet import np, npx from mxnet.gluon import nn npx.set_np()

x = np.arange(4) npx.save(‘x-file’, x)

  1. ```{.python .input}
  2. #@tab pytorch
  3. import torch
  4. from torch import nn
  5. from torch.nn import functional as F
  6. x = torch.arange(4)
  7. torch.save(x, 'x-file')

```{.python .input}

@tab tensorflow

import tensorflow as tf import numpy as np

x = tf.range(4) np.save(“x-file.npy”, x)

  1. We can now read the data from the stored file back into memory.
  2. ```{.python .input}
  3. x2 = npx.load('x-file')
  4. x2

```{.python .input}

@tab pytorch

x2 = torch.load(“x-file”) x2

  1. ```{.python .input}
  2. #@tab tensorflow
  3. x2 = np.load('x-file.npy', allow_pickle=True)
  4. x2

We can store a list of tensors and read them back into memory.

```{.python .input} y = np.zeros(4) npx.save(‘x-files’, [x, y]) x2, y2 = npx.load(‘x-files’) (x2, y2)

  1. ```{.python .input}
  2. #@tab pytorch
  3. y = torch.zeros(4)
  4. torch.save([x, y],'x-files')
  5. x2, y2 = torch.load('x-files')
  6. (x2, y2)

```{.python .input}

@tab tensorflow

y = tf.zeros(4) np.save(‘xy-files.npy’, [x, y]) x2, y2 = np.load(‘xy-files.npy’, allow_pickle=True) (x2, y2)

  1. We can even write and read a dictionary that maps
  2. from strings to tensors.
  3. This is convenient when we want
  4. to read or write all the weights in a model.
  5. ```{.python .input}
  6. mydict = {'x': x, 'y': y}
  7. npx.save('mydict', mydict)
  8. mydict2 = npx.load('mydict')
  9. mydict2

```{.python .input}

@tab pytorch

mydict = {‘x’: x, ‘y’: y} torch.save(mydict, ‘mydict’) mydict2 = torch.load(‘mydict’) mydict2

  1. ```{.python .input}
  2. #@tab tensorflow
  3. mydict = {'x': x, 'y': y}
  4. np.save('mydict.npy', mydict)
  5. mydict2 = np.load('mydict.npy', allow_pickle=True)
  6. mydict2

Loading and Saving Model Parameters

Saving individual weight vectors (or other tensors) is useful, but it gets very tedious if we want to save (and later load) an entire model. After all, we might have hundreds of parameter groups sprinkled throughout. For this reason the deep learning framework provides built-in functionalities to load and save entire networks. An important detail to note is that this saves model parameters and not the entire model. For example, if we have a 3-layer MLP, we need to specify the architecture separately. The reason for this is that the models themselves can contain arbitrary code, hence they cannot be serialized as naturally. Thus, in order to reinstate a model, we need to generate the architecture in code and then load the parameters from disk. Let us start with our familiar MLP.

```{.python .input} class MLP(nn.Block): def init(self, kwargs): super(MLP, self).init(kwargs) self.hidden = nn.Dense(256, activation=’relu’) self.output = nn.Dense(10)

  1. def forward(self, x):
  2. return self.output(self.hidden(x))

net = MLP() net.initialize() X = np.random.uniform(size=(2, 20)) Y = net(X)

  1. ```{.python .input}
  2. #@tab pytorch
  3. class MLP(nn.Module):
  4. def __init__(self):
  5. super().__init__()
  6. self.hidden = nn.Linear(20, 256)
  7. self.output = nn.Linear(256, 10)
  8. def forward(self, x):
  9. return self.output(F.relu(self.hidden(x)))
  10. net = MLP()
  11. X = torch.randn(size=(2, 20))
  12. Y = net(X)

```{.python .input}

@tab tensorflow

class MLP(tf.keras.Model): def init(self): super().init() self.flatten = tf.keras.layers.Flatten() self.hidden = tf.keras.layers.Dense(units=256, activation=tf.nn.relu) self.out = tf.keras.layers.Dense(units=10)

  1. def call(self, inputs):
  2. x = self.flatten(inputs)
  3. x = self.hidden(x)
  4. return self.out(x)

net = MLP() X = tf.random.uniform((2, 20)) Y = net(X)

  1. Next, we store the parameters of the model as a file with the name "mlp.params".
  2. ```{.python .input}
  3. net.save_parameters('mlp.params')

```{.python .input}

@tab pytorch

torch.save(net.state_dict(), ‘mlp.params’)

  1. ```{.python .input}
  2. #@tab tensorflow
  3. net.save_weights('mlp.params')

To recover the model, we instantiate a clone of the original MLP model. Instead of randomly initializing the model parameters, we read the parameters stored in the file directly.

```{.python .input} clone = MLP() clone.load_parameters(‘mlp.params’)

  1. ```{.python .input}
  2. #@tab pytorch
  3. clone = MLP()
  4. clone.load_state_dict(torch.load("mlp.params"))
  5. clone.eval()

```{.python .input}

@tab tensorflow

clone = MLP() clone.load_weights(“mlp.params”)

  1. Since both instances have the same model parameters,
  2. the computational result of the same input `X` should be the same.
  3. Let us verify this.
  4. ```{.python .input}
  5. Y_clone = clone(X)
  6. Y_clone == Y

```{.python .input}

@tab pytorch

Y_clone = clone(X) Y_clone == Y

  1. ```{.python .input}
  2. #@tab tensorflow
  3. Y_clone = clone(X)
  4. Y_clone == Y

Summary

  • The save and load functions can be used to perform file I/O for tensor objects.
  • We can save and load the entire sets of parameters for a network via a parameter dictionary.
  • Saving the architecture has to be done in code rather than in parameters.

Exercises

  1. Even if there is no need to deploy trained models to a different device, what are the practical benefits of storing model parameters?
  2. Assume that we want to reuse only parts of a network to be incorporated into a network of a different architecture. How would you go about using, say the first two layers from a previous network in a new network?
  3. How would you go about saving the network architecture and parameters? What restrictions would you impose on the architecture?

:begin_tab:mxnet Discussions :end_tab:

:begin_tab:pytorch Discussions :end_tab:

:begin_tab:tensorflow Discussions :end_tab: