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把稠密矩阵转换为稀疏矩阵.

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