简体   繁体   English

为什么 Google Colab 花了这么长时间来学习这些简单的神经网络?

[英]Why is Google Colab taking so long for learning these simple neural networks?

I'm trying to run the followin code on google colab:我正在尝试在 google colab 上运行以下代码:

import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from keras import models
from keras import layers
from keras.datasets import boston_housing
from sklearn.model_selection import KFold
from sklearn.model_selection import ShuffleSplit
from sklearn.model_selection import GridSearchCV
from keras.wrappers.scikit_learn import KerasRegressor
from sklearn.metrics import mean_squared_error

sns.set()

(train_data,train_targets),(test_data,test_targets)=boston_housing.load_data()

mean=np.mean(train_data)
std=np.std(train_data)

train_data_norm=(train_data-mean)/std
test_data_norm=(test_data-mean)/std

def build_model():
    model=models.Sequential()
    model.add(layers.Dense(64,activation="relu",
                          input_shape=(train_data_norm.shape[1],)))
    model.add(layers.Dense(64,activation="relu"))
    model.add(layers.Dense(1))
    model.compile(optimizer='rmsprop',loss="mse",metrics=["mae"])
    return model 


model=KerasRegressor(build_fn=build_model,epochs=30,verbose=0)
param_grid = {"epochs":range(1,11)}

kf1=KFold(n_splits=5, random_state=None, shuffle=False) #n_splits = number of folds
kf2=KFold(n_splits=5, random_state=1, shuffle=True)
ss = ShuffleSplit(n_splits=5,test_size=0.20,random_state=1)

grid_model_KFFalse=GridSearchCV(model,param_grid,cv=kf1,n_jobs=-1,scoring='neg_mean_squared_error')
grid_model_KFTrue=GridSearchCV(model,param_grid,cv=kf2,n_jobs=-1,scoring='neg_mean_squared_error')
grid_model_SS=GridSearchCV(model,param_grid,cv=ss,n_jobs=-1,scoring='neg_mean_squared_error')

listKFFalse=[]
listKFTrue=[]
listSS=[]

for i in range(1,21):
  grid_model_KFFalse.fit(train_data, train_targets)
  grid_model_KFTrue.fit(train_data, train_targets)
  grid_model_SS.fit(train_data, train_targets)
  mseKFFalse=mean_squared_error(grid_model_KFFalse.predict(test_data),test_targets)
  mseKFTrue=mean_squared_error(grid_model_KFTrue.predict(test_data),test_targets)
  mseSS=mean_squared_error(grid_model_SS.predict(test_data),test_targets)
  listKFFalse=np.append(listKFFalse,[mseKFFalse])
  listKFTrue=np.append(listKFTrue,[mseKFTrue])
  listSS=np.append(listSS,[mseSS])

You can check it here .你可以在这里查看

I've done on my laptop 1 run on a jupyter notebook, instead of the 21, and the part where it's slowest is in the 'for' block, but that's to be expected.我已经在我的笔记本电脑上完成了 1 在 jupyter 笔记本上运行,而不是 21,它最慢的部分是在“for”块中,但这是可以预料的。

The code compiles without error, since I've already managed to run it for 5 cycles/simulations without problems.代码编译没有错误,因为我已经设法运行它 5 个周期/模拟没有问题。

However, it's taking a very long time... I wonder why it is.但是,这需要很长时间......我想知道为什么会这样。 Isn't google colab supposed to use GPU(or TPU) when learning neural networks with keras?使用 keras 学习神经网络时,google colab 不应该使用 GPU(或 TPU)吗? I've already changed the Runtime type to GPU or TPU, and it's still very slow.我已经把Runtime类型改成了GPU或者TPU,还是很慢。

From the FAQ来自常见问题

Seems too good to be true.似乎好得令人难以置信。 What are the limitations?有什么限制?

Colab resources are not guaranteed and not unlimited, and the usage limits sometimes fluctuate. Colab 资源没有保证也不是无限的,使用限制有时会波动。 This is necessary for Colab to be able to provide resources for free.这是 Colab 能够免费提供资源所必需的。 For more details, see Resource Limits有关更多详细信息,请参阅资源限制

Regarding TPU usage, if you refer to TPUs in Colab you'll see some code snippets for connecting to a TPU:关于 TPU 的使用,如果您参考Colab中的 TPU,您会看到一些用于连接到 TPU 的代码片段:

tpu = tf.distribute.cluster_resolver.TPUClusterResolver() 
tf.config.experimental_connect_to_cluster(tpu)
tf.tpu.experimental.initialize_tpu_system(tpu)
tpu_strategy = tf.distribute.experimental.TPUStrategy(tpu)

In the snippet/colab provided, the TPU isn't being used which is why you see no difference in performance.在提供的代码片段/colab 中,没有使用 TPU,这就是为什么您看不到性能差异的原因。 You would need to migrate from base keras to tf.keras and use distribution strategies if you want to use TPUs.如果要使用 TPU,则需要从基本keras迁移到tf.keras并使用分发策略。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM