在tensorflow中卷积两个矩阵的最佳方法是什么?

时间:2017-12-27 11:29:40

标签: python tensorflow machine-learning deep-learning convolution

假设

    A = [[1,2,3],[4,5,6],[7,8,9]]
    B = [[1,2,1],[2,1,1],[1,1,2]]

在使用kernel_size = 2 * 2和stride = 1进行卷积后,输出应为

[[18,18],[28,37]]

我们需要在每2 * 2部分之间应用卷积运算         A的每2 * 2部分         如何使用tensorflow有效地执行此操作?         tensorflow有没有直接这样做的方法?

2 个答案:

答案 0 :(得分:3)

以下是使用tf.nn.conv2D执行此操作的直接方式:

Building the projects in this solution one at a time. To enable parallel 

build, please add the "/m" switch.
Build started 12/27/2017 4:42:15 PM.
Project "C:\Users\aleksandr.galkin\Desktop\RCnew\MyProject.sln" on node 1 (default targets).
ValidateSolutionConfiguration:
  Building solution configuration "Release|Any CPU".
Project "C:\Users\aleksandr.galkin\Desktop\RCnew\MyProject.sln" (1) is building "C:\Users\aleksandr.galkin\Desktop\RCnew\MyProject.WebApi\MyProject.WebApi.csproj" (2) on node 1 (default targets).
Project "C:\Users\aleksandr.galkin\Desktop\RCnew\MyProject.WebApi\MyProject.WebApi.csproj" (2) is building "C:\Users\aleksandr.galkin\Desktop\RCnew\MyProject.Data\MyProject.Data.csproj" (3:2) on node 1 (default targets).
GenerateBindingRedirects:
  No suggested binding redirects from ResolveAssemblyReferences.
GenerateTargetFrameworkMonikerAttribute:
Skipping target "GenerateTargetFrameworkMonikerAttribute" because all output files are up-to-date with respect to the input files.
CoreCompile:
Skipping target "CoreCompile" because all output files are up-to-date with respect to the input files.
_CopyFilesMarkedCopyLocal:
  Copying file from "C:\Users\aleksandr.galkin\Desktop\RCnew\packages\EntityFramework.6.1.0\lib\net45\EntityFramework.dll" to "C:\Some\Path\out\EntityFramework.dll".
  Copying file from "C:\Users\aleksandr.galkin\Desktop\RCnew\packages\EntityFramework.6.1.0\lib\net45\EntityFramework.SqlServer.dll" to "C:\Some\Path\out\EntityFramework.SqlServer.dll".
  Copying file from "C:\Users\aleksandr.galkin\Desktop\RCnew\packages\Microsoft.Owin.2.1.0\lib\net45\Microsoft.Owin.dll" to "C:\Some\Path\out\Microsoft.Owin.dll".
  Copying file from "C:\Users\aleksandr.galkin\Desktop\RCnew\packages\Microsoft.Owin.Security.2.1.0\lib\net45\Microsoft.Owin.Security.dll" to "C:\Some\Path\out\Microsoft.Owin.Security.dll".
  Copying file from "C:\Users\aleksandr.galkin\Desktop\RCnew\packages\Newtonsoft.Json.4.5.11\lib\net40\Newtonsoft.Json.dll" to "C:\Some\Path\out\Newtonsoft.Json.dll".
  Copying file from "C:\Users\aleksandr.galkin\Desktop\RCnew\packages\EntityFramework.6.1.0\lib\net45\EntityFramework.xml" to "C:\Some\Path\out\EntityFramework.xml".
  Copying file from "C:\Users\aleksandr.galkin\Desktop\RCnew\packages\EntityFramework.6.1.0\lib\net45\EntityFramework.SqlServer.xml" to "C:\Some\Path\out\EntityFramework.SqlServer.xml".
  Copying file from "C:\Users\aleksandr.galkin\Desktop\RCnew\packages\Microsoft.Owin.2.1.0\lib\net45\Microsoft.Owin.xml" to "C:\Some\Path\out\Microsoft.Owin.xml".
  Copying file from "C:\Users\aleksandr.galkin\Desktop\RCnew\packages\Microsoft.Owin.Security.2.1.0\lib\net45\Microsoft.Owin.Security.xml" to "C:\Some\Path\out\Microsoft.Owin.Security.xml".
  Copying file from "C:\Users\aleksandr.galkin\Desktop\RCnew\packages\Newtonsoft.Json.4.5.11\lib\net40\Newtonsoft.Json.xml" to "C:\Some\Path\out\Newtonsoft.Json.xml".
  Touching "C:\Users\aleksandr.galkin\Desktop\RCnew\MyProject.Data\obj\Release\MyProject.Data.csproj.CopyComplete".
