如何在Keras模型摘要中固定显示Model: "sequential"名称
解决Keras Sequential模型名称每次运行递增的问题
这是因为Keras会自动为新创建的模型实例生成递增的默认名称,要固定模型名称为"sequential",可以用以下两种方法:
方法1:显式指定模型名称
在创建Sequential模型时,通过name参数直接指定固定名称,无论运行多少次,模型名称都不会变化:
np.random.seed(0) tensorflow.random.set_seed(0) # 显式指定name参数为"sequential" model1 = Sequential(name="sequential") model1.add(Dense(100, input_dim= Train_X2_Tfidf.shape[1], activation='sigmoid')) model1.add(Dense(1, activation='sigmoid')) opt = Adam(learning_rate=0.01) model1.compile(loss='binary_crossentropy', optimizer=opt, metrics=['accuracy']) model1.summary()
方法2:重置Keras会话(适合交互式环境)
如果是在Jupyter Notebook这类交互式环境中运行代码,每次运行前清除Keras会话,重置内部的名称计数器:
import tensorflow as tf # 清除会话,重置模型名称计数器 tf.keras.backend.clear_session() np.random.seed(0) tensorflow.random.set_seed(0) model1 = Sequential() model1.add(Dense(100, input_dim= Train_X2_Tfidf.shape[1], activation='sigmoid')) model1.add(Dense(1, activation='sigmoid')) opt = Adam(learning_rate=0.01) model1.compile(loss='binary_crossentropy', optimizer=opt, metrics=['accuracy']) model1.summary()
内容的提问来源于stack exchange,提问作者andryan86
相关产品推荐
相关产品推荐

