19.7. Tài liệu API của d2l

Mã nguồn của những thành phần dưới đây trong gói d2l và các mục mà trong đó chúng được định nghĩa và giải thích có thể được tìm thấy tại tập tin mã nguồn.

class d2l.mxnet.Accumulator(n)[source]

For accumulating sums over n variables.

class d2l.mxnet.AddNorm(dropout, **kwargs)[source]
forward(X, Y)[source]

Overrides to implement forward computation using NDArray. Only accepts positional arguments.

*argslist of NDArray
Input tensors.
class d2l.mxnet.Animator(xlabel=None, ylabel=None, legend=None, xlim=None, ylim=None, xscale='linear', yscale='linear', fmts='-', 'm--', 'g-.', 'r:', nrows=1, ncols=1, figsize=3.5, 2.5)[source]

For plotting data in animation.

class d2l.mxnet.BERTEncoder(vocab_size, num_hiddens, ffn_num_hiddens, num_heads, num_layers, dropout, max_len=1000, **kwargs)[source]
forward(tokens, segments, valid_lens)[source]

Overrides to implement forward computation using NDArray. Only accepts positional arguments.

*argslist of NDArray
Input tensors.
class d2l.mxnet.BERTModel(vocab_size, num_hiddens, ffn_num_hiddens, num_heads, num_layers, dropout, max_len=1000)[source]
forward(tokens, segments, valid_lens=None, pred_positions=None)[source]

Overrides to implement forward computation using NDArray. Only accepts positional arguments.

*argslist of NDArray
Input tensors.
class d2l.mxnet.BPRLoss(weight=None, batch_axis=0, **kwargs)[source]
forward(positive, negative)[source]

Defines the forward computation. Arguments can be either NDArray or Symbol.

class d2l.mxnet.CTRDataset(data_path, feat_mapper=None, defaults=None, min_threshold=4, num_feat=34)[source]
class d2l.mxnet.Decoder(**kwargs)[source]

The base decoder interface for the encoder-decoder architecture.

forward(X, state)[source]

Overrides to implement forward computation using NDArray. Only accepts positional arguments.

*argslist of NDArray
Input tensors.
class d2l.mxnet.DotProductAttention(dropout, **kwargs)[source]
forward(query, key, value, valid_len=None)[source]

Overrides to implement forward computation using NDArray. Only accepts positional arguments.

*argslist of NDArray
Input tensors.
class d2l.mxnet.Encoder(**kwargs)[source]

The base encoder interface for the encoder-decoder architecture.

forward(X, *args)[source]

Overrides to implement forward computation using NDArray. Only accepts positional arguments.

*argslist of NDArray
Input tensors.
class d2l.mxnet.EncoderBlock(num_hiddens, ffn_num_hiddens, num_heads, dropout, use_bias=False, **kwargs)[source]
forward(X, valid_len)[source]

Overrides to implement forward computation using NDArray. Only accepts positional arguments.

*argslist of NDArray
Input tensors.
class d2l.mxnet.EncoderDecoder(encoder, decoder, **kwargs)[source]

The base class for the encoder-decoder architecture.

forward(enc_X, dec_X, *args)[source]

Overrides to implement forward computation using NDArray. Only accepts positional arguments.

*argslist of NDArray
Input tensors.
class d2l.mxnet.HingeLossbRec(weight=None, batch_axis=0, **kwargs)[source]
forward(positive, negative, margin=1)[source]

Defines the forward computation. Arguments can be either NDArray or Symbol.

class d2l.mxnet.MLPAttention(units, dropout, **kwargs)[source]
forward(query, key, value, valid_len)[source]

Overrides to implement forward computation using NDArray. Only accepts positional arguments.

*argslist of NDArray
Input tensors.
class d2l.mxnet.MaskLM(vocab_size, num_hiddens, **kwargs)[source]
forward(X, pred_positions)[source]

Overrides to implement forward computation using NDArray. Only accepts positional arguments.

*argslist of NDArray
Input tensors.
class d2l.mxnet.MaskedSoftmaxCELoss(axis=- 1, sparse_label=True, from_logits=False, weight=None, batch_axis=0, **kwargs)[source]
forward(pred, label, valid_len)[source]

Defines the forward computation. Arguments can be either NDArray or Symbol.

class d2l.mxnet.MultiHeadAttention(num_hiddens, num_heads, dropout, use_bias=False, **kwargs)[source]
forward(query, key, value, valid_len)[source]

Overrides to implement forward computation using NDArray. Only accepts positional arguments.

*argslist of NDArray
Input tensors.
class d2l.mxnet.NextSentencePred(**kwargs)[source]
forward(X)[source]

