MacOS M1安装TensorFlow

1. 创建环境

1
2
conda create -n [name]
conda activate [name]

2. 安装基础 TensorFlow

1
2
conda install -c apple tensorflow-deps
python -m pip install tensorflow-macos

3. 安装 tensorflow-metal 插件

1
python -m pip install tensorflow-metal

4. 验证

验证脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 启动python
python

# 粘贴下面脚本执行
import tensorflow as tf

cifar = tf.keras.datasets.cifar100
(x_train, y_train), (x_test, y_test) = cifar.load_data()
model = tf.keras.applications.ResNet50(
include_top=True,
weights=None,
input_shape=(32, 32, 3),
classes=100,)

loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
model.compile(optimizer="adam", loss=loss_fn, metrics=["accuracy"])
model.fit(x_train, y_train, epochs=5, batch_size=64)
作者

马少杰

发布于

2023-02-05

更新于

2023-03-27

许可协议