keras中Lambda层

1、Lambda 层

如果你只是想对流经该层的数据做个变换,而这个变换本身没有什么需要学习的参数,那么直接用Lambda Layer是最合适的了。

  1. <span style="font-size:18px;">def get_submean_model():  
  2.     model = Sequential()  
  3.     model.add(Dense(5,input_dim=7))  
  4.     def sub_mean(x):  
  5.         x -= K.mean(x,axis=1,keepdims=True)  
  6.         return x  
  7.     model.add( Lambda(sub_mean,output_shape=lambda input_shape:input_shape))  
  8.     model.compile(optimizer='rmsprop',loss='mse')  
  9.     return model  
  10. model = get_submean_model()  
  11. res=model.predict(np.random.random((3,7)))</span>  
得到地res的平均值是[  5.96046448e-08  -5.96046448e-08   0.00000000e+00],可见确实实现了减去均值的作用。

猜你喜欢

转载自blog.csdn.net/qq_25964837/article/details/79695631
今日推荐