我有一个用例,在这种情况下,我需要从训练有素的LSTM模型(TensorFlow = 2.8和Python3.9)中进行预测,其中数据集很大,并在我的GPU卡上导致OOM错误。我的转变是对每一批进行预测,并将结果张量存储在Python3列表中:
# Create TF dataset to iterate over-
train_dataset = tf.data.Dataset.from_tensor_slices(X_train).batch(batch_size)
# batch_size = 256
# Python3 list to contain all model predictions-
preditcions = []
for x in train_dataset:
y_pred = model_e2d2.predict(x)
preditcions.append(y_pred)
# Sanity check-
len(preditcions)
# (2048)
# Each batch of prediction has the size: (256, 5, 11).
# Sanity check-
preditcions[0].shape, preditcions[-1].shape
# ((256, 5, 11), (222, 5, 11))其目的是将精确的Python3列表转换为所需形状的numpy数组:(524254,5,11)
发布于 2022-05-25 11:05:35
一般来说,您可以这样做:
predictions = np.concatenate(predictions, axis=0)这使您可以控制要连接的轴。
看看这里给出的例子,https://numpy.org/doc/stable/reference/generated/numpy.concatenate.html#numpy-concatenate
>>a = np.array([[1, 2], [3, 4]])
>>b = np.array([[5, 6]])
>>np.concatenate([a, b], axis=0) #slight difference from example.
# instead of tuple I have passed a list as is OP's requirement
array([[1, 2],
[3, 4],
[5, 6]])这一切都要感谢@hpaulj纠正我的错误。
https://stackoverflow.com/questions/72376093
复制相似问题