【问题标题】:TensorFlow - ValueError: Shapes (3, 1) and (4, 3) are incompatibleTensorFlow - ValueError:形状 (3, 1) 和 (4, 3) 不兼容
【发布时间】:2020-08-20 08:48:56
【问题描述】:

我对 DL 完全陌生,当我适合我的模型时,我遇到了这个错误

ValueError: Shapes (3, 1) and (4, 3) are incompatible

数据集:

Features: [0.22222222 0.625      0.06779661 0.04166667], Target: [1 0 0]
Features: [0.16666667 0.41666667 0.06779661 0.04166667], Target: [1 0 0]
Features: [0.11111111 0.5        0.05084746 0.04166667], Target: [1 0 0]
Features: [0.08333333 0.45833333 0.08474576 0.04166667], Target: [1 0 0]
Features: [0.19444444 0.66666667 0.06779661 0.04166667], Target: [1 0 0]

型号:

def build_fc_model():
  fc_model = tf.keras.Sequential([
      tf.keras.layers.Dense(4, activation=tf.nn.softmax),
      tf.keras.layers.Dense(4, activation=tf.nn.softmax),
      tf.keras.layers.Dense(3, activation=tf.nn.softmax),
  ])
  return fc_model```

model.fit 出错

model = build_fc_model()
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=1e-1), loss='categorical_crossentropy', metrics=['accuracy'])

BATCH_SIZE = 10
EPOCHS = 5

model.fit(dataset, batch_size=BATCH_SIZE, epochs=EPOCHS)

感谢您的帮助

【问题讨论】:

    标签: python tensorflow model


    【解决方案1】:

    在您的代码中,build_fc_model 中缺少 InputLayer,因此请检查一下:

    import tensorflow as tf
    import numpy as np
    
    def build_fc_model():
      fc_model = tf.keras.Sequential([
          tf.keras.layers.InputLayer((4,)),
          tf.keras.layers.Dense(4, activation=tf.nn.softmax),
          tf.keras.layers.Dense(4, activation=tf.nn.softmax),
          tf.keras.layers.Dense(3, activation=tf.nn.softmax),
      ])
      return fc_model
    
    
    data = np.array([[0.22222222, 0.625,      0.06779661, 0.04166667], 
                     [0.16666667, 0.41666667, 0.06779661, 0.04166667],
                     [0.11111111, 0.5 ,       0.05084746, 0.04166667], 
                     [0.08333333, 0.45833333, 0.08474576, 0.04166667], 
                     [0.19444444, 0.66666667, 0.06779661, 0.04166667]])
    
    target = np.array([[1, 0 ,0],
                       [1, 0 ,0],
                       [1, 0 ,0],
                       [1, 0 ,0],
                       [1, 0 ,0]])
    
    model = build_fc_model()
    model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=1e-1), loss='categorical_crossentropy', metrics=['accuracy'])
    
    BATCH_SIZE = 1
    EPOCHS = 5
    
    model.fit(data, target, batch_size=BATCH_SIZE, epochs=EPOCHS)
    

    输出:

    Epoch 1/5
    5/5 [==============================] - 0s 991us/step - loss: 0.8198 - accuracy: 0.6000
    Epoch 2/5
    5/5 [==============================] - 0s 603us/step - loss: 0.1590 - accuracy: 1.0000
    Epoch 3/5
    5/5 [==============================] - 0s 593us/step - loss: 0.0372 - accuracy: 1.0000
    Epoch 4/5
    5/5 [==============================] - 0s 597us/step - loss: 0.0131 - accuracy: 1.0000
    Epoch 5/5
    5/5 [==============================] - 0s 680us/step - loss: 0.0064 - accuracy: 1.0000
    

    【讨论】:

    • 问题出在我的数据集中,我不知道为什么......我用 pandas 创建了一个 datafrane,然后转换为 tf 数据集
    • 如果问题不可重现,或者是由拼写错误引起的,则应完全删除该问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-07
    • 2021-07-27
    • 1970-01-01
    • 2021-12-05
    • 2021-05-12
    相关资源
    最近更新 更多