keras 找到与输出断开连接的未访问输入Tensor

w41d8nur  于 2022-11-13  发布在  其他
关注(0)|答案(1)|浏览(158)

我试图连接由声学数据和辅助变量组成的输入。该模型基于变压器架构。我的问题是,我一直在获得输出:
找到与输出断开连接的未访问输入Tensor
这是我的代码:

def transformer_encoder(inputs, head_size, num_heads, ff_dim, dropout=0):
    # Normalization and Attention
    x = layers.LayerNormalization(epsilon=1e-6)(inputs)
    x = layers.MultiHeadAttention(
        key_dim=head_size, num_heads=num_heads, dropout=dropout
    )(x, x)
    x = layers.Dropout(dropout)(x)
    res = x + inputs

    # Feed Forward Part
    x = layers.LayerNormalization(epsilon=1e-6)(res)
    x = layers.Conv1D(filters=ff_dim, kernel_size=1, activation="relu")(x)
    x = layers.Dropout(dropout)(x)
    x = layers.Conv1D(filters=inputs.shape[-1], kernel_size=1)(x)
    return x + res

def build_model(
    input_shape_acoustic,
    input_shape_fotography,
    #input_weight,
    head_size,
    num_heads,
    ff_dim,
    num_transformer_blocks,
    num_vison_transformer_blocks,
    mlp_units,
    dropout=0,
    mlp_dropout=0,
):
    input_acoustic = keras.Input(shape=input_shape_acoustic)
    input_fotography = keras.Input(shape=input_shape_fotography)
    #input_weight = keras.Input(shape=input_weight)
    
    #building acoustic model
    x = input_acoustic
    print(x)
    for _ in range(num_transformer_blocks):
        x = transformer_encoder(x, head_size, num_heads, ff_dim, dropout)
    #input_acoustic = layers.Concatenate()([input_acoustic, input_weight])
    input_acoustic = layers.GlobalAveragePooling1D(data_format="channels_first")(x)
    
    
    #buidling vision transformer
    patches = Patches(patch_size)(input_fotography)
    encoded_patches = PatchEncoder(num_patches, projection_dim)(patches)

    for _ in range(num_vison_transformer_blocks):
        # Layer normalization 1.
        x1 = layers.LayerNormalization(epsilon=1e-6)(encoded_patches)
        # Create a multi-head attention layer.
        attention_output = layers.MultiHeadAttention(
            num_heads=num_heads, key_dim=projection_dim, dropout=0.1
        )(x1, x1)
        # Skip connection 1.
        x2 = layers.Add()([attention_output, encoded_patches])
        # Layer normalization 2.
        x3 = layers.LayerNormalization(epsilon=1e-6)(x2)
        # MLP.
        x3 = mlp(x3, hidden_units=transformer_units, dropout_rate=0.1)
        # Skip connection 2.
        encoded_patches = layers.Add()([x3, x2])

    representation = layers.LayerNormalization(epsilon=1e-6)(encoded_patches)
    representation = layers.Flatten()(representation)
    representation = layers.Dropout(0.5)(representation)
    
    #fusiing information
    x = layers.Concatenate()([input_acoustic, representation])
    print(x.shape)
    for dim in mlp_units:
        x = layers.Dense(dim, activation="relu")(x)
        x = layers.Dropout(mlp_dropout)(x)
    outputs = layers.Dense(n_classes, activation="softmax")(x)
    
    return keras.Model(inputs=[input_acoustic, input_fotography], outputs=outputs)

import tensorflow as tf
from tensorflow import keras

input_shape = np.array(a).reshape(-1, 132096,1).shape[1:]
patch_size = 6
num_patches = (100 // patch_size) ** 2
projection_dim=64
transformer_units = [
    projection_dim * 2,
    projection_dim,
]  # Size of the transformer layers
transformer_layers = 4
mlp_head_units = [2048, 1024]
n_classes = 3

model = build_model(
    np.array(a).shape[1:],
    np.array(f).shape[1:],
    #np.array(w).shape[0],
    head_size=256,
    num_heads=4,
    ff_dim=4,
    num_transformer_blocks=1,
    num_vison_transformer_blocks=1,
    mlp_units=[128],
    mlp_dropout=0.4,
    dropout=0.25,
)

model.compile(
    loss="sparse_categorical_crossentropy",
    optimizer=keras.optimizers.Adam(learning_rate=1e-5),
    metrics=["sparse_categorical_accuracy"],
)

#callbacks = [keras.callbacks.EarlyStopping(patience=10, restore_best_weights=True)]

history = model.fit(
    [np.array(a), np.array(f)],
    np.array(s.reshape(-1,1)),
    validation_split=0.3,
    epochs=20,
    batch_size=64,
    #callbacks=callbacks,
)

根据论坛上enter link description here的帖子,我理解了错误信息,但是,我通过layers.Input()``` and I do not see that I overwrite my variable for input_weight "'"'正确地初始化了我的变量。有人知道是什么导致了这个问题吗?

vybvopom

vybvopom1#

您正在覆盖input_acoustic

input_acoustic = layers.GlobalAveragePooling1D(data_format="channels_first")(x)

因此,您没有向Model提供输入层。它无法更新GlobalAveragePooling1D之前的层的权重。
将变量名称更改为其他名称,例如

acoustic_global_pool = layers.GlobalAveragePooling1D(data_format="channels_first")(x)

不要覆盖input_acoustic,因为它是您的输入层,您需要将其作为Model的输入。

相关问题