说我有一些课程:
objDict = {"a" : Object1(), "b" : Object2()}
我可以定义一个像这样的字典:
firstObj = objDict["a"]("foo")
这将实例化输入为None的对象。 有没有办法我可以使用字典作为模板,所以我可以传入一个参数?像这样:
# Multiple Outputs
from keras.utils import plot_model
from keras.models import Model
from keras.layers import Input
from keras.layers import Dense
from keras.layers.recurrent import LSTM
from keras.layers.wrappers import TimeDistributed
# input layer
visible = Input(shape=(4,2))
# feature extraction
extract = LSTM(10, return_sequences=True)(visible)
# classification output
class11 = LSTM(10)(extract)
class12 = Dense(8, activation='relu')(class11)
class13 = Dense(8, activation='relu')(class12)
output1 = Dense(9, activation='softmax')(class13)
# sequence output
output2 = TimeDistributed(Dense(1, activation='tanh'))(extract)
# output
model = Model(inputs=visible, outputs=[output1, output2])
# summarize layers
print(model.summary())
如果可以的话,这会是最好的方法吗?
答案 0 :(得分:1)
你在制作字典时实例化了这个类,这不是你想要的。只需将类作为值:
class_dict = {"a": Object1, "b": Object2}
first_obj = class_dict["a"]("foo")