Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
154 views
in Technique[技术] by (71.8m points)

python - Use callbacks to pick best network

I'm working on a classification task, trying to reconstruct a network from paper. In that paper, they are talking about doing a train test split 300 times and training the network each time after they are taking the mean of all predictions from each network for specific input data.

So here's the question: After the first evaluation it's skipping all the others because the evaluation of the last epoch of the first training is always better than the first epoch evaluation of the next network training, what can I do to compare the last epochs of networks? Here's the code:

checkpoint_filepath = './checkpoint'

model_checkpoint_callback = tf.keras.callbacks.ModelCheckpoint(
    filepath=checkpoint_filepath,
    save_weights_only=True,
    monitor='precision',
    mode='max',
    save_best_only=True)

lis = []

for i in range(300):

# Train test split

X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.3, stratify = Y)

# Create model

model_two_neuron = tf.keras.Sequential([
  tf.keras.layers.Dense(40, input_shape=(15,)),
  tf.keras.layers.Dense(2, activation=tf.nn.sigmoid)
])

model_two_neuron.compile(optimizer=tf.keras.optimizers.Adam(),
              loss=tf.keras.losses.MeanSquaredError(),
              metrics=[tf.keras.metrics.Precision()])

# Train

model_two_neuron.fit(X_train, y_train, epochs=20, callbacks=[model_checkpoint_callback])

# evaluate

value = model_two_neuron.evaluate(X_test, y_test)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...