Overrides to implement forward computation using NDArray. Only accepts positional arguments.

*argslist of NDArray
Input tensors.
class d2l.mxnet.PositionWiseFFN(ffn_num_hiddens, pw_num_outputs, **kwargs)[source]
forward(X)[source]

Overrides to implement forward computation using NDArray. Only accepts positional arguments.

*argslist of NDArray
Input tensors.
class d2l.mxnet.PositionalEncoding(num_hiddens, dropout, max_len=1000)[source]
forward(X)[source]

Overrides to implement forward computation using NDArray. Only accepts positional arguments.

*argslist of NDArray
Input tensors.
class d2l.mxnet.RNNModel(rnn_layer, vocab_size, **kwargs)[source]
forward(inputs, state)[source]

Overrides to implement forward computation using NDArray. Only accepts positional arguments.

*argslist of NDArray
Input tensors.
class d2l.mxnet.RNNModelScratch(vocab_size, num_hiddens, device, get_params, init_state, forward_fn)[source]

An RNN Model implemented from scratch.

class d2l.mxnet.RandomGenerator(sampling_weights)[source]

Draw a random int in [0, n] according to n sampling weights.

class d2l.mxnet.Residual(num_channels, use_1x1conv=False, strides=1, **kwargs)[source]
forward(X)[source]

Overrides to implement forward computation using NDArray. Only accepts positional arguments.

*argslist of NDArray
Input tensors.
class d2l.mxnet.SNLIDataset(dataset, num_steps, vocab=None)[source]

A customized dataset to load the SNLI dataset.

class d2l.mxnet.Seq2SeqDecoder(vocab_size, embed_size, num_hiddens, num_layers, dropout=0, **kwargs)[source]
forward(X, state)[source]

Overrides to implement forward computation using NDArray. Only accepts positional arguments.

*argslist of NDArray
Input tensors.
class d2l.mxnet.Seq2SeqEncoder(vocab_size, embed_size, num_hiddens, num_layers, dropout=0, **kwargs)[source]
forward(X, *args)[source]

Overrides to implement forward computation using NDArray. Only accepts positional arguments.

*argslist of NDArray
Input tensors.
class d2l.mxnet.SeqDataLoader(batch_size, num_steps, use_random_iter, max_tokens)[source]

An iterator to load sequence data.

class d2l.mxnet.Timer[source]

Record multiple running times.

avg()[source]

Return the average time.

cumsum()[source]

Return the accumulated time.

start()[source]

Start the timer.

stop()[source]

Stop the timer and record the time in a list.

sum()[source]

Return the sum of time.

class d2l.mxnet.TokenEmbedding(embedding_name)[source]

Token Embedding.

class d2l.mxnet.TransformerEncoder(vocab_size, num_hiddens, ffn_num_hiddens, num_heads, num_layers, dropout, use_bias=False, **kwargs)[source]
forward(X, valid_len, *args)[source]

Overrides to implement forward computation using NDArray. Only accepts positional arguments.

*argslist of NDArray
Input tensors.
class d2l.mxnet.VOCSegDataset(is_train, crop_size, voc_dir)[source]

A customized dataset to load VOC dataset.

filter(imgs)[source]

Returns a new dataset with samples filtered by the filter function fn.

Note that if the Dataset is the result of a lazily transformed one with transform(lazy=False), the filter is eagerly applied to the transformed samples without materializing the transformed result. That is, the transformation will be applied again whenever a sample is retrieved after filter().

fncallable
A filter function that takes a sample as input and returns a boolean. Samples that return False are discarded.
Dataset
The filtered dataset.
class d2l.mxnet.Vocab(tokens=None, min_freq=0, reserved_tokens=None)[source]

Vocabulary for text.

d2l.mxnet.abs(x, out=None, **kwargs)

Calculate the absolute value element-wise.

xndarray or scalar
Input array.
outndarray or None, optional
A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned.
absolutendarray
An ndarray containing the absolute value of each element in x. This is a scalar if x is a scalar.
>>> x = np.array([-1.2, 1.2])
>>> np.abs(x)
array([1.2, 1.2])
d2l.mxnet.accuracy(y_hat, y)[source]

Compute the number of correct predictions.

d2l.mxnet.arange(start, stop=None, step=1, dtype=None, ctx=None)

Return evenly spaced values within a given interval.

Values are generated within the half-open interval [start, stop) (in other words, the interval including start but excluding stop). For integer arguments the function is equivalent to the Python built-in range function, but returns an ndarray rather than a list.

