import numpy as np
from keras.models import Sequential
from keras.layers import Dense, Activation,Reshape
from keras.layers import merge
from keras.utils.visualize_util import plot
from keras.layers import Input, Lambda
from keras.models import Model
def slice(x,index):
return x[:,:,index]
a = Input(shape=(4,2))
x1 = Lambda(slice,output_shape=(4,1),arguments={‘index‘:0})(a)
x2 = Lambda(slice,output_shape=(4,1),arguments={‘index‘:1})(a)
x1 = Reshape((4,1,1))(x1)
x2 = Reshape((4,1,1))(x2)
output = merge([x1,x2],mode=‘concat‘)
model = Model(a, output)
x_test = np.array([[[1,2],[2,3],[3,4],[4,5]]])
print model.predict(x_test)
plot(model, to_file=‘lambda.png‘,show_shapes=True)
keras lambda 层
最新推荐文章于 2021-12-13 21:05:21 发布
本文深入探讨了Keras中Lambda层的应用,展示了如何使用Lambda层进行数据操作,如切片和重塑,以及如何将多个Lambda层的输出进行合并。通过一个具体示例,解释了Lambda层如何帮助构建更灵活的神经网络模型。
3万+

被折叠的 条评论
为什么被折叠?



