keras tensorflow gpu 安装参考资料:
https://www.jianshu.com/p/ff8b57e1552b
https://www.bilibili.com/video/BV1rN411F7q6/?spm_id_from=333.337.search-card.all.click&vd_source=bf6499e4fe14cd2ab4f31e6079409a28
https://blog.csdn.net/shandiandwb/article/details/131140145
比较cpu和gpu运行性能代码
import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt
X_train, y_train), (X_test, y_test) = keras.datasets.cifar10.load_data()
X_train.shape, X_test.shape
X_train[0].shape
y_train[:5]
# scaling image values between 0-1
X_train_scaled = X_train/255
X_test_scaled = X_test/255
# one hot encoding labels
y_train_encoded = keras.utils.to_categorical(y_train, num_classes = 10, dtype = 'float32')
y_test_encoded = keras.utils.to_categorical(y_test, num_classes = 10, dtype = 'float32')
def get_model():
model = keras.Sequential([
keras.layers.Flatten(input_shape=(32,32,3)),
keras.layers.Dense(3000, activation='relu'),
keras.layers.Dense(1000, activation='relu'),
keras.layers.Dense(10, activation='sigmoid')
])
model.compile(optimizer='SGD',
loss='categorical_crossentropy',
metrics=['accuracy'])
return model
%%timeit -n1 -r1
# CPU
with tf.device('/CPU:0'):
model_cpu = get_model()
model_cpu.fit(X_train_scaled, y_train_encoded, epochs = 10)
%%timeit -n1 -r1
# GPU
with tf.device('/GPU:0'):
model_gpu = get_model()
model_gpu.fit(X_train_scaled, y_train_encoded, epochs = 10)