日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

mxnet基础到提高(46)-ndarray.zeros,CSRNDArray稀疏矩阵

發(fā)布時(shí)間:2025/3/12 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mxnet基础到提高(46)-ndarray.zeros,CSRNDArray稀疏矩阵 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
mxnet.ndarray.zeros(shape, ctx=None, dtype=None, stype=None, **kwargs)[source] 返回一個(gè)新的array數(shù)組,指定shape和type(形狀和類型),用0填充 參數(shù) shape(int 或 int tuple元組):空數(shù)組的形狀ctx (Context, 可選項(xiàng)) – 一個(gè)可選設(shè)備上下文dtype (字符串表示的類型名稱 or numpy.dtype, 可選) – 可選類型類型,默認(rèn)為float32stype (string, optional) – 空數(shù)組的存儲(chǔ)類型,比如T ‘row_sparse’, ‘csr’, 等 返回 一個(gè)新創(chuàng)建的數(shù)組 返回類型 NDArray, CSRNDArray或RowSparseNDArray lass mxnet.ndarray.sparse.CSRNDArray(handle, writable=True)[source] Bases: mxnet.ndarray.sparse.BaseSparseNDArray 二維NDArray壓縮稀疏行格式的稀疏表示。CSRNDArray將NDArray表示為三個(gè)獨(dú)立的數(shù)組:data、indptr和indices。它使用CSR表示,行i的列indices索引存儲(chǔ)在indices[indptr[i]:indptr[i+1]]中,它們對應(yīng)的值存儲(chǔ)在data[indptr[i]:indptr[i+1]]中。給定行的列索引應(yīng)按升序排序。同一行不允許重復(fù)列項(xiàng)。A sparse representation of 2D NDArray in the Compressed Sparse Row format.A CSRNDArray represents an NDArray as three separate arrays: data, indptr and indices. It uses the CSR representation where the column indices for row i are stored in indices[indptr[i]:indptr[i+1]] and their corresponding values are stored in data[indptr[i]:indptr[i+1]].The column indices for a given row are expected to be sorted in ascending order. Duplicate column entries for the same row are not allowed. dataA deep copy NDArray of the data array of the CSRNDArray.indicesA deep copy NDArray of the indices array of the CSRNDArray.indptrA deep copy NDArray of the indptr array of the CSRNDArray. a = mx.nd.array([[0, 1, 0], [2, 0, 0], [0, 0, 0], [0, 0, 3]]) a = a.tostype('csr') a.data.asnumpy() array([ 1., 2., 3.], dtype=float32) a.indices.asnumpy()#得到列 array([1, 0, 2]) a.indptr.asnumpy() array([0, 1, 2, 2, 3]) >>> b.asnumpy() array([[0., 1., 0.],[2., 0., 0.],[0., 0., 0.],[0., 0., 0.],[0., 0., 3.]], dtype=float32) >>> b.data[1. 2. 3.] <NDArray 3 @cpu(0)> >>> b.indices[1 0 2] <NDArray 3 @cpu(0)> >>> b.indptr[0 1 2 2 2 3] <NDArray 6 @cpu(0)>>>> #第2行的 >>> b.indptr[2][2] <NDArray 1 @cpu(0)> >>> b.indptr[3][2] <NDArray 1 @cpu(0)> >>>#第2行無數(shù)據(jù) >>> b.data[2:2][] >>>#第3行無數(shù)據(jù) >>> b.indptr[3][2] <NDArray 1 @cpu(0)>>>> b.indptr[3][2] <NDArray 1 @cpu(0)> >>> b.indptr[4][2] <NDArray 1 @cpu(0)>>>>#第4行有數(shù)據(jù) >>> b.indptr[4][2] <NDArray 1 @cpu(0)>>>>> b.indptr[5][3] <NDArray 1 @cpu(0)> >>> b.data[2:3][3.] <NDArray 1 @cpu(0)> >>> b<CSRNDArray 5x3 @cpu(0)> >>> b.asnumpy() array([[0., 1., 0.],[2., 0., 0.],[0., 0., 0.],[0., 0., 0.],[0., 0., 3.]], dtype=float32) >>> a = mx.nd.array([[0, 1, 0], [2, 0, 0], [0,0,0],[0, 0, 0], [0, 1, 3]]) >>> b = a.tostype('csr') >>> b.asnumpy() array([[0., 1., 0.],[2., 0., 0.],[0., 0., 0.],[0., 0., 0.],[0., 1., 3.]], dtype=float32) >>> b.data[1. 2. 1. 3.] <NDArray 4 @cpu(0)> >>> b.indices[1 0 1 2] <NDArray 4 @cpu(0)> >>> b.indptr[0 1 2 2 2 4] <NDArray 6 @cpu(0)> #下面取第4行的數(shù)值 >>> b.indptr[4][2] <NDArray 1 @cpu(0)> >>> b.indptr[5][4] <NDArray 1 @cpu(0)> >>> b.data[2:4][1. 3.] <NDArray 2 @cpu(0)> >>> from mxnet import nd import mxnet as mx x=nd.zeros(8) y=nd.zeros((2,3),mx.cpu(),'int32','csr')print(x) print(y) print(y.asnumpy()) [0. 0. 0. 0. 0. 0. 0. 0.] <NDArray 8 @cpu(0)><CSRNDArray 2x3 @cpu(0)> [[0 0 0][0 0 0]] from mxnet import nd import mxnet as mx a = mx.nd.array([[0, 1, 0,1,1], [2,0,1, 0, 0], [0,0,0,0,0],[0, 0, 0,0,0], [0, 1,0,3,2]]) b = a.tostype('csr') print(b.asnumpy()) print(b.indices) print(b.indptr) print(b.indptr[4],b.indptr[5]) print(b.data) print(b.data[5:8]) print(b.indices[5:8]) [[0. 1. 0. 1. 1.][2. 0. 1. 0. 0.][0. 0. 0. 0. 0.][0. 0. 0. 0. 0.][0. 1. 0. 3. 2.]][1 3 4 0 2 1 3 4] <NDArray 8 @cpu(0)>[0 3 5 5 5 8] <NDArray 6 @cpu(0)>[5] <NDArray 1 @cpu(0)> [8] <NDArray 1 @cpu(0)>[1. 1. 1. 2. 1. 1. 3. 2.] <NDArray 8 @cpu(0)>[1. 3. 2.] <NDArray 3 @cpu(0)>[1 3 4] <NDArray 3 @cpu(0)>

總結(jié)

以上是生活随笔為你收集整理的mxnet基础到提高(46)-ndarray.zeros,CSRNDArray稀疏矩阵的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。