GithubHelp home page GithubHelp logo

dmlc / minpy Goto Github PK

View Code? Open in Web Editor NEW
1.1K 1.1K 112.0 58.21 MB

NumPy interface with mixed backend execution

Home Page: https://minpy.readthedocs.io/en/latest/

License: Other

Python 99.63% Shell 0.37%

minpy's Introduction

Distributed Machine Learning Common Codebase

Build Status Documentation Status GitHub license

DMLC-Core is the backbone library to support all DMLC projects, offers the bricks to build efficient and scalable distributed machine learning libraries.

Developer Channel Join the chat at https://gitter.im/dmlc/dmlc-core

What's New

Contents

Known Issues

  • RecordIO format is not portable across different processor endians. So it is not possible to save RecordIO file on a x86 machine and then load it on a SPARC machine, because x86 is little endian while SPARC is big endian.

Contributing

Contributing to dmlc-core is welcomed! dmlc-core follows google's C style guide. If you are interested in contributing, take a look at feature wishlist and open a new issue if you like to add something.

  • DMLC-Core uses C++11 standard. Ensure that your C++ compiler supports C++11.
  • Try to introduce minimum dependency when possible

CheckList before submit code

  • Type make lint and fix all the style problems.
  • Type make doc and fix all the warnings.

NOTE

deps:

libcurl4-openssl-dev

minpy's People

Contributors

gaiyu0 avatar hotpxl avatar jermainewang avatar lryta avatar samuela avatar sneakerkg avatar taco-w avatar wangg12 avatar wellecks avatar zihengjiang avatar zzhang-cn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

minpy's Issues

Please compile with CUDA enabled

I'm trying to run the Logistic regression tutorial from here: http://minpy.readthedocs.io/en/latest/get-started/logistic_regression.html on my mac pro and I met this problem. I am currently using a cpu version of MXNET on my laptop and I thought this should support cpu. You may find the error message below:

172-16-100-143:minpy MufeiLi$ python LogisticRegressionTutorial.py
[20:38:15] /Users/MufeiLi/mxnet/dmlc-core/include/dmlc/logging.h:235: [20:38:15] src/storage/storage.cc:50: Please compile with CUDA enabled
Traceback (most recent call last):
File "LogisticRegressionTutorial.py", line 53, in
data, label = gaussian_cluster_generator(num_samples, num_features, num_classes)
File "LogisticRegressionTutorial.py", line 16, in gaussian_cluster_generator
sigma = np.ones((num_classes, num_features)) * 0.1
File "//anaconda/lib/python2.7/site-packages/minpy/dispatch/primitive_selector.py", line 41, in call
return prim(_args, *_kwargs)
File "//anaconda/lib/python2.7/site-packages/minpy/array.py", line 633, in call
result_value = self._func(_arg_values, *_kwargs_values)
File "/Users/MufeiLi/mxnet/python/mxnet/ndarray.py", line 861, in ones
arr = empty(shape, ctx, dtype)
File "/Users/MufeiLi/mxnet/python/mxnet/ndarray.py", line 578, in empty
return NDArray(handle=_new_alloc_handle(shape, ctx, False, dtype))
File "/Users/MufeiLi/mxnet/python/mxnet/ndarray.py", line 69, in _new_alloc_handle
ctypes.byref(hdl)))
File "/Users/MufeiLi/mxnet/python/mxnet/base.py", line 77, in check_call
raise MXNetError(py_str(_LIB.MXGetLastError()))
mxnet.base.MXNetError: [20:38:15] src/storage/storage.cc:50: Please compile with CUDA enabled

AttributeError: 'NoneType' object has no attribute 'add_partial_derivative'

I am training a very simple network in cifar 10, the network contains 2 conv layers and 2 fully connected layers, received an Error, here is the report:

AttributeError Traceback (most recent call last)
in ()
32 solver.init()
33 # Train!
---> 34 solver.train()

/home/silvan/minpy/minpy/nn/solver.pyc in train(self)
256 # evaluate after each epoch
257 train_acc = self.check_accuracy(
--> 258 self.train_dataiter, num_samples=self.train_acc_num_samples)
259 val_acc = self.check_accuracy(self.test_dataiter)
260 self.train_acc_history.append(train_acc)

