将数据喂进模型的时候,发现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