Hyperopt报错HyperoptEstimator.fit报错[Errno 22] Invalid argument的解决办法

本文解决了一个在使用Hyperopt进行超参数优化时遇到的错误,通过添加if __name__ == '__main__':条件语句,成功避免了多进程引起的IOError,并展示了完整的代码示例和运行结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在使用Hyperopt测试下面官方的例子时,报错[Errno 22] Invalid argument

from hpsklearn import HyperoptEstimator, any_classifier,any_preprocessing
from sklearn.datasets import load_iris
from hyperopt import tpe
import numpy as np

# Download the data and split into training and test sets
iris = load_iris()
X = iris.data
y = iris.target

test_size = int(0.2 * len(y))
np.random.seed(13)
indices = np.random.permutation(len(X))
X_train = X[indices[:-test_size]]
y_train = y[indices[:-test_size]]
X_test = X[indices[-test_size:]]
y_test = y[indices[-test_size:]]

# Instantiate a HyperoptEstimator with the search space and number of evaluations
estim = HyperoptEstimator(classifier=any_classifier('my_clf'),
                          preprocessing=any_preprocessing('my_pre'),
                          algo=tpe.suggest)

# Search the hyperparameter space based on the data
estim.fit(X_train, y_train)

# Show the results
print estim.score(X_test, y_test) 

报错详细内容,

Traceback (most recent call last):
  File "<ipython-input-11-8f964d113a8b>", line 1, in <module>
    runfile('C:/Users/asus/untitled56.py', wdir='C:/Users/asus')
  File "d:\Program Files\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile
    execfile(filename, namespace)
  File "d:\Program Files\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py", line 87, in execfile
    exec(compile(scripttext, filename, 'exec'), glob, loc)
  File "C:/Users/asus/untitled56.py", line 29, in <module>
    estim.fit(X_train, y_train)
  File "d:\Program Files\Anaconda2\lib\site-packages\hpsklearn\estimator.py", line 746, in fit
    fit_iter.send(increment)
  File "d:\Program Files\Anaconda2\lib\site-packages\hpsklearn\estimator.py", line 657, in fit_iter
    return_argmin=False, # -- in case no success so far
  File "d:\Program Files\Anaconda2\lib\site-packages\hyperopt\fmin.py", line 367, in fmin
    return_argmin=return_argmin,
  File "d:\Program Files\Anaconda2\lib\site-packages\hyperopt\base.py", line 635, in fmin
    return_argmin=return_argmin)
  File "d:\Program Files\Anaconda2\lib\site-packages\hyperopt\fmin.py", line 385, in fmin
    rval.exhaust()
  File "d:\Program Files\Anaconda2\lib\site-packages\hyperopt\fmin.py", line 244, in exhaust
    self.run(self.max_evals - n_done, block_until_done=self.asynchronous)
  File "d:\Program Files\Anaconda2\lib\site-packages\hyperopt\fmin.py", line 218, in run
    self.serial_evaluate()
  File "d:\Program Files\Anaconda2\lib\site-packages\hyperopt\fmin.py", line 137, in serial_evaluate
    result = self.domain.evaluate(spec, ctrl)
  File "d:\Program Files\Anaconda2\lib\site-packages\hyperopt\base.py", line 840, in evaluate
    rval = self.fn(pyll_rval)
  File "d:\Program Files\Anaconda2\lib\site-packages\hpsklearn\estimator.py", line 605, in fn_with_timeout
    th.start()
  File "d:\Program Files\Anaconda2\lib\multiprocessing\process.py", line 130, in start
    self._popen = Popen(self)
  File "d:\Program Files\Anaconda2\lib\multiprocessing\forking.py", line 280, in __init__
    to_child.close()
IOError: [Errno 22] Invalid argument

最后找到了解决方法,只需加上if __name__ == '__main__':即可,具体如下。

from hpsklearn import HyperoptEstimator, any_classifier,any_preprocessing
from sklearn.datasets import load_iris
from hyperopt import tpe
import numpy as np

# Download the data and split into training and test sets
iris = load_iris()
X = iris.data
y = iris.target

test_size = int(0.2 * len(y))
np.random.seed(13)
indices = np.random.permutation(len(X))
X_train = X[indices[:-test_size]]
y_train = y[indices[:-test_size]]
X_test = X[indices[-test_size:]]
y_test = y[indices[-test_size:]]

