【问题标题】:Keras / Tensorflow incompatible shapeKeras / Tensorflow 不兼容的形状
【发布时间】:2021-12-03 10:10:16
【问题描述】:

我对 tensorflow / keras 很陌生,我找不到解决这个问题的方法。我有一个约 4000 个 20 维向量的训练数据集,每个向量都描述一个文档。在以后的状态下,我也有相同的文档向量。我想预测文档向量将如何从初始状态结束。我使用余弦相似度比较了状态 0 的文档向量和它们的最终状态,得到了大约 0.5。目标是用一个简单的模型来改进它。目前我正在做:

model = Sequential()
model.add(Dense(20, activation='relu', input_dim=20))
model.compile(optimizer='adam', loss='cosine_similarity', metrics [tf.keras.metrics.CosineSimilarity(axis=1)])
model.summary()
history = model.fit(input_train, y_train,
                epochs=30,
                batch_size=16,
                validation_data=(input_test,y_test),
                callbacks=[tbCallBack]
               )

在 30 个 epoch 之后,这给了我 0.66 的验证余弦相似度,所以我的猜测是这实际上确实提高了我的初始余弦相似度并产生了至少某种附加值。

然后我想看看这些预测是否有意义:

lol = np.asarray([0.0125064 , 0.01250269, 0.01250133, 0.01250481, 0.01250508,
   0.0125009 , 0.0125009 , 0.01250437, 0.01250131, 0.01250181,
   0.01250403, 0.0125038 , 0.01250372, 0.01250246, 0.01250183,
   0.01250226, 0.01250294, 0.76244247, 0.01250485, 0.01250205])
model.predict([lol])
#model.predict(lol)

两个预测版本都给我以下错误:

WARNING:tensorflow:Model was constructed with shape (None, 20) for input KerasTensor(type_spec=TensorSpec(shape=(None, 20), dtype=tf.float32, name='dense_69_input'), name='dense_69_input', description="created by layer 'dense_69_input'"), but it was called on an input with incompatible shape (None,).

有人知道如何解决这个问题吗?另外,如果有人熟悉这种目标,这是正确的方法吗?有什么我可以做的不同的事情吗?

非常感谢任何帮助!

【问题讨论】:

    标签: python numpy tensorflow keras deep-learning


    【解决方案1】:

    尝试np.expand_dims 将批处理维度添加到您的数组中:

    import tensorflow as tf
    import numpy as np
    
    model = tf.keras.Sequential()
    model.add(tf.keras.layers.Dense(20, activation='relu', input_dim=20))
    model.compile(optimizer='adam', loss='cosine_similarity', metrics= [tf.keras.metrics.CosineSimilarity(axis=1)])
    model.summary()
    input_train = tf.random.normal((5, 20))
    y_train = tf.random.normal((5, 20))
    history = model.fit(input_train, y_train,
                    epochs=1,
                    batch_size=2)
    
    lol = np.asarray([0.0125064 , 0.01250269, 0.01250133, 0.01250481, 0.01250508,
       0.0125009 , 0.0125009 , 0.01250437, 0.01250131, 0.01250181,
       0.01250403, 0.0125038 , 0.01250372, 0.01250246, 0.01250183,
       0.01250226, 0.01250294, 0.76244247, 0.01250485, 0.01250205])
    lol = np.expand_dims(lol, axis=0)
    model.predict(lol)
    
    array([[0.0727988 , 0.        , 0.3008919 , 0.00460427, 0.        ,
            0.01472487, 0.31665963, 0.11831823, 0.        , 0.05261957,
            0.        , 0.        , 0.        , 0.        , 0.13595472,
            0.07765757, 0.09340346, 0.        , 0.        , 0.        ]],
          dtype=float32)
    

    【讨论】:

      猜你喜欢
      • 2020-12-11
      • 1970-01-01
      • 2018-01-10
      • 2020-10-16
      • 2019-10-20
      • 2018-10-21
      • 2021-05-12
      • 2021-05-29
      • 1970-01-01
      相关资源
      最近更新 更多