startnumber, optional
Start of interval. The interval includes this value. The default start value is 0.
stopnumber
End of interval. The interval does not include this value, except in some cases where step is not an integer and floating point round-off affects the length of out.
stepnumber, optional
Spacing between values. For any output out, this is the distance between two adjacent values, out[i+1] - out[i]. The default step size is 1. If step is specified as a position argument, start must also be given.
dtypedtype
The type of the output array. The default is float32.
arangendarray

Array of evenly spaced values.

For floating point arguments, the length of the result is ceil((stop - start)/step). Because of floating point overflow, this rule may result in the last element of out being greater than stop.

>>> np.arange(3)
array([0., 1., 2.])
>>> np.arange(3.0)
array([0., 1., 2.])
>>> np.arange(3,7)
array([3., 4., 5., 6.])
>>> np.arange(3,7,2)
array([3., 5.])
d2l.mxnet.bbox_to_rect(bbox, color)[source]

Convert bounding box to matplotlib format.

d2l.mxnet.build_colormap2label()[source]

Build an RGB color to label mapping for segmentation.

d2l.mxnet.concat(seq, axis=0, out=None)

Join a sequence of arrays along an existing axis.

a1, a2, …sequence of array_like
The arrays must have the same shape, except in the dimension corresponding to axis (the first, by default).
axisint, optional
The axis along which the arrays will be joined. If axis is None, arrays are flattened before use. Default is 0.
outndarray, optional
If provided, the destination to place the result. The shape must be correct, matching that of what concatenate would have returned if no out argument were specified.
resndarray
The concatenated array.

split : Split array into a list of multiple sub-arrays of equal size. hsplit : Split array into multiple sub-arrays horizontally (column wise) vsplit : Split array into multiple sub-arrays vertically (row wise) dsplit : Split array into multiple sub-arrays along the 3rd axis (depth). stack : Stack a sequence of arrays along a new axis. hstack : Stack arrays in sequence horizontally (column wise) vstack : Stack arrays in sequence vertically (row wise) dstack : Stack arrays in sequence depth wise (along third dimension)

>>> a = np.array([[1, 2], [3, 4]])
>>> b = np.array([[5, 6]])
>>> np.concatenate((a, b), axis=0)
array([[1., 2.],
       [3., 4.],
       [5., 6.]])
>>> np.concatenate((a, b.T), axis=1)
array([[1., 2., 5.],
       [3., 4., 6.]])
>>> np.concatenate((a, b), axis=None)
array([1., 2., 3., 4., 5., 6.])
d2l.mxnet.copyfile(filename, target_dir)[source]

Copy a file into a target directory.

d2l.mxnet.corr2d(X, K)[source]

Compute 2D cross-correlation.

d2l.mxnet.cos(x, out=None, **kwargs)

Cosine, element-wise.

xndarray or scalar
Angle, in radians (\(2 \pi\) rad equals 360 degrees).
outndarray or None
A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. The dtype of the output is the same as that of the input if the input is an ndarray.
yndarray or scalar
The corresponding cosine values. This is a scalar if x is a scalar.

This function only supports input type of float.

>>> np.cos(np.array([0, np.pi/2, np.pi]))
array([ 1.000000e+00, -4.371139e-08, -1.000000e+00])
>>> # Example of providing the optional output parameter
>>> out1 = np.array([0], dtype='f')
>>> out2 = np.cos(np.array([0.1]), out1)
>>> out2 is out1
True
d2l.mxnet.cosh(x, out=None, **kwargs)

Hyperbolic cosine, element-wise. Equivalent to 1/2 * (np.exp(x) + np.exp(-x)) and np.cos(1j*x).

xndarray or scalar
Input array or scalar.
outndarray or None
A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. The dtype of the output is the same as that of the input if the input is an ndarray.
yndarray or scalar
The corresponding hyperbolic cosine values. This is a scalar if x is a scalar.

This function only supports input type of float.

>>> np.cosh(0)
1.0
d2l.mxnet.count_corpus(tokens)[source]

Count token frequencies.

class d2l.mxnet.defaultdict

defaultdict(default_factory[, …]) –> dict with default factory

The default factory is called without arguments to produce a new value when a key is not present, in __getitem__ only. A defaultdict compares equal to a dict with the same items. All remaining arguments are treated the same as if they were passed to the dict constructor, including keyword arguments.

copy() → a shallow copy of D.
default_factory

Factory for default value called by __missing__().

d2l.mxnet.download(name, cache_dir='../data')[source]

Download a file inserted into DATA_HUB, return the local filename.

d2l.mxnet.download_all()[source]

Download all files in the DATA_HUB.

d2l.mxnet.download_extract(name, folder=None)[source]

Download and extract a zip/tar file.

d2l.mxnet.evaluate_accuracy(net, data_iter)[source]

Compute the accuracy for a model on a dataset.

