Outils pour utilisateurs

Outils du site


keras_input_explanation

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentesRévision précédente
Prochaine révision
Révision précédente
keras_input_explanation [2020/10/03 11:53] – [Input Dimension Clarified] sergekeras_input_explanation [2020/12/27 15:14] (Version actuelle) serge
Ligne 1: Ligne 1:
 ====== Keras input explanation: input_shape, units, batch_size, dim, etc ====== ====== Keras input explanation: input_shape, units, batch_size, dim, etc ======
 +<WRAP center round box 60% centeralign>
 +**{{tagpage>ia|Intelligence Artificielle}}**     **[[http://translate.google.com/translate?hl=&sl=auto&tl=en&u=https%3A%2F%2Fressources.labomedia.org%2Fkeras_input_explanation|English Version]]**
 +</WRAP>
 +<WRAP center round box 60% centeralign>
 +**[[les_pages_intelligence_artificielle_en_details|Les Pages Intelligence Artificielle en détails]]**
 +</WRAP>
 +
 Mise en forme d'un post de **stackoverflow.com** non traduit en français, car très technique ce qui ne se comprend qu'en anglais. Mise en forme d'un post de **stackoverflow.com** non traduit en français, car très technique ce qui ne se comprend qu'en anglais.
  
 **[[https://stackoverflow.com/questions/44747343/keras-input-explanation-input-shape-units-batch-size-dim-etc|Keras input explanation: input_shape, units, batch_size, dim, etc]]** **[[https://stackoverflow.com/questions/44747343/keras-input-explanation-input-shape-units-batch-size-dim-etc|Keras input explanation: input_shape, units, batch_size, dim, etc]]**
 +
 +=====Ressources=====
 +  * [[https://keras.io/guides/working_with_rnns/|Working with RNNs]]
 +  * [[https://keras.io/api/layers/|Keras layers API]]
 +  * [[https://keras.io/examples/|Code examples]]
  
 =====Question===== =====Question=====
Ligne 13: Ligne 25:
 {{ :media_10:input_explanation.jpg?600 |}} {{ :media_10:input_explanation.jpg?600 |}}
  
-=====Réponse 1=====+=====Définitions=====
  
-===Units===+====Units====
 The amount of "neurons", or "cells", or whatever the layer has inside it. \\ The amount of "neurons", or "cells", or whatever the layer has inside it. \\
 It's a property of each layer, and yes, it's related to the output shape (as we will see later). In your picture, except for the input layer, which is conceptually different from other layers, you have:  It's a property of each layer, and yes, it's related to the output shape (as we will see later). In your picture, except for the input layer, which is conceptually different from other layers, you have: 
Ligne 22: Ligne 34:
   * Last layer: 1 unit    * Last layer: 1 unit 
  
-===Shapes===+====Shapes====
 Shapes are consequences of the model's configuration. Shapes are tuples representing how many elements an array or tensor has in each dimension.\\  Shapes are consequences of the model's configuration. Shapes are tuples representing how many elements an array or tensor has in each dimension.\\ 
 Ex: a shape (30,4,10) means an array or tensor with 3 dimensions, containing 30 elements in the first dimension, 4 in the second and 10 in the third, totaling 30*4*10 = 1200 elements or numbers.  Ex: a shape (30,4,10) means an array or tensor with 3 dimensions, containing 30 elements in the first dimension, 4 in the second and 10 in the third, totaling 30*4*10 = 1200 elements or numbers. 
  
-===The input shape===+====The input shape====
 What flows between layers are tensors. Tensors can be seen as matrices, with shapes. \\ What flows between layers are tensors. Tensors can be seen as matrices, with shapes. \\
-In Keras, the input layer itself is not a layer, but a tensor. It's the starting tensor you send to the first hidden layer. This tensor must have the same shape as your training data. \\ +In Keras, the input layer itself is not a layer, but a tensor. It's the starting tensor you send to the first hidden layer. **This tensor must have the same shape as your training data**. \\ 
-Example: if you have 30 images of 50x50 pixels in RGB (3 channels), the shape of your input data is (30,50,50,3). Then your input layer tensor, must have this shape (see details in the "shapes in keras" section). \\+Example: if you have 30 images of 50x50 pixels in RGB (3 channels), the shape of your input data is (30,50,50,3). Then your input layer tensor, must have this shape (see [[keras_input_explanation#shapes_in_keras| Shapes in Keras]]). \\
 Each type of layer requires the input with a certain number of dimensions: Each type of layer requires the input with a certain number of dimensions:
   * Dense layers require inputs as (batch_size, input_size)    * Dense layers require inputs as (batch_size, input_size) 
Ligne 37: Ligne 49:
     *if using channels_first: (batch_size, channels, imageside1, imageside2)      *if using channels_first: (batch_size, channels, imageside1, imageside2) 
   * 1D convolutions and recurrent layers use (batch_size, sequence_length, features)    * 1D convolutions and recurrent layers use (batch_size, sequence_length, features) 
-    *Details on how to prepare data for recurrent layers +    *Details on [[https://stackoverflow.com/questions/38714959/understanding-keras-lstms/50235563#50235563|how to prepare data for recurrent layers ]] 
 Now, the input shape is the only one you must define, because your model cannot know it. Only you know that, based on your training data. \\ Now, the input shape is the only one you must define, because your model cannot know it. Only you know that, based on your training data. \\
 All the other shapes are calculated automatically based on the units and particularities of each layer.  All the other shapes are calculated automatically based on the units and particularities of each layer. 
  
-===Relation between shapes and units - The output shape===+====Relation between shapes and units - The output shape====
 Given the input shape, all other shapes are results of layers calculations. \\ Given the input shape, all other shapes are results of layers calculations. \\
 The "units" of each layer will define the output shape (the shape of the tensor that is produced by the layer and that will be the input of the next layer). \\ The "units" of each layer will define the output shape (the shape of the tensor that is produced by the layer and that will be the input of the next layer). \\
Ligne 51: Ligne 64:
   * Last layer: 1 unit, output shape: (batch_size,1).    * Last layer: 1 unit, output shape: (batch_size,1). 
  
-===Weights===+====Weights====
 Weights will be entirely automatically calculated based on the input and the output shapes. Again, each type of layer works in a certain way. But the weights will be a matrix capable of transforming the input shape into the output shape by some mathematical operation. \\ Weights will be entirely automatically calculated based on the input and the output shapes. Again, each type of layer works in a certain way. But the weights will be a matrix capable of transforming the input shape into the output shape by some mathematical operation. \\
 In a dense layer, weights multiply all inputs. It's a matrix with one column per input and one row per unit, but this is often not important for basic works. \\ In a dense layer, weights multiply all inputs. It's a matrix with one column per input and one row per unit, but this is often not important for basic works. \\
 In the image, if each arrow had a multiplication number on it, all numbers together would form the weight matrix. In the image, if each arrow had a multiplication number on it, all numbers together would form the weight matrix.
  
-===Shapes in Keras===+====Shapes in Keras====
 Earlier, I gave an example of 30 images, 50x50 pixels and 3 channels, having an input shape of (30,50,50,3). \\ Earlier, I gave an example of 30 images, 50x50 pixels and 3 channels, having an input shape of (30,50,50,3). \\
 Since the input shape is the only one you need to define, Keras will demand it in the first layer.  Since the input shape is the only one you need to define, Keras will demand it in the first layer. 
Ligne 72: Ligne 85:
   * When keras sends you a message, the shape will be (None,50,50,3) or (30,50,50,3), depending on what type of message it sends you.   * When keras sends you a message, the shape will be (None,50,50,3) or (30,50,50,3), depending on what type of message it sends you.
  
-===Dim===+====Dim====
 And in the end, what is dim? \\ And in the end, what is dim? \\
 If your input shape has only one dimension, you don't need to give it as a tuple, you give input_dim as a scalar number. \\ If your input shape has only one dimension, you don't need to give it as a tuple, you give input_dim as a scalar number. \\
Ligne 80: Ligne 93:
 But when dealing directly with the tensors, often dim will refer to how many dimensions a tensor has. For instance a tensor with shape (25,10909) has 2 dimensions.  But when dealing directly with the tensors, often dim will refer to how many dimensions a tensor has. For instance a tensor with shape (25,10909) has 2 dimensions. 
  
-====Defining your image in Keras====+=====Defining your image in Keras=====
 Keras has two ways of doing it, Sequential models, or the functional API Model. I don't like using the sequential model, later you will have to forget it anyway because you will want models with branches.  Keras has two ways of doing it, Sequential models, or the functional API Model. I don't like using the sequential model, later you will have to forget it anyway because you will want models with branches. 
 PS: here I ignored other aspects, such as activation functions. PS: here I ignored other aspects, such as activation functions.
Ligne 90: Ligne 103:
 model = Sequential()     model = Sequential()    
  
-#start from the first hidden layer, since the input is not actually a layer    +Start from the first hidden layer, since the input is not actually a layer    
-#but inform the shape of the input, with 3 elements.     +# but inform the shape of the input, with 3 elements.     
-model.add(Dense(units=4,input_shape=(3,))) #hidden layer 1 with input+model.add(Dense(units=4,input_shape=(3,))) # hidden layer 1 with input
  
-#further layers:     +Further layers:     
-model.add(Dense(units=4)) #hidden layer 2 +model.add(Dense(units=4)) # hidden layer 2 
-model.add(Dense(units=1)) #output layer    +model.add(Dense(units=1)) # output layer  
-With the functional API Model:+</code> 
 +   
 +**With the functional API Model:** 
 + 
 +<code python>
 from keras.models import Model    from keras.models import Model   
 from keras.layers import *  from keras.layers import * 
  
-#Start defining the input tensor:+# Start defining the input tensor:
 inpTensor = Input((3,))    inpTensor = Input((3,))   
  
-#create the layers and pass them the input tensor to get the output tensor:    +Create the layers and pass them the input tensor to get the output tensor:    
 hidden1Out = Dense(units=4)(inpTensor)     hidden1Out = Dense(units=4)(inpTensor)    
 hidden2Out = Dense(units=4)(hidden1Out)     hidden2Out = Dense(units=4)(hidden1Out)    
 finalOut = Dense(units=1)(hidden2Out)    finalOut = Dense(units=1)(hidden2Out)   
  
-#define the model's start and end points     +Define the model's start and end points     
-model = Model(inpTensor,finalOut)+model = Model(inpTensor, finalOut)
 </code> </code>
  
 ====Shapes of the tensors==== ====Shapes of the tensors====
-Remember you ignore batch sizes when defining layers: +Remember you ignore batch sizes when defining layers, so set it to None
   * inpTensor: (None,3)    * inpTensor: (None,3) 
   * hidden1Out: (None,4)    * hidden1Out: (None,4) 
Ligne 142: Ligne 159:
   * Examples, video frames that should be treated equally. Image pixels when you want to process only the color channels for each pixel instead of mixing pixels, etc.    * Examples, video frames that should be treated equally. Image pixels when you want to process only the color channels for each pixel instead of mixing pixels, etc. 
  
-=====Réponse 2===== +=====Input Dimension Clarified=====
 It (the word dimension alone) can refer to: It (the word dimension alone) can refer to:
   - **The dimension of Input Data (or stream)** such as # N of sensor axes to beam the time series signal, or RGB color channel (3): suggested word=> "InputStream Dimension"   - **The dimension of Input Data (or stream)** such as # N of sensor axes to beam the time series signal, or RGB color channel (3): suggested word=> "InputStream Dimension"
-  - **The total number /length of Input Features** (or Input layer) (28 x 28 = 784 for the MINST color image) or 3000 in the FFT transformed Spectrum Values, or +  - **The total number /length of Input Features** (or Input layer) (28 x 28 = 784 for the MINST color image) or 3000 in the FFT transformed Spectrum Values, or "Input Layer / Input Feature Dimension"
-"Input Layer / Input Feature Dimension"+
   - **The dimensionality** (# of dimension) of the input (typically 3D as expected in Keras LSTM) or (#RowofSamples, #of Senors, #of Values..) 3 is the answer.   - **The dimensionality** (# of dimension) of the input (typically 3D as expected in Keras LSTM) or (#RowofSamples, #of Senors, #of Values..) 3 is the answer.
-"N Dimensionality of Input" +  - "N Dimensionality of Input" **The SPECIFIC Input Shape** (eg. (30,50,50,3) in this unwrapped input image data, or (30, 250, 3) if unwrapped Keras: 
-  - **The SPECIFIC Input Shape** (eg. (30,50,50,3) in this unwrapped input image data, or (30, 250, 3) if unwrapped Keras: +
-  +
 Keras has its input_dim refers to the Dimension of Input Layer / Number of Input Feature Keras has its input_dim refers to the Dimension of Input Layer / Number of Input Feature
 +
 <code python> <code python>
 model = Sequential() model = Sequential()
Ligne 166: Ligne 181:
  
  
-{{tag>sb ia}}+{{tag>sb ia keras}}
keras_input_explanation.1601726012.txt.gz · Dernière modification : 2020/10/03 11:53 de serge