Python 量子運算(二一):張量
2023/02/19
-----
Fig. 21.1. Tensor.
-----
純量:秩 0 的張量。
向量:秩 1 的張量。
矩陣:秩 2 的張量。
張量:秩 3 及以上稱為張量。
-----
代碼 21.1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | # Program 21.1:Tensor import numpy as np import matplotlib as mpl import matplotlib.pyplot as plt class Point: def __init__(self, x, y, z): self.x = x self.y = y self.z = z def Line(ax, A, B): ax.plot([A.x, B.x], [A.y, B.y], [A.z, B.z], 'b') return def Cube(ax, P1, P2): # parallel to x axis for z in range(P1.z, P2.z+1): S = Point(P1.x, P1.y, z) E = Point(P2.x, P1.y, z) Line(ax, S, E) for y in range(P1.y, P2.y+1): S = Point(P1.x, y, P2.z) E = Point(P2.x, y, P2.z) Line(ax, S, E) # parallel to y axis for x in range(P1.x, P2.x+1): S = Point(x, P1.y, P2.z) E = Point(x, P2.y, P2.z) Line(ax, S, E) for z in range(P1.z, P2.z+1): S = Point(P2.x, P1.y, z) E = Point(P2.x, P2.y, z) Line(ax, S, E) # parallel to z axis for x in range(P1.x, P2.x+1): S = Point(x, P1.y, P1.z) E = Point(x, P1.y, P2.z) Line(ax, S, E) for y in range(P1.y, P2.y+1): S = Point(P2.x, y, P1.z) E = Point(P2.x, y, P2.z) Line(ax, S, E) return def Set(ax, lim): ax.set_xlim([0, lim]) ax.set_ylim([0, lim]) ax.set_zlim([0, lim]) ax.set_axis_off() return def Subplot_1(): ax = fig.add_subplot(221, projection='3d') # 3d subplot P1 = Point(0, 0, 0) P2 = Point(1, 1, 1) Cube(ax, P1, P2) Set(ax, 6) plt.title('Rank 0 Tensor', color='r') ax.text(P2.x, P2.y, P2.z, 'scalar') ax.text(4, 0, -1, '(a)', fontsize=20) return def Subplot_2(): ax = fig.add_subplot(222, projection='3d') # 3d subplot P1 = Point(0, 0, 0) P2 = Point(4, 1, 1) Cube(ax, P1, P2) P3 = Point(5, 0, 0) P4 = Point(6, 1, 3) Cube(ax, P3, P4) Set(ax, 6) plt.title("Rank 1 Tensor", color='r') ax.text(0, 0, 3, 'Row Vecor(shape 1x4)', fontsize=16) ax.text(3, 0, 5, 'Column Vector(shape 3x1)', fontsize=16) ax.text(P4.x, P4.y, P4.z, 'vector') ax.text(4, 0, -1, '(b)', fontsize=20) return def Subplot_3(): ax = fig.add_subplot(223, projection='3d') # 3d subplot P1 = Point(0, 0, 0) P2 = Point(4, 1, 3) Cube(ax, P1, P2) Set(ax, 6) plt.title('Rank 2 Tensor', color='r') ax.text(P2.x, P2.y, P2.z, 'matrix') ax.text(4, 0, -1, '(c)', fontsize=20) return def Subplot_4(): ax = fig.add_subplot(224, projection='3d') # 3d subplot P1 = Point(0, 0, 0) P2 = Point(4, 5, 3) Cube(ax, P1, P2) Set(ax, 6) plt.title('Rank 3 Tensor', color='r') ax.text(P2.x, P2.y, P2.z, 'tensor') ax.text(4, 0, -1, '(d)', fontsize=20) return # figure setting mpl.rcParams['font.size'] = 40 fig = plt.figure(figsize=(16, 16)) Subplot_1() Subplot_2() Subplot_3() Subplot_4() # plt.savefig('/content/drive/My Drive/pqc/0021_001.png') plt.show() |
解說:
-----
References
[1] 淺談張量分解(一):如何簡單地進行張量分解? - 知乎
https://zhuanlan.zhihu.com/p/24798389
[2] 淺談張量分解(二):張量分解的數學基礎- 知乎
https://zhuanlan.zhihu.com/p/24824550
[3] 淺談張量分解(三):如何對稀疏矩陣進行奇異值分解? - 知乎
https://zhuanlan.zhihu.com/p/25512080
[4] 淺談張量分解(四):外積、Kronecker積和張量積- 知乎
https://zhuanlan.zhihu.com/p/26774182
[5] 淺談張量分解(五):稀疏張量的CP分解- 知乎
https://zhuanlan.zhihu.com/p/25067269
# 張量
[6] Tensor Explained with Python Numpy Examples - Data Analytics
https://vitalflux.com/tensor-explained-with-python-numpy-examples/
# 張量
[7] TensorFlow ranks and tensors | Python Machine Learning - Second Edition
# 向量的矩陣
[8] What is a Tensor? | Medium
https://furkangulsen.medium.com/what-is-a-tensor-ce8e78835d08
# 矩陣的向量
[9] Chapter 1. Tensor — TensorFlow.NET 0.6.0 documentation
https://tensorflownet.readthedocs.io/en/latest/Tensor.html
# 張量
[10] What is the Tensor in deep learning? | by Bala Venkatesh | DataDrivenInvestor
https://medium.datadriveninvestor.com/what-is-the-tensor-in-deep-learning-77c2af7224a1
# 繪圖
[11] Appendix: Figure Code | Python Data Science Handbook
https://jakevdp.github.io/PythonDataScienceHandbook/06.00-figure-code.html
-----
Python 量子運算(目錄)
https://mandhistory.blogspot.com/2022/01/quantum-computing.html
-----
沒有留言:
張貼留言
注意:只有此網誌的成員可以留言。