Keras / Tensorflow 不兼容的形状

Keras / Tensorflow incompatible shape

我是 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 个时期后,这给了我 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,).

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

非常感谢任何帮助!

尝试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)