TPU runs as slow as CPU when using keras_to_tpu_model in colab










2















I use tf.contrib.tpu.keras_to_tpu_model to make my code be able to run on TPU,but it took 170 hours to finish an epoch while CPU took the same time and GPU took only 40 hours per epoch.I tried to adjust batch size but nothing changed.And I've tested the input function may take up 20% of the run time when running on GPU, so I think it's maybe not the main reason.



Here is my code:https://github.com/WangHexie/DHNE/blob/master/src/hypergraph_embedding.py



Run on colab:



  1. TPU:https://colab.research.google.com/gist/WangHexie/30c385509f9cd93be747f04c39f039a4/tpu-error.ipynb

  2. GPU:https://colab.research.google.com/gist/WangHexie/5bfac53bf92ef0ad527f15ddbf8705e1/-gpu-ipynb.ipynb

The model:



def build_model(self):
self.inputs = [Input(shape=(self.options.dim_feature[i], ), name='input_'.format(i), dtype='float') for i in range(3)]

self.encodeds = [Dense(self.options.embedding_size[i], activation='tanh', name='encode_'.format(i))(self.inputs[i]) for i in range(3)]
self.decodeds = [Dense(self.options.dim_feature[i], activation='sigmoid', name='decode_'.format(i),
activity_regularizer = regularizers.l2(0.0))(self.encodeds[i]) for i in range(3)]

self.merged = concatenate(self.encodeds, axis=1)
self.hidden_layer = Dense(self.options.hidden_size, activation='tanh', name='full_connected_layer')(self.merged)
self.ouput_layer = Dense(1, activation='sigmoid', name='classify_layer')(self.hidden_layer)

self.model = Model(inputs=self.inputs, outputs=self.decodeds+[self.ouput_layer])

self.model.compile(optimizer=tf.train.AdamOptimizer(learning_rate=self.options.learning_rate),
loss=[self.sparse_autoencoder_error]*3+['binary_crossentropy'],
loss_weights=[self.options.alpha]*3+[1.0],
metrics=dict([('decode_'.format(i), 'mse') for i in range(3)]+[('classify_layer', 'accuracy')]))
self.model = tf.contrib.tpu.keras_to_tpu_model(
self.model,
strategy=tf.contrib.tpu.TPUDistributionStrategy(
tf.contrib.cluster_resolver.TPUClusterResolver(
tpu='grpc://' + os.environ['COLAB_TPU_ADDR'])
)
)
self.model.summary()









