Python 如何将tensorflow模型转换为针对TPU进行优化?

Python 如何将tensorflow模型转换为针对TPU进行优化?,python,tensorflow,machine-learning,Python,Tensorflow,Machine Learning,我一直在尝试使用colab提供的TPU,因为它据说速度很快,但似乎无法做到这一点。我使用的是tensorflow 2.4.1。我一直在努力遵循这一点,但没有运气。这是密码 这是我试图转换它的方法 resolver = tf.distribute.cluster_resolver.TPUClusterResolver(tpu='') tf.config.experimental_connect_to_cluster(resolver) # This is the TPU initializati

我一直在尝试使用colab提供的TPU,因为它据说速度很快,但似乎无法做到这一点。我使用的是tensorflow 2.4.1。我一直在努力遵循这一点,但没有运气。这是密码

这是我试图转换它的方法

resolver = tf.distribute.cluster_resolver.TPUClusterResolver(tpu='')
tf.config.experimental_connect_to_cluster(resolver)
# This is the TPU initialization code that has to be at the beginning.
tf.tpu.experimental.initialize_tpu_system(resolver)
print("All devices: ", tf.config.list_logical_devices('TPU'))
a = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
b = tf.constant([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])
with tf.device('/TPU:0'):
  c = tf.matmul(a, b)
print("c device: ", c.device)
print(c)
strategy = tf.distribute.TPUStrategy(resolver)
@tf.function
def matmul_fn(x, y):
  z = tf.matmul(x, y)
  return z

z = strategy.run(matmul_fn, args=(a, b))
print(z)
@tf.function
def matmul_fn(x, y):
  z = tf.matmul(x, y)
  return z

z = strategy.run(matmul_fn, args=(a, b))
print(z)
def create_model():
  return tf.keras.models.Sequential([
    tf.keras.layers.Dense(30, activation='relu'),
    tf.keras.layers.Dense(10, activation='relu'),
    tf.keras.layers.Dense(3, activation='softmax')])
def get_dataset(batch_size, is_training=True):
  split = 'train' if is_training else 'test'
  dataset, info = tfds.load(name='mnist', split=split, with_info=True,
                            as_supervised=True, try_gcs=True)


  # Only shuffle and repeat the dataset in training. The advantage to have a
  # infinite dataset for training is to avoid the potential last partial batch
  # in each epoch, so users don't need to think about scaling the gradients
  # based on the actual batch size.
  if is_training:
    dataset = dataset.shuffle(10000)
    dataset = dataset.repeat()

  dataset = dataset.batch(batch_size)

  return dataset
with strategy.scope():
  model = create_model()
  model.compile(optimizer='adam',
                loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
                metrics=['sparse_categorical_accuracy'])

batch_size = 200
steps_per_epoch = 60000 // batch_size
validation_steps = 10000 // batch_size

train_dataset = get_dataset(batch_size, is_training=True)
test_dataset = get_dataset(batch_size, is_training=False)

model.fit(train_dataset,
          epochs=5,
          steps_per_epoch=steps_per_epoch,
          validation_data=test_dataset, 
          validation_steps=validation_steps)

您提到的代码段失败,出现以下错误消息:

  (0) Invalid argument: {{function_node __inference_train_function_10150}} Compilation failure: Incompatible shapes: [25,1] vs. [25,28,28]
这意味着数据和输入之间存在形状不匹配


这里最简单的解决方案是参考(在MNIST上也使用了一个使用TFDS的简单模型),找出代码的不同之处。

您的协作链接没有公开我的问题我修复了它。还要把代码放在问题中
  (0) Invalid argument: {{function_node __inference_train_function_10150}} Compilation failure: Incompatible shapes: [25,1] vs. [25,28,28]