我正在从Keras加载一个预先创建的模型,并且形状有些问题。当我按以下方式加载模型
neuruleModelPath = "model/diabetes/neurules_model.h5"
neuruleLoadedModel = load_model(neuruleModelPath)
并检查print(neuruleLoadedModel.summary())
输出的内容是:
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input (InputLayer) (None, 24) 0
__________________________________________________________________________________________________
neurules (Dense) (None, 20) 500 input[0][0]
__________________________________________________________________________________________________
signumAfterNeurules (Lambda) (None, 20) 0 neurules[0][0]
__________________________________________________________________________________________________
layer_split0 (Lambda) (None, 11) 0 signumAfterNeurules[0][0]
__________________________________________________________________________________________________
layer_split1 (Lambda) (None, 9) 0 signumAfterNeurules[0][0]
__________________________________________________________________________________________________
or0 (Lambda) (None, 1) 0 layer_split0[0][0]
__________________________________________________________________________________________________
or1 (Lambda) (None, 1) 0 layer_split1[0][0]
__________________________________________________________________________________________________
output (Lambda) (None, 2) 0 or0[0][0]
or1[0][0]
==================================================================================================
Total params: 500
Trainable params: 0
Non-trainable params: 500
__________________________________________________________________________________________________
所以一切似乎都很好,我的输出形状为2,所以(None,2)
似乎还可以,因为正在考虑“批处理”。问题是,当我尝试将模型加载为一层时(它需要进入另一个模型的中间),如下所示:
#this is the input tensor
inputs = Input(shape=(inputSize,), name='main_input')
#normal network
x_up = Dense(hiddenNeuronsQt, activation='softsign', name="classic_network")(inputs)
#neurule network
neuruleNetwork = neuruleLoadedModel(inputs)
#concatenating/merging them
merged = concatenate([x_up, neuruleNetwork], name='merging_layer')
#this is the only layer after concatenating/merge
x = Dense(hiddenNeuronsQt, activation='softsign', name='layer_after_merge')(merged)
#this is the output layer
y = Dense(outputSize, activation='softsign', name='output_layer')(x)
concatenate()
引发错误:
InvalidArgumentError: Shape must be rank 2 but is rank 1 for 'merging_layer_10/concat' (op: 'ConcatV2') with input shapes: [?,16], [2], [].
如果我检查neuruleNetwork.shape
会给我(2,)
,但是当我直接用neuruleLoadedModel.summary()
检查原始加载的模型时,最后一层会给我{{1} }。
我应该如何进行合并才能在那里合并?
编辑: 可能我的问题实际上是在我的原始模型中,在加载之前,我创建了这样的图层:
(None, 2)
如果我将第def signumTransform(x):
"""
SIGNUM function
if positive 1
if negative -1
"""
import keras.backend
return keras.backend.sign(x)
def logical_or_layer(x):
"""Processing an OR operation"""
import keras.backend
#normalized to 0,1
aux_array = keras.backend.sign(x)
aux_array = keras.backend.relu(aux_array)
# OR operation
aux_array = keras.backend.any(aux_array)
# casting back the True/False to 1,0
aux_array = keras.backend.cast(aux_array, dtype='float32')
return aux_array
#this is the input tensor
inputs = Input(shape=(inputSize,), name='input')
#this is the Neurule layer
x = Dense(neurulesQt, activation='softsign', name='neurules')(inputs)
#after each neuron layer, the outputs need to be put into SIGNUM (-1 or 1)
x = Lambda(signumTransform, output_shape=lambda x:x, name='signumAfterNeurules')(x)
#separating into 2 (2 possible outputs)
layer_split0 = Lambda( lambda x: x[:, :11], output_shape=[11], name='layer_split0')(x)
layer_split1 = Lambda( lambda x: x[:, 11:20], output_shape=[9], name='layer_split1')(x)
#this is the OR layer
y_0 = Lambda(logical_or_layer, output_shape=[1], name='or0')(layer_split0)
y_1 = Lambda(logical_or_layer, output_shape=[1], name='or1')(layer_split1)
y = Lambda(lambda x: K.stack([x[0], x[1]]),output_shape=[2], name="output")([y_0, y_1])
层用作输出,没问题,但是当我尝试使用layer_split0
时,它不再起作用了,我的意思是,如果加载它,模型本身就可以工作,不再。在函数y_0
中,我可能做错了,但我无法弄清楚。