请 [注册] 或 [登录]  | 返回主站

量化交易吧 /  数理科学 帖子:3364712 新帖:0

张量计算

你在说什么呢发表于:5 月 10 日 16:15回复(1)

结合深度学习神经网络,

NN.png

6.PNG

复杂的公式里面涉及到四类张量运算,从里到外按顺序来看:

  1. 重塑形状 (reshape)
  2. 张量点乘 (tensor dot)
  3. 广播机制 (boardcasting)
  4. 元素层面 (element-wise)

具体文章见我“王的机器”公众号里的 张量 101

import numpy as np
import tensorflow as tf
import tensorflow.keras as keras
from tensorflow.keras.datasets import mnist
from tensorflow.keras import backend as K
from IPython.display import Image
print(tf.__version__)
print(tf.keras.__version__)
1.11.0
2.1.6-tf
Image("张量 101/NN.png", width=800, height=800)
Image("张量 101/NN computation.PNG", width=800, height=800)

复杂的公式里面涉及到四类张量运算,从里到外按顺序来看:

  1. 重塑形状 (reshape)
  2. 张量点乘 (tensor dot)
  3. 广播机制 (boardcasting)
  4. 元素层面 (element-wise)

重塑形状 (reshape)¶

Image("张量 101/reshape.png", width=800, height=800)

reshape 的例子¶

x = np.array( [[0, 1], [2, 3], [4, 5]] )
print(x.shape)
x
(3, 2)
array([[0, 1],
       [2, 3],
       [4, 5]])
x = x.reshape( 6, 1 )
print(x.shape)
x
(6, 1)
array([[0],
       [1],
       [2],
       [3],
       [4],
       [5]])
x = x.reshape( 2, -1 )
print(x.shape)
x
(2, 3)
array([[0, 1, 2],
       [3, 4, 5]])

张量点乘 (tensor dot)¶

x = np.array( [1, 2, 3] )
y = np.array( [3, 2, 1] )
z = np.dot(x,y)
print(z.shape)
z
()
10
x = np.array( [1, 2, 3] )
y = np.array( [[3, 2, 1], [1, 1, 1]] )
z = np.dot(y,x)
print(z.shape)
z
(2,)
array([10,  6])
x = np.array( [[1, 2, 3], [1, 2, 3], [1, 2, 3]] )
y = np.array( [[3, 2, 1], [1, 1, 1]] )
z = np.dot(y,x)
print(z.shape)
z
(2, 3)
array([[ 6, 12, 18],
       [ 3,  6,  9]])
x = np.ones( shape=(2, 3, 4) )
y = np.array( [1, 2, 3, 4] )
z = np.dot(x,y)
print(z.shape)
z
(2, 3)
array([[10., 10., 10.],
       [10., 10., 10.]])
x = np.random.normal( 0, 1, size=(2, 3, 4) )
y = np.random.normal( 0, 1, size=(4, 2) )
z = np.dot(x,y)
print(z.shape)
z
(2, 3, 2)
array([[[ 0.27963726,  4.93637739],
        [ 0.62224755,  0.36002537],
        [-0.33935473, -2.43217814]],

       [[-2.82055388, -4.42610199],
        [-0.12477807,  2.91632184],
        [ 1.00213325,  1.04066881]]])

广播机制 (boardcasting)¶

Image("张量 101/boardcasting.png", width=800, height=800)

boardcasting 的例子¶

x = np.arange(1,4).reshape(3,1)
y = np.arange(1,3).reshape(1,2)
print( (x + y).shape )
x + y
(3, 2)
array([[2, 3],
       [3, 4],
       [4, 5]])
x = np.arange(1,7).reshape(3,2)
y = np.arange(1,3).reshape(2)
print( (x + y).shape )
x + y
(3, 2)
array([[2, 4],
       [4, 6],
       [6, 8]])