d2l.mxnet.evaluate_accuracy_gpu(net, data_iter, device=None)[source]

Compute the accuracy for a model on a dataset using a GPU.

d2l.mxnet.evaluate_loss(net, data_iter, loss)[source]

Evaluate the loss of a model on the given dataset.

d2l.mxnet.exp(x, out=None, **kwargs)

Calculate the exponential of all elements in the input array.

xndarray or scalar
Input values.
outndarray or None, optional
A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned.
outndarray or scalar
Output array, element-wise exponential of x. This is a scalar if x is a scalar.
>>> np.exp(1)
2.718281828459045
>>> x = np.array([-1, 1, -2, 2])
>>> np.exp(x)
array([0.36787945, 2.7182817 , 0.13533528, 7.389056  ])
class d2l.mxnet.float32

Single-precision floating-point number type, compatible with C float. Character code: 'f'. Canonical name: np.single. Alias on this platform: np.float32: 32-bit-precision floating-point number type: sign bit, 8 bits exponent, 23 bits mantissa.

as_integer_ratio()

Return a pair of integers, whose ratio is exactly equal to the original floating point number, and with a positive denominator. Raise OverflowError on infinities and a ValueError on NaNs.

>>> np.single(10.0).as_integer_ratio()
(10, 1)
>>> np.single(0.0).as_integer_ratio()
(0, 1)
>>> np.single(-.25).as_integer_ratio()
(-1, 4)
d2l.mxnet.get_dataloader_workers()[source]

Use 4 processes to read the data except for Windows.

d2l.mxnet.get_fashion_mnist_labels(labels)[source]

Return text labels for the Fashion-MNIST dataset.

class d2l.mxnet.int32

Signed integer type, compatible with C int. Character code: 'i'. Canonical name: np.intc. Alias on this platform: np.int32: 32-bit signed integer (-2147483648 to 2147483647).

d2l.mxnet.linreg(X, w, b)[source]

The linear regression model.

d2l.mxnet.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0, ctx=None)

Return evenly spaced numbers over a specified interval.

Returns num evenly spaced samples, calculated over the interval [start, stop]. The endpoint of the interval can optionally be excluded.

startreal number
The starting value of the sequence.
stopreal number
The end value of the sequence, unless endpoint is set to False. In that case, the sequence consists of all but the last of num + 1 evenly spaced samples, so that stop is excluded. Note that the step size changes when endpoint is False.
numint, optional
Number of samples to generate. Default is 50. Must be non-negative.
endpointbool, optional
If True, stop is the last sample. Otherwise, it is not included. Default is True.
retstepbool, optional
If True, return (samples, step), where step is the spacing between samples.
dtypedtype, optional
The type of the output array. If dtype is not given, infer the data type from the other input arguments.
axisint, optional
The axis in the result to store the samples. Relevant only if start or stop are array-like. By default (0), the samples will be along a new axis inserted at the beginning. Use -1 to get an axis at the end.
samplesndarray
There are num equally spaced samples in the closed interval [start, stop] or the half-open interval [start, stop) (depending on whether endpoint is True or False).
stepfloat, optional
Only returned if retstep is True Size of spacing between samples.
arangeSimilar to linspace, but uses a step size (instead of the
number of samples).
>>> np.linspace(2.0, 3.0, num=5)
array([2.  , 2.25, 2.5 , 2.75, 3.  ])
>>> np.linspace(2.0, 3.0, num=5, endpoint=False)
array([2. , 2.2, 2.4, 2.6, 2.8])
>>> np.linspace(2.0, 3.0, num=5, retstep=True)
(array([2.  , 2.25, 2.5 , 2.75, 3.  ]), 0.25)

Graphical illustration:

>>> import matplotlib.pyplot as plt
>>> N = 8
>>> y = np.zeros(N)
>>> x1 = np.linspace(0, 10, N, endpoint=True)
>>> x2 = np.linspace(0, 10, N, endpoint=False)
>>> plt.plot(x1.asnumpy(), y.asnumpy(), 'o')
[<matplotlib.lines.Line2D object at 0x...>]
>>> plt.plot(x2.asnumpy(), (y + 0.5).asnumpy(), 'o')
[<matplotlib.lines.Line2D object at 0x...>]
>>> plt.ylim([-0.5, 1])
(-0.5, 1)
>>> plt.show()

This function differs from the original numpy.linspace in the following aspects:

  • start and stop do not support list, numpy ndarray and mxnet ndarray
  • axis could only be 0
  • There could be an additional ctx argument to specify the device, e.g. the i-th GPU.
d2l.mxnet.load_array(data_arrays, batch_size, is_train=True)[source]

Construct a Gluon data iterator.

