第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何在 Tensorflow 2.0 中使用 Google Colab 的 TPU?

如何在 Tensorflow 2.0 中使用 Google Colab 的 TPU?

忽然笑 2022-06-14 10:55:36
我正在嘗試?yán)肎oogle Colab使用張量處理單元 (TPU) 來訓(xùn)練神經(jīng)網(wǎng)絡(luò)。Tensorflow 剛剛發(fā)布了一個(gè)主要版本 2.0,所以我試圖在 Tensorflow 2.0 中實(shí)現(xiàn)這一點(diǎn)。我嘗試了以下三個(gè)指南,但所有指南都是為 Tensorflow 1.14 編寫的,并且在 Tensorflow 2.0 中失?。?)按照Colab 中的 TPU指南,我收到錯(cuò)誤消息:AttributeError: module 'tensorflow' has no attribute 'Session'(來自參考:使用 tf.Session(tpu_address) 作為會(huì)話:)2)按照指南Simple Classification Model using Keras on Colab TPU,我得到了同樣的錯(cuò)誤3)按照指南cloud_tpu_custom_training,我得到錯(cuò)誤:AttributeError: module 'tensorflow' has no attribute 'contrib'(來自參考:resolver = tf.contrib.cluster_resolver.TPUClusterResolver(tpu=TPU_WORKER))有沒有人有使用 TPU 在 Tensorflow 2.0 中訓(xùn)練神經(jīng)網(wǎng)絡(luò)的示例?編輯:這個(gè)問題似乎也出現(xiàn)在 github 上:InvalidArgumentError: Unable to find a context_id matching the specified one #1
查看完整描述

3 回答

?
ITMISS

TA貢獻(xiàn)1871條經(jīng)驗(yàn) 獲得超8個(gè)贊

最后,在 Tensorflow 2.1.0 中添加了對(duì) TPU 的支持(截至 2020 年 1 月 8 日)。從這里的發(fā)行說明https://github.com/tensorflow/tensorflow/releases/tag/v2.1.0

對(duì) Keras .compile、.fit、.evaluate 和 .predict 的實(shí)驗(yàn)性支持適用于 Cloud TPU、Cloud TPU,適用于所有類型的 Keras 模型(順序、功能和子類模型)。

該教程可在此處獲得:https ://www.tensorflow.org/guide/tpu

為了完整起見,我將在此處添加演練:

  1. 轉(zhuǎn)到 Google Colab 并在此處創(chuàng)建一個(gè)新的 Python 3 Notebook:https ://colab.research.google.com/

  2. 在工具欄中,單擊運(yùn)行時(shí)/更改運(yùn)行時(shí)類型,然后在硬件加速器下選擇“TPU”。

  3. 將以下代碼復(fù)制并粘貼到筆記本中,然后單擊運(yùn)行單元(播放按鈕)。

from __future__ import absolute_import, division, print_function, unicode_literals

import tensorflow as tf

import os

import tensorflow_datasets as tfds


# Distribution strategies

resolver = tf.distribute.cluster_resolver.TPUClusterResolver(tpu='grpc://' + os.environ['COLAB_TPU_ADDR'])

tf.config.experimental_connect_to_cluster(resolver)

tf.tpu.experimental.initialize_tpu_system(resolver)


# MNIST model

def create_model():

  return tf.keras.Sequential(

      [tf.keras.layers.Conv2D(32, 3, activation='relu', input_shape=(28, 28, 1)),

       tf.keras.layers.Flatten(),

       tf.keras.layers.Dense(128, activation='relu'),

       tf.keras.layers.Dense(10)])


# Input datasets

def get_dataset(batch_size=200):

  datasets, info = tfds.load(name='mnist', with_info=True, as_supervised=True,

                             try_gcs=True)

  mnist_train, mnist_test = datasets['train'], datasets['test']


  def scale(image, label):

    image = tf.cast(image, tf.float32)

    image /= 255.0


    return image, label


  train_dataset = mnist_train.map(scale).shuffle(10000).batch(batch_size)

  test_dataset = mnist_test.map(scale).batch(batch_size)


  return train_dataset, test_dataset


# Create and train a model

strategy = tf.distribute.experimental.TPUStrategy(resolver)

with strategy.scope():

  model = create_model()

  model.compile(optimizer='adam',

                loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),

                metrics=['sparse_categorical_accuracy'])


train_dataset, test_dataset = get_dataset()


model.fit(train_dataset,

          epochs=5,

          validation_data=test_dataset,steps_per_epoch=50)

請(qǐng)注意,當(dāng)我按原樣運(yùn)行 tensorflow 教程中的代碼時(shí),會(huì)出現(xiàn)以下錯(cuò)誤。我已經(jīng)通過在 model.fit() 中添加 steps_per_epoch 參數(shù)來糾正這個(gè)問題


ValueError:無法從數(shù)據(jù)中推斷出步數(shù),請(qǐng)傳遞 steps_per_epoch 參數(shù)。


查看完整回答
反對(duì) 回復(fù) 2022-06-14
?
鳳凰求蠱

TA貢獻(xiàn)1825條經(jīng)驗(yàn) 獲得超4個(gè)贊

首先,教程中給出的代碼與 2.x 不兼容

  1. 在colab中需要選擇runtime作為TPU才能在TPU中執(zhí)行代碼

  2. 對(duì)于錯(cuò)誤

    AttributeError:模塊“張量流”沒有屬性“會(huì)話”

    您需要tf.compat.v1.Session()tf.session已棄用的方式使用。

  3. 代替tf.contrib.cluster_resolver請(qǐng)使用tf.distribute.cluster_resolver

請(qǐng)參考 Tensorflow Addon-repo將代碼從 1.x 轉(zhuǎn)換為 2.x 兼容。


查看完整回答
反對(duì) 回復(fù) 2022-06-14
?
心有法竹

TA貢獻(xiàn)1866條經(jīng)驗(yàn) 獲得超5個(gè)贊

“tf”的升級(jí)版本將解決上述問題。

!pip install tensorflow==2.7.0


查看完整回答
反對(duì) 回復(fù) 2022-06-14
  • 3 回答
  • 0 關(guān)注
  • 215 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)