且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Keras输入解释:input_shape、units、batch_size、dim等

更新时间:2023-12-01 23:33:34

单位:

神经元"或细胞"的数量,或层内部的任何内容.

它是每一层的一个属性,是的,它与输出形状有关(我们稍后会看到).在你的图片中,除了输入层,它在概念上与其他层不同,你有:

  • 隐藏层 1:4 个单元(4 个神经元)
  • 隐藏层2:4个单元
  • 最后一层:1 个单元

形状

形状是模型配置的结果.形状是表示数组或张量在每个维度中有多少元素的元组.

例:一个形状 (30,4,10) 表示一个数组或张量有 3 个维度,第一维包含 30 个元素,第二维包含 4 个元素,第三个10,总共30*4*10=1200个元素或数字.

输入形状

层与层之间流动的是张量.张量可以被视为具有形状的矩阵.

在 Keras 中,输入层本身不是层,而是张量.它是您发送到第一个隐藏层的起始张量.该张量必须与您的训练数据具有相同的形状.

示例:如果您有 30 张 50x50 像素的 RGB 图像(3 个通道),则输入数据的形状为 (30,50,50,3).然后你的输入层张量,必须有这个形状(请参阅keras 中的形状"部分中的详细信息).

每种类型的层都需要一定维数的输入:

  • Dense 层需要输入为 (batch_size, input_size)
    • (batch_size, optional,...,optional, input_size)
  • 2D 卷积层需要如下输入:
    • 如果使用channels_last:(batch_size, imageside1, imageside2, channels)
    • 如果使用channels_first:(batch_size, channels, imageside1, imageside2)
  • 一维卷积和循环层使用(batch_size, sequence_length, features)

现在,输入形状是您唯一必须定义的形状,因为您的模型无法识别它.根据您的训练数据,只有您知道这一点.

所有其他形状都是根据每一层的单位和特殊性自动计算的.

形状和单位之间的关系 - 输出形状

给定输入形状,所有其他形状都是层计算的结果.

每一层的单位"将定义输出形状(由该层产生的张量的形状,它将成为下一层的输入).

每种类型的层都以特定的方式工作.密集层具有基于单元"的输出形状,卷积层具有基于过滤器"的输出形状.但它始终基于某些图层属性.(有关每层输出的内容,请参阅文档)

让我们展示一下密集"层会发生什么,这是您图表中显示的类型.

密集层的输出形状为 (batch_size,units).所以,是的,单位,层的属性,也定义了输出形状.

  • 隐藏层 1:4 个单元,输出形状:(batch_size,4).
  • 隐藏层 2:4 个单元,输出形状:(batch_size,4).
  • 最后一层:1 个单元,输出形状:(batch_size,1).

权重

权重将根据输入和输出形状完全自动计算.同样,每种类型的层都以某种方式工作.但是权重将是一个矩阵,能够通过一些数学运算将输入形状转换为输出形状.

在密集层中,权重乘以所有输入.它是一个矩阵,每个输入一列,每个单元一行,但这对于基本工作通常并不重要.

在图像中,如果每个箭头上都有一个乘法数字,则所有数字一起将形成权重矩阵.

Keras 中的形状

之前,我举了一个例子,有 30 张图像,50x50 像素和 3 个通道,输入形状为 (30,50,50,3).

由于输入形状是您唯一需要定义的形状,因此 Keras 将在第一层要求它.

但在这个定义中,Keras 忽略了第一个维度,即批量大小.您的模型应该能够处理任何批量大小,因此您只需定义其他维度:

input_shape = (50,50,3)#不管我有多少张图片,每张图片都有这个形状

可选,或者当某些类型的模型需要时,您可以通过batch_input_shape=(30,50,50,3)batch_shape=(30,50,50,3).这将您的训练可能性限制在这种独特的批量大小上,因此应仅在真正需要时使用.

无论您选择哪种方式,模型中的张量都将具有批次维度.

所以,即使你使用了 input_shape=(50,50,3),当 keras 给你发送消息,或者当你打印模型摘要时,它会显示 (None,50,50,3).

第一个维度是批大小,它是None,因为它会根据您提供的训练示例数量而有所不同.(如果你明确定义了batch size,那么你定义的数字会出现而不是None)

此外,在高级工作中,当您实际直接对张量进行操作时(例如在 Lambda 层内或在损失函数中),批次大小维度将在那里.

  • 因此,在定义输入形状时,您可以忽略批量大小:input_shape=(50,50,3)
  • 直接对张量进行运算时,形状又会是(30,50,50,3)
  • 当 keras 向您发送消息时,形状将是 (None,50,50,3)(30,50,50,3),具体取决于它向您发送什么类型的消息.

昏暗

最后,什么是dim?

如果您的输入形状只有一维,则不需要将其作为元组提供,而是将 input_dim 作为标量数提供.

因此,在您的模型中,您的输入层有 3 个元素,您可以使用这两个元素中的任何一个:

  • input_shape=(3,) -- 当你只有一维时,逗号是必须的
  • input_dim = 3

但是当直接处理张量时,通常 dim 会指一个张量有多少维.例如,形状为 (25,10909) 的张量有 2 个维度.

在 Keras 中定义图像

Keras 有两种实现方式,Sequential 模型,或函数式 API Model.我不喜欢使用顺序模型,以后无论如何你都不得不忘记它,因为你会想要带有分支的模型.

PS:这里我忽略了其他方面,比如激活函数.

使用顺序模型:

from keras.models import Sequential从 keras.layers 导入 *模型 = 顺序()#从第一个隐藏层开始,因为输入实际上不是一个层#but 通知输入的形状,有 3 个元素.model.add(Dense(units=4,input_shape=(3,))) #hidden layer 1 with input#进一步层:model.add(Dense(units=4)) #隐藏层2model.add(Dense(units=1)) #输出层

使用函数式 API 模型:

from keras.models 导入模型从 keras.layers 导入 *#开始定义输入张量:inpTensor = Input((3,))#创建层并将输入张量传递给它们以获得输出张量:hidden1Out = Dense(units=4)(inpTensor)hidden2Out = Dense(units=4)(hidden1Out)finalOut = Dense(units=1)(hidden2Out)#定义模型的起点和终点模型 = 模型(输入张量,finalOut)

张量的形状

记住在定义层时忽略批量大小:

  • inpTensor:(None,3)
  • hidden1Out: (None,4)
  • hidden2Out: (None,4)
  • finalOut: (None,1)

For any Keras layer (Layer class), can someone explain how to understand the difference between input_shape, units, dim, etc.?

For example the doc says units specify the output shape of a layer.

In the image of the neural net below hidden layer1 has 4 units. Does this directly translate to the units attribute of the Layer object? Or does units in Keras equal the shape of every weight in the hidden layer times the number of units?

In short how does one understand/visualize the attributes of the model - in particular the layers - with the image below?

Units:

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:

  • Hidden layer 1: 4 units (4 neurons)
  • Hidden layer 2: 4 units
  • Last layer: 1 unit

Shapes

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.

The input shape

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.

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).

