无法在带有 BigSur 的 Mac M1 上使用 keras 模型

Cannot use keras models on Mac M1 with BigSur

我正在尝试使用 tensorflow 的 keras 中的顺序模型。当我执行以下语句时:

model.fit(x_train, y_train, epochs=20, verbose=True, validation_data=(x_dev, y_dev) , batch_size=10)

我收到以下错误:

I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:116] None of the MLIR optimization passes are enabled (registered 2)

W tensorflow/core/platform/profile_utils/cpu_utils.cc:126] Failed to get CPU frequency: 0 Hz

F tensorflow/core/grappler/costs/op_level_cost_estimator.cc:710] Check failed: 0 < gflops (0 vs. 0)type: "CPU"

我无法理解如何修复它。谁能帮帮我。

从 github 的 this issue 开始,我了解到 device.frequency() 返回 0 可能是因为 NominalCPUFrequency() 返回 1。 但是,这些信息对我来说似乎太抽象了,我看不懂。

前两个不用担心。

第三个有问题。您安装了错误版本的 TensorFlow。使用支持 Mac M1 芯片的芯片。

运行下面的bash脚本下载安装TensorFlow。

#!/bin/bash

set -e

VERSION=0.1alpha3
INSTALLER_PACKAGE=tensorflow_macos-$VERSION.tar.gz
INSTALLER_PATH=https://github.com/apple/tensorflow_macos/releases/download/v$VERSION/$INSTALLER_PACKAGE
INSTALLER_SCRIPT=install_venv.sh

echo

# Check to make sure we're good to go.
if [[ $(uname) != Darwin ]] || [[ $(sw_vers -productName) != macOS ]] || [[ $(sw_vers -productVersion) != "11."* ]] ; then 
  echo "ERROR: TensorFlow with ML Compute acceleration is only available on macOS 11.0 and later." 
  exit 1
fi

# This 
echo "Installation script for pre-release tensorflow_macos $VERSION.  Please visit https://github.com/apple/tensorflow_macos "
echo "for instructions and license information."   
echo
echo "This script will download tensorflow_macos $VERSION and needed binary dependencies, then install them into a new "
echo "or existing Python 3.8 virtual environment."

# Make sure the user knows what's going on.  
read -p 'Continue [y/N]? '    

if [[ ! $REPLY =~ ^[Yy]$ ]]
then
exit 1
fi
echo

echo "Downloading installer."
tmp_dir=$(mktemp -d)

pushd $tmp_dir

curl -LO $INSTALLER_PATH 

echo "Extracting installer."
tar xf $INSTALLER_PACKAGE

cd tensorflow_macos 

function graceful_error () { 
  echo 
  echo "Error running installation script with default options.  Please fix the above errors and proceed by running "
  echo 
  echo "  $PWD/$INSTALLER_SCRIPT --prompt"
  echo 
  echo
  exit 1
}

bash ./$INSTALLER_SCRIPT --prompt || graceful_error 

popd
rm -rf $tmp_dir

参考:https://github.com/apple/tensorflow_macos

我在 macOS 11.4 上做了如下操作(尽管 ref 说“OS Requirements macOS 12.0+”),python== 3.8.2 并工作 [ref: https://developer.apple.com/metal/tensorflow-plugin/]:

  1. 在 x86 终端上创建一个 venv,即 Rosetta 终端(参见:https://dev.to/courier/tips-and-tricks-to-setup-your-apple-m1-for-development-547g) 即环境设置: x86:AMD 创建 venv:python3 -m venv ~/PATH/tensorflow-metal (用你的真实 PATH 替换 PATH) 激活 venv:source ~/PATH/tensorflow-metal/bin/activate 更新 pip:python -m pip install -U pip

  2. 安装您需要的任何 library/package。例如: 例如:pip install matplotlib jupyterlab

  3. 安装基础张量流: python -m pip install tensorflow-macos

  4. 安装金属插件: python -m pip 安装 tensorflow-metal

祝你好运,干杯!

这可能根本没有帮助,但由于我 运行 遇到了同样的问题,我设法在没有此处提供的解决方案(我很快就会尝试)的情况下训练模型,只需更改我的Y_test(0s 和 1s)在制作 train_test_split 时像这样:(to_categorical(label)。所以:

X_train, X_test, Y_train, Y_test = train_test_split(dataset, 
                                                to_categorical(label), 
                                                test_size=.2, 
                                                random_state=42)

然后,在训练模型时,我收到以下消息 - 我不完全理解:

2022-04-03 23:10:08.941296: I tensorflow/core/grappler/optimizers/custom_graph_optimizer_registry.cc:112] Plugin optimizer for device_type GPU is enabled.

因此,这并不是一个真正的解决方案,而是一个临时的解决方法 - 或者它可能会深入了解哪里出了问题。