windows10安装Tensorflow-gpu 2.10.0
本文主要目的是 从0开始演示 在windows10 平台安装Tensorflow-gpu 2.10.0。
Tensorflow-gpu 2.10.0 之后的版本,不再支持这样的安装方式,如果有需要,请参考wsl安装ubuntu的方式,进行安装。
1.安装miniconda
https://docs.anaconda.com/free/miniconda/index.html
2.安装CUDA
tensorflow-cuda-cudnn对应版本
tensorflow-cuda-cudnn
下载 CUDA11.2
cuda11.2 | https://developer.nvidia.com/cuda-toolkit-archive
cuda安装完之后,已经配置好环境路径了,直接在cmd中查看
nvcc -V
下载cudnn8.10
cudnn | https://developer.nvidia.com/rdp/cudnn-archive
把cudnn8.10解压出来的文件,拷贝到cuda下,有对应的文件下名称,对应拷贝过去。
3.创建python环境
conda create --name tf2.10 python==3.10.14conda activate tf2.10
4.安装Tensorflow-GPU 2.10.0
Tensorflow-GPU 2.10.0
pip install tensorflow-gpu==2.10.0 -i https://pypi.tuna.tsinghua.edu.cn/simple/
安装一些常用常用包
pip install scikit-learn einops ipywidgets pandas tqdm jupyterlab matplotlib seaborn -i https://pypi.tuna.tsinghua.edu.cn/simple/
测试
python ./mnist.py
import tensorflow as tf
print(tf.__version__)
print(tf.config.list_physical_devices('GPU'))
print(tf.test.is_built_with_cuda())import tensorflow as tf
import seaborn as sns
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.image as mpimgprint(tf.__version__)
print(tf.config.list_physical_devices('GPU'))mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data(path="mnist.npz")input_shape = (28, 28, 1)x_train=x_train.reshape(x_train.shape[0], x_train.shape[1], x_train.shape[2], 1)
x_train=x_train / 255.0
x_test = x_test.reshape(x_test.shape[0], x_test.shape[1], x_test.shape[2], 1)
x_test=x_test/255.0y_train = tf.one_hot(y_train.astype(np.int32), depth=10)
y_test = tf.one_hot(y_test.astype(np.int32), depth=10)batch_size = 64
num_classes = 10
epochs = 5model = tf.keras.models.Sequential([tf.keras.layers.Conv2D(32, (5,5), padding='same', activation='relu', input_shape=input_shape),tf.keras.layers.Conv2D(32, (5,5), padding='same', activation='relu'),tf.keras.layers.MaxPool2D(),tf.keras.layers.Dropout(0.25),tf.keras.layers.Conv2D(64, (3,3), padding='same', activation='relu'),tf.keras.layers.Conv2D(64, (3,3), padding='same', activation='relu'),tf.keras.layers.MaxPool2D(strides=(2,2)),tf.keras.layers.Dropout(0.25),tf.keras.layers.Flatten(),tf.keras.layers.Dense(128, activation='relu'),tf.keras.layers.Dropout(0.5),tf.keras.layers.Dense(num_classes, activation='softmax')
])model.compile(optimizer=tf.keras.optimizers.RMSprop(epsilon=1e-08), loss='categorical_crossentropy', metrics=['acc'])class myCallback(tf.keras.callbacks.Callback):def on_epoch_end(self, epoch, logs={}):if(logs.get('acc')>0.995):print("\nReached 99.5% accuracy so cancelling training!")self.model.stop_training = Truecallbacks = myCallback()history = model.fit(x_train, y_train,batch_size=batch_size,epochs=epochs,validation_split=0.1,callbacks=[callbacks])test_loss, test_acc = model.evaluate(x_test, y_test)