求助:使用MongoDB实现Hyperopt并行评估功能失败
Hey there! Let's work through getting Hyperopt's parallel evaluation up and running with your MongoDB setup—since you've already got the basics in place, let's dig into the common issues and fixes.
First, Double-Check Your MongoDB Connection
You mentioned your mongod process is showing "waiting for connections on port 1234"—that's a good start, but let's confirm the connection is actually usable:
- Open a new command prompt window and run:
If you can access the MongoDB shell (you'll see a"C:\Mongodb\bin\mongo.exe" --port 1234>prompt), your database is listening correctly. If not, double-check that yourtest_trialfolder exists and has write permissions, and that no other program is using port 1234 (usenetstat -ano | findstr :1234to verify).
Configure Hyperopt to Use Your MongoDB Instance
The most likely issue is that your Hyperopt code isn't properly pointing to your running MongoDB instance. Here's how to set up the MongoTrials object correctly:
from hyperopt import fmin, tpe, hp, STATUS_OK from hyperopt.mongoexp import MongoTrials import time # Define a simple objective function to test with def objective(params): time.sleep(2) # Simulate a time-consuming evaluation return {'loss': params['x'] ** 2, 'status': STATUS_OK} # Connect to your MongoDB instance # Format: mongo://<host>:<port>/<database_name>/jobs trials = MongoTrials( 'mongo://localhost:1234/hyperopt_parallel/jobs', exp_key='my_parallel_exp' ) # Define your search space search_space = {'x': hp.uniform('x', -10, 10)} # Run the optimization best_result = fmin( fn=objective, space=search_space, algo=tpe.suggest, max_evals=10, trials=trials ) print("Best parameters found:", best_result)
Key notes here:
- The database name (
hyperopt_parallelin this example) will be created automatically—you don't need to set it up manually. - The
exp_keymust be identical across all parallel processes you run. This is how Hyperopt tracks which trials belong to the same experiment.
Fix Common Pitfalls
- Version Compatibility: MongoDB 3.7.3 is a development preview version, which might have compatibility issues with Hyperopt. Try downgrading to a stable release like MongoDB 3.6.x or 4.x, and make sure Hyperopt is up to date with
pip install --upgrade hyperopt. - Firewall Blocking: Windows Firewall might be blocking connections to port 1234. Temporarily disable it for testing, or add an inbound rule allowing traffic to MongoDB's port.
- Remote/Multi-Machine Setup: If you're running Hyperopt across multiple machines, replace
localhostwith the IP address of the machine runningmongod, and ensure that machine allows incoming connections on port 1234. - Permission Issues: If you've enabled MongoDB authentication later, you'll need to include credentials in the connection string:
mongo://username:password@localhost:1234/hyperopt_parallel/jobs.
Test with Parallel Processes
To verify parallelism works:
- Keep your
mongodprocess running. - Open 2-3 separate command prompt windows.
- Run the test script above in each window.
You should see each script pick up a trial and run it simultaneously (the 2-second sleep will make this obvious). If this works, the issue was likely in your original Hyperopt code configuration; if not, go back to checking MongoDB's connection and port accessibility.
内容的提问来源于stack exchange,提问作者Nilakash Das

