It turned out the code wasn't the problem, but there was something wrong with my software. After the following steps, the above code runs without errors or warnings:
卸载anaconda
安装anaconda
创建新环境
将所需的软件包安装到该环境中(keras,tensorflow,
间谍...)
在该环境中运行代码
uninstall anaconda
install anaconda
create new environment
install required packages into that environment (keras, tensorflow,
spyder...)