d2l.mxnet.load_data_bananas(batch_size, edge_size=256)[source]

Load the bananas dataset.

d2l.mxnet.load_data_fashion_mnist(batch_size, resize=None)[source]

Download the Fashion-MNIST dataset and then load it into memory.

d2l.mxnet.load_data_snli(batch_size, num_steps=50)[source]

Download the SNLI dataset and return data iterators and vocabulary.

d2l.mxnet.load_data_voc(batch_size, crop_size)[source]

Download and load the VOC2012 semantic dataset.

d2l.mxnet.log(x, out=None, **kwargs)

Natural logarithm, element-wise. The natural logarithm log is the inverse of the exponential function, so that log(exp(x)) = x. The natural logarithm is logarithm in base e.

xndarray
Input value. Elements must be of real value.
outndarray or None, optional
A location into which the result is stored. If provided, it must have the same shape and dtype as input ndarray. If not provided or None, a freshly-allocated array is returned.
yndarray
The natural logarithm of x, element-wise. This is a scalar if x is a scalar.

Currently only supports data of real values and inf as input. Returns data of real value, inf, -inf and nan according to the input. This function differs from the original numpy.log in the following aspects: - Does not support complex number for now - Input type does not support Python native iterables(list, tuple, …). - out param: cannot perform auto broadcasting. out ndarray’s shape must be the same as the expected output. - out param: cannot perform auto type cast. out ndarray’s dtype must be the same as the expected output. - out param does not support scalar input case.

>>> a = np.array([1, np.exp(1), np.exp(2), 0], dtype=np.float64)
>>> np.log(a)
array([  0.,   1.,   2., -inf], dtype=float64)
>>> # Using the default float32 dtype leads to slightly different behavior
>>> a = np.array([1, np.exp(1), np.exp(2), 0])
>>> np.log(a)
array([  0.,  0.99999994,   2., -inf])
>>> np.log(1)
0.0
d2l.mxnet.masked_softmax(X, valid_len)[source]

Perform softmax by filtering out some elements.

d2l.mxnet.matmul(a, b, out=None)

Dot product of two arrays. Specifically,

  • If both a and b are 1-D arrays, it is inner product of vectors

  • If both a and b are 2-D arrays, it is matrix multiplication,

  • If either a or b is 0-D (scalar), it is equivalent to multiply() and using np.multiply(a, b) or a * b is preferred.

  • If a is an N-D array and b is a 1-D array, it is a sum product over the last axis of a and b.

  • If a is an N-D array and b is a 2-D array, it is a sum product over the last axis of a and the second-to-last axis of b:

    dot(a, b)[i,j,k] = sum(a[i,j,:] * b[:,k])
    
andarray
First argument.
bndarray
Second argument.
outndarray, optional
Output argument. It must have the same shape and type as the expected output.
outputndarray
Returns the dot product of a and b. If a and b are both scalars or both 1-D arrays then a scalar is returned; otherwise an array is returned. If out is given, then it is returned
>>> a = np.array(3)
>>> b = np.array(4)
>>> np.dot(a, b)
array(12.)

For 2-D arrays it is the matrix product:

>>> a = np.array([[1, 0], [0, 1]])
>>> b = np.array([[4, 1], [2, 2]])
>>> np.dot(a, b)
array([[4., 1.],
       [2., 2.]])
>>> a = np.arange(3*4*5*6).reshape((3,4,5,6))
>>> b = np.arange(5*6)[::-1].reshape((6,5))
>>> np.dot(a, b)[2,3,2,2]
array(29884.)
>>> np.sum(a[2,3,2,:] * b[:,2])
array(29884.)
d2l.mxnet.meshgrid(*xi, **kwargs)[source]

Return coordinate matrices from coordinate vectors.

Make N-D coordinate arrays for vectorized evaluations of N-D scalar/vector fields over N-D grids, given one-dimensional coordinate arrays x1, x2,…, xn.

x1, x2,…, xnndarrays
1-D arrays representing the coordinates of a grid.
indexing{‘xy’, ‘ij’}, optional
Cartesian (‘xy’, default) or matrix (‘ij’) indexing of output. See Notes for more details.
sparsebool, optional
If True a sparse grid is returned in order to conserve memory. Default is False. Please note that sparse=True is currently not supported.
copybool, optional
If False, a view into the original arrays are returned in order to conserve memory. Default is True. Please note that copy=False is currently not supported.
X1, X2,…, XNndarray
For vectors x1, x2,…, ‘xn’ with lengths Ni=len(xi) , return (N1, N2, N3,...Nn) shaped arrays if indexing=’ij’ or (N2, N1, N3,...Nn) shaped arrays if indexing=’xy’ with the elements of xi repeated to fill the matrix along the first dimension for x1, the second for x2 and so on.

