<jupyter_start><jupyter_text>数值稳定性和模型初始化梯度消失<jupyter_code>import sys
sys.path.append('..')
%matplotlib inline
import mindspore
import mindspore.numpy as mnp
import mindspore.ops as ops
from d2l import mindspore as d2l

def sigmoid(x):
    return ops.Sigmoid()(x)

x = mnp.arange(-8.0, 8.0, 0.1)
y = sigmoid(x)
grad_all = ops.GradOperation(get_all=True)
x_grad = grad_all(sigmoid)(x)[0]

d2l.plot(x.asnumpy(), [y.asnumpy(), x_grad.asnumpy()],
         legend=['sigmoid', 'gradient'], figsize=(4.5, 2.5))<jupyter_output>[WARNING] OPTIMIZER(3870984,7f87f7fe4740,python):2021-11-05-00:20:32.730.325 [mindspore/ccsrc/frontend/optimizer/ad/dfunctor.cc:803] GetPrimalUser] J operation has no relevant primal call in the same graph. Func graph: 1_after_grad.2, J user: 1_after_grad.2:sigmoid{[0]: 3, [1]: args0}<jupyter_text>梯度爆炸<jupyter_code>from mindspore import Tensor

M = ops.normal((4, 4), Tensor(0), Tensor(1))
print('一个矩阵 \n',M)
for i in range(100):
    M = ops.matmul(M, ops.normal((4,4), Tensor(0), Tensor(1)))

print('乘以100个矩阵后\n', M)<jupyter_output>一个矩阵 
 [[-1.3180416   0.21586044 -1.6110967  -0.6184063 ]
 [ 0.36339304 -0.36989677 -0.6256188  -0.4508158 ]
 [-0.12890556  0.03007882  1.1935389  -0.5326446 ]
 [-0.09897626 -1.9187572   0.89836234 -1.4689225 ]]
乘以100个矩阵后
 [[ 4.8074875e+24 -2.7623282e+24 -1.3187021e+24  1.2426934e+25]
 [-1.4034659e+24  8.0641556e+23  3.8497296e+23 -3.6278361e+24]
 [-3.0235626e+24  1.7373048e+24  8.2936814e+23 -7.8156434e+24]
 [-9.1700892e+24  5.2690288e+24  2.5153709e+24 -2.3703872e+25]]
{'fraction_non_alphanumeric': 0.17146596858638743, 'fraction_numerical': 0.24345549738219896, 'mean_word_length': 4.726591760299626, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '6968', 'license': 'MIT', 'n_tokens_mistral': 944, 'n_tokens_neox': 765, 'n_words': 119, 'path': 'chapter_04_multilayer-perceptrons/6_numerical-stability-and-init.ipynb', 'repo_name': 'lvyufeng/d2l-mindspore', 'size': '1528'}