值错误:预期输入形状的轴 -1 的值为 444,但收到的输入形状为(无,708)

问题描述 投票:0回答:1

我正在尝试训练用于信用卡欺诈检测的 FNN 模型。我的分类特征是商家、类别、城市和州。我还使用 StandardScaler 标准化了我的数值特征。我首先对分类特征使用标签编码,将它们转换为整数值。然后我想为神经网络模型的分类特征创建嵌入。当我尝试运行代码时,出现此错误:

ValueError:调用Functional.call()时遇到异常。

“dense_6”层的输入 0 与该层不兼容:输入形状的预期轴 -1 的值为 444,但收到的输入形状为(无,708)

这是我的代码:

# Split data into training, validation, and test sets (70%, 15%, 15%)
from sklearn.model_selection import train_test_split
X = df.drop(columns=['is_fraud'])
y = df['is_fraud']
X_train, X_temp, y_train, y_temp = train_test_split(X, y, test_size=0.3, random_state=42)  # 70% train, 30% temp
X_val, X_test, y_val, y_test = train_test_split(X_temp, y_temp, test_size=0.5, random_state=42)  # Split temp into 15% val and 15% test

from sklearn.preprocessing import LabelEncoder, StandardScaler
# Label Encoding
categorical_columns = ['merchant', 'category', 'city', 'state']
label_encoded = {}
for col in categorical_columns:
    le = LabelEncoder()
    X_train[col] = le.fit_transform(X_train[col])
    X_val[col] = le.transform(X_val[col])
    X_test[col] = le.transform(X_test[col])
    label_encoded[col] = le

# Normalization
scaler = StandardScaler()
numerical_features = ['cc_num', 'amt', 'lat', 'long', 'merch_lat', 'merch_long', 'trans_hour', 'trans_day', 'trans_year', 'amt_ratio', 'daily_transaction_count', 'user_location_distance']
X_train_numerical = scaler.fit_transform(X_train[numerical_features])
X_val_numerical = scaler.transform(X_val[numerical_features])
X_test_numerical = scaler.transform(X_test[numerical_features])

from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, Embedding, Dense, Flatten, Concatenate
from sklearn.metrics import precision_score, recall_score, roc_auc_score, roc_curve
import matplotlib.pyplot as plt

# Define Embedding Dimensions
embedding_dim = {
    'merchant': 200,
    'category': 7,
    'city': 200,
    'state': 25
}

# Create embeddings and flatten to 1D vectors
merchant_input = Input(shape=(1,), name='merchant')
merchant_embedding = Embedding(input_dim=len(label_encoded['merchant'].classes_), output_dim=embedding_dim['merchant'])(merchant_input)
merchant_embedding = Flatten()(merchant_embedding)

category_input = Input(shape=(1,), name='category')
category_embedding = Embedding(input_dim=len(label_encoded['category'].classes_), output_dim=embedding_dim['category'])(category_input)
category_embedding = Flatten()(category_embedding)

city_input = Input(shape=(1,), name='city')
city_embedding = Embedding(input_dim=len(label_encoded['city'].classes_), output_dim=embedding_dim['city'])(city_input)
city_embedding = Flatten()(city_embedding)

state_input = Input(shape=(1,), name='state')
state_embedding = Embedding(input_dim=len(label_encoded['state'].classes_), output_dim=embedding_dim['state'])(state_input)
state_embedding = Flatten()(state_embedding)

numerical_input = Input(shape=(len(numerical_features),), name='numerical_features')

# Concatenate embeddings and numerical inputs
concat_embed = Concatenate()([merchant_embedding, category_embedding, city_embedding, state_embedding])

concat_layer = Concatenate()([concat_embed, numerical_input])

# FNN Model
x = Dense(32, activation='relu')(concat_layer)
x = Dense(32, activation='relu')(x)
output = Dense(1, activation='sigmoid')(x)

fnn_model = Model(inputs=[merchant_input, category_input, city_input, state_input, numerical_input], outputs=output)
fnn_model.compile(optimizer=tf.keras.optimizers.Adam(0.001), loss='binary_crossentropy', metrics=['accuracy'])

# Prepare Training and Validation Data
train_inputs = {
    'merchant': X_train['merchant'],
    'category': X_train['category'],
    'city': X_train['city'],
    'state': X_train['state'],
    'numerical_features': X_train_numerical
}

val_inputs = {
    'merchant': X_val['merchant'],
    'category': X_val['category'],
    'city': X_val['city'],
    'state': X_val['state'],
    'numerical_features': X_val_numerical
}

# Train model
history = fnn_model.fit(train_inputs, y_train, epochs=100, batch_size=32, validation_data=(val_inputs, y_val), verbose=0)
python tensorflow machine-learning keras deep-learning
1个回答
0
投票

该错误是由输入形状不匹配引起的。连接嵌入和数字特征后的特征总数应为 444(而不是 708)。在将输入传递到第一个密集层之前,确保嵌入的大小正确并打印输入的形状非常重要。可能需要在密集层之前展平串联输入。

为了解决这个问题,用户必须确保嵌入和数字特征的特征总数一致。检查输入形状的一种方法是在将连接输入传递到密集层之前打印连接输入的形状:

print(concat_layer.shape)

此外,用户应该在将连接的输入传递到密集层之前显式地展平它:

concat_flattened = Flatten()(concat_layer)
x = Dense(32, activation='relu')(concat_flattened)
x = Dense(32, activation='relu')(x)
© www.soinside.com 2019 - 2024. All rights reserved.