尝试在 Jupiter 和 Google Colab 上训练模型。尝试实现梯度计算时出错
Trying to train a model on Jupiter and Google Colab. getting errors when trying to implement gradient calculations
我得到的错误是:
ZipFile requires mode 'r', 'w', 'x', or 'a'
或
ZipFile.__init__() got multiple values for argument 'mode'
或
类型错误:'ZipFile' 对象不可调用
我的代码:
@tf.function
def train_step(batch):
# Record all of our operations
with tf.GradientTape() as tape:
# Get anchor and positive/negative image
X = batch[:2]
# Get label
y = batch[2]
# Forward pass
yhat = siamese_model(X, training=True)
# Calculate loss
loss = binary_cross_loss(y, yhat)
print(loss)
# Calculate gradients
grad = tape.gradient(loss, [siamese_model.trainable_variables])
# Calculate updated weights and apply to siamese model
opt.apply_gradients(zipfile.ZipFile(grad, [siamese_model.trainable_variables], mode
= "w"))
# Return loss
return loss
def train(data, EPOCHS):
# Loop through epochs
for epoch in range(1, EPOCHS+1):
print('\n Epoch {}/{}'.format(epoch, EPOCHS))
progbar = tf.keras.utils.Progbar(len(data))
# Creating a metric object
r = Recall()
p = Precision()
# Loop through each batch
for idx, batch in enumerate(data):
# Run train step here
loss = train_step(batch)
yhat = siamese_model.predict(batch[:2])
r.update_state(batch[2], yhat)
p.update_state(batch[2], yhat)
progbar.update(idx+1)
print(loss.numpy(), r.result().numpy(), p.result().numpy())
# Save checkpoints
if epoch % 10 == 0:
checkpoint.save(file_prefix=checkpoint_prefix)
EPOCHS = 50
train(train_data, EPOCHS)
错误表明问题出在 train 调用 train_step(batch) 函数时。
我也尝试过不使用 mode = "w"
,当括号打开时仅使用 'zip' 而没有 siamese_model.trainable_variables
的括号,我已经尝试过
import zipfile
或 import zip file from ZipFile
但没有区别。
我得到的错误是:
ZipFile requires mode 'r', 'w', 'x', or 'a'
或
ZipFile.__init__() got multiple values for argument 'mode'
或
类型错误:'ZipFile' 对象不可调用
我的代码:
@tf.function
def train_step(batch):
# Record all of our operations
with tf.GradientTape() as tape:
# Get anchor and positive/negative image
X = batch[:2]
# Get label
y = batch[2]
# Forward pass
yhat = siamese_model(X, training=True)
# Calculate loss
loss = binary_cross_loss(y, yhat)
print(loss)
# Calculate gradients
grad = tape.gradient(loss, [siamese_model.trainable_variables])
# Calculate updated weights and apply to siamese model
opt.apply_gradients(zipfile.ZipFile(grad, [siamese_model.trainable_variables], mode
= "w"))
# Return loss
return loss
def train(data, EPOCHS):
# Loop through epochs
for epoch in range(1, EPOCHS+1):
print('\n Epoch {}/{}'.format(epoch, EPOCHS))
progbar = tf.keras.utils.Progbar(len(data))
# Creating a metric object
r = Recall()
p = Precision()
# Loop through each batch
for idx, batch in enumerate(data):
# Run train step here
loss = train_step(batch)
yhat = siamese_model.predict(batch[:2])
r.update_state(batch[2], yhat)
p.update_state(batch[2], yhat)
progbar.update(idx+1)
print(loss.numpy(), r.result().numpy(), p.result().numpy())
# Save checkpoints
if epoch % 10 == 0:
checkpoint.save(file_prefix=checkpoint_prefix)
EPOCHS = 50
train(train_data, EPOCHS)
错误表明问题出在 train 调用 train_step(batch) 函数时。
我也尝试过不使用 mode = "w"
,当括号打开时仅使用 'zip' 而没有 siamese_model.trainable_variables
的括号,我已经尝试过
import zipfile
或 import zip file from ZipFile
但没有区别。