Python 在TPU上训练时如何使用tensorFlow中的交叉熵损失?
我正在尝试在TPU上培训变压器编码器(从这里-):Python 在TPU上训练时如何使用tensorFlow中的交叉熵损失?,python,tensorflow,tpu,Python,Tensorflow,Tpu,我正在尝试在TPU上培训变压器编码器(从这里-): def test(): 列车步骤签名=[ tf.TensorSpec(shape=(None,None),dtype=tf.int64), tf.TensorSpec(shape=(None,None),dtype=tf.int64), ] @tf.功能(输入\签名=训练\步骤\签名) def系列步骤(inp、tar): 使用tf.GradientTape()作为磁带: 预测=转换(inp, 是的, 没有一个 没有一个 (无) 损耗=损耗函数(
def test():
列车步骤签名=[
tf.TensorSpec(shape=(None,None),dtype=tf.int64),
tf.TensorSpec(shape=(None,None),dtype=tf.int64),
]
@tf.功能(输入\签名=训练\步骤\签名)
def系列步骤(inp、tar):
使用tf.GradientTape()作为磁带:
预测=转换(inp,
是的,
没有一个
没有一个
(无)
损耗=损耗函数(tar、预测)#
def test():
train_step_signature = [
tf.TensorSpec(shape=(None, None), dtype=tf.int64),
tf.TensorSpec(shape=(None, None), dtype=tf.int64),
]
@tf.function(input_signature=train_step_signature)
def train_step(inp, tar):
with tf.GradientTape() as tape:
predictions = transf(inp,
True,
None,
None,
None)
loss = loss_function(tar, predictions) # <- error is here
# I use SparseCategoricalCrossentropy()
vocabsize=1000
transf = Transformer(num_layers, d_model, num_heads, dff,
vocabsize, vocabsize,
pe_input=vocabsize,
pe_target=vocabsize,
rate=dropout_rate)
for iter in range(1,75000):
print(iter)
inp=np.random.randint(vocabsize, size=(5,11))
tar=np.random.randint(vocabsize, size=(5,11))
train_step(inp,tar)