_CopyAppConfigFile:
Skipping target "_CopyAppConfigFile" because all output files are up-to-date with respect to the input files.
CopyFilesToOutputDirectory:
  MyProject.Data -> C:\Some\Path\out\MyProject.Data.exe
Done Building Project "C:\Users\aleksandr.galkin\Desktop\RCnew\MyProject.Data\MyProject.Data.csproj" (default targets).
Project "C:\Users\aleksandr.galkin\Desktop\RCnew\MyProject.WebApi\MyProject.WebApi.csproj" (2) is building "C:\Users\aleksandr.galkin\Desktop\RCnew\MyProject.Helper\MyProject.Helper.csproj" (4:2) on node 1 (default targets).
GenerateTargetFrameworkMonikerAttribute:
Skipping target "GenerateTargetFrameworkMonikerAttribute" because all output files are up-to-date with respect to the input files.
CoreCompile:
Skipping target "CoreCompile" because all output files are up-to-date with respect to the input files.
CopyFilesToOutputDirectory:
  MyProject.Helper -> C:\Some\Path\out\MyProject.Helper.dll
Done Building Project "C:\Users\aleksandr.galkin\Desktop\RCnew\MyProject.Helper\MyProject.Helper.csproj" (default targets).
Project "C:\Users\aleksandr.galkin\Desktop\RCnew\MyProject.WebApi\MyProject.WebApi.csproj" (2) is building "C:\Users\aleksandr.galkin\Desktop\RCnew\MyProject.Infrastructure\MyProject.Infrastructure.csproj" (5:2) on node 1 (default targets).
GenerateTargetFrameworkMonikerAttribute:
Skipping target "GenerateTargetFrameworkMonikerAttribute" because all output files are up-to-date with respect to the input files.
CoreCompile:
Skipping target "CoreCompile" because all output files are up-to-date with respect to the input files.
_CopyFilesMarkedCopyLocal:
  Touching "C:\Users\aleksandr.galkin\Desktop\RCnew\MyProject.Infrastructure\obj\Release\MyProject.Infrastructure.csproj.CopyComplete".
_CopyAppConfigFile:
Skipping target "_CopyAppConfigFile" because all output files are up-to-date with respect to the input files.
CopyFilesToOutputDirectory:
  MyProject.Infrastructure -> C:\Some\Path\out\MyProject.Infrastructure.dll
Done Building Project "C:\Users\aleksandr.galkin\Desktop\RCnew\MyProject.Infrastructure\MyProject.Infrastructure.csproj" (default targets).
Project "C:\Users\aleksandr.galkin\Desktop\RCnew\MyProject.WebApi\MyProject.WebApi.csproj" (2) is building "C:\Users\aleksandr.galkin\Desktop\RCnew\MyProject.Service\MyProject.Service.csproj" (6:2) on node 1 (default targets).
GenerateTargetFrameworkMonikerAttribute:
Skipping target "GenerateTargetFrameworkMonikerAttribute" because all output files are up-to-date with respect to the input files.
CoreCompile:
Skipping target "CoreCompile" because all output files are up-to-date with respect to the input files.
_CopyFilesMarkedCopyLocal:
  Copying file from "C:\Users\aleksandr.galkin\Desktop\RCnew\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll" to "C:\Some\Path\out\Newtonsoft.Json.dll".
  Copying file from "C:\Users\aleksandr.galkin\Desktop\RCnew\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.xml" to "C:\Some\Path\out\Newtonsoft.Json.xml".
  Touching "C:\Users\aleksandr.galkin\Desktop\RCnew\MyProject.Service\obj\Release\MyProject.Service.csproj.CopyComplete".
