如何为 Keras 顺序模型的层添加名称
How to add names to layers of Keras sequential model
我在 Tensorflow 2.0 中使用 Keras 创建一个顺序模型:
def create_model():
model = keras.Sequential([
keras.layers.Flatten(input_shape=(28,28), name="bla"),
keras.layers.Dense(128, kernel_regularizer=keras.regularizers.l2(REGULARIZE), activation="relu",),
keras.layers.Dropout(DROPOUT_RATE),
keras.layers.Dense(128, kernel_regularizer=keras.regularizers.l2(REGULARIZE), activation="relu"),
keras.layers.Dropout(DROPOUT_RATE),
keras.layers.Dense(10, activation="softmax")
])
model.compile(optimizer="adam",
loss="sparse_categorical_crossentropy",
metrics=["accuracy"])
return model
model = create_model()
# Checkpoint callback
cp_callback = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_path,
save_weights_only=True)
# Train model
model.fit(train_images,
train_labels,
epochs=EPOCHS,
callbacks=[cp_callback])
如果我在单独的文件中加载模型后提取名称,我会得到以下信息:
# Create model instance
model = create_model()
# Load weights of pre-trained model
model.load_weights(checkpoint_path)
output_names = [layer.name for layer in model.layers]
print(output_names) = ['flatten', 'dense', 'dropout', 'dense_1', 'dropout_1', 'dense_2']
在这种情况下,我希望 bla
而不是 flatten
。
如何向图层添加自定义名称?
你做得对,直接来自我的 jupyter :
from tensorflow import keras
model = keras.Sequential([
keras.layers.Flatten(input_shape=(28,28), name="bla"),
keras.layers.Dense(128, activation="relu",),
keras.layers.Dropout(0.5),
keras.layers.Dense(128, activation="relu"),
keras.layers.Dropout(0.5),
keras.layers.Dense(10, activation="softmax")
])
model.compile(optimizer="adam",
loss="sparse_categorical_crossentropy",
metrics=["accuracy"])
model
tensorflow.python.keras.engine.sequential.Sequential at 0x2b6ecf083c18>
output_names = [layer.name for layer in model.layers]
output_names
['bla', 'dense', 'dropout', 'dense_1', 'dropout_1', 'dense_2']
编辑添加 load/save 部分:
model.save('my_model.h5')
second_model = keras.models.load_model('my_model.h5')
output_names = [layer.name for layer in second_model.layers]
output_names
['bla', 'dense', 'dropout', 'dense_1', 'dropout_1', 'dense_2']
你能把你的全部代码加进去吗,问题可能出在别处。
你也可以添加你的tensorflow版本吗?
我在 Tensorflow 2.0 中使用 Keras 创建一个顺序模型:
def create_model():
model = keras.Sequential([
keras.layers.Flatten(input_shape=(28,28), name="bla"),
keras.layers.Dense(128, kernel_regularizer=keras.regularizers.l2(REGULARIZE), activation="relu",),
keras.layers.Dropout(DROPOUT_RATE),
keras.layers.Dense(128, kernel_regularizer=keras.regularizers.l2(REGULARIZE), activation="relu"),
keras.layers.Dropout(DROPOUT_RATE),
keras.layers.Dense(10, activation="softmax")
])
model.compile(optimizer="adam",
loss="sparse_categorical_crossentropy",
metrics=["accuracy"])
return model
model = create_model()
# Checkpoint callback
cp_callback = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_path,
save_weights_only=True)
# Train model
model.fit(train_images,
train_labels,
epochs=EPOCHS,
callbacks=[cp_callback])
如果我在单独的文件中加载模型后提取名称,我会得到以下信息:
# Create model instance
model = create_model()
# Load weights of pre-trained model
model.load_weights(checkpoint_path)
output_names = [layer.name for layer in model.layers]
print(output_names) = ['flatten', 'dense', 'dropout', 'dense_1', 'dropout_1', 'dense_2']
在这种情况下,我希望 bla
而不是 flatten
。
如何向图层添加自定义名称?
你做得对,直接来自我的 jupyter :
from tensorflow import keras
model = keras.Sequential([
keras.layers.Flatten(input_shape=(28,28), name="bla"),
keras.layers.Dense(128, activation="relu",),
keras.layers.Dropout(0.5),
keras.layers.Dense(128, activation="relu"),
keras.layers.Dropout(0.5),
keras.layers.Dense(10, activation="softmax")
])
model.compile(optimizer="adam",
loss="sparse_categorical_crossentropy",
metrics=["accuracy"])
model
tensorflow.python.keras.engine.sequential.Sequential at 0x2b6ecf083c18>
output_names = [layer.name for layer in model.layers]
output_names
['bla', 'dense', 'dropout', 'dense_1', 'dropout_1', 'dense_2']
编辑添加 load/save 部分:
model.save('my_model.h5')
second_model = keras.models.load_model('my_model.h5')
output_names = [layer.name for layer in second_model.layers]
output_names
['bla', 'dense', 'dropout', 'dense_1', 'dropout_1', 'dense_2']
你能把你的全部代码加进去吗,问题可能出在别处。
你也可以添加你的tensorflow版本吗?