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 <[email protected]> Authored: Sun Apr 29 12:16:54 2018 +0800 Committer: sheyujian <[email protected]> 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()