/home/silvan/minpy/minpy/nn/solver.pyc in check_accuracy(self, dataiter, num_samples)
219 for each_batch in check_dataiter:
220 predict = self.model.forward(
--> 221 each_batch.data[0], mode='test').asnumpy()
222 acc_count += np.sum(
223 np.argmax(

in forward(self, X, mode)
63 out, self.params['gamma1'], self.params['beta1'],
64 running_mean=self.aux_params['running_mean1'],
---> 65 running_var=self.aux_params['running_var1'])
66 out = layers.affine(out, self.params['w2'], self.params['b2'])
67 # END TODO

/home/silvan/minpy/minpy/core.pyc in wrapper(_args, *_kwargs)
250 mpy_kwargs = convert(kwargs)
251 # call func
--> 252 return func(_mpy_args, *_mpy_kwargs)
253 # pylint: enable= missing-docstring
254 return wrapper

/home/silvan/minpy/minpy/nn/layers.pyc in batchnorm(x, gamma, beta, mode, eps, momentum, running_mean, running_var)
108 out = gamma * x_hat + beta
109
--> 110 running_mean = momentum * running_mean + (1.0 - momentum) * mean
111 running_var = momentum * running_var + (1.0 - momentum) * var
112 elif mode == 'test':

/home/silvan/minpy/minpy/array.pyc in rmul(self, other)
183
184 def rmul(self, other):
--> 185 return Value._ns.multiply(other, self)
186
187 def rfloordiv(self, other):

/home/silvan/minpy/minpy/dispatch/primitive_selector.pyc in call(self, _args, *_kwargs)
31 """
32 return self._policy.resolve_call(self._name, self._registry, args,
---> 33 kwargs)

/home/silvan/minpy/minpy/dispatch/policy.pyc in resolve_call(self, name, reg, args, kwargs)
119 _logger.debug('Found primitive "{}" with type {}.'.format(
120 name, prim.typestr))
--> 121 return prim(_args, *_kwargs)
122
123

/home/silvan/minpy/minpy/primitive.pyc in call(self, _args, *_kwargs)
183 owner.append(None)
184 visited_arg_indices.add(grad_index)
--> 185 tape.global_tape().add_partial_derivative(
186 grad_func, owner, result, self.type)
187 for k, arg in kwargs.items():

AttributeError: 'NoneType' object has no attribute 'add_partial_derivative'

Broadcast issue

I'm trying to run examples/nn/cnn.py to build travis performance test.

The error "mxnet.base.MXNetError: [15:36:05] src/operator/tensor/../elemwise_op_common.h:54: Incompatible attr in node at 1-th input: expected (128,512), got (512,)" happens when doing affine(), i.e. np.dot(x, w) + b. Here (128, 512) is the dims for np.dot(x, w) while (512) is the dim for b.

MXNet would do automatically broadcast in the past. However, it seems to me that the latest nnvm doesn't support this feature at the interface, i.e. elemwise_op_common.h, called from MinPy.

I'm still digging the problem and will update later if any progress.

Please let me know if you guys have any clue.

error: argument --data_dir is required

Hi, I was trying to run the cnn tutorial and this error occurred. I thought I was expected to replace '--data_dir' with the directory where I downloaded the data so I replaced it with

    parser.add_argument(data_dir='cifar-10-python.tar.gz',
                        type=str,
                        required=True,
                        help='Directory that contains cifar10 data')

But it still doesn't work:

TypeError: _get_positional_kwargs() takes exactly 2 arguments (1 given)

TypeError: _sum_grad() got an unexpected keyword argument 'keepdims'

"""Convolution Neural Network with batch normalization"""
import sys
import argparse

import minpy
import minpy.numpy as np
import mxnet as mx
from minpy.nn.io import NDArrayIter
# Can also use MXNet IO here
# from mxnet.io import NDArrayIter
from minpy.core import Function
from minpy.nn import layers
from minpy.nn.model import ModelBase
from minpy.nn.solver import Solver
from data_utils import get_CIFAR10_data

# Please uncomment following if you have GPU-enabled MXNet installed.
#from minpy.context import set_context, gpu
#set_context(gpu(0)) # set the global context as gpu(0)

batch_size=128
input_size=(3, 32, 32)
flattened_input_size=3 * 32 * 32
hidden_size=64
num_classes=10
reg = 0.001
nfilter = 32
ks = (5,5)
nepo = 5
learning_rate = 2e-4
bn = True


class ConvolutionNet(ModelBase):
    def __init__(self):
        super(ConvolutionNet, self).__init__()
        # Define symbols that using convolution and max pooling to extract better features
        # from input image.
        net = mx.sym.Variable(name='X')
        
        net = mx.sym.Convolution(
                data=net, name='conv1', kernel=ks, num_filter=nfilter)
        net = mx.symbol.BatchNorm(data=net, name='bn1')
        net = mx.sym.Activation(
                data=net, act_type='relu')
        net = mx.sym.Pooling(
                data=net, name='pool1', pool_type='max', kernel=(2, 2),
                stride=(2, 2))
        net = mx.sym.Convolution(
                data=net, name='conv2', kernel=ks, num_filter=nfilter)
        net = mx.symbol.BatchNorm(data=net, name='bn2')
        net = mx.sym.Activation(
                data=net, act_type='relu')
        
        net = mx.sym.Pooling(
                data=net, name='pool2', pool_type='max', kernel=(2, 2),
                stride=(2, 2))
        net = mx.sym.Flatten(data=net)
        
        # Create forward function and add parameters to this model.
        self.conv = Function(
                net, input_shapes={'X': (batch_size,) + input_size},
                name='conv')
        self.add_params(self.conv.get_params())
        # Define ndarray parameters used for classification part.
        output_shape = self.conv.get_one_output_shape()
        conv_out_size = output_shape[1]
        self.add_param(name='w1', shape=(conv_out_size, hidden_size)) \
            .add_param(name='b1', shape=(hidden_size,)) \
            .add_param(name='w2', shape=(hidden_size, num_classes)) \
            .add_param(name='b2', shape=(num_classes,))\
            .add_aux_param(name='running_mean', value=None) \
            .add_aux_param(name='running_var', value=None)\
            .add_param(name='gamma1', shape=(hidden_size,), init_rule='constant', init_config={'value': 1.0}) \
            .add_param(name='beta1', shape=(hidden_size,), init_rule='constant') 

    def forward(self, X, mode):
        out = self.conv(X=X, **self.params)
        out = layers.affine(out, self.params['w1'], self.params['b1'])
       
        out, self.aux_params['running_mean'], self.aux_params['running_var'] = layers.batchnorm(\
            out, self.params['gamma1'], self.params['beta1'], running_mean=self.aux_params['running_mean'],\
            running_var=self.aux_params['running_var'])
       
        out = layers.relu(out)
        out = layers.affine(out, self.params['w2'], self.params['b2'])
        return out

    def loss(self, predict, y):
	loss_reg = reg
	for name, weight in self.params.iteritems():
    	    loss_reg += np.sum(weight**2)
        return layers.softmax_loss(predict, y) + 0.5*reg*loss_reg

def main(args):
    # Create model.
    model = ConvolutionNet()
    # Create data iterators for training and testing sets.
    data = get_CIFAR10_data(args.data_dir)
    train_dataiter = NDArrayIter(data=data['X_train'],
                                 label=data['y_train'],
                                 batch_size=batch_size,
                                 shuffle=True)
    test_dataiter = NDArrayIter(data=data['X_test'],
                                label=data['y_test'],
                                batch_size=batch_size,
                                shuffle=False)
    # Create solver.
    solver = Solver(model,
                    train_dataiter,
                    test_dataiter,
                    num_epochs=nepo,
                    init_rule='gaussian',
                    init_config={
                        'stdvar': 0.001
                    },
                    #update_rule='sgd_momentum',
	            #update_rule='rmsprop',
                    update_rule ='adam',
                    optim_config={
                        'learning_rate': learning_rate
                    },
                    verbose=True,
                    print_every=40)
    # Initialize model parameters.
    solver.init()
    # Train!
    solver.train()


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description="Multi-layer perceptron example using minpy operators")
    parser.add_argument('--data_dir',
                        type=str,
                        default='cifar-10-batches-py',
                        help='Directory that contains cifar10 data')
main(parser.parse_args())


---------------------------------------------------------The code is shown above-----------------------------------------
python demo_cnn_bn.py
Traceback (most recent call last):
  File "demo_cnn_bn.py", line 137, in <module>
    main(parser.parse_args())
  File "demo_cnn_bn.py", line 128, in main
    solver.train()
  File "/usr/local/lib/python2.7/dist-packages/minpy-0.2.2-py2.7.egg/minpy/nn/solver.py", line 246, in train
    self._step(each_batch)
  File "/usr/local/lib/python2.7/dist-packages/minpy-0.2.2-py2.7.egg/minpy/nn/solver.py", line 176, in _step
    grad_arrays, loss = grad_and_loss_func(*param_arrays)
  File "/usr/local/lib/python2.7/dist-packages/minpy-0.2.2-py2.7.egg/minpy/core.py", line 44, in wrapped
    result_array = func(*arrays)
  File "/usr/local/lib/python2.7/dist-packages/minpy-0.2.2-py2.7.egg/minpy/nn/solver.py", line 170, in loss_func
    return self.model.loss_batch(batch, predict)
  File "/usr/local/lib/python2.7/dist-packages/minpy-0.2.2-py2.7.egg/minpy/nn/model.py", line 92, in loss_batch
    return self.loss(forward_outputs, batch.label[0])
  File "demo_cnn_bn.py", line 93, in loss
    return layers.softmax_loss(predict, y) + 0.5*reg*loss_reg
  File "/usr/local/lib/python2.7/dist-packages/minpy-0.2.2-py2.7.egg/minpy/core.py", line 248, in wrapper
    return func(*mpy_args, **mpy_kwargs)
  File "/usr/local/lib/python2.7/dist-packages/minpy-0.2.2-py2.7.egg/minpy/nn/layers.py", line 203, in softmax_loss
    loss += np.sum(np.log(np.sum(np.exp(probs), axis=1, keepdims=True))) / N
  File "/usr/local/lib/python2.7/dist-packages/minpy-0.2.2-py2.7.egg/minpy/dispatch/primitive_selector.py", line 33, in __call__
    kwargs)
  File "/usr/local/lib/python2.7/dist-packages/minpy-0.2.2-py2.7.egg/minpy/dispatch/policy.py", line 141, in resolve_call
    return prim(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/minpy-0.2.2-py2.7.egg/minpy/primitive.py", line 208, in __call__
    **kwargs_values)
TypeError: _sum_grad() got an unexpected keyword argument 'keepdims'

Minpy cannot be properly imported in PyCharm community edition

Traceback (most recent call last):
  File "/Users/lixiangci/Google Drive/GitHub_codes/VirtualRat/debugger.py", line 2, in <module>
    import minpy.numpy as np
  File "/Users/lixiangci/minpy/minpy/numpy/__init__.py", line 18, in <module>
    _mod = Module(_old)
  File "/Users/lixiangci/minpy/minpy/numpy/mocking.py", line 46, in __init__
    mod.register_primitives(self._registry, primitive_wrapper)
  File "/Users/lixiangci/minpy/minpy/array_variants/mxnet/mxnet_core.py", line 61, in register_primitives
    reg.register('reshape', prim_wrapper(NDArray.reshape))
AttributeError: type object 'NDArray' has no attribute 'reshape'

dtype and bitwise_xor not well supported

The original numpy package's dtype and bitwise_xor operation is not well supported.
minpy's numpy cannot find np.int32.
bitwise_xor prompts error saying that the operation is not safe.

binary addition problem implementation

I've implemented the binary addition problem described in Prof Hinton's online course using MinPy's RNN framework, primarily based on Peter Roelants' code (http://peterroelants.github.io/posts/rnn_implementation_part02/).

A few underlying functions need to be modified to match Peter's implementation. if anyone is interested, I can spend some time on adding pull requests for the example part.

code can be found here:
https://github.com/shadowleaves/deep_learning/blob/master/rnn/minpy_binary.py

While working on this issue, i've noticed that MinPy's autograd function is considerably slower than explicitly written formula for derivatives which were implemented in Peter's code. on my CPU (Intel(R) Xeon(R) CPU E5-2620 v3 @ 2.40GHz) it took on average 50ms for one autograd calculation using core.grad_and_loss function, whereas Peter's code took on average just 1.5ms. I understand that autograd would be slower - but somewhat surprised that it's so much slower. Is MinPy/MXNet doing gradient calculation on runtime? and is that the reason for the (much) slower performance?

import minpy.numpy error after git pull

I did git pull just now, which updated the code you guys pushed recently. I got error message when I imported minpy.numpy

ImportError                               Traceback (most recent call last)
<ipython-input-1-4d11ad0a5dbf> in <module>()
      1 import numpy as npp
----> 2 import minpy.numpy as np
      3 from minpy.nn import layers
      4 from minpy.nn.model import ModelBase
      5 from nn.solver import Solver

/Users/lixiangci/minpy/minpy/__init__.py in <module>()
      3 #pylint: disable= invalid-name
      4 from __future__ import absolute_import
----> 5 from minpy.dispatch.policy import PreferMXNetPolicy, OnlyNumPyPolicy, OnlyMXNetPolicy, wrap_policy
      6 from minpy.dispatch.policy import AutoBlacklistPolicy
      7 # Global config

/Users/lixiangci/minpy/minpy/dispatch/policy.py in <module>()
      8 import functools
      9 import minpy
---> 10 from .rule import Blacklist
     11 from minpy.array import Value
     12 from minpy.array_variants import ArrayType

/Users/lixiangci/minpy/minpy/dispatch/rule.py in <module>()
      3 
      4 import os
----> 5 import yaml
      6 import numpy
      7 import atexit

ImportError: No module named yaml

`Solver` accuracy check fails with small dataset

The check_accuracy call in Solver throws a ZeroDivisionError: float division by zero when the training dataset has less than 1000 examples.

This is because a sub iterator is not used, and the train_dataiter hasn't been reset at the time check_accuracy is called.

Gradient definition needed

  • slice (only numpy version is needed)
  • power (both numpy, mxnet)
  • split/concat (both numpy, mxnet)
  • minimum

TypeError: float() argument must be a string or a number

It's from the latest Minpy...Another Error when training on cifar 10 with a very simple network...Here is the Error:

(Iteration 1 / 3828) loss: 2.301242
(Iteration 21 / 3828) loss: 2.293782
(Iteration 41 / 3828) loss: 2.288820
(Iteration 61 / 3828) loss: 2.277458
(Iteration 81 / 3828) loss: 2.259784
(Iteration 101 / 3828) loss: 2.246727
(Iteration 121 / 3828) loss: 2.239867
(Iteration 141 / 3828) loss: 2.231653
(Iteration 161 / 3828) loss: 2.220054
(Iteration 181 / 3828) loss: 2.190943
(Iteration 201 / 3828) loss: 2.178167
(Iteration 221 / 3828) loss: 2.145582
(Iteration 241 / 3828) loss: 2.123701
(Iteration 261 / 3828) loss: 2.120092
(Iteration 281 / 3828) loss: 2.089282
(Iteration 301 / 3828) loss: 2.089207
(Iteration 321 / 3828) loss: 2.069961
(Iteration 341 / 3828) loss: 2.046455
(Iteration 361 / 3828) loss: 1.993805

(Iteration 381 / 3828) loss: 1.961865

TypeError Traceback (most recent call last)
in ()
32 solver.init()
33 # Train!
---> 34 solver.train()

/home/silvan/minpy/minpy/nn/solver.pyc in train(self)
256 # evaluate after each epoch
257 train_acc = self.check_accuracy(
--> 258 self.train_dataiter, num_samples=self.train_acc_num_samples)
259 val_acc = self.check_accuracy(self.test_dataiter)
260 self.train_acc_history.append(train_acc)

/home/silvan/minpy/minpy/nn/solver.pyc in check_accuracy(self, dataiter, num_samples)
224 predict, axis=1) == each_batch.label[0])
225 num_samples += check_dataiter.batch_size
--> 226 return float(acc_count) / num_samples
227
228 def init(self):

TypeError: float() argument must be a string or a number

AttributeError: 'NoneType' object has no attribute 'add_partial_derivative'

"""Convolution Neural Network with batch normalization"""
import sys
import argparse

import minpy
import minpy.numpy as np
import mxnet as mx
from minpy.nn.io import NDArrayIter
# Can also use MXNet IO here
# from mxnet.io import NDArrayIter
from minpy.core import Function
from minpy.nn import layers
from minpy.nn.model import ModelBase
from minpy.nn.solver import Solver
from data_utils import get_CIFAR10_data

# Please uncomment following if you have GPU-enabled MXNet installed.
from minpy.context import set_context, gpu
set_context(gpu(0)) # set the global context as gpu(0)

batch_size=128
input_size=(3, 32, 32)
flattened_input_size=3 * 32 * 32
hidden_size=512 # 1024
num_classes=10
reg = 0.001
nfilter = 128
ks = (5,5)
nepo = 5
learning_rate = 2e-4
bn = True


class ConvolutionNet(ModelBase):
    def __init__(self):
        super(ConvolutionNet, self).__init__()
        # Define symbols that using convolution and max pooling to extract better features
        # from input image.
        net = mx.sym.Variable(name='X')

        net = mx.sym.Convolution(
                data=net, name='conv1', kernel=ks, num_filter=nfilter)
        net = mx.symbol.BatchNorm(data=net, name='bn1')
        net = mx.sym.Activation(
                data=net, act_type='relu')
        net = mx.sym.Pooling(
                data=net, name='pool1', pool_type='max', kernel=(2, 2),
                stride=(2, 2))
        net = mx.sym.Convolution(
                data=net, name='conv2', kernel=ks, num_filter=nfilter)
        net = mx.symbol.BatchNorm(data=net, name='bn2')
        net = mx.sym.Activation(
                data=net, act_type='relu')

        net = mx.sym.Pooling(
                data=net, name='pool2', pool_type='max', kernel=(2, 2),
                stride=(2, 2))
        net = mx.sym.Flatten(data=net)

        # Create forward function and add parameters to this model.
        self.conv = Function(
                net, input_shapes={'X': (batch_size,) + input_size},
                name='conv')
        self.add_params(self.conv.get_params())
        # Define ndarray parameters used for classification part.
        output_shape = self.conv.get_one_output_shape()
        conv_out_size = output_shape[1]
        self.add_param(name='w1', shape=(conv_out_size, hidden_size)) \
            .add_param(name='b1', shape=(hidden_size,)) \
            .add_param(name='w2', shape=(hidden_size, num_classes)) \
            .add_param(name='b2', shape=(num_classes,))\
            .add_aux_param(name='running_mean', value=None) \
            .add_aux_param(name='running_var', value=None)\
            .add_param(name='gamma1', shape=(hidden_size,), init_rule='constant', init_config={'value': 1.0}) \
            .add_param(name='beta1', shape=(hidden_size,), init_rule='constant') 

    def forward(self, X, mode):
        out = self.conv(X=X, **self.params)
        out = layers.affine(out, self.params['w1'], self.params['b1'])

        out, self.aux_params['running_mean'], self.aux_params['running_var'] = layers.batchnorm(\
            out, self.params['gamma1'], self.params['beta1'], running_mean=self.aux_params['running_mean'],\
            running_var=self.aux_params['running_var'])

        out = layers.relu(out)
        out = layers.affine(out, self.params['w2'], self.params['b2'])
        return out

    def loss(self, predict, y):
    loss_reg = reg
    for name, weight in self.params.iteritems():
            loss_reg += np.sum(weight**2)
        return layers.softmax_loss(predict, y) + 0.5*reg*loss_reg

def main(args):
    # Create model.
    model = ConvolutionNet()
    # Create data iterators for training and testing sets.
    data = get_CIFAR10_data(args.data_dir)
    train_dataiter = NDArrayIter(data=data['X_train'],
                                 label=data['y_train'],
                                 batch_size=batch_size,
                                 shuffle=True)
    test_dataiter = NDArrayIter(data=data['X_test'],
                                label=data['y_test'],
                                batch_size=batch_size,
                                shuffle=False)
    # Create solver.
    solver = Solver(model,
                    train_dataiter,
                    test_dataiter,
                    num_epochs=nepo,
                    init_rule='gaussian',
                    init_config={
                        'stdvar': 0.001
                    },
                    #update_rule='sgd_momentum',
                #update_rule='rmsprop',
                    update_rule ='adam',
                    optim_config={
                        'learning_rate': learning_rate
                    },
                    verbose=True,
                    print_every=40)
    # Initialize model parameters.
    solver.init()
    # Train!
    solver.train()


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description="Multi-layer perceptron example using minpy operators")
    parser.add_argument('--data_dir',
                        type=str,
                        required=True,
                        help='Directory that contains cifar10 data')
    main(parser.parse_args())

Code style refinement to solve pylint error

@HrWangChengdu fixed pylint on travis. Could @hotpxl have a look at his Pylint options to check if the current configuration fits our project? (Do we need to turn on all the pylint options?)

After the confirmation of pylint options, we may distribute the work to fix the pylint error.

Policy mechanism

As mentioned in #86 #90 #92 , there are several discussions about how to support the policy switch.

I think the principle is not to have policy switch during runtime. We could have some static switch like MINPY_POLICY=AUTO_BLACKLIST python some_minpy_program.py. The change will include:

  1. Remove set_global_policy API. Policy choose could only be specified through environment.
  2. Policy object could not be created by users. There will be only one policy object throughout minpy's lifetime, which is created according to environment.
  3. Query API is not an object method, but a global function. For ABL only queries #92, simply returns the ABL that will be loaded if MINPY_POLICY=AUTO_BLACKLIST is specified.
  4. Make sure that auto blacklist policy will workout if fallback fail, because now we don't have policy switch to save (or hack) us during runtime.

@lryta @ZihengJiang

Installation error

Hi,

I am getting the following error after running either pip minpy install or installing via setup.py.

Any idea what is the issue?

>>> import minpy.numpy as np
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/daniel/anaconda2/lib/python2.7/site-packages/minpy/numpy/__init__.py", line 8, in <module>
    from .mocking import Module
  File "/home/daniel/anaconda2/lib/python2.7/site-packages/minpy/numpy/mocking.py", line 13, in <module>
    from ..dispatch import policy
  File "/home/daniel/anaconda2/lib/python2.7/site-packages/minpy/dispatch/policy.py", line 5, in <module>
    from .. import array
  File "/home/daniel/anaconda2/lib/python2.7/site-packages/minpy/array.py", line 16, in <module>
    from .array_variants import ArrayType
  File "/home/daniel/anaconda2/lib/python2.7/site-packages/minpy/array_variants/__init__.py", line 9, in <module>
    from . import mxnet
  File "/home/daniel/anaconda2/lib/python2.7/site-packages/minpy/array_variants/mxnet/__init__.py", line 7, in <module>
    import mxnet
  File "/home/daniel/anaconda2/lib/python2.7/site-packages/mxnet/__init__.py", line 7, in <module>
    from .base import MXNetError
  File "/home/daniel/anaconda2/lib/python2.7/site-packages/mxnet/base.py", line 43, in <module>
    _LIB = _load_lib()
  File "/home/daniel/anaconda2/lib/python2.7/site-packages/mxnet/base.py", line 35, in _load_lib
    lib = ctypes.cdll.LoadLibrary(lib_path[0])
  File "/home/daniel/anaconda2/lib/python2.7/ctypes/__init__.py", line 443, in LoadLibrary
    return self._dlltype(name)
  File "/home/daniel/anaconda2/lib/python2.7/ctypes/__init__.py", line 365, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: /home/daniel/anaconda2/lib/python2.7/site-packages/mxnet/./lib/libmxnet.so: undefined symbol: _ZN4dmlc19RecordIOChunkReaderC1ENS_10InputSplit4BlobEjj

Problem with concatenate

NDArrayIter will have problem when need padding. The problem occurs at:
np.concatenate( (x[1][self.cursor:], x[1][:pad]), axis=0) for x in data_source

I've checked the shape of x[1][self.cursor:] and x[1][:pad], and the call of np.concatenate is the same with numpy. So I suppose there's bug in concatenate.

AttributeError when run lr.py

When I try to run lr.py, it reports errors as below:

Traceback (most recent call last): File "/usr/lib/python2.7/logging/__init__.py", line 859, in emit msg = self.format(record) File "/usr/lib/python2.7/logging/__init__.py", line 732, in format return fmt.format(record) File "/home/joe/Projects/minpy/python/minpy/utils/log.py", line 42, in format self._style._fmt = fmt AttributeError: '_Formatter' object has no attribute '_style' Logged from file mocking.py, line 25 Traceback (most recent call last): File "/usr/lib/python2.7/logging/__init__.py", line 859, in emit msg = self.format(record) File "/usr/lib/python2.7/logging/__init__.py", line 732, in format return fmt.format(record) File "/home/joe/Projects/minpy/python/minpy/utils/log.py", line 42, in format self._style._fmt = fmt AttributeError: '_Formatter' object has no attribute '_style' Logged from file mocking.py, line 33 Traceback (most recent call last): File "/usr/lib/python2.7/logging/__init__.py", line 859, in emit msg = self.format(record) File "/usr/lib/python2.7/logging/__init__.py", line 732, in format return fmt.format(record) File "/home/joe/Projects/minpy/python/minpy/utils/log.py", line 42, in format self._style._fmt = fmt AttributeError: '_Formatter' object has no attribute '_style' Logged from file mocking.py, line 40 Traceback (most recent call last): File "/usr/lib/python2.7/logging/__init__.py", line 859, in emit msg = self.format(record) File "/usr/lib/python2.7/logging/__init__.py", line 732, in format return fmt.format(record) File "/home/joe/Projects/minpy/python/minpy/utils/log.py", line 42, in format self._style._fmt = fmt AttributeError: '_Formatter' object has no attribute '_style' Logged from file mocking.py, line 33 Traceback (most recent call last): File "/usr/lib/python2.7/logging/__init__.py", line 859, in emit msg = self.format(record) File "/usr/lib/python2.7/logging/__init__.py", line 732, in format return fmt.format(record) File "/home/joe/Projects/minpy/python/minpy/utils/log.py", line 42, in format self._style._fmt = fmt AttributeError: '_Formatter' object has no attribute '_style' Logged from file mocking.py, line 40 Traceback (most recent call last): File "/usr/lib/python2.7/logging/__init__.py", line 859, in emit msg = self.format(record) File "/usr/lib/python2.7/logging/__init__.py", line 732, in format return fmt.format(record) File "/home/joe/Projects/minpy/python/minpy/utils/log.py", line 42, in format self._style._fmt = fmt AttributeError: '_Formatter' object has no attribute '_style' Logged from file mocking.py, line 44 Traceback (most recent call last): File "/usr/lib/python2.7/logging/__init__.py", line 859, in emit msg = self.format(record) File "/usr/lib/python2.7/logging/__init__.py", line 732, in format return fmt.format(record) File "/home/joe/Projects/minpy/python/minpy/utils/log.py", line 42, in format self._style._fmt = fmt AttributeError: '_Formatter' object has no attribute '_style' Logged from file mocking.py, line 25 Traceback (most recent call last): File "/usr/lib/python2.7/logging/__init__.py", line 859, in emit msg = self.format(record) File "/usr/lib/python2.7/logging/__init__.py", line 732, in format return fmt.format(record) File "/home/joe/Projects/minpy/python/minpy/utils/log.py", line 42, in format self._style._fmt = fmt AttributeError: '_Formatter' object has no attribute '_style' Logged from file mocking.py, line 33 Traceback (most recent call last): File "/usr/lib/python2.7/logging/__init__.py", line 859, in emit msg = self.format(record) File "/usr/lib/python2.7/logging/__init__.py", line 732, in format return fmt.format(record) File "/home/joe/Projects/minpy/python/minpy/utils/log.py", line 42, in format self._style._fmt = fmt AttributeError: '_Formatter' object has no attribute '_style' Logged from file mocking.py, line 40 Traceback (most recent call last): File "/usr/lib/python2.7/logging/__init__.py", line 859, in emit msg = self.format(record) File "/usr/lib/python2.7/logging/__init__.py", line 732, in format return fmt.format(record) File "/home/joe/Projects/minpy/python/minpy/utils/log.py", line 42, in format self._style._fmt = fmt AttributeError: '_Formatter' object has no attribute '_style' Logged from file mocking.py, line 33 Traceback (most recent call last): File "lr.py", line 2, in <module> import minpy.numpy as np File "/home/joe/Projects/minpy/python/minpy/numpy/__init__.py", line 17, in <module> _mod = Module(_old) File "/home/joe/Projects/minpy/python/minpy/numpy/mocking.py", line 39, in __init__ mod.register_primitives(self._registry, primitive_wrapper) File "/home/joe/Projects/minpy/python/minpy/array_variants/mxnet/mxnet_core.py", line 34, in register_primitives reg.register('power', prim_wrapper(NDArray._power)) AttributeError: type object 'NDArray' has no attribute '_power'

Version 0.2 Release Check List

Writing down all things need to do for version 0.2. There are following main milestones:

cs231n course code & solution

  • Allow running without GPU.
  • Course code & solution should be separated. Solution code will be put in another private repo.
  • Code clean on Assignment 2 (need more detailed checklist from @HrWangChengdu @sneakerkg )
  • Code clean on Assignment 3 (need more detailed checklist from @lryta @sneakerkg )
    • Alternative Type system purely on MinPy.
    • Better change NDArray to have better type system (Add int and others besides int32 etc.) @lryta
    • In Array_variants, change NDArray related code to have correct type initialization. @lryta
  • Match the results with pure numpy solution. (All of us)
    • Assignment3
  • Match the performance with pure numpy solution. (All of us)
    • Edit grad checker in assignment 3 @lryta
  • Installation: @hotpxl
    • Virtual machine image installation with all the course code shipped in.
    • Installation: non-VM installation guide and "How to start assignment 1" guide.

Documents & Tutorials

There are many tutorials need to be finished before release for students. Ideally, we should have three types of tutorials:

  • Novice level: All written in IPython Notebook for pure greenhands.
  • Apprentice level: Written in readdoc & sphinx with plain code and explanation.
  • Adept level: API docs and some advanced topics like adding new operators and missing gradient definitions, etc.

Novice level is the highest priority in the current status. Adept level tutorial could be delayed or even released in the next major version. Novice & apprentice level tutorials MUST be finished before release.

Words could be shared between IPython notebook and sphinx doc to save efforts.

IPython Notebook Tutorials

  • Basic ndarray doc tutorial [Novice]. @jermainewang
    • @mli has made a similar tutorial for MXNet ndarray. Not sure about how to combine both? Need Mu's opinion.
    • Basic ndarray operations.
    • Lazy evaluation.
    • Concept of transparent fallback.
    • Pitfalls when working together with NumPy (general suggestions).
  • Autograd tutorial [Novice]. ( @lryta & need a reviewer)
    • Simple gradient
    • Important Tips when using autograd (immutable op, prefer pure function, etc.)
    • Multiple gradients
    • Gradient injection [Apprentice]
    • Gradient checker [Apprentice]
    • Several Tips on How to debug gradient (Important!).
  • I/O tutorial [Novice] @sneakerkg
    • Simple ndarray I/O
    • BucketIO for sequence data (RNN)
    • How to use MXNet's IO together with Minpy? [Apprentice]
  • MinPy Neural Net Guide [Apprentice] @zzhang-cn
    • MinPy nn package
    • Work with MXNet's symbolic layers
  • Multi-device tutorial [Adept]
  • Define new operators and gradients [Adept]

Non-notebook tutorials & examples

  • MinPy Installation guide. @hotpxl
  • Example workthrough. (Important!)
  • Frequent Ask Questions (VERY Important!)
    • This may serve as an index for people to jump to the related articles.
    • Common installation problems (dependencies, cuda, ...)
    • Errors when some operators have no proper gradient defined.
    • ...

Context dis-alignment error

I found minpy.numpy.zeros() could return a cpu mxnet.ndarray. If the gradient generated by grad_func() is gpu, then doing parameter updates would raise an context dis-alignment error.

minpy.numpy import error

Hi. I tried to import minpy.numpy, and python raised the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/bo/venv/lib/python2.7/site-packages/minpy/numpy/__init__.py", line 22, in <module> 
    numpy, name='numpy injector', injected_type={float, int, type(None), type}))
  File "/home/bo/venv/lib/python2.7/site-packages/minpy/numpy/mocking.py", line 65, in __init__  
    mod.def_grads(primitive_getter)
  File "/home/bo/venv/lib/python2.7/site-packages/minpy/array_variants/mxnet/mxnet_core.py", line 103, in def_grads
    prims('tanh').def_grad(lambda ans, x: lambda g: g * (1 - ans ** 2))
  File "/home/bo/venv/lib/python2.7/site-packages/minpy/numpy/mocking.py", line 63, in <lambda>  
    primitive_getter = lambda name: self._registry.get(name, vtype)
  File "/home/bo/venv/lib/python2.7/site-packages/minpy/dispatch/registry.py", line 69, in get   
    return self._reg[name][ptype]
KeyError: 1

Does it mean that something is called before it's registered?

I printed name, ptype and return self._reg[name], the last few lines of output is like:

expand_dims
0   
{0: <minpy.primitive.Primitive object at 0x3768ad0>}
sigmoid
0   
{0: <minpy.primitive.Primitive object at 0x376a4b0>}
softmax_output
0   
{0: <minpy.primitive.Primitive object at 0x376a590>}
dot 
1   
{0: <minpy.primitive.Primitive object at 0x36fcde0>, 1: <minpy.primitive.Primitive object at 0x37714b0>}
dot 
1   
{0: <minpy.primitive.Primitive object at 0x36fcde0>, 1: <minpy.primitive.Primitive object at 0x37714b0>}
tanh
1 
{0: <minpy.primitive.Primitive object at 0x3702b40>}
Traceback ......

I'm using minpy 0.2.6 and mxnet 962271410059156180ab1d5e79b805e687512be9.
How can I solve this problem?

Assignment2 correctness check with numpy

The commitid is 47586

Several differences found:
FullyConnectedNets:

  1. In SGD+Momentum, velocity error is too huge for minpy version;
    2.In RMSProp and Adam, cache error, v error, m error are too huge for minpy version;
    However, the learning curve looks similar between numpy and minpy for those optimization methods;

ConvolutionNets:

  1. Three-layer ConvNet: Initial loss (with regularization) is too huge for minpy version; This lead to the training produce Nan;

Dropout and BN looks fine;

Other problem in notebook:
FullyConnectedNets:
In Multilayer Nets -> Use a three-layer Net to overfit 50 training examples, please set num_train = 50, and modify lr and wd to the same as numpy version.

Error import minpy

Hello,

I get the following error. Please see my log:

piyush@fulmar:~/Downloads/GitHub/MXNet-examples$ python 
Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import find_mxnet
>>> import minpy.numpy as np
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/piyush/Downloads/GitHub/minpy/python/minpy/numpy/__init__.py", line 8, in <module>
    from .mocking import Module
  File "/home/piyush/Downloads/GitHub/minpy/python/minpy/numpy/mocking.py", line 13, in <module>
    from ..dispatch import policy
  File "/home/piyush/Downloads/GitHub/minpy/python/minpy/dispatch/policy.py", line 5, in <module>
    from .. import array
  File "/home/piyush/Downloads/GitHub/minpy/python/minpy/array.py", line 16, in <module>
    from .array_variants import ArrayType
  File "/home/piyush/Downloads/GitHub/minpy/python/minpy/array_variants/__init__.py", line 6, in <module>
    import enum
ImportError: No module named enum
>>> 

Any idea how I can fix it? Thanks.

Installation?

Just wondering how to install minpy.

Thanks,

Amir

Training time is very long when number of batch N is small.

I was translating my vanilla RNN from pure numpy implementation to minpy. The problem is for the same task, minpy version is significant slower than numpy version (approximately 60 times). Especially when number of batch N is small, the training is significantly slow. When I simply double the input data by doubling N, the training time remains the same, even deceases.

My code is in my repo Virtual-Rat. Master branch is for pure numpy implementation and minpy branch is the minpy implementation.

Import error on minpy 0.3.0

ImportError
Traceback (most recent call last)
in ()
----> 1 import minpy

/usr/local/lib/python2.7/dist-packages/minpy/init.py in ()
15
16 # Import minpy.numpy package to do some initialization.
---> 17 from . import numpy # pylint: disable= wrong-import-position
18
19 def set_global_policy(plc):

/usr/local/lib/python2.7/dist-packages/minpy/numpy/init.py in ()
9 from minpy.numpy.mocking import Module, NameInjector
10
---> 11 from . import random
12
13 _old = {

ImportError: cannot import name random

Error import minpy.numpy

`Python 2.7.11 (v2.7.11:6d1b6a68f775, Dec 5 2015, 12:54:16)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

import minpy
import minpy.numpy as np
Traceback (most recent call last):
File "", line 1, in
File "minpy/numpy/init.py", line 19, in
_mod = Module(_old)
File "minpy/numpy/mocking.py", line 43, in init
mod.register_primitives(self._registry, primitive_wrapper)
File "minpy/array_variants/mxnet/mxnet_core.py", line 41, in register_primitives
reg.register('reshape', prim_wrapper(NDArray.reshape))
AttributeError: type object 'NDArray' has no attribute 'reshape'`

GPU broadcast fails

Binary matrix operator, e.g.(20, 30) op (30), would fail in gpu policy (in running tests/test_broadcast.py) while code could work in cpu policy. Since Mxnet broadcast requires lhs.dim() == rhs.dim(), I need to add np.expand_dims(one-dim-matrix, axis=0) to pass.

I guess it's be a known issue. Just post it here in case we forget.

Proposal: Simplify array wrapping mechanism

We know that array wrapping makes our python overhead a little bit awkward in small-scale network. Here I have some proposals to reduce some overhead.

  1. Remove Number class. Currently, we use Number class to represent scalar values. We plan to support something like np.zeros([128, np.prod(x.shape)]). However, this is still not well-supported and there seems to be no good solution for this. I propose just remove the Number class and treat all the scalar values the same as array values. We will provide __float__, __int__ and __long__ methods override to allow users explicitly convert the array to scalar types:
np.zeros([128, int(np.prod(x.shape))])

I think this is more explicit and save us from some unexpected errors.

  1. Merge Array class and Value class, so that we only have Value class. If Number class is removed, there is no need to have another Array class. The Value class only serves for two purposes:
  • Method function dispatching (e.g. __add__, etc)
  • Transparent CPU/GPU copying.

The principle of this class is that:

  • It does NOT care about the type of the underlying data. For example, we could have Value.wrap(tuple(a, b, c)) and we will NOT wrap a, b and c recursively..
  • It will pop exception if the underlying data type is not used in a proper way. For example, you could not call float(x) on a value class that wraps an array.

This results in:

  • Much lighter wrapping logic (no type checking at all).
  • Error only upon incorrect usage (heavy sanity checking stuffs only happens in rare cases).

For multiple inputs and outputs:

  • Multiple inputs are usually written as separate arguments rather than a tuple in a function call. So it is really rare to have something like z = foo([x, y]). If this really happens and the user also want to calculate derivative of x in the above expression, s/he should explicitly say z = foo([array.wrap(x), y]).
  • Multiple outputs will be handled internally by the primitive.__call__ by a simple check.

How would I replace numpy with minpy in an import?

I wanted to see if there was a speedup in using minpy for all numpy imports in a library such as statsmodels or sklearn. Aside from manually replacing the imports, is there an efficient way to do this?

Cheers,

Amir

Simplify _match_shape

The current _match_shape implementation is too complicated. Need to simplify the implementation.

side effect brought by some expressions

As you can see in the comment of the script below, I found some expressions which should have no side effect in general programming will cause errors in MinPy. And I'm trying to debug it, if you have any suggestion, feel free to comment.

this script is adapted from rnn_mnist.ipynb

import mxnet
from minpy.nn.model import ModelBase
import minpy.nn.layers as layers
from minpy.context import set_context, gpu
import minpy.numpy as mnp
from minpy.nn.io import NDArrayIter
from minpy.nn.solver import Solver
import joblib
import numpy as np
set_context(gpu(0))

class RNNNet(ModelBase):
    def __init__(self,
                 batch_size=100,
                 input_size=1,
                 hidden_size=64,
                 num_classes=10):
        super(RNNNet, self).__init__()
        self.add_param(name='h0', shape=(batch_size, hidden_size)) \
            .add_param(name='Wx', shape=(input_size, hidden_size)) \
            .add_param(name='Wh', shape=(hidden_size, hidden_size),
                       init_rule='constant',
                       init_config={'value': np.identity(hidden_size)})\
            .add_param(name='b', shape=(hidden_size,),
                       init_rule='constant',
                       init_config={'value': np.zeros(hidden_size)})\
            .add_param(name='Wa', shape=(hidden_size, num_classes))\
            .add_param(name='ba', shape=(num_classes,)) \
            .add_param(name='A', shape=(hidden_size, hidden_size),
                       init_rule='constant',
                       init_config={'value': np.random.uniform(0.0, 0.01,
                           [hidden_size, hidden_size])})

    def forward(self, X, mode):
        seq_len = X.shape[1]
        self.params['h0'][:] = 0
        h = self.params['h0']
        A = self.params['A']
        for t in xrange(seq_len):
            # ERRORS AT HERE
            # uncomment the line bellow will raise num_inputs error
            # mnp.dot(h.T, h)
            # uncomment the line bellow will raise no attribute error
            # A = A + mnp.dot(h.T, h)
            h = layers.rnn_step(X[:, t, :], h, self.params['Wx'],
                         self.params['Wh'], self.params['b'])
        y = layers.affine(h, self.params['Wa'], self.params['ba'])

        return y

    def loss(self, predict, y):
        return layers.softmax_loss(predict, y)

data = joblib.load("data/mnist.dat")

mean = np.mean(data["train_data"], axis=0)
std = np.std(data["train_data"] - mean, axis=0)
data["train_data"] = (data["train_data"][:] - mean)/(std+1e-5)
data["test_data"] = (data["test_data"][:] - mean)/(std+1e-5)


BATCH = 50
INPUT_DIM = 7
HIDDEN_DIM = 128

_, dim = data["train_data"].shape
seq_len = dim / INPUT_DIM

train_iter = NDArrayIter(data["train_data"][:5000].reshape(5000, seq_len, INPUT_DIM),
                         data["train_label"][:5000],
                         batch_size=BATCH,
                         shuffle=True)

test_iter = NDArrayIter(data["test_data"][:1000].reshape(1000, seq_len, INPUT_DIM),
                        data["test_label"][:1000],
                        batch_size=BATCH,
                        shuffle=False)

model = RNNNet(batch_size=BATCH, input_size=INPUT_DIM, hidden_size=HIDDEN_DIM)

solver = Solver(model,
                train_iter,
                test_iter,
                num_epochs=100,
                init_rule='xavier',
                update_rule='rmsprop',
                optim_config={
                        'learning_rate': 0.0002,
                },
                verbose=True,
                print_every=10)

solver.init()
solver.train()

Error1:

[10:05:24] /home/joe/Projects/minpy/mxnet/dmlc-core/include/dmlc/./logging.h:300: [10:05:24] src/c_api/c_api_ndarray.cc:59: Check failed: num_inputs == infered_num_inputs (1 vs. 2) Expecting 2 inputs, got 1

Stack trace returned 34 entries:
[bt] (0) /home/joe/Library/PyEnv/minpy/local/lib/python2.7/site-packages/mxnet-0.7.0-py2.7.egg/mxnet/libmxnet.so(_ZN4dmlc15LogMessageFatalD1Ev+0x3c) [0x7eff9528bf2c]
[bt] (1) /home/joe/Library/PyEnv/minpy/local/lib/python2.7/site-packages/mxnet-0.7.0-py2.7.egg/mxnet/libmxnet.so(MXImperativeInvoke+0x439) [0x7eff958f6a19]
[bt] (2) /usr/lib/x86_64-linux-gnu/libffi.so.6(ffi_call_unix64+0x4c) [0x7efffdb95adc]
[bt] (3) /usr/lib/x86_64-linux-gnu/libffi.so.6(ffi_call+0x1fc) [0x7efffdb9540c]
[bt] (4) /home/joe/Library/PyEnv/minpy/lib/python2.7/lib-dynload/_ctypes.x86_64-linux-gnu.so(_ctypes_callproc+0x48e) [0x7efffddac5fe]
[bt] (5) /home/joe/Library/PyEnv/minpy/lib/python2.7/lib-dynload/_ctypes.x86_64-linux-gnu.so(+0x15f9e) [0x7efffddadf9e]
[bt] (6) python(PyEval_EvalFrameEx+0x965) [0x499be5]
[bt] (7) python(PyEval_EvalCodeEx+0x2ac) [0x4a090c]
[bt] (8) python(PyEval_EvalFrameEx+0x18c5) [0x49ab45]
[bt] (9) python(PyEval_EvalCodeEx+0x2ac) [0x4a090c]
[bt] (10) python(PyEval_EvalFrameEx+0x18c5) [0x49ab45]
[bt] (11) python(PyEval_EvalCodeEx+0x2ac) [0x4a090c]
[bt] (12) python(PyEval_EvalFrameEx+0x18c5) [0x49ab45]
[bt] (13) python(PyEval_EvalCodeEx+0x2ac) [0x4a090c]
[bt] (14) python(PyEval_EvalFrameEx+0x18c5) [0x49ab45]
[bt] (15) python(PyEval_EvalCodeEx+0x2ac) [0x4a090c]
[bt] (16) python(PyEval_EvalFrameEx+0x18c5) [0x49ab45]
[bt] (17) python() [0x4a2ca4]
[bt] (18) python() [0x56f9d5]
[bt] (19) python() [0x5038d5]
[bt] (20) python(PyEval_EvalFrameEx+0x965) [0x499be5]
[bt] (21) python() [0x4a1c9a]
[bt] (22) python(PyObject_Call+0x36) [0x505f96]
[bt] (23) python(PyEval_EvalFrameEx+0x1dfa) [0x49b07a]
[bt] (24) python(PyEval_EvalCodeEx+0x2ac) [0x4a090c]
[bt] (25) python(PyEval_EvalFrameEx+0x18c5) [0x49ab45]
[bt] (26) python(PyEval_EvalCodeEx+0x2ac) [0x4a090c]
[bt] (27) python(PyEval_EvalFrameEx+0x18c5) [0x49ab45]
[bt] (28) python() [0x4a1634]
[bt] (29) python(PyRun_FileExFlags+0x92) [0x44e4a5]
[bt] (30) python(PyRun_SimpleFileExFlags+0x2ee) [0x44ec9f]
[bt] (31) python(Py_Main+0xb5e) [0x44f904]
[bt] (32) /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf5) [0x7efffefe2f45]
[bt] (33) python() [0x578c4e]

Traceback (most recent call last):
  File "rnn_mnist.py", line 93, in <module>
    solver.train()
  File "/home/joe/Library/PyEnv/minpy/local/lib/python2.7/site-packages/minpy-0.2.5-py2.7.egg/minpy/nn/solver.py", line 264, in train
    self._step(each_batch)
  File "/home/joe/Library/PyEnv/minpy/local/lib/python2.7/site-packages/minpy-0.2.5-py2.7.egg/minpy/nn/solver.py", line 182, in _step
    grad_arrays, loss = grad_and_loss_func(*param_arrays)
  File "/home/joe/Library/PyEnv/minpy/local/lib/python2.7/site-packages/minpy-0.2.5-py2.7.egg/minpy/core.py", line 47, in wrapped
    grad_vals = tuple(current_tape.get_gradient(arrays[i]) for i in argnums)
  File "/home/joe/Library/PyEnv/minpy/local/lib/python2.7/site-packages/minpy-0.2.5-py2.7.egg/minpy/core.py", line 47, in <genexpr>
    grad_vals = tuple(current_tape.get_gradient(arrays[i]) for i in argnums)
  File "/home/joe/Library/PyEnv/minpy/local/lib/python2.7/site-packages/minpy-0.2.5-py2.7.egg/minpy/tape.py", line 129, in get_gradient
    grad = grad_record.grad_func(self._grads[grad_record.result])
  File "/home/joe/Library/PyEnv/minpy/local/lib/python2.7/site-packages/minpy-0.2.5-py2.7.egg/minpy/primitive.py", line 164, in wrapped
    return func(result)
  File "/home/joe/Library/PyEnv/minpy/local/lib/python2.7/site-packages/minpy-0.2.5-py2.7.egg/minpy/primitive.py", line 178, in wrapped
    return func(result)
  File "/home/joe/Library/PyEnv/minpy/local/lib/python2.7/site-packages/minpy-0.2.5-py2.7.egg/minpy/array_variants/mxnet/mxnet_core.py", line 86, in <lambda>
    prims('dot').def_grad(lambda ans, a, b: lambda g: ndarray.dot(g, b.T))
  File "/home/joe/Library/PyEnv/minpy/local/lib/python2.7/site-packages/mxnet-0.7.0-py2.7.egg/mxnet/_ctypes/ndarray.py", line 127, in generic_ndarray_function
    c_array(ctypes.c_char_p, [c_str(str(i)) for i in kwargs.values()])))
  File "/home/joe/Library/PyEnv/minpy/local/lib/python2.7/site-packages/mxnet-0.7.0-py2.7.egg/mxnet/base.py", line 75, in check_call
    raise MXNetError(py_str(_LIB.MXGetLastError()))
mxnet.base.MXNetError: [10:05:24] src/c_api/c_api_ndarray.cc:59: Check failed: num_inputs == infered_num_inputs (1 vs. 2) Expecting 2 inputs, got 1

Stack trace returned 34 entries:
[bt] (0) /home/joe/Library/PyEnv/minpy/local/lib/python2.7/site-packages/mxnet-0.7.0-py2.7.egg/mxnet/libmxnet.so(_ZN4dmlc15LogMessageFatalD1Ev+0x3c) [0x7eff9528bf2c]
[bt] (1) /home/joe/Library/PyEnv/minpy/local/lib/python2.7/site-packages/mxnet-0.7.0-py2.7.egg/mxnet/libmxnet.so(MXImperativeInvoke+0x439) [0x7eff958f6a19]
[bt] (2) /usr/lib/x86_64-linux-gnu/libffi.so.6(ffi_call_unix64+0x4c) [0x7efffdb95adc]
[bt] (3) /usr/lib/x86_64-linux-gnu/libffi.so.6(ffi_call+0x1fc) [0x7efffdb9540c]
[bt] (4) /home/joe/Library/PyEnv/minpy/lib/python2.7/lib-dynload/_ctypes.x86_64-linux-gnu.so(_ctypes_callproc+0x48e) [0x7efffddac5fe]
[bt] (5) /home/joe/Library/PyEnv/minpy/lib/python2.7/lib-dynload/_ctypes.x86_64-linux-gnu.so(+0x15f9e) [0x7efffddadf9e]
[bt] (6) python(PyEval_EvalFrameEx+0x965) [0x499be5]
[bt] (7) python(PyEval_EvalCodeEx+0x2ac) [0x4a090c]
[bt] (8) python(PyEval_EvalFrameEx+0x18c5) [0x49ab45]
[bt] (9) python(PyEval_EvalCodeEx+0x2ac) [0x4a090c]
[bt] (10) python(PyEval_EvalFrameEx+0x18c5) [0x49ab45]
[bt] (11) python(PyEval_EvalCodeEx+0x2ac) [0x4a090c]
[bt] (12) python(PyEval_EvalFrameEx+0x18c5) [0x49ab45]
[bt] (13) python(PyEval_EvalCodeEx+0x2ac) [0x4a090c]
[bt] (14) python(PyEval_EvalFrameEx+0x18c5) [0x49ab45]
[bt] (15) python(PyEval_EvalCodeEx+0x2ac) [0x4a090c]
[bt] (16) python(PyEval_EvalFrameEx+0x18c5) [0x49ab45]
[bt] (17) python() [0x4a2ca4]
[bt] (18) python() [0x56f9d5]
[bt] (19) python() [0x5038d5]
[bt] (20) python(PyEval_EvalFrameEx+0x965) [0x499be5]
[bt] (21) python() [0x4a1c9a]
[bt] (22) python(PyObject_Call+0x36) [0x505f96]
[bt] (23) python(PyEval_EvalFrameEx+0x1dfa) [0x49b07a]
[bt] (24) python(PyEval_EvalCodeEx+0x2ac) [0x4a090c]
[bt] (25) python(PyEval_EvalFrameEx+0x18c5) [0x49ab45]
[bt] (26) python(PyEval_EvalCodeEx+0x2ac) [0x4a090c]
[bt] (27) python(PyEval_EvalFrameEx+0x18c5) [0x49ab45]
[bt] (28) python() [0x4a1634]
[bt] (29) python(PyRun_FileExFlags+0x92) [0x44e4a5]
[bt] (30) python(PyRun_SimpleFileExFlags+0x2ee) [0x44ec9f]
[bt] (31) python(Py_Main+0xb5e) [0x44f904]
[bt] (32) /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf5) [0x7efffefe2f45]
[bt] (33) python() [0x578c4e]

Error2:

Traceback (most recent call last):
  File "rnn_mnist.py", line 93, in <module>
    solver.train()
  File "/home/joe/Library/PyEnv/minpy/local/lib/python2.7/site-packages/minpy-0.2.5-py2.7.egg/minpy/nn/solver.py", line 264, in train
    self._step(each_batch)
  File "/home/joe/Library/PyEnv/minpy/local/lib/python2.7/site-packages/minpy-0.2.5-py2.7.egg/minpy/nn/solver.py", line 182, in _step
    grad_arrays, loss = grad_and_loss_func(*param_arrays)
  File "/home/joe/Library/PyEnv/minpy/local/lib/python2.7/site-packages/minpy-0.2.5-py2.7.egg/minpy/core.py", line 47, in wrapped
    grad_vals = tuple(current_tape.get_gradient(arrays[i]) for i in argnums)
  File "/home/joe/Library/PyEnv/minpy/local/lib/python2.7/site-packages/minpy-0.2.5-py2.7.egg/minpy/core.py", line 47, in <genexpr>
    grad_vals = tuple(current_tape.get_gradient(arrays[i]) for i in argnums)
  File "/home/joe/Library/PyEnv/minpy/local/lib/python2.7/site-packages/minpy-0.2.5-py2.7.egg/minpy/tape.py", line 129, in get_gradient
    grad = grad_record.grad_func(self._grads[grad_record.result])
  File "/home/joe/Library/PyEnv/minpy/local/lib/python2.7/site-packages/minpy-0.2.5-py2.7.egg/minpy/primitive.py", line 164, in wrapped
    return func(result)
  File "/home/joe/Library/PyEnv/minpy/local/lib/python2.7/site-packages/minpy-0.2.5-py2.7.egg/minpy/primitive.py", line 178, in wrapped
    return func(result)
  File "/home/joe/Library/PyEnv/minpy/local/lib/python2.7/site-packages/minpy-0.2.5-py2.7.egg/minpy/array_variants/mxnet/mxnet_core.py", line 23, in newgradfun
    for axis, (i, j) in enumerate(zip(g.shape, padded_shape)):
AttributeError: 'float' object has no attribute 'shape'

No direct support for numpy.ndarray to minpy.array conversion?

It makes life easier if I can directly initialize a minpy array with numpy ndarray. : )

import minpy
import numpy

array_list = [1,2,3]
array_numpy = numpy.array(array_list)
array_minpy = minpy.numpy.array(array_list)

# array_minpy = minpy.numpy.array(array_numpy) # This will raise a type error message
array_minpy = minpy.numpy.array(array_numpy.tolist()) # This works

AttributeError: type object 'NDArray' has no attribute 'reshape'

Hi,
I encountered an error when importing minpy. And I checked that mxnet and minpy are up to date.
Thank you!

Traceback (most recent call last):
File "LogisticRegressionTutorial.py", line 10, in
import minpy.numpy as np
File "/Applications/anaconda/lib/python2.7/site-packages/minpy/numpy/init.py", line 18, in
_mod = Module(_old)
File "/Applications/anaconda/lib/python2.7/site-packages/minpy/numpy/mocking.py", line 46, in init
mod.register_primitives(self._registry, primitive_wrapper)
File "/Applications/anaconda/lib/python2.7/site-packages/minpy/array_variants/mxnet/mxnet_core.py", line 61, in register_primitives
reg.register('reshape', prim_wrapper(NDArray.reshape))
AttributeError: type object 'NDArray' has no attribute 'reshape'

Key error in tape.py after upgrade

Hi all, I'm working on a new reinforcement learning example with MinPy. Training was working fine on commit bc2e64f (version 0.2.5). After installing from the latest repo (fcb6e8a), I now see this error when calling core.grad_and_loss:

param_arrays = list(self.params.values())
param_keys = list(self.params.keys())
grad_and_loss_func = core.grad_and_loss(loss_func, argnum=range(len(param_arrays)))
  File "model.py", line 85, in _forward_backward
    grad_arrays, loss = grad_and_loss_func(*param_arrays)
  File "minpy/core.py", line 60, in wrapped
    tuple(arrays[i] for i in argnums), result)
  File "minpy/tape.py", line 248, in get_gradient
    return tuple(self._grads[arrid] for arrid in origin_id)
  File "minpy/tape.py", line 248, in <genexpr>
    return tuple(self._grads[arrid] for arrid in origin_id)
KeyError: 5869

Perhaps this commit introduced something since it looks like the lines from tape.py were modified there?

I can put the example into a repo and link to it if it would help- thanks!

Minpy Convolution Padding Issue

Hi,

I encounter an issue when using mxnet convolution symbol in minpy. In minpy/examples/basics/mxnet_convolution.py, I add pad=(1, 1) to the convolution symbol and get an mxnet error.

image

broadcast issue

for the recurrent neural network, when the train label size be (batch_ize,5), it occurs the problem:

  File "rnn-mp.py", line 96, in <module>
    solver.train()
  File "/home/abc/.local/lib/python2.7/site-packages/minpy/nn/solver.py", line 255, in train
    self.train_dataiter, num_samples=self.train_acc_num_samples)
  File "/home/abc/.local/lib/python2.7/site-packages/minpy/nn/solver.py", line 221, in check_accuracy
    predict, axis=1) == each_batch.label[0])
  File "/home/abc/.local/lib/python2.7/site-packages/minpy/array.py", line 65, in __eq__
    return Value._ns.equal(self, other)
  File "/home/abc/.local/lib/python2.7/site-packages/minpy/dispatch/primitive_selector.py", line 33, in __call__
    kwargs)
  File "/home/abc/.local/lib/python2.7/site-packages/minpy/dispatch/policy.py", line 138, in resolve_call
    return prim(args, kwargs)
  File "/home/abc/.local/lib/python2.7/site-packages/minpy/primitive.py", line 138, in __call__
    result_value = self._func(*arg_values, **kwargs_values)
ValueError: operands could not be broadcast together with shapes (100,) (100,5) 
here, batch_size = 100

Problem in RNN example

I try to reproduce RNN training in minpy compared with MXNet, but can't get the same curve.
When trying to dig in, I print dw in nn/optim.py, then finding out a problem:
w and dw should both be array with the same shape, however, in this case, sometimes dw is a Number. And if dw is a Number, it is always 0. All the bias in this example is the case, thus it's not training. And the Wh (hidden to hidden) is also like that.
Could someone can help on this?

LSTM_Captioning using minpy: memory issue

I implement an LSTM captioning which can be found in https://github.com/zx0502/cs231-homework-with-MinPy/blob/master/LSTM_Captioning_minpy_ans.ipynb, based on the codes in https://github.com/lryta/cs231n-winter2016-sol-minpy/tree/master/assignment3.
It appears that minpy version is very memory consuming, taking over 7GB (only 8GB available in my PC). In contrast, numpy version costs less than 1.5GB with the same parameters and a batch_size of 512, which is 4 times as large as what I test in the minpy notebook .

A number of warnings appear and the algorithm seems to be crashed, see below

(Iteration 561 / 1560) loss: 18.893697
(Iteration 571 / 1560) loss: 19.122100
(Iteration 581 / 1560) loss: 17.164288
(Iteration 591 / 1560) loss: 19.072623
(Iteration 601 / 1560) loss: 17.494023
(Iteration 611 / 1560) loss: 18.056801
(Iteration 621 / 1560) loss: 18.518261
(Iteration 631 / 1560) loss: 17.263882
(Iteration 641 / 1560) loss: 17.908625
(Iteration 651 / 1560) loss: 17.269566
(Iteration 661 / 1560) loss: 17.401004

/usr/local/lib/python2.7/dist-packages/minpy/array_variants/numpy/numpy_core.py:215: RuntimeWarning: overflow encountered in cosh
prims('tanh').def_grad(lambda ans, x: lambda g: g / np.cosh(x)**2)
/usr/local/lib/python2.7/dist-packages/minpy/primitive.py:136: RuntimeWarning: overflow encountered in exp
result_value = self._func(*arg_values, **kwargs_values)
/usr/local/lib/python2.7/dist-packages/minpy/array_variants/numpy/numpy_core.py:217: RuntimeWarning: invalid value encountered in multiply
prims('exp').def_grad(lambda ans, x: lambda g: ans * g)

(Iteration 671 / 1560) loss: nan
(Iteration 681 / 1560) loss: nan
(Iteration 691 / 1560) loss: nan
(Iteration 701 / 1560) loss: nan
(Iteration 711 / 1560) loss: nan
(Iteration 721 / 1560) loss: nan

GPU parallel computation support and explicit GPU assigning

I found minpy currently does not support multi-GPU parallel computation. Also, I cannot find minpy interfaces capable of explicitly assigning tasks to a specific GPU. I hope there could be something similar to the mxnet approach: ctx = mx.gpu(0), ctx=mx.gpu(1), etc.

consider using THC

Hi there devs, very good work!

I see that a lot of numpy functions are yet to be implemented for the GPU.
Instead of writing Tensor Op CUDA kernels from scratch, I suggest that you take a look at https://github.com/pytorch/pytorch/tree/master/torch/lib/THC
It is a C99 library with a lot of tensor functions implemented and might be helpful in saving time and effort.

This library currently powers torch and pytorch and has been developed and optimized for years.
The documentation for the functions is here: http://pytorch.org/docs/torch.html

Just a suggestion that might help accelerate your progress :)

cc: @tqchen

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.