【问题标题】:What is the difference between batch, batch_size, timesteps & features in Tensorflow?Tensorflow 中的 batch、batch_size、timesteps 和 features 有什么区别?
【发布时间】:2022-01-08 19:46:22
【问题描述】:

我是深度学习的新手,我对术语完全感到困惑。

在 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,我们不必指定批次/批次大小。 但我仍然想知道批量和批量大小之间的区别。

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

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

示例 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)))

【问题讨论】:

    标签: tensorflow keras lstm


    【解决方案1】:

    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_size, timesteps, feature) 格式。
    • 图像数据通常以 NHWC 格式表示:(batch_size, image_height, image_width, channels)

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

    虽然轴通常由它们的索引来引用,但您应该始终 跟踪每个的含义。通常轴是从全局排序的 to local:首先是batch轴,然后是空间维度,然后 每个位置的功能最后。这种方式特征向量是 连续的内存区域。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-20
      • 2020-03-16
      • 1970-01-01
      • 2016-11-15
      • 2017-10-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多