写作初衷,本人学了半年TensorFlow,为此买了一个RTX3080的本本,安装和调试TF花了也将近500多,但是最后还是自己的项目跑不起来。就是安装错了TF版本。
目前已知RTX3080支持的TensorFlow版本有TensorFlow2.0.0的CPU版,和TensorFlow2.5.0的GPU版。
安装教程如下:
安装anaconda较高版本,不然会报如下错误:
WARNING: The conda.compat module is deprecated and will be removed in a future release
安装TensorFlow2.0.0的CPU版本,如果安装GPU版本就会报如下错误:
InternalError: Blas GEMM launch failed : a.shape=(100, 784), b.shape=(784, 10), m=100, n=10...
意思就是无法产生这种矩阵,因为TensorFlow2.0.0的GPU版调不动RTX3080显卡。看不起谁呢!!!
所以:安装TensorFlow2.0.0的CPU版就行了。
步骤如下:
打开cmd(按win+R):
依次输入:
#创建tensorflow环境(带#句子不用输入)
conda create -n tensorflow tensorflow=2.0.0
#激活tensorflow环境
conda activate tensorflow
#安装Python
pip install ipython
这样几步下来就成功了。
如果不按上面步骤安装,自己下载cuda安装的话就会报如下错误:
tensorflow. python. framework. errors_impl. InternalError: cudaGetDevice) failed. Status: cudaGetErr
或:
failed to run cuBLAS routine cublasSgemm_v2: CUBLAS_STATUS_EXECUTION_FAILED
啊!我到底经历了什么!!!
====================+++=====华丽的分割线+++++==================
如果想用GPU版本的那就得安装TensorFlow2.5.0的GPU版,更高版本的会报如下错误:
ModuleNotFoundError: No module named ‘keras
或:
tensorflow.python.framework.errors_impl.InternalError: Failed to create session.
或:
module 'tf.random has no attribute 'set_seed'
此时也不能自己安装最新Keras,因为安装后会报如下错误:
AlreadyExistsError: Another metric with the same name already exists.
所以安装TensorFlow2.5.0的GPU版的步骤如下:
1.确保先安装好了高版本的Anaconda,然后打开cmd(按win+R):
2.依次输入:
#创建tf2环境(带#句子不用输入),这是为了创建一个新环境,和前面2.0.0版本的环境名区分。
conda create -n tf2 tensorflow-gpu=2.5.0
#激活tf2环境
conda activate tf2
#安装Python
pip install ipython
这样几步下来就成功了。
PS:安装完了如果你想换numpy它会有很多和当前版本的类库不匹配的提示。所以,根据提示把类库改成相应版本。
方法,卸载numpy,重新安装适应版本的numpy.就会出现提示。
测试:在pycharm中粘贴以下代码:
亲测两个版本的tf都能运行。
运行前,首先要在pycharm中选择anaconda中的编译环境:上面安装时创建的tf2或者tensorflow。可以在创建.py文件时指定,也可以在复制完代码后点pycharm的右下角Python版本:选择AddInterpreter,指定anaconda环境。
注意:本文章只针对RTX3080显卡,其它显卡请自行探索。
import tensorflow as tf
from tensorflow.keras import datasets, layers, optimizers, Sequential, metrics
def preprocess(x, y):
x = tf.cast(x, dtype=tf.float32) / 255.
y = tf.cast(y, dtype=tf.int32)
return x,y
batchsz = 128
(x, y), (x_val, y_val) = datasets.mnist.load_data()
print('datasets:', x.shape, y.shape, x.min(), x.max())
db = tf.data.Dataset.from_tensor_slices((x,y))
db = db.map(preprocess).shuffle(60000).batch(batchsz).repeat(10)
ds_val = tf.data.Dataset.from_tensor_slices((x_val, y_val))
ds_val = ds_val.map(preprocess).batch(batchsz)
network = Sequential([layers.Dense(256, activation='relu'),
layers.Dense(128, activation='relu'),
layers.Dense(64, activation='relu'),
layers.Dense(32, activation='relu'),
layers.Dense(10)])
network.build(input_shape=(None, 28*28))
network.summary()
optimizer = optimizers.Adam(learning_rate=0.01)
acc_meter = metrics.Accuracy()
loss_meter = metrics.Mean()
for step, (x,y) in enumerate(db):
with tf.GradientTape() as tape:
# [b, 28, 28] => [b, 784]
x = tf.reshape(x, (-1, 28*28))
# [b, 784] => [b, 10]
out = network(x)
# [b] => [b, 10]
y_onehot = tf.one_hot(y, depth=10)
# [b]
loss = tf.reduce_mean(tf.losses.categorical_crossentropy(y_onehot, out, from_logits=True))
loss_meter.update_state(loss)
grads = tape.gradient(loss, network.trainable_variables)
optimizer.apply_gradients(zip(grads, network.trainable_variables))
if step % 100 == 0:
print(step, 'loss:', loss_meter.result().numpy())
loss_meter.reset_states()
# evaluate
if step % 500 == 0:
total, total_correct = 0., 0
acc_meter.reset_states()
for step, (x, y) in enumerate(ds_val):
# [b, 28, 28] => [b, 784]
x = tf.reshape(x, (-1, 28*28))
# [b, 784] => [b, 10]
out = network(x)
# [b, 10] => [b]
pred = tf.argmax(out, axis=1)
pred = tf.cast(pred, dtype=tf.int32)
# bool type
correct = tf.equal(pred, y)
# bool tensor => int tensor => numpy
total_correct += tf.reduce_sum(tf.cast(correct, dtype=tf.int32)).numpy()
total += x.shape[0]
acc_meter.update_state(y, pred)
print(step, 'Evaluate Acc:', total_correct/total, acc_meter.result().numpy())
博主在使用RTX3080运行TensorFlow时遇到问题,包括版本兼容性、矩阵运算失败和库冲突等。解决办法是安装TensorFlow2.0.0的CPU版或2.5.0的GPU版,并通过conda创建特定环境。详细步骤包括创建环境、激活环境、安装Python和避免库版本冲突。

1万+

被折叠的 条评论
为什么被折叠?