x = np.arange(1,25).reshape(2,3,4)
y = np.arange(1,5).reshape(4)
print( (x + y).shape )
x + y
(2, 3, 4)
array([[[ 2,  4,  6,  8],
        [ 6,  8, 10, 12],
        [10, 12, 14, 16]],

       [[14, 16, 18, 20],
        [18, 20, 22, 24],
        [22, 24, 26, 28]]])
x = np.arange(1,25).reshape(2,3,4)
y = np.arange(1,13).reshape(3,4)
print( (x + y).shape )
x + y
(2, 3, 4)
array([[[ 2,  4,  6,  8],
        [10, 12, 14, 16],
        [18, 20, 22, 24]],

       [[14, 16, 18, 20],
        [22, 24, 26, 28],
        [30, 32, 34, 36]]])

元素层面 (element-wise)¶

x = np.random.normal( 0, 1, size=(2,3) )
y = np.random.normal( 0, 1, size=(2,3) )
x
array([[-0.59743703,  1.0801919 ,  1.1655119 ],
       [-0.67385601,  0.70086319, -0.84998934]])
y
array([[ 0.10656352, -0.89703461,  1.02297872],
       [-1.15690704, -0.71015809,  0.748454  ]])
x + y
array([[-0.49087351,  0.18315729,  2.18849062],
       [-1.83076305, -0.0092949 , -0.10153534]])
x - y
array([[-0.70400055,  1.97722651,  0.14253318],
       [ 0.48305103,  1.41102129, -1.59844333]])
x * y
array([[-0.06366499, -0.96896952,  1.19229387],
       [ 0.77958876, -0.49772367, -0.63617792]])
x / y
array([[-5.6063936 , -1.20418085,  1.13933152],
       [ 0.5824634 , -0.9869115 , -1.1356601 ]])
np.exp(x)
array([[0.55022003, 2.9452447 , 3.20756441],
       [0.50973922, 2.01549171, 0.42741949]])
def softmax(x, axis=-1):
    e_x = np.exp(x - np.max(x,axis,keepdims=True))
    return e_x / e_x.sum(axis,keepdims=True)
y = softmax( x, axis=0 )
y
array([[0.51909545, 0.59371119, 0.88241503],
       [0.48090455, 0.40628881, 0.11758497]])
np.sum( y, axis=0 )
array([1., 1., 1.])
y = softmax( x, axis=1 )
y
array([[0.08208528, 0.43939011, 0.47852461],
       [0.17263785, 0.68260424, 0.1447579 ]])
np.sum( y, axis=1 )
array([1., 1.])

回到 MNIST 的例子¶

# input image dimensions and class dimensions
n_W, n_H = 28, 28
n_y = 10
# the data, shuffled and split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train.shape
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz
11493376/11490434 [==============================] - 48s 4us/step
(60000, 28, 28)

0. 起点¶

Image("张量 101/1.PNG", width=800, height=800)

1. 重塑¶

X = x_train.reshape( x_train.shape[0], -1 ).T
X.shape
(784, 60000)
Image("张量 101/2.PNG", width=800, height=800)

2. 点乘¶

W = np.random.normal( 0, 1, size=(n_y, X.shape[0]) )
b = np.random.normal( 0, 1, size=(n_y, 1) )
print( W.shape )
print( b.shape )
(10, 784)
(10, 1)
WX = np.dot(W,X)
WX.shape
(10, 60000)
Image("张量 101/3.PNG", width=800, height=800)

3. 广播¶

Z = WX + b
Z.shape
(10, 60000)
Image("张量 101/4.PNG", width=800, height=800)

4. 元素层面¶

tensor_y = tf.nn.softmax( Z, axis=0 )
y = keras.backend.eval(tensor_y)
y.shape
(10, 60000)
np.sum( y, axis=0 )
array([1., 1., 1., ..., 1., 1., 1.])
Image("张量 101/5.PNG", width=800, height=800)

总结¶

Image("张量 101/6.PNG", width=800, height=800)

全部回复

0/140

量化课程

    移动端课程