_CopyAppConfigFile:
Skipping target "_CopyAppConfigFile" because all output files are up-to-date with respect to the input files.
CopyFilesToOutputDirectory:
  MyProject.Service -> C:\Some\Path\out\MyProject.Service.dll
Done Building Project "C:\Users\aleksandr.galkin\Desktop\RCnew\MyProject.Service\MyProject.Service.csproj" (default targets).
ResolveAssemblyReferences:
  No way to resolve conflict between "Microsoft.Owin, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" and "Microsoft.Owin, Version=2.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35". Choosing "Microsoft.Owin, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" arbitrarily.
  No way to resolve conflict between "Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed" and "Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed". Choosing "Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed" arbitrarily.
  No way to resolve conflict between "Microsoft.Owin, Version=2.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" and "Microsoft.Owin, Version=2.0.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35". Choosing "Microsoft.Owin, Version=2.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" arbitrarily.
  Consider app.config remapping of assembly "Microsoft.Owin, Culture=neutral, PublicKeyToken=31bf3856ad364e35" from Version "3.0.0.0" [] to Version "3.1.0.0" [C:\Users\aleksandr.galkin\Desktop\RCnew\packages\Microsoft.Owin.3.1.0\lib\net45\Microsoft.Owin.dll] to solve conflict and get rid of warning.
  Consider app.config remapping of assembly "System.Net.Http.Formatting, Culture=neutral, PublicKeyToken=31bf3856ad364e35" from Version "5.2.0.0" [] to Version "5.2.3.0" [C:\Users\aleksandr.galkin\Desktop\RCnew\packages\Microsoft.AspNet.WebApi.Client.5.2.3\lib\net45\System.Net.Http.Formatting.dll] to solve conflict and get rid of warning.
  Consider app.config remapping of assembly "System.Web.Http, Culture=neutral, PublicKeyToken=31bf3856ad364e35" from Version "5.2.0.0" [] to Version "5.2.3.0" [C:\Users\aleksandr.galkin\Desktop\RCnew\packages\Microsoft.AspNet.WebApi.Core.5.2.3\lib\net45\System.Web.Http.dll] to solve conflict and get rid of warning.
  Consider app.config remapping of assembly "Newtonsoft.Json, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed" from Version "6.0.0.0" [] to Version "9.0.0.0" [C:\Users\aleksandr.galkin\Desktop\RCnew\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll] to solve conflict and get rid of warning.
  Consider app.config remapping of assembly "Microsoft.Owin.Security, Culture=neutral, PublicKeyToken=31bf3856ad364e35" from Version "2.1.0.0" [C:\Some\Path\out\Microsoft.Owin.Security.dll] to Version "3.1.0.0" [C:\Users\aleksandr.galkin\Desktop\RCnew\packages\Microsoft.Owin.Security.3.1.0\lib\net45\Microsoft.Owin.Security.dll] to solve conflict and get rid of warning.
  Consider app.config remapping of assembly "System.Web.Http.Owin, Culture=neutral, PublicKeyToken=31bf3856ad364e35" from Version "5.2.0.0" [] to Version "5.2.3.0" [C:\Users\aleksandr.galkin\Desktop\RCnew\packages\Microsoft.AspNet.WebApi.Owin.5.2.3\lib\net45\System.Web.Http.Owin.dll] to solve conflict and get rid of warning.
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\Microsoft.Common.CurrentVersion.targets(1987,5): warning MSB3247: Found conflicts between different versions of the same dependent assembly. In Visual Studio, double-click this warning (or select it and press Enter) to fix the conflicts; otherwise, add the following binding redirects to the "runtime" node in the application configuration file: <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><dependentAssembly><assemblyIdentity name="Microsoft.Owin" culture="neutral" publicKeyToken="31bf3856ad364e35" /><bindingRedirect oldVersion="0.0.0.0-3.1.0.0" newVersion="3.1.0.0" /></dependentAssembly></assemblyBinding><assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><dependentAssembly><assemblyIdentity name="System.Net.Http.Formatting" culture="neutral" publicKeyToken="31bf3856ad364e35" /><bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" /></dependentAssembly></assemblyBinding><assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><dependentAssembly><assemblyIdentity name="System.Web.Http" culture="neutral" publicKeyToken="31bf3856ad364e35" /><bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" /></dependentAssembly></assemblyBinding><assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><dependentAssembly><assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" /><bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" /></dependentAssembly></assemblyBinding><assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><dependentAssembly><assemblyIdentity name="Microsoft.Owin.Security" culture="neutral" publicKeyToken="31bf3856ad364e35" /><bindingRedirect oldVersion="0.0.0.0-3.1.0.0" newVersion="3.1.0.0" /></dependentAssembly></assemblyBinding><assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><dependentAssembly><assemblyIdentity name="System.Web.Http.Owin" culture="neutral" publicKeyToken="31bf3856ad364e35" /><bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" /></dependentAssembly></assemblyBinding> [C:\Users\aleksandr.galkin\Desktop\RCnew\MyProject.WebApi\MyProject.WebApi.csproj]
GenerateTargetFrameworkMonikerAttribute:
Skipping target "GenerateTargetFrameworkMonikerAttribute" because all output files are up-to-date with respect to the input files.
CoreCompile:
Skipping target "CoreCompile" because all output files are up-to-date with respect to the input files.
_CopyFilesMarkedCopyLocal:
  Copying file from "C:\Users\aleksandr.galkin\Desktop\RCnew\packages\EntityFramework.6.1.3\lib\net45\EntityFramework.dll" to "C:\Some\Path\out\EntityFramework.dll".
  Copying file from "C:\Users\aleksandr.galkin\Desktop\RCnew\packages\EntityFramework.6.1.3\lib\net45\EntityFramework.SqlServer.dll" to "C:\Some\Path\out\EntityFramework.SqlServer.dll".
  Copying file from "C:\Users\aleksandr.galkin\Desktop\RCnew\packages\Microsoft.Owin.3.1.0\lib\net45\Microsoft.Owin.dll" to "C:\Some\Path\out\Microsoft.Owin.dll".
  Copying file from "C:\Users\aleksandr.galkin\Desktop\RCnew\packages\Microsoft.Owin.Security.3.1.0\lib\net45\Microsoft.Owin.Security.dll" to "C:\Some\Path\out\Microsoft.Owin.Security.dll".
  Copying file from "C:\Users\aleksandr.galkin\Desktop\RCnew\packages\EntityFramework.6.1.3\lib\net45\EntityFramework.xml" to "C:\Some\Path\out\EntityFramework.xml".
  Copying file from "C:\Users\aleksandr.galkin\Desktop\RCnew\packages\EntityFramework.6.1.3\lib\net45\EntityFramework.SqlServer.xml" to "C:\Some\Path\out\EntityFramework.SqlServer.xml".
  Copying file from "C:\Users\aleksandr.galkin\Desktop\RCnew\packages\Microsoft.Owin.3.1.0\lib\net45\Microsoft.Owin.xml" to "C:\Some\Path\out\Microsoft.Owin.xml".
  Copying file from "C:\Users\aleksandr.galkin\Desktop\RCnew\packages\Microsoft.Owin.Security.3.1.0\lib\net45\Microsoft.Owin.Security.xml" to "C:\Some\Path\out\Microsoft.Owin.Security.xml".
  Touching "C:\Users\aleksandr.galkin\Desktop\RCnew\MyProject.WebApi\obj\Release\MyProject.WebApi.csproj.CopyComplete".
