You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用TensorFlow和Keras构建CNN时如何修复Invalid Argument Error

纤维对齐角度预测CNN模型报错问题

背景

我是机器学习新手,尝试通过图像分类测量纤维的对齐角度(精确到度数),训练数据由MATLAB生成,按对齐度数分类存储在training_data文件夹中。

使用代码

import tensorflow as tf
from tensorflow.keras import layers
from tensorflow.keras.preprocessing.image import ImageDataGenerator

# Define the CNN model
model = tf.keras.Sequential([
    layers.Conv2D(32, (3, 3), activation='relu', input_shape=(1024, 768, 1)),
    layers.MaxPooling2D((2, 2)),
    layers.Conv2D(64, (3, 3), activation='relu'),
    layers.MaxPooling2D((2, 2)),
    layers.Conv2D(128, (3, 3), activation='relu'),
    layers.MaxPooling2D((2, 2)),
    layers.Flatten(),
    layers.Dense(64, activation='relu'),
    layers.Dense(2)
])

# Compile the model
model.compile(optimizer='adam',
              loss=tf.keras.losses.MeanSquaredError(),
                            metrics=['accuracy'])

# Set up the data generator for image augmentation
train_datagen = ImageDataGenerator(rescale=1./255,
                                   rotation_range=20,
                                   width_shift_range=0.1,
                                   height_shift_range=0.1,
                                   shear_range=0.2,
                                   zoom_range=0.2,
                                   horizontal_flip=True)

# Load the training data from directory
train_data = train_datagen.flow_from_directory(
    'C:/Users/atlgu/Desktop/CNT Research/ML Image Analysis/train_images/training_data',
    target_size=(1024, 768),
    color_mode='grayscale',
    batch_size=20,
    class_mode='input')

# Train the model
model.fit(train_data, epochs=10)

报错信息

InvalidArgumentError: Graph execution error:

Detected at node 'gradient_tape/mean_squared_error/BroadcastGradientArgs' defined at (most recent call last):

怀疑问题点

  • 图像尺寸
  • 图像格式(颜色通道数)
  • 训练数据目录格式

内容的提问来源于stack exchange,提问作者Brett

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 22:42:24