This function supports both indexing conventions through the indexing keyword argument. Giving the string ‘ij’ returns a meshgrid with matrix indexing, while ‘xy’ returns a meshgrid with Cartesian indexing. In the 2-D case with inputs of length M and N, the outputs are of shape (N, M) for ‘xy’ indexing and (M, N) for ‘ij’ indexing. In the 3-D case with inputs of length M, N and P, outputs are of shape (N, M, P) for ‘xy’ indexing and (M, N, P) for ‘ij’ indexing. The difference is illustrated by the following code snippet:

xv, yv = np.meshgrid(x, y, sparse=False, indexing='ij')
for i in range(nx):
    for j in range(ny):
        # treat xv[i,j], yv[i,j]

xv, yv = np.meshgrid(x, y, sparse=False, indexing='xy')
for i in range(nx):
    for j in range(ny):
        # treat xv[j,i], yv[j,i]

In the 1-D and 0-D case, the indexing and sparse keywords have no effect.

d2l.mxnet.mkdir_if_not_exist(path)[source]

Make a directory if it does not exist.

d2l.mxnet.normal(loc=0.0, scale=1.0, size=None, dtype=None, ctx=None, out=None)[source]

Draw random samples from a normal (Gaussian) distribution.

Samples are distributed according to a normal distribution parametrized by loc (mean) and scale (standard deviation).

locfloat, optional
Mean (centre) of the distribution.
scalefloat, optional
Standard deviation (spread or “width”) of the distribution.
sizeint or tuple of ints, optional
Output shape. If the given shape is, e.g., (m, n, k), then m * n * k samples are drawn. If size is None (default), a scalar tensor containing a single value is returned if loc and scale are both scalars. Otherwise, np.broadcast(low, high).size samples are drawn.
dtype{‘float16’, ‘float32’, ‘float64’}, optional
Data type of output samples. Default is ‘float32’
ctxContext, optional
Device context of output, default is current context.
outndarray, optional
Store output to an existing ndarray.
outndarray
Drawn samples from the parameterized normal distribution.

The probability density for the Gaussian distribution is

(19.7.1)\[p(x) = \frac{1}{\sqrt{ 2 \pi \sigma^2 }} e^{ - \frac{ (x - \mu)^2 } {2 \sigma^2} },\]

where \(\mu\) is the mean and \(\sigma\) the standard deviation. The square of the standard deviation, \(\sigma^2\), is called the variance.

The function has its peak at the mean, and its “spread” increases with the standard deviation (the function reaches 0.607 times its maximum at \(x + \sigma\) and \(x - \sigma\) [2]). This implies that numpy.random.normal is more likely to return samples lying close to the mean, rather than those far away.

[1]Wikipedia, “Normal distribution”, https://en.wikipedia.org/wiki/Normal_distribution
[2]P. R. Peebles Jr., “Central Limit Theorem” in “Probability, Random Variables and Random Signal Principles”, 4th ed., 2001, pp. 51, 51, 125.
>>> mu, sigma = 0, 0.1 # mean and standard deviation
>>> s = np.random.normal(mu, sigma, 1000)

Verify the mean and the variance:

>>> np.abs(mu - np.mean(s)) < 0.01
array(True)
d2l.mxnet.ones(shape, dtype=<class 'numpy.float32'>, order='C', ctx=None)

Return a new array of given shape and type, filled with ones. This function currently only supports storing multi-dimensional data in row-major (C-style).

shapeint or tuple of int
The shape of the empty array.
dtypestr or numpy.dtype, optional
An optional value type. Default is numpy.float32. Note that this behavior is different from NumPy’s ones function where float64 is the default value, because float32 is considered as the default data type in deep learning.
order{‘C’}, optional, default: ‘C’
How to store multi-dimensional data in memory, currently only row-major (C-style) is supported.
ctxContext, optional
An optional device context (default is the current default context).
outndarray
Array of ones with the given shape, dtype, and ctx.
>>> np.ones(5)
array([1., 1., 1., 1., 1.])
>>> np.ones((5,), dtype=int)
array([1, 1, 1, 1, 1], dtype=int64)
>>> np.ones((2, 1))
array([[1.],
       [1.]])
>>> s = (2,2)
>>> np.ones(s)
array([[1., 1.],
       [1., 1.]])
d2l.mxnet.plot(X, Y=None, xlabel=None, ylabel=None, legend=None, xlim=None, ylim=None, xscale='linear', yscale='linear', fmts='-', 'm--', 'g-.', 'r:', figsize=3.5, 2.5, axes=None)[source]

Plot data points.

