![](/img/trans.png)
[英]Tensorflow. Difference between [batch_size, 1] & [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].
我了解 input_shape,我們不必指定批次/批次大小。 但我仍然想知道批量和批量大小之間的區別。
什么是時間步長與特征?
第一維度總是批次嗎? 第 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)))
batch_size
與batch
之間的區別在您引用的文檔中, batch
表示batch_size
。
timesteps
和feature
的含義看一眼https://www.tensorflow.org/tutorials/structured_data/time_series (帶有真實世界數據的天氣預報示例。)將幫助您了解有關時間序列數據的更多信息。
feature
是您希望 model 從中進行預測的內容; 在上面的預測示例中,它是壓力、溫度等的向量(數組)......
RNN/LSTM 旨在處理時間序列。 這就是為什么您需要將timesteps
以及feature
給您的 model。 timesteps
表示記錄數據的時間; 同樣,在上面的示例中,數據每小時采樣一次,因此timesteps == 0
是第一個小時獲取的數據, timesteps == 1
第二個小時,...
在 TensorFlow 中,數據的第一個維度通常代表一個批次。
批處理軸之后的內容取決於問題字段。 一般來說,全局特征(如批量大小)優先於特定元素的特征(如圖像大小)。
例子:
(batch_size, timesteps, feature)
格式。(batch_size, image_height, image_width, channels)
。來自https://www.tensorflow.org/guide/tensor#about_shapes :
雖然軸通常由它們的索引引用,但您應該始終跟蹤每個軸的含義。 通常軸從全局到局部排序:首先是批處理軸,然后是空間維度,最后是每個位置的特征。 這種方式特征向量是 memory 的連續區域。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.