Git Product home page Git Product logo

mindtorch's Introduction

mindtorch

A torch-like frontend for MindSpore, which use Define-by-run method to achieve dynamic graph.

Installation

Dependency

  • mindspore >= 2.1.0 (which optimized operator excution)

Install from source

To install MindTorch from source, please run:

pip install git+https://github.com/lvyufeng/mindtorch.git

Simple demo

import numpy as np
from mindtorch import Tensor
from mindtorch.nn import Parameter, Module
from mindtorch.optim import SGD

x_data = Tensor(np.random.randn(100, 1024))
coef = Tensor(np.random.randn(1024, 2048))
y_data = x_data @ coef + 5.

class Model(Module):
    def __init__(self) -> None:
        super().__init__()
        self.w = Parameter(Tensor(np.random.randn(1024, 2048)))
        self.b = Parameter(Tensor(np.random.randn(2048)))

    def forward(self, inputs: Tensor) -> Tensor:
        return inputs @ self.w + self.b

batch_size = 32
model = Model()
optimizer = SGD(model.parameters(), 0.001)

for epoch in range(100):

    epoch_loss = 0.0
    for start in range(0, 100, batch_size):
        end = start + batch_size
        optimizer.zero_grad()
        inputs = x_data[start:end]
        actual = y_data[start:end]
        predicted = model(inputs)

        errors = predicted - actual
        loss = (errors * errors).sum()

        loss.backward()
        epoch_loss += loss

        optimizer.step()

    print(epoch, epoch_loss)

Acknowledgement

This repository refers to the implementation of many deep learning frameworks. Thanks to their inspiration for this project, the following are their links:

mindtorch's People

Contributors

lvyufeng avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

mindtorch's Issues

cpp engine compile error

g++ -std=c++11 test_engine.cpp -o my_program

In file included from /usr/include/c++/7/unordered_map:41:0,
                 from engine.cpp:3,
                 from test_engine.cpp:3:
/usr/include/c++/7/tuple: In instantiation of ‘std::pair<_T1, _T2>::pair(std::tuple<_Args1 ...>&, std::tuple<_Args2 ...>&, std::_Index_tuple<_Indexes1 ...>, std::_Index_tuple<_Indexes2 ...>) [with _Args1 = {Node* const&}; long unsigned int ..._Indexes1 = {0}; _Args2 = {}; long unsigned int ..._Indexes2 = {}; _T1 = Node* const; _T2 = NodeTask]’:
/usr/include/c++/7/tuple:1641:63:   required from ‘std::pair<_T1, _T2>::pair(std::piecewise_construct_t, std::tuple<_Args1 ...>, std::tuple<_Args2 ...>) [with _Args1 = {Node* const&}; _Args2 = {}; _T1 = Node* const; _T2 = NodeTask]’
/usr/include/c++/7/ext/new_allocator.h:136:4:   required from ‘void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = std::pair<Node* const, NodeTask>; _Args = {const std::piecewise_construct_t&, std::tuple<Node* const&>, std::tuple<>}; _Tp = std::pair<Node* const, NodeTask>]’
/usr/include/c++/7/bits/alloc_traits.h:475:4:   required from ‘static void std::allocator_traits<std::allocator<_CharT> >::construct(std::allocator_traits<std::allocator<_CharT> >::allocator_type&, _Up*, _Args&& ...) [with _Up = std::pair<Node* const, NodeTask>; _Args = {const std::piecewise_construct_t&, std::tuple<Node* const&>, std::tuple<>}; _Tp = std::pair<Node* const, NodeTask>; std::allocator_traits<std::allocator<_CharT> >::allocator_type = std::allocator<std::pair<Node* const, NodeTask> >]’
/usr/include/c++/7/bits/hashtable_policy.h:2066:37:   required from ‘std::__detail::_Hashtable_alloc<_NodeAlloc>::__node_type* std::__detail::_Hashtable_alloc<_NodeAlloc>::_M_allocate_node(_Args&& ...) [with _Args = {const std::piecewise_construct_t&, std::tuple<Node* const&>, std::tuple<>}; _NodeAlloc = std::allocator<std::__detail::_Hash_node<std::pair<Node* const, NodeTask>, false> >; std::__detail::_Hashtable_alloc<_NodeAlloc>::__node_type = std::__detail::_Hash_node<std::pair<Node* const, NodeTask>, false>]’
/usr/include/c++/7/bits/hashtable_policy.h:725:8:   required from ‘std::__detail::_Map_base<_Key, _Pair, _Alloc, std::__detail::_Select1st, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits, true>::mapped_type& std::__detail::_Map_base<_Key, _Pair, _Alloc, std::__detail::_Select1st, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits, true>::operator[](const key_type&) [with _Key = Node*; _Pair = std::pair<Node* const, NodeTask>; _Alloc = std::allocator<std::pair<Node* const, NodeTask> >; _Equal = std::equal_to<Node*>; _H1 = std::hash<Node*>; _H2 = std::__detail::_Mod_range_hashing; _Hash = std::__detail::_Default_ranged_hash; _RehashPolicy = std::__detail::_Prime_rehash_policy; _Traits = std::__detail::_Hashtable_traits<false, false, true>; std::__detail::_Map_base<_Key, _Pair, _Alloc, std::__detail::_Select1st, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits, true>::mapped_type = NodeTask; std::__detail::_Map_base<_Key, _Pair, _Alloc, std::__detail::_Select1st, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits, true>::key_type = Node*]’
/usr/include/c++/7/bits/unordered_map.h:973:20:   required from ‘std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::mapped_type& std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::operator[](const key_type&) [with _Key = Node*; _Tp = NodeTask; _Hash = std::hash<Node*>; _Pred = std::equal_to<Node*>; _Alloc = std::allocator<std::pair<Node* const, NodeTask> >; std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::mapped_type = NodeTask; std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::key_type = Node*]’
engine.cpp:68:42:   required from here
/usr/include/c++/7/tuple:1652:70: error: no matching function for call to ‘NodeTask::NodeTask()’
         second(std::forward<_Args2>(std::get<_Indexes2>(__tuple2))...)
                                                                      ^
In file included from test_engine.cpp:3:0:
engine.cpp:35:5: note: candidate: NodeTask::NodeTask(Node*, const std::vector<double>&)
     NodeTask(Node* n, const std::vector<double>& grad_input) : node(n), gradInput(grad_input) {}
     ^~~~~~~~
engine.cpp:35:5: note:   candidate expects 2 arguments, 0 provided
engine.cpp:29:7: note: candidate: NodeTask::NodeTask(const NodeTask&)
 class NodeTask {
       ^~~~~~~~
engine.cpp:29:7: note:   candidate expects 1 argument, 0 provided
engine.cpp:29:7: note: candidate: NodeTask::NodeTask(NodeTask&&)
engine.cpp:29:7: note:   candidate expects 1 argument, 0 provided```

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.