在 Python 中使用 Tensorflow 构建 CNN 模型时的一个错误

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

我现在有一个大小为(419,128,128)的图片数据,共有419张图片,每张图片为128*128。 我想构建一个 CNN 模型来进行特征选择。 (p.s. 从每张图片中提取128个重要特征) 这是我的代码:

import tensorflow as tf

# shape of input data
input_shape = (None, 128, 128, 1)

# Define the CNN model
model = tf.keras.Sequential([
    tf.keras.layers.Conv2D(128, 3, activation='relu', padding='same', input_shape=input_shape[1:]),
    tf.keras.layers.Conv2D(128, 4, activation='relu', padding='same'),
    tf.keras.layers.Conv2D(128, 5, activation='relu', padding='same'),

    tf.keras.layers.MaxPooling2D(),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(128, activation='relu'),

    # Output layer, since we need to extract 128 important factors, the number of output nodes is 128
    tf.keras.layers.Dense(128, activation='sigmoid')
])

# Compile the model
model.compile(optimizer='adam', loss='mse')

# My input data is sp_pics, shape is (419,128,128)
data_input = np.expand_dims(sp_pics, axis=-1)

# Train the model
model.fit(data, data, epochs=10, batch_size=32)

# Extract the important factors
important_factors = model.predict(data)

现在我收到以下错误消息:

就我而言,错误是由我的输入(419,128,128)和输出(419,128)之间的形状差异引起的。这是真的?我该如何解决?

非常感谢您的帮助!!!

我尝试了不同的批量大小和不同的损失函数。但这没有帮助

python tensorflow deep-learning conv-neural-network
© www.soinside.com 2019 - 2024. All rights reserved.