tensorflow使用掉坑記錄

1.

tf.SparseTensor函數

官方用法:

tf.sparse_to_dense(sparse_indices, output_shape, sparse_values, default_value, name=None)

第一個參數sparse_indices:稀疏矩陣中那些個別元素對應的索引值。

     有三種情況:

     sparse_indices是個數,那麼它只能指定一維矩陣的某一個元素

     sparse_indices是個向量,那麼它可以指定一維矩陣的多個元素

     sparse_indices是個矩陣,那麼它可以指定二維矩陣的多個元素

第二個參數output_shape:輸出的稀疏矩陣的shape

第三個參數sparse_values:個別元素的值。

     分爲兩種情況:

     sparse_values是個數:所有索引指定的位置都用這個數

     sparse_values是個向量:輸出矩陣的某一行向量裏某一行對應的數(所以這裏向量的長度應該和輸出矩陣的行數對應,不然報錯)

第四個參數default_value:未指定元素的默認值,一般如果是稀疏矩陣的話就是0了

坑:默認值得類型爲int型,即value不能爲float型,否則會報錯:

Failed to convert object of type <class 'tensorflow.python.framework.sparse_tensor.SparseTensor'> to Tensor. Contents: SparseTensor(indices=Tensor("SparseTensor/indices:0", shape=(16930, 2), dtype=int64), values=Tensor("SparseTensor/values:0", shape=(16930,), dtype=float32), dense_shape=Tensor("SparseTensor/dense_shape:0", shape=(2,), dtype=int64)). Consider casting elements to a supported type.
 

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