AttributeError: 'Tensor' object has no attribute '_keras_history'


原因是因為keras在建立網格是, 將數據流定義為Layer和Tensor會產生不兼容的現象, 因此會有這種錯誤.

只需要將我們tensor要進行的運算用keras提供的Lambda封裝一下就可以將Tensor類型轉化為為Layer類型.

 

出現錯誤提示的代碼:

def TotalModel(input_shape, noise_model, avo_model, verbose = 1):
    
    noise_input = Input(input_shape)
    noise_output = noise_model(noise_input)
    avo_output = avo_model(noise_input)
    total_output =avo_output + noise_output

    total = Model(inputs = noise_input, outputs = total_output)
    
    if verbose == 1:
        print()
        total.summary()
    
    return total

 

修改為:

def TotalModel(input_shape, noise_model, avo_model, verbose = 1):
    
    noise_input = Input(input_shape)
    noise_output = noise_model(noise_input)
    avo_output = avo_model(noise_input)
    total_output = Lambda(lambda x: x[0] + x[1])([avo_output, noise_output])

    total = Model(inputs = noise_input, outputs = total_output)
    
    if verbose == 1:
        print()
        total.summary()
    
    return total

 

參考鏈接:

https://blog.csdn.net/scythe666/article/details/79962836

https://codeday.me/bug/20190131/581332.html

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM