是否可以在 keras 图的中途更改 batch_size?
Is it possible to change the batch_size half way through a keras graph?
是否可以在图表中途动态更改批量大小?
我的系统使用单词生成句子表示,然后使用句子生成文档表示。
如果文档包含 20 个句子,每个句子有 50 个词(为简单起见,词向量大小为 1)。我的批量大小为 10 个文档:
我的初始输入将是 batch_shape = (10 * 20, 50, 1)
我使用批量大小=200,time_steps=50 的 LSTM,然后对隐藏输出执行加权求和以生成形状为 (10*20, 100) 并行
- 在这里我想将我的张量重塑为 (10, 20, 100)
- 为了输入批量大小为10的句子LSTM,time_steps 20
- 并执行 LSTM 的加权和以生成文档表示
我已经尝试了重塑层和 keras.backend 重塑层,但是 keras 似乎坚持我的批量大小在整个图表中保持不变 (200),即使操作本身感觉它们应该是合法的.
实际错误是:
ValueError: 无法将输入数组从形状 (10, 20, 100) 广播到形状 (200, 20, 100)。 也就是说,在让我重塑我的张量之后,它现在正试图将它洗牌返回批量大小为 200
的张量
您可以在您的模型中使用 tf.reshpae
,它可以让您改变张量的形状甚至批量维度,但您必须使所有内容保持一致,以便在训练期间数据流是正确的。
这是一个虚拟网络:
from tensorflow.keras import layers
from tensorflow.keras import models
import tensorflow as tf
init_batch_sz = 10 # let's assume initial batch size is 10
ip1 = layers.Input((20,10))
dense = layers.Dense(10)(ip1)
res = tf.reshape(dense, (init_batch_sz//2, -1, -1))
model = models.Model(ip1, res)
model.summary()
Model: "model_3"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_8 (InputLayer) [(None, 20, 10)] 0
_________________________________________________________________
dense_1 (Dense) (None, 20, 10) 110
_________________________________________________________________
tf_op_layer_Reshape_1 (Tenso [(5, None, None)] 0
=================================================================
Total params: 110
Trainable params: 110
Non-trainable params: 0
但是你不应该使用 fit
来训练这样的网络,因为你会得到一个错误。
其他一些选项是:
- 使用虚拟 1 批次维度。
from tensorflow.keras import layers
from tensorflow.keras import models
import tensorflow as tf
import numpy as np
init_batch_sz = 10 # let's assume initial batch size is 10
ip1 = layers.Input((10, 20,10))
dense = layers.Dense(10)(ip1)
res = tf.reshape(dense, (-1, init_batch_sz//2, 40, 10)) # you need to make some calculations here to get the correct output_shape
model = models.Model(ip1, res)
model.summary()
x = np.random.randn(1, 10, 20, 10) # dummy 1 batch
y = np.random.randn(1, 5, 40, 10) # dummy 1 batch
model.compile('adam', 'mse')
model.fit(x, y)
Model: "model_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_2 (InputLayer) [(None, 10, 20, 10)] 0
_________________________________________________________________
dense_1 (Dense) (None, 10, 20, 10) 110
_________________________________________________________________
tf_op_layer_Reshape_1 (Tenso [(None, 5, 40, 10)] 0
=================================================================
Total params: 110
Trainable params: 110
Non-trainable params: 0
_________________________________________________________________
1/1 [==============================] - 0s 1ms/step - loss: 1.9959
<tensorflow.python.keras.callbacks.History at 0x7f600d0eb630>
是否可以在图表中途动态更改批量大小?
我的系统使用单词生成句子表示,然后使用句子生成文档表示。
如果文档包含 20 个句子,每个句子有 50 个词(为简单起见,词向量大小为 1)。我的批量大小为 10 个文档:
我的初始输入将是 batch_shape = (10 * 20, 50, 1)
我使用批量大小=200,time_steps=50 的 LSTM,然后对隐藏输出执行加权求和以生成形状为 (10*20, 100) 并行
- 在这里我想将我的张量重塑为 (10, 20, 100)
- 为了输入批量大小为10的句子LSTM,time_steps 20
- 并执行 LSTM 的加权和以生成文档表示
我已经尝试了重塑层和 keras.backend 重塑层,但是 keras 似乎坚持我的批量大小在整个图表中保持不变 (200),即使操作本身感觉它们应该是合法的.
实际错误是: ValueError: 无法将输入数组从形状 (10, 20, 100) 广播到形状 (200, 20, 100)。 也就是说,在让我重塑我的张量之后,它现在正试图将它洗牌返回批量大小为 200
的张量您可以在您的模型中使用 tf.reshpae
,它可以让您改变张量的形状甚至批量维度,但您必须使所有内容保持一致,以便在训练期间数据流是正确的。
这是一个虚拟网络:
from tensorflow.keras import layers
from tensorflow.keras import models
import tensorflow as tf
init_batch_sz = 10 # let's assume initial batch size is 10
ip1 = layers.Input((20,10))
dense = layers.Dense(10)(ip1)
res = tf.reshape(dense, (init_batch_sz//2, -1, -1))
model = models.Model(ip1, res)
model.summary()
Model: "model_3"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_8 (InputLayer) [(None, 20, 10)] 0
_________________________________________________________________
dense_1 (Dense) (None, 20, 10) 110
_________________________________________________________________
tf_op_layer_Reshape_1 (Tenso [(5, None, None)] 0
=================================================================
Total params: 110
Trainable params: 110
Non-trainable params: 0
但是你不应该使用 fit
来训练这样的网络,因为你会得到一个错误。
其他一些选项是:
- 使用虚拟 1 批次维度。
from tensorflow.keras import layers
from tensorflow.keras import models
import tensorflow as tf
import numpy as np
init_batch_sz = 10 # let's assume initial batch size is 10
ip1 = layers.Input((10, 20,10))
dense = layers.Dense(10)(ip1)
res = tf.reshape(dense, (-1, init_batch_sz//2, 40, 10)) # you need to make some calculations here to get the correct output_shape
model = models.Model(ip1, res)
model.summary()
x = np.random.randn(1, 10, 20, 10) # dummy 1 batch
y = np.random.randn(1, 5, 40, 10) # dummy 1 batch
model.compile('adam', 'mse')
model.fit(x, y)
Model: "model_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_2 (InputLayer) [(None, 10, 20, 10)] 0
_________________________________________________________________
dense_1 (Dense) (None, 10, 20, 10) 110
_________________________________________________________________
tf_op_layer_Reshape_1 (Tenso [(None, 5, 40, 10)] 0
=================================================================
Total params: 110
Trainable params: 110
Non-trainable params: 0
_________________________________________________________________
1/1 [==============================] - 0s 1ms/step - loss: 1.9959
<tensorflow.python.keras.callbacks.History at 0x7f600d0eb630>