Mac M1 由于arm导致“tensorflow 报错”解决方法

Mac m1 由于arm导致“tensorflow 报错”解决方法

问题描述

最近发现之前在Mac m1安装的tensorflow无法使用了,这里的无法使用不是指无法import之类的错误,而是使用相关模块时代码报错,报错如下(小伙伴们可以参考):

2021-09-03 17:05:21.327506: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:116] None of the MLIR optimization passes are enabled (registered 2)
2021-09-03 17:05:21.327724: W tensorflow/core/platform/profile_utils/cpu_utils.cc:126] Failed to get CPU frequency: 0 Hz
2021-09-03 17:05:21.328620: F tensorflow/core/grappler/costs/op_level_cost_estimator.cc:710] Check failed: 0 < gflops (0 vs. 0)type: "CPU"
model: "0"
num_cores: 8
environment {
  key: "cpu_instruction_set"
  value: "ARM NEON"
}
environment {
  key: "eigen"
  value: "3.3.90"
}
l1_cache_size: 16384
l2_cache_size: 524288
l3_cache_size: 524288
memory_size: 268435456
进程已结束,退出代码为 134 (interrupted by signal 6: SIGABRT)

导致问题的原因

简而言之,就是安装了不正确的tensorflow版本~~

问题解决

  1. First of all, 你需要python版本为 3.8(经靓仔尝试,3.9对本方法似乎不太行)
  2. 在github上下载这个bash脚本:github链接
    或者在下面直接复制,保存为.sh文件:
#!/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

  1. 关于怎么使用bash脚本,请大家参考这个链接哦
    怎样在macOS上运行Shell或.sh脚本
  2. 运行完bash脚本就可以了哦,中间运行脚本需要涉及两个操作:输入y、输入虚拟环境的路径
  3. 现在可以进行测试一下哦
import tensorflow as tf
import time
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
model = tf.keras.models.Sequential([
    tf.keras.layers.Flatten(input_shape=(28, 28)),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dropout(0.2),
    tf.keras.layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])
start = time.time()
model.fit(x_train, y_train, epochs=5)
end = time.time()
model.evaluate(x_test, y_test)
print(end - start)

希望可以解决大家的问题,嘻嘻嘻!

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值