keras,LSTM - 预测不同长度的输入?
keras, LSTM - predict on inputs of different length?
我安装了一个处理不同长度输入的 LSTM:
model = Sequential()
model.add(LSTM(units=10, return_sequences=False, input_shape=(None, 5)))
model.add(Dense(units=1, activation='sigmoid'))
拟合模型后,我想在不同大小的输入上对其进行测试。
x_test.shape # = 100000
x_test[0].shape # = (1, 5)
x_test[1].shape # = (3, 5)
x_test[2].shape # = (8, 5)
在单个实例上测试 j
不是问题 (model.predict(x_test[j]
),但在所有实例上循环 真的很慢。
有没有办法加快计算速度? model.predict(x_test)
不起作用。
谢谢!
加速模型推理的最常见方法是 运行 在 GPU 上推理,而不是 CPU(我假设你是还没有这样做)。您可以按照官方指南 here. Unless you are explicitly asking keras to run inference on CPU, your code should work as is, without any changes. To confirm if you are using GPU, you can use this 文章设置 GPU 支持。
希望回答对您有所帮助!
目前我发现的最佳解决方案是将具有相同长度的数据 windows 分组在一起。对于我的问题,这足以显着加快计算速度。
希望这个技巧能帮助到其他人。
import numpy as np
def predict_custom(model, x):
"""x should be a list of np.arrays with different number of rows, but same number of columns"""
# dictionary with key = length of the window, value = indices of samples with such length
dic = {}
for i, x in enumerate(x):
if dic.get(x.shape[0]):
dic[x.shape[0]].append(i)
else:
dic[x.shape[0]] = [i]
y_pred = np.full((len(x),1), np.nan)
# loop over dictionary and predict together samples of the same length
for key, indexes in dic.items():
# select samples of the same length (conversion to np.array is used for subsetting "x" using "indexes")
x = np.asarray(x, dtype=object)[indexes].tolist()
# gather such samples in a 3D np.array
x_3d = np.stack(x, axis=0)
# use dictionary values to insert results in the correspondent row of y_pred
y_pred[indexes] = model.predict(x_3d)
return y_pred
我安装了一个处理不同长度输入的 LSTM:
model = Sequential()
model.add(LSTM(units=10, return_sequences=False, input_shape=(None, 5)))
model.add(Dense(units=1, activation='sigmoid'))
拟合模型后,我想在不同大小的输入上对其进行测试。
x_test.shape # = 100000
x_test[0].shape # = (1, 5)
x_test[1].shape # = (3, 5)
x_test[2].shape # = (8, 5)
在单个实例上测试 j
不是问题 (model.predict(x_test[j]
),但在所有实例上循环 真的很慢。
有没有办法加快计算速度? model.predict(x_test)
不起作用。
谢谢!
加速模型推理的最常见方法是 运行 在 GPU 上推理,而不是 CPU(我假设你是还没有这样做)。您可以按照官方指南 here. Unless you are explicitly asking keras to run inference on CPU, your code should work as is, without any changes. To confirm if you are using GPU, you can use this 文章设置 GPU 支持。
希望回答对您有所帮助!
目前我发现的最佳解决方案是将具有相同长度的数据 windows 分组在一起。对于我的问题,这足以显着加快计算速度。
希望这个技巧能帮助到其他人。
import numpy as np
def predict_custom(model, x):
"""x should be a list of np.arrays with different number of rows, but same number of columns"""
# dictionary with key = length of the window, value = indices of samples with such length
dic = {}
for i, x in enumerate(x):
if dic.get(x.shape[0]):
dic[x.shape[0]].append(i)
else:
dic[x.shape[0]] = [i]
y_pred = np.full((len(x),1), np.nan)
# loop over dictionary and predict together samples of the same length
for key, indexes in dic.items():
# select samples of the same length (conversion to np.array is used for subsetting "x" using "indexes")
x = np.asarray(x, dtype=object)[indexes].tolist()
# gather such samples in a 3D np.array
x_3d = np.stack(x, axis=0)
# use dictionary values to insert results in the correspondent row of y_pred
y_pred[indexes] = model.predict(x_3d)
return y_pred