tensorflow2.3以上keras model.fit()出现维度错误问题

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

将数据喂进模型的时候,发现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,), dtype=tf
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值