我正在尝试读取Keras中的中间层的输出:
input_dim = 30
encoding_dim = 14
input_layer = Input(shape=(input_dim, ))
encoder = Dense(encoding_dim, activation="tanh",
activity_regularizer=regularizers.l1(10e-5))(input_layer)
encoder = Dense(int(encoding_dim / 2), activation="relu")(encoder)
decoder = Dense(int(encoding_dim / 2), activation='tanh',name='decoder_input')(encoder)
decoder = Dense(input_dim, activation='relu',name='decoder_output')(decoder)
autoencoder = Model(inputs=input_layer, outputs=decoder)
#evaluate decoder...
layer_output_dec = Model(inputs=autoencoder.layers[3].input,
outputs=autoencoder.layers[4].output)
但是我得到了错误:
/usr/local/lib/python2.7/site-packages/keras/engine/network.pyc in __init__(self, *args, **kwargs)
89 'inputs' in kwargs and 'outputs' in kwargs):
90 # Graph network
---> 91 self._init_graph_network(*args, **kwargs)
92 else:
93 # Subclassed network
/usr/local/lib/python2.7/site-packages/keras/engine/network.pyc in _init_graph_network(self, inputs, outputs, name)
181 'instantiated via '
182 '`tensor = tf.layers.Input(shape)`.\n'
--> 183 'The tensor that caused the issue was: ' +
184 str(x.name))
185 for x in self.outputs:
AttributeError: 'Model' object has no attribute 'name'
我认为该错误是由于缺少图层名称造成的,但没有帮助。任何帮助表示赞赏。
答案 0 :(得分:2)
这将采用已经连接到之前输入的输入。无法使该模型正常运行。
#evaluate decoder...
layer_output_dec = Model(inputs=autoencoder.layers[3].input,
outputs=autoencoder.layers[4].output)
您需要合适的编码器和解码器型号:
#encoder
input_tensor = Input(shape=(input_dim, ))
encoderOut = Dense(encoding_dim, activation="tanh",
activity_regularizer=regularizers.l1(10e-5))(input_tensor)
encoderOut = Dense(int(encoding_dim / 2), activation="relu")(encoderOut)
encoder = Model(input_tensor, encoderOut)
#decoder
decoder_input = Input(shape=(int(encoding_dim / 2),))
decoderOut = Dense(int(encoding_dim / 2), activation='tanh',name='decoder_input')(decoder_input)
decoderOut = Dense(input_dim, activation='relu',name='decoder_output')(decoderOut)
decoder = Model(decoder_input, decoderOut)
#autoencoder
autoInput = Input(shape=(input_dim, ))
encoderOut = encoder(autoInput)
decoderOut = decoder(encoderOut)
autoencoder = Model(inputs=autoInput, outputs=decoderOut)
根据需要使用三种模型。
对于您的问题,请使用decoder
作为模型。