作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要使用 Tensorflow/Keras 创建一个自定义训练循环(因为我想要拥有多个优化器并告诉每个优化器应该作用于哪些权重)。
虽然this tutorial和 that one too对于这个问题非常清楚,他们错过了一个非常重要的一点:我如何预测训练阶段以及如何预测验证阶段?
假设我的模型有 Dropout
层或 BatchNormalization
层。无论是在训练还是验证中,它们的工作方式肯定是完全不同的。
我如何改编这些教程?这是一个虚拟示例(可能包含一两段伪代码):
# Iterate over epochs.
for epoch in range(3):
# Iterate over the batches of the dataset.
for step, (x_batch_train, y_batch_train) in enumerate(train_dataset):
with tf.GradientTape() as tape:
#model with two outputs
#IMPORTANT: must be in training phase (use dropouts, calculate batch statistics)
logits1, logits2 = model(x_batch_train) #must be "training"
loss_value1 = loss_fn1(y_batch_train[0], logits1)
loss_value2 = loss_fn2(y_batch_train[1], logits2)
grads1 = tape.gradient(loss_value1, model.trainable_weights[selection1])
grads2 = tape.gradient(loss_value2, model.trainable_weights[selection2])
optimizer1.apply_gradients(zip(grads1, model.trainable_weights[selection1]))
optimizer2.apply_gradients(zip(grads2, model.trainable_weights[selection2]))
# Run a validation loop at the end of each epoch.
for x_batch_val, y_batch_val in val_dataset:
##Important: must be validation phase
#dropouts are off: calculate all neurons and divide value
#batch norms use previously calculated statistics
val_logits1, val_logits2 = model(x_batch_val)
#.... do the evaluations
最佳答案
我认为你可以在调用 tf.keras.Model
时传递一个 training
参数。 ,并且它将被传递到各层:
# On training
logits1, logits2 = model(x_batch_train, training=True)
# On evaluation
val_logits1, val_logits2 = model(x_batch_val, training=False)
关于python - 分阶段的 Tensorflow 自定义训练,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58012094/
有没有办法使用 jOOQ 分阶段构建 SQL?像这样的东西: DSLContext create = DSL.using(conn, SQLDialect.MYSQL);
我想要一个执行所有@Before 方法、然后是所有@Test 方法、然后是所有@After 方法的 junit 运行程序。 这就是我的系统测试的工作方式。运行@Before 方法,以设置测试数据和场景
我正在寻找讨论编写 monad 的良好实践的资源。我最紧迫的问题是我正在编写一个系统,该系统在不同的状态类型上使用一系列状态单子(monad),似乎处理这种情况的最好方法就是创建一个大的产品类型(也许
我是一名优秀的程序员,十分优秀!