d2l.mxnet.predict_ch3(net, test_iter, n=6)[source]

Predict labels (defined in Chapter 3).

d2l.mxnet.read_csv_labels(fname)[source]

Read fname to return a name to label dictionary.

d2l.mxnet.read_snli(data_dir, is_train)[source]

Read the SNLI dataset into premises, hypotheses, and labels.

d2l.mxnet.read_time_machine()[source]

Load the time machine book into a list of text lines.

d2l.mxnet.read_voc_images(voc_dir, is_train=True)[source]

Read all VOC feature and label images.

d2l.mxnet.resnet18(num_classes)[source]

A slightly modified ResNet-18 model.

d2l.mxnet.set_axes(axes, xlabel, ylabel, xlim, ylim, xscale, yscale, legend)[source]

Set the axes for matplotlib.

d2l.mxnet.set_figsize(figsize=3.5, 2.5)[source]

Set the figure size for matplotlib.

d2l.mxnet.sgd(params, lr, batch_size)[source]

Minibatch stochastic gradient descent.

d2l.mxnet.show_bboxes(axes, bboxes, labels=None, colors=None)[source]

Show bounding boxes.

d2l.mxnet.show_images(imgs, num_rows, num_cols, titles=None, scale=1.5)[source]

Plot a list of images.

d2l.mxnet.show_trace_2d(f, results)[source]

Show the trace of 2D variables during optimization.

d2l.mxnet.sin(x, out=None, **kwargs)

Trigonometric sine, element-wise.

xndarray or scalar
Angle, in radians (\(2 \pi\) rad equals 360 degrees).
outndarray or None
A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. The dtype of the output is the same as that of the input if the input is an ndarray.
yndarray or scalar
The sine of each element of x. This is a scalar if x is a scalar.

This function only supports input type of float.

>>> np.sin(np.pi/2.)
1.0
>>> np.sin(np.array((0., 30., 45., 60., 90.)) * np.pi / 180.)
array([0.        , 0.5       , 0.70710677, 0.86602545, 1.        ])
d2l.mxnet.sinh(x, out=None, **kwargs)

Hyperbolic sine, element-wise. Equivalent to 1/2 * (np.exp(x) - np.exp(-x)) or -1j * np.sin(1j*x).

xndarray or scalar
Input array or scalar.
outndarray or None
A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. The dtype of the output is the same as that of the input if the input is an ndarray.
yndarray or scalar
The corresponding hyperbolic sine values. This is a scalar if x is a scalar.

This function only supports input type of float.

>>> np.sinh(0)
0.0
>>> # Example of providing the optional output parameter
>>> out1 = np.array([0], dtype='f')
>>> out2 = np.sinh(np.array([0.1]), out1)
>>> out2 is out1
True
d2l.mxnet.split_batch(X, y, devices)[source]

Split X and y into multiple devices.

d2l.mxnet.split_batch_multi_inputs(X, y, devices)[source]

Split multi-input X and y into multiple devices.

d2l.mxnet.split_data_ml100k(data, num_users, num_items, split_mode='random', test_ratio=0.1)[source]

Split the dataset in random mode or seq-aware mode.

d2l.mxnet.squared_loss(y_hat, y)[source]

Squared loss.

d2l.mxnet.stack(arrays, axis=0, out=None)
Join a sequence of arrays along a new axis.
The axis parameter specifies the index of the new axis in the dimensions of the result. For example, if axis=0 it will be the first dimension and if axis=-1 it will be the last dimension.
arrayssequence of array_like
Each array must have the same shape.
axisint, optional
The axis in the result array along which the input arrays are stacked.
outndarray, optional
If provided, the destination to place the result. The shape must be correct, matching that of what stack would have returned if no out argument were specified.
stackedndarray
The stacked array has one more dimension than the input arrays.

concatenate : Join a sequence of arrays along an existing axis. split : Split array into a list of multiple sub-arrays of equal size.

>>> arrays = [np.random.rand(3, 4) for _ in range(10)]
>>> np.stack(arrays, axis=0).shape
(10, 3, 4)
>>> np.stack(arrays, axis=1).shape
(3, 10, 4)
>>> np.stack(arrays, axis=2).shape
(3, 4, 10)
>>> a = np.array([1, 2, 3])
>>> b = np.array([2, 3, 4])
>>> np.stack((a, b))
array([[1., 2., 3.],
       [2., 3., 4.]])
>>> np.stack((a, b), axis=-1)
array([[1., 2.],
       [2., 3.],
       [3., 4.]])
d2l.mxnet.synthetic_data(w, b, num_examples)[source]

Generate y = Xw + b + noise.

d2l.mxnet.tanh(x, out=None, **kwargs)

