编辑:
显然,keras vs tensorflow.keras的一些问题导致了我的问题。我遵循此操作来更改导入: The added layer must be an instance of class Layer. Found: <tensorflow.python.keras.engine.input_layer.InputLayer>
我正在尝试加载模型,并与另一个具有不同输出尺寸的密度层交换最后一个密集层。 这是到目前为止我得到的:
所保存的模型:
def create_model(n_timesteps=828 , n_features=1, n_outputs=7):
dtype='float32'
model = Sequential([
Convolution1D(input_shape=(n_timesteps, n_features), kernel_size=3, filters=128, activation='relu', kernel_regularizer='l2', dtype=dtype, name="conv1"),
MaxPool1D(pool_size=4, strides=4), # reduce size of the input by 4
Dropout(0.3, dtype=dtype),
Flatten(),
Dense(32, activation='relu', kernel_regularizer=tf.keras.regularizers.l2(), dtype=dtype, name="dense1"),
Dropout(0.2, dtype=dtype),
Dense(n_outputs, activation='softmax', dtype=dtype, name="dense2")
])
return model
并尝试重新训练:
def retrain(model_name="cnn_general"):
model = tf.keras.models.load_model('saved_models\\' + model_name)
model.trainable = False
model.compile(optimizer='adam', loss='categorical_crossentropy',
metrics=['accuracy'])
print(model.summary())
output = Dense(1, activation='sigmoid')(model.layers[-1].output)
retrained_model = Model(inputs=model.inputs, outputs=output)
print(retrained_model.summary())
我得到错误输出:
Traceback (most recent call last):
File "C:/.../CNN_heatbeats.py", line 233, in <module>
retrain((X_train, y_train, X_test, y_test, X_val, y_val))
File "C:/.../CNN_heatbeats.py", line 161, in retrain
output = Dense(10, activation='sigmoid')(model.layers[-1].output)
File "C:\Miniconda\envs\tensorflow-gpu-cuda10\lib\site-packages\keras\backend\tensorflow_backend.py", line 75, in symbolic_fn_wrapper
return func(*args, **kwargs)
File "C:\Miniconda\envs\tensorflow-gpu-cuda10\lib\site-packages\keras\engine\base_layer.py", line 475, in __call__
previous_mask = _collect_previous_mask(inputs)
File "C:\Miniconda\envs\tensorflow-gpu-cuda10\lib\site-packages\keras\engine\base_layer.py", line 1441, in _collect_previous_mask
mask = node.output_masks[tensor_index]
AttributeError: 'Node' object has no attribute 'output_masks'
我仅找到有关VGG16之类的信息,但无法将其应用于我自己的网络。我在retrain()函数中所做的事情是否合适?我该如何运作?
答案 0 :(得分:1)
尝试过这种方式吗?
def retrain(model_name="cnn_general"):
model = tf.keras.models.load_model('saved_models\\' + model_name)
model.trainable = False
model.compile(optimizer='adam', loss='categorical_crossentropy',
metrics=['accuracy'])
model.summary()
retrained_model = Sequential()
for layer in model.layers[:-1]:
layer.trainable = False
retrained_model.add(layer)
retrained_model.add(Dense(1, activation='sigmoid'))
retrained_model.summary()