mxnet-索引

The basic slice syntax is i:j:k where i is the starting index, j is the stopping index, and k is the step (). This selects the m elements (in the corresponding dimension) with index values i, i + k, ..., i + (m - 1) k where  and q and r are the quotient and remainder obtained by dividing j - i by k: j - i = q k + r, so that i + (m - 1) k < j.

 # -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
import mxnet as mx
import numpy as np

import pickle as pkl
import time

x = mx.nd.zeros((2,3))
x[:] = 1
print x.asnumpy()

x[:,1:2] = 2
print x.asnumpy()
x[1:2,1:] = 3
print x.asnumpy()
x[1:,0:2] = mx.nd.zeros((1,2))
print x.asnumpy()
x[1,2] = 4
print x.asnumpy()
x[[0], [1, 2]] = 5
print x.asnumpy()
x[::-1, 0:2:2] = [6]
print x.asnumpy()

[[1. 1. 1.]
 [1. 1. 1.]]
[[1. 2. 1.]
 [1. 2. 1.]]
[[1. 2. 1.]
 [1. 3. 3.]]
[[1. 2. 1.]
 [0. 0. 3.]]
[[1. 2. 1.]
 [0. 0. 4.]]
[[1. 5. 5.]
 [0. 0. 4.]]
[[6. 5. 5.]
 [6. 0. 4.]]

 # -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
import mxnet as mx
import numpy as np

x = mx.nd.arange(12)
print x
print x[3:10:3]
print x[10:3:-3]
print x[::3]
print x[::-3]

[ 0.  1.  2. ...  9. 10. 11.]
<NDArray 12 @cpu(0)>

[3. 6. 9.]
<NDArray 3 @cpu(0)>

[10.  7.  4.]
<NDArray 3 @cpu(0)>

[0. 3. 6. 9.]
<NDArray 4 @cpu(0)>

[11.  8.  5.  2.]
<NDArray 4 @cpu(0)>

# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
import mxnet as mx
import numpy as np

x = mx.nd.arange(0,6).reshape((2,3))
print x.asnumpy()
print x[1].asnumpy()
y = x[0:1]
y[:] = 2
print y
print x.asnumpy()
x = mx.nd.arange(0, 8, dtype='int32').reshape((2, 2, 2))
print x[[0, 1]]
print x[1:, [0, 1]]
y = np.array([0, 1], dtype='int32')
print x[1:, y]
y = mx.nd.array([0, 1], dtype='int32')
print x[1:, y]

_register_converters
[[0. 1. 2.]
 [3. 4. 5.]]
[3. 4. 5.]

[[2. 2. 2.]]
<NDArray 1x3 @cpu(0)>
[[2. 2. 2.]
 [3. 4. 5.]]

[[[0 1]
  [2 3]]

 [[4 5]
  [6 7]]]
<NDArray 2x2x2 @cpu(0)>

[[[4 5]
  [6 7]]]
<NDArray 1x2x2 @cpu(0)>

[[[4 5]
  [6 7]]]
<NDArray 1x2x2 @cpu(0)>

[[[4 5]
  [6 7]]]
<NDArray 1x2x2 @cpu(0)>

==

 # -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
import mxnet as mx
import numpy as np

x = mx.nd.arange(0,6).reshape(2,3)
print x
y = x.reshape(3,2)
print y
y = x.reshape(3,-1)
print y
y = x.reshape(3,2)
print y
y = x.reshape(-3)
print y
y[:] = -1
print x

[[0. 1. 2.]
 [3. 4. 5.]]
<NDArray 2x3 @cpu(0)>

[[0. 1.]
 [2. 3.]
 [4. 5.]]
<NDArray 3x2 @cpu(0)>

[[0. 1.]
 [2. 3.]
 [4. 5.]]
<NDArray 3x2 @cpu(0)>

[[0. 1.]
 [2. 3.]
 [4. 5.]]
<NDArray 3x2 @cpu(0)>

[0. 1. 2. 3. 4. 5.]
<NDArray 6 @cpu(0)>

[[-1. -1. -1.]
 [-1. -1. -1.]]
<NDArray 2x3 @cpu(0)>

猜你喜欢

转载自blog.51cto.com/13959448/2316501