这是我的代码:
java.lang.SecurityException: Permission denied (missing INTERNET permission?)
当我使用cpu模式时,结果如下:
%tensorflow_version 2.x
import tensorflow as tf
import timeit
dataset = tf.keras.datasets.mnist
(train_images, train_labels), (test_images, test_labels) = dataset.load_data()
train_images = train_images/255.0
test_images = test_images/255.0
def gpu():
with tf.device("/cpu:0"):
# with tf.device("/device:GPU:0"):
network = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape = (28,28)),
tf.keras.layers.Dense(128,activation = 'relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10,activation = 'softmax')
])
network.compile(optimizer=tf.keras.optimizers.Adam(0.0001),
loss=tf.keras.losses.SparseCategoricalCrossentropy(),
metrics=['accuracy'])
# network.evaluate(test_images,test_labels,verbose=2)
callbacks = [
tf.keras.callbacks.EarlyStopping(patience=2, monitor='val_accuracy')
# tf.keras.callbacks.TensorBoard(log_dir='./log')
]
history = network.fit(train_images,train_labels,batch_size=32,epochs = 1,validation_data=(test_images,test_labels),validation_batch_size=32,validation_freq=1,callbacks=callbacks)
network.evaluate(test_images, test_labels, verbose=2)
gpu_time = timeit.timeit('gpu()', number=1, setup="from __main__ import gpu")
print(gpu_time)
当我更改为GPU模式时,结果如下:
1875/1875 [==============================] - 3s 2ms/step - loss: 0.6937 - accuracy: 0.8148 - val_loss: 0.3347 - val_accuracy: 0.9113
313/313 - 0s - loss: 0.3347 - accuracy: 0.9113
3.9582922519998647
我想知道为什么在我的代码中使用gpu的速度更慢。当我使用colab提供的代码时,GPU的速度大约是CPU的10倍。我确信GPU可以在我的环境中正常工作。