share|improve this question




























    2















    I use tf.contrib.tpu.keras_to_tpu_model to make my code be able to run on TPU,but it took 170 hours to finish an epoch while CPU took the same time and GPU took only 40 hours per epoch.I tried to adjust batch size but nothing changed.And I've tested the input function may take up 20% of the run time when running on GPU, so I think it's maybe not the main reason.



    Here is my code:https://github.com/WangHexie/DHNE/blob/master/src/hypergraph_embedding.py



    Run on colab:



    1. TPU:https://colab.research.google.com/gist/WangHexie/30c385509f9cd93be747f04c39f039a4/tpu-error.ipynb

    2. GPU:https://colab.research.google.com/gist/WangHexie/5bfac53bf92ef0ad527f15ddbf8705e1/-gpu-ipynb.ipynb

    The model:



    def build_model(self):
    self.inputs = [Input(shape=(self.options.dim_feature[i], ), name='input_'.format(i), dtype='float') for i in range(3)]

    self.encodeds = [Dense(self.options.embedding_size[i], activation='tanh', name='encode_'.format(i))(self.inputs[i]) for i in range(3)]
    self.decodeds = [Dense(self.options.dim_feature[i], activation='sigmoid', name='decode_'.format(i),
    activity_regularizer = regularizers.l2(0.0))(self.encodeds[i]) for i in range(3)]

    self.merged = concatenate(self.encodeds, axis=1)
    self.hidden_layer = Dense(self.options.hidden_size, activation='tanh', name='full_connected_layer')(self.merged)
    self.ouput_layer = Dense(1, activation='sigmoid', name='classify_layer')(self.hidden_layer)

    self.model = Model(inputs=self.inputs, outputs=self.decodeds+[self.ouput_layer])

    self.model.compile(optimizer=tf.train.AdamOptimizer(learning_rate=self.options.learning_rate),
    loss=[self.sparse_autoencoder_error]*3+['binary_crossentropy'],
    loss_weights=[self.options.alpha]*3+[1.0],
    metrics=dict([('decode_'.format(i), 'mse') for i in range(3)]+[('classify_layer', 'accuracy')]))
    self.model = tf.contrib.tpu.keras_to_tpu_model(
    self.model,
    strategy=tf.contrib.tpu.TPUDistributionStrategy(
    tf.contrib.cluster_resolver.TPUClusterResolver(
    tpu='grpc://' + os.environ['COLAB_TPU_ADDR'])
    )
    )
    self.model.summary()









    share|improve this question


























      2












      2








      2


      1






      I use tf.contrib.tpu.keras_to_tpu_model to make my code be able to run on TPU,but it took 170 hours to finish an epoch while CPU took the same time and GPU took only 40 hours per epoch.I tried to adjust batch size but nothing changed.And I've tested the input function may take up 20% of the run time when running on GPU, so I think it's maybe not the main reason.



      Here is my code:https://github.com/WangHexie/DHNE/blob/master/src/hypergraph_embedding.py



      Run on colab:



      1. TPU:https://colab.research.google.com/gist/WangHexie/30c385509f9cd93be747f04c39f039a4/tpu-error.ipynb

      2. GPU:https://colab.research.google.com/gist/WangHexie/5bfac53bf92ef0ad527f15ddbf8705e1/-gpu-ipynb.ipynb

      The model:



      def build_model(self):
      self.inputs = [Input(shape=(self.options.dim_feature[i], ), name='input_'.format(i), dtype='float') for i in range(3)]

      self.encodeds = [Dense(self.options.embedding_size[i], activation='tanh', name='encode_'.format(i))(self.inputs[i]) for i in range(3)]
      self.decodeds = [Dense(self.options.dim_feature[i], activation='sigmoid', name='decode_'.format(i),
      activity_regularizer = regularizers.l2(0.0))(self.encodeds[i]) for i in range(3)]

      self.merged = concatenate(self.encodeds, axis=1)
      self.hidden_layer = Dense(self.options.hidden_size, activation='tanh', name='full_connected_layer')(self.merged)
      self.ouput_layer = Dense(1, activation='sigmoid', name='classify_layer')(self.hidden_layer)

      self.model = Model(inputs=self.inputs, outputs=self.decodeds+[self.ouput_layer])

      self.model.compile(optimizer=tf.train.AdamOptimizer(learning_rate=self.options.learning_rate),
      loss=[self.sparse_autoencoder_error]*3+['binary_crossentropy'],
      loss_weights=[self.options.alpha]*3+[1.0],
      metrics=dict([('decode_'.format(i), 'mse') for i in range(3)]+[('classify_layer', 'accuracy')]))
      self.model = tf.contrib.tpu.keras_to_tpu_model(
      self.model,
      strategy=tf.contrib.tpu.TPUDistributionStrategy(
      tf.contrib.cluster_resolver.TPUClusterResolver(
      tpu='grpc://' + os.environ['COLAB_TPU_ADDR'])
      )
      )
      self.model.summary()









      share|improve this question
















      I use tf.contrib.tpu.keras_to_tpu_model to make my code be able to run on TPU,but it took 170 hours to finish an epoch while CPU took the same time and GPU took only 40 hours per epoch.I tried to adjust batch size but nothing changed.And I've tested the input function may take up 20% of the run time when running on GPU, so I think it's maybe not the main reason.



      Here is my code:https://github.com/WangHexie/DHNE/blob/master/src/hypergraph_embedding.py



      Run on colab:



      1. TPU:https://colab.research.google.com/gist/WangHexie/30c385509f9cd93be747f04c39f039a4/tpu-error.ipynb

      2. GPU:https://colab.research.google.com/gist/WangHexie/5bfac53bf92ef0ad527f15ddbf8705e1/-gpu-ipynb.ipynb

      The model:



      def build_model(self):
      self.inputs = [Input(shape=(self.options.dim_feature[i], ), name='input_'.format(i), dtype='float') for i in range(3)]

      self.encodeds = [Dense(self.options.embedding_size[i], activation='tanh', name='encode_'.format(i))(self.inputs[i]) for i in range(3)]
      self.decodeds = [Dense(self.options.dim_feature[i], activation='sigmoid', name='decode_'.format(i),
      activity_regularizer = regularizers.l2(0.0))(self.encodeds[i]) for i in range(3)]

      self.merged = concatenate(self.encodeds, axis=1)
      self.hidden_layer = Dense(self.options.hidden_size, activation='tanh', name='full_connected_layer')(self.merged)
      self.ouput_layer = Dense(1, activation='sigmoid', name='classify_layer')(self.hidden_layer)

      self.model = Model(inputs=self.inputs, outputs=self.decodeds+[self.ouput_layer])

      self.model.compile(optimizer=tf.train.AdamOptimizer(learning_rate=self.options.learning_rate),
      loss=[self.sparse_autoencoder_error]*3+['binary_crossentropy'],
      loss_weights=[self.options.alpha]*3+[1.0],
      metrics=dict([('decode_'.format(i), 'mse') for i in range(3)]+[('classify_layer', 'accuracy')]))
      self.model = tf.contrib.tpu.keras_to_tpu_model(
      self.model,
      strategy=tf.contrib.tpu.TPUDistributionStrategy(
      tf.contrib.cluster_resolver.TPUClusterResolver(
      tpu='grpc://' + os.environ['COLAB_TPU_ADDR'])
      )
      )
      self.model.summary()






      python tensorflow keras google-colaboratory google-cloud-tpu






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 15 '18 at 6:06









      Milo Lu

      1,65311628




      1,65311628










      asked Nov 15 '18 at 3:24









      DiIliDiIli

      258




      258






















          0






          active

          oldest

          votes












          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53311962%2ftpu-runs-as-slow-as-cpu-when-using-keras-to-tpu-model-in-colab%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53311962%2ftpu-runs-as-slow-as-cpu-when-using-keras-to-tpu-model-in-colab%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo