将数据喂进模型的时候,发现fit模型的时候,总是出现维度不对的错误,当打印喂进模型数据的明细时,发现如某一条记录,ty为array([0.96276813, 0.46342801]) ,shape=[2,1]和ty这个特征为2维不符,
({
'ty': <tf.Tensor: shape=(2,), dtype=float64, numpy=array([0.96276813, 0.46342801])>, 'a_label': <tf.Tensor: shape=(1,), dtype=float32, numpy=array([0.], dtype=float32)>, 'b_label': <tf.Tensor: shape=(1,), dtype=float32, numpy=array([0.], dtype=float32)>}, {
'a': <tf.Tensor: shape=(1,), dtype=float32, numpy=array([0.], dtype=float32)>, 'b': <tf.Tensor: shape=(1,), dtype=float32, numpy=array([0.], dtype=float32)>, 'loss': <tf.Tensor: shape=(1,), dtype=float32, numpy=array([0.], dtype=float32)>})
因而会报Input 0 of layer dense_132 is incompatible with the layer: expected axis -1 of input shape to have value 2 but received input with shape [2, 1]的错误
解决方案如下
- 1 加batch解决
构造一个简单的模型
def gen_model():
input1 = tf.keras.layers.Input(shape=(2,)

在使用TensorFlow 2.3及以上版本的Keras训练模型时,遇到输入维度不匹配的错误。问题在于数据的一个特征具有形状[2,1],而模型期望的输入轴-1的维度为2。解决方案包括:1) 使用batch处理数据,通过添加`tf.data.Dataset.batch`来调整数据形状;2) 将TensorFlow版本降至2.1或更低,以避免该问题。"
111235812,10292961,指令级并行编译技术:循环展开与流水线调度,"['并行编译技术', '计算机体系结构', 'RISC-V', '编译器优化']
最低0.47元/天 解锁文章
2309

被折叠的 条评论
为什么被折叠?