_CopyOutOfDateSourceItemsToOutputDirectory:
Skipping target "_CopyOutOfDateSourceItemsToOutputDirectory" because all output files are up-to-date with respect to the input files.
CopyFilesToOutputDirectory:
  MyProject.WebApi -> C:\Some\Path\out\MyProject.dll
_CopyWebApplicationLegacy:
  Copying Web Application Project Files for MyProject.WebApi
Done Building Project "C:\Users\aleksandr.galkin\Desktop\RCnew\MyProject.WebApi\MyProject.WebApi.csproj" (default targets).
Project "C:\Users\aleksandr.galkin\Desktop\RCnew\MyProject.sln" (1) is building "C:\Users\aleksandr.galkin\Desktop\RCnew\MyProject_WebUI.metaproj" (7) on node 1 (default targets).
Build:
  C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_compiler.exe -v /localhost_53896 -p MyProject.WebUI\ -u -f C:\Some\Path\out_PublishedWebsites\MyProject.WebUI\ 
Done Building Project "C:\Users\aleksandr.galkin\Desktop\RCnew\MyProject_WebUI.metaproj" (default targets).
Done Building Project "C:\Users\aleksandr.galkin\Desktop\RCnew\MyProject.sln" (default targets).

Build succeeded.

"C:\Users\aleksandr.galkin\Desktop\RCnew\MyProject.sln" (default target) (1) ->
"C:\Users\aleksandr.galkin\Desktop\RCnew\MyProject.WebApi\MyProject.WebApi.csproj" (default target) (2) ->
(ResolveAssemblyReferences target) -> 
  C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\Microsoft.Common.CurrentVersion.targets(1987,5): warning MSB3247: Found conflicts between different versions of the same dependent assembly. In Visual Studio, double-click this warning (or select it and press Enter) to fix the conflicts; otherwise, add the following binding redirects to the "runtime" node in the application configuration file: <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><dependentAssembly><assemblyIdentity name="Microsoft.Owin" culture="neutral" publicKeyToken="31bf3856ad364e35" /><bindingRedirect oldVersion="0.0.0.0-3.1.0.0" newVersion="3.1.0.0" /></dependentAssembly></assemblyBinding><assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><dependentAssembly><assemblyIdentity name="System.Net.Http.Formatting" culture="neutral" publicKeyToken="31bf3856ad364e35" /><bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" /></dependentAssembly></assemblyBinding><assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><dependentAssembly><assemblyIdentity name="System.Web.Http" culture="neutral" publicKeyToken="31bf3856ad364e35" /><bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" /></dependentAssembly></assemblyBinding><assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><dependentAssembly><assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" /><bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" /></dependentAssembly></assemblyBinding><assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><dependentAssembly><assemblyIdentity name="Microsoft.Owin.Security" culture="neutral" publicKeyToken="31bf3856ad364e35" /><bindingRedirect oldVersion="0.0.0.0-3.1.0.0" newVersion="3.1.0.0" /></dependentAssembly></assemblyBinding><assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><dependentAssembly><assemblyIdentity name="System.Web.Http.Owin" culture="neutral" publicKeyToken="31bf3856ad364e35" /><bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" /></dependentAssembly></assemblyBinding> [C:\Users\aleksandr.galkin\Desktop\RCnew\MyProject.WebApi\MyProject.WebApi.csproj]

    1 Warning(s)
    0 Error(s)

