Tensorflow 中的批处理、batch_size、时间步长和特征之间有什么区别?

What is the difference between batch, batch_size, timesteps & features in Tensorflow?

我是深度学习的新手,对术语一窍不通。

在 Tensorflow 文档中,

对于 [RNN 层] https://www.tensorflow.org/api_docs/python/tf/keras/layers/RNN#input_shape

N-D tensor with shape [batch_size, timesteps, ...] 

对于 [LSTM 层] https://www.tensorflow.org/api_docs/python/tf/keras/layers/LSTM

inputs: A 3D tensor with shape [batch, timesteps, feature].
  1. 我理解 input_shape,我们不必指定 batch/batch 大小。 但我还是想知道batch和batch size的区别。

  2. 什么是时间步长与特征?

第一个维度总是批次吗? 2nd-D = 时间步长,3rd-D = 特征?

示例 1

data = array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
data = data.reshape((1, 5, 2))
print(data.shape) --> (1, 5, 2)

print(data)

[[[ 1  2]
  [ 3  4]
  [ 5  6]
  [ 7  8]
  [ 9 10]]]


model = Sequential()
model.add(LSTM(32, input_shape=(5, 2)))


示例 2

data1 = array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10,11])
n_features = 1
data1 = data1.reshape((len(data1), n_features))

print(data1)
# define generator
n_input = 2
generator = TimeseriesGenerator(data1, data1, length=n_input, stride=2, batch_size=10)

# number of batch
print('Batches: %d' % len(generator))
# OUT --> Batches: 1

# print each batch
for i in range(len(generator)):
   x, y = generator[i]
   print('%s => %s' % (x, y))
x, y = generator[0]
print(x.shape)

[[[ 1]
  [ 2]]

 [[ 3]
  [ 4]]

 [[ 5]
  [ 6]]

 [[ 7]
  [ 8]]

 [[ 9]
  [10]]] => [[ 3]
 [ 5]
 [ 7]
 [ 9]
 [11]]
(5, 2, 1)

# define model
model = Sequential()
model.add(LSTM(100, activation='relu', input_shape=(n_input, n_features)))

batch_sizebatch

之间的区别

在您引用的文档中,batch 表示 batch_size

timestepsfeature

的含义

看一眼 https://www.tensorflow.org/tutorials/structured_data/time_series(使用真实数据的天气预报示例!)将帮助您更多地了解时间序列数据。

feature 是您希望模型根据其进行预测的内容;在上面的预测例子中,它是压力、温度等的向量(数组)...

RNN/ LSTM 旨在处理时间序列。这就是为什么您需要将 timestepsfeature 一起提供给您的模型。 timesteps表示记录数据的时间;同样,在上面的示例中,数据每小时采样一次,因此 timesteps == 0 是第一个小时采集的数据,timesteps == 1 第二个小时采集的数据,...

输入/输出数据的维度顺序

在TensorFlow中,数据的第一个维度经常代表一个batch。

批处理轴之后是什么,取决于问题字段。通常,全局特征(如批量大小)先于元素特定特征(如图像大小)。

示例:

  • 时间序列数据采用 (batch_size, timesteps, feature) 格式。
  • 图像数据通常以 NHWC 格式表示:(batch_size, image_height, image_width, channels)

来自 https://www.tensorflow.org/guide/tensor#about_shapes :

While axes are often referred to by their indices, you should always keep track of the meaning of each. Often axes are ordered from global to local: The batch axis first, followed by spatial dimensions, and features for each location last. This way feature vectors are contiguous regions of memory.