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把稠密矩陣轉換為稀疏矩陣.

loss值巨大
表現為loss值非常大,或者loss不大,但是val_loss非常大。原因是數值特征沒有統一量綱就給了全鏈接層,
而神經元對數值大小敏感導致評估結果無法很大。
解決辦法:數值特征要統一量綱。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM