最近在阅读动手学深度学习一书,记录一些跟着书写的实例代码 供大家参考
from mxnet import nd
x = nd.arange(12)
print(x)
print(x.shape)
print(x.size)
y=x.reshape(3,4)
print(y)
print(y.shape)
z = nd.zeros((2, 3, 4))
print(z)
n=nd.ones((2,3,4))
print(n)
Y = nd.array([[2, 1, 4, 3], [1, 2, 3, 4], [4, 3, 2, 1]])
print(Y)
q=nd.random.normal(0, 1, shape=(3, 4))
print(q)
print(Y+y)
print(Y*y)
print(y/Y)
print(Y.exp())
print(Y.T)
print(nd.dot(y , Y.T))
print(nd.concat(y,Y,dim=0))
print(nd.concat(y,Y,dim=1))
print(y==Y)
print(y.sum())
print(y.norm())
print(y.norm().asscalar())
以下为程序运行结果
D:\miniconda3\envs\gluon\python.exe D:/DongShouDeep/Gluon/first.py
[ 0. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11.]
<NDArray 12 @cpu(0)>
(12,)
12
[[ 0. 1. 2. 3.]
[ 4. 5. 6. 7.]
[ 8. 9. 10. 11.]]
<NDArray 3x4 @cpu(0)>
(3, 4)
[[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]]
<NDArray 2x3x4 @cpu(0)>
[[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]
[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]]
<NDArray 2x3x4 @cpu(0)>
[[2. 1. 4. 3.]
[1. 2. 3. 4.]
[4. 3. 2. 1.]]
<NDArray 3x4 @cpu(0)>
[[ 1.1630785 0.4838046 0.29956347 0.15302546]
[-1.1688148 1.558071 -0.5459446 -2.3556297 ]
[ 0.54144025 2.6785064 1.2546344 -0.54877406]]
<NDArray 3x4 @cpu(0)>
[[ 2. 2. 6. 6.]
[ 5. 7. 9. 11.]
[12. 12. 12. 12.]]
<NDArray 3x4 @cpu(0)>
[[ 0. 1. 8. 9.]
[ 4. 10. 18. 28.]
[32. 27. 20. 11.]]
<NDArray 3x4 @cpu(0)>
[[ 0. 1. 0.5 1. ]
[ 4. 2.5 2. 1.75]
[ 2. 3. 5. 11. ]]
<NDArray 3x4 @cpu(0)>
[[ 7.389056 2.7182817 54.59815 20.085537 ]
[ 2.7182817 7.389056 20.085537 54.59815 ]
[54.59815 20.085537 7.389056 2.7182817]]
<NDArray 3x4 @cpu(0)>
[[2. 1. 4.]
[1. 2. 3.]
[4. 3. 2.]
[3. 4. 1.]]
<NDArray 4x3 @cpu(0)>
[[ 18. 20. 10.]
[ 58. 60. 50.]
[ 98. 100. 90.]]
<NDArray 3x3 @cpu(0)>
[[ 0. 1. 2. 3.]
[ 4. 5. 6. 7.]
[ 8. 9. 10. 11.]
[ 2. 1. 4. 3.]
[ 1. 2. 3. 4.]
[ 4. 3. 2. 1.]]
<NDArray 6x4 @cpu(0)>
[[ 0. 1. 2. 3. 2. 1. 4. 3.]
[ 4. 5. 6. 7. 1. 2. 3. 4.]
[ 8. 9. 10. 11. 4. 3. 2. 1.]]
<NDArray 3x8 @cpu(0)>
[[0. 1. 0. 1.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]
<NDArray 3x4 @cpu(0)>
[66.]
<NDArray 1 @cpu(0)>
[22.494442]
<NDArray 1 @cpu(0)>
22.494442
Process finished with exit code 0
|