程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Shape usage in Python

編輯:Python

One dimensional matrix

Two dimensional matrix


More details :Python Li for shape() The understanding of the

Python Li for shape() The understanding of the

(1)shape() meaning

In the author debug Deep learning of relevant code , It's easy to show up shape() Something in this form , Used to inform the form of output data , because shape() There are different numbers in , Often shape(?,64,512) Such data exist , So I checked some information on the Internet , Make a more understandable explanation :

import numpy as np
a = np.array([[[1,2,3],[4,5,6]]])
print(a.shape)
(1, 2, 3)

Indicates that the array has 1 individual , yes 2 That's ok 3 Array of columns . every last shape The number in corresponds to a pair of brackets in the array , The first number 1 Represents the outermost bracket . And so on , Numbers 2 Indicates the second level of brackets , Numbers 3 Brackets indicating the innermost layer . If you define array When the outermost bracket is removed, the output is shape by (2, 3).

(2) Distinguish between one-dimensional array and two-dimensional array

A few brackets are a few dimensional array , namely shape There are several numbers in the result .
So in the above , There are three pairs of brackets , yes 3 Dimension group ,shape() There is 3 Number .

a = np.array([1,2]) #a.shape value (2,), It means one-dimensional array , Array has 2 Elements .
b = np.array([[1],[2]]) #b.shape The value is (2,1), It means a two-dimensional array , Each row has 1 Elements .
c = np.array([[1,2]]) #c.shape The value is (1,2), It means a two-dimensional array , Each row has 2 Elements .

(3)shape() in “?” The meaning of

And in the debug Relevant procedures , There may be shape(?,2,3) This means that each array is 2 That's ok 3 Column , This is the front. “?” It represents the number of batches , if 1 Then there are 1 individual , by 2 There are two , But in debug I didn't know how many , So “?” The form shows .

(4)shape() There are four numbers in , The first number represents the time series ( Number of batches )

from keras.models import Input,Model
from keras.layers import Dense,Conv2D,TimeDistributed
input_ = Input(shape=(12,32,32,3))
out = TimeDistributed(Conv2D(filters=32,kernel_size=(3,3),padding='same'))(input_)
model = Model(inputs=input_,outputs=out)
model.summary()

And here ,shape() There are four numbers in . first 12 Represents time series ,32,32,3 It means high , wide , The channel number . Convolution operation uses TimeDistributed It's quite similar to this 12 Time series share a convolution layer parameter information , No matter what the time series value is , The total number of parameters is still certain . There are altogether 896 Parameters , Convolution kernel weights Yes 3×3×3×32=864 individual , Convolution kernel bias Yes 32 individual .

attach TimeDistributed explain

About TimeDistributed There is a popular example to explain :
Consider a batch 32 Samples , Each of these samples is a sample made up of 16 Composed of four dimensions 10 A sequence of vectors . The layer's batch input shape then (32, 10, 16).

It's understandable , The input data is a characteristic equation ,X1+X2+…+X10=Y, From a matrix point of view , Take out the unknowns , Namely 10 Vector , Each vector has 16 Dimensions , this 16 The first dimension is evaluation Y Of 16 Feature directions .

TimeDistributed The function of the layer is to Dense Layer is applied here 10 On a specific vector , For each vector, a Dense operation , Suppose the following code :

model = Sequential()model.add(TimeDistributed(Dense(8), input_shape=(10, 16)))

Output or 10 Vector , But the dimension of the output is determined by 16 Turned into 8, That is to say (32,10,8).

TimeDistributed The layer gives the model a one to many , Many to many ability , The dimension of the model is added .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved