tensorflow的gpu和cpu计算时间对比的小例子(tensorflow对cpu要求)

网友投稿 543 2022-08-23


tensorflow的gpu和cpu计算时间对比的小例子(tensorflow对cpu要求)

例子1

参数设置

NVIDIA3070, cuda11.2 cudnn8.1.0 tensorfow2.5.0,tensorflow-gpu2.5.0

cpu约80 s计算1代epoch, 而 gpu却约3 s计算一代epoch

# -*- coding: utf-8 -*-# @Time : 2022/6/11 16:03# @Author : chuqianyu# @FileName: testtt2tt.py# @Software: PyCharm# # 指定GPU训练# import os# os.environ["CUDA_VISIBLE_DEVICES"]="/gpu:0" ##表示使用GPU编号为0的GPU进行计算import numpy as npfrom tensorflow.keras.models import Sequential # 采用贯序模型from tensorflow.keras.layers import Dense, Dropout, Conv2D, MaxPool2D, Flattenfrom tensorflow.keras.datasets import mnistfrom tensorflow.keras.utils import to_categoricalfrom tensorflow.keras.callbacks import TensorBoardimport timedef create_model(): model = Sequential() model.add(Conv2D(32, (5, 5), activation='relu', input_shape=[28, 28, 1])) # 第一卷积层 model.add(Conv2D(64, (5, 5), activation='relu')) # 第二卷积层 model.add(MaxPool2D(pool_size=(2, 2))) # 池化层 model.add(Flatten()) # 平铺层 model.add(Dropout(0.5)) model.add(Dense(128, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(10, activation='softmax')) return modeldef compile_model(model): model.compile(loss='categorical_crossentropy', optimizer="adam", metrics=['acc']) return modeldef train_model(model, x_train, y_train, batch_size=128, epochs=10): tbCallBack = TensorBoard(log_dir="model", histogram_freq=1, write_grads=True) history = model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, shuffle=True, verbose=2, validation_split=0.2, callbacks=[tbCallBack]) return history, modelif __name__ == "__main__": import tensorflow as tf print(tf.__version__) # NVIDIA3070, cuda11.2 cudnn8.1.0 tensorfow2.5.0,tensorflow-gpu2.5.0 # cpu约80 s计算1代epoch, 而 gpu却约3 s计算一代epoch with tf.device("/gpu:0"): from tensorflow.python.client import device_lib print(device_lib.list_local_devices()) (x_train, y_train), (x_test, y_test) = mnist.load_data() # mnist的数据我自己已经下载好了的 print(np.shape(x_train), np.shape(y_train), np.shape(x_test), np.shape(y_test)) x_train = np.expand_dims(x_train, axis=3) x_test = np.expand_dims(x_test, axis=3) y_train = to_categorical(y_train, num_classes=10) y_test = to_categorical(y_test, num_classes=10) print(np.shape(x_train), np.shape(y_train), np.shape(x_test), np.shape(y_test)) model = create_model() model = compile_model(model) print("start training") ts = time.time() history, model = train_model(model, x_train, y_train, epochs=20) print("start training", time.time() - ts)

gpu约3 s计算一代epoch

cpu约80 s计算一代epoch

例子2

# -*- coding: utf-8 -*-# @Time : 2022/6/11 20:32# @Author : chuqianyu# @FileName: testtt3tt.py# @Software: PyCharmimport tensorflow as tffrom tensorflow.keras import *import timetf.config.set_soft_device_placement(True)tf.debugging.set_log_device_placement(True)gpus = tf.config.experimental.list_physical_devices('GPU')print(gpus)tf.config.experimental.set_visible_devices(gpus[0], 'GPU')tf.config.experimental.set_memory_growth(gpus[0], True)t=time.time()with tf.device("/gpu:0"): tf.random.set_seed(0) a = tf.random.uniform((10000,10000),minval = 0,maxval = 3.0) c = tf.matmul(a, tf.transpose(a)) d = tf.reduce_sum(c)print('gpu: ', time.time()-t)t=time.time()with tf.device("/cpu:0"): tf.random.set_seed(0) a = tf.random.uniform((10000,10000),minval = 0,maxval = 3.0) c = tf.matmul(a, tf.transpose(a)) d = tf.reduce_sum(c)print('cpu: ', time.time()-t)


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Python基础 -- 入门语句(python基础训练题)
下一篇:7天限时 | 机器学习资料(书籍+视频)推荐与分享!(7天限时领取7天体验9毛9)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~