Compute hyperbolic tangent element-wise. Equivalent to np.sinh(x)/np.cosh(x).

xndarray or scalar.
Input array.
outndarray or None
A location into which the result is stored. If provided, it must have a shape that the inputs fill into. If not provided or None, a freshly-allocated array is returned. The dtype of the output and input must be the same.
yndarray or scalar
The corresponding hyperbolic tangent values.

If out is provided, the function writes the result into it, and returns a reference to out. (See Examples) - input x does not support complex computation (like imaginary number) >>> np.tanh(np.pi*1j) TypeError: type <type ‘complex’> not supported

>>> np.tanh(np.array[0, np.pi]))
array([0.       , 0.9962721])
>>> np.tanh(np.pi)
0.99627207622075
>>> # Example of providing the optional output parameter illustrating
>>> # that what is returned is a reference to said parameter
>>> out1 = np.array(1)
>>> out2 = np.tanh(np.array(0.1), out1)
>>> out2 is out1
True
d2l.mxnet.tensor(object, dtype=None, ctx=None)

Create an array.

objectarray_like or numpy.ndarray or mxnet.numpy.ndarray
An array, any object exposing the array interface, an object whose __array__ method returns an array, or any (nested) sequence.
dtypedata-type, optional
The desired data-type for the array. Default is float32.
ctxdevice context, optional
Device context on which the memory is allocated. Default is mxnet.context.current_context().
outndarray
An array object satisfying the specified requirements.
>>> np.array([1, 2, 3])
array([1., 2., 3.])
>>> np.array([[1, 2], [3, 4]])
array([[1., 2.],
       [3., 4.]])
>>> np.array([[1, 0], [0, 1]], dtype=bool)
array([[ True, False],
       [False,  True]])
d2l.mxnet.tokenize(lines, token='word')[source]

Split text lines into word or character tokens.

d2l.mxnet.train_2d(trainer, steps=20)[source]

Optimize a 2-dim objective function with a customized trainer.

d2l.mxnet.train_ch3(net, train_iter, test_iter, loss, num_epochs, updater)[source]

Train a model (defined in Chapter 3).

d2l.mxnet.train_ch6(net, train_iter, test_iter, num_epochs, lr, device=gpu(0))[source]

Train a model with a GPU (defined in Chapter 6).

d2l.mxnet.train_ch8(model, train_iter, vocab, lr, num_epochs, device, use_random_iter=False)[source]

Train a model (defined in Chapter 8).

d2l.mxnet.train_epoch_ch3(net, train_iter, loss, updater)[source]

Train a model within one epoch (defined in Chapter 3).

d2l.mxnet.train_epoch_ch8(model, train_iter, loss, updater, device, use_random_iter)[source]

Train a model within one epoch (defined in Chapter 8).

d2l.mxnet.try_all_gpus()[source]

Return all available GPUs, or [cpu()] if no GPU exists.

d2l.mxnet.try_gpu(i=0)[source]

Return gpu(i) if exists, otherwise return cpu().

d2l.mxnet.update_D(X, Z, net_D, net_G, loss, trainer_D)[source]

Update discriminator.

d2l.mxnet.update_G(Z, net_D, net_G, loss, trainer_G)[source]

Update generator.

d2l.mxnet.use_svg_display()[source]

Use the svg format to display a plot in Jupyter.

d2l.mxnet.voc_label_indices(colormap, colormap2label)[source]

Map an RGB color to a label.

d2l.mxnet.voc_rand_crop(feature, label, height, width)[source]

Randomly crop for both feature and label images.

d2l.mxnet.zeros(shape, dtype=<class 'numpy.float32'>, order='C', ctx=None)

Return a new array of given shape and type, filled with zeros. This function currently only supports storing multi-dimensional data in row-major (C-style).

shapeint or tuple of int
The shape of the empty array.
dtypestr or numpy.dtype, optional
An optional value type (default is numpy.float32). Note that this behavior is different from NumPy’s zeros function where float64 is the default value, because float32 is considered as the default data type in deep learning.
order{‘C’}, optional, default: ‘C’
How to store multi-dimensional data in memory, currently only row-major (C-style) is supported.
ctxContext, optional
An optional device context (default is the current default context).
outndarray
Array of zeros with the given shape, dtype, and ctx.
>>> np.zeros(5)
array([0., 0., 0., 0., 0.])
>>> np.zeros((5,), dtype=int)
array([0, 0, 0, 0, 0], dtype=int64)
>>> np.zeros((2, 1))
array([[0.],
       [0.]])

19.7.1. Những người thực hiện

Bản dịch trong trang này được thực hiện bởi:

  • Đoàn Võ Duy Thanh
  • Phạm Hồng Vinh