我使用交互式会话来评估这些张量,但即使您定义了计算图,然后使用显式会话稍后运行它,这应该可以正常工作。

修改

此外,澄清一下,这种方法适用于任何In [1055]: A = np.array([[1,2,3],[4,5,6],[7,8,9]]) ...: B = np.array([[1,1,1],[1,1,1],[1,1,1]]) ...: # define input tensor In [1056]: tfA = tf.constant(A, dtype=tf.float32) # reshape it to 4D tensor (as needed by tf.nn.conv2d) In [1057]: tfA = tfA[tf.newaxis, :, :, tf.newaxis] # define kernel tensor In [1058]: tfK = tf.constant(B, dtype=tf.float32) # again reshape it to 4D tensor (also, we use 2x2 convolution) In [1059]: tfK = tfK[:-1, :-1, tf.newaxis, tf.newaxis] # convolving the input tensor with kernel In [1060]: convolved = tf.nn.conv2d(tfA, tfK, strides=[1, 1, 1, 1], padding="VALID") In [1061]: convolved.eval() Out[1061]: array([[[[ 12.], [ 16.]], [[ 24.], [ 28.]]]], dtype=float32) 内核张量(2x2)。请考虑以下示例,其中内核张量中的条目加倍。正如预期的那样,与上述例子中获得的结果相比,最终结果也将加倍。

另一个例子:

B

答案 1 :(得分:0)

这可能会对你有所帮助。

import numpy as np
from scipy.signal import convolve2d

def conv2(x, y, mode='same'):
    return np.rot90(convolve2d(np.rot90(x, 2), np.rot90(y, 2), mode=mode), 2)


A = [[1,2,3],[4,5,6],[7,8,9]]
B = [[1,1,1],[1,1,1],[1,1,1]]

print(conv2(A,B))

输出

[[12 21 16]
 [27 45 33]
 [24 39 28]]