Giter Club home page Giter Club logo

cntk-r's Introduction

R Interface to the Microsoft Cognitive Toolkit

Please take a look at the package website microsoft.github.io/CNTK-R for complete documentation.

CNTK-R is an R package for CNTK, which uses the reticulate package to bind to CNTK's Python API. Since it binds directly to Python, the R interface can perform any operation the Python bindings can, including running on the GPU. See the CIFAR-10 image classification vignette to see a basic example of training and evaluation of image classification on the CIFAR-10 dataset.

Installation

To use CNTK with R you'll need to have the appropriate Python wheel for your system already installed. See CNTK's documentation for help setting up CNTK's Python wheel on your system.

Then run the following to install CNTK's R package:

devtools::install_github("Microsoft/CNTK-R")

You can also take a look at our article on setting up CNTK and CNTK-R on your machine.

Usage

Documentation is still a work in progress, but the R package closely follows the CNTK Python interface where possible (Python docs). Here's the basic rundown of the differences:

  1. Properties are the same as in Python, and are accessed using the dollar sign ($) syntax:

    l <- Learner(parameters, lrschedule)
    l$parameters # returns parameters associated with learner
  2. Class methods are made global, and take the class object as the first property:

    learner.update(...) # Python
    update_learner(learner, ...) # R equivalent
    learner %>% update_learner(...) # R equivalent via pipe

    Since class methods are made global, some renaming from the original python API was necessary to avoid conflicts. See the documentation for a list of all available functions.

  3. R matrices are automatically converted to and from NumPy array's with float32 dtype.

  4. Python enums are accessed via function argument, e.g.:

    UnitType.Error # Python
    UnitType("Error") # R equivalent

cntk-r's People

Contributors

akzaidi avatar drdarshan avatar joeddav 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

Watchers

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

cntk-r's Issues

Error while running code in CharacterLM.rmd vignettes

I ran into a problem while attempting to follow the code exactly from the provided CharacterLM.rmd in the vignettes folder .

When I run this line of code, it gives the following error:

> train_lm("shakespeare.txt", epochs, max_num_minibatches)

Error in py_call_impl(callable, dots$args, dots$keywords) : 
  AttributeError: 'Variable' object has no attribute 'astype'
Detailed traceback: 
  File "D:\anaconda3\envs\cntk-py36\lib\site-packages\cntk\ops\functions.py", line 393, in __call__
    _, output_map = self.forward(arg_map, outputs)
  File "D:\anaconda3\envs\cntk-py36\lib\site-packages\cntk\internal\swig_helper.py", line 69, in wrapper
    result = f(*args, **kwds)
  File "D:\anaconda3\envs\cntk-py36\lib\site-packages\cntk\ops\functions.py", line 755, in forward
    None, device)
  File "D:\anaconda3\envs\cntk-py36\lib\site-packages\cntk\internal\sanitize.py", line 431, in sanitize_var_map
    batch = sanitize_batch(var, batch, seq_starts, device)
  File "D:\anaconda3\envs\cntk-py36\lib\site-packages\cntk\internal\swig_helper.py", line 69, in wrapper
    result = f(*args, **kwds)
  File "D:\anaconda3\envs\cntk-py36\lib\site-packages\cntk\internal\sanitize.py", line 200, in sanitize_batch
    return Value.create(var, batch, seq_starts, device)
  File "D:\anaconda3\envs\cntk-py
Called from: py_call_impl(callable, dots$args, dots$keywords)

A deeper analysis identified that the problem actually happens at this section of the train_lm() function:

z <- model(input_sequence)

Help on this issue will be greatly appreciated!

trouble with op_reduce_sum()

Hi,
This is a rewording of CNTK issue @2492 I opened, but was directed here.

