官方中文网站:
前提:
安装了cuda 和cudnn;否则就只能安装cpu版本的tensorflow了
炸一下当前可选版本:
pip install tensorflow==7777
本地安装tensorflow gpu 版本的命令:
$ pip install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple
$ pip install tensorflow-gpu -i https://pypi.tuna.tsinghua.edu.cn/simple
[2023年,最新的tensorflow不需要 tensorflow-gpu了,都使用 pip install tensorflow 即可,这包含了gpu版本;】
本地安装tensorflow cpu 版本的命令:
$ pip install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple
$ pip install tensorflow==2.11.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
测试:
进入ipython3后测试,
In [1]: import tensorflow as tf
In [2]: from tensorflow import keras
In [3]: a = tf.constant([1,3,7])
In [4]: a
Out[4]: <tf.Tensor: shape=(3,), dtype=int32, numpy=array([1, 3, 7], dtype=int32)>
In [5]: a.shape
Out[5]: TensorShape([3])
In [6]: print(a)
tf.Tensor([1 3 7], shape=(3,), dtype=int32)
In [7]: a.device
Out[7]: '/job:localhost/replica:0/task:0/device:GPU:0'
In [8]: net_1 = keras.layers.Dense(100)
In [9]: net_1
Out[9]: <keras.layers.core.dense.Dense at 0x7fb85c412940>
In [10]: net_1.device
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-10-f73786dcf74c> in <module>
----> 1 net_1.device
AttributeError: 'Dense' object has no attribute 'device'
In [11]: print(net_1)
<keras.layers.core.dense.Dense object at 0x7fb85c412940>
In [12]: