jstzwj 6 年 前
コミット
2efbd5af2e

+ 4 - 1
.gitignore

@@ -35,4 +35,7 @@
 build/
 .vscode
 *.pyc
-*.pyd
+*.pyd
+*.whl
+python/pytraph.egg-info/
+python/dist

+ 1 - 0
python/MANIFEST.in

@@ -0,0 +1 @@
+include pytraph/core/_traph_tensor.pyd

+ 1 - 1
python/setup.py

@@ -5,7 +5,7 @@ from setuptools import setup, find_packages
 setup(
     name = "pytraph",
     version = "0.0.1",
-    keywords = ("pip", "deep learning"),
+    keywords = ["pip", "deep learning"],
     description = "Deep learning framework",
     long_description = "Deep learning framework",
     license = "MIT Licence",

+ 7 - 7
traph/include/traph/core/slice.h

@@ -1,14 +1,11 @@
 #ifndef TRAPH_TENSOR_SLICE_H_
 #define TRAPH_TENSOR_SLICE_H_
 
-#include <utility>
-#include <variant>
 #include <vector>
 
 #include <traph/core/type.h>
-#include <traph/tensor/index.h>
-#include <traph/tensor/utils.h>
-#include <traph/tensor/tensor.h>
+#include <traph/core/index.h>
+#include <traph/core/utils.h>
 
 namespace traph
 {
@@ -20,6 +17,7 @@ namespace traph
         idx_type end;
     };
 
+    /*
     class AdvancedSlice
     {
     public:
@@ -31,12 +29,14 @@ namespace traph
         BASIC,
         ADVANCED
     };
+    */
 
     class Slice
     {
     public:
-        std::variant<BasicSlice, AdvancedSlice> slice;
-        SliceMode mode;
+        idx_type start;
+        idx_type step;
+        idx_type end;
     };
 
     using SliceVector = std::vector<Slice>;

+ 3 - 0
traph/include/traph/core/tensor.h

@@ -7,6 +7,7 @@
 
 #include <traph/core/type.h>
 #include <traph/core/index.h>
+#include <traph/core/slice.h>
 
 #include <traph/core/tensor_storage.h>
 
@@ -37,6 +38,7 @@ namespace traph
         virtual platform_type platform() = 0;
         virtual void reshape_(const DimVector& dims) = 0;
         virtual void resize_(const DimVector& dims) = 0;
+        virtual std::shared_ptr<TensorInterface> select(const SliceVector& slice) const = 0;
         virtual void sin_() = 0;
 		virtual DimVector size() const = 0;
 		virtual idx_type size(idx_type i) const = 0;
@@ -82,6 +84,7 @@ namespace traph
         virtual TensorInterfacePtr reduce_dim(idx_type dim, std::function<T(T,T)> f) const = 0;
         virtual void reshape_(const DimVector& dims) = 0;
         virtual void resize_(const DimVector& dims) = 0;
+        virtual std::shared_ptr<TensorInterface> select(const SliceVector& slice) const = 0;
         virtual void sin_() = 0;
 		virtual DimVector size() const = 0;
 		virtual idx_type size(idx_type i) const = 0;

+ 39 - 1
traph/include/traph/tensor/tensor.h

@@ -83,6 +83,7 @@ namespace traph
         virtual TensorInterfacePtr reduce_dim(idx_type dim, std::function<T(T,T)> f) const override;
         virtual void reshape_(const DimVector& dims) override;
         virtual void resize_(const DimVector& dims) override;
+        virtual std::shared_ptr<TensorInterface> select(const SliceVector& slice) const override;
         virtual void sin_() override;
 		virtual DimVector size() const override;
 		virtual idx_type size(idx_type i) const override;
@@ -400,9 +401,46 @@ namespace traph
         auto_strides();
     }
     template<typename T>
+    std::shared_ptr<TensorInterface> Tensor<T>::select(const SliceVector& slice) const
+    {
+        std::shared_ptr<Tensor<T>> result(new Tensor<T>);
+        result->_rep = _rep;
+        auto compute_offset = [](){
+
+        };
+
+        // dimension
+        DimVector dim;
+        for(auto& each:slice)
+        {
+            dim.push_back(std::ceil((each.end - each.start)/(float)each.step));
+        }
+        result->_dimensions = dim;
+
+        // offset
+        idx_type new_offset =1;
+        for(idx_type i = 0; i < slice.size(); ++i)
+        {
+            new_offset *= _strides[i] * slice[i].start;
+        }
+        result->_offset = _offset + new_offset;
+
+        // strides
+        DimVector strides;
+        for(idx_type i = 0; i < slice.size(); ++i)
+        {
+            strides.push_back(_strides[i] * slice[i].step);
+        }
+        result->_strides = strides;
+
+        result->_order = _order;
+
+        return std::dynamic_pointer_cast<TensorInterface>(result);
+    }
+    template<typename T>
     void Tensor<T>::sin_()
     {
-        apply_([](T a)->T {return std::sin(a); });
+        apply_([](T a)->T { return std::sin(a); });
     }
     template<typename T>
     DimVector Tensor<T>::size() const { return _dimensions;}