Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
tensorflow
GitHub Repository: tensorflow/docs-l10n
Path: blob/master/site/zh-cn/xla/tutorials/autoclustering_xla.ipynb
25118 views
Kernel: Python 3
#@title Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # https://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License.

使用 XLA 对 CIFAR-10 进行分类

本教程将训练一个 TensorFlow 模型来对 CIFAR-10 数据集进行分类,我们会使用 XLA 编译该模型。

您将使用 TensorFlow Datasets (TFDS) API 加载和规范化数据集。首先,安装/升级 TensorFlow 和 TFDS:

!pip install -U -q tensorflow tensorflow_datasets
import tensorflow as tf import tensorflow_datasets as tfds
# Check that GPU is available: cf. https://colab.research.google.com/notebooks/gpu.ipynb assert(tf.test.gpu_device_name()) tf.keras.backend.clear_session() tf.config.optimizer.set_jit(False) # Start with XLA disabled. def load_data(): result = tfds.load('cifar10', batch_size = -1) (x_train, y_train) = result['train']['image'],result['train']['label'] (x_test, y_test) = result['test']['image'],result['test']['label'] x_train = x_train.numpy().astype('float32') / 256 x_test = x_test.numpy().astype('float32') / 256 # Convert class vectors to binary class matrices. y_train = tf.keras.utils.to_categorical(y_train, num_classes=10) y_test = tf.keras.utils.to_categorical(y_test, num_classes=10) return ((x_train, y_train), (x_test, y_test)) (x_train, y_train), (x_test, y_test) = load_data()

我们定义根据 Keras CIFAR-10 示例改编的模型:

def generate_model(): return tf.keras.models.Sequential([ tf.keras.layers.Conv2D(32, (3, 3), padding='same', input_shape=x_train.shape[1:]), tf.keras.layers.Activation('relu'), tf.keras.layers.Conv2D(32, (3, 3)), tf.keras.layers.Activation('relu'), tf.keras.layers.MaxPooling2D(pool_size=(2, 2)), tf.keras.layers.Dropout(0.25), tf.keras.layers.Conv2D(64, (3, 3), padding='same'), tf.keras.layers.Activation('relu'), tf.keras.layers.Conv2D(64, (3, 3)), tf.keras.layers.Activation('relu'), tf.keras.layers.MaxPooling2D(pool_size=(2, 2)), tf.keras.layers.Dropout(0.25), tf.keras.layers.Flatten(), tf.keras.layers.Dense(512), tf.keras.layers.Activation('relu'), tf.keras.layers.Dropout(0.5), tf.keras.layers.Dense(10), tf.keras.layers.Activation('softmax') ]) model = generate_model()

我们使用 RMSprop 优化器训练模型:

def compile_model(model): opt = tf.keras.optimizers.RMSprop(learning_rate=0.0001) model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy']) return model model = compile_model(model) def train_model(model, x_train, y_train, x_test, y_test, epochs=25): model.fit(x_train, y_train, batch_size=256, epochs=epochs, validation_data=(x_test, y_test), shuffle=True) def warmup(model, x_train, y_train, x_test, y_test): # Warm up the JIT, we do not wish to measure the compilation time. initial_weights = model.get_weights() train_model(model, x_train, y_train, x_test, y_test, epochs=1) model.set_weights(initial_weights) warmup(model, x_train, y_train, x_test, y_test) %time train_model(model, x_train, y_train, x_test, y_test) scores = model.evaluate(x_test, y_test, verbose=1) print('Test loss:', scores[0]) print('Test accuracy:', scores[1])

现在,我们使用 XLA 编译器再次训练模型。为了在应用中间启用编译器,我们需要重置 Keras 会话。

# We need to clear the session to enable JIT in the middle of the program. tf.keras.backend.clear_session() tf.config.optimizer.set_jit(True) # Enable XLA. model = compile_model(generate_model()) (x_train, y_train), (x_test, y_test) = load_data() warmup(model, x_train, y_train, x_test, y_test) %time train_model(model, x_train, y_train, x_test, y_test)

在配备 Titan V GPU 和 Intel Xeon E5-2690 CPU 的计算机上,速度提高约 1.17 倍。