我想在for循环中创建多个对象。我的代码是这样的:
regressor1 = Sequential()
# Adding the first LSTM layer and some Dropout regularisation
regressor1.add(LSTM(units = 50, return_sequences = True, input_shape = (X_train.shape[1], 6)))
regressor1.add(Dropout(0.2))
regressor2 = Sequential()
# Adding the first LSTM layer and some Dropout regularisation
regressor2.add(LSTM(units = 50, return_sequences = True, input_shape = (X_train.shape[1], 6)))
regressor2.add(Dropout(0.2))
.
.
.
regressor20 = Sequential()
# Adding the first LSTM layer and some Dropout regularisation
regressor20.add(LSTM(units = 50, return_sequences = True, input_shape = (X_train.shape[1], 6)))
regressor20.add(Dropout(0.2))
我如何在一个for循环中这样做?
谢谢你的帮助。
最佳答案
模型没有什么特别之处,它们是对象,因此可以在循环中创建模型对象(回归器)列表:
regressors = list()
for _ in range(20):
model = Sequential()
model.add(LSTM(units=50, ...))
model.add(Dropout(0.2))
regressors.append(model)
# Now access like you would any array
# regressors[0] etc will give you models.
关于python - 在for循环中创建多个对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51477729/