PyTorch Notes
gadget functions (torch)
x.numel()number of elements inxbroadcast mechanism
Y = Y + Xwould allocate a new space for the result ofY + X.Ythen is the reference to this new memory.Y += XorY[:] = Y + Xis better. (id(Y)will be unchanged.)float()orint()converts a scalar tensor to a standard number, similar tox.item()A * Belement-wise product.A_sum = A.sum(axis=1, keepdims=True)will keep the number of axis.A / A_sumwill enable broadcast mechanism.A.cumsum(axis=0)accumulated sum.linear algebra (matrix, vector) multiplication
dot product:
torch.dot(x, y)only accept 1-D tensor.matrix-vector multiplication:
torch.mv(A, x)matrix and vector. x is 1-D vector. All in column vectors.matrix-matrix multiplication:
torch.mm(A, B).
torch.distributionsmultinomial.Multinomial(n, probs).sample(N, )
gadget functions (pandas)
data.fillna(data.mean())pd.get_dummies(data, dummy_na=True)Convert categorical variable into dummy/indicator variables. Works forstr.
gadget functions (d2l)
d2l.plotexamplex = np.arange(0, 3, 0.1) d2l.plot(x, [x ** 2, 2 * x - 1], 'x', 'f(x)', legend=['f(x)', 'Tangent'])
plot
example
plt.plot(y, label=("label"))plt.axhline(y=0, color='black', linestyle='dashed')plt.gca().set_xlabel('x label')plt.gca().set_ylabel('y label')plt.legend()