如何从TensorFlow内置history中提取字典?用于CNN参数调优数据收集
Hey there! Great job getting your hierarchical MNIST classifier up and running locally— that’s a solid first step for diving into CNN parameter tuning. Let’s break down exactly how to extract data from TensorFlow’s built-in History object so you can track and analyze your model’s performance as you tweak things like learning rate, layer properties, and more.
1. First, Understand What the History Object Holds
When you train a Keras model (TensorFlow’s go-to high-level API) with model.fit(), it returns a History object. This object’s history attribute is a dictionary where keys are metric names (like accuracy, loss, val_loss) and values are lists of metric values recorded at each training epoch. It’s basically a log of how your model performed over time during that run.
2. Capture the History Object During Training
First, make sure you save the output of model.fit() to a variable every time you train. Here’s a quick example:
# Assuming you've already defined your model and loaded MNIST data history = model.fit( train_images, train_labels, epochs=15, # Your chosen epoch count validation_data=(test_images, test_labels), verbose=1 # Keep this to see training logs, or set to 0 to mute )
3. Extract the Dictionary & Collect Data Across Runs
To track performance across different parameter combinations, you’ll want to store each run’s parameters and metrics in a structured way. A list of dictionaries works perfectly for this. Here’s how to set it up:
# Initialize a list to store all your training run data all_run_results = [] # Example: Test different learning rates (replace with your parameters) learning_rates = [0.0001, 0.001, 0.01] num_conv_filters = [16, 32, 64] # Loop through your parameter combinations for lr in learning_rates: for filters in num_conv_filters: # Rebuild your model with the current parameters model = build_your_custom_model(learning_rate=lr, conv_filters=filters) # Replace with your model function # Train and capture the history history = model.fit( train_images, train_labels, epochs=15, validation_data=(test_images, test_labels), verbose=0 # Mute logs to keep output clean during bulk runs ) # Package parameters and metrics into a single dictionary run_data = { "learning_rate": lr, "conv_layer_filters": filters, "training_loss": history.history["loss"], "training_accuracy": history.history["accuracy"], "validation_loss": history.history["val_loss"], "validation_accuracy": history.history["val_accuracy"] } # Add this run's data to your master list all_run_results.append(run_data)
4. Analyze Your Collected Data
Once you have all your runs stored, you can easily compare performance across parameters. For example, use matplotlib to plot how validation accuracy changes with different learning rates and filter counts:
import matplotlib.pyplot as plt plt.figure(figsize=(12, 8)) for run in all_run_results: label = f"LR={run['learning_rate']}, Filters={run['conv_layer_filters']}" plt.plot(run["validation_accuracy"], label=label) plt.xlabel("Epoch") plt.ylabel("Validation Accuracy") plt.title("Validation Accuracy Trends Across Parameter Combinations") plt.legend() plt.show()
5. Pro Tips for Effective Data Collection
- Track every variable: Make sure to log every parameter you change (dropout rate, optimizer type, number of layers, etc.) alongside metrics— this lets you isolate exactly which changes impact performance.
- Save your data: After collecting runs, save
all_run_resultsto a JSON or CSV file (usingjson.dump()orpandas.DataFrame.to_csv()) so you don’t lose your work between sessions. - Control variables: When testing one parameter (e.g., learning rate), keep all other model attributes identical. This way you can be sure any performance changes are due to that single parameter.
That’s the core workflow! By leveraging the history dictionary from each training run, you’ll have all the data you need to spot patterns in how your CNN’s output shifts with different design choices.
内容的提问来源于stack exchange,提问作者Benjamin Danek

