Tensorflow報錯總結

輸入不對應

報錯內容:

WARNING:tensorflow:Model was constructed with shape (None, 79) for input Tensor("genres:0", shape=(None, 79), dtype=float32), but it was called on an input with incompatible shape (128, 5).

定義模型的輸入和訓練時候傳入的input不對應,比如:

input1 = Input(shape=(3,))
input2 = Input(shape=(3,))
model = Model(inputs=[input1, input2], output=...)
model.compile(...)
model.fit([X], y). # 定義了兩個輸入,訓練時只傳了一個

如果是一個稀疏的矩陣可以用tf.SparseTensor表示,定義稀疏矩陣三個參數:

  • indices 用來設置有值的位置,也就是下標
  • values 用來指定有值位置的值
  • dense_shape 指定 矩陣的形狀

例如定義一個2行32列的矩陣,只有第1行的第二個位置,和2行的第一個位置有值,值分別爲1, 2:

sparse_tensor = tf.SparseTensor(indices=[[0, 1], [1, 0]], values=[1, 2], dense_shape=[2, 32])
print(tf.sparse.to_dense(sparse_tensor)) # 轉換爲稠密矩陣

輸出:

tf.Tensor(
[[0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
 [2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]], shape=(2, 32), dtype=int32)

同樣還有方法tf.sparse.from_dense把稠密矩陣轉換爲稀疏矩陣.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章