AttributeError:'NoneType'对象在Keras的自动编码器中没有属性'_inbound_nodes'

时间:2019-04-29 16:31:30

标签: keras attributeerror nonetype autoencoder

我无法解决以下错误,如果听起来很天真,请接受我的道歉,我对Keras还是陌生的。

编码器的输出实际上是一个复数值,因此每个输出都是实部和虚部, input_h1 也是一个复数,实部和虚部都表示为矢量。我想把它们都乘以。

# Input bits 
input_bits1 = Input(shape=(2,))
input_bits2 = Input(shape=(2,))

# Input Channels
input_h1 = Input(shape=(2,))
input_h2 = Input(shape=(2,))

# Concatenate both inputs
input_bits = keras.layers.concatenate([input_bits1, input_bits2], axis=1)  
print(input_bits)

# Create Encoder
m1 = Dense(64, activation='relu')(input_bits)  
m2 = Dense(128, activation='relu')(m1)   
encoded1 = Dense(2, activation='linear')(m2)

# Normalize the encoded value
encoded = Lambda(lambda x: K.l2_normalize(x, axis=1))(encoded1)

# The output of the encoder is actually a complex value, so each output is real and imaginary part,input_h1 is also a complex value with real and imaginary parts represented as a vector. I want to multiply both of them. 

# mt1 is the real part of complex number multiplication
mt1 = encoded[:,0:1]*input_h1[:,0:1] - encoded[:,1:2]*input_h1[:,1:2]   
print(mt1)

# nt1 is the imaginary part of the complex number multiplication
nt1 = encoded[:,0:1]*input_h1[:,1:2] + encoded[:,1:2]*input_h1[:,0:1]   
print(nt1)

# Concatenate real and imaginary parts to feed into the decoder
mnt2 = keras.layers.concatenate([mt1, nt1], axis=1)   
print(mnt2)

# Decoder 1
x5 = Dense(1024, activation='relu')(mnt2)
x6 = Dense(512, activation='relu')(x5)   
x7 = Dense(64, activation='relu')(x6)

decoded_UP1 = Dense(2, activation='tanh')(x7)

# Decoder 2
a3 = Dense(1024, activation='relu')(mnt2)  
a4 = Dense(512, activation='relu')(a3)  
a5 = Dense(64, activation='relu')(a4)

decoded_UP2 = Dense(2, activation='tanh')(a5)

decoded = keras.layers.concatenate([decoded_UP1, decoded_UP2], axis=1) 

autoencoder = Model([input_bits1, input_bits2, input_h1, input_h2], decoded)
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')  
autoencoder.summary()

我得到以下输出/错误:

AttributeError                            Traceback (most recent call last)
<ipython-input-9-c3710aa7e060> in <module>()
     35 decoded = keras.layers.concatenate([decoded_UP1, decoded_UP2], axis=1)
     36 
---> 37 autoencoder = Model([input_bits1, input_bits2, input_h1, input_h2], decoded)
     38 autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')
     39 autoencoder.summary()

AttributeError: 'NoneType' object has no attribute '_inbound_nodes'

1 个答案:

答案 0 :(得分:0)

要提高代码的清晰度,您应该创建多个模型,例如一个用于编码器,一个用于解码器。这样,您每个人都可以拥有一个 model.summary()

位编码器示例:

from keras.layers import Input, Dense, concatenate
from keras.models import Model

# Input
input_bits1 = Input(shape=(2,))
input_bits2 = Input(shape=(2,))
input_bits = keras.layers.concatenate([input_bits1, input_bits2], axis=1)

# Hidden Layers
encoder_bits_h1 = Dense(64, activation='relu')(input_bits)  
encoder_bits_h2 = Dense(128, activation='relu')(encoder_bits_h1)   
encoder_bits_h3 = Dense(2, activation='linear')(encoder_bits_h2)

# Create the model
bits_encoder = Model(inputs=[input_bits1, input_bits2], outputs=[encoder_bits_h3])
bits_encoder.summary()

哪个返回位编码器配置:

__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_5 (InputLayer)            (None, 2)            0                                            
__________________________________________________________________________________________________
input_6 (InputLayer)            (None, 2)            0                                            
__________________________________________________________________________________________________
concatenate_2 (Concatenate)     (None, 4)            0           input_5[0][0]                    
                                                                 input_6[0][0]                    
__________________________________________________________________________________________________
dense_4 (Dense)                 (None, 64)           320         concatenate_2[0][0]              
__________________________________________________________________________________________________
dense_5 (Dense)                 (None, 128)          8320        dense_4[0][0]                    
__________________________________________________________________________________________________
dense_6 (Dense)                 (None, 2)            258         dense_5[0][0]                    
==================================================================================================
Total params: 8,898
Trainable params: 8,898
Non-trainable params: 0
__________________________________________________________________________________________________