這個程式正確嗎?
我的意圖是在連接后添加一個 dropout 層,但為此我需要將 concat 層的輸出調整為適當的
uj5u.com熱心網友回復:
在 tensorflow 2.7.0 中,您可以只使用 keepdims=True 作為 GlobalAveragePooling2D 層的引數,而不是顯式添加新維度。
例子:
def TestModel():
# specify the input shape
in_1 = tf.keras.layers.Input(shape = (256,256,3))
in_2 = tf.keras.layers.Input(shape = (256,256,3))
x1 = tf.keras.layers.Conv2D(64, (3,3))(in_1)
x1 = tf.keras.layers.LeakyReLU()(x1)
x1 = tf.keras.layers.GlobalAveragePooling2D(keepdims = True)(x1)
x2 = tf.keras.layers.Conv2D(64, (3,3))(in_2)
x2 = tf.keras.layers.LeakyReLU()(x2)
x2 = tf.keras.layers.GlobalAveragePooling2D(keepdims = True)(x2)
x = tf.keras.layers.concatenate([x1,x2])
x = tf.keras.layers.SpatialDropout2D(0.2)(x)
x = tf.keras.layers.Dense(1000)(x)
# create the model
model = tf.keras.Model(inputs=(in_1,in_2), outputs=x)
return model
#Testcode
model = TestModel()
model.summary()
tf.keras.utils.plot_model(model, show_shapes=True, expand_nested=False, show_dtype=True, to_file="model.png")

如果你最后需要擠壓它,你仍然可以做到。
uj5u.com熱心網友回復:
如果您打算使用該SpatialDropout1D層,它必須接收 3D 張量(batch_size, time_steps, features),因此在將張量提供給 dropout 層之前添加一個額外的維度是一個完全合法的選項。但請注意,在您的情況下,您可以同時使用SpatialDropout1Dor Dropout:
import tensorflow as tf
samples = 2
timesteps = 1
features = 5
x = tf.random.normal((samples, timesteps, features))
s = tf.keras.layers.SpatialDropout1D(0.5)
d = tf.keras.layers.Dropout(0.5)
print(s(x, training=True))
print(d(x, training=True))
tf.Tensor(
[[[-0.5976591 1.481788 0. 0. 0. ]]
[[ 0. -4.6607018 -0. 0.7036132 0. ]]], shape=(2, 1, 5), dtype=float32)
tf.Tensor(
[[[-0.5976591 1.481788 0.5662646 2.8400114 0.9111476]]
[[ 0. -0. -0. 0.7036132 0. ]]], shape=(2, 1, 5), dtype=float32)
我認為SpatialDropout1D層后層最合適CNN。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/416382.html
標籤:
