繁簡切換您正在訪問的是FX168財經網,本網站所提供的內容及信息均遵守中華人民共和國香港特別行政區當地法律法規。

FX168财经网>人物频道>帖子

张量计算

作者/adasda 2019-05-10 16:15 0 来源: FX168财经网人物频道

结合深度学习神经网络,

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)
分享到:
举报财经168客户端下载

全部回复

0/140

投稿 您想发表你的观点和看法?

更多人气分析师

  • 张亦巧

    人气2144文章4145粉丝45

    暂无个人简介信息

  • 梁孟梵

    人气2152文章3177粉丝39

    qq:2294906466 了解群指导添加微信mfmacd

  • 指导老师

    人气1856文章4423粉丝52

    暂无个人简介信息

  • 李冉晴

    人气2296文章3821粉丝34

    李冉晴,专业现贷实盘分析师。

  • 刘钥钥1

    人气2016文章3119粉丝34

    专业从事现货黄金、现货白银模似实盘操作分析指导

  • 张迎妤

    人气1896文章3305粉丝34

    个人专注于行情技术分析,消息面解读剖析,给予您第一时间方向...

  • 金泰铬J

    人气2320文章3925粉丝51

    投资问答解咨询金泰铬V/信tgtg67即可获取每日的实时资讯、行情...

  • 金算盘

    人气2696文章7761粉丝125

    高级分析师,混过名校,厮杀于股市和期货、证券市场多年,专注...

  • 金帝财神

    人气4728文章8329粉丝118

    本文由资深分析师金帝财神微信:934295330,指导黄金,白银,...