I am trying to write my first R program to forecast time series, along the lines of my previous Python programs. So, I have a number of time series of varying length. Each point of each time series has been preprocessed to an input vector of length 6 and an output vector of similar length (INPUT_SIZE and OUPUT_SIZE respectively).
I want to code my own loss function sMAPE (see https://en.wikipedia.org/wiki/Symmetric_mean_absolute_percentage_error) .
So I declare
input_var <- seq_input_variable(INPUT_SIZE, name = "input")
output_var <- seq_input_variable(OUTPUT_SIZE, name = "label")
I create my network, e.g.
r=Recurrence(LSTM(STATE_SIZE))(input_var)
r=Dense(OUTPUT_SIZE, activation=op_softplus)(r)
z=op_minus(r,1)
Now, I am writing sMAPE:
a=op_element_divide(op_abs(op_minus(z,output_var)), op_plus(op_abs(output_var),op_abs(z)))
loss=op_reduce_sum(a)
and the last line fails, with
"AttributeError: 'Variable' object has no attribute 'is_static_axis'"

Very similar code works with Python. (It is attached listed in CNTK issue @2492).
seq_reduce_sum is not a good replacement, the result is
Composite(input: Sequence[Tensor[6]], label: Sequence[Tensor[6]]) -> Tensor[6]
while in Python, after execution o.reduce_sum() I am getting
Output('ElementTimes4266_Output_0', [#, *], []) and this works with an optimizer.

Regards,
Slawek

Malformed numpy

From @andrie:

> use_condaenv("cntk-py35", conda = "/home/adevries/anaconda3/envs/cntk-py35/bin/conda", required = TRUE)
> py_config()
Error in py_run_string_impl(paste0("import sys; sys.path.append('", system.file("python",  :
  ImportError:
Importing the multiarray numpy extension module failed.  Most
likely you are trying to import a failed build of numpy.
If you're working with a numpy git repo, try `git clean -xdf` (removes all
files not under version control).  Otherwise reinstall numpy.
 
 
Detailed traceback:
  File "/home/adevries/anaconda3/lib/python3.6/site-packages/numpy/__init__.py", line 146, in <module>
    from . import add_newdocs
  File "/home/adevries/anaconda3/lib/python3.6/site-packages/numpy/add_newdocs.py", line 13, in <module>
    from numpy.lib import add_newdoc
  File "/home/adevries/anaconda3/lib/python3.6/site-packages/numpy/lib/__init__.py", line 8, in <module>
    from .type_check import *
  File "/home/adevries/anaconda3/lib/python3.6/site-packages/numpy/lib/type_check.py", line 11, in <module>
    import numpy.core.numeric as _nx
  File "/home/adevries/anaconda3/lib/python3.6/site-packages/numpy/core/__init__.py", line 24, in <module>
    raise ImportError(msg)

Update numpy from 1.12 -> 1.13 seems to fix, but this issue doesn't effect the python API, just the R API.

Save and load model in cntk-r

What is the function to save and load model trained using cntk in R? I cannot find it in the documentation. Or is such model available in the CNTK-R package yet?
If I use keras with cntk backend in R, what is the function to save the model in cntk format? Or it has not been established?

failure while creating Trainer

Hi,
I am trying to port my Python program for time series forecasting (where it works). It is a regression style setup with both input and output being vectors. There are many time series, with varying lengths, but every point on a series is preprocessed to an input vector (of INPUT_SIZE), and at each point we also have the vector forecast (of OUTPUT_SIZE).

I am using stacked LSTMs, but to simplify matters, only single one here, created with this function:
createLSTMnet<-function(input_var , hidden_layer_dim1, output_dim) {
r=Recurrence(LSTM(hidden_layer_dim1,
use_peepholes=LSTM_USE_PEEPHOLES,
enable_self_stabilization=LSTM_USE_STABILIZATION, name="firstCell"))(input_var)
r=Dense(output_dim, bias=TRUE, name="lastLayer")(r)
}

I also use custom loss function:
sMAPELoss<-function (z, t){
t1=op_element_select(op_less(t,-0.9), z, t, name="t1")
a=op_abs(op_minus(t1,z))
b=op_plus(op_abs(t1),op_abs(z))
op_reduce_sum(op_element_divide(a,b))
}

The most important few more lines of code:
input_var <- seq_input_variable(INPUT_SIZE, name = "input")
output_var <- seq_input_variable(OUTPUT_SIZE, name = "label")

z = createLSTMnet(input_var, LSTM_STATE_SIZE, OUTPUT_SIZE)
loss = sMAPELoss(z,output_var) 
eval_error = loss
learner = learner_momentum_sgd(z$parameters, lr_schedule, mm_schedule,
		l2_regularization_weight = 0.0005, gaussian_noise_injection_std_dev = 0.0005)
trainer <- Trainer(z, c(loss, eval_error), learner)

And the last line fails with:
Error in py_call_impl(callable, dots$args, dots$keywords) :
TypeError: argument label's type Sequence[Tensor[2]] is incompatible with the type Sequence[np.float32] of the passed Variable

Could some kind soul help me please?

BTW, there is only the SGD learner available, right? No Adam, Adagrad, etc.
Any plans for brining the R functionality closer to the Python's one?

Regards,
Slawek

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.