Each type of layer requires the input with a certain number of dimensions:

  • Dense layers require inputs as (batch_size, input_size)
    • or (batch_size, optional,...,optional, input_size)
  • 2D convolutional layers need inputs as:
    • if using channels_last: (batch_size, imageside1, imageside2, channels)
    • if using channels_first: (batch_size, channels, imageside1, imageside2)
  • 1D convolutions and recurrent layers use (batch_size, sequence_length, features)

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.

Relation between shapes and units - The output shape

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).

Each type of layer works in a particular way. Dense layers have output shape based on "units", convolutional layers have output shape based on "filters". But it's always based on some layer property. (See the documentation for what each layer outputs)

Let's show what happens with "Dense" layers, which is the type shown in your graph.

A dense layer has an output shape of (batch_size,units). So, yes, units, the property of the layer, also defines the output shape.

  • Hidden layer 1: 4 units, output shape: (batch_size,4).
  • Hidden layer 2: 4 units, output shape: (batch_size,4).
  • Last layer: 1 unit, output shape: (batch_size,1).

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.

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.

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).

Since the input shape is the only one you need to define, Keras will demand it in the first layer.

But in this definition, Keras ignores the first dimension, which is the batch size. Your model should be able to deal with any batch size, so you define only the other dimensions:

input_shape = (50,50,3)
    #regardless of how many images I have, each image has this shape        

Optionally, or when it's required by certain kinds of models, you can pass the shape containing the batch size via batch_input_shape=(30,50,50,3) or batch_shape=(30,50,50,3). This limits your training possibilities to this unique batch size, so it should be used only when really required.

Either way you choose, tensors in the model will have the batch dimension.

So, even if you used input_shape=(50,50,3), when keras sends you messages, or when you print the model summary, it will show (None,50,50,3).

The first dimension is the batch size, it's None because it can vary depending on how many examples you give for training. (If you defined the batch size explicitly, then the number you defined will appear instead of None)

Also, in advanced works, when you actually operate directly on the tensors (inside Lambda layers or in the loss function, for instance), the batch size dimension will be there.

  • So, when defining the input shape, you ignore the batch size: input_shape=(50,50,3)
  • When doing operations directly on tensors, the shape will be again (30,50,50,3)
  • 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

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.

So, in your model, where your input layer has 3 elements, you can use any of these two:

  • input_shape=(3,) -- The comma is necessary when you have only one dimension
  • input_dim = 3

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

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.

With the Sequential model:

from keras.models import Sequential  
from keras.layers import *  

model = Sequential()    

#start from the first hidden layer, since the input is not actually a layer   
#but inform the shape of the input, with 3 elements.    
model.add(Dense(units=4,input_shape=(3,))) #hidden layer 1 with input

#further layers:    
model.add(Dense(units=4)) #hidden layer 2
model.add(Dense(units=1)) #output layer   

With the functional API Model:

from keras.models import Model   
from keras.layers import * 

#Start defining the input tensor:
inpTensor = Input((3,))   

#create the layers and pass them the input tensor to get the output tensor:    
hidden1Out = Dense(units=4)(inpTensor)    
hidden2Out = Dense(units=4)(hidden1Out)    
finalOut = Dense(units=1)(hidden2Out)   

#define the model's start and end points    
model = Model(inpTensor,finalOut)

Shapes of the tensors

Remember you ignore batch sizes when defining layers:

  • inpTensor: (None,3)
  • hidden1Out: (None,4)
  • hidden2Out: (None,4)
  • finalOut: (None,1)