大饺子96

导航

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

 

 

posted on 2019-11-14 16:46  大昂子96  阅读(7530)  评论(0编辑  收藏  举报