K.function的用法

K.function除了能够实现指定输入和输出之外,还能够决定网络是处于训练模式还是测试模型

1或True:训练模式,dropout开启
0或False:测试模式,dropout失活

代码如下,需要先用K.function“实例化”一个f函数,然后f((X_test, True))代表将X_test传入作为model.layers[0]的输入,True代表训练模式——dropout打开,最终是model.output作为输出

    f = K.function([self.model.layers[0].input, 
                    K.symbolic_learning_phase()], 
                    [self.model.output])

    mc_preds = np.array([f((X_test, True))[0] for _ in range(mc_samples)])
发布了103 篇原创文章 · 获赞 17 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_44684139/article/details/104626345