You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@singa.apache.org by wa...@apache.org on 2018/04/29 15:31:10 UTC
[01/12] incubator-singa git commit: SINGA-347 Create a function that
supports einsum provide the tensordot function to do the tensor
multiplication of two tensors along specified axes
Repository: incubator-singa
Updated Branches:
refs/heads/master 9090160ed -> 08675e3a5
SINGA-347 Create a function that supports einsum
provide the tensordot function to do the tensor multiplication of two tensors along specified axes
Project: http://git-wip-us.apache.org/repos/asf/incubator-singa/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-singa/commit/e27498df
Tree: http://git-wip-us.apache.org/repos/asf/incubator-singa/tree/e27498df
Diff: http://git-wip-us.apache.org/repos/asf/incubator-singa/diff/e27498df
Branch: refs/heads/master
Commit: e27498df74fecf30cb7e0408c36465791802320d
Parents: b4ea650
Author: sheyujian <sh...@me.com>
Authored: Thu Apr 12 22:58:32 2018 +0800
Committer: sheyujian <sh...@me.com>
Committed: Wed Apr 18 13:55:31 2018 +0800
----------------------------------------------------------------------
python/singa/tensor.py | 99 ++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 98 insertions(+), 1 deletion(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/e27498df/python/singa/tensor.py
----------------------------------------------------------------------
diff --git a/python/singa/tensor.py b/python/singa/tensor.py
index 2fcadb4..b5f5d99 100644
--- a/python/singa/tensor.py
+++ b/python/singa/tensor.py
@@ -938,7 +938,104 @@ def mult(A, B, C=None, alpha=1.0, beta=0.0):
singa.MultWithScale(alpha, A.singa_tensor, B.singa_tensor,
beta, C.singa_tensor)
return C
-
+def tensordot (A,B,axes=2):
+
+ """Returns the tensor multiplication of two tensors along specified axes.
+
+ This is equivalent to compute dot product along the specified axes which
+ are treated as one axis by reshaping.
+
+ Args:
+ a (Singa.Tensor): The first argument.
+ b (Singa.Tensor): The second argument.
+ axes:
+ - If it is an integer, then ''axes'' represent axes at the last of ''a`'' and
+ the first of ''b'' are used.
+ - If it is a pair of sequences of integers, then these two
+ sequences specify the list of axes for ''a'' and ''b''. The
+ corresponding axes are paired for sum-product.
+
+ Return:
+ singa.tensor: The tensor product of ''a'' and ''b'' along the
+ axes specified by ''axes''.
+ """
+ # when axes is an integer, axes_A and axes_B represent axes at the last of ''a`'' and
+ # the first of ''b''. For example, when axes is 1, we do the normal multiplication :
+ # if A is in shape(3,2,4), B is in shape(4,2,5), it will return a matrix in shape(3,2,2,5)
+ #when axes is 2 and A,B are in the same shape, it will return a matrix in shape(3,5)
+ try:
+ iter(axes)
+ except Exception:
+ axes_A = list(range(-axes, 0))
+ axes_B = list(range(0, axes))
+ else:
+ axes_A, axes_B = axes
+ # when axes is a pair of sequences of integers.For example, A is in shape(3,2,4),
+ #B is in shape(4,2,5), we set axes as ([1,2],[1,0]), it will return a matrix in shape(3,5)
+ try:
+ na = len(axes_A)
+ axes_A = list(axes_B)
+ except TypeError:
+ axes_A = [axes_A]
+ na = 1
+ try:
+ nb = len(axes_A)
+ axes_B = list(axes_B)
+ except TypeError:
+ axes_B = [axes_B]
+ nb = 1
+ # a_shape and b_shape are the shape of tensor A and B, while nda and ndb are the dim of A and B
+ a_shape = A.shape
+ nda = A.ndim
+ b_shape = B.shape
+ ndb = B.ndim
+ equal = True
+ # to check if the length of axe_A is equal to axes_B
+ if na != nb:
+ equal = False
+ else:
+ # to make the shape match
+ for k in range(na):
+ if a_shape[axes_a[k]] != b_shape[axes_b[k]]:
+ equal = False
+ break
+ if axes_a[k] < 0:
+ axes_a[k] += nda
+ if axes_b[k] < 0:
+ axes_b[k] += ndb
+ if not equal:
+ raise ValueError("shape-mismatch for sum")
+ '''start to do the calculation according to the axes'''
+ notin = [k for k in range(nda) if k not in axes_a]
+ # nda is the dim of A, and axes_a is the axis for A, notin is the axis which is not in axes_A
+ newaxes_a = notin + axes_a
+ N2 = 1
+ for axis in axes_a:
+ N2 *= a_shape[axis]
+ N1 = 1
+ for ax in notin:
+ N1 *=a_shape[ax]
+ # newshape_a is the shape to do multiplication.For example, A is in shape(3,2,4),
+ #B is in shape(4,2,5), we set axes as ([1,2],[1,0]), then newshape_a should be (3,8)
+ #olda is the shape that will be shown in the result.
+ newshape_a = (N1,N2)
+ olda = [a_shape[axis] for axis in notin]
+ notin = [k for k in range(ndb) if k not in axes_b]
+ newaxes_b = axes_b + notin
+ N2 = 1
+ for axis in axes_b:
+ N2 *= b_shape[axis]
+ N1 = 1
+ for ax in notin:
+ N1 *=a_shape[ax]
+ newshape_b = (N2, N1)
+ oldb = [b_shape[axis] for axis in notin]
+ # do transpose and reshape to get the 2D matrix to do multiplication
+ at = a.transpose(newaxes_a).reshape(newshape_a)
+ bt = b.transpose(newaxes_b).reshape(newshape_b)
+ res = mult(a, b)
+ #reshape the result
+ return res.reshape(olda + oldb)
def div(lhs, rhs, ret=None):
'''Elementi-wise division.
[08/12] incubator-singa git commit: SINGA-347 Create a function that
supports einsum 1. revise to make it similar to numpy.einsum 2. write an unit
test for einsum
Posted by wa...@apache.org.
SINGA-347 Create a function that supports einsum
1. revise to make it similar to numpy.einsum
2. write an unit test for einsum
Project: http://git-wip-us.apache.org/repos/asf/incubator-singa/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-singa/commit/c037e2f9
Tree: http://git-wip-us.apache.org/repos/asf/incubator-singa/tree/c037e2f9
Diff: http://git-wip-us.apache.org/repos/asf/incubator-singa/diff/c037e2f9
Branch: refs/heads/master
Commit: c037e2f9468cd77a9bb8f11988a1c3cb5c6e16a3
Parents: fa1b023
Author: sheyujian <sh...@me.com>
Authored: Sun Apr 29 12:16:54 2018 +0800
Committer: sheyujian <sh...@me.com>
Committed: Sun Apr 29 12:16:54 2018 +0800
----------------------------------------------------------------------
.idea/dictionaries/sheyujian.xml | 3 +
.idea/incubator-singa.iml | 19 ++
.idea/misc.xml | 4 +
.idea/modules.xml | 8 +
.idea/vcs.xml | 6 +
.idea/workspace.xml | 342 ++++++++++++++++++++++++++++++++++
python/singa/tensor.py | 73 +++++++-
test/python/test_tensor.py | 12 ++
8 files changed, 464 insertions(+), 3 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/c037e2f9/.idea/dictionaries/sheyujian.xml
----------------------------------------------------------------------
diff --git a/.idea/dictionaries/sheyujian.xml b/.idea/dictionaries/sheyujian.xml
new file mode 100644
index 0000000..ac14bc9
--- /dev/null
+++ b/.idea/dictionaries/sheyujian.xml
@@ -0,0 +1,3 @@
+<component name="ProjectDictionaryState">
+ <dictionary name="sheyujian" />
+</component>
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/c037e2f9/.idea/incubator-singa.iml
----------------------------------------------------------------------
diff --git a/.idea/incubator-singa.iml b/.idea/incubator-singa.iml
new file mode 100644
index 0000000..eac2c49
--- /dev/null
+++ b/.idea/incubator-singa.iml
@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module type="PYTHON_MODULE" version="4">
+ <component name="NewModuleRootManager">
+ <content url="file://$MODULE_DIR$" />
+ <orderEntry type="inheritedJdk" />
+ <orderEntry type="sourceFolder" forTests="false" />
+ </component>
+ <component name="TemplatesService">
+ <option name="TEMPLATE_CONFIGURATION" value="Jinja2" />
+ <option name="TEMPLATE_FOLDERS">
+ <list>
+ <option value="$MODULE_DIR$/doc/en/_templates" />
+ </list>
+ </option>
+ </component>
+ <component name="TestRunnerService">
+ <option name="PROJECT_TEST_RUNNER" value="Unittests" />
+ </component>
+</module>
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/c037e2f9/.idea/misc.xml
----------------------------------------------------------------------
diff --git a/.idea/misc.xml b/.idea/misc.xml
new file mode 100644
index 0000000..bb831c4
--- /dev/null
+++ b/.idea/misc.xml
@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+ <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.5 (untitled)" project-jdk-type="Python SDK" />
+</project>
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/c037e2f9/.idea/modules.xml
----------------------------------------------------------------------
diff --git a/.idea/modules.xml b/.idea/modules.xml
new file mode 100644
index 0000000..b066728
--- /dev/null
+++ b/.idea/modules.xml
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+ <component name="ProjectModuleManager">
+ <modules>
+ <module fileurl="file://$PROJECT_DIR$/.idea/incubator-singa.iml" filepath="$PROJECT_DIR$/.idea/incubator-singa.iml" />
+ </modules>
+ </component>
+</project>
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/c037e2f9/.idea/vcs.xml
----------------------------------------------------------------------
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
new file mode 100644
index 0000000..94a25f7
--- /dev/null
+++ b/.idea/vcs.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+ <component name="VcsDirectoryMappings">
+ <mapping directory="$PROJECT_DIR$" vcs="Git" />
+ </component>
+</project>
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/c037e2f9/.idea/workspace.xml
----------------------------------------------------------------------
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
new file mode 100644
index 0000000..95e5f35
--- /dev/null
+++ b/.idea/workspace.xml
@@ -0,0 +1,342 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+ <component name="ChangeListManager">
+ <list default="true" id="33c7ea95-5465-40ee-9c51-dfee0bfee69f" name="Default" comment="">
+ <change beforePath="" afterPath="$PROJECT_DIR$/.idea/dictionaries/sheyujian.xml" />
+ <change beforePath="" afterPath="$PROJECT_DIR$/.idea/incubator-singa.iml" />
+ <change beforePath="" afterPath="$PROJECT_DIR$/.idea/misc.xml" />
+ <change beforePath="" afterPath="$PROJECT_DIR$/.idea/modules.xml" />
+ <change beforePath="" afterPath="$PROJECT_DIR$/.idea/vcs.xml" />
+ <change beforePath="" afterPath="$PROJECT_DIR$/.idea/workspace.xml" />
+ <change beforePath="$PROJECT_DIR$/python/singa/tensor.py" afterPath="$PROJECT_DIR$/python/singa/tensor.py" />
+ <change beforePath="$PROJECT_DIR$/test/python/test_tensor.py" afterPath="$PROJECT_DIR$/test/python/test_tensor.py" />
+ </list>
+ <option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
+ <option name="TRACKING_ENABLED" value="true" />
+ <option name="SHOW_DIALOG" value="false" />
+ <option name="HIGHLIGHT_CONFLICTS" value="true" />
+ <option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
+ <option name="LAST_RESOLUTION" value="IGNORE" />
+ </component>
+ <component name="CoverageDataManager">
+ <SUITE FILE_PATH="coverage/incubator_singa$singa.coverage" NAME="singa Coverage Results" MODIFIED="1524818212622" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="" />
+ </component>
+ <component name="FileEditorManager">
+ <leaf SIDE_TABS_SIZE_LIMIT_KEY="300">
+ <file leaf-file-name="setup.py.in" pinned="false" current-in-tab="false">
+ <entry file="file://$PROJECT_DIR$/python/setup.py.in">
+ <provider selected="true" editor-type-id="text-editor">
+ <state relative-caret-position="0">
+ <caret line="0" column="0" lean-forward="false" selection-start-line="0" selection-start-column="0" selection-end-line="0" selection-end-column="0" />
+ <folding />
+ </state>
+ </provider>
+ </entry>
+ </file>
+ <file leaf-file-name="tensor.py" pinned="false" current-in-tab="true">
+ <entry file="file://$PROJECT_DIR$/python/singa/tensor.py">
+ <provider selected="true" editor-type-id="text-editor">
+ <state relative-caret-position="435">
+ <caret line="1036" column="15" lean-forward="true" selection-start-line="1036" selection-start-column="15" selection-end-line="1036" selection-end-column="15" />
+ <folding />
+ </state>
+ </provider>
+ </entry>
+ </file>
+ <file leaf-file-name="test_tensor.py" pinned="false" current-in-tab="false">
+ <entry file="file://$PROJECT_DIR$/test/python/test_tensor.py">
+ <provider selected="true" editor-type-id="text-editor">
+ <state relative-caret-position="432">
+ <caret line="167" column="26" lean-forward="true" selection-start-line="167" selection-start-column="26" selection-end-line="167" selection-end-column="26" />
+ <folding>
+ <element signature="e#865#896#0" expanded="true" />
+ </folding>
+ </state>
+ </provider>
+ </entry>
+ </file>
+ <file leaf-file-name="layer.py" pinned="false" current-in-tab="false">
+ <entry file="file://$PROJECT_DIR$/python/singa/layer.py">
+ <provider selected="true" editor-type-id="text-editor">
+ <state relative-caret-position="527">
+ <caret line="1310" column="31" lean-forward="true" selection-start-line="1310" selection-start-column="31" selection-end-line="1310" selection-end-column="31" />
+ <folding />
+ </state>
+ </provider>
+ </entry>
+ </file>
+ </leaf>
+ </component>
+ <component name="Git.Settings">
+ <option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
+ </component>
+ <component name="IdeDocumentHistory">
+ <option name="CHANGED_PATHS">
+ <list>
+ <option value="$PROJECT_DIR$/test/python/test_tensor.py" />
+ <option value="$PROJECT_DIR$/python/singa/tensor.py" />
+ </list>
+ </option>
+ </component>
+ <component name="JsBuildToolGruntFileManager" detection-done="true" sorting="DEFINITION_ORDER" />
+ <component name="JsBuildToolPackageJson" detection-done="true" sorting="DEFINITION_ORDER" />
+ <component name="JsGulpfileManager">
+ <detection-done>true</detection-done>
+ <sorting>DEFINITION_ORDER</sorting>
+ </component>
+ <component name="ProjectFrameBounds">
+ <option name="x" value="55" />
+ <option name="y" value="63" />
+ <option name="width" value="1400" />
+ <option name="height" value="790" />
+ </component>
+ <component name="ProjectInspectionProfilesVisibleTreeState">
+ <entry key="Project Default">
+ <profile-state>
+ <expanded-state>
+ <State>
+ <id />
+ </State>
+ </expanded-state>
+ <selected-state>
+ <State>
+ <id>AngularJS</id>
+ </State>
+ </selected-state>
+ </profile-state>
+ </entry>
+ </component>
+ <component name="ProjectView">
+ <navigator currentView="ProjectPane" proportions="" version="1">
+ <flattenPackages />
+ <showMembers />
+ <showModules />
+ <showLibraryContents />
+ <hideEmptyPackages />
+ <abbreviatePackageNames />
+ <autoscrollToSource />
+ <autoscrollFromSource />
+ <sortByType />
+ <manualOrder />
+ <foldersAlwaysOnTop value="true" />
+ </navigator>
+ <panes>
+ <pane id="ProjectPane">
+ <subPane>
+ <expand>
+ <path>
+ <item name="incubator-singa" type="b2602c69:ProjectViewProjectNode" />
+ <item name="incubator-singa" type="462c0819:PsiDirectoryNode" />
+ </path>
+ <path>
+ <item name="incubator-singa" type="b2602c69:ProjectViewProjectNode" />
+ <item name="incubator-singa" type="462c0819:PsiDirectoryNode" />
+ <item name="python" type="462c0819:PsiDirectoryNode" />
+ </path>
+ <path>
+ <item name="incubator-singa" type="b2602c69:ProjectViewProjectNode" />
+ <item name="incubator-singa" type="462c0819:PsiDirectoryNode" />
+ <item name="python" type="462c0819:PsiDirectoryNode" />
+ <item name="singa" type="462c0819:PsiDirectoryNode" />
+ </path>
+ <path>
+ <item name="incubator-singa" type="b2602c69:ProjectViewProjectNode" />
+ <item name="incubator-singa" type="462c0819:PsiDirectoryNode" />
+ <item name="src" type="462c0819:PsiDirectoryNode" />
+ </path>
+ <path>
+ <item name="incubator-singa" type="b2602c69:ProjectViewProjectNode" />
+ <item name="incubator-singa" type="462c0819:PsiDirectoryNode" />
+ <item name="test" type="462c0819:PsiDirectoryNode" />
+ </path>
+ <path>
+ <item name="incubator-singa" type="b2602c69:ProjectViewProjectNode" />
+ <item name="incubator-singa" type="462c0819:PsiDirectoryNode" />
+ <item name="test" type="462c0819:PsiDirectoryNode" />
+ <item name="python" type="462c0819:PsiDirectoryNode" />
+ </path>
+ </expand>
+ <select />
+ </subPane>
+ </pane>
+ <pane id="Scope" />
+ <pane id="Scratches" />
+ </panes>
+ </component>
+ <component name="PropertiesComponent">
+ <property name="nodejs_interpreter_path.stuck_in_default_project" value="undefined stuck path" />
+ <property name="settings.editor.selected.configurable" value="settings.typescriptcompiler" />
+ <property name="WebServerToolWindowFactoryState" value="false" />
+ <property name="last_opened_file_path" value="$PROJECT_DIR$" />
+ </component>
+ <component name="RunDashboard">
+ <option name="ruleStates">
+ <list>
+ <RuleState>
+ <option name="name" value="ConfigurationTypeDashboardGroupingRule" />
+ </RuleState>
+ <RuleState>
+ <option name="name" value="StatusDashboardGroupingRule" />
+ </RuleState>
+ </list>
+ </option>
+ </component>
+ <component name="RunManager">
+ <configuration name="singa" type="PythonConfigurationType" factoryName="Python">
+ <option name="INTERPRETER_OPTIONS" value="" />
+ <option name="PARENT_ENVS" value="true" />
+ <envs>
+ <env name="PYTHONUNBUFFERED" value="1" />
+ </envs>
+ <option name="SDK_HOME" value="$PROJECT_DIR$/../PycharmProjects/untitled/venv/bin/python" />
+ <option name="WORKING_DIRECTORY" value="" />
+ <option name="IS_MODULE_SDK" value="false" />
+ <option name="ADD_CONTENT_ROOTS" value="true" />
+ <option name="ADD_SOURCE_ROOTS" value="true" />
+ <module name="incubator-singa" />
+ <EXTENSION ID="PythonCoverageRunConfigurationExtension" enabled="false" sample_coverage="true" runner="coverage.py" />
+ <option name="SCRIPT_NAME" value="" />
+ <option name="PARAMETERS" value="" />
+ <option name="SHOW_COMMAND_LINE" value="false" />
+ <option name="EMULATE_TERMINAL" value="false" />
+ <option name="MODULE_MODE" value="false" />
+ </configuration>
+ </component>
+ <component name="ShelveChangesManager" show_recycled="false">
+ <option name="remove_strategy" value="false" />
+ </component>
+ <component name="SvnConfiguration">
+ <configuration />
+ </component>
+ <component name="TaskManager">
+ <task active="true" id="Default" summary="Default task">
+ <changelist id="33c7ea95-5465-40ee-9c51-dfee0bfee69f" name="Default" comment="" />
+ <created>1524817684476</created>
+ <option name="number" value="Default" />
+ <option name="presentableId" value="Default" />
+ <updated>1524817684476</updated>
+ </task>
+ <servers />
+ </component>
+ <component name="ToolWindowManager">
+ <frame x="55" y="63" width="1400" height="790" extended-state="0" />
+ <editor active="true" />
+ <layout>
+ <window_info id="TODO" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="6" side_tool="false" content_ui="tabs" />
+ <window_info id="Event Log" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="7" side_tool="true" content_ui="tabs" />
+ <window_info id="Run" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.32729805" sideWeight="0.5" order="2" side_tool="false" content_ui="tabs" />
+ <window_info id="Version Control" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="7" side_tool="false" content_ui="tabs" />
+ <window_info id="Python Console" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="7" side_tool="false" content_ui="tabs" />
+ <window_info id="Terminal" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="7" side_tool="false" content_ui="tabs" />
+ <window_info id="Project" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" show_stripe_button="true" weight="0.25" sideWeight="0.5" order="0" side_tool="false" content_ui="combo" />
+ <window_info id="Docker" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="false" weight="0.33" sideWeight="0.5" order="7" side_tool="false" content_ui="tabs" />
+ <window_info id="Database" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="3" side_tool="false" content_ui="tabs" />
+ <window_info id="SciView" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="3" side_tool="false" content_ui="tabs" />
+ <window_info id="Structure" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.25" sideWeight="0.5" order="1" side_tool="false" content_ui="tabs" />
+ <window_info id="Debug" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.4" sideWeight="0.5" order="3" side_tool="false" content_ui="tabs" />
+ <window_info id="Favorites" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="2" side_tool="true" content_ui="tabs" />
+ <window_info id="Cvs" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.25" sideWeight="0.5" order="4" side_tool="false" content_ui="tabs" />
+ <window_info id="Message" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="0" side_tool="false" content_ui="tabs" />
+ <window_info id="Commander" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.4" sideWeight="0.5" order="0" side_tool="false" content_ui="tabs" />
+ <window_info id="Inspection" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.4" sideWeight="0.5" order="5" side_tool="false" content_ui="tabs" />
+ <window_info id="Hierarchy" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.25" sideWeight="0.5" order="2" side_tool="false" content_ui="combo" />
+ <window_info id="Find" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="1" side_tool="false" content_ui="tabs" />
+ <window_info id="Ant Build" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.25" sideWeight="0.5" order="1" side_tool="false" content_ui="tabs" />
+ </layout>
+ </component>
+ <component name="TypeScriptGeneratedFilesManager">
+ <option name="version" value="1" />
+ </component>
+ <component name="VcsContentAnnotationSettings">
+ <option name="myLimit" value="2678400000" />
+ </component>
+ <component name="XDebuggerManager">
+ <breakpoint-manager />
+ <watches-manager />
+ </component>
+ <component name="editorHistoryManager">
+ <entry file="file://$PROJECT_DIR$/python/setup.py.in">
+ <provider selected="true" editor-type-id="text-editor">
+ <state relative-caret-position="0">
+ <caret line="0" column="0" lean-forward="false" selection-start-line="0" selection-start-column="0" selection-end-line="0" selection-end-column="0" />
+ <folding />
+ </state>
+ </provider>
+ </entry>
+ <entry file="file://$PROJECT_DIR$/python/singa/tensor.py">
+ <provider selected="true" editor-type-id="text-editor">
+ <state relative-caret-position="14940">
+ <caret line="996" column="41" lean-forward="true" selection-start-line="996" selection-start-column="41" selection-end-line="996" selection-end-column="41" />
+ <folding />
+ </state>
+ </provider>
+ </entry>
+ <entry file="file://$PROJECT_DIR$/python/singa/layer.py">
+ <provider selected="true" editor-type-id="text-editor">
+ <state relative-caret-position="19500">
+ <caret line="1310" column="31" lean-forward="true" selection-start-line="1310" selection-start-column="31" selection-end-line="1310" selection-end-column="31" />
+ <folding />
+ </state>
+ </provider>
+ </entry>
+ <entry file="file://$PROJECT_DIR$/test/python/test_tensor.py">
+ <provider selected="true" editor-type-id="text-editor">
+ <state relative-caret-position="675">
+ <caret line="53" column="62" lean-forward="true" selection-start-line="53" selection-start-column="62" selection-end-line="53" selection-end-column="62" />
+ <folding>
+ <element signature="e#865#896#0" expanded="true" />
+ </folding>
+ </state>
+ </provider>
+ </entry>
+ <entry file="file://$PROJECT_DIR$/python/setup.py.in">
+ <provider selected="true" editor-type-id="text-editor">
+ <state relative-caret-position="0">
+ <caret line="0" column="0" lean-forward="false" selection-start-line="0" selection-start-column="0" selection-end-line="0" selection-end-column="0" />
+ <folding />
+ </state>
+ </provider>
+ </entry>
+ <entry file="file://$PROJECT_DIR$/python/singa/layer.py">
+ <provider selected="true" editor-type-id="text-editor">
+ <state relative-caret-position="527">
+ <caret line="1310" column="31" lean-forward="true" selection-start-line="1310" selection-start-column="31" selection-end-line="1310" selection-end-column="31" />
+ <folding />
+ </state>
+ </provider>
+ </entry>
+ <entry file="file://$PROJECT_DIR$/test/python/test_tensor.py">
+ <provider selected="true" editor-type-id="text-editor">
+ <state relative-caret-position="432">
+ <caret line="167" column="26" lean-forward="true" selection-start-line="167" selection-start-column="26" selection-end-line="167" selection-end-column="26" />
+ <folding>
+ <element signature="e#865#896#0" expanded="true" />
+ </folding>
+ </state>
+ </provider>
+ </entry>
+ <entry file="file://$PROJECT_DIR$/python/singa/tensor.py">
+ <provider selected="true" editor-type-id="text-editor">
+ <state relative-caret-position="435">
+ <caret line="1036" column="15" lean-forward="true" selection-start-line="1036" selection-start-column="15" selection-end-line="1036" selection-end-column="15" />
+ <folding />
+ </state>
+ </provider>
+ </entry>
+ </component>
+ <component name="masterDetails">
+ <states>
+ <state key="ScopeChooserConfigurable.UI">
+ <settings>
+ <splitter-proportions>
+ <option name="proportions">
+ <list>
+ <option value="0.2" />
+ </list>
+ </option>
+ </splitter-proportions>
+ </settings>
+ </state>
+ </states>
+ </component>
+</project>
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/c037e2f9/python/singa/tensor.py
----------------------------------------------------------------------
diff --git a/python/singa/tensor.py b/python/singa/tensor.py
index caf5998..98a3db9 100644
--- a/python/singa/tensor.py
+++ b/python/singa/tensor.py
@@ -941,7 +941,7 @@ def mult(A, B, C=None, alpha=1.0, beta=0.0):
-def einsum(A,B,ops):
+def einsum(ops,*args):
'''
function TODO list to finish the function in cpp(just like numpy function):
@@ -951,24 +951,91 @@ def einsum(A,B,ops):
Do the matrix to matrix einsum calculation according to the operands
+ Warning : this function could only support two matrix' einsum calcultion
+
Args:
- A (Singa.Tensor): The first argument.
- B (Singa.Tensor): The second argument.
+
ops(string):
the string specifies the subscripts for summation such as 'ki,kj->kij'
+ Here all the 26 lowercase letter can be used here.
+ arg(list of array_like):
+ These are the tensors for the operation,but here only support two tensors.
Returns: Singa.Tensor
the output matirx of the einsum calculation
+
+ The best way to understand this function is to try the examples below:
+
+ A_ = [0,1,2,3,4,5,6,7,8,9,10,11]
+ A = A_.reshape(4,3)
+ B = A_.reshape(3,4)
+ Here this einsum calculation is the same as normal 'mult'
+ Res = einsum('ij,jk->ik',A,B)
+ >>> [[ 20 23 26 29]
+ [ 56 68 80 92]
+ [ 92 113 134 155]
+ [128 158 188 218]]
+
+
+ A_ = [0,1,2,3,4,5,6,7,8,9,10,11]
+ A = A_.reshape(4,3)
+ B = A_.reshape(4,3)
+ Here the einsum calculation is the same as normol 'eltwise_mult'
+ Res = einsum('ki,ki->ki',A,B)
+ >>> [[ 0 1 4]
+ [ 9 16 25]
+ [ 36 49 64]
+ [ 81 100 121]]
+
+
+ A = [0,1,2,3,4,5,6,7,8,9,10,11]
+ A = A.reshape(4,3)
+ Res = einsum('ki,kj->kij',A,A)
+ >>> [[[ 0 0 0]
+ [ 0 1 2]
+ [ 0 2 4]]
+
+ [[ 9 12 15]
+ [ 12 16 20]
+ [ 15 20 25]]
+
+ [[ 36 42 48]
+ [ 42 49 56]
+ [ 48 56 64]]
+
+ [[ 81 90 99]
+ [ 90 100 110]
+ [ 99 110 121]]]
+
+
+ A_ = [0,1,2,3,4,5,6,7,8,9,10,11]
+ A = A_.reshape(3,2,2)
+ Res = einsum('ki,kj->kij',A,A)
+ >>> [[[ 1 3]
+ [ 3 13]]
+
+ [[ 41 59]
+ [ 59 85]]
+
+ [[145 179]
+ [179 221]]]
+
'''
if len(ops) == 0:
raise ValueError("No input operands")
+ if len(args) != 2:
+ raise ValueError("Currently only two operands are supported")
# to get the input and output ops
inputops, outputops = ops.split('->')
inputops = inputops.split(',')
+ # to get the two input tensor
+ A = args[0]
+ B = args[1]
+
if A.ndim != len(inputops[0]) or B.ndim != len(inputops[1]):
raise ValueError("input dim doesn't match operands")
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/c037e2f9/test/python/test_tensor.py
----------------------------------------------------------------------
diff --git a/test/python/test_tensor.py b/test/python/test_tensor.py
index dc3ff13..f952019 100644
--- a/test/python/test_tensor.py
+++ b/test/python/test_tensor.py
@@ -165,6 +165,18 @@ class TestTensorMethods(unittest.TestCase):
b = tensor.to_numpy(t)
self.assertEqual(np.sum(a-b), 0.)
+ def test_einsum(self):
+ a = np.arange(12).reshape(3,2,2)
+ ta = tensor.from_numpy(a)
+ res1 = np.einsum('kij,kij->kij',a,a)
+ tres1 = tensor.einsum('kij,kij->kij',ta,ta)
+ res2 = np.einsum('kij,kih->kjh',a,a)
+ tres2 = tensor.einsum('kij,kih->kjh',ta,ta)
+
+ self.assertEqual(np.sum(tres1 - res1), 0.)
+ self.assertEqual(np.sum(tres2 - res2), 0.)
+
+
if __name__ == '__main__':
unittest.main()
[02/12] incubator-singa git commit: SINGA-347 Create a function that
supports einsum 1.provide the diag() function which could make the matrix
diagonalized along the given axis 2.provide some comments for some existing
function 3. to do: provide complete
Posted by wa...@apache.org.
SINGA-347 Create a function that supports einsum
1.provide the diag() function which could make the matrix diagonalized along the given axis
2.provide some comments for some existing function
3. to do: provide complete comments and fix the codes. It still needs test under Singa environment
SINGA-347 Create a function that supports einsum
fix one bug
Project: http://git-wip-us.apache.org/repos/asf/incubator-singa/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-singa/commit/b09aed3e
Tree: http://git-wip-us.apache.org/repos/asf/incubator-singa/tree/b09aed3e
Diff: http://git-wip-us.apache.org/repos/asf/incubator-singa/diff/b09aed3e
Branch: refs/heads/master
Commit: b09aed3e395b3bbf1eb32badaf1f8af19e0799e8
Parents: f595f10
Author: sheyujian <sh...@me.com>
Authored: Wed Apr 18 13:41:54 2018 +0800
Committer: sheyujian <sh...@me.com>
Committed: Wed Apr 18 13:56:27 2018 +0800
----------------------------------------------------------------------
python/singa/tensor.py | 128 +++++++++++++++++++++++++++++++++-----------
1 file changed, 97 insertions(+), 31 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/b09aed3e/python/singa/tensor.py
----------------------------------------------------------------------
diff --git a/python/singa/tensor.py b/python/singa/tensor.py
index fde1667..e81f3ab 100644
--- a/python/singa/tensor.py
+++ b/python/singa/tensor.py
@@ -947,8 +947,8 @@ def tensordot(A, B, axes=2):
are treated as one axis by reshaping.
Args:
- a (Singa.Tensor): The first argument.
- b (Singa.Tensor): The second argument.
+ A (Singa.Tensor): The first argument.
+ B (Singa.Tensor): The second argument.
axes:
- If it is an integer, then ''axes'' represent axes at the last of ''a`'' and
the first of ''b'' are used.
@@ -961,6 +961,7 @@ def tensordot(A, B, axes=2):
axes specified by ''axes''.
Thanks to numpy.tensordot.
+ the link is https://github.com/numpy/numpy/blob/v1.14.0/numpy/core/numeric.py#L1123-L1306
"""
# when axes is an integer, axes_A and axes_B represent axes at the last of ''a`'' and
# the first of ''b''. For example, when axes is 1, we do the normal multiplication :
@@ -971,16 +972,16 @@ def tensordot(A, B, axes=2):
axes_B = list(range(0, axes))
axes_B = axes_B
else:
- axes_A, axes_B = axes
+ axes_A,axes_B =axes
# when axes is a pair of sequences of integers.For example, A is in shape(3,2,4),
- # B is in shape(4,2,5), we set axes as ([1,2],[1,0]), it will return a matrix in shape(3,5)
- if isinstance(axes_A, list):
+ #B is in shape(4,2,5), we set axes as ([1,2],[1,0]), it will return a matrix in shape(3,5)
+ if isinstance(axes_A,list):
na = len(axes_A)
axes_A = list(axes_A)
else:
axes_A = [axes_A]
na = 1
- if isinstance(axes_B, list):
+ if isinstance(axes_B,list):
nb = len(axes_B)
axes_B = list(axes_B)
else:
@@ -997,7 +998,7 @@ def tensordot(A, B, axes=2):
if na != nb:
equal = False
else:
- # to make the shape match
+ # to make the shape match
for k in range(na):
if a_shape[axes_A[k]] != b_shape[axes_B[k]]:
equal = False
@@ -1018,12 +1019,13 @@ def tensordot(A, B, axes=2):
N2 *= a_shape[axis]
N1 = 1
for ax in notin:
- N1 *= a_shape[ax]
- # newshape_a is the shape to do multiplication.For example, A is in shape(3,2,4),
- # B is in shape(4,2,5), we set axes as ([1,2],[1,0]), then newshape_a should be (3,5)
- # olda is the shape that will be shown in the result.
- newshape_a = (N1, N2)
+ N1 *=a_shape[ax]
+ #newshape_a is the shape to do multiplication.For example, A is in shape(3,2,4),
+ #B is in shape(4,2,5), we set axes as ([1,2],[1,0]), then newshape_a should be (3,5)
+ #olda is the shape that will be shown in the result.
+ newshape_a = (N1,N2)
olda = [a_shape[axis] for axis in notin]
+ notin = [k for k in range(ndb) if k not in axes_B]
newaxes_b = axes_B + notin
N2 = 1
for axis in axes_B:
@@ -1034,42 +1036,69 @@ def tensordot(A, B, axes=2):
newshape_b = (N2, N1)
oldb = [b_shape[axis] for axis in notin]
# do transpose and reshape to get the 2D matrix to do multiplication
- at = a.transpose(newaxes_a).reshape(newshape_a)
- bt = b.transpose(newaxes_b).reshape(newshape_b)
+ at = A.transpose(newaxes_a).reshape(newshape_a)
+ bt = B.transpose(newaxes_b).reshape(newshape_b)
res = mult(at, bt)
- # reshape the result
+ #reshape the result
return res.reshape(olda + oldb)
-def einsum(A,B,ops):
- '''
- Thanks to nils-werner/sparse.einsum()
+def einsum_(A,B,ops):
+ '''Do the matrix to matrix einsum calculation according to the operands
+
+ Args:
+ A (Singa.Tensor): The first argument.
+ B (Singa.Tensor): The second argument.
+ ops(string):
+ the string specifies the subscripts for summation such as 'ki,kj->kij'
+
+ Returns: Singa.Tensor
+ the output matirx of the einsum calculation
+ Thanks to nils-werner/sparse.einsum(),
+ the link is https://github.com/nils-werner/sparse/commit/449c75d21d3158630bc5be79c690a60cfc002578
'''
if len(ops) == 0:
raise ValueError("No input operands")
-
- nputops, outputops = ops.split('->')
+ # to get the input and output ops
+ inputops, outputops = ops.split('->')
inputops = inputops.split(',')
+ if A.ndim != len(inputops[0]) or B.ndim != len(inputops[1]):
+ raise ValueError("input dim doesn't match operands")
+
# All indices that are in input AND in output are multiplies
- multiplies = sorted(list(set(inputops[0]) & set(inputops[1]) & set(outputops)))
# All indices that are in input BUT NOT in output are sum contractions
+ multiplies = sorted(list(set(inputops[0]) & set(inputops[1]) & set(outputops)))
sums = sorted(list((set(inputops[0]) & set(inputops[1])) - set(outputops)))
# Map sums and indices to axis integers
- multiplies = [[inop.find(x) for x in multiplies] for inop in inputops]
+ multiplies = [[inops.find(x) for x in multiplies] for inops in inputops]
+ sums = [[inops.find(x) for x in sums] for inops in inputops]
+
- sums = [[inop.find(x) for x in sums] for inop in inputops]
# Find output axes in input axes for final transpose
- # Values very likely lie outside of output tensor shape, so
- # just map them values to their rank (index in ordered list)
transpose = [''.join(inputops).find(x) for x in outputops]
+ #to make the transpose match to its rank
transpose = sorted(range(len(transpose)), key = transpose.__getitem__)
- return tensormult(A,B, sum=sums, multiply=multiplies).transpose(transpose)
+ return tensormult(A,B,sums,multiplies).transpose(transpose)
+
-def tensordotmult(A, B, sum=None, multiply=None):
+def tensormult(A, B, sum=None, multiply=None):
+ '''
+ Args:
+ A (Singa.Tensor): The first input tensor from einsum.
+ B (Singa.Tensor): The second input tensor from einsum.
+ sum (list[list[int]]):
+ The axis to do the normal tensordot calculation
+
+ multiply (list[list[int]]):
+ The axis to multiply
+
+ Return: (Singa.Tensor)
+ The output of the tensormult calculation
+ '''
if sum is None:
sum = [[], []]
else:
@@ -1079,14 +1108,15 @@ def tensordotmult(A, B, sum=None, multiply=None):
multiply = [[], []]
else:
multiply = list(multiply)
-
# For each multiply[0] we are adding one axis, thus we need to increment
# all following items by one: (0, 1, 2) -> (0, 2, 4)
idx = multipliessort(multiply[0])
+
post_multiply = multiply[0]
for i, v in enumerate(idx):
post_multiply[v] += i
+
for i in post_multiply:
A = diag(A,i)
@@ -1096,6 +1126,15 @@ def tensordotmult(A, B, sum=None, multiply=None):
return tensordot(A, B, axes=sum)
def multipliessort(multiplies):
+ '''
+ Returns the indices that would sort an array
+ Args:
+ multiplies(list[int]):
+ the list of the input multiplies
+
+ Return(list[int]):
+ the indices list
+ '''
if multiplies is None:
multiplies = []
@@ -1106,9 +1145,36 @@ def multipliessort(multiplies):
idx = [x[0] for x in sort_multiplies]
return idx
-def diag(A,axis = -1):
- A_diag = clone(A)
- '''sheyujian todo: to make a tensor's axis to be diagonalization'''
+def diag(A,axis=-1):
+ '''
+ Return the matrix that is diagonalize along the axis
+ Args:
+ A (Singa.Tensor):the input tensor
+ axis (int):
+ the axis which the matrix use to diagonalization
+ Return(Singa.Tensor):
+ the tensor which has been diagonalize along the given axis
+
+
+ It is like using numpy.diag() in the 1D condition, but this function can be used to do high dimension
+ matrix diagonalization
+
+ '''
+ # to get the shape of the diagonalize matirx
+ axis_diag = list(range(out.ndim))[axis]
+ shape = list(A.shape)
+ shape.insert(axis_diag,shape[axis_diag])
+
+ npA = to_numpy(A)
+ npA_diag = np.zeros(product(shape))
+ index = np.argwhere(npA != 0)
+ for i in index:
+ i_diag = list(i)
+ i_diag.insert(axis, i[axis])
+ index_npA = [[i[x]] for x in range(len(i))]
+ index_npA_diag = [[i_diag[x]] for x in range(len(i_diag))]
+ npA_diag[index_npA_diag] = npA[index_npA]
+ A_diag = from_numpy(npA_diag)
return A_diag
[12/12] incubator-singa git commit: Merge branch 'PR355'
Posted by wa...@apache.org.
Merge branch 'PR355'
Project: http://git-wip-us.apache.org/repos/asf/incubator-singa/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-singa/commit/08675e3a
Tree: http://git-wip-us.apache.org/repos/asf/incubator-singa/tree/08675e3a
Diff: http://git-wip-us.apache.org/repos/asf/incubator-singa/diff/08675e3a
Branch: refs/heads/master
Commit: 08675e3a5d67f77a3bcd421da9a3829614363996
Parents: 9090160 f548fa3
Author: Wei Wang <wa...@comp.nus.edu.sg>
Authored: Sun Apr 29 23:31:03 2018 +0800
Committer: Wei Wang <wa...@comp.nus.edu.sg>
Committed: Sun Apr 29 23:31:03 2018 +0800
----------------------------------------------------------------------
.gitignore | 1 +
python/singa/tensor.py | 142 ++++++++++++++++++++++++++++++++++++++++
test/python/test_tensor.py | 12 ++++
3 files changed, 155 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/08675e3a/python/singa/tensor.py
----------------------------------------------------------------------
[11/12] incubator-singa git commit: SINGA-347 Create a function that
supports einsum revise the example
Posted by wa...@apache.org.
SINGA-347 Create a function that supports einsum
revise the example
Project: http://git-wip-us.apache.org/repos/asf/incubator-singa/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-singa/commit/f548fa34
Tree: http://git-wip-us.apache.org/repos/asf/incubator-singa/tree/f548fa34
Diff: http://git-wip-us.apache.org/repos/asf/incubator-singa/diff/f548fa34
Branch: refs/heads/master
Commit: f548fa343d7057316b432ce36d743ec0c113c339
Parents: 4f5f902
Author: sheyujian <sh...@me.com>
Authored: Sun Apr 29 13:48:27 2018 +0800
Committer: sheyujian <sh...@me.com>
Committed: Sun Apr 29 13:48:27 2018 +0800
----------------------------------------------------------------------
python/singa/tensor.py | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/f548fa34/python/singa/tensor.py
----------------------------------------------------------------------
diff --git a/python/singa/tensor.py b/python/singa/tensor.py
index 98a3db9..bca5b2f 100644
--- a/python/singa/tensor.py
+++ b/python/singa/tensor.py
@@ -1010,7 +1010,7 @@ def einsum(ops,*args):
A_ = [0,1,2,3,4,5,6,7,8,9,10,11]
A = A_.reshape(3,2,2)
- Res = einsum('ki,kj->kij',A,A)
+ Res = einsum('kia,kja->kij',A,A)
>>> [[[ 1 3]
[ 3 13]]
[04/12] incubator-singa git commit: SINGA-347 Create a function that
supports einsum 1.test the tensordot function and fix some error in the
function 2.tweak the code to be more readable and fix the some error in the
comment
Posted by wa...@apache.org.
SINGA-347 Create a function that supports einsum
1.test the tensordot function and fix some error in the function
2.tweak the code to be more readable and fix the some error in the comment
Project: http://git-wip-us.apache.org/repos/asf/incubator-singa/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-singa/commit/9a3ce585
Tree: http://git-wip-us.apache.org/repos/asf/incubator-singa/tree/9a3ce585
Diff: http://git-wip-us.apache.org/repos/asf/incubator-singa/diff/9a3ce585
Branch: refs/heads/master
Commit: 9a3ce585e468f4f5f17b8ea82658baf9d6ccd2aa
Parents: e27498d
Author: sheyujian <sh...@me.com>
Authored: Fri Apr 13 14:18:08 2018 +0800
Committer: sheyujian <sh...@me.com>
Committed: Wed Apr 18 13:56:27 2018 +0800
----------------------------------------------------------------------
python/singa/tensor.py | 85 +++++++++++++++++++++++----------------------
1 file changed, 43 insertions(+), 42 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/9a3ce585/python/singa/tensor.py
----------------------------------------------------------------------
diff --git a/python/singa/tensor.py b/python/singa/tensor.py
index b5f5d99..49fa052 100644
--- a/python/singa/tensor.py
+++ b/python/singa/tensor.py
@@ -938,52 +938,53 @@ def mult(A, B, C=None, alpha=1.0, beta=0.0):
singa.MultWithScale(alpha, A.singa_tensor, B.singa_tensor,
beta, C.singa_tensor)
return C
-def tensordot (A,B,axes=2):
-
+
+
+def tensordot(A, B, axes=2):
"""Returns the tensor multiplication of two tensors along specified axes.
-
+
This is equivalent to compute dot product along the specified axes which
are treated as one axis by reshaping.
-
+
Args:
a (Singa.Tensor): The first argument.
b (Singa.Tensor): The second argument.
axes:
- - If it is an integer, then ''axes'' represent axes at the last of ''a`'' and
- the first of ''b'' are used.
- - If it is a pair of sequences of integers, then these two
- sequences specify the list of axes for ''a'' and ''b''. The
- corresponding axes are paired for sum-product.
-
+ - If it is an integer, then ''axes'' represent axes at the last of ''a`'' and
+ the first of ''b'' are used.
+ - If it is a pair of sequences of integers, then these two
+ sequences specify the list of axes for ''a'' and ''b''. The
+ corresponding axes are paired for sum-product.
+
Return:
singa.tensor: The tensor product of ''a'' and ''b'' along the
axes specified by ''axes''.
- """
+ """
# when axes is an integer, axes_A and axes_B represent axes at the last of ''a`'' and
# the first of ''b''. For example, when axes is 1, we do the normal multiplication :
# if A is in shape(3,2,4), B is in shape(4,2,5), it will return a matrix in shape(3,2,2,5)
- #when axes is 2 and A,B are in the same shape, it will return a matrix in shape(3,5)
- try:
- iter(axes)
- except Exception:
+ # when axes is 2 and A,B are in the same shape, it will return a matrix in shape(3,5)
+ if type(axes) == int:
axes_A = list(range(-axes, 0))
axes_B = list(range(0, axes))
+ axes_B = axes_B.reverse()
else:
axes_A, axes_B = axes
# when axes is a pair of sequences of integers.For example, A is in shape(3,2,4),
- #B is in shape(4,2,5), we set axes as ([1,2],[1,0]), it will return a matrix in shape(3,5)
- try:
+ # B is in shape(4,2,5), we set axes as ([1,2],[1,0]), it will return a matrix in shape(3,5)
+ if isinstance(axes_A, list):
na = len(axes_A)
- axes_A = list(axes_B)
- except TypeError:
+ axes_A = list(axes_A)
+ else:
axes_A = [axes_A]
na = 1
- try:
- nb = len(axes_A)
+ if isinstance(axes_B, list):
+ nb = len(axes_B)
axes_B = list(axes_B)
- except TypeError:
+ else:
axes_B = [axes_B]
nb = 1
+
# a_shape and b_shape are the shape of tensor A and B, while nda and ndb are the dim of A and B
a_shape = A.shape
nda = A.ndim
@@ -994,47 +995,47 @@ def tensordot (A,B,axes=2):
if na != nb:
equal = False
else:
- # to make the shape match
+ # to make the shape match
for k in range(na):
- if a_shape[axes_a[k]] != b_shape[axes_b[k]]:
+ if a_shape[axes_A[k]] != b_shape[axes_B[k]]:
equal = False
break
- if axes_a[k] < 0:
- axes_a[k] += nda
- if axes_b[k] < 0:
- axes_b[k] += ndb
+ if axes_A[k] < 0:
+ axes_A[k] += nda
+ if axes_B[k] < 0:
+ axes_B[k] += ndb
if not equal:
raise ValueError("shape-mismatch for sum")
'''start to do the calculation according to the axes'''
- notin = [k for k in range(nda) if k not in axes_a]
+
+ notin = [k for k in range(nda) if k not in axes_A]
# nda is the dim of A, and axes_a is the axis for A, notin is the axis which is not in axes_A
- newaxes_a = notin + axes_a
+ newaxes_a = notin + axes_A
N2 = 1
- for axis in axes_a:
+ for axis in axes_A:
N2 *= a_shape[axis]
N1 = 1
for ax in notin:
- N1 *=a_shape[ax]
+ N1 *= a_shape[ax]
# newshape_a is the shape to do multiplication.For example, A is in shape(3,2,4),
- #B is in shape(4,2,5), we set axes as ([1,2],[1,0]), then newshape_a should be (3,8)
- #olda is the shape that will be shown in the result.
- newshape_a = (N1,N2)
+ # B is in shape(4,2,5), we set axes as ([1,2],[1,0]), then newshape_a should be (3,5)
+ # olda is the shape that will be shown in the result.
+ newshape_a = (N1, N2)
olda = [a_shape[axis] for axis in notin]
- notin = [k for k in range(ndb) if k not in axes_b]
- newaxes_b = axes_b + notin
+ newaxes_b = axes_B + notin
N2 = 1
- for axis in axes_b:
+ for axis in axes_B:
N2 *= b_shape[axis]
N1 = 1
- for ax in notin:
- N1 *=a_shape[ax]
+ for bx in notin:
+ N1 *= b_shape[bx]
newshape_b = (N2, N1)
oldb = [b_shape[axis] for axis in notin]
# do transpose and reshape to get the 2D matrix to do multiplication
at = a.transpose(newaxes_a).reshape(newshape_a)
bt = b.transpose(newaxes_b).reshape(newshape_b)
- res = mult(a, b)
- #reshape the result
+ res = mult(at, bt)
+ # reshape the result
return res.reshape(olda + oldb)
def div(lhs, rhs, ret=None):
[06/12] incubator-singa git commit: SINGA-347 Create a function that
supports einsum assuming have the numpy.repeat,
finish the einsum function (using elementwisemult, no need to use tensordot)
Posted by wa...@apache.org.
SINGA-347 Create a function that supports einsum
assuming have the numpy.repeat, finish the einsum function (using elementwisemult, no need to use tensordot)
Project: http://git-wip-us.apache.org/repos/asf/incubator-singa/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-singa/commit/2ec13649
Tree: http://git-wip-us.apache.org/repos/asf/incubator-singa/tree/2ec13649
Diff: http://git-wip-us.apache.org/repos/asf/incubator-singa/diff/2ec13649
Branch: refs/heads/master
Commit: 2ec1364930a7a5b17640151b7cd16ae8fe75e131
Parents: 2ec06ed
Author: sheyujian <sh...@me.com>
Authored: Thu Apr 19 16:35:38 2018 +0800
Committer: sheyujian <sh...@me.com>
Committed: Thu Apr 19 16:35:38 2018 +0800
----------------------------------------------------------------------
python/singa/tensor.py | 60 ++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 59 insertions(+), 1 deletion(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/2ec13649/python/singa/tensor.py
----------------------------------------------------------------------
diff --git a/python/singa/tensor.py b/python/singa/tensor.py
index e6502fc..38acf6d 100644
--- a/python/singa/tensor.py
+++ b/python/singa/tensor.py
@@ -1042,7 +1042,6 @@ def tensordot(A, B, axes=2):
#reshape the result
return res.reshape(olda + oldb)
-
def einsum_(A,B,ops):
'''Do the matrix to matrix einsum calculation according to the operands
@@ -1183,6 +1182,65 @@ def diag(A,axis=-1):
A_diag = from_numpy(npA_diag)
return A_diag
+def einsum2(A,B,ops):
+ '''
+ Do the matrix to matrix einsum calculation according to the operands
+
+ Args:
+ A (Singa.Tensor): The first argument.
+ B (Singa.Tensor): The second argument.
+ ops(string):
+ the string specifies the subscripts for summation such as 'ki,kj->kij'
+
+ Returns: Singa.Tensor
+ the output matirx of the einsum calculation
+ '''
+
+ if len(ops) == 0:
+ raise ValueError("No input operands")
+
+ inputops, outputops = ops.split('->')
+ inputops = inputops.split(',')
+
+ if A.ndim != len(inputops[0]) or B.ndim != len(inputops[1]):
+ raise ValueError("input dim doesn't match operands")
+
+ sums = sorted(list((set(inputops[0]) | set(inputops[1])) - set(outputops)))
+
+ broadcast_A = sorted(list(set(inputops[1]) - set(inputops[0])))
+ broadcast_B = sorted(list(set(inputops[0]) - set(inputops[1])))
+
+
+ outputall = sorted(list(set(inputops[0]) | set(inputops[1])))
+
+ sums = [outputall.index(x) for x in sums]
+ broadcast_idA = [inputops[1].find(x) for x in broadcast_A]
+ broadcast_idB = [inputops[0].find(x) for x in broadcast_B]
+
+ broadcast_a = [B.shape[x] for x in broadcast_idA]
+ broadcast_b = [A.shape[x] for x in broadcast_idB]
+
+ transpose_A = [(list(inputops[0])+broadcast_A) .index(x) for x in outputall]
+ transpose_B = [(list(inputops[1])+broadcast_B) .index(x) for x in outputall]
+
+
+ reshape_A = list(A.shape)+broadcast_a
+ reshape_B = list(B.shape)+broadcast_b
+
+ mult_A = np.repeat(A, np.product(broadcast_a)).reshape(reshape_A).transpose(transpose_A)
+ mult_B = np.repeat(B, np.product(broadcast_b)).reshape(reshape_B).transpose(transpose_B)
+
+ if mult_A.shape != mult_B.shape:
+ raise ValueError("error: matrix dimension mismatch")
+ res = eltwise_mult(mult_A, mult_B)
+
+ sum_R = sorted(sums, reverse=True)
+ for i in sum_R:
+ res = res.sum(axis=i)
+ transpose_res = [sorted(list(outputops)).index(x) for x in list(outputops)]
+
+ return res.transpose(transpose_res)
+
def div(lhs, rhs, ret=None):
[09/12] incubator-singa git commit: SINGA-347 Create a function that
supports einsum remove idea
Posted by wa...@apache.org.
SINGA-347 Create a function that supports einsum
remove idea
Project: http://git-wip-us.apache.org/repos/asf/incubator-singa/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-singa/commit/90015d6e
Tree: http://git-wip-us.apache.org/repos/asf/incubator-singa/tree/90015d6e
Diff: http://git-wip-us.apache.org/repos/asf/incubator-singa/diff/90015d6e
Branch: refs/heads/master
Commit: 90015d6e9841b20843612b1837a4cf902e932682
Parents: c037e2f
Author: sheyujian <sh...@me.com>
Authored: Sun Apr 29 12:27:50 2018 +0800
Committer: sheyujian <sh...@me.com>
Committed: Sun Apr 29 12:27:50 2018 +0800
----------------------------------------------------------------------
.gitignore | 1 +
1 file changed, 1 insertion(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/90015d6e/.gitignore
----------------------------------------------------------------------
diff --git a/.gitignore b/.gitignore
index 887c409..a2cddf1 100644
--- a/.gitignore
+++ b/.gitignore
@@ -12,3 +12,4 @@ build/
thirdparty/*
!thirdparty/install.sh
test/samples/
+.idea
[05/12] incubator-singa git commit: SINGA-347 Create a function that
supports einsum 1. fix some calculation error in the tensor mult function 2.
by doing the fixing, the test using numpy could pass
Posted by wa...@apache.org.
SINGA-347 Create a function that supports einsum
1. fix some calculation error in the tensor mult function
2. by doing the fixing, the test using numpy could pass
Project: http://git-wip-us.apache.org/repos/asf/incubator-singa/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-singa/commit/2ec06ed1
Tree: http://git-wip-us.apache.org/repos/asf/incubator-singa/tree/2ec06ed1
Diff: http://git-wip-us.apache.org/repos/asf/incubator-singa/diff/2ec06ed1
Branch: refs/heads/master
Commit: 2ec06ed15951fdcd02e55b20757bc708f0081cb4
Parents: b09aed3
Author: sheyujian <sh...@me.com>
Authored: Wed Apr 18 15:35:20 2018 +0800
Committer: sheyujian <sh...@me.com>
Committed: Wed Apr 18 15:44:41 2018 +0800
----------------------------------------------------------------------
python/singa/tensor.py | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/2ec06ed1/python/singa/tensor.py
----------------------------------------------------------------------
diff --git a/python/singa/tensor.py b/python/singa/tensor.py
index e81f3ab..e6502fc 100644
--- a/python/singa/tensor.py
+++ b/python/singa/tensor.py
@@ -1098,6 +1098,8 @@ def tensormult(A, B, sum=None, multiply=None):
Return: (Singa.Tensor)
The output of the tensormult calculation
+
+ fix some error of source code in the nils-werner/sparse
'''
if sum is None:
sum = [[], []]
@@ -1117,8 +1119,12 @@ def tensormult(A, B, sum=None, multiply=None):
post_multiply[v] += i
- for i in post_multiply:
+# for i in post_multiply:
+# A = diag(A,i)
+ for i in sorted(post_multiply):
+
A = diag(A,i)
+ sum[0] = [x+1 if x >= i else x for x in sum[0]]
sum[0] += post_multiply
sum[1] += multiply[1]
[10/12] incubator-singa git commit: SINGA-347 Create a function that
supports einsum 1. delete .idea
Posted by wa...@apache.org.
SINGA-347 Create a function that supports einsum
1. delete .idea
Project: http://git-wip-us.apache.org/repos/asf/incubator-singa/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-singa/commit/4f5f902a
Tree: http://git-wip-us.apache.org/repos/asf/incubator-singa/tree/4f5f902a
Diff: http://git-wip-us.apache.org/repos/asf/incubator-singa/diff/4f5f902a
Branch: refs/heads/master
Commit: 4f5f902a1c71785f05419492856e8f215d316495
Parents: 90015d6
Author: sheyujian <sh...@me.com>
Authored: Sun Apr 29 12:29:27 2018 +0800
Committer: sheyujian <sh...@me.com>
Committed: Sun Apr 29 12:29:27 2018 +0800
----------------------------------------------------------------------
.idea/dictionaries/sheyujian.xml | 3 -
.idea/incubator-singa.iml | 19 --
.idea/misc.xml | 4 -
.idea/modules.xml | 8 -
.idea/vcs.xml | 6 -
.idea/workspace.xml | 342 ----------------------------------
6 files changed, 382 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/4f5f902a/.idea/dictionaries/sheyujian.xml
----------------------------------------------------------------------
diff --git a/.idea/dictionaries/sheyujian.xml b/.idea/dictionaries/sheyujian.xml
deleted file mode 100644
index ac14bc9..0000000
--- a/.idea/dictionaries/sheyujian.xml
+++ /dev/null
@@ -1,3 +0,0 @@
-<component name="ProjectDictionaryState">
- <dictionary name="sheyujian" />
-</component>
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/4f5f902a/.idea/incubator-singa.iml
----------------------------------------------------------------------
diff --git a/.idea/incubator-singa.iml b/.idea/incubator-singa.iml
deleted file mode 100644
index eac2c49..0000000
--- a/.idea/incubator-singa.iml
+++ /dev/null
@@ -1,19 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<module type="PYTHON_MODULE" version="4">
- <component name="NewModuleRootManager">
- <content url="file://$MODULE_DIR$" />
- <orderEntry type="inheritedJdk" />
- <orderEntry type="sourceFolder" forTests="false" />
- </component>
- <component name="TemplatesService">
- <option name="TEMPLATE_CONFIGURATION" value="Jinja2" />
- <option name="TEMPLATE_FOLDERS">
- <list>
- <option value="$MODULE_DIR$/doc/en/_templates" />
- </list>
- </option>
- </component>
- <component name="TestRunnerService">
- <option name="PROJECT_TEST_RUNNER" value="Unittests" />
- </component>
-</module>
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/4f5f902a/.idea/misc.xml
----------------------------------------------------------------------
diff --git a/.idea/misc.xml b/.idea/misc.xml
deleted file mode 100644
index bb831c4..0000000
--- a/.idea/misc.xml
+++ /dev/null
@@ -1,4 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<project version="4">
- <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.5 (untitled)" project-jdk-type="Python SDK" />
-</project>
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/4f5f902a/.idea/modules.xml
----------------------------------------------------------------------
diff --git a/.idea/modules.xml b/.idea/modules.xml
deleted file mode 100644
index b066728..0000000
--- a/.idea/modules.xml
+++ /dev/null
@@ -1,8 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<project version="4">
- <component name="ProjectModuleManager">
- <modules>
- <module fileurl="file://$PROJECT_DIR$/.idea/incubator-singa.iml" filepath="$PROJECT_DIR$/.idea/incubator-singa.iml" />
- </modules>
- </component>
-</project>
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/4f5f902a/.idea/vcs.xml
----------------------------------------------------------------------
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
deleted file mode 100644
index 94a25f7..0000000
--- a/.idea/vcs.xml
+++ /dev/null
@@ -1,6 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<project version="4">
- <component name="VcsDirectoryMappings">
- <mapping directory="$PROJECT_DIR$" vcs="Git" />
- </component>
-</project>
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/4f5f902a/.idea/workspace.xml
----------------------------------------------------------------------
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
deleted file mode 100644
index 95e5f35..0000000
--- a/.idea/workspace.xml
+++ /dev/null
@@ -1,342 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<project version="4">
- <component name="ChangeListManager">
- <list default="true" id="33c7ea95-5465-40ee-9c51-dfee0bfee69f" name="Default" comment="">
- <change beforePath="" afterPath="$PROJECT_DIR$/.idea/dictionaries/sheyujian.xml" />
- <change beforePath="" afterPath="$PROJECT_DIR$/.idea/incubator-singa.iml" />
- <change beforePath="" afterPath="$PROJECT_DIR$/.idea/misc.xml" />
- <change beforePath="" afterPath="$PROJECT_DIR$/.idea/modules.xml" />
- <change beforePath="" afterPath="$PROJECT_DIR$/.idea/vcs.xml" />
- <change beforePath="" afterPath="$PROJECT_DIR$/.idea/workspace.xml" />
- <change beforePath="$PROJECT_DIR$/python/singa/tensor.py" afterPath="$PROJECT_DIR$/python/singa/tensor.py" />
- <change beforePath="$PROJECT_DIR$/test/python/test_tensor.py" afterPath="$PROJECT_DIR$/test/python/test_tensor.py" />
- </list>
- <option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
- <option name="TRACKING_ENABLED" value="true" />
- <option name="SHOW_DIALOG" value="false" />
- <option name="HIGHLIGHT_CONFLICTS" value="true" />
- <option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
- <option name="LAST_RESOLUTION" value="IGNORE" />
- </component>
- <component name="CoverageDataManager">
- <SUITE FILE_PATH="coverage/incubator_singa$singa.coverage" NAME="singa Coverage Results" MODIFIED="1524818212622" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="" />
- </component>
- <component name="FileEditorManager">
- <leaf SIDE_TABS_SIZE_LIMIT_KEY="300">
- <file leaf-file-name="setup.py.in" pinned="false" current-in-tab="false">
- <entry file="file://$PROJECT_DIR$/python/setup.py.in">
- <provider selected="true" editor-type-id="text-editor">
- <state relative-caret-position="0">
- <caret line="0" column="0" lean-forward="false" selection-start-line="0" selection-start-column="0" selection-end-line="0" selection-end-column="0" />
- <folding />
- </state>
- </provider>
- </entry>
- </file>
- <file leaf-file-name="tensor.py" pinned="false" current-in-tab="true">
- <entry file="file://$PROJECT_DIR$/python/singa/tensor.py">
- <provider selected="true" editor-type-id="text-editor">
- <state relative-caret-position="435">
- <caret line="1036" column="15" lean-forward="true" selection-start-line="1036" selection-start-column="15" selection-end-line="1036" selection-end-column="15" />
- <folding />
- </state>
- </provider>
- </entry>
- </file>
- <file leaf-file-name="test_tensor.py" pinned="false" current-in-tab="false">
- <entry file="file://$PROJECT_DIR$/test/python/test_tensor.py">
- <provider selected="true" editor-type-id="text-editor">
- <state relative-caret-position="432">
- <caret line="167" column="26" lean-forward="true" selection-start-line="167" selection-start-column="26" selection-end-line="167" selection-end-column="26" />
- <folding>
- <element signature="e#865#896#0" expanded="true" />
- </folding>
- </state>
- </provider>
- </entry>
- </file>
- <file leaf-file-name="layer.py" pinned="false" current-in-tab="false">
- <entry file="file://$PROJECT_DIR$/python/singa/layer.py">
- <provider selected="true" editor-type-id="text-editor">
- <state relative-caret-position="527">
- <caret line="1310" column="31" lean-forward="true" selection-start-line="1310" selection-start-column="31" selection-end-line="1310" selection-end-column="31" />
- <folding />
- </state>
- </provider>
- </entry>
- </file>
- </leaf>
- </component>
- <component name="Git.Settings">
- <option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
- </component>
- <component name="IdeDocumentHistory">
- <option name="CHANGED_PATHS">
- <list>
- <option value="$PROJECT_DIR$/test/python/test_tensor.py" />
- <option value="$PROJECT_DIR$/python/singa/tensor.py" />
- </list>
- </option>
- </component>
- <component name="JsBuildToolGruntFileManager" detection-done="true" sorting="DEFINITION_ORDER" />
- <component name="JsBuildToolPackageJson" detection-done="true" sorting="DEFINITION_ORDER" />
- <component name="JsGulpfileManager">
- <detection-done>true</detection-done>
- <sorting>DEFINITION_ORDER</sorting>
- </component>
- <component name="ProjectFrameBounds">
- <option name="x" value="55" />
- <option name="y" value="63" />
- <option name="width" value="1400" />
- <option name="height" value="790" />
- </component>
- <component name="ProjectInspectionProfilesVisibleTreeState">
- <entry key="Project Default">
- <profile-state>
- <expanded-state>
- <State>
- <id />
- </State>
- </expanded-state>
- <selected-state>
- <State>
- <id>AngularJS</id>
- </State>
- </selected-state>
- </profile-state>
- </entry>
- </component>
- <component name="ProjectView">
- <navigator currentView="ProjectPane" proportions="" version="1">
- <flattenPackages />
- <showMembers />
- <showModules />
- <showLibraryContents />
- <hideEmptyPackages />
- <abbreviatePackageNames />
- <autoscrollToSource />
- <autoscrollFromSource />
- <sortByType />
- <manualOrder />
- <foldersAlwaysOnTop value="true" />
- </navigator>
- <panes>
- <pane id="ProjectPane">
- <subPane>
- <expand>
- <path>
- <item name="incubator-singa" type="b2602c69:ProjectViewProjectNode" />
- <item name="incubator-singa" type="462c0819:PsiDirectoryNode" />
- </path>
- <path>
- <item name="incubator-singa" type="b2602c69:ProjectViewProjectNode" />
- <item name="incubator-singa" type="462c0819:PsiDirectoryNode" />
- <item name="python" type="462c0819:PsiDirectoryNode" />
- </path>
- <path>
- <item name="incubator-singa" type="b2602c69:ProjectViewProjectNode" />
- <item name="incubator-singa" type="462c0819:PsiDirectoryNode" />
- <item name="python" type="462c0819:PsiDirectoryNode" />
- <item name="singa" type="462c0819:PsiDirectoryNode" />
- </path>
- <path>
- <item name="incubator-singa" type="b2602c69:ProjectViewProjectNode" />
- <item name="incubator-singa" type="462c0819:PsiDirectoryNode" />
- <item name="src" type="462c0819:PsiDirectoryNode" />
- </path>
- <path>
- <item name="incubator-singa" type="b2602c69:ProjectViewProjectNode" />
- <item name="incubator-singa" type="462c0819:PsiDirectoryNode" />
- <item name="test" type="462c0819:PsiDirectoryNode" />
- </path>
- <path>
- <item name="incubator-singa" type="b2602c69:ProjectViewProjectNode" />
- <item name="incubator-singa" type="462c0819:PsiDirectoryNode" />
- <item name="test" type="462c0819:PsiDirectoryNode" />
- <item name="python" type="462c0819:PsiDirectoryNode" />
- </path>
- </expand>
- <select />
- </subPane>
- </pane>
- <pane id="Scope" />
- <pane id="Scratches" />
- </panes>
- </component>
- <component name="PropertiesComponent">
- <property name="nodejs_interpreter_path.stuck_in_default_project" value="undefined stuck path" />
- <property name="settings.editor.selected.configurable" value="settings.typescriptcompiler" />
- <property name="WebServerToolWindowFactoryState" value="false" />
- <property name="last_opened_file_path" value="$PROJECT_DIR$" />
- </component>
- <component name="RunDashboard">
- <option name="ruleStates">
- <list>
- <RuleState>
- <option name="name" value="ConfigurationTypeDashboardGroupingRule" />
- </RuleState>
- <RuleState>
- <option name="name" value="StatusDashboardGroupingRule" />
- </RuleState>
- </list>
- </option>
- </component>
- <component name="RunManager">
- <configuration name="singa" type="PythonConfigurationType" factoryName="Python">
- <option name="INTERPRETER_OPTIONS" value="" />
- <option name="PARENT_ENVS" value="true" />
- <envs>
- <env name="PYTHONUNBUFFERED" value="1" />
- </envs>
- <option name="SDK_HOME" value="$PROJECT_DIR$/../PycharmProjects/untitled/venv/bin/python" />
- <option name="WORKING_DIRECTORY" value="" />
- <option name="IS_MODULE_SDK" value="false" />
- <option name="ADD_CONTENT_ROOTS" value="true" />
- <option name="ADD_SOURCE_ROOTS" value="true" />
- <module name="incubator-singa" />
- <EXTENSION ID="PythonCoverageRunConfigurationExtension" enabled="false" sample_coverage="true" runner="coverage.py" />
- <option name="SCRIPT_NAME" value="" />
- <option name="PARAMETERS" value="" />
- <option name="SHOW_COMMAND_LINE" value="false" />
- <option name="EMULATE_TERMINAL" value="false" />
- <option name="MODULE_MODE" value="false" />
- </configuration>
- </component>
- <component name="ShelveChangesManager" show_recycled="false">
- <option name="remove_strategy" value="false" />
- </component>
- <component name="SvnConfiguration">
- <configuration />
- </component>
- <component name="TaskManager">
- <task active="true" id="Default" summary="Default task">
- <changelist id="33c7ea95-5465-40ee-9c51-dfee0bfee69f" name="Default" comment="" />
- <created>1524817684476</created>
- <option name="number" value="Default" />
- <option name="presentableId" value="Default" />
- <updated>1524817684476</updated>
- </task>
- <servers />
- </component>
- <component name="ToolWindowManager">
- <frame x="55" y="63" width="1400" height="790" extended-state="0" />
- <editor active="true" />
- <layout>
- <window_info id="TODO" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="6" side_tool="false" content_ui="tabs" />
- <window_info id="Event Log" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="7" side_tool="true" content_ui="tabs" />
- <window_info id="Run" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.32729805" sideWeight="0.5" order="2" side_tool="false" content_ui="tabs" />
- <window_info id="Version Control" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="7" side_tool="false" content_ui="tabs" />
- <window_info id="Python Console" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="7" side_tool="false" content_ui="tabs" />
- <window_info id="Terminal" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="7" side_tool="false" content_ui="tabs" />
- <window_info id="Project" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" show_stripe_button="true" weight="0.25" sideWeight="0.5" order="0" side_tool="false" content_ui="combo" />
- <window_info id="Docker" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="false" weight="0.33" sideWeight="0.5" order="7" side_tool="false" content_ui="tabs" />
- <window_info id="Database" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="3" side_tool="false" content_ui="tabs" />
- <window_info id="SciView" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="3" side_tool="false" content_ui="tabs" />
- <window_info id="Structure" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.25" sideWeight="0.5" order="1" side_tool="false" content_ui="tabs" />
- <window_info id="Debug" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.4" sideWeight="0.5" order="3" side_tool="false" content_ui="tabs" />
- <window_info id="Favorites" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="2" side_tool="true" content_ui="tabs" />
- <window_info id="Cvs" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.25" sideWeight="0.5" order="4" side_tool="false" content_ui="tabs" />
- <window_info id="Message" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="0" side_tool="false" content_ui="tabs" />
- <window_info id="Commander" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.4" sideWeight="0.5" order="0" side_tool="false" content_ui="tabs" />
- <window_info id="Inspection" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.4" sideWeight="0.5" order="5" side_tool="false" content_ui="tabs" />
- <window_info id="Hierarchy" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.25" sideWeight="0.5" order="2" side_tool="false" content_ui="combo" />
- <window_info id="Find" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="1" side_tool="false" content_ui="tabs" />
- <window_info id="Ant Build" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.25" sideWeight="0.5" order="1" side_tool="false" content_ui="tabs" />
- </layout>
- </component>
- <component name="TypeScriptGeneratedFilesManager">
- <option name="version" value="1" />
- </component>
- <component name="VcsContentAnnotationSettings">
- <option name="myLimit" value="2678400000" />
- </component>
- <component name="XDebuggerManager">
- <breakpoint-manager />
- <watches-manager />
- </component>
- <component name="editorHistoryManager">
- <entry file="file://$PROJECT_DIR$/python/setup.py.in">
- <provider selected="true" editor-type-id="text-editor">
- <state relative-caret-position="0">
- <caret line="0" column="0" lean-forward="false" selection-start-line="0" selection-start-column="0" selection-end-line="0" selection-end-column="0" />
- <folding />
- </state>
- </provider>
- </entry>
- <entry file="file://$PROJECT_DIR$/python/singa/tensor.py">
- <provider selected="true" editor-type-id="text-editor">
- <state relative-caret-position="14940">
- <caret line="996" column="41" lean-forward="true" selection-start-line="996" selection-start-column="41" selection-end-line="996" selection-end-column="41" />
- <folding />
- </state>
- </provider>
- </entry>
- <entry file="file://$PROJECT_DIR$/python/singa/layer.py">
- <provider selected="true" editor-type-id="text-editor">
- <state relative-caret-position="19500">
- <caret line="1310" column="31" lean-forward="true" selection-start-line="1310" selection-start-column="31" selection-end-line="1310" selection-end-column="31" />
- <folding />
- </state>
- </provider>
- </entry>
- <entry file="file://$PROJECT_DIR$/test/python/test_tensor.py">
- <provider selected="true" editor-type-id="text-editor">
- <state relative-caret-position="675">
- <caret line="53" column="62" lean-forward="true" selection-start-line="53" selection-start-column="62" selection-end-line="53" selection-end-column="62" />
- <folding>
- <element signature="e#865#896#0" expanded="true" />
- </folding>
- </state>
- </provider>
- </entry>
- <entry file="file://$PROJECT_DIR$/python/setup.py.in">
- <provider selected="true" editor-type-id="text-editor">
- <state relative-caret-position="0">
- <caret line="0" column="0" lean-forward="false" selection-start-line="0" selection-start-column="0" selection-end-line="0" selection-end-column="0" />
- <folding />
- </state>
- </provider>
- </entry>
- <entry file="file://$PROJECT_DIR$/python/singa/layer.py">
- <provider selected="true" editor-type-id="text-editor">
- <state relative-caret-position="527">
- <caret line="1310" column="31" lean-forward="true" selection-start-line="1310" selection-start-column="31" selection-end-line="1310" selection-end-column="31" />
- <folding />
- </state>
- </provider>
- </entry>
- <entry file="file://$PROJECT_DIR$/test/python/test_tensor.py">
- <provider selected="true" editor-type-id="text-editor">
- <state relative-caret-position="432">
- <caret line="167" column="26" lean-forward="true" selection-start-line="167" selection-start-column="26" selection-end-line="167" selection-end-column="26" />
- <folding>
- <element signature="e#865#896#0" expanded="true" />
- </folding>
- </state>
- </provider>
- </entry>
- <entry file="file://$PROJECT_DIR$/python/singa/tensor.py">
- <provider selected="true" editor-type-id="text-editor">
- <state relative-caret-position="435">
- <caret line="1036" column="15" lean-forward="true" selection-start-line="1036" selection-start-column="15" selection-end-line="1036" selection-end-column="15" />
- <folding />
- </state>
- </provider>
- </entry>
- </component>
- <component name="masterDetails">
- <states>
- <state key="ScopeChooserConfigurable.UI">
- <settings>
- <splitter-proportions>
- <option name="proportions">
- <list>
- <option value="0.2" />
- </list>
- </option>
- </splitter-proportions>
- </settings>
- </state>
- </states>
- </component>
-</project>
\ No newline at end of file
[03/12] incubator-singa git commit: SINGA-347 Create a function that
supports einsum 1. provide functions to calculate einsum calculation but
still needs one function to make the tensor diagonalization along one axis 2.
give a credit to numpy for tensord
Posted by wa...@apache.org.
SINGA-347 Create a function that supports einsum
1. provide functions to calculate einsum calculation but still needs one function to make the tensor diagonalization along one axis
2. give a credit to numpy for tensordot
Project: http://git-wip-us.apache.org/repos/asf/incubator-singa/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-singa/commit/f595f10c
Tree: http://git-wip-us.apache.org/repos/asf/incubator-singa/tree/f595f10c
Diff: http://git-wip-us.apache.org/repos/asf/incubator-singa/diff/f595f10c
Branch: refs/heads/master
Commit: f595f10c2556ef1dad27dfc7a532a09ee85665be
Parents: 9a3ce58
Author: sheyujian <sh...@me.com>
Authored: Mon Apr 16 06:27:38 2018 +0800
Committer: sheyujian <sh...@me.com>
Committed: Wed Apr 18 13:56:27 2018 +0800
----------------------------------------------------------------------
python/singa/tensor.py | 79 +++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 77 insertions(+), 2 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/f595f10c/python/singa/tensor.py
----------------------------------------------------------------------
diff --git a/python/singa/tensor.py b/python/singa/tensor.py
index 49fa052..fde1667 100644
--- a/python/singa/tensor.py
+++ b/python/singa/tensor.py
@@ -959,15 +959,17 @@ def tensordot(A, B, axes=2):
Return:
singa.tensor: The tensor product of ''a'' and ''b'' along the
axes specified by ''axes''.
+
+ Thanks to numpy.tensordot.
"""
# when axes is an integer, axes_A and axes_B represent axes at the last of ''a`'' and
# the first of ''b''. For example, when axes is 1, we do the normal multiplication :
# if A is in shape(3,2,4), B is in shape(4,2,5), it will return a matrix in shape(3,2,2,5)
- # when axes is 2 and A,B are in the same shape, it will return a matrix in shape(3,5)
+ # when axes is 2 and A,B are in the shape (3,2,4) and (2,4,5), it will return a matrix in shape(3,5)
if type(axes) == int:
axes_A = list(range(-axes, 0))
axes_B = list(range(0, axes))
- axes_B = axes_B.reverse()
+ axes_B = axes_B
else:
axes_A, axes_B = axes
# when axes is a pair of sequences of integers.For example, A is in shape(3,2,4),
@@ -1038,6 +1040,79 @@ def tensordot(A, B, axes=2):
# reshape the result
return res.reshape(olda + oldb)
+
+def einsum(A,B,ops):
+ '''
+ Thanks to nils-werner/sparse.einsum()
+ '''
+
+ if len(ops) == 0:
+ raise ValueError("No input operands")
+
+ nputops, outputops = ops.split('->')
+ inputops = inputops.split(',')
+
+ # All indices that are in input AND in output are multiplies
+ multiplies = sorted(list(set(inputops[0]) & set(inputops[1]) & set(outputops)))
+ # All indices that are in input BUT NOT in output are sum contractions
+ sums = sorted(list((set(inputops[0]) & set(inputops[1])) - set(outputops)))
+
+ # Map sums and indices to axis integers
+ multiplies = [[inop.find(x) for x in multiplies] for inop in inputops]
+
+ sums = [[inop.find(x) for x in sums] for inop in inputops]
+ # Find output axes in input axes for final transpose
+ # Values very likely lie outside of output tensor shape, so
+ # just map them values to their rank (index in ordered list)
+ transpose = [''.join(inputops).find(x) for x in outputops]
+ transpose = sorted(range(len(transpose)), key = transpose.__getitem__)
+
+ return tensormult(A,B, sum=sums, multiply=multiplies).transpose(transpose)
+
+def tensordotmult(A, B, sum=None, multiply=None):
+ if sum is None:
+ sum = [[], []]
+ else:
+ sum = list(sum)
+
+ if multiply is None:
+ multiply = [[], []]
+ else:
+ multiply = list(multiply)
+
+ # For each multiply[0] we are adding one axis, thus we need to increment
+ # all following items by one: (0, 1, 2) -> (0, 2, 4)
+ idx = multipliessort(multiply[0])
+ post_multiply = multiply[0]
+ for i, v in enumerate(idx):
+ post_multiply[v] += i
+
+ for i in post_multiply:
+ A = diag(A,i)
+
+ sum[0] += post_multiply
+ sum[1] += multiply[1]
+
+ return tensordot(A, B, axes=sum)
+
+def multipliessort(multiplies):
+
+ if multiplies is None:
+ multiplies = []
+ else:
+ multiplies = list(multiplies)
+
+ sort_multiplies = sorted(enumerate(multiplies), key=lambda x: x[1])
+ idx = [x[0] for x in sort_multiplies]
+ return idx
+
+def diag(A,axis = -1):
+ A_diag = clone(A)
+ '''sheyujian todo: to make a tensor's axis to be diagonalization'''
+ return A_diag
+
+
+
def div(lhs, rhs, ret=None):
'''Elementi-wise division.
[07/12] incubator-singa git commit: SINGA-347 Create a function that
supports einsum 1. finish the einsum function in python but still needs some
funciton to do in cpp 2. function TODO list to finish the function in
cpp(just like numpy function):a.sum(A,
Posted by wa...@apache.org.
SINGA-347 Create a function that supports einsum
1. finish the einsum function in python but still needs some funciton to do in cpp
2. function TODO list to finish the function in cpp(just like numpy function):a.sum(A,axis = None) b.repeat(A,repeats) c.transpose(A,axes = None)
Project: http://git-wip-us.apache.org/repos/asf/incubator-singa/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-singa/commit/fa1b0237
Tree: http://git-wip-us.apache.org/repos/asf/incubator-singa/tree/fa1b0237
Diff: http://git-wip-us.apache.org/repos/asf/incubator-singa/diff/fa1b0237
Branch: refs/heads/master
Commit: fa1b023717b029df6136579be4c3bf95790c914b
Parents: 2ec1364
Author: sheyujian <sh...@me.com>
Authored: Fri Apr 20 13:00:49 2018 +0800
Committer: sheyujian <sh...@me.com>
Committed: Fri Apr 20 13:00:49 2018 +0800
----------------------------------------------------------------------
python/singa/tensor.py | 262 +++-----------------------------------------
1 file changed, 17 insertions(+), 245 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/fa1b0237/python/singa/tensor.py
----------------------------------------------------------------------
diff --git a/python/singa/tensor.py b/python/singa/tensor.py
index 38acf6d..caf5998 100644
--- a/python/singa/tensor.py
+++ b/python/singa/tensor.py
@@ -940,251 +940,16 @@ def mult(A, B, C=None, alpha=1.0, beta=0.0):
return C
-def tensordot(A, B, axes=2):
- """Returns the tensor multiplication of two tensors along specified axes.
- This is equivalent to compute dot product along the specified axes which
- are treated as one axis by reshaping.
-
- Args:
- A (Singa.Tensor): The first argument.
- B (Singa.Tensor): The second argument.
- axes:
- - If it is an integer, then ''axes'' represent axes at the last of ''a`'' and
- the first of ''b'' are used.
- - If it is a pair of sequences of integers, then these two
- sequences specify the list of axes for ''a'' and ''b''. The
- corresponding axes are paired for sum-product.
-
- Return:
- singa.tensor: The tensor product of ''a'' and ''b'' along the
- axes specified by ''axes''.
-
- Thanks to numpy.tensordot.
- the link is https://github.com/numpy/numpy/blob/v1.14.0/numpy/core/numeric.py#L1123-L1306
- """
- # when axes is an integer, axes_A and axes_B represent axes at the last of ''a`'' and
- # the first of ''b''. For example, when axes is 1, we do the normal multiplication :
- # if A is in shape(3,2,4), B is in shape(4,2,5), it will return a matrix in shape(3,2,2,5)
- # when axes is 2 and A,B are in the shape (3,2,4) and (2,4,5), it will return a matrix in shape(3,5)
- if type(axes) == int:
- axes_A = list(range(-axes, 0))
- axes_B = list(range(0, axes))
- axes_B = axes_B
- else:
- axes_A,axes_B =axes
- # when axes is a pair of sequences of integers.For example, A is in shape(3,2,4),
- #B is in shape(4,2,5), we set axes as ([1,2],[1,0]), it will return a matrix in shape(3,5)
- if isinstance(axes_A,list):
- na = len(axes_A)
- axes_A = list(axes_A)
- else:
- axes_A = [axes_A]
- na = 1
- if isinstance(axes_B,list):
- nb = len(axes_B)
- axes_B = list(axes_B)
- else:
- axes_B = [axes_B]
- nb = 1
-
- # a_shape and b_shape are the shape of tensor A and B, while nda and ndb are the dim of A and B
- a_shape = A.shape
- nda = A.ndim
- b_shape = B.shape
- ndb = B.ndim
- equal = True
- # to check if the length of axe_A is equal to axes_B
- if na != nb:
- equal = False
- else:
- # to make the shape match
- for k in range(na):
- if a_shape[axes_A[k]] != b_shape[axes_B[k]]:
- equal = False
- break
- if axes_A[k] < 0:
- axes_A[k] += nda
- if axes_B[k] < 0:
- axes_B[k] += ndb
- if not equal:
- raise ValueError("shape-mismatch for sum")
- '''start to do the calculation according to the axes'''
-
- notin = [k for k in range(nda) if k not in axes_A]
- # nda is the dim of A, and axes_a is the axis for A, notin is the axis which is not in axes_A
- newaxes_a = notin + axes_A
- N2 = 1
- for axis in axes_A:
- N2 *= a_shape[axis]
- N1 = 1
- for ax in notin:
- N1 *=a_shape[ax]
- #newshape_a is the shape to do multiplication.For example, A is in shape(3,2,4),
- #B is in shape(4,2,5), we set axes as ([1,2],[1,0]), then newshape_a should be (3,5)
- #olda is the shape that will be shown in the result.
- newshape_a = (N1,N2)
- olda = [a_shape[axis] for axis in notin]
- notin = [k for k in range(ndb) if k not in axes_B]
- newaxes_b = axes_B + notin
- N2 = 1
- for axis in axes_B:
- N2 *= b_shape[axis]
- N1 = 1
- for bx in notin:
- N1 *= b_shape[bx]
- newshape_b = (N2, N1)
- oldb = [b_shape[axis] for axis in notin]
- # do transpose and reshape to get the 2D matrix to do multiplication
- at = A.transpose(newaxes_a).reshape(newshape_a)
- bt = B.transpose(newaxes_b).reshape(newshape_b)
- res = mult(at, bt)
- #reshape the result
- return res.reshape(olda + oldb)
-
-def einsum_(A,B,ops):
- '''Do the matrix to matrix einsum calculation according to the operands
-
- Args:
- A (Singa.Tensor): The first argument.
- B (Singa.Tensor): The second argument.
- ops(string):
- the string specifies the subscripts for summation such as 'ki,kj->kij'
-
- Returns: Singa.Tensor
- the output matirx of the einsum calculation
- Thanks to nils-werner/sparse.einsum(),
- the link is https://github.com/nils-werner/sparse/commit/449c75d21d3158630bc5be79c690a60cfc002578
+def einsum(A,B,ops):
'''
- if len(ops) == 0:
- raise ValueError("No input operands")
- # to get the input and output ops
- inputops, outputops = ops.split('->')
- inputops = inputops.split(',')
-
- if A.ndim != len(inputops[0]) or B.ndim != len(inputops[1]):
- raise ValueError("input dim doesn't match operands")
-
- # All indices that are in input AND in output are multiplies
- # All indices that are in input BUT NOT in output are sum contractions
- multiplies = sorted(list(set(inputops[0]) & set(inputops[1]) & set(outputops)))
- sums = sorted(list((set(inputops[0]) & set(inputops[1])) - set(outputops)))
-
- # Map sums and indices to axis integers
- multiplies = [[inops.find(x) for x in multiplies] for inops in inputops]
- sums = [[inops.find(x) for x in sums] for inops in inputops]
+ function TODO list to finish the function in cpp(just like numpy function):
+ 1.sum(A,axis = None)
+ 2.repeat(A,repeats)
+ 3.transpose(A,axes = None)
-
- # Find output axes in input axes for final transpose
- transpose = [''.join(inputops).find(x) for x in outputops]
- #to make the transpose match to its rank
- transpose = sorted(range(len(transpose)), key = transpose.__getitem__)
-
- return tensormult(A,B,sums,multiplies).transpose(transpose)
-
-
-def tensormult(A, B, sum=None, multiply=None):
- '''
- Args:
- A (Singa.Tensor): The first input tensor from einsum.
- B (Singa.Tensor): The second input tensor from einsum.
- sum (list[list[int]]):
- The axis to do the normal tensordot calculation
-
- multiply (list[list[int]]):
- The axis to multiply
-
- Return: (Singa.Tensor)
- The output of the tensormult calculation
-
- fix some error of source code in the nils-werner/sparse
- '''
- if sum is None:
- sum = [[], []]
- else:
- sum = list(sum)
-
- if multiply is None:
- multiply = [[], []]
- else:
- multiply = list(multiply)
- # For each multiply[0] we are adding one axis, thus we need to increment
- # all following items by one: (0, 1, 2) -> (0, 2, 4)
- idx = multipliessort(multiply[0])
-
- post_multiply = multiply[0]
- for i, v in enumerate(idx):
- post_multiply[v] += i
-
-
-# for i in post_multiply:
-# A = diag(A,i)
- for i in sorted(post_multiply):
-
- A = diag(A,i)
- sum[0] = [x+1 if x >= i else x for x in sum[0]]
-
- sum[0] += post_multiply
- sum[1] += multiply[1]
-
- return tensordot(A, B, axes=sum)
-
-def multipliessort(multiplies):
- '''
- Returns the indices that would sort an array
- Args:
- multiplies(list[int]):
- the list of the input multiplies
-
- Return(list[int]):
- the indices list
- '''
-
- if multiplies is None:
- multiplies = []
- else:
- multiplies = list(multiplies)
-
- sort_multiplies = sorted(enumerate(multiplies), key=lambda x: x[1])
- idx = [x[0] for x in sort_multiplies]
- return idx
-
-def diag(A,axis=-1):
- '''
- Return the matrix that is diagonalize along the axis
- Args:
- A (Singa.Tensor):the input tensor
- axis (int):
- the axis which the matrix use to diagonalization
- Return(Singa.Tensor):
- the tensor which has been diagonalize along the given axis
-
-
- It is like using numpy.diag() in the 1D condition, but this function can be used to do high dimension
- matrix diagonalization
-
- '''
- # to get the shape of the diagonalize matirx
- axis_diag = list(range(out.ndim))[axis]
- shape = list(A.shape)
- shape.insert(axis_diag,shape[axis_diag])
-
- npA = to_numpy(A)
- npA_diag = np.zeros(product(shape))
- index = np.argwhere(npA != 0)
- for i in index:
- i_diag = list(i)
- i_diag.insert(axis, i[axis])
- index_npA = [[i[x]] for x in range(len(i))]
- index_npA_diag = [[i_diag[x]] for x in range(len(i_diag))]
- npA_diag[index_npA_diag] = npA[index_npA]
- A_diag = from_numpy(npA_diag)
- return A_diag
-
-def einsum2(A,B,ops):
- '''
- Do the matrix to matrix einsum calculation according to the operands
+ Do the matrix to matrix einsum calculation according to the operands
Args:
A (Singa.Tensor): The first argument.
@@ -1196,23 +961,27 @@ def einsum2(A,B,ops):
the output matirx of the einsum calculation
'''
+
if len(ops) == 0:
raise ValueError("No input operands")
+ # to get the input and output ops
inputops, outputops = ops.split('->')
inputops = inputops.split(',')
if A.ndim != len(inputops[0]) or B.ndim != len(inputops[1]):
raise ValueError("input dim doesn't match operands")
+ #to get the indices in input but not in output
sums = sorted(list((set(inputops[0]) | set(inputops[1])) - set(outputops)))
+ #to get the indices that A and B use to broadcast to each other
broadcast_A = sorted(list(set(inputops[1]) - set(inputops[0])))
broadcast_B = sorted(list(set(inputops[0]) - set(inputops[1])))
-
-
+ # to get all the indices in input
outputall = sorted(list(set(inputops[0]) | set(inputops[1])))
+ ## Map indices to axis integers
sums = [outputall.index(x) for x in sums]
broadcast_idA = [inputops[1].find(x) for x in broadcast_A]
broadcast_idB = [inputops[0].find(x) for x in broadcast_B]
@@ -1220,10 +989,10 @@ def einsum2(A,B,ops):
broadcast_a = [B.shape[x] for x in broadcast_idA]
broadcast_b = [A.shape[x] for x in broadcast_idB]
+ # get the the transpose and reshape parameter used in the elementwise calculation
transpose_A = [(list(inputops[0])+broadcast_A) .index(x) for x in outputall]
transpose_B = [(list(inputops[1])+broadcast_B) .index(x) for x in outputall]
-
reshape_A = list(A.shape)+broadcast_a
reshape_B = list(B.shape)+broadcast_b
@@ -1231,9 +1000,10 @@ def einsum2(A,B,ops):
mult_B = np.repeat(B, np.product(broadcast_b)).reshape(reshape_B).transpose(transpose_B)
if mult_A.shape != mult_B.shape:
- raise ValueError("error: matrix dimension mismatch")
+ raise ValueError("Error: matrix dimension mismatch")
res = eltwise_mult(mult_A, mult_B)
+ # reduce the axis and find the final transpose for the output
sum_R = sorted(sums, reverse=True)
for i in sum_R:
res = res.sum(axis=i)
@@ -1243,6 +1013,8 @@ def einsum2(A,B,ops):
+
+
def div(lhs, rhs, ret=None):
'''Elementi-wise division.