# Instantiate a HyperoptEstimator with the search space and number of evaluations
estim = HyperoptEstimator(classifier=any_classifier('my_clf'),
                          preprocessing=any_preprocessing('my_pre'),
                          algo=tpe.suggest,
                          max_evals=100,
                          trial_timeout=120)

if __name__ == '__main__':
    # Search the hyperparameter space based on the data
    estim.fit(X_train, y_train)
    
    # Show the results
    print estim.score(X_test, y_test) 

 输出结果:0.933333333333

完美解决!

参考:https://github.com/hyperopt/hyperopt-sklearn/issues/72

root@VM-20-12-ubuntu:/srs-6.0-d2/trunk# ./objs/srs -c conf/rtmp_srt.conf [2025-07-17 14:51:59.302][INFO][437863][717y9o37] XCORE-SRS/6.0.85(Hang) [2025-07-17 14:51:59.302][INFO][437863][717y9o37] config parse complete [2025-07-17 14:51:59.302][INFO][437863][717y9o37] write log to console [2025-07-17 14:51:59.302][INFO][437863][717y9o37] SRS/6.0.85(Hang), MIT [2025-07-17 14:51:59.302][INFO][437863][717y9o37] authors: Winlin<winlin@vip.126.com> ZhaoWenjie<zhaowenjie@tal.com> ShiWei<shiwei05@kuaishou.com> XiaoZhihong<hondaxiao@tencent.com> WuPengqiang<pengqiang.wpq@alibaba-inc.com> XiaLixin<xialixin@kanzhun.com> LiPeng<mozhan.lp@alibaba-inc.com> ChenGuanghua<jinxue.cgh@alibaba-inc.com> ChenHaibo<nmgchenhaibo@foxmail.com> ZhangJunqin<chundonglinlin@126.com> and https://github.com/ossrs/srs/blob/develop/trunk/AUTHORS.md#contributors [2025-07-17 14:51:59.302][INFO][437863][717y9o37] cwd=/srs-6.0-d2/trunk, work_dir=./, build: 2025-07-17 14:50:05, configure: --srt=on, uname: Linux VM-20-12-ubuntu 5.4.0-192-generic #212-Ubuntu SMP Fri Jul 5 09:47:39 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux, osx: 0, env: 0, pkg: [2025-07-17 14:51:59.302][INFO][437863][717y9o37] configure detail: --prefix=/usr/local/srs --config=conf/srs.conf --osx=off --hls=on --hds=off --dvr=on --ssl=on --https=on --ssl-1-0=off --ssl-local=off --sys-ssl=off --transcode=on --ingest=on --stat=on --http-callback=on --http-server=on --stream-converter=on --http-api=on --utest=off --srt=on --rtc=on --h265=off --gb28181=off --simulator=off --cxx11=on --cxx14=off --backtrace=on --ffmpeg-fit=on --ffmpeg-opus=off --nasm=on --srtp-nasm=on --clean=on --gperf=off --gmc=off --gmd=off --gmp=off --gcp=off --gprof=off --static=off --shared-st=off --shared-srt=off --shared-ffmpeg=off --log-verbose=off --log-info=off --log-trace=on --log-level_v2=on --gcov=off --apm=off --debug=off --debug-stats=off --cross-build=off --sanitizer=on --sanitizer-static=off --sanitizer-log=off --cygwin64=off --single-thread=off --generic-linux=off --cc=gcc --cxx=g++ --ar=ar --ld=ld --randlib=randlib [2025-07-17 14:51:59.302][INFO][437863][717y9o37] srs checking config... [2025-07-17 14:51:59.303][ERROR][437863][717y9o37][22] Failed, code=1023(ConfigInvalid)(Configuration is invalid) : check config : check normal : illegal directive rtmp_server thread [437863][717y9o37]: do_main() [./src/main/srs_main_server.cpp:225][errno=22] thread [437863][717y9o37]: check_config() [./src/app/srs_app_config.cpp:2242][errno=22] thread [437863][717y9o37]: check_normal_config() [./src/app/srs_app_config.cpp:2290][errno=22](Invalid argument) 上述示例启动报错修改
最新发布
07-18
root@VM-20-12-ubuntu:/srs-6.0-d2/trunk# ./objs/srs -c conf/rtmp_srt.conf [2025-07-17 15:47:50.172][INFO][449748][42045zy8] XCORE-SRS/6.0.85(Hang) [2025-07-17 15:47:50.173][INFO][449748][42045zy8] config parse complete [2025-07-17 15:47:50.173][INFO][449748][42045zy8] write log to console [2025-07-17 15:47:50.173][INFO][449748][42045zy8] SRS/6.0.85(Hang), MIT [2025-07-17 15:47:50.173][INFO][449748][42045zy8] authors: Winlin<winlin@vip.126.com> ZhaoWenjie<zhaowenjie@tal.com> ShiWei<shiwei05@kuaishou.com> XiaoZhihong<hondaxiao@tencent.com> WuPengqiang<pengqiang.wpq@alibaba-inc.com> XiaLixin<xialixin@kanzhun.com> LiPeng<mozhan.lp@alibaba-inc.com> ChenGuanghua<jinxue.cgh@alibaba-inc.com> ChenHaibo<nmgchenhaibo@foxmail.com> ZhangJunqin<chundonglinlin@126.com> and https://github.com/ossrs/srs/blob/develop/trunk/AUTHORS.md#contributors [2025-07-17 15:47:50.173][INFO][449748][42045zy8] cwd=/srs-6.0-d2/trunk, work_dir=./, build: 2025-07-17 14:50:05, configure: --srt=on, uname: Linux VM-20-12-ubuntu 5.4.0-192-generic #212-Ubuntu SMP Fri Jul 5 09:47:39 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux, osx: 0, env: 0, pkg: [2025-07-17 15:47:50.173][INFO][449748][42045zy8] configure detail: --prefix=/usr/local/srs --config=conf/srs.conf --osx=off --hls=on --hds=off --dvr=on --ssl=on --https=on --ssl-1-0=off --ssl-local=off --sys-ssl=off --transcode=on --ingest=on --stat=on --http-callback=on --http-server=on --stream-converter=on --http-api=on --utest=off --srt=on --rtc=on --h265=off --gb28181=off --simulator=off --cxx11=on --cxx14=off --backtrace=on --ffmpeg-fit=on --ffmpeg-opus=off --nasm=on --srtp-nasm=on --clean=on --gperf=off --gmc=off --gmd=off --gmp=off --gcp=off --gprof=off --static=off --shared-st=off --shared-srt=off --shared-ffmpeg=off --log-verbose=off --log-info=off --log-trace=on --log-level_v2=on --gcov=off --apm=off --debug=off --debug-stats=off --cross-build=off --sanitizer=on --sanitizer-static=off --sanitizer-log=off --cygwin64=off --single-thread=off --generic-linux=off --cc=gcc --cxx=g++ --ar=ar --ld=ld --randlib=randlib [2025-07-17 15:47:50.173][INFO][449748][42045zy8] srs checking config... [2025-07-17 15:47:50.174][ERROR][449748][42045zy8][22] Failed, code=1023(ConfigInvalid)(Configuration is invalid) : check config : check normal : illegal srt_server.gop_cache thread [449748][42045zy8]: do_main() [./src/main/srs_main_server.cpp:225][errno=22] thread [449748][42045zy8]: check_config() [./src/app/srs_app_config.cpp:2242][errno=22] thread [449748][42045zy8]: check_normal_config() [./src/app/srs_app_config.cpp:2340][errno=22](Invalid argument)
07-18
root@VM-20-12-ubuntu:/srs/trunk# cat conf/srt2.conf # 基础配置 (SRS 6.0+) listen 19935; # RTMP 监听端口 max_connections 1000; daemon off; srs_log_tank console; srs_log_level info; # 生产环境建议使用 info # HTTP 管理接口 http_api { enabled on; listen 1985; crossdomain on; } # HTTP 服务器 http_server { enabled on; listen 8080; dir ./objs/nginx/html; } # SRT 服务器配置 (6.0+ 新语法) srt_server { enabled on; listen 6666; # SRT 高级参数 connect_timeout 4000; maxbw 1000000000; latency 200; tlpktdrop on; payload_size 1316; # 重要:设置MTU优化 } # 默认虚拟主机 vhost __defaultVhost__ { # RTMP 输入配置 rtmp { enabled on; chunk_size 4096; # 超时设置防止流占用 publish_1stpkt_timeout 15000; publish_normal_timeout 15000; drop_idle_publisher on; } # SRT 输出配置 (6.0+ 新语法) srt { enabled on; # 启用 RTMP 到 SRT 的转换 rtmp_to_srt on; # 设置 SRT 为拉流模式 srt_mode caller; # 流映射规则 (6.0+ 使用新变量) srt_stream { input rtmp://127.0.0.1:19935/$ app/$ stream; output srt://:6666?streamid=#!::r=$ app/$ stream,m=request; } } # 启用 HTTP-FLV 用于监控 http_remux { enabled on; mount [vhost]/[app]/[stream].flv; } } root@VM-20-12-ubuntu:/srs/trunk# ./objs/srs -c conf/srt2.conf [2025-07-17 17:39:11.728][INFO][495841][71k10522] XCORE-SRS/6.0.134(Hang) [2025-07-17 17:39:11.728][INFO][495841][71k10522] config parse complete [2025-07-17 17:39:11.728][INFO][495841][71k10522] write log to console [2025-07-17 17:39:11.728][INFO][495841][71k10522] SRS/6.0.134(Hang), MIT [2025-07-17 17:39:11.728][INFO][495841][71k10522] authors: Winlin<winlinvip@gmail.com> XiaoZhihong<hondaxiao@tencent.com> Winlin<winlinvip@gmail.com> ZhaoWenjie<zhaowenjie@tal.com> ShiWei<xiaoq_bj@126.com> XiaoZhihong<hondaxiao@tencent.com> WuPengqiang<pengqiang.wpq@alibaba-inc.com> XiaLixin<xialixin@kanzhun.com> LiPeng<lipeng19811218@gmail.com> ChenGuanghua<jinxue.cgh@alibaba-inc.com> ChenHaibo<nmgchenhaibo@foxmail.com> ZhangJunqin<chundonglinlin@126.com> and https://github.com/ossrs/srs/blob/develop/trunk/AUTHORS.md#contributors [2025-07-17 17:39:11.728][INFO][495841][71k10522] cwd=/srs/trunk, work_dir=./, build: 2025-07-17 16:13:24, configure: --srt=on, uname: Linux VM-20-12-ubuntu 5.4.0-192-generic #212-Ubuntu SMP Fri Jul 5 09:47:39 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux, osx: 0, env: 0, pkg: [2025-07-17 17:39:11.728][INFO][495841][71k10522] configure detail: --prefix=/usr/local/srs --config=conf/srs.conf --osx=off --hls=on --hds=off --dvr=on --ssl=on --https=on --ssl-1-0=off --ssl-local=off --sys-ssl=off --transcode=on --ingest=on --stat=on --http-callback=on --http-server=on --stream-converter=on --http-api=on --utest=off --srt=on --sys-srt=off --rtc=on --h265=on --gb28181=off --simulator=off --cxx11=on --cxx14=off --backtrace=on --ffmpeg-fit=on --sys-ffmpeg=off --ffmpeg-opus=off --nasm=on --srtp-nasm=on --sys-srtp=off --clean=on --gperf=off --gmc=off --gmd=off --gmp=off --gcp=off --gprof=off --static=off --shared-st=off --shared-srt=reserved --shared-ffmpeg=reserved --shared-srtp=reserved --log-verbose=off --log-info=off --log-trace=on --log-level_v2=on --gcov=off --apm=off --debug=off --debug-stats=off --cross-build=off --sanitizer=on --sanitizer-static=off --sanitizer-log=off --cygwin64=off --single-thread=off --generic-linux=off --build-cache=on --cc=gcc --cxx=g++ --ar=ar --ld=ld --randlib=randlib [2025-07-17 17:39:11.728][INFO][495841][71k10522] srs checking config... [2025-07-17 17:39:11.729][ERROR][495841][71k10522][22] Failed, code=1023(ConfigInvalid)(Configuration is invalid) : check config : check normal : illegal srt_server.payload_size thread [495841][71k10522]: do_main() [./src/main/srs_main_server.cpp:225][errno=22] thread [495841][71k10522]: check_config() [./src/app/srs_app_config.cpp:2287][errno=22] thread [495841][71k10522]: check_normal_config() [./src/app/srs_app_config.cpp:2385][errno=22](Invalid argument)
07-18
root@VM-20-12-ubuntu:/srs/trunk# cat conf/srt2.conf # 基础配置 (SRS 6.0.134) listen 19935; # RTMP 监听端口 max_connections 1000; daemon off; srs_log_tank console; srs_log_level info; # HTTP 管理接口 http_api { enabled on; listen 1985; crossdomain on; } # HTTP 服务器 http_server { enabled on; listen 8080; dir ./objs/nginx/html; } # SRT 服务器配置 srt_server { enabled on; listen 6666; connect_timeout 4000; maxbw 1000000000; latency 200; tlpktdrop on; mss 1316; # 最大分段大小 # 新增流映射配置 stream { input rtmp://127.0.0.1:19935/$ app/$ stream; output srt://:6666?streamid=#!::r=$ app/$ stream,m=request; } } # 默认虚拟主机 vhost __defaultVhost__ { # RTMP 配置直接放在 vhost 根级别 chunk_size 4096; # 发布者配置放在 publish 块中 publish { firstpkt_timeout 15000; normal_timeout 15000; } # SRT 输出配置 (简化) srt { enabled on; } # HTTP-FLV 监控 http_remux { enabled on; mount [vhost]/[app]/[stream].flv; } } root@VM-20-12-ubuntu:/srs/trunk# ./objs/srs -c conf/srt2.conf [2025-07-17 18:05:01.091][INFO][503706][3vf0j07n] XCORE-SRS/6.0.134(Hang) [2025-07-17 18:05:01.091][INFO][503706][3vf0j07n] config parse complete [2025-07-17 18:05:01.091][INFO][503706][3vf0j07n] write log to console [2025-07-17 18:05:01.091][INFO][503706][3vf0j07n] SRS/6.0.134(Hang), MIT [2025-07-17 18:05:01.091][INFO][503706][3vf0j07n] authors: Winlin<winlinvip@gmail.com> XiaoZhihong<hondaxiao@tencent.com> Winlin<winlinvip@gmail.com> ZhaoWenjie<zhaowenjie@tal.com> ShiWei<xiaoq_bj@126.com> XiaoZhihong<hondaxiao@tencent.com> WuPengqiang<pengqiang.wpq@alibaba-inc.com> XiaLixin<xialixin@kanzhun.com> LiPeng<lipeng19811218@gmail.com> ChenGuanghua<jinxue.cgh@alibaba-inc.com> ChenHaibo<nmgchenhaibo@foxmail.com> ZhangJunqin<chundonglinlin@126.com> and https://github.com/ossrs/srs/blob/develop/trunk/AUTHORS.md#contributors [2025-07-17 18:05:01.091][INFO][503706][3vf0j07n] cwd=/srs/trunk, work_dir=./, build: 2025-07-17 17:53:32, configure: --srt=on --sanitizer=off, uname: Linux VM-20-12-ubuntu 5.4.0-192-generic #212-Ubuntu SMP Fri Jul 5 09:47:39 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux, osx: 0, env: 0, pkg: [2025-07-17 18:05:01.091][INFO][503706][3vf0j07n] configure detail: --prefix=/usr/local/srs --config=conf/srs.conf --osx=off --hls=on --hds=off --dvr=on --ssl=on --https=on --ssl-1-0=off --ssl-local=off --sys-ssl=off --transcode=on --ingest=on --stat=on --http-callback=on --http-server=on --stream-converter=on --http-api=on --utest=off --srt=on --sys-srt=off --rtc=on --h265=on --gb28181=off --simulator=off --cxx11=on --cxx14=off --backtrace=on --ffmpeg-fit=on --sys-ffmpeg=off --ffmpeg-opus=off --nasm=on --srtp-nasm=on --sys-srtp=off --clean=on --gperf=off --gmc=off --gmd=off --gmp=off --gcp=off --gprof=off --static=off --shared-st=off --shared-srt=reserved --shared-ffmpeg=reserved --shared-srtp=reserved --log-verbose=off --log-info=off --log-trace=on --log-level_v2=on --gcov=off --apm=off --debug=off --debug-stats=off --cross-build=off --sanitizer=off --sanitizer-static=off --sanitizer-log=off --cygwin64=off --single-thread=off --generic-linux=off --build-cache=on --cc=gcc --cxx=g++ --ar=ar --ld=ld --randlib=randlib [2025-07-17 18:05:01.091][INFO][503706][3vf0j07n] srs checking config... [2025-07-17 18:05:01.091][ERROR][503706][3vf0j07n][22] Failed, code=1023(ConfigInvalid)(Configuration is invalid) : check config : check normal : illegal srt_server.stream thread [503706][3vf0j07n]: do_main() [./src/main/srs_main_server.cpp:225][errno=22] thread [503706][3vf0j07n]: check_config() [./src/app/srs_app_config.cpp:2287][errno=22] thread [503706][3vf0j07n]: check_normal_config() [./src/app/srs_app_config.cpp:2385][errno=22](Invalid argument)
07-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值