mxnet随笔-type,context,copyto

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010255642/article/details/82079739
 # -*- coding: utf-8 -*-
"""
Spyder Editor

This is a temporary script file.
"""
import mxnet as mx
import numpy as np
x = mx.nd.zeros((3, 5, 2))
print x.size
x = mx.nd.array([1, 2, 3, 4])
print x.context
print type(x.context)

x = mx.nd.zeros((2,3))
print x.dtype

y = mx.nd.zeros((2,3), dtype='int32')
print y.dtype

x = mx.nd.ones((1,), dtype='int32')
print x.asscalar()
print type(x.asscalar())


x = mx.nd.zeros((2,3), dtype='float32')
y = x.astype('int32')
print y.dtype

30
cpu(0)
<class 'mxnet.context.Context'>
<type 'numpy.float32'>
<type 'numpy.int32'>
1
<type 'numpy.int32'>
<type 'numpy.int32'>

 # -*- coding: utf-8 -*-
"""
Spyder Editor

This is a temporary script file.
"""
import mxnet as mx
import numpy as np

x = mx.nd.ones((2,3))
y = mx.nd.zeros((2,3))
z = x.copyto(y)
print z is y
print y

True

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

如果context相同,返回源内容的链接

如果context不同,则copy

>>> x = mx.nd.ones((2,3))
>>> y = x.as_in_context(mx.cpu())
>>> y is x
True
>>> z = x.as_in_context(mx.gpu(0))
>>> z is x
False

猜你喜欢

转载自blog.csdn.net/u010255642/article/details/82079739