from mxnet import nd
n_train, n_test, true_w, true_b = 100, 100, [1.2, -3.4, 5.6], 5
features = nd.random.normal(shape=(n_train + n_test, 1))
poly_features = nd.concat(features, nd.power(features, 2),
nd.power(features, 3))
labels = (true_w[0] * poly_features[:, 0] + true_w[1] * poly_features[:, 1] + true_w[2] * poly_features[:, 2] + true_b)
labels += nd.random.normal(scale=0.01, shape=labels.shape)
print(labels[:2])
由于形状features
和poly_features
都是2D NDArray,我认为这种代码的输出的形式,如下:
NDArray 2x1 @cpu(0)
,
但真正的输出形式是
NDArray 2 @cpu(0)
.
为什么输出不是 2D NDArray?
虽然features
和poly_features
是 2D NDArray,但在计算时labels
仅使用 的切片poly_features
,它们是 1D NDArray。这是断线的代码:
labels = true_w[0] * poly_features[:, 0] # true_w[0] is scalar, poly_features[:, 0] is 1D NDAarray
+ true_w[1] * poly_features[:, 1] # true_w[1] is scalar, poly_features[:, 1] is 1D NDAarray
+ true_w[2] * poly_features[:, 2] # true_w[2] is scalar, poly_features[:, 2] is 1D NDAarray
+ true_b # true_b is scalar
所以,你得到一维数组作为答案。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句