直接在tensorflow中的dropout层:如何训练?
Dropout layer directly in tensorflow: how to train?
在 Keras 中创建模型后,我想获取梯度并使用 tf.train.AdamOptimizer class 直接在 Tensorflow 中应用它们。但是,由于我使用的是 Dropout 层,我不知道如何告诉模型它是否处于训练模式。不接受 training 关键字。这是代码:
net_input = Input(shape=(1,))
net_1 = Dense(50)
net_2 = ReLU()
net_3 = Dropout(0.5)
net = Model(net_input, net_3(net_2(net_1(net_input))))
#mycost = ...
optimizer = tf.train.AdamOptimizer()
gradients = optimizer.compute_gradients(mycost, var_list=[net.trainable_weights])
# perform some operations on the gradients
# gradients = ...
trainstep = optimizer.apply_gradients(gradients)
即使有 dropout rate=1
,我在使用和不使用 dropout 层时得到相同的行为。如何解决?
Keras 层继承自 tf.keras.layers.Layer class。 Keras API 在内部用 model.fit
处理这个问题。如果 Keras Dropout 与纯 TensorFlow 训练循环一起使用,它在其调用函数中支持训练参数。
所以你可以用
控制它
dropout = tf.keras.layers.Dropout(rate, noise_shape, seed)(prev_layer, training=is_training)
来自官方 TF 文档
Note: - The following optional keyword arguments are reserved for
specific uses: * training: Boolean scalar tensor of Python boolean
indicating whether the call is meant for training or inference. *
mask: Boolean input mask. - If the layer's call method takes a mask
argument (as some Keras layers do), its default value will be set to
the mask generated for inputs by the previous layer (if input did come
from a layer that generated a corresponding mask, i.e. if it came from
a Keras layer with masking support.
https://www.tensorflow.org/api_docs/python/tf/keras/layers/Dropout#call
正如@Sharky 已经说过的,您可以在调用 Dropout
class 的 call()
方法时使用 training
参数。但是,如果您想在张量流图模式下进行训练,则需要传递一个占位符并在训练期间为其提供布尔值。这是适用于您的情况的拟合高斯斑点的示例:
import tensorflow as tf
import numpy as np
from sklearn.datasets import make_blobs
from sklearn.model_selection import train_test_split
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Dropout
from tensorflow.keras.layers import ReLU
from tensorflow.keras.layers import Input
from tensorflow.keras import Model
x_train, y_train = make_blobs(n_samples=10,
n_features=2,
centers=[[1, 1], [-1, -1]],
cluster_std=1)
x_train, x_test, y_train, y_test = train_test_split(
x_train, y_train, test_size=0.2)
# `istrain` indicates whether it is inference or training
istrain = tf.placeholder(tf.bool, shape=())
y = tf.placeholder(tf.int32, shape=(None))
net_input = Input(shape=(2,))
net_1 = Dense(2)
net_2 = Dense(2)
net_3 = Dropout(0.5)
net = Model(net_input, net_3(net_2(net_1(net_input)), training=istrain))
xentropy = tf.nn.sparse_softmax_cross_entropy_with_logits(
labels=y, logits=net.output)
loss_fn = tf.reduce_mean(xentropy)
optimizer = tf.train.AdamOptimizer(0.01)
grads_and_vars = optimizer.compute_gradients(loss_fn,
var_list=[net.trainable_variables])
trainstep = optimizer.apply_gradients(grads_and_vars)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
l1 = loss_fn.eval({net_input:x_train,
y:y_train,
istrain:True}) # apply dropout
print(l1) # 1.6264652
l2 = loss_fn.eval({net_input:x_train,
y:y_train,
istrain:False}) # no dropout
print(l2) # 1.5676715
sess.run(trainstep, feed_dict={net_input:x_train,
y:y_train,
istrain:True}) # train with dropout
在 Keras 中创建模型后,我想获取梯度并使用 tf.train.AdamOptimizer class 直接在 Tensorflow 中应用它们。但是,由于我使用的是 Dropout 层,我不知道如何告诉模型它是否处于训练模式。不接受 training 关键字。这是代码:
net_input = Input(shape=(1,))
net_1 = Dense(50)
net_2 = ReLU()
net_3 = Dropout(0.5)
net = Model(net_input, net_3(net_2(net_1(net_input))))
#mycost = ...
optimizer = tf.train.AdamOptimizer()
gradients = optimizer.compute_gradients(mycost, var_list=[net.trainable_weights])
# perform some operations on the gradients
# gradients = ...
trainstep = optimizer.apply_gradients(gradients)
即使有 dropout rate=1
,我在使用和不使用 dropout 层时得到相同的行为。如何解决?
Keras 层继承自 tf.keras.layers.Layer class。 Keras API 在内部用 model.fit
处理这个问题。如果 Keras Dropout 与纯 TensorFlow 训练循环一起使用,它在其调用函数中支持训练参数。
所以你可以用
控制它dropout = tf.keras.layers.Dropout(rate, noise_shape, seed)(prev_layer, training=is_training)
来自官方 TF 文档
Note: - The following optional keyword arguments are reserved for specific uses: * training: Boolean scalar tensor of Python boolean indicating whether the call is meant for training or inference. * mask: Boolean input mask. - If the layer's call method takes a mask argument (as some Keras layers do), its default value will be set to the mask generated for inputs by the previous layer (if input did come from a layer that generated a corresponding mask, i.e. if it came from a Keras layer with masking support. https://www.tensorflow.org/api_docs/python/tf/keras/layers/Dropout#call
正如@Sharky 已经说过的,您可以在调用 Dropout
class 的 call()
方法时使用 training
参数。但是,如果您想在张量流图模式下进行训练,则需要传递一个占位符并在训练期间为其提供布尔值。这是适用于您的情况的拟合高斯斑点的示例:
import tensorflow as tf
import numpy as np
from sklearn.datasets import make_blobs
from sklearn.model_selection import train_test_split
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Dropout
from tensorflow.keras.layers import ReLU
from tensorflow.keras.layers import Input
from tensorflow.keras import Model
x_train, y_train = make_blobs(n_samples=10,
n_features=2,
centers=[[1, 1], [-1, -1]],
cluster_std=1)
x_train, x_test, y_train, y_test = train_test_split(
x_train, y_train, test_size=0.2)
# `istrain` indicates whether it is inference or training
istrain = tf.placeholder(tf.bool, shape=())
y = tf.placeholder(tf.int32, shape=(None))
net_input = Input(shape=(2,))
net_1 = Dense(2)
net_2 = Dense(2)
net_3 = Dropout(0.5)
net = Model(net_input, net_3(net_2(net_1(net_input)), training=istrain))
xentropy = tf.nn.sparse_softmax_cross_entropy_with_logits(
labels=y, logits=net.output)
loss_fn = tf.reduce_mean(xentropy)
optimizer = tf.train.AdamOptimizer(0.01)
grads_and_vars = optimizer.compute_gradients(loss_fn,
var_list=[net.trainable_variables])
trainstep = optimizer.apply_gradients(grads_and_vars)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
l1 = loss_fn.eval({net_input:x_train,
y:y_train,
istrain:True}) # apply dropout
print(l1) # 1.6264652
l2 = loss_fn.eval({net_input:x_train,
y:y_train,
istrain:False}) # no dropout
print(l2) # 1.5676715
sess.run(trainstep, feed_dict={net_input:x_train,
y:y_train,
istrain:True}) # train with dropout