In [1]:
import mxnet as mx
from mxnet import nd
import numpy as np
mx.random.seed(1)
In [2]:
x = nd.empty((3, 4))
print(x)
In [3]:
x = nd.ones((3, 4))
x
Out[3]:
In [4]:
y = nd.random_normal(0, 1, shape=(3, 4))
print y
print y.shape
print y.size
In [5]:
x * y
Out[5]:
In [6]:
nd.exp(y)
Out[6]:
In [7]:
nd.dot(x, y.T)
Out[7]:
In [8]:
# Memory Host
print "The current mem host y is {}".format(id(y))
y[:] = x + y
print "The current mem host after add + assigning y is {}".format(id(y))
y = x + y
print "The current mem host after add y is {}".format(id(y))
In [9]:
print y
print y[1:3]
print y[1:3,1:2]
In [10]:
print x
x[1,2] = 9
print x
x[1:2,1:3] = 5
print x
In [11]:
x = nd.ones(shape=(3,3))
print('x = ', x)
y = nd.arange(3)
print('y = ', y)
print('x + y = ', x + y)
In [12]:
a = x.asnumpy()
print "The type of a is {}".format(type(a))
y = nd.array(a)
print "The type of a is {}".format(type(y))
In [13]:
# z = nd.ones(shape=(3,3), ctx=mx.gpu(0))
# z
In [14]:
# x_gpu = x.copyto(mx.gpu(0))
# print(x_gpu)
In [15]:
# scalars
x = nd.array([3.0])
y = nd.array([2.0])
print 'x + y = ', x + y
print 'x * y = ', x * y
print 'x / y = ', x / y
print 'x ** y = ', nd.power(x,y)
In [16]:
# convert it to python scala
x.asscalar()
Out[16]:
In [17]:
# Vector
u = nd.arange(4)
print('u = ', u)
print u[3]
print len(u)
print u.shape
a = 2
x = nd.array([1,2,3])
y = nd.array([10,20,30])
print(a * x)
print(a * x + y)
In [18]:
# Matrices
x = nd.arange(20)
A = x.reshape((5, 4))
print A
print 'A[2, 3] = ', A[2, 3]
print('row 2', A[2, :])
print('column 3', A[:, 3])
print A.T
In [19]:
# Tensor
X = nd.arange(24).reshape((2, 3, 4))
print 'X.shape =', X.shape
print 'X =', X
In [20]:
u = nd.array([1, 2, 4, 8])
v = nd.ones_like(u) * 2
print 'v =', v
print 'u + v', u + v
print 'u - v', u - v
print 'u * v', u * v
print 'u / v', u / v
print nd.sum(u)
print nd.mean(u)
print nd.sum(u) / u.size
print nd.dot(u, v)
print nd.sum(u * v)
In [21]:
# Matrices multiple Vector
print nd.dot(A, u)
print nd.dot(A, A.T)
In [22]:
print nd.norm(u)
print nd.sqrt(nd.sum(u**2))
print nd.sum(nd.abs(u))
In [ ]: