diff --git a/.github/workflows/build_and_test.yml b/.github/workflows/build_and_test.yml index 070c7cbd7..9fd34fc49 100644 --- a/.github/workflows/build_and_test.yml +++ b/.github/workflows/build_and_test.yml @@ -28,9 +28,9 @@ jobs: - name: Test CPU version run: dotnet test --no-build --verbosity normal - name: uninstall redist cpu for unit tests - run: dotnet remove helpers/Tensorflow.UnitTest.RedistHolder package SciSharp.TensorFlow.Redist + run: dotnet remove tools/Tensorflow.UnitTest.RedistHolder package SciSharp.TensorFlow.Redist - name: install redist gpu for unit tests - run: dotnet add helpers/Tensorflow.UnitTest.RedistHolder package SciSharp.TensorFlow.Redist-Windows-GPU + run: dotnet add tools/Tensorflow.UnitTest.RedistHolder package SciSharp.TensorFlow.Redist-Windows-GPU - name: Restore dependencies run: dotnet restore - name: Build GPU version @@ -52,12 +52,12 @@ jobs: run: dotnet restore - name: Build CPU version run: dotnet build --no-restore - # - name: Test CPU version - # run: dotnet test --no-build --verbosity normal + - name: Test CPU version + run: dotnet test --no-build --verbosity normal - name: uninstall redist cpu for unit tests - run: dotnet remove helpers/Tensorflow.UnitTest.RedistHolder package SciSharp.TensorFlow.Redist + run: dotnet remove tools/Tensorflow.UnitTest.RedistHolder package SciSharp.TensorFlow.Redist - name: install redist gpu for unit tests - run: dotnet add helpers/Tensorflow.UnitTest.RedistHolder package SciSharp.TensorFlow.Redist-Linux-GPU + run: dotnet add tools/Tensorflow.UnitTest.RedistHolder package SciSharp.TensorFlow.Redist-Linux-GPU - name: Restore dependencies run: dotnet restore - name: Build GPU version diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 8f862e329..02601764c 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -53,7 +53,7 @@ jobs: } - name: Upload packages artifacts - uses: actions/upload-artifact@v1.0.0 + uses: actions/upload-artifact@v4.0.0 with: name: "drop-ci-packages" path: './packages' diff --git a/README.md b/README.md index c3ffdbaa5..75cad0aa7 100644 --- a/README.md +++ b/README.md @@ -2,16 +2,27 @@ **TensorFlow.NET** (TF.NET) provides a .NET Standard binding for [TensorFlow](https://www.tensorflow.org/). It aims to implement the complete Tensorflow API in C# which allows .NET developers to develop, train and deploy Machine Learning models with the cross-platform .NET Standard framework. TensorFlow.NET has built-in Keras high-level interface and is released as an independent package [TensorFlow.Keras](https://www.nuget.org/packages/TensorFlow.Keras/). +[![Discord](https://img.shields.io/discord/1106946823282761851?label=Discord)](https://discord.gg/qRVm82fKTS) +[![QQ群聊](https://img.shields.io/static/v1?label=QQ&message=群聊&color=brightgreen)](http://qm.qq.com/cgi-bin/qm/qr?_wv=1027&k=sN9VVMwbWjs5L0ATpizKKxOcZdEPMrp8&authKey=RLDw41bLTrEyEgZZi%2FzT4pYk%2BwmEFgFcrhs8ZbkiVY7a4JFckzJefaYNW6Lk4yPX&noverify=0&group_code=985366726) [![Join the chat at https://gitter.im/publiclab/publiclab](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/sci-sharp/community) [![CI Status](https://github.com/SciSharp/TensorFlow.NET/actions/workflows/build_and_test.yml/badge.svg)](https://github.com/SciSharp/TensorFlow.NET/actions/workflows/build_and_test.yml) -[![NuGet Badge](https://buildstats.info/nuget/TensorFlow.NET?includePreReleases=true)](https://www.nuget.org/packages/TensorFlow.NET) -[![MyGet Badge](https://img.shields.io/badge/dynamic/json?color=purple&label=nightly%20release&prefix=myget-v&query=items%5B0%5D.lower&url=https%3A%2F%2Fwww.myget.org%2FF%2Fscisharp%2Fapi%2Fv3%2Fregistration1%2Ftensorflow.net%2Findex.json)](https://www.myget.org/feed/scisharp/package/nuget/Tensorflow.NET) [![Documentation Status](https://readthedocs.org/projects/tensorflownet/badge/?version=latest)](https://tensorflownet.readthedocs.io/en/latest/?badge=latest) +[![TensorFlow.NET Badge](https://img.shields.io/nuget/v/TensorFlow.NET?label=TensorFlow.NET)](https://www.nuget.org/packages/TensorFlow.NET) +[![TensorFlow.Keras Badge](https://img.shields.io/nuget/v/TensorFlow.Keras?label=TensorFlow.Keras)](https://www.nuget.org/packages/TensorFlow.Keras) +[![MyGet Badge](https://img.shields.io/badge/dynamic/json?color=purple&label=Nightly%20Release&prefix=myget-v&query=items%5B0%5D.lower&url=https%3A%2F%2Fwww.myget.org%2FF%2Fscisharp%2Fapi%2Fv3%2Fregistration1%2Ftensorflow.net%2Findex.json)](https://www.myget.org/feed/scisharp/package/nuget/Tensorflow.NET) [![Badge](https://img.shields.io/badge/link-996.icu-red.svg)](https://996.icu/#/en_US) [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/javiercp/BinderTF.NET/master?urlpath=lab) English | [中文](docs/README-CN.md) +> [!IMPORTANT] +> We're happy that our work on tensorflow.net has attracted many users. However, at this time, none of the main maintainers of this repo is available for new features and bug fix. We won't refuse PRs and will help to review them. +> +> If you would like to be a contributor or maintainer of tensorflow.net, we'd like to help you to start up. +> +> We feel sorry for that and we'll resume the maintaining for this project once one of us has bandwidth for it. +> + *master branch and v0.100.x is corresponding to tensorflow v2.10, v0.6x branch is from tensorflow v2.6, v0.15-tensorflow1.15 is from tensorflow1.15. Please add `https://www.myget.org/F/scisharp/api/v3/index.json` to nuget source to use nightly release.* @@ -58,9 +69,12 @@ PM> Install-Package TensorFlow.Keras The second part is the computing support part. Only one of the following packages is needed, depending on your device and system. ``` -### CPU version for Windows, Linux and Mac +### CPU version for Windows and Linux PM> Install-Package SciSharp.TensorFlow.Redist +### CPU version for MacOS +PM> Install-Package SciSharp.TensorFlow.Redist-OSX + ### GPU version for Windows (CUDA and cuDNN are required) PM> Install-Package SciSharp.TensorFlow.Redist-Windows-GPU @@ -238,9 +252,9 @@ Buy our book to make open source project be sustainable [TensorFlow.NET实战](h ### Contact -Follow us on [Twitter](https://twitter.com/ScisharpStack), [Facebook](https://www.facebook.com/scisharp.stack.9), [Medium](https://medium.com/scisharp), [LinkedIn](https://www.linkedin.com/company/scisharp-stack/). +Join our chat on [Discord](https://discord.gg/qRVm82fKTS) or [Gitter](https://gitter.im/sci-sharp/community). -Join our chat on [Gitter](https://gitter.im/sci-sharp/community). +Follow us on [Twitter](https://twitter.com/ScisharpStack), [Facebook](https://www.facebook.com/scisharp.stack.9), [Medium](https://medium.com/scisharp), [LinkedIn](https://www.linkedin.com/company/scisharp-stack/). TensorFlow.NET is a part of [SciSharp STACK](https://scisharp.github.io/SciSharp/)
diff --git a/TensorFlow.NET.sln b/TensorFlow.NET.sln index 0c7d6e3c2..e0c273568 100644 --- a/TensorFlow.NET.sln +++ b/TensorFlow.NET.sln @@ -5,12 +5,8 @@ VisualStudioVersion = 17.4.33213.308 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tensorflow.Binding", "src\TensorFlowNET.Core\Tensorflow.Binding.csproj", "{FD682AC0-7B2D-45D3-8B0D-C6D678B04144}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tensorflow.Benchmark", "src\TensorFlowNet.Benchmarks\Tensorflow.Benchmark.csproj", "{3A6EB896-604F-4E25-B677-B8103BCF3D2E}" -EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tensorflow.Binding.UnitTest", "test\TensorFlowNET.UnitTest\Tensorflow.Binding.UnitTest.csproj", "{23C28035-2FCE-41F3-9A12-E73CE8A5AE32}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tensorflow.Console", "src\TensorFlowNET.Console\Tensorflow.Console.csproj", "{03F06299-3F4B-4449-A709-3A647657BC0C}" -EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tensorflow.Keras", "src\TensorFlowNET.Keras\Tensorflow.Keras.csproj", "{49D71826-C03D-4FA7-9BAC-22C1327E65CF}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tensorflow.Text", "src\TensorFlowNET.Text\Tensorflow.Text.csproj", "{1AB8108D-4FFE-4A16-88E7-328EAF686370}" @@ -31,9 +27,21 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{01A1787F-A9B EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{1B0918B9-65AD-4F34-A287-AF4597B27DBD}" EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "helpers", "helpers", "{E1A5D2B7-10AF-4876-85C0-7714EF274214}" +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tools", "tools", "{E1A5D2B7-10AF-4876-85C0-7714EF274214}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tensorflow.CodeGen", "tools\Tensorflow.CodeGen\Tensorflow.CodeGen.csproj", "{3D92142F-EEDB-469B-B03C-4E38728BFE4C}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tensorflow.Redist.NativeLibrarySplitter", "tools\Tensorflow.Redist.NativeLibrarySplitter\Tensorflow.Redist.NativeLibrarySplitter.csproj", "{AB131FA7-B7C3-4ABF-ABDE-E059C72A613C}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tensorflow.UnitTest.RedistHolder", "tools\Tensorflow.UnitTest.RedistHolder\Tensorflow.UnitTest.RedistHolder.csproj", "{D24FCAA5-548C-4251-B226-A1B6535D0845}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tensorflow.Benchmark", "tools\TensorFlowNET.Benchmarks\Tensorflow.Benchmark.csproj", "{C23563DB-FE21-48E7-A411-87A109E4A899}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tensorflow.Console", "tools\TensorFlowNET.Console\Tensorflow.Console.csproj", "{1DC32255-BA1F-4D6D-A9C9-5BD5ED71CAA0}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TensorFlow.Kernel.UnitTest", "test\TensorFlow.Kernel.UnitTest\TensorFlow.Kernel.UnitTest.csproj", "{654A027D-1364-4729-880B-144DFE1FF5BB}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tensorflow.UnitTest.RedistHolder", "helpers\Tensorflow.UnitTest.RedistHolder\Tensorflow.UnitTest.RedistHolder.csproj", "{62D543A2-8846-45A3-829B-5754B094A8E2}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tensorflow.UnitTest", "test\Tensorflow.UnitTest\Tensorflow.UnitTest.csproj", "{A73DF5A6-866E-4AED-9017-AA2EE86368C4}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -66,24 +74,6 @@ Global {FD682AC0-7B2D-45D3-8B0D-C6D678B04144}.Release|x64.Build.0 = Release|x64 {FD682AC0-7B2D-45D3-8B0D-C6D678B04144}.Release|x86.ActiveCfg = Release|Any CPU {FD682AC0-7B2D-45D3-8B0D-C6D678B04144}.Release|x86.Build.0 = Release|Any CPU - {3A6EB896-604F-4E25-B677-B8103BCF3D2E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {3A6EB896-604F-4E25-B677-B8103BCF3D2E}.Debug|Any CPU.Build.0 = Debug|Any CPU - {3A6EB896-604F-4E25-B677-B8103BCF3D2E}.Debug|x64.ActiveCfg = Debug|x64 - {3A6EB896-604F-4E25-B677-B8103BCF3D2E}.Debug|x64.Build.0 = Debug|x64 - {3A6EB896-604F-4E25-B677-B8103BCF3D2E}.Debug|x86.ActiveCfg = Debug|Any CPU - {3A6EB896-604F-4E25-B677-B8103BCF3D2E}.Debug|x86.Build.0 = Debug|Any CPU - {3A6EB896-604F-4E25-B677-B8103BCF3D2E}.GPU|Any CPU.ActiveCfg = Release|Any CPU - {3A6EB896-604F-4E25-B677-B8103BCF3D2E}.GPU|Any CPU.Build.0 = Release|Any CPU - {3A6EB896-604F-4E25-B677-B8103BCF3D2E}.GPU|x64.ActiveCfg = Release|x64 - {3A6EB896-604F-4E25-B677-B8103BCF3D2E}.GPU|x64.Build.0 = Release|x64 - {3A6EB896-604F-4E25-B677-B8103BCF3D2E}.GPU|x86.ActiveCfg = Release|Any CPU - {3A6EB896-604F-4E25-B677-B8103BCF3D2E}.GPU|x86.Build.0 = Release|Any CPU - {3A6EB896-604F-4E25-B677-B8103BCF3D2E}.Release|Any CPU.ActiveCfg = Release|Any CPU - {3A6EB896-604F-4E25-B677-B8103BCF3D2E}.Release|Any CPU.Build.0 = Release|Any CPU - {3A6EB896-604F-4E25-B677-B8103BCF3D2E}.Release|x64.ActiveCfg = Release|x64 - {3A6EB896-604F-4E25-B677-B8103BCF3D2E}.Release|x64.Build.0 = Release|x64 - {3A6EB896-604F-4E25-B677-B8103BCF3D2E}.Release|x86.ActiveCfg = Release|Any CPU - {3A6EB896-604F-4E25-B677-B8103BCF3D2E}.Release|x86.Build.0 = Release|Any CPU {23C28035-2FCE-41F3-9A12-E73CE8A5AE32}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {23C28035-2FCE-41F3-9A12-E73CE8A5AE32}.Debug|Any CPU.Build.0 = Debug|Any CPU {23C28035-2FCE-41F3-9A12-E73CE8A5AE32}.Debug|x64.ActiveCfg = Debug|x64 @@ -102,24 +92,6 @@ Global {23C28035-2FCE-41F3-9A12-E73CE8A5AE32}.Release|x64.Build.0 = Release|x64 {23C28035-2FCE-41F3-9A12-E73CE8A5AE32}.Release|x86.ActiveCfg = Release|Any CPU {23C28035-2FCE-41F3-9A12-E73CE8A5AE32}.Release|x86.Build.0 = Release|Any CPU - {03F06299-3F4B-4449-A709-3A647657BC0C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {03F06299-3F4B-4449-A709-3A647657BC0C}.Debug|Any CPU.Build.0 = Debug|Any CPU - {03F06299-3F4B-4449-A709-3A647657BC0C}.Debug|x64.ActiveCfg = Debug|x64 - {03F06299-3F4B-4449-A709-3A647657BC0C}.Debug|x64.Build.0 = Debug|x64 - {03F06299-3F4B-4449-A709-3A647657BC0C}.Debug|x86.ActiveCfg = Debug|Any CPU - {03F06299-3F4B-4449-A709-3A647657BC0C}.Debug|x86.Build.0 = Debug|Any CPU - {03F06299-3F4B-4449-A709-3A647657BC0C}.GPU|Any CPU.ActiveCfg = Release|Any CPU - {03F06299-3F4B-4449-A709-3A647657BC0C}.GPU|Any CPU.Build.0 = Release|Any CPU - {03F06299-3F4B-4449-A709-3A647657BC0C}.GPU|x64.ActiveCfg = Release|x64 - {03F06299-3F4B-4449-A709-3A647657BC0C}.GPU|x64.Build.0 = Release|x64 - {03F06299-3F4B-4449-A709-3A647657BC0C}.GPU|x86.ActiveCfg = Release|Any CPU - {03F06299-3F4B-4449-A709-3A647657BC0C}.GPU|x86.Build.0 = Release|Any CPU - {03F06299-3F4B-4449-A709-3A647657BC0C}.Release|Any CPU.ActiveCfg = Release|Any CPU - {03F06299-3F4B-4449-A709-3A647657BC0C}.Release|Any CPU.Build.0 = Release|Any CPU - {03F06299-3F4B-4449-A709-3A647657BC0C}.Release|x64.ActiveCfg = Release|x64 - {03F06299-3F4B-4449-A709-3A647657BC0C}.Release|x64.Build.0 = Release|x64 - {03F06299-3F4B-4449-A709-3A647657BC0C}.Release|x86.ActiveCfg = Release|Any CPU - {03F06299-3F4B-4449-A709-3A647657BC0C}.Release|x86.Build.0 = Release|Any CPU {49D71826-C03D-4FA7-9BAC-22C1327E65CF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {49D71826-C03D-4FA7-9BAC-22C1327E65CF}.Debug|Any CPU.Build.0 = Debug|Any CPU {49D71826-C03D-4FA7-9BAC-22C1327E65CF}.Debug|x64.ActiveCfg = Debug|x64 @@ -264,33 +236,139 @@ Global {7DEA8760-E401-4872-81F3-405F185A13A0}.Release|x64.Build.0 = Release|Any CPU {7DEA8760-E401-4872-81F3-405F185A13A0}.Release|x86.ActiveCfg = Release|Any CPU {7DEA8760-E401-4872-81F3-405F185A13A0}.Release|x86.Build.0 = Release|Any CPU - {62D543A2-8846-45A3-829B-5754B094A8E2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {62D543A2-8846-45A3-829B-5754B094A8E2}.Debug|Any CPU.Build.0 = Debug|Any CPU - {62D543A2-8846-45A3-829B-5754B094A8E2}.Debug|x64.ActiveCfg = Debug|Any CPU - {62D543A2-8846-45A3-829B-5754B094A8E2}.Debug|x64.Build.0 = Debug|Any CPU - {62D543A2-8846-45A3-829B-5754B094A8E2}.Debug|x86.ActiveCfg = Debug|Any CPU - {62D543A2-8846-45A3-829B-5754B094A8E2}.Debug|x86.Build.0 = Debug|Any CPU - {62D543A2-8846-45A3-829B-5754B094A8E2}.GPU|Any CPU.ActiveCfg = Debug|Any CPU - {62D543A2-8846-45A3-829B-5754B094A8E2}.GPU|Any CPU.Build.0 = Debug|Any CPU - {62D543A2-8846-45A3-829B-5754B094A8E2}.GPU|x64.ActiveCfg = Debug|Any CPU - {62D543A2-8846-45A3-829B-5754B094A8E2}.GPU|x64.Build.0 = Debug|Any CPU - {62D543A2-8846-45A3-829B-5754B094A8E2}.GPU|x86.ActiveCfg = Debug|Any CPU - {62D543A2-8846-45A3-829B-5754B094A8E2}.GPU|x86.Build.0 = Debug|Any CPU - {62D543A2-8846-45A3-829B-5754B094A8E2}.Release|Any CPU.ActiveCfg = Release|Any CPU - {62D543A2-8846-45A3-829B-5754B094A8E2}.Release|Any CPU.Build.0 = Release|Any CPU - {62D543A2-8846-45A3-829B-5754B094A8E2}.Release|x64.ActiveCfg = Release|Any CPU - {62D543A2-8846-45A3-829B-5754B094A8E2}.Release|x64.Build.0 = Release|Any CPU - {62D543A2-8846-45A3-829B-5754B094A8E2}.Release|x86.ActiveCfg = Release|Any CPU - {62D543A2-8846-45A3-829B-5754B094A8E2}.Release|x86.Build.0 = Release|Any CPU + {3D92142F-EEDB-469B-B03C-4E38728BFE4C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3D92142F-EEDB-469B-B03C-4E38728BFE4C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3D92142F-EEDB-469B-B03C-4E38728BFE4C}.Debug|x64.ActiveCfg = Debug|Any CPU + {3D92142F-EEDB-469B-B03C-4E38728BFE4C}.Debug|x64.Build.0 = Debug|Any CPU + {3D92142F-EEDB-469B-B03C-4E38728BFE4C}.Debug|x86.ActiveCfg = Debug|Any CPU + {3D92142F-EEDB-469B-B03C-4E38728BFE4C}.Debug|x86.Build.0 = Debug|Any CPU + {3D92142F-EEDB-469B-B03C-4E38728BFE4C}.GPU|Any CPU.ActiveCfg = Debug|Any CPU + {3D92142F-EEDB-469B-B03C-4E38728BFE4C}.GPU|Any CPU.Build.0 = Debug|Any CPU + {3D92142F-EEDB-469B-B03C-4E38728BFE4C}.GPU|x64.ActiveCfg = Debug|Any CPU + {3D92142F-EEDB-469B-B03C-4E38728BFE4C}.GPU|x64.Build.0 = Debug|Any CPU + {3D92142F-EEDB-469B-B03C-4E38728BFE4C}.GPU|x86.ActiveCfg = Debug|Any CPU + {3D92142F-EEDB-469B-B03C-4E38728BFE4C}.GPU|x86.Build.0 = Debug|Any CPU + {3D92142F-EEDB-469B-B03C-4E38728BFE4C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3D92142F-EEDB-469B-B03C-4E38728BFE4C}.Release|Any CPU.Build.0 = Release|Any CPU + {3D92142F-EEDB-469B-B03C-4E38728BFE4C}.Release|x64.ActiveCfg = Release|Any CPU + {3D92142F-EEDB-469B-B03C-4E38728BFE4C}.Release|x64.Build.0 = Release|Any CPU + {3D92142F-EEDB-469B-B03C-4E38728BFE4C}.Release|x86.ActiveCfg = Release|Any CPU + {3D92142F-EEDB-469B-B03C-4E38728BFE4C}.Release|x86.Build.0 = Release|Any CPU + {AB131FA7-B7C3-4ABF-ABDE-E059C72A613C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {AB131FA7-B7C3-4ABF-ABDE-E059C72A613C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {AB131FA7-B7C3-4ABF-ABDE-E059C72A613C}.Debug|x64.ActiveCfg = Debug|Any CPU + {AB131FA7-B7C3-4ABF-ABDE-E059C72A613C}.Debug|x64.Build.0 = Debug|Any CPU + {AB131FA7-B7C3-4ABF-ABDE-E059C72A613C}.Debug|x86.ActiveCfg = Debug|Any CPU + {AB131FA7-B7C3-4ABF-ABDE-E059C72A613C}.Debug|x86.Build.0 = Debug|Any CPU + {AB131FA7-B7C3-4ABF-ABDE-E059C72A613C}.GPU|Any CPU.ActiveCfg = Debug|Any CPU + {AB131FA7-B7C3-4ABF-ABDE-E059C72A613C}.GPU|Any CPU.Build.0 = Debug|Any CPU + {AB131FA7-B7C3-4ABF-ABDE-E059C72A613C}.GPU|x64.ActiveCfg = Debug|Any CPU + {AB131FA7-B7C3-4ABF-ABDE-E059C72A613C}.GPU|x64.Build.0 = Debug|Any CPU + {AB131FA7-B7C3-4ABF-ABDE-E059C72A613C}.GPU|x86.ActiveCfg = Debug|Any CPU + {AB131FA7-B7C3-4ABF-ABDE-E059C72A613C}.GPU|x86.Build.0 = Debug|Any CPU + {AB131FA7-B7C3-4ABF-ABDE-E059C72A613C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {AB131FA7-B7C3-4ABF-ABDE-E059C72A613C}.Release|Any CPU.Build.0 = Release|Any CPU + {AB131FA7-B7C3-4ABF-ABDE-E059C72A613C}.Release|x64.ActiveCfg = Release|Any CPU + {AB131FA7-B7C3-4ABF-ABDE-E059C72A613C}.Release|x64.Build.0 = Release|Any CPU + {AB131FA7-B7C3-4ABF-ABDE-E059C72A613C}.Release|x86.ActiveCfg = Release|Any CPU + {AB131FA7-B7C3-4ABF-ABDE-E059C72A613C}.Release|x86.Build.0 = Release|Any CPU + {D24FCAA5-548C-4251-B226-A1B6535D0845}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D24FCAA5-548C-4251-B226-A1B6535D0845}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D24FCAA5-548C-4251-B226-A1B6535D0845}.Debug|x64.ActiveCfg = Debug|Any CPU + {D24FCAA5-548C-4251-B226-A1B6535D0845}.Debug|x64.Build.0 = Debug|Any CPU + {D24FCAA5-548C-4251-B226-A1B6535D0845}.Debug|x86.ActiveCfg = Debug|Any CPU + {D24FCAA5-548C-4251-B226-A1B6535D0845}.Debug|x86.Build.0 = Debug|Any CPU + {D24FCAA5-548C-4251-B226-A1B6535D0845}.GPU|Any CPU.ActiveCfg = Debug|Any CPU + {D24FCAA5-548C-4251-B226-A1B6535D0845}.GPU|Any CPU.Build.0 = Debug|Any CPU + {D24FCAA5-548C-4251-B226-A1B6535D0845}.GPU|x64.ActiveCfg = Debug|Any CPU + {D24FCAA5-548C-4251-B226-A1B6535D0845}.GPU|x64.Build.0 = Debug|Any CPU + {D24FCAA5-548C-4251-B226-A1B6535D0845}.GPU|x86.ActiveCfg = Debug|Any CPU + {D24FCAA5-548C-4251-B226-A1B6535D0845}.GPU|x86.Build.0 = Debug|Any CPU + {D24FCAA5-548C-4251-B226-A1B6535D0845}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D24FCAA5-548C-4251-B226-A1B6535D0845}.Release|Any CPU.Build.0 = Release|Any CPU + {D24FCAA5-548C-4251-B226-A1B6535D0845}.Release|x64.ActiveCfg = Release|Any CPU + {D24FCAA5-548C-4251-B226-A1B6535D0845}.Release|x64.Build.0 = Release|Any CPU + {D24FCAA5-548C-4251-B226-A1B6535D0845}.Release|x86.ActiveCfg = Release|Any CPU + {D24FCAA5-548C-4251-B226-A1B6535D0845}.Release|x86.Build.0 = Release|Any CPU + {C23563DB-FE21-48E7-A411-87A109E4A899}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C23563DB-FE21-48E7-A411-87A109E4A899}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C23563DB-FE21-48E7-A411-87A109E4A899}.Debug|x64.ActiveCfg = Debug|x64 + {C23563DB-FE21-48E7-A411-87A109E4A899}.Debug|x64.Build.0 = Debug|x64 + {C23563DB-FE21-48E7-A411-87A109E4A899}.Debug|x86.ActiveCfg = Debug|Any CPU + {C23563DB-FE21-48E7-A411-87A109E4A899}.Debug|x86.Build.0 = Debug|Any CPU + {C23563DB-FE21-48E7-A411-87A109E4A899}.GPU|Any CPU.ActiveCfg = Debug|Any CPU + {C23563DB-FE21-48E7-A411-87A109E4A899}.GPU|Any CPU.Build.0 = Debug|Any CPU + {C23563DB-FE21-48E7-A411-87A109E4A899}.GPU|x64.ActiveCfg = Debug|x64 + {C23563DB-FE21-48E7-A411-87A109E4A899}.GPU|x64.Build.0 = Debug|x64 + {C23563DB-FE21-48E7-A411-87A109E4A899}.GPU|x86.ActiveCfg = Debug|Any CPU + {C23563DB-FE21-48E7-A411-87A109E4A899}.GPU|x86.Build.0 = Debug|Any CPU + {C23563DB-FE21-48E7-A411-87A109E4A899}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C23563DB-FE21-48E7-A411-87A109E4A899}.Release|Any CPU.Build.0 = Release|Any CPU + {C23563DB-FE21-48E7-A411-87A109E4A899}.Release|x64.ActiveCfg = Release|x64 + {C23563DB-FE21-48E7-A411-87A109E4A899}.Release|x64.Build.0 = Release|x64 + {C23563DB-FE21-48E7-A411-87A109E4A899}.Release|x86.ActiveCfg = Release|Any CPU + {C23563DB-FE21-48E7-A411-87A109E4A899}.Release|x86.Build.0 = Release|Any CPU + {1DC32255-BA1F-4D6D-A9C9-5BD5ED71CAA0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1DC32255-BA1F-4D6D-A9C9-5BD5ED71CAA0}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1DC32255-BA1F-4D6D-A9C9-5BD5ED71CAA0}.Debug|x64.ActiveCfg = Debug|x64 + {1DC32255-BA1F-4D6D-A9C9-5BD5ED71CAA0}.Debug|x64.Build.0 = Debug|x64 + {1DC32255-BA1F-4D6D-A9C9-5BD5ED71CAA0}.Debug|x86.ActiveCfg = Debug|Any CPU + {1DC32255-BA1F-4D6D-A9C9-5BD5ED71CAA0}.Debug|x86.Build.0 = Debug|Any CPU + {1DC32255-BA1F-4D6D-A9C9-5BD5ED71CAA0}.GPU|Any CPU.ActiveCfg = Debug|Any CPU + {1DC32255-BA1F-4D6D-A9C9-5BD5ED71CAA0}.GPU|Any CPU.Build.0 = Debug|Any CPU + {1DC32255-BA1F-4D6D-A9C9-5BD5ED71CAA0}.GPU|x64.ActiveCfg = Debug|x64 + {1DC32255-BA1F-4D6D-A9C9-5BD5ED71CAA0}.GPU|x64.Build.0 = Debug|x64 + {1DC32255-BA1F-4D6D-A9C9-5BD5ED71CAA0}.GPU|x86.ActiveCfg = Debug|Any CPU + {1DC32255-BA1F-4D6D-A9C9-5BD5ED71CAA0}.GPU|x86.Build.0 = Debug|Any CPU + {1DC32255-BA1F-4D6D-A9C9-5BD5ED71CAA0}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1DC32255-BA1F-4D6D-A9C9-5BD5ED71CAA0}.Release|Any CPU.Build.0 = Release|Any CPU + {1DC32255-BA1F-4D6D-A9C9-5BD5ED71CAA0}.Release|x64.ActiveCfg = Release|x64 + {1DC32255-BA1F-4D6D-A9C9-5BD5ED71CAA0}.Release|x64.Build.0 = Release|x64 + {1DC32255-BA1F-4D6D-A9C9-5BD5ED71CAA0}.Release|x86.ActiveCfg = Release|Any CPU + {1DC32255-BA1F-4D6D-A9C9-5BD5ED71CAA0}.Release|x86.Build.0 = Release|Any CPU + {654A027D-1364-4729-880B-144DFE1FF5BB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {654A027D-1364-4729-880B-144DFE1FF5BB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {654A027D-1364-4729-880B-144DFE1FF5BB}.Debug|x64.ActiveCfg = Debug|Any CPU + {654A027D-1364-4729-880B-144DFE1FF5BB}.Debug|x64.Build.0 = Debug|Any CPU + {654A027D-1364-4729-880B-144DFE1FF5BB}.Debug|x86.ActiveCfg = Debug|Any CPU + {654A027D-1364-4729-880B-144DFE1FF5BB}.Debug|x86.Build.0 = Debug|Any CPU + {654A027D-1364-4729-880B-144DFE1FF5BB}.GPU|Any CPU.ActiveCfg = Debug|Any CPU + {654A027D-1364-4729-880B-144DFE1FF5BB}.GPU|Any CPU.Build.0 = Debug|Any CPU + {654A027D-1364-4729-880B-144DFE1FF5BB}.GPU|x64.ActiveCfg = Debug|Any CPU + {654A027D-1364-4729-880B-144DFE1FF5BB}.GPU|x64.Build.0 = Debug|Any CPU + {654A027D-1364-4729-880B-144DFE1FF5BB}.GPU|x86.ActiveCfg = Debug|Any CPU + {654A027D-1364-4729-880B-144DFE1FF5BB}.GPU|x86.Build.0 = Debug|Any CPU + {654A027D-1364-4729-880B-144DFE1FF5BB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {654A027D-1364-4729-880B-144DFE1FF5BB}.Release|Any CPU.Build.0 = Release|Any CPU + {654A027D-1364-4729-880B-144DFE1FF5BB}.Release|x64.ActiveCfg = Release|Any CPU + {654A027D-1364-4729-880B-144DFE1FF5BB}.Release|x64.Build.0 = Release|Any CPU + {654A027D-1364-4729-880B-144DFE1FF5BB}.Release|x86.ActiveCfg = Release|Any CPU + {654A027D-1364-4729-880B-144DFE1FF5BB}.Release|x86.Build.0 = Release|Any CPU + {A73DF5A6-866E-4AED-9017-AA2EE86368C4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A73DF5A6-866E-4AED-9017-AA2EE86368C4}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A73DF5A6-866E-4AED-9017-AA2EE86368C4}.Debug|x64.ActiveCfg = Debug|Any CPU + {A73DF5A6-866E-4AED-9017-AA2EE86368C4}.Debug|x64.Build.0 = Debug|Any CPU + {A73DF5A6-866E-4AED-9017-AA2EE86368C4}.Debug|x86.ActiveCfg = Debug|Any CPU + {A73DF5A6-866E-4AED-9017-AA2EE86368C4}.Debug|x86.Build.0 = Debug|Any CPU + {A73DF5A6-866E-4AED-9017-AA2EE86368C4}.GPU|Any CPU.ActiveCfg = Debug|Any CPU + {A73DF5A6-866E-4AED-9017-AA2EE86368C4}.GPU|Any CPU.Build.0 = Debug|Any CPU + {A73DF5A6-866E-4AED-9017-AA2EE86368C4}.GPU|x64.ActiveCfg = Debug|Any CPU + {A73DF5A6-866E-4AED-9017-AA2EE86368C4}.GPU|x64.Build.0 = Debug|Any CPU + {A73DF5A6-866E-4AED-9017-AA2EE86368C4}.GPU|x86.ActiveCfg = Debug|Any CPU + {A73DF5A6-866E-4AED-9017-AA2EE86368C4}.GPU|x86.Build.0 = Debug|Any CPU + {A73DF5A6-866E-4AED-9017-AA2EE86368C4}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A73DF5A6-866E-4AED-9017-AA2EE86368C4}.Release|Any CPU.Build.0 = Release|Any CPU + {A73DF5A6-866E-4AED-9017-AA2EE86368C4}.Release|x64.ActiveCfg = Release|Any CPU + {A73DF5A6-866E-4AED-9017-AA2EE86368C4}.Release|x64.Build.0 = Release|Any CPU + {A73DF5A6-866E-4AED-9017-AA2EE86368C4}.Release|x86.ActiveCfg = Release|Any CPU + {A73DF5A6-866E-4AED-9017-AA2EE86368C4}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection GlobalSection(NestedProjects) = preSolution {FD682AC0-7B2D-45D3-8B0D-C6D678B04144} = {01A1787F-A9BE-4221-84E8-6360DD010AB6} - {3A6EB896-604F-4E25-B677-B8103BCF3D2E} = {E1A5D2B7-10AF-4876-85C0-7714EF274214} {23C28035-2FCE-41F3-9A12-E73CE8A5AE32} = {1B0918B9-65AD-4F34-A287-AF4597B27DBD} - {03F06299-3F4B-4449-A709-3A647657BC0C} = {E1A5D2B7-10AF-4876-85C0-7714EF274214} {49D71826-C03D-4FA7-9BAC-22C1327E65CF} = {01A1787F-A9BE-4221-84E8-6360DD010AB6} {1AB8108D-4FFE-4A16-88E7-328EAF686370} = {01A1787F-A9BE-4221-84E8-6360DD010AB6} {F17AAECB-960A-4E18-A270-BAD776F0E55B} = {01A1787F-A9BE-4221-84E8-6360DD010AB6} @@ -299,7 +377,13 @@ Global {3F5388FF-FBB4-462B-8F6F-829FFBAEB8A3} = {1B0918B9-65AD-4F34-A287-AF4597B27DBD} {9738D16A-CFA0-405C-A7DF-D3D203B0CB18} = {01A1787F-A9BE-4221-84E8-6360DD010AB6} {7DEA8760-E401-4872-81F3-405F185A13A0} = {1B0918B9-65AD-4F34-A287-AF4597B27DBD} - {62D543A2-8846-45A3-829B-5754B094A8E2} = {E1A5D2B7-10AF-4876-85C0-7714EF274214} + {3D92142F-EEDB-469B-B03C-4E38728BFE4C} = {E1A5D2B7-10AF-4876-85C0-7714EF274214} + {AB131FA7-B7C3-4ABF-ABDE-E059C72A613C} = {E1A5D2B7-10AF-4876-85C0-7714EF274214} + {D24FCAA5-548C-4251-B226-A1B6535D0845} = {E1A5D2B7-10AF-4876-85C0-7714EF274214} + {C23563DB-FE21-48E7-A411-87A109E4A899} = {E1A5D2B7-10AF-4876-85C0-7714EF274214} + {1DC32255-BA1F-4D6D-A9C9-5BD5ED71CAA0} = {E1A5D2B7-10AF-4876-85C0-7714EF274214} + {654A027D-1364-4729-880B-144DFE1FF5BB} = {1B0918B9-65AD-4F34-A287-AF4597B27DBD} + {A73DF5A6-866E-4AED-9017-AA2EE86368C4} = {1B0918B9-65AD-4F34-A287-AF4597B27DBD} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {2DEAD3CC-486B-4918-A607-50B0DE7B114A} diff --git a/data/img001.bmp b/data/img001.bmp new file mode 100644 index 000000000..d149d76f1 Binary files /dev/null and b/data/img001.bmp differ diff --git a/src/TensorFlowNET.Core/APIs/c_api.cs b/src/TensorFlowNET.Core/APIs/c_api.cs index 10f678e0a..a91b86827 100644 --- a/src/TensorFlowNET.Core/APIs/c_api.cs +++ b/src/TensorFlowNET.Core/APIs/c_api.cs @@ -16,6 +16,7 @@ limitations under the License. using System; using System.Runtime.InteropServices; +using static Tensorflow.CppShapeInferenceResult.Types; namespace Tensorflow { @@ -50,6 +51,35 @@ public static string StringPiece(IntPtr handle) return handle == IntPtr.Zero ? String.Empty : Marshal.PtrToStringAnsi(handle); } + public unsafe static byte[] ByteStringPiece(Buffer? handle) + { + if (handle is null) + { + return new byte[0]; + } + var data = handle.ToArray(); + return data; + } + + public unsafe static byte[] ByteStringPieceFromNativeString(IntPtr handle) + { + if (handle == IntPtr.Zero) + { + return new byte[0]; + } + + byte* str_data = (byte*)handle.ToPointer(); + List bytes = new List(); + byte current = 255; + while (current != ((byte)'\0')) + { + current = *(str_data++); + bytes.Add(current); + } + var data = bytes.ToArray(); + return data; + } + [UnmanagedFunctionPointer(CallingConvention.Winapi)] public delegate void Deallocator(IntPtr data, IntPtr size, ref DeallocatorArgs args); diff --git a/src/TensorFlowNET.Core/APIs/c_api.customize.cs b/src/TensorFlowNET.Core/APIs/c_api.customize.cs index d2aab9ac0..bee4897ee 100644 --- a/src/TensorFlowNET.Core/APIs/c_api.customize.cs +++ b/src/TensorFlowNET.Core/APIs/c_api.customize.cs @@ -8,10 +8,10 @@ namespace Tensorflow public partial class c_api { [DllImport(TensorFlowLibName)] - public static extern void TFC_SetAttr(SafeGraphHandle graph, IntPtr op, string attr_name, SafeBufferHandle attr_value_proto, SafeStatusHandle status); + public static extern void TF_SetAttr(SafeGraphHandle graph, IntPtr op, string attr_name, SafeBufferHandle attr_value_proto, SafeStatusHandle status); [DllImport(TensorFlowLibName)] - public static extern IntPtr TFC_GetHandleShapeAndType(SafeGraphHandle c_graph, TF_Output output); + public static extern SafeBufferHandle TF_GetHandleShapeAndType(SafeGraphHandle c_graph, TF_Output output); [DllImport(TensorFlowLibName)] - public static extern void TFC_SetHandleShapeAndType(SafeGraphHandle c_graph, TF_Output output, byte[] data, long proto_len, SafeStatusHandle status); + public static extern void TF_SetHandleShapeAndType(SafeGraphHandle c_graph, TF_Output output, byte[] data, long proto_len, SafeStatusHandle status); } } diff --git a/src/TensorFlowNET.Core/APIs/tf.array.cs b/src/TensorFlowNET.Core/APIs/tf.array.cs index a2c91983e..b529cd319 100644 --- a/src/TensorFlowNET.Core/APIs/tf.array.cs +++ b/src/TensorFlowNET.Core/APIs/tf.array.cs @@ -44,7 +44,8 @@ public partial class tensorflow /// /// public Tensor batch_to_space_nd(T input, int[] block_shape, int[,] crops, string name = null) - => gen_array_ops.batch_to_space_nd(input, block_shape, crops, name: name); + => gen_array_ops.batch_to_space_nd(ops.convert_to_tensor(input), ops.convert_to_tensor(block_shape), + ops.convert_to_tensor(crops), name: name); /// /// Apply boolean mask to tensor. @@ -90,8 +91,7 @@ public Tensor concat(IEnumerable values, int axis, string name = "concat return identity(values.First(), name: scope); }); } - - return gen_array_ops.concat_v2(values.ToArray(), axis, name: name); + return array_ops.concat(values.ToArray(), axis, name: name); } /// @@ -115,7 +115,7 @@ public Tensor expand_dims(Tensor input, int axis = -1, string name = null) /// /// public Tensor fill(Tensor dims, T value, string name = null) - => gen_array_ops.fill(dims, value, name: name); + => gen_array_ops.fill(dims, ops.convert_to_tensor(value), name: name); public Tensor fill(Shape dims, T value, string name = null) => array_ops.fill(dims, value, name: name); @@ -138,7 +138,17 @@ public Tensor identity(Tensor input, string name = null) /// /// public Tensor gather(Tensor @params, Tensor indices, string name = null, int axis = 0) - => array_ops.gather(@params, indices, name: name, axis: axis); + => array_ops.gather(@params, indices, name: name, axis: ops.convert_to_tensor(axis)); + + /// + /// Gather slices from `params` into a Tensor with shape specified by `indices`. + /// + /// + /// + /// + /// + public Tensor gather_nd(Tensor @params, Tensor indices, string name = null) + => gen_array_ops.gather_nd(@params, indices, name: name); /// /// Return the elements, either from `x` or `y`, depending on the `condition`. @@ -162,14 +172,17 @@ public Tensor transpose(T1 a, Axis perm = null, string name = "transpose", b /// Reverses specific dimensions of a tensor. /// /// - /// + /// The indices of the dimensions to reverse. Must be in the range [-rank(tensor), rank(tensor)). /// /// - public Tensor reverse(Tensor tensor, int[] axis, string name = null) - => gen_array_ops.reverse(tensor, axis, name: name); - - public Tensor reverse(Tensor tensor, Tensor axis, string name = null) - => gen_array_ops.reverse(tensor, axis, name: name); + public Tensor reverse(Tensor tensor, Axis axis, string name = null) + { + if (axis.IsScalar) + { + axis = new Axis(axis.axis); + } + return array_ops.reverse(tensor, axis, name: name); + } /// /// Returns the rank of a tensor. @@ -189,7 +202,8 @@ public Tensor rank(Tensor input, string name = null) /// A name for the operation (optional). /// A `Tensor` the same type as `input`. public Tensor slice(Tensor input, Tb[] begin, Ts[] size, string name = null) - => array_ops.slice(input, begin, size, name: name); + => array_ops.slice(input, begin.Select(x => ops.convert_to_tensor(x)).ToArray(), + size.Select(x => ops.convert_to_tensor(x)).ToArray(), name: name); public Tensor squeeze(Tensor input, int axis, string name = null, int squeeze_dims = -1) => array_ops.squeeze(input, new[] { axis }, name); @@ -255,7 +269,7 @@ public Tensor pad(Tensor tensor, Tensor paddings, string mode = "CONSTANT", stri /// A name for the operation (optional). /// A `Tensor`. Has the same type as `input`. public Tensor placeholder_with_default(T input, int[] shape, string name = null) - => gen_array_ops.placeholder_with_default(input, shape, name: name); + => gen_array_ops.placeholder_with_default(ops.convert_to_tensor(input), shape, name: name); /// /// Returns the shape of a tensor. diff --git a/src/TensorFlowNET.Core/APIs/tf.control_flow.cs b/src/TensorFlowNET.Core/APIs/tf.control_flow.cs index 239487e05..cd5a71e50 100644 --- a/src/TensorFlowNET.Core/APIs/tf.control_flow.cs +++ b/src/TensorFlowNET.Core/APIs/tf.control_flow.cs @@ -46,10 +46,10 @@ public Tensor while_loop(Func cond, Tensor loop_vars, int parallel_iterations = 10) { - Func cond1 = x + Func cond1 = x => cond(x[0]); - Func body1 = x + Func body1 = x => new[] { body(x[0]) }; var results = control_flow_ops.while_loop(cond1, @@ -58,9 +58,9 @@ public Tensor while_loop(Func cond, return results[0]; } - public Tensor[] while_loop(Func cond, - Func body, - Tensor[] loop_vars, + public Tensor[] while_loop(Func cond, + Func body, + Tensors loop_vars, int parallel_iterations = 10, string name = null) => control_flow_ops.while_loop(cond, body, loop_vars, diff --git a/src/TensorFlowNET.Core/APIs/tf.image.cs b/src/TensorFlowNET.Core/APIs/tf.image.cs index 9230b50dc..41ef52967 100644 --- a/src/TensorFlowNET.Core/APIs/tf.image.cs +++ b/src/TensorFlowNET.Core/APIs/tf.image.cs @@ -14,6 +14,10 @@ You may obtain a copy of the License at limitations under the License. ******************************************************************************/ +using OneOf.Types; +using System; +using System.Buffers.Text; +using Tensorflow.Contexts; using static Tensorflow.Binding; namespace Tensorflow @@ -162,17 +166,108 @@ public Tensor ssim_multiscale(Tensor img1, Tensor img2, float max_val, float[] p public Tensor sobel_edges(Tensor image) => image_ops_impl.sobel_edges(image); - public Tensor decode_jpeg(Tensor contents, - int channels = 0, - int ratio = 1, - bool fancy_upscaling = true, - bool try_recover_truncated = false, - int acceptable_fraction = 1, - string dct_method = "", - string name = null) - => gen_image_ops.decode_jpeg(contents, channels: channels, ratio: ratio, - fancy_upscaling: fancy_upscaling, try_recover_truncated: try_recover_truncated, - acceptable_fraction: acceptable_fraction, dct_method: dct_method); + /// + /// Adjust contrast of RGB or grayscale images. + /// + /// Images to adjust. At least 3-D. + /// + /// A float multiplier for adjusting contrast. + /// The contrast-adjusted image or images. + public Tensor adjust_contrast(Tensor images, float contrast_factor, string name = null) + => gen_image_ops.adjust_contrastv2(images, contrast_factor, name); + + /// + /// Adjust hue of RGB images. + /// + /// RGB image or images. The size of the last dimension must be 3. + /// float. How much to add to the hue channel. + /// A name for this operation (optional). + /// Adjusted image(s), same shape and DType as `image`. + /// if `delta` is not in the interval of `[-1, 1]`. + public Tensor adjust_hue(Tensor images, float delta, string name = null) + { + if (tf.Context.executing_eagerly()) + { + if (delta < -1f || delta > 1f) + throw new ValueError("delta must be in the interval [-1, 1]"); + } + return gen_image_ops.adjust_hue(images, delta, name: name); + } + + /// + /// Adjust saturation of RGB images. + /// + /// RGB image or images. The size of the last dimension must be 3. + /// float. Factor to multiply the saturation by. + /// A name for this operation (optional). + /// Adjusted image(s), same shape and DType as `image`. + public Tensor adjust_saturation(Tensor image, float saturation_factor, string name = null) + => gen_image_ops.adjust_saturation(image, saturation_factor, name); + + /// + /// Greedily selects a subset of bounding boxes in descending order of score. + /// + /// + /// A 4-D float `Tensor` of shape `[batch_size, num_boxes, q, 4]`. If `q` + /// is 1 then same boxes are used for all classes otherwise, if `q` is equal + /// to number of classes, class-specific boxes are used. + /// + /// + /// A 3-D float `Tensor` of shape `[batch_size, num_boxes, num_classes]` + /// representing a single score corresponding to each box(each row of boxes). + /// + /// + /// A scalar integer `Tensor` representing the + /// maximum number of boxes to be selected by non-max suppression per class + /// + /// + /// A int32 scalar representing maximum number of boxes retained + /// over all classes.Note that setting this value to a large number may + /// result in OOM error depending on the system workload. + /// + /// + /// A float representing the threshold for deciding whether boxes + /// overlap too much with respect to IOU. + /// + /// + /// A float representing the threshold for deciding when to + /// remove boxes based on score. + /// + /// + /// If false, the output nmsed boxes, scores and classes are + /// padded/clipped to `max_total_size`. If true, the output nmsed boxes, scores and classes are padded to be of length `max_size_per_class`*`num_classes`, + /// unless it exceeds `max_total_size` in which case it is clipped to `max_total_size`. Defaults to false. + /// + /// + /// If true, the coordinates of output nmsed boxes will be clipped + /// to[0, 1]. If false, output the box coordinates as it is. Defaults to true. + /// + /// + /// 'nmsed_boxes': A [batch_size, max_detections, 4] float32 tensor containing the non-max suppressed boxes. + /// 'nmsed_scores': A [batch_size, max_detections] float32 tensor containing the scores for the boxes. + /// 'nmsed_classes': A [batch_size, max_detections] float32 tensor containing the class for boxes. + /// 'valid_detections': A [batch_size] int32 tensor indicating the number of + /// valid detections per batch item. Only the top valid_detections[i] entries + /// in nms_boxes[i], nms_scores[i] and nms_class[i] are valid. The rest of the + /// entries are zero paddings. + /// + public (Tensor, Tensor, Tensor, Tensor) combined_non_max_suppression( + Tensor boxes, + Tensor scores, + int max_output_size_per_class, + int max_total_size, + float iou_threshold, + float score_threshold, + bool pad_per_class = false, + bool clip_boxes = true) + { + var iou_threshold_t = ops.convert_to_tensor(iou_threshold, TF_DataType.TF_FLOAT, name: "iou_threshold"); + var score_threshold_t = ops.convert_to_tensor(score_threshold, TF_DataType.TF_FLOAT, name: "score_threshold"); + var max_total_size_t = ops.convert_to_tensor(max_total_size); + var max_output_size_per_class_t = ops.convert_to_tensor(max_output_size_per_class); + return gen_image_ops.combined_non_max_suppression(boxes, scores, max_output_size_per_class_t, max_total_size_t, + iou_threshold_t, score_threshold_t, pad_per_class, clip_boxes); + } /// /// Extracts crops from the input image tensor and resizes them using bilinear sampling or nearest neighbor sampling (possibly with aspect ratio change) to a common output size specified by crop_size. This is more general than the crop_to_bounding_box op which extracts a fixed size slice from the input image and does not allow resizing or aspect ratio change. @@ -187,7 +282,19 @@ public Tensor decode_jpeg(Tensor contents, /// A name for the operation (optional). /// A 4-D tensor of shape [num_boxes, crop_height, crop_width, depth]. public Tensor crop_and_resize(Tensor image, Tensor boxes, Tensor box_ind, Tensor crop_size, string method = "bilinear", float extrapolation_value = 0f, string name = null) => - image_ops_impl.crop_and_resize(image, boxes, box_ind, crop_size, method, extrapolation_value, name); + gen_image_ops.crop_and_resize(image, boxes, box_ind, crop_size, method, extrapolation_value, name); + + public Tensor decode_jpeg(Tensor contents, + int channels = 0, + int ratio = 1, + bool fancy_upscaling = true, + bool try_recover_truncated = false, + int acceptable_fraction = 1, + string dct_method = "", + string name = null) + => gen_image_ops.decode_jpeg(contents, channels: channels, ratio: ratio, + fancy_upscaling: fancy_upscaling, try_recover_truncated: try_recover_truncated, + acceptable_fraction: acceptable_fraction, dct_method: dct_method); public Tensor extract_glimpse(Tensor input, Tensor size, Tensor offsets, bool centered = true, bool normalized = true, bool uniform_noise = true, string name = null) @@ -232,6 +339,13 @@ public Tensor decode_image(Tensor contents, int channels = 0, TF_DataType dtype => image_ops_impl.decode_image(contents, channels: channels, dtype: dtype, name: name, expand_animations: expand_animations); + public Tensor encode_png(Tensor contents, string name = null) + => image_ops_impl.encode_png(contents, name: name); + + public Tensor encode_jpeg(Tensor contents, string name = null) + => image_ops_impl.encode_jpeg(contents, name: name); + + /// /// Convenience function to check if the 'contents' encodes a JPEG image. /// diff --git a/src/TensorFlowNET.Core/APIs/tf.io.cs b/src/TensorFlowNET.Core/APIs/tf.io.cs index be1e86e6c..ea1e44b28 100644 --- a/src/TensorFlowNET.Core/APIs/tf.io.cs +++ b/src/TensorFlowNET.Core/APIs/tf.io.cs @@ -16,6 +16,7 @@ limitations under the License. using System.Collections.Generic; using Tensorflow.IO; +using Tensorflow.Operations; namespace Tensorflow { @@ -46,6 +47,12 @@ public Operation save_v2(Tensor prefix, string[] tensor_names, public Tensor[] restore_v2(Tensor prefix, string[] tensor_names, string[] shape_and_slices, TF_DataType[] dtypes, string name = null) => ops.restore_v2(prefix, tensor_names, shape_and_slices, dtypes, name: name); + + public Operation write_file(string filename, Tensor conentes, string name = null) + => write_file(Tensorflow.ops.convert_to_tensor(filename, TF_DataType.TF_STRING), conentes, name); + + public Operation write_file(Tensor filename, Tensor conentes, string name = null) + => gen_ops.write_file(filename, conentes, name); } public GFile gfile = new GFile(); diff --git a/src/TensorFlowNET.Core/APIs/tf.math.cs b/src/TensorFlowNET.Core/APIs/tf.math.cs index 83653c8bb..da54a9dd7 100644 --- a/src/TensorFlowNET.Core/APIs/tf.math.cs +++ b/src/TensorFlowNET.Core/APIs/tf.math.cs @@ -14,6 +14,7 @@ You may obtain a copy of the License at limitations under the License. ******************************************************************************/ +using Tensorflow.NumPy; using Tensorflow.Operations; namespace Tensorflow @@ -42,10 +43,20 @@ public Tensor erf(Tensor x, string name = null) public Tensor multiply(Tensor x, Tensor y, string name = null) => math_ops.multiply(x, y, name: name); - public Tensor divide_no_nan(Tensor a, Tensor b, string name = null) => math_ops.div_no_nan(a, b); + /// + /// Computes the Euclidean norm of elements across dimensions of a tensor. + /// + /// The tensor to reduce. Should have numeric type. + /// The dimensions to reduce. If `None` (the default), reduces all dimensions.Must be in the range `[-rank(input_tensor), rank(input_tensor))` + /// If true, retains reduced dimensions with length 1. + /// A name for the operation (optional). + /// The reduced tensor, of the same dtype as the input_tensor. + public Tensor reduce_euclidean_norm(Tensor input_tensor, Axis? axis = null, bool keepdims = false, string name = null) + => math_ops.reduce_euclidean_norm(input_tensor, axis: axis, keepdims: keepdims, name); + public Tensor square(Tensor x, string name = null) => math_ops.square(x, name: name); @@ -130,7 +141,7 @@ public Tensor add(Tensor a, Tensor b, string name = null) => gen_math_ops.add(a, b, name: name); public Tensor add(Tx a, Ty b, string name = null) - => gen_math_ops.add(a, b, name: name); + => gen_math_ops.add(ops.convert_to_tensor(a), ops.convert_to_tensor(b), name: name); /// /// Adds all input tensors element-wise. @@ -151,10 +162,10 @@ public Tensor atan(Tensor x, string name = null) => gen_math_ops.atan(x, name); public Tensor arg_max(Tensor input, int dimension, TF_DataType output_type = TF_DataType.TF_INT64, string name = null) - => gen_math_ops.arg_max(input, dimension, output_type: output_type, name: name); + => gen_math_ops.arg_max(input, ops.convert_to_tensor(dimension), output_type: output_type, name: name); public Tensor arg_min(Tensor input, int dimension, TF_DataType output_type = TF_DataType.TF_INT64, string name = null) - => gen_math_ops.arg_min(input, dimension, output_type: output_type, name: name); + => gen_math_ops.arg_min(input, ops.convert_to_tensor(dimension), output_type: output_type, name: name); public Tensor is_finite(Tensor input, string name = null) => gen_math_ops.is_finite(input, name); @@ -199,7 +210,7 @@ public Tensor cos(Tensor x, string name = null) => gen_math_ops.cos(x, name); public Tensor cos(float x, string name = null) - => gen_math_ops.cos(x, name); + => gen_math_ops.cos(ops.convert_to_tensor(x), name); /// /// Computes hyperbolic cosine of x element-wise. @@ -235,7 +246,7 @@ public Tensor floor(Tensor x, string name = null) /// /// public Tensor greater(Tx x, Ty y, string name = null) - => gen_math_ops.greater(x, y, name); + => gen_math_ops.greater(ops.convert_to_tensor(x), ops.convert_to_tensor(y), name); /// /// Returns the truth value of (x >= y) element-wise. @@ -247,7 +258,7 @@ public Tensor greater(Tx x, Ty y, string name = null) /// /// public Tensor greater_equal(Tx x, Ty y, string name = null) - => gen_math_ops.greater_equal(x, y, name); + => gen_math_ops.greater_equal(ops.convert_to_tensor(x), ops.convert_to_tensor(y), name); /// /// Returns the truth value of (x < y) element-wise. @@ -259,7 +270,7 @@ public Tensor greater_equal(Tx x, Ty y, string name = null) /// /// public Tensor less(Tx x, Ty y, string name = null) - => gen_math_ops.less(x, y, name); + => gen_math_ops.less(ops.convert_to_tensor(x), ops.convert_to_tensor(y), name); /// /// Computes the log of the absolute value of `Gamma(x)` element-wise. @@ -280,7 +291,7 @@ public Tensor lgamma(Tensor x, string name = null) /// /// public Tensor less_equal(Tx x, Ty y, string name = null) - => gen_math_ops.less_equal(x, y, name); + => gen_math_ops.less_equal(ops.convert_to_tensor(x), ops.convert_to_tensor(y), name); /// /// Computes natural logarithm of (1 + x) element-wise. @@ -292,7 +303,7 @@ public Tensor log1p(Tensor x, string name = null) => gen_math_ops.log1p(x, name); public Tensor logical_and(T x, T y, string name = null) - => gen_math_ops.logical_and(x, y, name); + => gen_math_ops.logical_and(ops.convert_to_tensor(x), ops.convert_to_tensor(y), name); public Tensor logical_not(Tensor x, string name = null) => gen_math_ops.logical_not(x, name); @@ -301,7 +312,10 @@ public Tensor logical_or(Tensor x, Tensor y, string name = null) => gen_math_ops.logical_or(x, y, name); public Tensor logical_xor(Tensor x, Tensor y, string name = "LogicalXor") - => gen_math_ops.logical_xor(x, y, name); + { + return gen_math_ops.logical_and(gen_math_ops.logical_or(x, y), + gen_math_ops.logical_not(gen_math_ops.logical_and(x, y)), name); + } /// /// Clips tensor values to a specified min and max. @@ -312,7 +326,7 @@ public Tensor logical_xor(Tensor x, Tensor y, string name = "LogicalXor") /// /// public Tensor _clip_by_value(Tensor t, Tensor clip_value_min, Tensor clip_value_max, string name = null) - => gen_math_ops._clip_by_value(t, clip_value_min, clip_value_max); + => gen_math_ops.clip_by_value(t, clip_value_min, clip_value_max); /// /// Clips tensor values to a specified min and max. @@ -345,13 +359,13 @@ public Tensor clip_by_value(Tensor t, T1 clip_value_min, T2 clip_value_m => clip_ops.clip_by_value(t, clip_value_min, clip_value_max, name); public Tensor sub(Tx a, Ty b, string name = null) - => gen_math_ops.sub(a, b, name: name); + => gen_math_ops.sub(ops.convert_to_tensor(a), ops.convert_to_tensor(b), name: name); public Tensor divide(Tensor a, Tensor b) => a / b; public Tensor sqrt(Tensor a, string name = null) - => gen_math_ops.sqrt(a, name); + => math_ops.sqrt(a, name); public Tensor sign(Tensor a, string name = null) => gen_math_ops.sign(a, name); @@ -396,7 +410,7 @@ public Tensor atan2(Tensor y, Tensor x, string name = null) /// /// public Tensor max(Tx input, Ty axis, bool keep_dims = false, string name = null) - => gen_math_ops._max(input, axis, keep_dims: keep_dims, name: name); + => gen_math_ops.max(ops.convert_to_tensor(input), ops.convert_to_tensor(axis), keep_dims: keep_dims, name: name); /// /// Computes the minimum of elements across dimensions of a tensor. @@ -409,7 +423,7 @@ public Tensor max(Tx input, Ty axis, bool keep_dims = false, string name /// /// public Tensor min(Tx input, Ty axis, bool keep_dims = false, string name = null) - => gen_math_ops._min(input, axis, keep_dims: keep_dims, name: name); + => gen_math_ops.min(ops.convert_to_tensor(input), ops.convert_to_tensor(axis), keep_dims: keep_dims, name: name); /// /// Returns the max of x and y (i.e. x > y ? x : y) element-wise. @@ -421,7 +435,7 @@ public Tensor min(Tx input, Ty axis, bool keep_dims = false, string name /// /// public Tensor maximum(T1 x, T2 y, string name = null) - => gen_math_ops.maximum(x, y, name: name); + => gen_math_ops.maximum(ops.convert_to_tensor(x), ops.convert_to_tensor(y), name: name); /// /// Returns the min of x and y (i.e. x < y ? x : y) element-wise. @@ -433,7 +447,7 @@ public Tensor maximum(T1 x, T2 y, string name = null) /// /// public Tensor minimum(T1 x, T2 y, string name = null) - => gen_math_ops.minimum(x, y, name: name); + => gen_math_ops.minimum(ops.convert_to_tensor(x), ops.convert_to_tensor(y), name: name); public Tensor multiply(Tensor x, Tensor y, string name = null) => gen_math_ops.mul(x, y, name: name); @@ -448,8 +462,19 @@ public Tensor multiply(Tensor x, Tensor y, string name = null) /// /// public Tensor multiply(Tx x, Ty y, string name = null) - => gen_math_ops.mul(x, y, name: name); - + => gen_math_ops.mul(ops.convert_to_tensor(x), ops.convert_to_tensor(y), name: name); + /// + /// return scalar product + /// + /// + /// + /// + /// + /// + /// + /// + public Tensor dot_prod(Tx x, Ty y, NDArray axes, string name = null) + => math_ops.tensordot(convert_to_tensor(x), convert_to_tensor(y), axes, name: name); public Tensor negative(Tensor x, string name = null) => gen_math_ops.neg(x, name); @@ -577,7 +602,7 @@ public Tensor sigmoid(T x, string name = null) => math_ops.sigmoid(x, name: name); public Tensor sum(Tensor input, int axis, bool keep_dims = false, string name = null) - => gen_math_ops._sum(input, axis, keep_dims: keep_dims, name: name); + => gen_math_ops.sum(input, ops.convert_to_tensor(axis), keep_dims: keep_dims, name: name); public Tensor reduce_mean(Tensor input_tensor, Axis? axis = null, bool keepdims = false, string name = null, int? reduction_indices = null) => math_ops.reduce_mean(input_tensor, axis: axis, keepdims: keepdims, name: name, reduction_indices: reduction_indices); @@ -597,5 +622,7 @@ public Tensor squared_difference(Tensor x, Tensor y, string name = null) => gen_math_ops.squared_difference(x: x, y: y, name: name); public Tensor complex(Tensor real, Tensor imag, Tensorflow.TF_DataType? dtype = null, string name = null) => gen_ops.complex(real, imag, dtype, name); + public Tensor exp(Tensor x, + string name = null) => gen_math_ops.exp(x, name); } } diff --git a/src/TensorFlowNET.Core/APIs/tf.nn.cs b/src/TensorFlowNET.Core/APIs/tf.nn.cs index 1595e52fc..112c48628 100644 --- a/src/TensorFlowNET.Core/APIs/tf.nn.cs +++ b/src/TensorFlowNET.Core/APIs/tf.nn.cs @@ -14,6 +14,7 @@ You may obtain a copy of the License at limitations under the License. ******************************************************************************/ +using System.Xml.Linq; using Tensorflow.Operations; using Tensorflow.Operations.Activation; using static Tensorflow.Binding; @@ -29,21 +30,8 @@ public class nn_internal public Tensor conv2d(Tensor input, Tensor filter, int[] strides, string padding, bool use_cudnn_on_gpu = true, string data_format = "NHWC", int[] dilations = null, string name = null) { - var parameters = new Conv2dParams - { - Input = input, - Filter = filter, - Strides = strides, - Padding = padding, - UseCudnnOnGpu = use_cudnn_on_gpu, - DataFormat = data_format, - Name = name - }; - - if (dilations != null) - parameters.Dilations = dilations; - - return gen_nn_ops.conv2d(parameters); + return gen_nn_ops.conv2d(input, filter, strides, padding, use_cudnn_on_gpu, + data_format: data_format, dilations: dilations, name: name); } public Tensor[] ctc_greedy_decoder(Tensor inputs, Tensor sequence_length, bool merge_repeated = true, string name = null) @@ -113,16 +101,21 @@ public Tensor embedding_lookup(Tensor @params, name: name); public IActivation relu() => new relu(); + + public IActivation swish() => new swish(); public IActivation tanh() => new tanh(); public IActivation softmax() => new softmax(); public Tensor tanh(Tensor x, string name = null) - => gen_nn_ops.tanh(x, name); + => gen_math_ops.tanh(x, name); public Tensor relu(Tensor features, string name = null) => gen_nn_ops.relu(features, name); + public Tensor relu6(Tensor features, string name = null) + => gen_nn_ops.relu6(features, name); + public Tensor[] fused_batch_norm(Tensor x, Tensor scale, Tensor offset, @@ -139,6 +132,26 @@ public Tensor[] fused_batch_norm(Tensor x, name: name, exponential_avg_factor: exponential_avg_factor); + /// + /// Normalizes a tensor by `mean` and `variance`, and applies (optionally) a`scale` \\(\gamma\\) to it, as well as an `offset` \\(\beta\\). + /// + /// A floating point tensor. + /// A mean `Tensor`. + /// A variance `Tensor`. + /// An offset `Tensor`, often denoted \\(\beta\\) in equations, or NULL. If present, will be added to the normalized tensor. + /// A scale `Tensor`, often denoted \\(\gamma\\) in equations, or NULL. If present, the scale is applied to the normalized tensor. + /// A small float number to avoid dividing by 0. + /// A name for this operation. + /// the normalized, scaled, offset tensor. + public Tensor batch_normalization(Tensor x, + Tensor mean, + Tensor variance, + Tensor offset, + Tensor scale, + float variance_epsilon, + string name = null) => nn_impl.batch_normalization(x, mean, variance, offset, scale, variance_epsilon, name); + + public Tensor max_pool(Tensor value, int[] ksize, int[] strides, string padding, string data_format = "NHWC", string name = null) => nn_ops.max_pool(value, ksize, strides, padding, data_format: data_format, name: name); @@ -146,14 +159,14 @@ public Tensor in_top_k(Tensor predictions, Tensor targets, int k, string name = => nn_ops.in_top_k(predictions, targets, k, name); public Tensor[] top_k(Tensor input, int k = 1, bool sorted = true, string name = null) - => gen_nn_ops.top_kv2(input, k: k, sorted: sorted, name: name); + => gen_nn_ops.top_kv2(input, k: ops.convert_to_tensor(k), sorted: sorted, name: name); public Tensor bias_add(Tensor value, IVariableV1 bias, string data_format = null, string name = null) { return tf_with(ops.name_scope(name, "BiasAdd", new { value, bias }), scope => { name = scope; - return gen_nn_ops.bias_add(value, bias, data_format: data_format, name: name); + return gen_nn_ops.bias_add(value, ops.convert_to_tensor(bias), data_format: data_format, name: name); }); } @@ -172,7 +185,7 @@ public Tensor l2_loss(Tensor t, string name = null) /// public Tensor lrn(Tensor input, int depth_radius = 5, int bias = 1, int alpha = 1, float beta = 0.5f, string name = null) - => gen_nn_ops.local_response_normalization(input, depth_radius: depth_radius, bias: bias, + => gen_nn_ops.lrn(input, depth_radius: depth_radius, bias: bias, alpha: alpha, beta: beta, name: name); public Tensor leaky_relu(Tensor features, float alpha = 0.2f, string name = null) diff --git a/src/TensorFlowNET.Core/APIs/tf.reshape.cs b/src/TensorFlowNET.Core/APIs/tf.reshape.cs index cdd5194a2..102a81323 100644 --- a/src/TensorFlowNET.Core/APIs/tf.reshape.cs +++ b/src/TensorFlowNET.Core/APIs/tf.reshape.cs @@ -31,6 +31,6 @@ public Tensor reshape(Tensor tensor, public Tensor reshape(Tensor tensor, object[] shape, string name = null) - => gen_array_ops.reshape(tensor, shape, name); + => array_ops.reshape(tensor, shape, name); } } diff --git a/src/TensorFlowNET.Core/APIs/tf.tensor.cs b/src/TensorFlowNET.Core/APIs/tf.tensor.cs index 35efde06b..b03168ab3 100644 --- a/src/TensorFlowNET.Core/APIs/tf.tensor.cs +++ b/src/TensorFlowNET.Core/APIs/tf.tensor.cs @@ -46,10 +46,10 @@ public Tensor strided_slice(Tensor input, T[] begin, T[] end, T[] strides = n int ellipsis_mask = 0, int new_axis_mask = 0, int shrink_axis_mask = 0, - string name = null) => gen_array_ops.strided_slice(input: input, - begin: begin, - end: end, - strides: strides, + string name = null) => array_ops.strided_slice(input, + begin: ops.convert_to_tensor(begin), + end: ops.convert_to_tensor(end), + strides: ops.convert_to_tensor(strides), begin_mask: begin_mask, end_mask: end_mask, ellipsis_mask: ellipsis_mask, @@ -68,20 +68,27 @@ public Tensor strided_slice(Tensor input, T[] begin, T[] end, T[] strides = n /// A name for the operation (optional) /// if num_or_size_splits is a scalar returns num_or_size_splits Tensor objects; /// if num_or_size_splits is a 1-D Tensor returns num_or_size_splits.get_shape[0] Tensor objects resulting from splitting value. - public Tensor[] split(Tensor value, int num_split, Tensor axis, string name = null) + public Tensor[] split(Tensor value, int num_split, Axis axis, string name = null) => array_ops.split( value: value, - num_split: num_split, + num_or_size_splits: num_split, axis: axis, name: name); - public Tensor[] split(Tensor value, int num_split, int axis, string name = null) + public Tensor[] split(Tensor value, int[] num_split, Axis axis, string name = null) => array_ops.split( value: value, - num_split: num_split, + num_or_size_splits: num_split, axis: axis, name: name); + //public Tensor[] split(Tensor value, int num_split, Axis axis, string name = null) + // => array_ops.split( + // value: value, + // num_or_size_splits: num_split, + // axis: axis, + // name: name); + public Tensor ensure_shape(Tensor x, Shape shape, string name = null) { return gen_ops.ensure_shape(x, shape, name); diff --git a/src/TensorFlowNET.Core/APIs/tf.tile.cs b/src/TensorFlowNET.Core/APIs/tf.tile.cs index be03e453c..a3b497e8a 100644 --- a/src/TensorFlowNET.Core/APIs/tf.tile.cs +++ b/src/TensorFlowNET.Core/APIs/tf.tile.cs @@ -23,7 +23,7 @@ public Tensor tile(Tensor input, Tensor multiples, string name = null) => gen_array_ops.tile(input, multiples, name); public Tensor tile(Tensor input, object[] multiples, string name = null) - => gen_array_ops.tile(input, multiples, name); + => array_ops.tile(input, constant_op.constant(shape_utils.from_object_array(multiples).dims), name); public Tensor tile(Tensor input, Shape multiples, string name = null) { diff --git a/src/TensorFlowNET.Core/Attributes/c_api.ops.cs b/src/TensorFlowNET.Core/Attributes/c_api.ops.cs index 2a22413b0..ba6f653a1 100644 --- a/src/TensorFlowNET.Core/Attributes/c_api.ops.cs +++ b/src/TensorFlowNET.Core/Attributes/c_api.ops.cs @@ -57,6 +57,21 @@ public partial class c_api [DllImport(TensorFlowLibName)] public static extern int TF_OperationGetAttrValueProto(IntPtr oper, string attr_name, SafeBufferHandle output_attr_value, SafeStatusHandle status); + [DllImport(TensorFlowLibName)] + public static extern void TF_OperationGetAttrType(IntPtr oper, string attr_name, IntPtr value, SafeStatusHandle status); + + [DllImport(TensorFlowLibName)] + public static extern void TF_OperationGetAttrInt(IntPtr oper, string attr_name, IntPtr value, SafeStatusHandle status); + + [DllImport(TensorFlowLibName)] + public static extern void TF_OperationGetAttrFloat(IntPtr oper, string attr_name, IntPtr value, SafeStatusHandle status); + + [DllImport(TensorFlowLibName)] + public static extern void TF_OperationGetAttrBool(IntPtr oper, string attr_name, IntPtr value, SafeStatusHandle status); + + [DllImport(TensorFlowLibName)] + public static extern void TF_OperationGetAttrShape(IntPtr oper, string attr_name, long[] value, int num_dims, SafeStatusHandle status); + [DllImport(TensorFlowLibName)] public static extern void TF_SetAttrBool(IntPtr desc, string attr_name, bool value); diff --git a/src/TensorFlowNET.Core/Binding.Util.cs b/src/TensorFlowNET.Core/Binding.Util.cs index 8df39334a..99ed5c1f3 100644 --- a/src/TensorFlowNET.Core/Binding.Util.cs +++ b/src/TensorFlowNET.Core/Binding.Util.cs @@ -486,7 +486,28 @@ public static Shape GetShape(this object data) throw new NotImplementedException(""); } } - + public static NDArray GetFlattenArray(NDArray x) + { + switch (x.GetDataType()) + { + case TF_DataType.TF_FLOAT: + x = x.ToArray(); + break; + case TF_DataType.TF_DOUBLE: + x = x.ToArray(); + break; + case TF_DataType.TF_INT16: + case TF_DataType.TF_INT32: + x = x.ToArray(); + break; + case TF_DataType.TF_INT64: + x = x.ToArray(); + break; + default: + break; + } + return x; + } public static TF_DataType GetDataType(this object data) { var type = data.GetType(); @@ -503,7 +524,7 @@ public static TF_DataType GetDataType(this object data) case Tensors tensors: return tensors.dtype; case IEnumerable tensors: - return tensors.First().dtype; + return tensors.Where(x => x is not null).First().dtype; case RefVariable variable: return variable.dtype; case ResourceVariable variable: diff --git a/src/TensorFlowNET.Core/Clustering/_InitializeClustersOpFactory.cs b/src/TensorFlowNET.Core/Clustering/_InitializeClustersOpFactory.cs index adb26ef29..1b295fcfd 100644 --- a/src/TensorFlowNET.Core/Clustering/_InitializeClustersOpFactory.cs +++ b/src/TensorFlowNET.Core/Clustering/_InitializeClustersOpFactory.cs @@ -88,7 +88,7 @@ private Tensor _initialize() public Tensor op() { - var x = control_flow_ops.cond(gen_math_ops.equal(_num_remaining, 0), + var x = control_flow_ops.cond(gen_math_ops.equal(_num_remaining, ops.convert_to_tensor(0)), () => { return check_ops.assert_equal(_cluster_centers_initialized, true); diff --git a/src/TensorFlowNET.Core/Extensions/DictionaryExtension.cs b/src/TensorFlowNET.Core/Common/Extensions/DictionaryExtension.cs similarity index 100% rename from src/TensorFlowNET.Core/Extensions/DictionaryExtension.cs rename to src/TensorFlowNET.Core/Common/Extensions/DictionaryExtension.cs diff --git a/src/TensorFlowNET.Core/Extensions/JObjectExtensions.cs b/src/TensorFlowNET.Core/Common/Extensions/JObjectExtensions.cs similarity index 80% rename from src/TensorFlowNET.Core/Extensions/JObjectExtensions.cs rename to src/TensorFlowNET.Core/Common/Extensions/JObjectExtensions.cs index 2e758dbf1..6ceba445a 100644 --- a/src/TensorFlowNET.Core/Extensions/JObjectExtensions.cs +++ b/src/TensorFlowNET.Core/Common/Extensions/JObjectExtensions.cs @@ -3,16 +3,16 @@ using System.Collections.Generic; using System.Text; -namespace Tensorflow.Extensions +namespace Tensorflow.Common.Extensions { public static class JObjectExtensions { public static T? TryGetOrReturnNull(this JObject obj, string key) { var res = obj[key]; - if(res is null) + if (res is null) { - return default(T); + return default; } else { diff --git a/src/TensorFlowNET.Core/Common/Extensions/LinqExtensions.cs b/src/TensorFlowNET.Core/Common/Extensions/LinqExtensions.cs new file mode 100644 index 000000000..287b48cc3 --- /dev/null +++ b/src/TensorFlowNET.Core/Common/Extensions/LinqExtensions.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Tensorflow.Common.Extensions +{ + public static class LinqExtensions + { +#if NETSTANDARD2_0 + public static IEnumerable TakeLast(this IEnumerable sequence, int count) + { + return sequence.Skip(sequence.Count() - count); + } + + public static IEnumerable SkipLast(this IEnumerable sequence, int count) + { + return sequence.Take(sequence.Count() - count); + } +#endif + public static Tensors ToTensors(this Tensor[] tensors) + { + return new Tensors(tensors); + } + + public static Tensors ToTensors(this IList tensors) + { + return new Tensors(tensors); + } + + public static void Deconstruct(this (T1, T2, T3) values, out T1 first, out T2 second, out T3 third) + { + first = values.Item1; + second = values.Item2; + third = values.Item3; + } + } +} diff --git a/src/TensorFlowNET.Core/Common/Extensions/NestExtensions.cs b/src/TensorFlowNET.Core/Common/Extensions/NestExtensions.cs new file mode 100644 index 000000000..76bdd6133 --- /dev/null +++ b/src/TensorFlowNET.Core/Common/Extensions/NestExtensions.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Tensorflow.Common.Types; + +namespace Tensorflow.Common.Extensions +{ + public static class NestExtensions + { + public static Tensors ToTensors(this INestable tensors) + { + return new Tensors(tensors.AsNest()); + } + + public static Tensors? ToTensors(this Nest tensors) + { + return Tensors.FromNest(tensors); + } + + /// + /// If the nested object is already a nested type, this function could reduce it. + /// For example, `Nest[Nest[T]]` can be reduced to `Nest[T]`. + /// + /// + /// + /// + /// + public static Nest ReduceTo(this INestStructure input) where TIn: INestStructure + { + return Nest.ReduceFrom(input); + } + } +} diff --git a/src/TensorFlowNET.Core/Extensions/OneofExtension.cs b/src/TensorFlowNET.Core/Common/Extensions/OneofExtension.cs similarity index 100% rename from src/TensorFlowNET.Core/Extensions/OneofExtension.cs rename to src/TensorFlowNET.Core/Common/Extensions/OneofExtension.cs diff --git a/src/TensorFlowNET.Core/Common/Types/FakeTensorByTensorArray.cs b/src/TensorFlowNET.Core/Common/Types/FakeTensorByTensorArray.cs new file mode 100644 index 000000000..d0c35ee70 --- /dev/null +++ b/src/TensorFlowNET.Core/Common/Types/FakeTensorByTensorArray.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tensorflow.Common.Types +{ + /// + /// This is a temp solution, which should be removed after refactoring `Tensors` + /// + [Obsolete] + public class FakeTensorByTensorArray: Tensor + { + public TensorArray TensorArray { get; set; } + + public FakeTensorByTensorArray(TensorArray array) + { + TensorArray = array; + } + } +} diff --git a/src/TensorFlowNET.Core/Common/Types/GeneralizedTensorShape.cs b/src/TensorFlowNET.Core/Common/Types/GeneralizedTensorShape.cs new file mode 100644 index 000000000..986136f4d --- /dev/null +++ b/src/TensorFlowNET.Core/Common/Types/GeneralizedTensorShape.cs @@ -0,0 +1,69 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Text; + +namespace Tensorflow.Common.Types +{ + public class GeneralizedTensorShape: Nest + { + public GeneralizedTensorShape(Shape value, string? name = null) + { + NodeValue = value; + NestType = NestType.Node; + } + + public GeneralizedTensorShape(IEnumerable values, string? name = null) + { + ListValue = values.Select(s => new Nest(s) as INestStructure).ToList(); + Name = name; + NestType = NestType.List; + } + + public GeneralizedTensorShape(Dictionary value, string? name = null) + { + DictValue = value.ToDictionary(x => x.Key, x => new Nest(x.Value) as INestStructure); + Name = name; + NestType = NestType.Dictionary; + } + + public GeneralizedTensorShape(Nest other) + { + NestType = other.NestType; + NodeValue = other.NodeValue; + DictValue = other.DictValue; + ListValue = other.ListValue; + Name = other.Name; + } + + public Shape ToSingleShape() + { + var shapes = Flatten().ToList(); + if (shapes.Count != 1) + { + throw new ValueError("The generalized shape contains more than 1 dim."); + } + return shapes[0]; + } + + public long ToNumber() + { + var shapes = Flatten().ToList(); + if (shapes.Count != 1 || shapes[0].ndim != 1) + { + throw new ValueError("The generalized shape contains more than 1 dim."); + } + return shapes[0].dims[0]; + } + + public INestStructure ToTensorShapeConfigs() + { + return MapStructure(s => new TensorShapeConfig() { Items = s.dims.Select(x => x == -1 ? null : x).ToArray() }); + } + + public static implicit operator GeneralizedTensorShape(Shape shape) + { + return new GeneralizedTensorShape(shape); + } + } +} diff --git a/src/TensorFlowNET.Core/Common/Types/INestStructure.cs b/src/TensorFlowNET.Core/Common/Types/INestStructure.cs new file mode 100644 index 000000000..32b662937 --- /dev/null +++ b/src/TensorFlowNET.Core/Common/Types/INestStructure.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tensorflow.Common.Types +{ + /// + /// This interface indicates that a class may have a nested structure and provide + /// methods to manipulate with the structure. + /// + public interface INestStructure: INestable + { + NestType NestType { get; } + + /// + /// The item count of depth 1 of the nested structure. + /// For example, [1, 2, [3, 4, 5]] has ShallowNestedCount = 3. + /// + int ShallowNestedCount { get; } + /// + /// The total item count of depth 1 of the nested structure. + /// For example, [1, 2, [3, 4, 5]] has TotalNestedCount = 5. + /// + int TotalNestedCount { get; } + + /// + /// Flatten the Nestable object. Node that if the object contains only one value, + /// it will be flattened to an enumerable with one element. + /// + /// + IEnumerable Flatten(); + /// + /// Construct a new object with the same nested structure. + /// + /// + /// + /// + INestStructure MapStructure(Func func); + } +} diff --git a/src/TensorFlowNET.Core/Common/Types/INestable.cs b/src/TensorFlowNET.Core/Common/Types/INestable.cs new file mode 100644 index 000000000..7ce49f85a --- /dev/null +++ b/src/TensorFlowNET.Core/Common/Types/INestable.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tensorflow.Common.Types +{ + public interface INestable + { + Nest AsNest(); + } +} diff --git a/src/TensorFlowNET.Core/Common/Types/IOptionalArgs.cs b/src/TensorFlowNET.Core/Common/Types/IOptionalArgs.cs new file mode 100644 index 000000000..427e71aaa --- /dev/null +++ b/src/TensorFlowNET.Core/Common/Types/IOptionalArgs.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tensorflow.Common.Types +{ + /// + /// This interface is used when some corresponding python methods have optional args. + /// For example, `Keras.Layer.Apply` generally takes three args as the inputs, while + /// `Keras.Layer.RNN` takes more. Then when calling RNN, you should add `RnnOptionalArgs` + /// as the parameter of the method. + /// + public interface IOptionalArgs + { + /// + /// The identifier of the class. It is not an argument but only something to + /// separate different OptionalArgs. + /// + string Identifier { get; } + } +} diff --git a/src/TensorFlowNET.Core/Extensions/NamedTuple.cs b/src/TensorFlowNET.Core/Common/Types/NamedTuple.cs similarity index 100% rename from src/TensorFlowNET.Core/Extensions/NamedTuple.cs rename to src/TensorFlowNET.Core/Common/Types/NamedTuple.cs diff --git a/src/TensorFlowNET.Core/Common/Types/Nest.Static.cs b/src/TensorFlowNET.Core/Common/Types/Nest.Static.cs new file mode 100644 index 000000000..dc7fd3a1f --- /dev/null +++ b/src/TensorFlowNET.Core/Common/Types/Nest.Static.cs @@ -0,0 +1,62 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tensorflow.Common.Types +{ + public static class Nest + { + /// + /// Pack the flat items to a nested sequence by the template. + /// + /// + /// + /// + /// + public static Nest PackSequenceAs(INestable template, TOut[] flatItems) + { + return template.AsNest().PackSequence(flatItems); + } + + /// + /// Pack the flat items to a nested sequence by the template. + /// + /// + /// + /// + /// + public static Nest PackSequenceAs(INestable template, List flatItems) + { + return template.AsNest().PackSequence(flatItems.ToArray()); + } + + /// + /// Flatten the nested object. + /// + /// + /// + /// + public static IEnumerable Flatten(INestable nestedObject) + { + return nestedObject.AsNest().Flatten(); + } + + /// + /// Map the structure with specified function. + /// + /// + /// + /// + /// + /// + public static INestStructure MapStructure(Func func, INestable nestedObject) + { + return nestedObject.AsNest().MapStructure(func); + } + + public static bool IsNested(INestable obj) + { + return obj.AsNest().IsNested(); + } + } +} diff --git a/src/TensorFlowNET.Core/Common/Types/Nest.cs b/src/TensorFlowNET.Core/Common/Types/Nest.cs new file mode 100644 index 000000000..89ce29f2f --- /dev/null +++ b/src/TensorFlowNET.Core/Common/Types/Nest.cs @@ -0,0 +1,485 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Tensorflow.Common.Extensions; + +namespace Tensorflow.Common.Types +{ + public enum NestType + { + Empty, + Node, + List, + Dictionary + } + + /// + /// A nested structure which may inclulde value, list and dictionary. + /// Note that dictionary does not ensure the data order. When using it as IEnumerable, + /// its order is depth-first. + /// + /// + public class Nest : INestStructure, IEnumerable + { + private static readonly Nest _empty = new Nest() + { + NestType = NestType.Empty, + }; + public static Nest Empty => _empty; + public NestType NestType { get; protected set; } + public string? Name { get; set; } + public T? NodeValue { get; protected set; } + public List>? ListValue { get; protected set; } + public Dictionary>? DictValue { get; protected set; } + + public int ShallowNestedCount + { + get + { + if (NestType == NestType.Empty) + { + return 0; + } + else if (NestType == NestType.Node) + { + return 1; + } + else if (NestType == NestType.List) + { + return ListValue!.Count; + } + else // dict + { + return DictValue!.Count; + } + } + } + + public int TotalNestedCount + { + get + { + return Flatten().Count(); + } + } + + protected Nest() { } + + public Nest(T value, string? name = null) + { + NodeValue = value; + Name = name; + NestType = NestType.Node; + } + + public Nest(IEnumerable> values, string? name = null) + { + ListValue = values.ToList(); + Name = name; + NestType = NestType.List; + } + + public Nest(Dictionary> value, string? name = null) + { + DictValue = value; + Name = name; + NestType = NestType.Dictionary; + } + + public Nest(Nest other) + { + NestType = other.NestType; + NodeValue = other.NodeValue; + DictValue = other.DictValue; + ListValue = other.ListValue; + Name = other.Name; + } + + public virtual IEnumerable Flatten() + { + return FlattenInternal(this); + } + public virtual INestStructure MapStructure(Func func) + { + return MapStructureInternal(func); + } + + /// + /// Pack the flat items to a nested sequence by the template. + /// + /// + /// + public virtual Nest PackSequence(TOut[] flatItems) + { + if(flatItems.Length == 0) + { + return Nest.Empty; + } + int index = 0; + return PackSequenceInternal(this, flatItems, ref index); + } + + private static Nest PackSequenceInternal(Nest template, TOut[] flatItems, ref int index) + { + if(template.NestType == NestType.Node) + { + if(index >= flatItems.Length) + { + throw new InvalidArgumentError("The template and flat items are not matched."); + } + return new Nest(flatItems[index++]); + } + else if(template.NestType == NestType.List) + { + List> nestedObjects = new List>(); + for (int i = 0; i < template.ListValue!.Count; i++) + { + nestedObjects.Add(PackSequenceInternal(template.ListValue![i].AsNest(), flatItems, ref index)); + } + return new Nest(nestedObjects); + } + else if(template.NestType == NestType.Node) + { + Dictionary> dict = new Dictionary>(); + foreach(var (key, value) in template.DictValue!) + { + dict[key] = PackSequenceInternal(value.AsNest(), flatItems, ref index); + } + return new Nest(dict); + } + // Consider Empty as invalid type. + throw new InvalidArgumentError("When using `PackSequenceAs`, the template cannot contain empty node."); + } + + public virtual Nest AsNest() + { + return this; + } + + public virtual Nest MergeWith(Nest? other) + { + if(other is null || other == Nest.Empty) + { + return this; + } + if(this == Nest.Empty) + { + return other; + } + if(NestType == NestType.Node && other.NestType == NestType.Node) + { + return new Nest(new Nest[] { this, other }); + } + else if(NestType == NestType.List && other.NestType == NestType.List) + { + return new Nest(this.ListValue!.Concat(other.ListValue!)); + } + else if(NestType == NestType.Dictionary && other.NestType == NestType.Dictionary) + { + return new Nest(this.DictValue!.Concat(other.DictValue!).ToDictionary(x => x.Key, x => x.Value)); + } + else + { + return new Nest(new Nest[] { this, other }); + } + } + + /// + /// To see if the nested object is really nested. Despite being called `Nest`, sometimes it's actually not + /// nested. For example, [1, 2, 3] is not nested, while [1, [2, 3]] is nested. + /// + /// + public bool IsNested() + { + if(NestType is NestType.Empty or NestType.Node) + { + return false; + } + else if(NestType is NestType.List) + { + return ListValue!.Count > 0; + } + else + { + return DictValue!.Count > 0; + } + } + + [Obsolete("The indexer of Tensors is not encouraged because it leads to unclear meanings.")] + public T this[int index] + { + get + { + bool success = FindInternal(this, index, out var result); + if (success) + { + return result; + } + else + { + throw new IndexOutOfRangeException(); + } + } + set + { + bool success = SetInternal(this, index, value); + if (!success) + { + throw new IndexOutOfRangeException(); + } + } + } + + /// + /// If the existing nested structure if of type `Nest[INestStructure[T]]`, we can reduce it + /// to `Nest[T]`. + /// + /// + /// + /// + public static Nest ReduceFrom(INestStructure input) where TOut: INestStructure + { + var nested = input.AsNest(); + return ReduceInternal(nested).AsNest(); + } + + private static INestStructure ReduceInternal(Nest node) where TOut : INestStructure + { + if(node.NestType == NestType.Empty) + { + return Nest.Empty; + } + else if(node.NestType == NestType.Node) + { + return node.NodeValue!.AsNest(); + } + else if(node.NestType == NestType.List) + { + return new Nest(node.ListValue!.Select(x => ReduceInternal(x.AsNest()))); + } + else // Dictionary type + { + return new Nest(node.DictValue!.ToDictionary(x => x.Key, x => ReduceInternal(x.Value.AsNest()))); + } + } + + private static bool FindInternal(Nest node, int index, out T? result) + { + if (node.NestType == NestType.Node) + { + if(index == 0) + { + result = node.NodeValue!; + return true; + } + result = default(T); + return false; + } + else if (node.NestType == NestType.List) + { + foreach (var item in node.ListValue!) + { + if(index == 0) + { + return FindInternal(item.AsNest(), index, out result); + } + index--; + } + result = default(T); + return false; + } + else if(node.NestType == NestType.Dictionary) + { + foreach (var item in node.DictValue!.Values) + { + if (index == 0) + { + return FindInternal(item.AsNest(), index, out result); + } + index--; + } + result = default(T); + return false; + } + else + { + result = default(T); + return false; + } + } + + private static bool SetInternal(Nest node, int index, T newValue) + { + if (node.NestType == NestType.Node) + { + if (index == 0) + { + node.NodeValue = newValue; + return true; + } + return false; + } + else if (node.NestType == NestType.List) + { + foreach (var item in node.ListValue!) + { + if (index == 0) + { + return SetInternal(item.AsNest(), index, newValue); + } + index--; + } + return false; + } + else if (node.NestType == NestType.Dictionary) + { + foreach (var item in node.DictValue!.Values) + { + if (index == 0) + { + return SetInternal(item.AsNest(), index, newValue); + } + index--; + } + return false; + } + else + { + return false; + } + } + + private static IEnumerable FlattenInternal(Nest node) + { + if (node.NestType == NestType.Node) + { + yield return node.NodeValue!; + } + else if (node.NestType == NestType.List) + { + foreach (var item in node.ListValue!) + { + foreach(var val in FlattenInternal(item.AsNest())) + { + yield return val; + } + } + } + else if (node.NestType == NestType.Dictionary) + { + foreach (var item in node.DictValue!.Values) + { + foreach (var val in FlattenInternal(item.AsNest())) + { + yield return val; + } + } + } + } + + private Nest MapStructureInternal(Func func) + { + if (NestType == NestType.Node) + { + return new Nest(func(NodeValue!)); + } + else if (NestType == NestType.List) + { + List> outs = new List>(); + foreach (var item in ListValue!) + { + outs.Add(item.AsNest().MapStructureInternal(func)); + } + return new Nest(outs); + } + else if (NestType == NestType.Dictionary) + { + Dictionary> outs = new Dictionary>(); + foreach (var (key, value) in DictValue!) + { + outs.Add(key, value.AsNest().MapStructureInternal(func)); + } + return new Nest(outs); + } + else + { + return Nest.Empty; + } + } + + public IEnumerator GetEnumerator() + { + return Flatten().GetEnumerator(); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } + + public override string ToString() + { + StringBuilder sb = new StringBuilder(); + sb.Append("("); + WriteString(this, sb); + sb.Append(")"); + return sb.ToString(); + } + + private static void WriteString(Nest node, StringBuilder sb) + { + if (!string.IsNullOrEmpty(node.Name)) + { + sb.Append($"{node.Name}: "); + } + if (node.NestType == NestType.Node) + { + sb.Append(node.NodeValue!.ToString()); + } + else if (node.NestType == NestType.List) + { + sb.Append("["); + for(int i = 0; i < node.ListValue!.Count; i++) + { + WriteString(node.ListValue![i].AsNest(), sb); + if(i != node.ListValue!.Count - 1) + { + sb.Append(", "); + } + } + sb.Append("]"); + } + else if (node.NestType == NestType.Dictionary) + { + sb.Append("{"); + int count = node.DictValue!.Count; + int i = 0; + foreach (var (key, value) in node.DictValue!) + { + sb.Append($"{key}: "); + WriteString(value.AsNest(), sb); + if (i != count - 1) + { + sb.Append(", "); + } + i++; + } + sb.Append("}"); + } + else + { + sb.Append(""); + } + } + + public static implicit operator Nest((INestStructure, INestStructure) inputs) + { + return new Nest(new INestStructure[] { inputs.Item1, inputs.Item2 }); + } + + public static implicit operator Nest((INestStructure, INestStructure, INestStructure) inputs) + { + return new Nest(new INestStructure[] { inputs.Item1, inputs.Item2, inputs.Item3 }); + } + } +} diff --git a/src/TensorFlowNET.Core/Common/Types/NestDictionary.cs b/src/TensorFlowNET.Core/Common/Types/NestDictionary.cs new file mode 100644 index 000000000..cf1994554 --- /dev/null +++ b/src/TensorFlowNET.Core/Common/Types/NestDictionary.cs @@ -0,0 +1,103 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tensorflow.Common.Types +{ + public class NestDictionary : INestStructure, IDictionary where TKey : notnull + { + public NestType NestType => NestType.Dictionary; + public IDictionary Value { get; set; } + public int ShallowNestedCount => Values.Count; + + public int TotalNestedCount => Values.Count; + public NestDictionary(IDictionary dict) + { + Value = dict; + } + public IEnumerable Flatten() + { + return Value.Select(x => x.Value); + } + public INestStructure MapStructure(Func func) + { + return new NestList(Value.Select(x => func(x.Value))); + } + + public Nest AsNest() + { + return new Nest(Value.Values.Select(x => new Nest(x))); + } + + // Required IDictionary members + public int Count => Value.Count; + + public bool IsReadOnly => Value.IsReadOnly; + + public ICollection Keys => Value.Keys; + + public ICollection Values => Value.Values; + + public void Add(TKey key, TValue value) + { + Value.Add(key, value); + } + + public void Add(KeyValuePair item) + { + Value.Add(item); + } + + public void Clear() + { + Value.Clear(); + } + + public bool Contains(KeyValuePair item) + { + return Value.Contains(item); + } + + public bool ContainsKey(TKey key) + { + return Value.ContainsKey(key); + } + + public void CopyTo(KeyValuePair[] array, int arrayIndex) + { + Value.CopyTo(array, arrayIndex); + } + + public IEnumerator> GetEnumerator() + { + return Value.GetEnumerator(); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } + + public bool Remove(TKey key) + { + return Value.Remove(key); + } + + public bool Remove(KeyValuePair item) + { + return Value.Remove(item); + } + + public bool TryGetValue(TKey key, out TValue value) + { + return Value.TryGetValue(key, out value); + } + + // Optional IDictionary members + public TValue this[TKey key] + { + get => Value[key]; + set => Value[key] = value; + } + } +} diff --git a/src/TensorFlowNET.Core/Common/Types/NestList.cs b/src/TensorFlowNET.Core/Common/Types/NestList.cs new file mode 100644 index 000000000..1e0d272b7 --- /dev/null +++ b/src/TensorFlowNET.Core/Common/Types/NestList.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tensorflow.Common.Types +{ + /// + /// The implementation of a list that support nest structure, in which the depth is 1. + /// + /// + public sealed class NestList : INestStructure, IEnumerable + { + public NestType NestType => NestType.List; + public List Values { get; set; } + public int ShallowNestedCount => Values.Count; + + public int TotalNestedCount => Values.Count; + + public NestList(params T[] values) + { + Values = new List(values); + } + + public NestList(IEnumerable values) + { + Values = new List(values); + } + public IEnumerable Flatten() + { + return Values; + } + public INestStructure MapStructure(Func func) + { + return new NestList(Values.Select(x => func(x))); + } + + public Nest AsNest() + { + return new Nest(Values.Select(x => new Nest(x))); + } + + // Enumerator implementation + public IEnumerator GetEnumerator() + { + return Values.GetEnumerator(); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } + } +} diff --git a/src/TensorFlowNET.Core/Common/Types/NestNode.cs b/src/TensorFlowNET.Core/Common/Types/NestNode.cs new file mode 100644 index 000000000..701aade9a --- /dev/null +++ b/src/TensorFlowNET.Core/Common/Types/NestNode.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tensorflow.Common.Types +{ + /// + /// A nested structure with only one element. + /// + /// + public class NestNode : INestStructure + { + public NestType NestType => NestType.Node; + public T Value { get; set; } + public int ShallowNestedCount => 1; + + public int TotalNestedCount => 1; + public NestNode(T value) + { + Value = value; + } + public IEnumerable Flatten() + { + yield return Value; + } + public INestStructure MapStructure(Func func) + { + return new NestNode(func(Value)); + } + + public Nest AsNest() + { + return new Nest(Value); + } + } +} diff --git a/src/TensorFlowNET.Core/Keras/Saving/TensorShapeConfig.cs b/src/TensorFlowNET.Core/Common/Types/TensorShapeConfig.cs similarity index 95% rename from src/TensorFlowNET.Core/Keras/Saving/TensorShapeConfig.cs rename to src/TensorFlowNET.Core/Common/Types/TensorShapeConfig.cs index 7abcfde26..a36930eca 100644 --- a/src/TensorFlowNET.Core/Keras/Saving/TensorShapeConfig.cs +++ b/src/TensorFlowNET.Core/Common/Types/TensorShapeConfig.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using System.Linq; -namespace Tensorflow.Keras.Saving +namespace Tensorflow.Common.Types { public class TensorShapeConfig { diff --git a/src/TensorFlowNET.Core/Contexts/Context.ExecuteOp.cs b/src/TensorFlowNET.Core/Contexts/Context.ExecuteOp.cs index ac1cd8660..f6e0911ca 100644 --- a/src/TensorFlowNET.Core/Contexts/Context.ExecuteOp.cs +++ b/src/TensorFlowNET.Core/Contexts/Context.ExecuteOp.cs @@ -49,7 +49,7 @@ Tensors ExecGraphAction(string OpType, string Name, ExecuteOpArgs args) Tensors ExecEagerAction(string OpType, string Name, ExecuteOpArgs args) { - var opExecInfo = new FastPathOpExecInfo(OpType, Name, args.OpInputArgs) + var opExecInfo = new FastPathOpExecInfo(tf.Context, OpType, Name, args.OpInputArgs) { attrs = args.OpAttrs }; diff --git a/src/TensorFlowNET.Core/Data/DatasetV2.cs b/src/TensorFlowNET.Core/Data/DatasetV2.cs index 324d7e834..c1762d670 100644 --- a/src/TensorFlowNET.Core/Data/DatasetV2.cs +++ b/src/TensorFlowNET.Core/Data/DatasetV2.cs @@ -161,8 +161,8 @@ public override string ToString() break; } - yield return (new Tensors(results.Take(FirstInputTensorCount)), results.Length == FirstInputTensorCount ? - null : new Tensors(results.Skip(FirstInputTensorCount))); + yield return (new Tensors(results.Take(FirstInputTensorCount).ToArray()), results.Length == FirstInputTensorCount ? + null : new Tensors(results.Skip(FirstInputTensorCount).ToArray())); } } diff --git a/src/TensorFlowNET.Core/Device/DeviceSpec.cs b/src/TensorFlowNET.Core/Device/DeviceSpec.cs index f4ea8cf05..255191cb5 100644 --- a/src/TensorFlowNET.Core/Device/DeviceSpec.cs +++ b/src/TensorFlowNET.Core/Device/DeviceSpec.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.Text; using System.Threading.Tasks; @@ -7,8 +8,8 @@ namespace Tensorflow.Device { public class DeviceSpec { - private static Dictionary _STRING_TO_COMPONENTS_CACHE = new(); - private static Dictionary _COMPONENTS_TO_STRING_CACHE = new(); + private static ConcurrentDictionary _STRING_TO_COMPONENTS_CACHE = new(); + private static ConcurrentDictionary _COMPONENTS_TO_STRING_CACHE = new(); private string _job; private int _replica; private int _task; diff --git a/src/TensorFlowNET.Core/Eager/EagerRunner.RecordGradient.cs b/src/TensorFlowNET.Core/Eager/EagerRunner.RecordGradient.cs index 59d5fd030..2bdd65f5b 100644 --- a/src/TensorFlowNET.Core/Eager/EagerRunner.RecordGradient.cs +++ b/src/TensorFlowNET.Core/Eager/EagerRunner.RecordGradient.cs @@ -80,6 +80,11 @@ BackwardFunction GetGradientFunction(string op_name, Tensor[] op_outputs) => (out_grads, unneeded_gradients) => { + if(!ops.gradientFunctions.ContainsKey(op_name)) + { + throw new Exception($"gradientFunctions not find op_name: {op_name}"); + } + if (ops.gradientFunctions[op_name] == null) return new Tensor[op_inputs.Length]; diff --git a/src/TensorFlowNET.Core/Eager/EagerRunner.TFE_FastPathExecute.cs b/src/TensorFlowNET.Core/Eager/EagerRunner.TFE_FastPathExecute.cs index fedc02cb9..0ce55841b 100644 --- a/src/TensorFlowNET.Core/Eager/EagerRunner.TFE_FastPathExecute.cs +++ b/src/TensorFlowNET.Core/Eager/EagerRunner.TFE_FastPathExecute.cs @@ -68,7 +68,8 @@ public Tensor[] TFE_FastPathExecute(FastPathOpExecInfo op_exec_info) var input_arg = op_def.InputArg[i]; if (!string.IsNullOrEmpty(input_arg.NumberAttr)) { - int len = (input as object[]).Length; + var fast_input_array = input is Tensors tensors ? (object[])tensors : (object[])input; + int len = fast_input_array.Length; c_api.TFE_OpSetAttrInt(op, input_arg.NumberAttr, len); if (op_exec_info.run_callbacks) { @@ -79,7 +80,6 @@ public Tensor[] TFE_FastPathExecute(FastPathOpExecInfo op_exec_info) if (len > 0) { - var fast_input_array = (object[])op_exec_info.args[i]; // First item adds the type attr. if (!AddInputToOp(fast_input_array[i], true, input_arg, flattened_attrs, flattened_inputs, op, status)) return null; @@ -352,13 +352,19 @@ bool SetOpAttrScalar(Context ctx, SafeEagerOpHandle op, c_api.TFE_OpSetAttrFloat(op, key, Convert.ToSingle(value)); break; case TF_AttrType.TF_ATTR_SHAPE: - var dims = (value as long[]).ToArray(); + long[] dims; + if (value is Shape shape) dims = shape.dims.ToArray(); + else if (value is long[] longs) dims = longs.ToArray(); + else if (value is int[] ints) dims = ints.Select(x => (long)x).ToArray(); + else dims = ((long[])value).ToArray(); c_api.TFE_OpSetAttrShape(op, key, dims, dims.Length, status); status.Check(true); break; case TF_AttrType.TF_ATTR_FUNC: if (value is ConcreteFunction func) c_api.TFE_OpSetAttrFunctionName(op, key, func.func_graph.FuncName, func.func_graph.FuncName.Length); + else if(value is string str) + c_api.TFE_OpSetAttrFunctionName(op, key, str, str.Length); else throw new NotImplementedException("TF_AttrType.TF_ATTR_FUNC"); break; diff --git a/src/TensorFlowNET.Core/Eager/EagerRunner.TFE_TapeGradient.cs b/src/TensorFlowNET.Core/Eager/EagerRunner.TFE_TapeGradient.cs index 1f7b3ae64..3515fed83 100644 --- a/src/TensorFlowNET.Core/Eager/EagerRunner.TFE_TapeGradient.cs +++ b/src/TensorFlowNET.Core/Eager/EagerRunner.TFE_TapeGradient.cs @@ -65,7 +65,7 @@ public Tensor[] TFE_TapeGradient(ITape tape, { outgrad_vec = output_gradients.ToList(); } - var result = tape.ComputeGradient(target_vec, sources_vec, source_tensors_that_are_targets, outgrad_vec, false); + var result = tape.ComputeGradient(target_vec, sources_vec, source_tensors_that_are_targets, outgrad_vec, true); bool unconnected_gradients_zero = unconnected_gradients == "zero"; @@ -137,7 +137,6 @@ TapeTensor TapeTensorFromTensor(Tensor tensor) { dims[i] = c_api.TFE_TensorHandleDim(handle, i, status); } - Shape tensor_shape = new(dims); if(status.Code != TF_Code.TF_OK) { @@ -145,6 +144,7 @@ TapeTensor TapeTensorFromTensor(Tensor tensor) } else { + Shape tensor_shape = new(dims); return new TapeTensor(id, dtype, tensor_shape); } } @@ -173,8 +173,12 @@ bool DTypeNeedsHandleData(TF_DataType dtype) return dtype == dtypes.variant || dtype == dtypes.resource; } - bool ListContainNone(long[] list) + bool ListContainNone(long[]? list) { + if(list is null) + { + return true; + } int len = list.Length; if(len == 0) { diff --git a/src/TensorFlowNET.Core/Eager/EagerTensor.ToString.cs b/src/TensorFlowNET.Core/Eager/EagerTensor.ToString.cs index ce3c983b5..71b3075aa 100644 --- a/src/TensorFlowNET.Core/Eager/EagerTensor.ToString.cs +++ b/src/TensorFlowNET.Core/Eager/EagerTensor.ToString.cs @@ -10,6 +10,11 @@ public override string ToString() var str = NDArrayRender.ToString(nd); return $"tf.Tensor: shape={shape}, dtype={dtype.as_numpy_name()}, numpy={str}"; } - + public string ToString(int maxLength) + { + var nd = new NDArray(this); + var str = NDArrayRender.ToString(nd, maxLength); + return $"tf.Tensor: shape={shape}, dtype={dtype.as_numpy_name()}, numpy={str}"; + } } } diff --git a/src/TensorFlowNET.Core/Eager/FastPathOpExecInfo.cs b/src/TensorFlowNET.Core/Eager/FastPathOpExecInfo.cs index 2cdf025a1..307ca2ce4 100644 --- a/src/TensorFlowNET.Core/Eager/FastPathOpExecInfo.cs +++ b/src/TensorFlowNET.Core/Eager/FastPathOpExecInfo.cs @@ -17,8 +17,9 @@ public class FastPathOpExecInfo public bool run_callbacks { get; set; } public Action callbacks { get; set; } - public FastPathOpExecInfo(string opName, string name, params object[] inputArgs) + public FastPathOpExecInfo(Context ctx, string opName, string name, params object[] inputArgs) { + this.ctx = ctx; this.op_name = opName; this.name = name; this.args = inputArgs; diff --git a/src/TensorFlowNET.Core/Eager/GraphOnlyOps.cs b/src/TensorFlowNET.Core/Eager/GraphOnlyOps.cs new file mode 100644 index 000000000..2c20cfe9b --- /dev/null +++ b/src/TensorFlowNET.Core/Eager/GraphOnlyOps.cs @@ -0,0 +1,25 @@ +using Tensorflow; + +internal static class GraphOnlyOps +{ + /// + /// Graph-only version of tf.compat.v1.placeholder(), for internal use only. + /// + /// + /// + /// + /// + internal static Tensor graph_placeholder(TF_DataType dtype, Shape shape, string? name = null) + { + var dtype_value = new AttrValue() { Type = dtype.as_datatype_enum() }; + var shape_value = new AttrValue() { Shape = shape.as_proto() }; + var g = ops.get_default_graph(); + Dictionary attrs = new(); + attrs["dtype"] = dtype_value; + attrs["shape"] = shape_value; + var op = g.create_op("Placeholder", new Tensor[0], new TF_DataType[] { dtype }, + new TF_DataType[0], attrs: attrs, name: name); + var result = op.outputs[0]; + return result; + } +} \ No newline at end of file diff --git a/src/TensorFlowNET.Core/Eager/execute.cs b/src/TensorFlowNET.Core/Eager/execute.cs index 1804992ac..e981c6c51 100644 --- a/src/TensorFlowNET.Core/Eager/execute.cs +++ b/src/TensorFlowNET.Core/Eager/execute.cs @@ -7,10 +7,11 @@ using static Tensorflow.ApiDef.Types; using static Tensorflow.CostGraphDef.Types; using static Tensorflow.Binding; +using Tensorflow.Gradients; namespace Tensorflow.Eager { - internal static class execute + internal static class _execute { public static (DataType[], Tensor[]) onvert_to_mixed_eager_tensors(Tensor[] values, Context ctx) { @@ -18,7 +19,7 @@ public static (DataType[], Tensor[]) onvert_to_mixed_eager_tensors(Tensor[] valu var types = v.Select(t => t.dtype.as_datatype_enum()); return (types.ToArray(), v.ToArray()); } - public static Tensor[] executes(string op_name, int num_outputs, Tensor[] inputs, object[] attrs, Context ctx, string name = null) + public static Tensor[] execute(string op_name, int num_outputs, Tensor[] inputs, object[] attrs, Context ctx, string name = null) { return quick_execute(op_name, num_outputs, inputs, attrs, ctx, name); } @@ -33,7 +34,12 @@ public static Tensor[] quick_execute(string op_name, int num_outputs, Tensor[] i } public static bool must_record_gradient() { - return false; + return tf.GetTapeSet().Count != 0; + } + + public static bool record_gradient(string op_name, Tensor[] inputs, object[] attrs, Tensor[] results) + { + return tf.Runner.RecordGradient(op_name, inputs, attrs, results); } } } diff --git a/src/TensorFlowNET.Core/Exceptions/NotOkStatusException.cs b/src/TensorFlowNET.Core/Exceptions/NotOkStatusException.cs new file mode 100644 index 000000000..c283c1a45 --- /dev/null +++ b/src/TensorFlowNET.Core/Exceptions/NotOkStatusException.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tensorflow.Exceptions +{ + public class NotOkStatusException : TensorflowException + { + public NotOkStatusException() : base() + { + + } + + public NotOkStatusException(string message) : base(message) + { + + } + } +} diff --git a/src/TensorFlowNET.Core/Framework/IndexedSlices.cs b/src/TensorFlowNET.Core/Framework/IndexedSlices.cs index 24d356fbb..bac5e6fb1 100644 --- a/src/TensorFlowNET.Core/Framework/IndexedSlices.cs +++ b/src/TensorFlowNET.Core/Framework/IndexedSlices.cs @@ -49,12 +49,25 @@ public IndexedSlices(Tensor values, Tensor indices, Tensor dense_shape = null) public static implicit operator Tensor(IndexedSlices indexedSlices) { - return indexedSlices.values; + return _indexed_slices_to_tensor(indexedSlices); } public static implicit operator IndexedSlices(Tensor tensor) { return tensor.Tag as IndexedSlices; } + + /// + /// Converts an IndexedSlices object `value` to a Tensor. + /// + /// + /// + /// + /// + /// + public static Tensor _indexed_slices_to_tensor(IndexedSlices indexedSlices, TF_DataType dtype = TF_DataType.DtInvalid, String name = "", bool as_ref = false) + { + return gen_math_ops.unsorted_segment_sum(indexedSlices.values, indexedSlices.indices, indexedSlices.dense_shape.slice(0)); + } } } diff --git a/src/TensorFlowNET.Core/Framework/Models/TensorSpec.cs b/src/TensorFlowNET.Core/Framework/Models/TensorSpec.cs index 083d4813a..ac099ae2b 100644 --- a/src/TensorFlowNET.Core/Framework/Models/TensorSpec.cs +++ b/src/TensorFlowNET.Core/Framework/Models/TensorSpec.cs @@ -1,4 +1,5 @@ using System.Linq; +using Tensorflow.Eager; namespace Tensorflow.Framework.Models { @@ -24,5 +25,17 @@ public TensorSpec _batch(int dim = -1) shapes.Insert(0, dim); return new TensorSpec(shapes.ToArray(), _dtype); } + + public static TensorSpec FromTensor(Tensor tensor, string? name = null) + { + if(tensor is EagerTensor) + { + return new TensorSpec(tensor.shape, tensor.dtype, name); + } + else + { + return new TensorSpec(tensor.shape, tensor.dtype, name ?? tensor.name); + } + } } } diff --git a/src/TensorFlowNET.Core/Framework/auto_control_deps_utils.cs b/src/TensorFlowNET.Core/Framework/auto_control_deps_utils.cs new file mode 100644 index 000000000..28d9e5008 --- /dev/null +++ b/src/TensorFlowNET.Core/Framework/auto_control_deps_utils.cs @@ -0,0 +1,89 @@ +using Tensorflow.Graphs; + +namespace Tensorflow.Framework +{ + internal static class auto_control_deps_utils + { + public static readonly string READ_ONLY_RESOURCE_INPUTS_ATTR = "_read_only_resource_inputs"; + public static List get_read_only_resource_input_indices_graph(FuncGraph func_graph) + { + List result = new List(); + // A cache to store the read only resource inputs of an Op. + // Operation -> ObjectIdentitySet of resource handles. + Dictionary> opReadOnlyResourceInputs = + new Dictionary>(); + + for (int inputIndex = 0; inputIndex < func_graph.Inputs.Length; inputIndex++) + { + Tensor t = func_graph.Inputs[inputIndex]; + if (t.dtype != dtypes.resource) + continue; + + bool readOnly = true; + foreach (var op in t.consumers()) + { + if (opReadOnlyResourceInputs.ContainsKey(op)) + { + if (!opReadOnlyResourceInputs[op].Contains(t)) + { + readOnly = false; + break; + } + } + else + { + List indices = _get_read_only_resource_input_indices_op(op); + opReadOnlyResourceInputs[op] = new HashSet( + indices.Select(i => op.inputs[i])); + if (!opReadOnlyResourceInputs[op].Contains(t)) + { + readOnly = false; + break; + } + } + } + + if (readOnly) + result.Add(inputIndex); + } + + return result; + } + + private static List _get_read_only_resource_input_indices_op(Operation op) + { + // ignore the RESOURCE_READ_OPS + + int[] read_only_input_indices; + + try + { + read_only_input_indices = op.get_attr(READ_ONLY_RESOURCE_INPUTS_ATTR); + } + catch (InvalidArgumentError) + { + return new List(); + } + + int read_only_index = 0; + List result = new(); + for (int i = 0; i < op.inputs.Length; i++) + { + if (read_only_index >= read_only_input_indices.Length) + { + break; + } + if (op.inputs[i].dtype != dtypes.resource) + { + continue; + } + if (read_only_index < read_only_input_indices.Length && i == read_only_input_indices[read_only_index]) + { + result.Add(i); + read_only_index++; + } + } + return result; + } + } +} diff --git a/src/TensorFlowNET.Core/Framework/function_def_lib.cs b/src/TensorFlowNET.Core/Framework/function_def_lib.cs index 67f8d324e..488c6b654 100644 --- a/src/TensorFlowNET.Core/Framework/function_def_lib.cs +++ b/src/TensorFlowNET.Core/Framework/function_def_lib.cs @@ -42,10 +42,10 @@ public static FuncGraph function_def_to_graph(FunctionDef fdef, object? structur func_graph.as_default(); importer.import_graph_def(graph_def, name: "", validate_colocation_constraints: false); var input_tensor_names = fdef.Signature.InputArg.Select(x => nested_to_flat_tensor_name[x.Name]); - func_graph.Inputs = new Tensors(input_tensor_names.Select(x => func_graph.get_tensor_by_name(x))); + func_graph.Inputs = new Tensors(input_tensor_names.Select(x => func_graph.get_tensor_by_name(x)).ToArray()); var output_tensor_names = fdef.Signature.OutputArg.Select(x => nested_to_flat_tensor_name[fdef.Ret[x.Name]]); - func_graph.Outputs = new Tensors(output_tensor_names.Select(x => func_graph.get_tensor_by_name(x))); + func_graph.Outputs = new Tensors(output_tensor_names.Select(x => func_graph.get_tensor_by_name(x)).ToArray()); // TODO(Rinne): func_graph.ControlOutputs _set_handle_data(func_graph, fdef); diff --git a/src/TensorFlowNET.Core/Functions/ConcreteFunction.cs b/src/TensorFlowNET.Core/Functions/ConcreteFunction.cs index 88dce7d98..8742e4535 100644 --- a/src/TensorFlowNET.Core/Functions/ConcreteFunction.cs +++ b/src/TensorFlowNET.Core/Functions/ConcreteFunction.cs @@ -8,6 +8,7 @@ using Tensorflow.Graphs; using Tensorflow.Train; using Tensorflow.Util; +using Tensorflow.Common.Extensions; using static Tensorflow.Binding; namespace Tensorflow.Functions @@ -40,6 +41,18 @@ public class ConcreteFunction: Trackable public Tensor[] FlatStructuredOutputs => func_graph.FlatStructuredOutputs; public IEnumerable Variables => func_graph.Variables; public IEnumerable TrainableVariables => func_graph.TrainableVariables; + internal NameAttrList AsNameAttrList + { + get + { + NameAttrList ret = new() { Name = this.Name }; + foreach (var (name, value) in _attrs) + { + ret.Attr[name] = value; + } + return ret; + } + } public ConcreteFunction(string name) { diff --git a/src/TensorFlowNET.Core/Functions/EagerDefinedFunction.cs b/src/TensorFlowNET.Core/Functions/EagerDefinedFunction.cs index cc38683db..d547b6120 100644 --- a/src/TensorFlowNET.Core/Functions/EagerDefinedFunction.cs +++ b/src/TensorFlowNET.Core/Functions/EagerDefinedFunction.cs @@ -147,7 +147,7 @@ public unsafe Tensors Call(Tensors args) Tensor[] outputs; if (executing_eagerly) { - outputs = execute.executes( + outputs = _execute.execute( Signature.Name, _num_outputs, args, diff --git a/src/TensorFlowNET.Core/GlobalUsing.cs b/src/TensorFlowNET.Core/GlobalUsing.cs index 2fd5b437b..7e02c9083 100644 --- a/src/TensorFlowNET.Core/GlobalUsing.cs +++ b/src/TensorFlowNET.Core/GlobalUsing.cs @@ -3,4 +3,7 @@ global using System.Text; global using System.Collections; global using System.Data; -global using System.Linq; \ No newline at end of file +global using System.Linq; +global using Tensorflow.Keras.Engine; +global using Tensorflow.Framework.Models; +global using static Tensorflow.Binding; \ No newline at end of file diff --git a/src/TensorFlowNET.Core/Gradients/GradientTape.cs b/src/TensorFlowNET.Core/Gradients/GradientTape.cs index b5fd373e9..a714436a3 100644 --- a/src/TensorFlowNET.Core/Gradients/GradientTape.cs +++ b/src/TensorFlowNET.Core/Gradients/GradientTape.cs @@ -44,6 +44,15 @@ public ITape PushTape(bool persistent = false, return tape; } + public void PushTape(ITape tape) + { + // Enters a context inside which operations are recorded on this tape. + if (tf.Context.executing_eagerly()) + tf.Context.ensure_initialized(); + + _tapeSet.Push(tape); + } + ITape PopTape() { _tape.StopRecord(); diff --git a/src/TensorFlowNET.Core/Gradients/array_grad.cs b/src/TensorFlowNET.Core/Gradients/array_grad.cs index c4cb9fbd1..a4da60eed 100644 --- a/src/TensorFlowNET.Core/Gradients/array_grad.cs +++ b/src/TensorFlowNET.Core/Gradients/array_grad.cs @@ -36,8 +36,7 @@ public static Tensor[] _BroadcastToGrad(Operation op, Tensor[] grads) var input_value = op.inputs[0]; var broadcast_shape = op.inputs[1]; var input_value_shape = array_ops.shape(input_value); - var (_, reduction_axes) = gen_array_ops.broadcast_gradient_args(broadcast_shape, - input_value_shape); + var reduction_axes = gen_array_ops.broadcast_gradient_args(broadcast_shape, input_value_shape)[1]; var updates_grad_reshaped = math_ops.reduce_sum(grad, axis: reduction_axes, keepdims: true); @@ -91,8 +90,7 @@ private static Tensor[] _ConcatGradHelper(Operation op, Tensor grad, int start_v ? input_values[0].rank + dim_int : dim_int % input_values[0].rank; var sizes = input_values.Select(x => x.shape[non_neg_concat_dim]).ToArray(); - var sizes_tensor = constant_op.constant(sizes); - out_grads = array_ops.split(grad, sizes_tensor, non_neg_concat_dim).ToList(); + out_grads = array_ops.split(grad, sizes.Select(x => (int)x).ToArray(), ops.convert_to_tensor(non_neg_concat_dim)).ToList(); } else if (constant_op.is_constant(concat_dim)) { @@ -128,7 +126,7 @@ there will be a small number of performance regressions.*/ new Tensor[] { non_neg_concat_dim, tf.constant(0) }, new Tensor[] { tf.constant(1), tf.constant(-1) }); var squeeze_sizes = array_ops.squeeze(slice); - out_grads = array_ops.split(axis: grad, value: squeeze_sizes, num_split: (int)non_neg_concat_dim).ToList(); + out_grads = array_ops.split(axis: grad, value: squeeze_sizes, num_or_size_splits: (int)non_neg_concat_dim).ToList(); } else { @@ -351,16 +349,16 @@ public static Tensor[] _StridedSliceGradGrad(Operation op, Tensor[] grads) null, null, null, - gen_array_ops.strided_slice( + array_ops.strided_slice( grad, begin, end, strides, - begin_mask: op.get_attr("begin_mask"), - end_mask: op.get_attr("end_mask"), - ellipsis_mask: op.get_attr("ellipsis_mask"), - new_axis_mask: op.get_attr("new_axis_mask"), - shrink_axis_mask: op.get_attr("shrink_axis_mask")) + begin_mask: (int)op.get_attr("begin_mask"), + end_mask: (int)op.get_attr("end_mask"), + ellipsis_mask: (int)op.get_attr("ellipsis_mask"), + new_axis_mask: (int)op.get_attr("new_axis_mask"), + shrink_axis_mask: (int)op.get_attr("shrink_axis_mask")) }; } @@ -375,5 +373,56 @@ public static Tensor[] _TransposeGrad(Operation op, Tensor[] grads) var p = op.inputs[1]; return new Tensor[] { array_ops.transpose(grads[0], array_ops.invert_permutation(p)), null }; } + + [RegisterGradient("ReverseV2")] + public static Tensor[] _ReverseV2Grad(Operation op, Tensor[] grads) + { + var grad = grads[0]; + var axis = op.inputs[1]; + return new Tensor[] { array_ops.reverse(grad, axis), null }; + } + + [RegisterGradient("Tile")] + public static Tensor[] _TileGrad(Operation op, Tensor[] grads) + { + var grad = grads[0]; + var input_shape = array_ops.shape(op.inputs[0], out_type: op.inputs[1].dtype); + var split_shape = array_ops.reshape(array_ops.transpose(array_ops.stack(new Tensor[] { op.inputs[1], input_shape })), new Shape(-1)); + var axes = math_ops.range(0, array_ops.size(split_shape), 2); + + //# Sum reduces grad along the first dimension for IndexedSlices + //if isinstance(grad, indexed_slices_lib.IndexedSlices): + //input_shape_0 = math_ops.cast(input_shape[0], grad.indices.dtype) + //grad = math_ops.unsorted_segment_sum( + // grad.values, math_ops.mod(grad.indices, input_shape_0), input_shape_0) + //split_shape = array_ops.concat([[1], split_shape[1:]], axis = 0) + + var input_grad = math_ops.reduce_sum(array_ops.reshape(grad, split_shape), axes); + if (!tf.Context.executing_eagerly()) + { + input_grad.set_shape(op.inputs[0].GetShape()); + } + return new Tensor[] { input_grad, null }; + } + + [RegisterGradient("GatherNd")] + public static Tensor[] _GatherNdGrad(Operation op, Tensor[] grads) + { + var @ref = op.inputs[0]; + var indices = op.inputs[1]; + var grad = grads[0]; + var ref_shape = array_ops.shape(@ref, out_type: indices.dtype); + Tensor ref_grad = null; + if (indices.shape.ndim == 2 && indices.shape.dims[indices.shape.Length - 1] == 1) + { + ref_grad = (Tensor)new IndexedSlices(grad, array_ops.squeeze(indices, axis: -1), ref_shape); + } + else + { + ref_grad = gen_array_ops.scatter_nd(indices, grad, ref_shape); + } + return new Tensor[] { ref_grad, null }; + } + } } diff --git a/src/TensorFlowNET.Core/Gradients/math_grad.cs b/src/TensorFlowNET.Core/Gradients/math_grad.cs index 89699d6bc..8c3f0f8bd 100644 --- a/src/TensorFlowNET.Core/Gradients/math_grad.cs +++ b/src/TensorFlowNET.Core/Gradients/math_grad.cs @@ -53,7 +53,8 @@ public static Tensor[] _AddGrad(Operation op, Tensor[] grads) var sx = array_ops.shape(x); var sy = array_ops.shape(y); - var (rx, ry) = gen_array_ops.broadcast_gradient_args(sx, sy); + var args = gen_array_ops.broadcast_gradient_args(sx, sy); + var (rx, ry) = (args[0], args[1]); var sum1 = math_ops.reduce_sum(grad, rx); var r1 = gen_array_ops.reshape(sum1, sx); @@ -101,7 +102,8 @@ public static Tensor[] _DivNoNanGrad(Operation op, Tensor[] grads) var y = op.inputs[1]; var sx = array_ops.shape(x); var sy = array_ops.shape(y); - var (rx, ry) = gen_array_ops.broadcast_gradient_args(sx, sy); + var args = gen_array_ops.broadcast_gradient_args(sx, sy); + var (rx, ry) = (args[0], args[1]); x = math_ops.conj(x); y = math_ops.conj(y); @@ -115,6 +117,137 @@ public static Tensor[] _DivNoNanGrad(Operation op, Tensor[] grads) }; } + public static string ellipsis = "..."; + [RegisterGradient("Einsum")] + public static Tensor[] _EinsumGrad(Operation op, Tensor[] grads) + { + // Gradient for Einsum. + string equation = (string)op.get_attr("equation"); + string[] split_equation = equation.Split(new string[] { "->" }, StringSplitOptions.None); + var input_subs = split_equation[0]; + var output_subs = split_equation[1]; + + if (op.inputs.Length == 1) + { + var input_shape = array_ops.shape(op.inputs[0]); + var reduced_label_set = new HashSet(new HashSet(input_subs).Except(new HashSet(output_subs + ellipsis))); + if (reduced_label_set.Count == 0) + return new Tensor[] { math_ops.einsum(string.Format("{0}->{1}", output_subs, input_subs), new Tensors(grads)) }; + return new Tensor[] { _GetGradReduced(new Tensors(grads), output_subs, input_subs, input_shape, reduced_label_set) }; + } + + string[] split_input_subs = input_subs.Split(new string[] { "," }, StringSplitOptions.None); + var x_subs = split_input_subs[0]; + var y_subs = split_input_subs[1]; + // Add ellipsis for broadcasted dimensions if any operand does not have it. + // This is because the equation "...ij,jk->ik" may be valid if the 0th input's + // batch shape is empty, but the VJP equation "jk,ik->...ij" is not valid + // because only the output subscripts contain ellipsis. + if (output_subs.Contains(ellipsis)) + { + if (!x_subs.Contains(ellipsis)) + x_subs += ellipsis; + if (!y_subs.Contains(ellipsis)) + y_subs += ellipsis; + } + // Obtain the gradients wrt the inputs x and y, without taking into account + // the unbroadcasting. + var x = op.inputs[0]; + var y = op.inputs[1]; + if (grads.GetDataType().is_complex()) + { + x = math_ops.conj(x); + y = math_ops.conj(y); + } + + var x_shape = array_ops.shape(x); + var y_shape = array_ops.shape(y); + var grad_x = _GetGradWrt(grads, y, x_shape, x_subs, y_subs, output_subs); + var grad_y = _GetGradWrt(grads, x, y_shape, y_subs, x_subs, output_subs); + + if (!output_subs.Contains(ellipsis)) + return new Tensor[] { grad_x, grad_y }; + var bx = _GetBcastSubshape(x_subs); + int bx_start = bx[0], bx_end = bx[1]; + var by = _GetBcastSubshape(y_subs); + int by_start = by[0], by_end = by[1]; + + var x_shape_static = x.shape; + var y_shape_static = y.shape; + if(x_shape_static.IsFullyDefined && + y_shape_static.IsFullyDefined && + x_shape_static[string.Format("{0}:{1}",bx_start,bx_end)] == y_shape_static[string.Format("{0}:{1}", by_start, by_end)]) + return new Tensor[] { grad_x, grad_y }; + + var r = gen_array_ops.broadcast_gradient_args(x_shape[string.Format("{0}:{1}", bx_start, bx_end)], + y_shape[string.Format("{0}:{1}", by_start, by_end)]); + var rx = r[0]; + var ry = r[1]; + grad_x = array_ops.reshape(math_ops.reduce_sum(grad_x, bx_start + rx), x_shape); + grad_y = array_ops.reshape(math_ops.reduce_sum(grad_y, by_start + ry), y_shape); + return new Tensor[] { grad_x, grad_y }; + } + protected static Tensor _GetGradWrt(Tensor[] output_grads, Tensor other_operand, Tensor input_shape, + string input_subs, string other_subs, string output_subs) + { + var reduced_label_set = new HashSet(new HashSet(input_subs).Except(new HashSet(output_subs + other_subs + "."))); + var left_subs = string.Join("", input_subs.Where(s => !reduced_label_set.Contains(s))); + var grad_reduced = math_ops.einsum(string.Format("{0},{1}->{2}", output_subs, other_subs, left_subs), new Tensors((Tensors)output_grads, other_operand)); + if (reduced_label_set.Count == 0) + return grad_reduced; + return _GetGradReduced(grad_reduced, left_subs, input_subs, input_shape, reduced_label_set); + } + protected static Tensor _GetGradReduced(Tensor output_grad, string output_subs, string input_subs, Tensor input_shape, HashSet reduced_label_set) + { + string reduced_subs; + Tensor reduced_dims; + List reduced_axes; + _GetReducedSubscripts(reduced_label_set, input_shape, input_subs, out reduced_subs, out reduced_dims, out reduced_axes); + bool has_repeated_labels = ( + new HashSet(input_subs).Count + new HashSet(output_subs).Count < + input_subs.Length + output_subs.Length); + var input_subs_without_reduced_labels = string.Join("", input_subs.Where(s => !reduced_label_set.Contains(s))); + + if (!has_repeated_labels && input_subs_without_reduced_labels == output_subs) + { + var reduced_shape = math_ops.reduced_shape(input_shape, ops.convert_to_tensor(reduced_axes)); + return gen_array_ops.broadcast_to(array_ops.reshape(output_grad, reduced_shape), input_shape); + } + else + { + var grad_shape_with_reduced_labels = array_ops.concat(new Tensor[] { reduced_dims, array_ops.shape(new Tensors(output_grad)) }, axis: 0); + var reduced_shape = array_ops.concat(new Tensor[] { array_ops.ones(reduced_label_set.Count, dtype: dtypes.int32), array_ops.shape(new Tensors(output_grad)) }, axis: 0); + var broadcasted_grad = gen_array_ops.broadcast_to(array_ops.reshape(output_grad, reduced_shape), grad_shape_with_reduced_labels); + return math_ops.einsum(string.Format("{0}->{1}", reduced_subs + output_subs, input_subs), new Tensors(broadcasted_grad)); + } + } + protected static void _GetReducedSubscripts(HashSet reduced_label_set, Tensor input_shape, string subscripts, out string reduced_subs, out Tensor reduced_dims, out List reduced_axes) + { + reduced_subs = string.Join("", reduced_label_set.Select(c => c.ToString())); + reduced_axes = reduced_subs.Select(s => _GetAxisFromLabel(subscripts, s)).ToList(); + reduced_dims = array_ops.stack(reduced_axes.Select(ax => input_shape[ax]).ToList()); + } + protected static int _GetAxisFromLabel(string subscripts, char label) + { + var splits = subscripts.Split(new string[] { ellipsis }, StringSplitOptions.None); + var index = splits[0].IndexOf(label); + if (index != -1) return index; + if (splits.Length < 2) throw new OutOfRangeError(); + index = splits[1].IndexOf(label); + if (index != -1) return index; + throw new ValueError(); + } + protected static int[] _GetBcastSubshape(string subscripts) + { + int start = subscripts.IndexOf(ellipsis); + if (start == -1) return new int[] { 0, 0 }; + int remaining = subscripts.Length - (start + ellipsis.Length); + int end; + if (remaining > 0) end = remaining; + else throw new Exception(); + return new int[] { start, end }; + } + /// /// Returns grad * exp(x). /// @@ -427,7 +560,8 @@ private static Tensor[] _MaximumMinimumGrad(bool isMaximum, Operation op, Tensor isMaximum ? gen_math_ops.greater_equal(x, y) : gen_math_ops.less_equal(x, y); - var (rx, ry) = gen_array_ops.broadcast_gradient_args(sx, sy); + var args = gen_array_ops.broadcast_gradient_args(sx, sy); + var (rx, ry) = (args[0], args[1]); var xgrad = array_ops.where(xmask, grad, zeros); var gx = array_ops.reshape(math_ops.reduce_sum(xgrad, rx), sx); var ygrad = array_ops.where(xmask, zeros, grad); @@ -458,7 +592,7 @@ public static Tensor[] _SelectGrad(Operation op, Tensor[] grads) private static Tensor _safe_shape_div(Tensor x, Tensor y) { - return math_ops.floordiv(x, gen_math_ops.maximum(y, 1)); + return math_ops.floordiv(x, gen_math_ops.maximum(y, ops.convert_to_tensor(1))); } [RegisterGradient("Sub")] @@ -573,7 +707,8 @@ public static Tensor[] _RealDivGrad(Operation op, Tensor[] grads) var sx = array_ops.shape(x); var sy = array_ops.shape(y); - var (rx, ry) = gen_array_ops.broadcast_gradient_args(sx, sy); + var args = gen_array_ops.broadcast_gradient_args(sx, sy); + var (rx, ry) = (args[0], args[1]); x = math_ops.conj(x); y = math_ops.conj(y); @@ -824,7 +959,7 @@ public static Tensor[] _PowGrad(Operation op, Tensor[] grads) mask = x > 0.0f; var ones = array_ops.ones_like(x); var safe_x = array_ops.where(mask, x, ones); - var x1 = gen_array_ops.log(safe_x); + var x1 = math_ops.log(safe_x); var y1 = array_ops.zeros_like(x); var log_x = array_ops.where(mask, x1, y1); var mul1 = grad * z * log_x; @@ -855,7 +990,8 @@ public static (Tensor, Tensor, bool)[] SmartBroadcastGradientArgs(Tensor x, Tens sy = array_ops.shape_internal(y, optimize: false); } - var (rx, ry) = gen_array_ops.broadcast_gradient_args(sx, sy); + var args = gen_array_ops.broadcast_gradient_args(sx, sy); + var (rx, ry) = (args[0], args[1]); return new[] { (sx, rx, !x.shape.Equals(grad.shape)), diff --git a/src/TensorFlowNET.Core/Gradients/math_grad_eager.cs b/src/TensorFlowNET.Core/Gradients/math_grad_eager.cs index 530bb6c08..f8b16090f 100644 --- a/src/TensorFlowNET.Core/Gradients/math_grad_eager.cs +++ b/src/TensorFlowNET.Core/Gradients/math_grad_eager.cs @@ -47,8 +47,8 @@ public static Tensor[] _MulGrad(EagerOperation op, IntPtr[] grads) { return new Tensor[] { - gen_math_ops.mul(grad, y), - gen_math_ops.mul(grad, x) + math_ops.multiply(grad, y), + math_ops.multiply(grad, x) }; } diff --git a/src/TensorFlowNET.Core/Gradients/nn_grad.cs b/src/TensorFlowNET.Core/Gradients/nn_grad.cs index e95163930..87646a9ea 100644 --- a/src/TensorFlowNET.Core/Gradients/nn_grad.cs +++ b/src/TensorFlowNET.Core/Gradients/nn_grad.cs @@ -192,17 +192,8 @@ public static Tensor[] _Conv2DBackpropInputGrad(Operation op, Tensor[] grads) explicit_paddings: explicit_paddings, dilations: dilations, data_format: data_format), - gen_nn_ops.conv2d(new Conv2dParams - { - Input = grad, - Filter = op.inputs[1], - Strides = strides, - Padding = padding, - DataFormat = data_format, - Dilations = dilations, - ExplicitPaddings = explicit_paddings, - UseCudnnOnGpu = use_cudnn_on_gpu - }) + gen_nn_ops.conv2d(grad, op.inputs[1], strides, padding, + use_cudnn_on_gpu, explicit_paddings, data_format, dilations) }; } @@ -238,6 +229,37 @@ public static Tensor[] _Conv2DGrad(Operation op, Tensor[] grads) }; } + /// + /// Gradient function for Conv2D. + /// + /// + /// + /// + [RegisterGradient("DepthwiseConv2dNative")] + public static Tensor[] _DepthwiseConv2DGrad(Operation op, Tensor[] grads) + { + var dilations = op.get_attr_list("dilations"); + var strides = op.get_attr_list("strides"); + var padding = op.get_attr("padding"); + var explicit_paddings = op.get_attr_list("explicit_paddings"); + var data_format = op.get_attr("data_format"); + var shape = gen_array_ops.shape_n(new Tensor[] { op.inputs[0], op.inputs[1] }); + + return new Tensor[] + { + gen_nn_ops.depthwise_conv2d_native_backprop_input( + shape[0], op.inputs[1], grads[0], + strides, padding, explicit_paddings, + dilations: dilations, + data_format: data_format), + gen_nn_ops.depthwise_conv2d_native_backprop_filter(op.inputs[0], shape[1], grads[0], + strides, padding, + dilations: dilations, + explicit_paddings: explicit_paddings, + data_format: data_format) + }; + } + [RegisterGradient("FusedBatchNorm")] public static Tensor[] _FusedBatchNormGrad(Operation op, Tensor[] grads) => _BaseFusedBatchNormGrad(op, 0, grads); @@ -265,20 +287,27 @@ public static Tensor[] _BaseFusedBatchNormGrad(Operation op, int version, Tensor var epsilon = op.get_attr("epsilon"); var data_format = op.get_attr("data_format"); var is_training = op.get_attr("is_training"); - Func grad_fun = null; - - switch (version) + Func grad_fun = (p) => { - case 2: - grad_fun = gen_nn_ops.fused_batch_norm_grad_v3; - break; - case 1: - // grad_fun = gen_nn_ops.fused_batch_norm_grad_v2; - throw new NotImplementedException(""); - default: - grad_fun = gen_nn_ops.fused_batch_norm_grad; - break; - } + if(version == 2) + { + return gen_nn_ops.fused_batch_norm_grad_v3(p.YBackprop, p.X, p.Scale, + p.ReserveSpace1, p.ReserveSpace2, p.ReserveSpace3, p.Epsilon, + p.DataFormat, p.IsTraining, p.Name); + } + else if(version == 1) + { + return gen_nn_ops.fused_batch_norm_grad_v2(p.YBackprop, p.X, p.Scale, + p.ReserveSpace1, p.ReserveSpace2, p.Epsilon, p.DataFormat, + p.IsTraining, p.Name); + } + else + { + return gen_nn_ops.fused_batch_norm_grad(p.YBackprop, p.X, p.Scale, + p.ReserveSpace1, p.ReserveSpace2, p.Epsilon, p.DataFormat, + p.IsTraining, p.Name); + } + }; if (is_training) { @@ -367,6 +396,23 @@ public static Tensor[] _MaxPoolGrad(Operation op, Tensor[] grads) }; } + [RegisterGradient("AvgPool")] + public static Tensor[] _AvgPoolGrad(Operation op, Tensor[] grads) + { + Tensor grad = grads[0]; + + return new Tensor[] + { + gen_nn_ops.avg_pool_grad( + array_ops.shape(op.inputs[0]), + grad, + op.get_attr_list("ksize"), + op.get_attr_list("strides"), + op.get_attr("padding"), + op.get_attr("data_format")) + }; + } + /// /// Return the gradients for TopK. /// @@ -406,7 +452,7 @@ public static Tensor[] _TopKGrad(Operation op, Tensor[] grads) // finally reshaping it to the original input shape. var scatter = gen_array_ops.scatter_nd(array_ops.expand_dims(ind, -1), array_ops.reshape(grad, new int[] { -1 }), - new Tensor[] { math_ops.reduce_prod(in_shape) }); + math_ops.reduce_prod(in_shape)); return new Tensor[] { diff --git a/src/TensorFlowNET.Core/Graphs/FuncGraph.cs b/src/TensorFlowNET.Core/Graphs/FuncGraph.cs index 3bce52ea5..6f7fa9c5f 100644 --- a/src/TensorFlowNET.Core/Graphs/FuncGraph.cs +++ b/src/TensorFlowNET.Core/Graphs/FuncGraph.cs @@ -81,7 +81,7 @@ internal set public IEnumerable TrainableVariables => Variables.Where(v => v.Trainable); public Dictionary Attrs { get; set; } - Dictionary _captures + internal Dictionary _captures = new Dictionary(); public Tensor[] external_captures @@ -399,7 +399,7 @@ public static FuncGraph func_graph_from_func(string name, Func x is Tensor).Select(x => (Tensor)x)); + .Where(x => x is Tensor).Select(x => (Tensor)x).ToArray()); //var func_args_before = nest.pack_sequence_as(func_args, flat_func_args, true); //var func_kwargs_before = nest.pack_sequence_as(func_kwargs, flat_func_kwargs, true); @@ -544,12 +544,12 @@ private static object _get_defun_input(object arg, string name) Tensor placeholder; try { - placeholder = tf.placeholder(tensor.dtype, tensor.shape, name); + placeholder = GraphOnlyOps.graph_placeholder(tensor.dtype, tensor.shape, name); } - catch (ValueError) + catch (ValueError ex) { - // TODO(Rinne): Add warning here. - placeholder = tf.placeholder(tensor.dtype, tensor.shape); + tf.Logger.Warning(ex.ToString()); + placeholder = GraphOnlyOps.graph_placeholder(tensor.dtype, tensor.shape); } handle_data_util.copy_handle_data(tensor, placeholder); if (name is not null) @@ -575,12 +575,12 @@ private static object _get_defun_input(object arg, string name) Tensor placeholder; try { - placeholder = tf.placeholder(spec.dtype, spec.shape, requested_name); + placeholder = GraphOnlyOps.graph_placeholder(spec.dtype, spec.shape, requested_name); } catch (ValueError) { // TODO(Rinne): Add warning here. - placeholder = tf.placeholder(spec.dtype, spec.shape); + placeholder = GraphOnlyOps.graph_placeholder(spec.dtype, spec.shape); } if (name is not null) { diff --git a/src/TensorFlowNET.Core/Graphs/Graph.cs b/src/TensorFlowNET.Core/Graphs/Graph.cs index eb8df5812..9e879a0f0 100644 --- a/src/TensorFlowNET.Core/Graphs/Graph.cs +++ b/src/TensorFlowNET.Core/Graphs/Graph.cs @@ -129,7 +129,7 @@ public int seed } } - protected Graph outer_graph; + internal Graph outer_graph; public Graph OuterGraph => outer_graph; public Dictionary Functions => _functions; public SafeGraphHandle c_graph => _handle; diff --git a/src/TensorFlowNET.Core/Keras/Activations/Activations.cs b/src/TensorFlowNET.Core/Keras/Activations/Activations.cs index f0d59ed62..37264104a 100644 --- a/src/TensorFlowNET.Core/Keras/Activations/Activations.cs +++ b/src/TensorFlowNET.Core/Keras/Activations/Activations.cs @@ -32,6 +32,7 @@ public interface IActivationsApi Activation Linear { get; } Activation Relu { get; } + Activation Relu6 { get; } Activation Sigmoid { get; } diff --git a/src/TensorFlowNET.Core/Keras/ArgsDefinition/Activation/ExponentialArgs.cs b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Activation/ExponentialArgs.cs new file mode 100644 index 000000000..ef024971d --- /dev/null +++ b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Activation/ExponentialArgs.cs @@ -0,0 +1,10 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tensorflow.Keras.ArgsDefinition +{ + public class ExponentialArgs : LayerArgs + { + } +} diff --git a/src/TensorFlowNET.Core/Keras/ArgsDefinition/Activation/HardSigmoidArgs.cs b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Activation/HardSigmoidArgs.cs new file mode 100644 index 000000000..788e0f36d --- /dev/null +++ b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Activation/HardSigmoidArgs.cs @@ -0,0 +1,10 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tensorflow.Keras.ArgsDefinition +{ + public class HardSigmoidArgs : LayerArgs + { + } +} diff --git a/src/TensorFlowNET.Core/Keras/ArgsDefinition/Activation/SELUArgs.cs b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Activation/SELUArgs.cs new file mode 100644 index 000000000..eb0e18446 --- /dev/null +++ b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Activation/SELUArgs.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tensorflow.Keras.ArgsDefinition +{ + public class SELUArgs : LayerArgs + { + + } +} diff --git a/src/TensorFlowNET.Core/Keras/ArgsDefinition/Activation/SoftplusArgs.cs b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Activation/SoftplusArgs.cs new file mode 100644 index 000000000..7b4f20795 --- /dev/null +++ b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Activation/SoftplusArgs.cs @@ -0,0 +1,10 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tensorflow.Keras.ArgsDefinition +{ + public class SoftplusArgs : LayerArgs + { + } +} diff --git a/src/TensorFlowNET.Core/Keras/ArgsDefinition/Activation/SoftsignArgs.cs b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Activation/SoftsignArgs.cs new file mode 100644 index 000000000..4e23d261d --- /dev/null +++ b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Activation/SoftsignArgs.cs @@ -0,0 +1,10 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tensorflow.Keras.ArgsDefinition +{ + public class SoftsignArgs : LayerArgs + { + } +} diff --git a/src/TensorFlowNET.Core/Keras/ArgsDefinition/Activation/SwishArgs.cs b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Activation/SwishArgs.cs new file mode 100644 index 000000000..3dea06a23 --- /dev/null +++ b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Activation/SwishArgs.cs @@ -0,0 +1,10 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tensorflow.Keras.ArgsDefinition +{ + public class SwishArgs : LayerArgs + { + } +} diff --git a/src/TensorFlowNET.Core/Keras/ArgsDefinition/Activation/TanhArgs.cs b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Activation/TanhArgs.cs new file mode 100644 index 000000000..5df41b71b --- /dev/null +++ b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Activation/TanhArgs.cs @@ -0,0 +1,10 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tensorflow.Keras.ArgsDefinition +{ + public class TanhArgs : LayerArgs + { + } +} diff --git a/src/TensorFlowNET.Core/Keras/ArgsDefinition/Convolution/Conv2DTransposeArgs.cs b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Convolution/Conv2DTransposeArgs.cs new file mode 100644 index 000000000..3daba9465 --- /dev/null +++ b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Convolution/Conv2DTransposeArgs.cs @@ -0,0 +1,10 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tensorflow.Keras.ArgsDefinition +{ + public class Conv2DTransposeArgs : Conv2DArgs + { + } +} diff --git a/src/TensorFlowNET.Core/Keras/ArgsDefinition/DataAdapterArgs.cs b/src/TensorFlowNET.Core/Keras/ArgsDefinition/DataAdapterArgs.cs index 78882e82d..ba0332836 100644 --- a/src/TensorFlowNET.Core/Keras/ArgsDefinition/DataAdapterArgs.cs +++ b/src/TensorFlowNET.Core/Keras/ArgsDefinition/DataAdapterArgs.cs @@ -1,5 +1,6 @@ using Tensorflow.Keras.Engine; using Tensorflow.Keras.Saving; +using Tensorflow.NumPy; namespace Tensorflow.Keras.ArgsDefinition { @@ -16,5 +17,7 @@ public class DataAdapterArgs: IKerasConfig public int Worker { get; set; } public bool UseMultiprocessing { get; set; } public IModel Model { get; set; } + public Dictionary ClassWeight = null; + public NDArray SampleWeight = null; } } diff --git a/src/TensorFlowNET.Core/Keras/ArgsDefinition/DataHandlerArgs.cs b/src/TensorFlowNET.Core/Keras/ArgsDefinition/DataHandlerArgs.cs index 82530e950..72d0bb811 100644 --- a/src/TensorFlowNET.Core/Keras/ArgsDefinition/DataHandlerArgs.cs +++ b/src/TensorFlowNET.Core/Keras/ArgsDefinition/DataHandlerArgs.cs @@ -1,5 +1,6 @@ using Tensorflow.Keras.Engine; using Tensorflow.Keras.Saving; +using Tensorflow.NumPy; namespace Tensorflow.Keras.ArgsDefinition { @@ -18,5 +19,7 @@ public class DataHandlerArgs: IKerasConfig public bool UseMultiprocessing { get; set; } = false; public IModel Model { get; set; } public IVariableV1 StepsPerExecution { get; set; } + public Dictionary ClassWeight = null; + public NDArray SampleWeight = null; } } diff --git a/src/TensorFlowNET.Core/Keras/ArgsDefinition/Merging/AddArgs.cs b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Merging/AddArgs.cs new file mode 100644 index 000000000..016d58203 --- /dev/null +++ b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Merging/AddArgs.cs @@ -0,0 +1,10 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tensorflow.Keras.ArgsDefinition +{ + public class AddArgs : MergeArgs + { + } +} diff --git a/src/TensorFlowNET.Core/Keras/ArgsDefinition/Merging/ConcatenateArgs.cs b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Merging/ConcatenateArgs.cs new file mode 100644 index 000000000..4a81d139d --- /dev/null +++ b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Merging/ConcatenateArgs.cs @@ -0,0 +1,10 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tensorflow.Keras.ArgsDefinition +{ + public class ConcatenateArgs : MergeArgs + { + } +} diff --git a/src/TensorFlowNET.Core/Keras/ArgsDefinition/Merging/MergeArgs.cs b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Merging/MergeArgs.cs index 0140b3dd0..9bcf1908e 100644 --- a/src/TensorFlowNET.Core/Keras/ArgsDefinition/Merging/MergeArgs.cs +++ b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Merging/MergeArgs.cs @@ -1,13 +1,15 @@ -using System; +using Newtonsoft.Json; +using System; using System.Collections.Generic; using System.Text; namespace Tensorflow.Keras.ArgsDefinition { // TODO: complete the implementation - public class MergeArgs : LayerArgs + public class MergeArgs : AutoSerializeLayerArgs { public Tensors Inputs { get; set; } + [JsonProperty("axis")] public int Axis { get; set; } } } diff --git a/src/TensorFlowNET.Core/Keras/ArgsDefinition/Merging/SubtractArgs.cs b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Merging/SubtractArgs.cs new file mode 100644 index 000000000..1e3621cb6 --- /dev/null +++ b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Merging/SubtractArgs.cs @@ -0,0 +1,10 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tensorflow.Keras.ArgsDefinition +{ + public class SubtractArgs : MergeArgs + { + } +} diff --git a/src/TensorFlowNET.Core/Keras/ArgsDefinition/Pooling/GlobalAveragePooling1DArgs.cs b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Pooling/GlobalAveragePooling1DArgs.cs new file mode 100644 index 000000000..e73aff766 --- /dev/null +++ b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Pooling/GlobalAveragePooling1DArgs.cs @@ -0,0 +1,10 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tensorflow.Keras.ArgsDefinition +{ + public class GlobalAveragePooling1DArgs : Pooling1DArgs + { + } +} diff --git a/src/TensorFlowNET.Core/Keras/ArgsDefinition/Pooling/GlobalAveragePooling2DArgs.cs b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Pooling/GlobalAveragePooling2DArgs.cs new file mode 100644 index 000000000..d143cf471 --- /dev/null +++ b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Pooling/GlobalAveragePooling2DArgs.cs @@ -0,0 +1,10 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tensorflow.Keras.ArgsDefinition +{ + public class GlobalAveragePooling2DArgs : Pooling2DArgs + { + } +} diff --git a/src/TensorFlowNET.Core/Keras/ArgsDefinition/Pooling/GlobalMaxPooling1DArgs.cs b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Pooling/GlobalMaxPooling1DArgs.cs new file mode 100644 index 000000000..e03227feb --- /dev/null +++ b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Pooling/GlobalMaxPooling1DArgs.cs @@ -0,0 +1,10 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tensorflow.Keras.ArgsDefinition +{ + public class GlobalMaxPooling1DArgs : Pooling1DArgs + { + } +} diff --git a/src/TensorFlowNET.Core/Keras/ArgsDefinition/Pooling/GlobalMaxPooling2DArgs.cs b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Pooling/GlobalMaxPooling2DArgs.cs new file mode 100644 index 000000000..a95cac836 --- /dev/null +++ b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Pooling/GlobalMaxPooling2DArgs.cs @@ -0,0 +1,10 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tensorflow.Keras.ArgsDefinition +{ + public class GlobalMaxPooling2DArgs : Pooling2DArgs + { + } +} diff --git a/src/TensorFlowNET.Core/Keras/ArgsDefinition/Pooling/MaxPooling1DArgs.cs b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Pooling/MaxPooling1DArgs.cs new file mode 100644 index 000000000..4cfff2c15 --- /dev/null +++ b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Pooling/MaxPooling1DArgs.cs @@ -0,0 +1,10 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tensorflow.Keras.ArgsDefinition +{ + public class MaxPooling1DArgs : Pooling1DArgs + { + } +} diff --git a/src/TensorFlowNET.Core/Keras/ArgsDefinition/Reshaping/UpSampling2DArgs.cs b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Reshaping/UpSampling2DArgs.cs index b35e0e4b6..504b3d46d 100644 --- a/src/TensorFlowNET.Core/Keras/ArgsDefinition/Reshaping/UpSampling2DArgs.cs +++ b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Reshaping/UpSampling2DArgs.cs @@ -7,7 +7,7 @@ public class UpSampling2DArgs : AutoSerializeLayerArgs [JsonProperty("size")] public Shape Size { get; set; } [JsonProperty("data_format")] - public string DataFormat { get; set; } + public string DataFormat { get; set; } = "channels_last"; /// /// 'nearest', 'bilinear' /// diff --git a/src/TensorFlowNET.Core/Keras/ArgsDefinition/Reshaping/Upsampling1DArgs.cs b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Reshaping/Upsampling1DArgs.cs new file mode 100644 index 000000000..4e3dbf17a --- /dev/null +++ b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Reshaping/Upsampling1DArgs.cs @@ -0,0 +1,10 @@ +using Newtonsoft.Json; + +namespace Tensorflow.Keras.ArgsDefinition +{ + public class UpSampling1DArgs : AutoSerializeLayerArgs + { + [JsonProperty("size")] + public int Size { get; set; } + } +} diff --git a/src/TensorFlowNET.Core/Keras/ArgsDefinition/Rnn/BidirectionalArgs.cs b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Rnn/BidirectionalArgs.cs new file mode 100644 index 000000000..d658a82e9 --- /dev/null +++ b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Rnn/BidirectionalArgs.cs @@ -0,0 +1,20 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Text; +using Tensorflow.NumPy; + +namespace Tensorflow.Keras.ArgsDefinition +{ + public class BidirectionalArgs : AutoSerializeLayerArgs + { + [JsonProperty("layer")] + public ILayer Layer { get; set; } + [JsonProperty("merge_mode")] + public string? MergeMode { get; set; } + [JsonProperty("backward_layer")] + public ILayer BackwardLayer { get; set; } + public NDArray Weights { get; set; } + } + +} diff --git a/src/TensorFlowNET.Core/Keras/ArgsDefinition/Rnn/GRUArgs.cs b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Rnn/GRUArgs.cs new file mode 100644 index 000000000..cdc3097e9 --- /dev/null +++ b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Rnn/GRUArgs.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tensorflow.Keras.ArgsDefinition +{ + public class GRUArgs : AutoSerializeLayerArgs + { + public int Units { get; set; } + public Activation Activation { get; set; } + public Activation RecurrentActivation { get; set; } + public bool UseBias { get; set; } = true; + public float Dropout { get; set; } = .0f; + public float RecurrentDropout { get; set; } = .0f; + public IInitializer KernelInitializer { get; set; } + public IInitializer RecurrentInitializer { get; set; } + public IInitializer BiasInitializer { get; set; } + public bool ReturnSequences { get;set; } + public bool ReturnState { get;set; } + public bool GoBackwards { get;set; } + public bool Stateful { get;set; } + public bool Unroll { get;set; } + public bool TimeMajor { get;set; } + public bool ResetAfter { get;set; } + public int Implementation { get; set; } = 2; + + } + +} diff --git a/src/TensorFlowNET.Core/Keras/ArgsDefinition/Rnn/GRUCellArgs.cs b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Rnn/GRUCellArgs.cs new file mode 100644 index 000000000..624756afe --- /dev/null +++ b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Rnn/GRUCellArgs.cs @@ -0,0 +1,39 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tensorflow.Keras.ArgsDefinition +{ + public class GRUCellArgs : AutoSerializeLayerArgs + { + [JsonProperty("units")] + public int Units { get; set; } + // TODO(Rinne): lack of initialized value of Activation. Merging keras + // into tf.net could resolve it. + [JsonProperty("activation")] + public Activation Activation { get; set; } + [JsonProperty("recurrent_activation")] + public Activation RecurrentActivation { get; set; } + [JsonProperty("use_bias")] + public bool UseBias { get; set; } = true; + [JsonProperty("dropout")] + public float Dropout { get; set; } = .0f; + [JsonProperty("recurrent_dropout")] + public float RecurrentDropout { get; set; } = .0f; + [JsonProperty("kernel_initializer")] + public IInitializer KernelInitializer { get; set; } + [JsonProperty("recurrent_initializer")] + public IInitializer RecurrentInitializer { get; set; } + [JsonProperty("bias_initializer")] + public IInitializer BiasInitializer { get; set; } + [JsonProperty("reset_after")] + public bool ResetAfter { get;set; } + [JsonProperty("implementation")] + public int Implementation { get; set; } = 2; + + + + } + +} diff --git a/src/TensorFlowNET.Core/Keras/ArgsDefinition/Rnn/GRUOptionalArgs.cs b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Rnn/GRUOptionalArgs.cs new file mode 100644 index 000000000..1d215576f --- /dev/null +++ b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Rnn/GRUOptionalArgs.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tensorflow.Keras.ArgsDefinition +{ + public class GRUOptionalArgs : RnnOptionalArgs + { + public string Identifier => "GRU"; + } +} diff --git a/src/TensorFlowNET.Core/Keras/ArgsDefinition/Rnn/LSTMArgs.cs b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Rnn/LSTMArgs.cs index 764641474..a6beb77e8 100644 --- a/src/TensorFlowNET.Core/Keras/ArgsDefinition/Rnn/LSTMArgs.cs +++ b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Rnn/LSTMArgs.cs @@ -1,11 +1,14 @@ -namespace Tensorflow.Keras.ArgsDefinition.Rnn +namespace Tensorflow.Keras.ArgsDefinition { public class LSTMArgs : RNNArgs { // TODO: maybe change the `RNNArgs` and implement this class. public bool UnitForgetBias { get; set; } - public float Dropout { get; set; } - public float RecurrentDropout { get; set; } public int Implementation { get; set; } + + public LSTMArgs Clone() + { + return (LSTMArgs)MemberwiseClone(); + } } } diff --git a/src/TensorFlowNET.Core/Keras/ArgsDefinition/Rnn/LSTMCellArgs.cs b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Rnn/LSTMCellArgs.cs index 594c99bb0..f45032312 100644 --- a/src/TensorFlowNET.Core/Keras/ArgsDefinition/Rnn/LSTMCellArgs.cs +++ b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Rnn/LSTMCellArgs.cs @@ -1,7 +1,35 @@ -namespace Tensorflow.Keras.ArgsDefinition.Rnn +using Newtonsoft.Json; +using static Tensorflow.Binding; + +namespace Tensorflow.Keras.ArgsDefinition { // TODO: complete the implementation - public class LSTMCellArgs : LayerArgs + public class LSTMCellArgs : AutoSerializeLayerArgs { + [JsonProperty("units")] + public int Units { get; set; } + // TODO(Rinne): lack of initialized value of Activation. Merging keras + // into tf.net could resolve it. + [JsonProperty("activation")] + public Activation Activation { get; set; } + [JsonProperty("recurrent_activation")] + public Activation RecurrentActivation { get; set; } + [JsonProperty("use_bias")] + public bool UseBias { get; set; } = true; + [JsonProperty("dropout")] + public float Dropout { get; set; } = .0f; + [JsonProperty("recurrent_dropout")] + public float RecurrentDropout { get; set; } = .0f; + [JsonProperty("kernel_initializer")] + public IInitializer KernelInitializer { get; set; } + [JsonProperty("recurrent_initializer")] + public IInitializer RecurrentInitializer { get; set; } + [JsonProperty("bias_initializer")] + public IInitializer BiasInitializer { get; set; } + [JsonProperty("unit_forget_bias")] + public bool UnitForgetBias { get; set; } = true; + [JsonProperty("implementation")] + public int Implementation { get; set; } = 2; + } } diff --git a/src/TensorFlowNET.Core/Keras/ArgsDefinition/Rnn/LSTMOptionalArgs.cs b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Rnn/LSTMOptionalArgs.cs new file mode 100644 index 000000000..2829927c3 --- /dev/null +++ b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Rnn/LSTMOptionalArgs.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tensorflow.Keras.ArgsDefinition.Rnn +{ + public class LSTMOptionalArgs : RnnOptionalArgs + { + public string Identifier => "LSTM"; + } +} diff --git a/src/TensorFlowNET.Core/Keras/ArgsDefinition/Rnn/RNNArgs.cs b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Rnn/RNNArgs.cs index 2585592c1..d0b73ba44 100644 --- a/src/TensorFlowNET.Core/Keras/ArgsDefinition/Rnn/RNNArgs.cs +++ b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Rnn/RNNArgs.cs @@ -1,17 +1,12 @@ using Newtonsoft.Json; using System.Collections.Generic; +using Tensorflow.Keras.Layers; -namespace Tensorflow.Keras.ArgsDefinition.Rnn +namespace Tensorflow.Keras.ArgsDefinition { + // TODO(Rinne): add regularizers. public class RNNArgs : AutoSerializeLayerArgs { - public interface IRnnArgCell : ILayer - { - object state_size { get; } - } - [JsonProperty("cell")] - // TODO: the cell should be serialized with `serialize_keras_object`. - public IRnnArgCell Cell { get; set; } = null; [JsonProperty("return_sequences")] public bool ReturnSequences { get; set; } = false; [JsonProperty("return_state")] @@ -24,31 +19,31 @@ public interface IRnnArgCell : ILayer public bool Unroll { get; set; } = false; [JsonProperty("time_major")] public bool TimeMajor { get; set; } = false; - // TODO: Add `num_constants` and `zero_output_for_mask`. - public Dictionary Kwargs { get; set; } = null; + public int? InputDim { get; set; } + public int? InputLength { get; set; } + // TODO: Add `num_constants` and `zero_output_for_mask`. + [JsonProperty("units")] public int Units { get; set; } + [JsonProperty("activation")] public Activation Activation { get; set; } + [JsonProperty("recurrent_activation")] public Activation RecurrentActivation { get; set; } + [JsonProperty("use_bias")] public bool UseBias { get; set; } = true; public IInitializer KernelInitializer { get; set; } public IInitializer RecurrentInitializer { get; set; } public IInitializer BiasInitializer { get; set; } + [JsonProperty("dropout")] + public float Dropout { get; set; } = .0f; + [JsonProperty("zero_output_for_mask")] + public bool ZeroOutputForMask { get; set; } = false; + [JsonProperty("recurrent_dropout")] + public float RecurrentDropout { get; set; } = .0f; - // kernel_regularizer=None, - // recurrent_regularizer=None, - // bias_regularizer=None, - // activity_regularizer=None, - // kernel_constraint=None, - // recurrent_constraint=None, - // bias_constraint=None, - // dropout=0., - // recurrent_dropout=0., - // return_sequences=False, - // return_state=False, - // go_backwards=False, - // stateful=False, - // unroll=False, - // **kwargs): + public RNNArgs Clone() + { + return (RNNArgs)MemberwiseClone(); + } } } diff --git a/src/TensorFlowNET.Core/Keras/ArgsDefinition/Rnn/RnnOptionalArgs.cs b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Rnn/RnnOptionalArgs.cs new file mode 100644 index 000000000..a6520589d --- /dev/null +++ b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Rnn/RnnOptionalArgs.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Tensorflow.Common.Types; + +namespace Tensorflow.Keras.ArgsDefinition +{ + public class RnnOptionalArgs: IOptionalArgs + { + public string Identifier => "Rnn"; + public Tensor Mask { get; set; } = null; + public Tensors Constants { get; set; } = null; + } +} diff --git a/src/TensorFlowNET.Core/Keras/ArgsDefinition/Rnn/SimpleRNNArgs.cs b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Rnn/SimpleRNNArgs.cs index fcfd694d1..e45ef79d0 100644 --- a/src/TensorFlowNET.Core/Keras/ArgsDefinition/Rnn/SimpleRNNArgs.cs +++ b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Rnn/SimpleRNNArgs.cs @@ -1,4 +1,4 @@ -namespace Tensorflow.Keras.ArgsDefinition.Rnn +namespace Tensorflow.Keras.ArgsDefinition { public class SimpleRNNArgs : RNNArgs { diff --git a/src/TensorFlowNET.Core/Keras/ArgsDefinition/Rnn/SimpleRNNCellArgs.cs b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Rnn/SimpleRNNCellArgs.cs new file mode 100644 index 000000000..b84ea21b3 --- /dev/null +++ b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Rnn/SimpleRNNCellArgs.cs @@ -0,0 +1,27 @@ +using Newtonsoft.Json; + +namespace Tensorflow.Keras.ArgsDefinition +{ + public class SimpleRNNCellArgs: AutoSerializeLayerArgs + { + [JsonProperty("units")] + public int Units { get; set; } + // TODO(Rinne): lack of initialized value of Activation. Merging keras + // into tf.net could resolve it. + [JsonProperty("activation")] + public Activation Activation { get; set; } + [JsonProperty("use_bias")] + public bool UseBias { get; set; } = true; + [JsonProperty("dropout")] + public float Dropout { get; set; } = .0f; + [JsonProperty("recurrent_dropout")] + public float RecurrentDropout { get; set; } = .0f; + [JsonProperty("kernel_initializer")] + public IInitializer KernelInitializer { get; set; } + [JsonProperty("recurrent_initializer")] + public IInitializer RecurrentInitializer { get; set; } + [JsonProperty("bias_initializer")] + public IInitializer BiasInitializer { get; set; } + + } +} diff --git a/src/TensorFlowNET.Core/Keras/ArgsDefinition/Rnn/SimpleRNNOptionalArgs.cs b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Rnn/SimpleRNNOptionalArgs.cs new file mode 100644 index 000000000..a8b8caf06 --- /dev/null +++ b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Rnn/SimpleRNNOptionalArgs.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tensorflow.Keras.ArgsDefinition.Rnn +{ + public class SimpleRNNOptionalArgs : RnnOptionalArgs + { + public string Identifier => "SimpleRNN"; + } +} diff --git a/src/TensorFlowNET.Core/Keras/ArgsDefinition/Rnn/StackedRNNCellsArgs.cs b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Rnn/StackedRNNCellsArgs.cs index fdfadab85..2600f14ee 100644 --- a/src/TensorFlowNET.Core/Keras/ArgsDefinition/Rnn/StackedRNNCellsArgs.cs +++ b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Rnn/StackedRNNCellsArgs.cs @@ -1,10 +1,10 @@ using System.Collections.Generic; +using Tensorflow.Keras.Layers; -namespace Tensorflow.Keras.ArgsDefinition.Rnn +namespace Tensorflow.Keras.ArgsDefinition { public class StackedRNNCellsArgs : LayerArgs { - public IList Cells { get; set; } - public Dictionary Kwargs { get; set; } = null; + public bool ReverseStateOrder = false; } } diff --git a/src/TensorFlowNET.Core/Keras/ArgsDefinition/Rnn/WrapperArgs.cs b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Rnn/WrapperArgs.cs new file mode 100644 index 000000000..ec8e16d59 --- /dev/null +++ b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Rnn/WrapperArgs.cs @@ -0,0 +1,24 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Runtime.CompilerServices; +using System.Text; + + +namespace Tensorflow.Keras.ArgsDefinition +{ + public class WrapperArgs : AutoSerializeLayerArgs + { + [JsonProperty("layer")] + public ILayer Layer { get; set; } + + public WrapperArgs(ILayer layer) + { + Layer = layer; + } + + public static implicit operator WrapperArgs(BidirectionalArgs args) + => new WrapperArgs(args.Layer); + } + +} diff --git a/src/TensorFlowNET.Core/Keras/Engine/ICallback.cs b/src/TensorFlowNET.Core/Keras/Engine/ICallback.cs index 096dbd2ef..e114ca97f 100644 --- a/src/TensorFlowNET.Core/Keras/Engine/ICallback.cs +++ b/src/TensorFlowNET.Core/Keras/Engine/ICallback.cs @@ -14,6 +14,9 @@ public interface ICallback void on_predict_batch_end(long end_step, Dictionary logs); void on_predict_end(); void on_test_begin(); + void on_test_end(Dictionary logs); void on_test_batch_begin(long step); void on_test_batch_end(long end_step, Dictionary logs); + + } diff --git a/src/TensorFlowNET.Core/Keras/Engine/IModel.cs b/src/TensorFlowNET.Core/Keras/Engine/IModel.cs index 19f3df9ba..889c76d91 100644 --- a/src/TensorFlowNET.Core/Keras/Engine/IModel.cs +++ b/src/TensorFlowNET.Core/Keras/Engine/IModel.cs @@ -3,6 +3,7 @@ using Tensorflow.Keras.Metrics; using Tensorflow.Keras.Saving; using Tensorflow.NumPy; +using Tensorflow.Util; namespace Tensorflow.Keras.Engine; @@ -22,8 +23,11 @@ ICallback fit(NDArray x, NDArray y, int verbose = 1, List callbacks = null, float validation_split = 0f, - (NDArray val_x, NDArray val_y)? validation_data = null, + ValidationDataPack validation_data = null, + int validation_step = 10, bool shuffle = true, + Dictionary class_weight = null, + NDArray sample_weight = null, int initial_epoch = 0, int max_queue_size = 10, int workers = 1, @@ -35,8 +39,24 @@ ICallback fit(IEnumerable x, NDArray y, int verbose = 1, List callbacks = null, float validation_split = 0f, - (IEnumerable val_x, NDArray val_y)? validation_data = null, + ValidationDataPack validation_data = null, bool shuffle = true, + Dictionary class_weight = null, + NDArray sample_weight = null, + int initial_epoch = 0, + int max_queue_size = 10, + int workers = 1, + bool use_multiprocessing = false); + + public ICallback fit(IDatasetV2 dataset, + int batch_size = -1, + int epochs = 1, + int verbose = 1, + List callbacks = null, + IDatasetV2 validation_data = null, + int validation_step = 10, // 间隔多少次会进行一次验证 + bool shuffle = true, + Dictionary class_weight = null, int initial_epoch = 0, int max_queue_size = 10, int workers = 1, @@ -63,6 +83,8 @@ void load_weights(string filepath, Dictionary evaluate(NDArray x, NDArray y, int batch_size = -1, int verbose = 1, + NDArray sample_weight = null, + int steps = -1, int max_queue_size = 10, int workers = 1, @@ -78,6 +100,14 @@ Tensors predict(Tensors x, int workers = 1, bool use_multiprocessing = false); + public Tensors predict(IDatasetV2 dataset, + int batch_size = -1, + int verbose = 0, + int steps = -1, + int max_queue_size = 10, + int workers = 1, + bool use_multiprocessing = false); + void summary(int line_length = -1, float[] positions = null); IKerasConfig get_config(); diff --git a/src/TensorFlowNET.Core/Keras/Engine/KerasTensor.cs b/src/TensorFlowNET.Core/Keras/Engine/KerasTensor.cs new file mode 100644 index 000000000..5a264b631 --- /dev/null +++ b/src/TensorFlowNET.Core/Keras/Engine/KerasTensor.cs @@ -0,0 +1,75 @@ +namespace Tensorflow.Keras.Engine; + +/// +/// A representation of a Keras in/output during Functional API construction. +/// +public class KerasTensor +{ + private Tensors _original_tensors; + public Tensors original_tensors + { + get => _original_tensors; + set => _original_tensors = value; + } + + private Shape _inferred_value; + public Shape inferred_value => _inferred_value; + + private string _name; + private TensorSpec _type_spec; + public Shape shape => _type_spec.shape; + public TF_DataType dtype => _type_spec.dtype; + + public KerasTensor(TensorSpec type_spec, Shape inferred_value = null, string name = null) + { + _type_spec = type_spec; + _inferred_value = inferred_value; + _name = name; + } + + public static KerasTensor from_tensor(Tensor tensor) + { + var type_spec = tensor.ToTensorSpec(); + Shape? inferred_value = default; + if (tensor.dtype == TF_DataType.TF_INT32 && tensor.rank < 2) + { + inferred_value = tf.ones(tensor).shape; + } + var kt = new KerasTensor(type_spec, inferred_value: inferred_value, name: tensor.name); + kt.original_tensors = tensor; + return kt; + } + + public KerasTensor this[int idx] + => _original_tensors.First()[idx]; + + public KerasTensor this[params Slice[] slices] + => _original_tensors.First()[slices]; + + public override string ToString() + => _original_tensors.Length switch + { + > 1 => "[" + string.Join(", ", _original_tensors.Select(x => $"KerasTensor: shape={x.shape} dtype={x.dtype.as_numpy_name()}{GetInferredValueString()}")) + "]", + 1 => $"KerasTensor: shape={_original_tensors.shape} dtype={_original_tensors.dtype.as_numpy_name()}{GetInferredValueString()}", + _ => _original_tensors.ToString(), + }; + + private string GetInferredValueString() + => _inferred_value == null ? "" : $" inferred_value={_inferred_value}"; + + public static implicit operator Tensors(KerasTensor kt) + => kt._original_tensors; + + public static implicit operator Tensor(KerasTensor kt) + { + Tensor tensor = kt._original_tensors; + tensor.IsFromKerasTensor = true; + return tensor; + } + + public static implicit operator KerasTensor(Tensor tensor) + => from_tensor(tensor); + + public static implicit operator KerasTensor(Tensors tensors) + => from_tensor(tensors.First()); +} diff --git a/src/TensorFlowNET.Core/Keras/IOptimizerApi.cs b/src/TensorFlowNET.Core/Keras/IOptimizerApi.cs index 961ce91ae..6c15fd469 100644 --- a/src/TensorFlowNET.Core/Keras/IOptimizerApi.cs +++ b/src/TensorFlowNET.Core/Keras/IOptimizerApi.cs @@ -25,6 +25,27 @@ IOptimizer Adam(float learning_rate = 0.001f, bool amsgrad = false, string name = "Adam"); + /// + /// Adam enables L2 weight decay on gradients. + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + IOptimizer AdamW(float learning_rate = 0.001f, + float weight_decay = 0.004f, + float beta_1 = 0.9f, + float beta_2 = 0.999f, + float epsilon = 1e-7f, + bool amsgrad = false, + List no_decay_params = null, + string name = "AdamW"); + /// /// Construct a new RMSprop optimizer. /// @@ -42,6 +63,6 @@ IOptimizer RMSprop(float learning_rate = 0.001f, bool centered = false, string name = "RMSprop"); - IOptimizer SGD(float learning_rate); + IOptimizer SGD(float learning_rate = 0.01f, float momentum = 0f); } } diff --git a/src/TensorFlowNET.Core/Keras/Layers/ILayer.cs b/src/TensorFlowNET.Core/Keras/Layers/ILayer.cs index f76693945..2f92c4e57 100644 --- a/src/TensorFlowNET.Core/Keras/Layers/ILayer.cs +++ b/src/TensorFlowNET.Core/Keras/Layers/ILayer.cs @@ -1,4 +1,5 @@ -using Tensorflow.Keras.Engine; +using Tensorflow.Common.Types; +using Tensorflow.Keras.Engine; using Tensorflow.Keras.Saving; using Tensorflow.NumPy; using Tensorflow.Training; @@ -14,7 +15,7 @@ public interface ILayer: IWithTrackable, IKerasConfigable List Layers { get; } List InboundNodes { get; } List OutboundNodes { get; } - Tensors Apply(Tensors inputs, Tensor state = null, bool training = false); + Tensors Apply(Tensors inputs, Tensors states = null, bool? training = false, IOptionalArgs? optional_args = null); List TrainableVariables { get; } List TrainableWeights { get; } List NonTrainableWeights { get; } diff --git a/src/TensorFlowNET.Core/Keras/Layers/ILayersApi.Reshaping.cs b/src/TensorFlowNET.Core/Keras/Layers/ILayersApi.Reshaping.cs index d41e06887..ae34c514f 100644 --- a/src/TensorFlowNET.Core/Keras/Layers/ILayersApi.Reshaping.cs +++ b/src/TensorFlowNET.Core/Keras/Layers/ILayersApi.Reshaping.cs @@ -9,6 +9,10 @@ public partial interface ILayersApi public ILayer Reshape(Shape target_shape); public ILayer Reshape(object[] target_shape); + public ILayer UpSampling1D( + int size + ); + public ILayer UpSampling2D(Shape size = null, string data_format = null, string interpolation = "nearest"); diff --git a/src/TensorFlowNET.Core/Keras/Layers/ILayersApi.cs b/src/TensorFlowNET.Core/Keras/Layers/ILayersApi.cs index 6a29f9e5e..57273eb08 100644 --- a/src/TensorFlowNET.Core/Keras/Layers/ILayersApi.cs +++ b/src/TensorFlowNET.Core/Keras/Layers/ILayersApi.cs @@ -1,5 +1,7 @@ using System; using Tensorflow.Framework.Models; +using Tensorflow.Keras.Engine; +using Tensorflow.Keras.Layers; using Tensorflow.NumPy; using static Google.Protobuf.Reflection.FieldDescriptorProto.Types; @@ -53,6 +55,12 @@ public ILayer Conv1D(int filters, string kernel_initializer = "glorot_uniform", string bias_initializer = "zeros"); + public ILayer Conv2D(int filters, + Shape kernel_size = null, + Shape strides = null, + string padding = "valid" + ); + public ILayer Conv2D(int filters, Shape kernel_size = null, Shape strides = null, @@ -93,6 +101,19 @@ public ILayer Conv2D(int filters, bool use_bias = true, string kernel_initializer = "glorot_uniform", string bias_initializer = "zeros"); + public ILayer DepthwiseConv2D(Shape kernel_size = null, + Shape strides = null, + string padding = "valid", + string data_format = null, + Shape dilation_rate = null, + int groups = 1, + int depth_multiplier = 1, + string activation = null, + bool use_bias = false, + string kernel_initializer = "glorot_uniform", + string bias_initializer = "zeros", + string depthwise_initializer = "glorot_uniform" + ); public ILayer Dense(int units); public ILayer Dense(int units, @@ -134,7 +155,7 @@ public ILayer EinsumDense(string equation, public ILayer GlobalMaxPooling1D(string data_format = "channels_last"); public ILayer GlobalMaxPooling2D(string data_format = "channels_last"); - public Tensors Input(Shape shape = null, + public KerasTensor Input(Shape shape = null, int batch_size = -1, string name = null, TF_DataType dtype = TF_DataType.DtInvalid, @@ -159,6 +180,21 @@ public ILayer LayerNormalization(Axis? axis, public ILayer Normalization(Shape? input_shape = null, int? axis = -1, float? mean = null, float? variance = null, bool invert = false); public ILayer LeakyReLU(float alpha = 0.3f); + public ILayer ReLU6(); + + + public IRnnCell LSTMCell(int uints, + string activation = "tanh", + string recurrent_activation = "sigmoid", + bool use_bias = true, + string kernel_initializer = "glorot_uniform", + string recurrent_initializer = "orthogonal", + string bias_initializer = "zeros", + bool unit_forget_bias = true, + float dropout = 0f, + float recurrent_dropout = 0f, + int implementation = 2); + public ILayer LSTM(int units, Activation activation = null, Activation recurrent_activation = null, @@ -192,6 +228,19 @@ public ILayer Rescaling(float scale, float offset = 0, Shape input_shape = null); + public IRnnCell SimpleRNNCell( + int units, + string activation = "tanh", + bool use_bias = true, + string kernel_initializer = "glorot_uniform", + string recurrent_initializer = "orthogonal", + string bias_initializer = "zeros", + float dropout = 0f, + float recurrent_dropout = 0f); + + public IRnnCell StackedRNNCells( + IEnumerable cells); + public ILayer SimpleRNN(int units, string activation = "tanh", string kernel_initializer = "glorot_uniform", @@ -200,6 +249,69 @@ public ILayer SimpleRNN(int units, bool return_sequences = false, bool return_state = false); + public ILayer RNN( + IRnnCell cell, + bool return_sequences = false, + bool return_state = false, + bool go_backwards = false, + bool stateful = false, + bool unroll = false, + bool time_major = false + ); + + public ILayer RNN( + IEnumerable cell, + bool return_sequences = false, + bool return_state = false, + bool go_backwards = false, + bool stateful = false, + bool unroll = false, + bool time_major = false + ); + + public IRnnCell GRUCell( + int units, + string activation = "tanh", + string recurrent_activation = "sigmoid", + bool use_bias = true, + string kernel_initializer = "glorot_uniform", + string recurrent_initializer = "orthogonal", + string bias_initializer = "zeros", + float dropout = 0f, + float recurrent_dropout = 0f, + bool reset_after = true); + + public ILayer GRU( + int units, + string activation = "tanh", + string recurrent_activation = "sigmoid", + bool use_bias = true, + string kernel_initializer = "glorot_uniform", + string recurrent_initializer = "orthogonal", + string bias_initializer = "zeros", + float dropout = 0f, + float recurrent_dropout = 0f, + bool return_sequences = false, + bool return_state = false, + bool go_backwards = false, + bool stateful = false, + bool unroll = false, + bool time_major = false, + bool reset_after = true + ); + + /// + /// Bidirectional wrapper for RNNs. + /// + /// `keras.layers.RNN` instance, such as `keras.layers.LSTM` or `keras.layers.GRU` + /// automatically. + /// + public ILayer Bidirectional( + ILayer layer, + string merge_mode = "concat", + NDArray weights = null, + ILayer backward_layer = null); + public ILayer Subtract(); } } diff --git a/src/TensorFlowNET.Core/Keras/Layers/Rnn/IRnnCell.cs b/src/TensorFlowNET.Core/Keras/Layers/Rnn/IRnnCell.cs new file mode 100644 index 000000000..43df75b17 --- /dev/null +++ b/src/TensorFlowNET.Core/Keras/Layers/Rnn/IRnnCell.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Tensorflow.Common.Types; + +namespace Tensorflow.Keras.Layers +{ + public interface IRnnCell: ILayer + { + /// + /// If the derived class tends to not implement it, please return null. + /// + INestStructure? StateSize { get; } + /// + /// If the derived class tends to not implement it, please return null. + /// + INestStructure? OutputSize { get; } + /// + /// Whether the optional RNN args are supported when appying the layer. + /// In other words, whether `Apply` is overwrited with process of `RnnOptionalArgs`. + /// + bool SupportOptionalArgs { get; } + Tensors GetInitialState(Tensors inputs, Tensor batch_size, TF_DataType dtype); + } +} diff --git a/src/TensorFlowNET.Core/Keras/Layers/Rnn/IStackedRnnCells.cs b/src/TensorFlowNET.Core/Keras/Layers/Rnn/IStackedRnnCells.cs new file mode 100644 index 000000000..8cf6150d3 --- /dev/null +++ b/src/TensorFlowNET.Core/Keras/Layers/Rnn/IStackedRnnCells.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tensorflow.Keras.Layers +{ + public interface IStackedRnnCells : IRnnCell + { + int Count { get; } + IRnnCell this[int idx] { get; } + } +} diff --git a/src/TensorFlowNET.Core/Keras/Regularizers/IRegularizer.cs b/src/TensorFlowNET.Core/Keras/Regularizers/IRegularizer.cs index f4045c7b2..06dbb7c8c 100644 --- a/src/TensorFlowNET.Core/Keras/Regularizers/IRegularizer.cs +++ b/src/TensorFlowNET.Core/Keras/Regularizers/IRegularizer.cs @@ -1,7 +1,25 @@ -namespace Tensorflow.Keras +using Newtonsoft.Json; +using System.Collections.Generic; +using Tensorflow.Keras.Saving.Common; + +namespace Tensorflow.Keras { - public interface IRegularizer - { - Tensor Apply(RegularizerArgs args); - } + [JsonConverter(typeof(CustomizedRegularizerJsonConverter))] + public interface IRegularizer + { + [JsonProperty("class_name")] + string ClassName { get; } + [JsonProperty("config")] + IDictionary Config { get; } + Tensor Apply(RegularizerArgs args); + } + + public interface IRegularizerApi + { + IRegularizer GetRegularizerFromName(string name); + IRegularizer L1 { get; } + IRegularizer L2 { get; } + IRegularizer L1L2 { get; } + } + } diff --git a/src/TensorFlowNET.Core/Keras/Saving/Json/CustomizedKerasShapesWrapperJsonConverter.cs b/src/TensorFlowNET.Core/Keras/Saving/Json/CustomizedKerasShapesWrapperJsonConverter.cs index 1a4245bf2..3a21db9d2 100644 --- a/src/TensorFlowNET.Core/Keras/Saving/Json/CustomizedKerasShapesWrapperJsonConverter.cs +++ b/src/TensorFlowNET.Core/Keras/Saving/Json/CustomizedKerasShapesWrapperJsonConverter.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; using System.Text; +using Tensorflow.Common.Types; namespace Tensorflow.Keras.Saving.Json { diff --git a/src/TensorFlowNET.Core/Keras/Saving/Json/CustomizedRegularizerJsonConverter.cs b/src/TensorFlowNET.Core/Keras/Saving/Json/CustomizedRegularizerJsonConverter.cs new file mode 100644 index 000000000..4b1790aca --- /dev/null +++ b/src/TensorFlowNET.Core/Keras/Saving/Json/CustomizedRegularizerJsonConverter.cs @@ -0,0 +1,57 @@ +using Newtonsoft.Json.Linq; +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Text; +using Tensorflow.Operations.Regularizers; + +namespace Tensorflow.Keras.Saving.Common +{ + class RegularizerInfo + { + public string class_name { get; set; } + public JObject config { get; set; } + } + + public class CustomizedRegularizerJsonConverter : JsonConverter + { + public override bool CanConvert(Type objectType) + { + return objectType == typeof(IRegularizer); + } + + public override bool CanRead => true; + + public override bool CanWrite => true; + + public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer) + { + var regularizer = value as IRegularizer; + if (regularizer is null) + { + JToken.FromObject(null).WriteTo(writer); + return; + } + JToken.FromObject(new RegularizerInfo() + { + class_name = regularizer.ClassName, + config = JObject.FromObject(regularizer.Config) + }, serializer).WriteTo(writer); + } + + public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer) + { + var info = serializer.Deserialize(reader); + if (info is null) + { + return null; + } + return info.class_name switch + { + "L1L2" => new L1L2 (info.config["l1"].ToObject(), info.config["l2"].ToObject()), + "L1" => new L1(info.config["l1"].ToObject()), + "L2" => new L2(info.config["l2"].ToObject()), + }; + } + } +} diff --git a/src/TensorFlowNET.Core/Keras/Saving/KerasShapesWrapper.cs b/src/TensorFlowNET.Core/Keras/Saving/KerasShapesWrapper.cs index d91d3161d..ea6fe976f 100644 --- a/src/TensorFlowNET.Core/Keras/Saving/KerasShapesWrapper.cs +++ b/src/TensorFlowNET.Core/Keras/Saving/KerasShapesWrapper.cs @@ -6,6 +6,7 @@ using System.Diagnostics; using OneOf.Types; using Tensorflow.Keras.Saving.Json; +using Tensorflow.Common.Types; namespace Tensorflow.Keras.Saving { diff --git a/src/TensorFlowNET.Core/NumPy/Axis.cs b/src/TensorFlowNET.Core/NumPy/Axis.cs index 976c764f2..7a3ecbf10 100644 --- a/src/TensorFlowNET.Core/NumPy/Axis.cs +++ b/src/TensorFlowNET.Core/NumPy/Axis.cs @@ -74,8 +74,3 @@ public override string ToString() => IsScalar ? $"{axis[0]}" : $"({string.Join(", ", axis)})"; } } - -namespace System.Runtime.CompilerServices -{ - internal static class IsExternalInit { } -} diff --git a/src/TensorFlowNET.Core/NumPy/Implementation/NumPyImpl.Creation.cs b/src/TensorFlowNET.Core/NumPy/Implementation/NumPyImpl.Creation.cs index f29879b0f..c0f9e695d 100644 --- a/src/TensorFlowNET.Core/NumPy/Implementation/NumPyImpl.Creation.cs +++ b/src/TensorFlowNET.Core/NumPy/Implementation/NumPyImpl.Creation.cs @@ -4,6 +4,8 @@ using System.Linq; using System.Text; using Tensorflow.Util; +using Razorvine.Pickle; +using Tensorflow.NumPy.Pickle; using static Tensorflow.Binding; namespace Tensorflow.NumPy @@ -97,6 +99,14 @@ Array ReadValueMatrix(BinaryReader reader, Array matrix, int bytes, Type type, i return matrix; } + Array ReadObjectMatrix(BinaryReader reader, Array matrix, int[] shape) + { + Stream deflateStream = reader.BaseStream; + BufferedStream bufferedStream = new BufferedStream(deflateStream); + var unpickler = new Unpickler(); + return (MultiArrayPickleWarpper)unpickler.load(bufferedStream); + } + public (NDArray, NDArray) meshgrid(T[] array, bool copy = true, bool sparse = false) { var tensors = array_ops.meshgrid(array, copy: copy, sparse: sparse); diff --git a/src/TensorFlowNET.Core/NumPy/Implementation/NumPyImpl.load.cs b/src/TensorFlowNET.Core/NumPy/Implementation/NumPyImpl.load.cs index 05f53d5e7..199e5ced3 100644 --- a/src/TensorFlowNET.Core/NumPy/Implementation/NumPyImpl.load.cs +++ b/src/TensorFlowNET.Core/NumPy/Implementation/NumPyImpl.load.cs @@ -27,8 +27,14 @@ public Array LoadMatrix(Stream stream) Array matrix = Array.CreateInstance(type, shape); //if (type == typeof(String)) - //return ReadStringMatrix(reader, matrix, bytes, type, shape); - return ReadValueMatrix(reader, matrix, bytes, type, shape); + //return ReadStringMatrix(reader, matrix, bytes, type, shape); + + if (type == typeof(Object)) + return ReadObjectMatrix(reader, matrix, shape); + else + { + return ReadValueMatrix(reader, matrix, bytes, type, shape); + } } } @@ -37,7 +43,7 @@ public T Load(Stream stream) ICloneable, IList, ICollection, IEnumerable, IStructuralComparable, IStructuralEquatable { // if (typeof(T).IsArray && (typeof(T).GetElementType().IsArray || typeof(T).GetElementType() == typeof(string))) - // return LoadJagged(stream) as T; + // return LoadJagged(stream) as T; return LoadMatrix(stream) as T; } @@ -93,7 +99,7 @@ bool ParseReader(BinaryReader reader, out int bytes, out Type t, out int[] shape Type GetType(string dtype, out int bytes, out bool? isLittleEndian) { isLittleEndian = IsLittleEndian(dtype); - bytes = Int32.Parse(dtype.Substring(2)); + bytes = dtype.Length > 2 ? Int32.Parse(dtype.Substring(2)) : 0; string typeCode = dtype.Substring(1); @@ -121,6 +127,8 @@ Type GetType(string dtype, out int bytes, out bool? isLittleEndian) return typeof(Double); if (typeCode.StartsWith("S")) return typeof(String); + if (typeCode.StartsWith("O")) + return typeof(Object); throw new NotSupportedException(); } diff --git a/src/TensorFlowNET.Core/NumPy/Implementation/RandomizedImpl.cs b/src/TensorFlowNET.Core/NumPy/Implementation/RandomizedImpl.cs index 064c7362f..a707e8aae 100644 --- a/src/TensorFlowNET.Core/NumPy/Implementation/RandomizedImpl.cs +++ b/src/TensorFlowNET.Core/NumPy/Implementation/RandomizedImpl.cs @@ -14,9 +14,9 @@ public class RandomizedImpl public NDArray permutation(NDArray x) => new NDArray(random_ops.random_shuffle(x)); [AutoNumPy] - public void shuffle(NDArray x) + public void shuffle(NDArray x, int? seed = null) { - var y = random_ops.random_shuffle(x); + var y = random_ops.random_shuffle(x, seed); Marshal.Copy(y.BufferToArray(), 0, x.TensorDataPointer, (int)x.bytesize); } diff --git a/src/TensorFlowNET.Core/NumPy/NDArray.Implicit.cs b/src/TensorFlowNET.Core/NumPy/NDArray.Implicit.cs index fd4f93fc1..45b236c7b 100644 --- a/src/TensorFlowNET.Core/NumPy/NDArray.Implicit.cs +++ b/src/TensorFlowNET.Core/NumPy/NDArray.Implicit.cs @@ -107,9 +107,15 @@ public unsafe static implicit operator double(NDArray nd) public static implicit operator NDArray(bool value) => new NDArray(value); + public static implicit operator NDArray(byte value) + => new NDArray(value); + public static implicit operator NDArray(int value) => new NDArray(value); + public static implicit operator NDArray(long value) + => new NDArray(value); + public static implicit operator NDArray(float value) => new NDArray(value); diff --git a/src/TensorFlowNET.Core/NumPy/NDArrayConverter.cs b/src/TensorFlowNET.Core/NumPy/NDArrayConverter.cs index c8c2d45fa..4c64eba74 100644 --- a/src/TensorFlowNET.Core/NumPy/NDArrayConverter.cs +++ b/src/TensorFlowNET.Core/NumPy/NDArrayConverter.cs @@ -10,6 +10,7 @@ public class NDArrayConverter public unsafe static T Scalar(NDArray nd) where T : unmanaged => nd.dtype switch { + TF_DataType.TF_BOOL => Scalar(*(bool*)nd.data), TF_DataType.TF_UINT8 => Scalar(*(byte*)nd.data), TF_DataType.TF_FLOAT => Scalar(*(float*)nd.data), TF_DataType.TF_INT32 => Scalar(*(int*)nd.data), diff --git a/src/TensorFlowNET.Core/NumPy/NDArrayRender.cs b/src/TensorFlowNET.Core/NumPy/NDArrayRender.cs index 02cb5926c..230797b8b 100644 --- a/src/TensorFlowNET.Core/NumPy/NDArrayRender.cs +++ b/src/TensorFlowNET.Core/NumPy/NDArrayRender.cs @@ -7,7 +7,7 @@ namespace Tensorflow.NumPy { public class NDArrayRender { - public static string ToString(NDArray array) + public static string ToString(NDArray array, int maxLength = 10) { Shape shape = array.shape; if (shape.IsScalar) @@ -15,12 +15,12 @@ public static string ToString(NDArray array) var s = new StringBuilder(); s.Append("array("); - Build(s, array); + Build(s, array, maxLength); s.Append(")"); return s.ToString(); } - static void Build(StringBuilder s, NDArray array) + static void Build(StringBuilder s, NDArray array, int maxLength) { var shape = array.shape; @@ -35,11 +35,11 @@ static void Build(StringBuilder s, NDArray array) var len = shape[0]; s.Append("["); - if (len <= 10) + if (len <= maxLength) { for (int i = 0; i < len; i++) { - Build(s, array[i]); + Build(s, array[i], maxLength); if (i < len - 1) { s.Append(", "); @@ -49,9 +49,9 @@ static void Build(StringBuilder s, NDArray array) } else { - for (int i = 0; i < 5; i++) + for (int i = 0; i < maxLength / 2; i++) { - Build(s, array[i]); + Build(s, array[i], maxLength); if (i < len - 1) { s.Append(", "); @@ -62,9 +62,9 @@ static void Build(StringBuilder s, NDArray array) s.Append(" ... "); s.AppendLine(); - for (int i = (int)len - 5; i < len; i++) + for (int i = (int)len - maxLength / 2; i < len; i++) { - Build(s, array[i]); + Build(s, array[i], maxLength); if (i < len - 1) { s.Append(", "); diff --git a/src/TensorFlowNET.Core/NumPy/NumPy.Sorting.Searching.Counting.cs b/src/TensorFlowNET.Core/NumPy/NumPy.Sorting.Searching.Counting.cs index 5182d5726..4cad36e0b 100644 --- a/src/TensorFlowNET.Core/NumPy/NumPy.Sorting.Searching.Counting.cs +++ b/src/TensorFlowNET.Core/NumPy/NumPy.Sorting.Searching.Counting.cs @@ -13,6 +13,10 @@ public partial class np public static NDArray argmax(NDArray a, Axis? axis = null) => new NDArray(math_ops.argmax(a, axis ?? 0)); + [AutoNumPy] + public static NDArray argmin(NDArray a, Axis? axis = null) + => new NDArray(math_ops.argmin(a, axis ?? 0)); + [AutoNumPy] public static NDArray argsort(NDArray a, Axis? axis = null) => new NDArray(sort_ops.argsort(a, axis: axis ?? -1)); diff --git a/src/TensorFlowNET.Core/NumPy/NumPy.Statistics.cs b/src/TensorFlowNET.Core/NumPy/NumPy.Statistics.cs index 5d86b1b39..bce16ec9f 100644 --- a/src/TensorFlowNET.Core/NumPy/NumPy.Statistics.cs +++ b/src/TensorFlowNET.Core/NumPy/NumPy.Statistics.cs @@ -10,10 +10,10 @@ namespace Tensorflow.NumPy public partial class np { [AutoNumPy] - public static NDArray amin(NDArray x, int axis = 0) => new NDArray(tf.arg_min(x, axis)); + public static NDArray amin(NDArray x, int axis = 0) => new NDArray(tf.min(x, axis)); [AutoNumPy] - public static NDArray amax(NDArray x, int axis = 0) => new NDArray(tf.math.argmax(x, axis)); + public static NDArray amax(NDArray x, int axis = 0) => new NDArray(tf.max(x, axis)); [AutoNumPy] public static NDArray average(NDArray a, int axis = -1, NDArray? weights = null, bool returned = false) diff --git a/src/TensorFlowNET.Core/NumPy/Numpy.Manipulation.cs b/src/TensorFlowNET.Core/NumPy/Numpy.Manipulation.cs index 940856056..5e2574170 100644 --- a/src/TensorFlowNET.Core/NumPy/Numpy.Manipulation.cs +++ b/src/TensorFlowNET.Core/NumPy/Numpy.Manipulation.cs @@ -30,6 +30,15 @@ public static NDArray concatenate((NDArray, NDArray) tuple, int axis = 0) [AutoNumPy] public static NDArray stack(params NDArray[] arrays) => new NDArray(array_ops.stack(arrays)); + [AutoNumPy] + public static NDArray stack(NDArray[] arrays, int axis = 0) => new NDArray(array_ops.stack(arrays, axis)); + + [AutoNumPy] + public static NDArray stack((NDArray, NDArray) tuple, int axis = 0) => new NDArray(array_ops.stack(new[] { tuple.Item1, tuple.Item2 }, axis)); + + [AutoNumPy] + public static NDArray stack((NDArray, NDArray, NDArray) tuple, int axis = 0) => new NDArray(array_ops.stack(new[] { tuple.Item1, tuple.Item2, tuple.Item3 }, axis)); + [AutoNumPy] public static NDArray moveaxis(NDArray array, Axis source, Axis destination) => new NDArray(array_ops.moveaxis(array, source, destination)); } diff --git a/src/TensorFlowNET.Core/NumPy/Numpy.Math.cs b/src/TensorFlowNET.Core/NumPy/Numpy.Math.cs index 0e50cd564..2559638b3 100644 --- a/src/TensorFlowNET.Core/NumPy/Numpy.Math.cs +++ b/src/TensorFlowNET.Core/NumPy/Numpy.Math.cs @@ -28,7 +28,16 @@ public partial class np public static NDArray multiply(NDArray x1, NDArray x2) => new NDArray(tf.multiply(x1, x2)); [AutoNumPy] - public static NDArray maximum(NDArray x1, NDArray x2) => new NDArray(tf.maximum(x1, x2)); + //public static NDArray maximum(NDArray x1, NDArray x2) => new NDArray(tf.maximum(x1, x2)); + public static NDArray maximum(NDArray x1, NDArray x2, int? axis = null) + { + var maxValues = tf.maximum(x1, x2); + if (axis.HasValue) + { + maxValues = tf.reduce_max(maxValues, axis: axis.Value); + } + return new NDArray(maxValues); + } [AutoNumPy] public static NDArray minimum(NDArray x1, NDArray x2) => new NDArray(tf.minimum(x1, x2)); @@ -40,9 +49,30 @@ public static NDArray prod(NDArray array, Axis? axis = null, Type? dtype = null, [AutoNumPy] public static NDArray prod(params T[] array) where T : unmanaged => new NDArray(tf.reduce_prod(new NDArray(array))); + [AutoNumPy] + public static NDArray dot(NDArray x1, NDArray x2, NDArray? axes = null, string? name = null) + { + //if axes mentioned + if (axes != null) + { + return new NDArray(tf.dot_prod(x1, x2, axes, name)); + } + if (x1.shape.ndim > 1) + { + x1 = GetFlattenArray(x1); + } + if (x2.shape.ndim > 1) + { + x2 = GetFlattenArray(x2); + } + //if axes not mentioned, default 0,0 + return new NDArray(tf.dot_prod(x1, x2, axes: new int[] { 0, 0 }, name)); + } [AutoNumPy] public static NDArray power(NDArray x, NDArray y) => new NDArray(tf.pow(x, y)); + [AutoNumPy] + public static NDArray square(NDArray x) => new NDArray(tf.square(x)); [AutoNumPy] public static NDArray sin(NDArray x) => new NDArray(math_ops.sin(x)); @@ -55,5 +85,11 @@ public static NDArray prod(params T[] array) where T : unmanaged [AutoNumPy] public static NDArray add(NDArray x, NDArray y) => new NDArray(math_ops.add(x, y)); + + [AutoNumPy] + public static NDArray greater(NDArray x, NDArray y) => new NDArray(tf.greater(x, y)); + + [AutoNumPy] + public static NDArray less(NDArray x, NDArray y) => new NDArray(tf.less(x, y)); } } diff --git a/src/TensorFlowNET.Core/NumPy/Pickle/DTypePickleWarpper.cs b/src/TensorFlowNET.Core/NumPy/Pickle/DTypePickleWarpper.cs new file mode 100644 index 000000000..5dff6c16b --- /dev/null +++ b/src/TensorFlowNET.Core/NumPy/Pickle/DTypePickleWarpper.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tensorflow.NumPy.Pickle +{ + public class DTypePickleWarpper + { + TF_DataType dtype { get; set; } + public DTypePickleWarpper(TF_DataType dtype) + { + this.dtype = dtype; + } + public void __setstate__(object[] args) { } + public static implicit operator TF_DataType(DTypePickleWarpper dTypeWarpper) + { + return dTypeWarpper.dtype; + } + } +} diff --git a/src/TensorFlowNET.Core/NumPy/Pickle/DtypeConstructor.cs b/src/TensorFlowNET.Core/NumPy/Pickle/DtypeConstructor.cs new file mode 100644 index 000000000..160c7d4e9 --- /dev/null +++ b/src/TensorFlowNET.Core/NumPy/Pickle/DtypeConstructor.cs @@ -0,0 +1,52 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Text; +using Razorvine.Pickle; + +namespace Tensorflow.NumPy.Pickle +{ + /// + /// + /// + [SuppressMessage("ReSharper", "InconsistentNaming")] + [SuppressMessage("ReSharper", "MemberCanBePrivate.Global")] + [SuppressMessage("ReSharper", "MemberCanBeMadeStatic.Global")] + class DtypeConstructor : IObjectConstructor + { + public object construct(object[] args) + { + var typeCode = (string)args[0]; + TF_DataType dtype; + if (typeCode == "b1") + dtype = np.@bool; + else if (typeCode == "i1") + dtype = np.@byte; + else if (typeCode == "i2") + dtype = np.int16; + else if (typeCode == "i4") + dtype = np.int32; + else if (typeCode == "i8") + dtype = np.int64; + else if (typeCode == "u1") + dtype = np.ubyte; + else if (typeCode == "u2") + dtype = np.uint16; + else if (typeCode == "u4") + dtype = np.uint32; + else if (typeCode == "u8") + dtype = np.uint64; + else if (typeCode == "f4") + dtype = np.float32; + else if (typeCode == "f8") + dtype = np.float64; + else if (typeCode.StartsWith("S")) + dtype = np.@string; + else if (typeCode.StartsWith("O")) + dtype = np.@object; + else + throw new NotSupportedException(); + return new DTypePickleWarpper(dtype); + } + } +} diff --git a/src/TensorFlowNET.Core/NumPy/Pickle/MultiArrayConstructor.cs b/src/TensorFlowNET.Core/NumPy/Pickle/MultiArrayConstructor.cs new file mode 100644 index 000000000..885f368c4 --- /dev/null +++ b/src/TensorFlowNET.Core/NumPy/Pickle/MultiArrayConstructor.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Text; +using Razorvine.Pickle; +using Razorvine.Pickle.Objects; + +namespace Tensorflow.NumPy.Pickle +{ + /// + /// Creates multiarrays of objects. Returns a primitive type multiarray such as int[][] if + /// the objects are ints, etc. + /// + [SuppressMessage("ReSharper", "InconsistentNaming")] + [SuppressMessage("ReSharper", "MemberCanBePrivate.Global")] + [SuppressMessage("ReSharper", "MemberCanBeMadeStatic.Global")] + public class MultiArrayConstructor : IObjectConstructor + { + public object construct(object[] args) + { + if (args.Length != 3) + throw new InvalidArgumentError($"Invalid number of arguments in MultiArrayConstructor._reconstruct. Expected three arguments. Given {args.Length} arguments."); + + var types = (ClassDictConstructor)args[0]; + if (types.module != "numpy" || types.name != "ndarray") + throw new RuntimeError("_reconstruct: First argument must be a sub-type of ndarray"); + + var arg1 = (object[])args[1]; + var dims = new int[arg1.Length]; + for (var i = 0; i < arg1.Length; i++) + { + dims[i] = (int)arg1[i]; + } + var shape = new Shape(dims); + + TF_DataType dtype; + string identifier; + if (args[2].GetType() == typeof(string)) + identifier = (string)args[2]; + else + identifier = Encoding.UTF8.GetString((byte[])args[2]); + switch (identifier) + { + case "u": dtype = np.uint32; break; + case "c": dtype = np.complex_; break; + case "f": dtype = np.float32; break; + case "b": dtype = np.@bool; break; + default: throw new NotImplementedException($"Unsupported data type: {args[2]}"); + } + return new MultiArrayPickleWarpper(shape, dtype); + } + } +} diff --git a/src/TensorFlowNET.Core/NumPy/Pickle/MultiArrayPickleWarpper.cs b/src/TensorFlowNET.Core/NumPy/Pickle/MultiArrayPickleWarpper.cs new file mode 100644 index 000000000..af8d1ecc2 --- /dev/null +++ b/src/TensorFlowNET.Core/NumPy/Pickle/MultiArrayPickleWarpper.cs @@ -0,0 +1,119 @@ +using Newtonsoft.Json.Linq; +using Serilog.Debugging; +using System; +using System.Collections; +using System.Collections.Generic; +using System.Text; + +namespace Tensorflow.NumPy.Pickle +{ + public class MultiArrayPickleWarpper + { + public Shape reconstructedShape { get; set; } + public TF_DataType reconstructedDType { get; set; } + public NDArray reconstructedNDArray { get; set; } + public Array reconstructedMultiArray { get; set; } + public MultiArrayPickleWarpper(Shape shape, TF_DataType dtype) + { + reconstructedShape = shape; + reconstructedDType = dtype; + } + public void __setstate__(object[] args) + { + if (args.Length != 5) + throw new InvalidArgumentError($"Invalid number of arguments in NDArray.__setstate__. Expected five arguments. Given {args.Length} arguments."); + + var version = (int)args[0]; // version + + var arg1 = (object[])args[1]; + var dims = new int[arg1.Length]; + for (var i = 0; i < arg1.Length; i++) + { + dims[i] = (int)arg1[i]; + } + var _ShapeLike = new Shape(dims); // shape + + TF_DataType _DType_co = (DTypePickleWarpper)args[2]; // DType + + var F_continuous = (bool)args[3]; // F-continuous + if (F_continuous) + throw new InvalidArgumentError("Fortran Continuous memory layout is not supported. Please use C-continuous layout or check the data format."); + + var data = args[4]; // Data + /* + * If we ever need another pickle format, increment the version + * number. But we should still be able to handle the old versions. + */ + if (version < 0 || version > 4) + throw new ValueError($"can't handle version {version} of numpy.dtype pickle"); + + // TODO: Implement the missing details and checks from the official Numpy C code here. + // https://github.com/numpy/numpy/blob/2f0bd6e86a77e4401d0384d9a75edf9470c5deb6/numpy/core/src/multiarray/descriptor.c#L2761 + + if (data.GetType() == typeof(ArrayList)) + { + Reconstruct((ArrayList)data); + } + else + throw new NotImplementedException(""); + } + private void Reconstruct(ArrayList arrayList) + { + int ndim = 1; + var subArrayList = arrayList; + while (subArrayList.Count > 0 && subArrayList[0] != null && subArrayList[0].GetType() == typeof(ArrayList)) + { + subArrayList = (ArrayList)subArrayList[0]; + ndim += 1; + } + var type = subArrayList[0].GetType(); + if (type == typeof(int)) + { + if (ndim == 1) + { + int[] list = (int[])arrayList.ToArray(typeof(int)); + Shape shape = new Shape(new int[] { arrayList.Count }); + reconstructedMultiArray = list; + reconstructedNDArray = new NDArray(list, shape); + } + if (ndim == 2) + { + int secondDim = 0; + foreach (ArrayList subArray in arrayList) + { + secondDim = subArray.Count > secondDim ? subArray.Count : secondDim; + } + int[,] list = new int[arrayList.Count, secondDim]; + for (int i = 0; i < arrayList.Count; i++) + { + var subArray = (ArrayList?)arrayList[i]; + if (subArray == null) + throw new NullReferenceException(""); + for (int j = 0; j < subArray.Count; j++) + { + var element = subArray[j]; + if (element == null) + throw new NoNullAllowedException("the element of ArrayList cannot be null."); + list[i, j] = (int)element; + } + } + Shape shape = new Shape(new int[] { arrayList.Count, secondDim }); + reconstructedMultiArray = list; + reconstructedNDArray = new NDArray(list, shape); + } + if (ndim > 2) + throw new NotImplementedException("can't handle ArrayList with more than two dimensions."); + } + else + throw new NotImplementedException(""); + } + public static implicit operator Array(MultiArrayPickleWarpper arrayWarpper) + { + return arrayWarpper.reconstructedMultiArray; + } + public static implicit operator NDArray(MultiArrayPickleWarpper arrayWarpper) + { + return arrayWarpper.reconstructedNDArray; + } + } +} diff --git a/src/TensorFlowNET.Core/Numpy/Numpy.cs b/src/TensorFlowNET.Core/Numpy/Numpy.cs index 72d2e981c..fee2d63fc 100644 --- a/src/TensorFlowNET.Core/Numpy/Numpy.cs +++ b/src/TensorFlowNET.Core/Numpy/Numpy.cs @@ -43,7 +43,9 @@ public partial class np public static readonly TF_DataType @decimal = TF_DataType.TF_DOUBLE; public static readonly TF_DataType complex_ = TF_DataType.TF_COMPLEX; public static readonly TF_DataType complex64 = TF_DataType.TF_COMPLEX64; - public static readonly TF_DataType complex128 = TF_DataType.TF_COMPLEX128; + public static readonly TF_DataType complex128 = TF_DataType.TF_COMPLEX128; + public static readonly TF_DataType @string = TF_DataType.TF_STRING; + public static readonly TF_DataType @object = TF_DataType.TF_VARIANT; #endregion public static double nan => double.NaN; diff --git a/src/TensorFlowNET.Core/Numpy/Shape.cs b/src/TensorFlowNET.Core/Numpy/Shape.cs index c339f12de..cbbf66b44 100644 --- a/src/TensorFlowNET.Core/Numpy/Shape.cs +++ b/src/TensorFlowNET.Core/Numpy/Shape.cs @@ -19,13 +19,14 @@ limitations under the License. using System.Collections.Generic; using System.Linq; using System.Text; +using Tensorflow.Common.Types; using Tensorflow.Keras.Saving.Common; using Tensorflow.NumPy; namespace Tensorflow { [JsonConverter(typeof(CustomizedShapeJsonConverter))] - public class Shape + public class Shape : INestStructure { public int ndim => _dims == null ? -1 : _dims.Length; long[] _dims; @@ -41,6 +42,27 @@ public long[] strides } } + public NestType NestType => NestType.List; + + public int ShallowNestedCount => ndim; + /// + /// The total item count of depth 1 of the nested structure. + /// For example, [1, 2, [3, 4, 5]] has TotalNestedCount = 5. + /// + public int TotalNestedCount => ndim; + + public IEnumerable Flatten() => dims.Select(x => x); + + public INestStructure MapStructure(Func func) + { + return new NestList(dims.Select(x => func(x))); + } + + public Nest AsNest() + { + return new NestList(Flatten()).AsNest(); + } + #region https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-8.0/ranges public int Length => ndim; public long[] Slice(int start, int length) diff --git a/src/TensorFlowNET.Core/Operations/Initializers/NpyLoadInitializer.cs b/src/TensorFlowNET.Core/Operations/Initializers/NpyLoadInitializer.cs new file mode 100644 index 000000000..202af652a --- /dev/null +++ b/src/TensorFlowNET.Core/Operations/Initializers/NpyLoadInitializer.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Tensorflow.NumPy; + +namespace Tensorflow.Operations.Initializers +{ + /// + /// An initializer specially used for debugging (to load weights from disk). + /// + class NpyLoadInitializer : IInitializer + { + string _path; + public NpyLoadInitializer(string path) { _path = path; } + public string ClassName => ""; + public IDictionary Config => new Dictionary(); + public Tensor Apply(InitializerArgs args) + { + return np.load(_path); + } + } +} diff --git a/src/TensorFlowNET.Core/Operations/Initializers/Orthogonal.cs b/src/TensorFlowNET.Core/Operations/Initializers/Orthogonal.cs index 492047c9f..ae8733740 100644 --- a/src/TensorFlowNET.Core/Operations/Initializers/Orthogonal.cs +++ b/src/TensorFlowNET.Core/Operations/Initializers/Orthogonal.cs @@ -53,13 +53,12 @@ private Tensor _generate_init_val(Shape shape, TF_DataType dtype) // Compute the qr factorization var (q, r) = tf.linalg.qr(a, full_matrices: false); // Make Q uniform - var d = tf.linalg.tensor_diag_part(r); + var d = tf.linalg.tensor_diag_part(r.Single); q *= tf.sign(d); if (num_rows < num_cols) { - // q = tf.linalg.matrix_transpose(q); - throw new NotImplementedException(""); + q = array_ops.matrix_transpose(q); } return _gain * tf.reshape(q, shape); diff --git a/src/TensorFlowNET.Core/Operations/NnOps/AveragePoolFunction.cs b/src/TensorFlowNET.Core/Operations/NnOps/AveragePoolFunction.cs index d43f8a0c8..84ce56a4b 100644 --- a/src/TensorFlowNET.Core/Operations/NnOps/AveragePoolFunction.cs +++ b/src/TensorFlowNET.Core/Operations/NnOps/AveragePoolFunction.cs @@ -34,7 +34,7 @@ public Tensor Apply(Tensor value, { name = scope; value = ops.convert_to_tensor(value, name: "input"); - return gen_nn_ops.average_pool( + return gen_nn_ops.avg_pool( value, ksize: ksize, strides: strides, diff --git a/src/TensorFlowNET.Core/Operations/NnOps/BasicLSTMCell.cs b/src/TensorFlowNET.Core/Operations/NnOps/BasicLSTMCell.cs index d3592514d..16cbd0010 100644 --- a/src/TensorFlowNET.Core/Operations/NnOps/BasicLSTMCell.cs +++ b/src/TensorFlowNET.Core/Operations/NnOps/BasicLSTMCell.cs @@ -11,6 +11,7 @@ namespace Tensorflow /// Basic LSTM recurrent network cell. /// The implementation is based on: http://arxiv.org/abs/1409.2329. /// + [Obsolete("This is an incompleted tf v1 api, pleas use keras RNNs instead.")] public class BasicLstmCell : LayerRnnCell { int _num_units; @@ -88,7 +89,7 @@ protected Tensors Call(Tensors inputs, Tensor state = null, bool is_training = f gate_inputs = nn_ops.bias_add(gate_inputs, _bias); // i = input_gate, j = new_input, f = forget_gate, o = output_gate - var tensors = array_ops.split(value: gate_inputs, num_split: 4, axis: one); + var tensors = array_ops.split(value: gate_inputs, num_or_size_splits: 4, axis: one); var (i, j, f, o) = (tensors[0], tensors[1], tensors[2], tensors[3]); var forget_bias_tensor = constant_op.constant(_forget_bias, dtype: f.dtype); diff --git a/src/TensorFlowNET.Core/Operations/NnOps/BasicRNNCell.cs b/src/TensorFlowNET.Core/Operations/NnOps/BasicRNNCell.cs index 17d51363f..3308aebb7 100644 --- a/src/TensorFlowNET.Core/Operations/NnOps/BasicRNNCell.cs +++ b/src/TensorFlowNET.Core/Operations/NnOps/BasicRNNCell.cs @@ -20,6 +20,7 @@ limitations under the License. namespace Tensorflow { + [Obsolete("This is an incompleted tf v1 api, pleas use keras RNNs instead.")] public class BasicRnnCell : LayerRnnCell { int _num_units; diff --git a/src/TensorFlowNET.Core/Operations/NnOps/ConvolutionInternal.cs b/src/TensorFlowNET.Core/Operations/NnOps/ConvolutionInternal.cs index 958d79f42..ec70b1858 100644 --- a/src/TensorFlowNET.Core/Operations/NnOps/ConvolutionInternal.cs +++ b/src/TensorFlowNET.Core/Operations/NnOps/ConvolutionInternal.cs @@ -67,16 +67,15 @@ public Tensor Apply(Tensors input, Tensor filters) var dilations = _get_sequence(args.DilationRate, num_spatial_dims, channel_index).ToArray(); var strides = _get_sequence(args.Strides, num_spatial_dims, channel_index).ToArray(); - result = gen_nn_ops.conv2d(new Conv2dParams - { - Input = input, - Filter = filters, - Strides = strides, - Padding = padding, - DataFormat = data_format, - Dilations = dilations, - Name = name - }); + result = gen_nn_ops.conv2d( + input, + filters, + strides, + padding, + data_format: data_format, + dilations: dilations, + name: name + ); } else { @@ -93,16 +92,15 @@ public Tensor Apply(Tensors input, Tensor filters) input = array_ops.expand_dims(input, spatial_start_dim); filters = array_ops.expand_dims(filters, 0); - result = gen_nn_ops.conv2d(new Conv2dParams - { - Input = input, - Filter = filters, - Strides = strides.ToArray(), - Padding = padding, - DataFormat = channel_first ? "NCHW" : "NHWC", - Dilations = dilations.ToArray(), - Name = name - }); + result = gen_nn_ops.conv2d( + input, + filters, + strides.ToArray(), + padding, + data_format: channel_first ? "NCHW" : "NHWC", + dilations: dilations.ToArray(), + name: name + ); result = array_ops.squeeze(result, new[] { spatial_start_dim }); } }); diff --git a/src/TensorFlowNET.Core/Operations/NnOps/LayerRNNCell.cs b/src/TensorFlowNET.Core/Operations/NnOps/LayerRNNCell.cs index 7394cb7f9..65de4fe90 100644 --- a/src/TensorFlowNET.Core/Operations/NnOps/LayerRNNCell.cs +++ b/src/TensorFlowNET.Core/Operations/NnOps/LayerRNNCell.cs @@ -19,6 +19,7 @@ limitations under the License. namespace Tensorflow { + [Obsolete("This is an incompleted tf v1 api, pleas use keras RNNs instead.")] public class LayerRnnCell : RnnCell { protected InputSpec inputSpec; diff --git a/src/TensorFlowNET.Core/Operations/NnOps/RNNCell.cs b/src/TensorFlowNET.Core/Operations/NnOps/RNNCell.cs index ecc9ca116..9905d39c8 100644 --- a/src/TensorFlowNET.Core/Operations/NnOps/RNNCell.cs +++ b/src/TensorFlowNET.Core/Operations/NnOps/RNNCell.cs @@ -16,10 +16,11 @@ limitations under the License. using System; using System.Collections.Generic; +using Tensorflow.Common.Types; using Tensorflow.Keras; using Tensorflow.Keras.ArgsDefinition; -using Tensorflow.Keras.ArgsDefinition.Rnn; using Tensorflow.Keras.Engine; +using Tensorflow.Keras.Layers; using Tensorflow.Keras.Saving; using Tensorflow.NumPy; using Tensorflow.Operations; @@ -50,7 +51,8 @@ namespace Tensorflow /// matching structure of Tensors having shape `[batch_size].concatenate(s)` /// for each `s` in `self.batch_size`. /// - public abstract class RnnCell : ILayer, RNNArgs.IRnnArgCell + [Obsolete("This is an incompleted tf v1 api, pleas use keras RNNs instead.")] + public abstract class RnnCell : ILayer, IRnnCell { /// /// Attribute that indicates whether the cell is a TF RNN cell, due the slight @@ -142,7 +144,7 @@ private Tensor _zero_state_tensors(object state_size, Tensor batch_size, TF_Data throw new NotImplementedException("_zero_state_tensors"); } - public Tensors Apply(Tensors inputs, Tensor state = null, bool is_training = false) + public Tensors Apply(Tensors inputs, Tensors state = null, bool? is_training = false, IOptionalArgs? optional_args = null) { throw new NotImplementedException(); } @@ -173,5 +175,18 @@ public void adapt(Tensor data, int? batch_size = null, int? steps = null) { throw new NotImplementedException(); } + + public (Tensor, Tensors) Call(Tensors inputs, Tensors states, bool? training = null) + { + throw new NotImplementedException(); + } + public Tensors GetInitialState(Tensors inputs = null, Tensor batch_size = null, TF_DataType dtype = TF_DataType.DtInvalid) + { + throw new NotImplementedException(); + } + public INestStructure StateSize => throw new NotImplementedException(); + public INestStructure OutputSize => throw new NotImplementedException(); + public bool IsTFRnnCell => throw new NotImplementedException(); + public bool SupportOptionalArgs => throw new NotImplementedException(); } } diff --git a/src/TensorFlowNET.Core/Operations/NnOps/gen_nn_ops.cs b/src/TensorFlowNET.Core/Operations/NnOps/gen_nn_ops.cs deleted file mode 100644 index 408d06ebf..000000000 --- a/src/TensorFlowNET.Core/Operations/NnOps/gen_nn_ops.cs +++ /dev/null @@ -1,373 +0,0 @@ -/***************************************************************************** - Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -******************************************************************************/ - -using System.Linq; -using static Tensorflow.Binding; - -namespace Tensorflow.Operations -{ - public class gen_nn_ops - { - /// - /// Computes a 2-D convolution given 4-D `input` and `filter` tensors. - /// - /// Given an input tensor of shape `[batch, in_height, in_width, in_channels]` - /// and a filter / kernel tensor of shape - /// `[filter_height, filter_width, in_channels, out_channels]`, this op - /// performs the following: - /// - /// 1. Flattens the filter to a 2-D matrix with shape - /// `[filter_height * filter_width * in_channels, output_channels]`. - /// 2. Extracts image patches from the input tensor to form a *virtual* - /// tensor of shape `[batch, out_height, out_width, - /// filter_height * filter_width * in_channels]`. - /// 3. For each patch, right-multiplies the filter matrix and the image patch - /// vector. - /// - /// - /// - public static Tensor conv2d(Conv2dParams parameters) - => tf.Context.ExecuteOp("Conv2D", parameters.Name, new ExecuteOpArgs(parameters.Input, parameters.Filter) - .SetAttributes(new - { - strides = parameters.Strides, - padding = parameters.Padding, - use_cudnn_on_gpu = parameters.UseCudnnOnGpu, - explicit_paddings = parameters.ExplicitPaddings, - data_format = parameters.DataFormat, - dilations = parameters.Dilations - })); - - /// - /// Computes the gradients of convolution with respect to the filter. - /// - /// - /// - public static Tensor conv2d_backprop_filter(Tensor input, Tensor filter_sizes, Tensor out_backprop, - int[] strides, string padding, bool use_cudnn_on_gpu = true, - int[] explicit_paddings = null, - string data_format = "NHWC", - int[] dilations = null, - string name = null) - => tf.Context.ExecuteOp("Conv2DBackpropFilter", name, new ExecuteOpArgs(input, filter_sizes, out_backprop) - .SetAttributes(new - { - strides, - padding, - use_cudnn_on_gpu, - explicit_paddings = explicit_paddings ?? new int[0], - data_format, - dilations = dilations ?? new int[] { 1, 1, 1, 1 } - })); - - /// - /// Computes the gradients of convolution with respect to the input. - /// - /// - /// - public static Tensor conv2d_backprop_input(Tensor input_sizes, Tensor filter, Tensor out_backprop, - int[] strides, string padding, bool use_cudnn_on_gpu = true, - int[] explicit_paddings = null, - string data_format = "NHWC", - int[] dilations = null, - string name = null) - => tf.Context.ExecuteOp("Conv2DBackpropInput", name, new ExecuteOpArgs(input_sizes, filter, out_backprop) - .SetAttributes(new - { - strides, - padding, - use_cudnn_on_gpu, - explicit_paddings = explicit_paddings ?? new int[0], - data_format, - dilations = dilations ?? new int[] { 1, 1, 1, 1 } - })); - - public static Tensor bias_add(Tensor value, - IVariableV1 bias, - string data_format = null, - string name = null) - => tf.Context.ExecuteOp("BiasAdd", name, new ExecuteOpArgs(value, bias) - .SetAttributes(new { data_format = data_format ?? "NHWC" })); - - public static Tensor bias_add_grad(Tensor out_backprop, - string data_format = "NHWC", - string name = null) - => tf.Context.ExecuteOp("BiasAddGrad", name, new ExecuteOpArgs(out_backprop) - .SetAttributes(new { data_format = data_format ?? "NHWC" })); - - /// - /// Computes exponential linear: exp(features) - 1 if &lt; 0, features otherwise. - /// - /// - /// - /// - /// If specified, the created operation in the graph will be this one, otherwise it will be named 'Elu'. - /// - /// - /// The Operation can be fetched from the resulting Tensor, by fetching the Operation property from the result. - /// - /// - /// See [Fast and Accurate Deep Network Learning by Exponential Linear Units (ELUs) - /// ](http://arxiv.org/abs/1511.07289) - /// - public static Tensor elu(Tensor features, string name = "Elu") - { - var op = tf.OpDefLib._apply_op_helper("Elu", name: name, args: new { features }); - return op.output; - } - - /// - /// Gradient for batch normalization. - /// - /// - /// - public static Tensor[] fused_batch_norm_grad(FusedBatchNormParams @params) - { - var op = tf.OpDefLib._apply_op_helper("FusedBatchNormGrad", name: @params.Name, args: new - { - y_backprop = @params.YBackprop, - x = @params.X, - scale = @params.Scale, - reserve_space_1 = @params.ReserveSpace1, - reserve_space_2 = @params.ReserveSpace2, - epsilon = @params.Epsilon, - data_format = @params.DataFormat, - is_training = @params.IsTraining - }); - return op.outputs; - } - - public static Tensor[] fused_batch_norm_grad_v3(FusedBatchNormParams @params) - => tf.Context.ExecuteOp("FusedBatchNormGradV3", @params.Name, - new ExecuteOpArgs(@params.YBackprop, - @params.X, - @params.Scale, - @params.ReserveSpace1, - @params.ReserveSpace2, - @params.ReserveSpace3) - .SetAttributes(new - { - epsilon = @params.Epsilon, - data_format = @params.DataFormat, - is_training = @params.IsTraining - })); - - public static Tensor[] fused_batch_norm(Tensor x, - Tensor scale, - Tensor offset, - Tensor mean, - Tensor variance, - float epsilon = 0.0001f, - string data_format = "NHWC", - bool is_training = true, - string name = null) - { - var _op = tf.OpDefLib._apply_op_helper("FusedBatchNorm", name: name, args: new - { - x, - scale, - offset, - mean, - variance, - epsilon, - data_format, - is_training - }); - - return _op.outputs; - } - - public static Tensors fused_batch_norm_v3(Tensor x, - Tensor scale, - Tensor offset, - Tensor mean, - Tensor variance, - float epsilon = 0.0001f, - float exponential_avg_factor = 1.0f, - string data_format = "NHWC", - bool is_training = true, - string name = null) - => tf.Context.ExecuteOp("FusedBatchNormV3", name, new ExecuteOpArgs(x, scale, offset, mean, variance) - .SetAttributes(new { epsilon, data_format, is_training })); - - /// - /// Local Response Normalization. - /// - /// - /// - /// - /// - /// - /// - /// - public static Tensor local_response_normalization(Tensor input, int depth_radius = 5, int bias = 1, - int alpha = 1, float beta = 0.5f, string name = null) - { - var _op = tf.OpDefLib._apply_op_helper("LRN", name: name, args: new - { - input, - depth_radius, - bias, - alpha, - beta - }); - - return _op.output; - } - - public static Tensor log_softmax(Tensor logits, string name = null) - => tf.Context.ExecuteOp("LogSoftmax", name, new ExecuteOpArgs(logits)); - - /// - /// Says whether the targets are in the top `K` predictions. - /// - /// - /// - /// - /// - /// A `Tensor` of type `bool`. - public static Tensor in_top_kv2(Tensor predictions, Tensor targets, int k, string name = null) - => tf.Context.ExecuteOp("InTopKV2", name, - new ExecuteOpArgs(predictions, targets, k)); - - public static Tensor leaky_relu(Tensor features, float alpha = 0.2f, string name = null) - => tf.Context.ExecuteOp("LeakyRelu", name, - new ExecuteOpArgs(features).SetAttributes(new { alpha })); - - public static Tensor average_pool(Tensor input, - int[] ksize, - int[] strides, - string padding, - string data_format = "NHWC", - string name = null) - => tf.Context.ExecuteOp("AvgPool", name, new ExecuteOpArgs(input) - .SetAttributes(new - { - ksize, - strides, - padding, - data_format - })); - - public static Tensor max_pool(Tensor input, - int[] ksize, - int[] strides, - string padding, - string data_format = "NHWC", - string name = null) - => tf.Context.ExecuteOp("MaxPool", name, new ExecuteOpArgs(input) - .SetAttributes(new - { - ksize, - strides, - padding, - data_format - })); - - public static Tensor max_pool_grad(Tensor orig_input, Tensor orig_output, Tensor grad, int[] ksize, int[] strides, string padding, - string data_format = "NHWC", string name = null) - => tf.Context.ExecuteOp("MaxPoolGrad", name, new ExecuteOpArgs(orig_input, orig_output, grad) - .SetAttributes(new - { - ksize, - strides, - padding, - data_format - })); - - public static Tensor[] top_kv2(Tensor input, T k, bool sorted = true, string name = null) - { - var _op = tf.OpDefLib._apply_op_helper("TopKV2", name: name, args: new - { - input, - k, - sorted - }); - - return _op.outputs; - } - - public static Tensor relu_grad(Tensor gradients, Tensor features, string name = null) - => tf.Context.ExecuteOp("ReluGrad", name, new ExecuteOpArgs(gradients, features)); - - public static Tensor leaky_relu_grad(Tensor gradients, Tensor features, float alpha = 0.2f, string name = null) - => tf.Context.ExecuteOp("LeakyReluGrad", name, new ExecuteOpArgs(gradients, features) - .SetAttributes(new { alpha })); - - public static Tensor softmax(Tensor logits, string name = null) - => tf.Context.ExecuteOp("Softmax", name, new ExecuteOpArgs(logits)); - - /// - /// Computes softmax cross entropy cost and gradients to backpropagate. - /// - /// - /// - /// - /// - public static (Tensor, Tensor) softmax_cross_entropy_with_logits(Tensor features, Tensor labels, string name = null) - { - var results = tf.Context.ExecuteOp("SoftmaxCrossEntropyWithLogits", name, new ExecuteOpArgs(features, labels)); - - return (results[0], results[1]); - } - - /// - /// Computes softmax cross entropy cost and gradients to backpropagate. - /// - /// - /// batch_size x num_classes matrix - /// - /// - /// batch_size vector with values in [0, num_classes). - /// This is the label for the given minibatch entry. - /// - /// - /// If specified, the created operation in the graph will be this one, otherwise it will be named 'SparseSoftmaxCrossEntropyWithLogits'. - /// - /// - /// Returns a tuple with multiple values, as follows: - /// loss : Per example loss (batch_size vector). - /// backprop : backpropagated gradients (batch_size x num_classes matrix). - /// The Operation can be fetched from any of the Tensorreturned in the tuple values, by fetching the Operation property. - /// - /// - /// Unlike SoftmaxCrossEntropyWithLogits, this operation does not accept - /// a matrix of label probabilities, but rather a single label per row - /// of features. This label is considered to have probability 1.0 for the - /// given row. - /// - /// Inputs are the logits, not probabilities. - /// - public static (Tensor loss, Tensor backprop) sparse_softmax_cross_entropy_with_logits(Tensor features, Tensor labels, string name = "SparseSoftmaxCrossEntropyWithLogits") - { - var results = tf.Context.ExecuteOp("SparseSoftmaxCrossEntropyWithLogits", name, new ExecuteOpArgs(features, labels)); - - return (results[0], results[1]); - } - - /// - /// Computes rectified linear: `max(features, 0)`. - /// - /// A `Tensor`. Must be one of the following types: `float32`, `float64`, `int32`, `uint8`, `int16`, `int8`, `int64`, `bfloat16`, `uint16`, `half`, `uint32`, `uint64`, `qint8`. - /// A name for the operation (optional). - /// A `Tensor`. Has the same type as `features`. - public static Tensor relu(Tensor features, string name = null) - => tf.Context.ExecuteOp("Relu", name, new ExecuteOpArgs(features)); - - public static Tensor tanh(Tensor x, string name = null) - => tf.Context.ExecuteOp("Tanh", name, new ExecuteOpArgs(x)); - } -} diff --git a/src/TensorFlowNET.Core/Operations/OpDefLibrary.cs b/src/TensorFlowNET.Core/Operations/OpDefLibrary.cs index 3ccf0c190..29e1f074f 100644 --- a/src/TensorFlowNET.Core/Operations/OpDefLibrary.cs +++ b/src/TensorFlowNET.Core/Operations/OpDefLibrary.cs @@ -15,9 +15,11 @@ limitations under the License. ******************************************************************************/ using Google.Protobuf; +using Google.Protobuf.Collections; using System; using System.Collections.Generic; using System.Linq; +using Tensorflow.Functions; using static Tensorflow.Binding; using static Tensorflow.OpDef.Types; @@ -103,6 +105,11 @@ public Operation _apply_op_helper(string op_type_name, string name = null, Dicti DataType dtype = DataType.DtInvalid; DataType default_dtype = DataType.DtInvalid; + if (values is Tensors tensors) + { + values = (Tensor[])tensors; + } + if (_IsListParameter(input_arg)) { if (!_IsListValue(values)) @@ -382,9 +389,13 @@ private AttrValue SetAttrValue(OpDef op_def, AttrDef attr_def, object value) case "list(type)": attr_value.List.Type.AddRange((value as IList).Select(x => _MakeType(x, attr_def))); break; + case "list(float)": + if (value != null) + attr_value.List.F.AddRange((value as IEnumerable).ToArray()); + break; case "list(int)": if (value != null) - attr_value.List.I.AddRange((value as int[]).Select(x => Convert.ToInt64(x))); + attr_value.List.I.AddRange((value as IEnumerable).Select(x => Convert.ToInt64(x))); break; case "bool": attr_value.B = (bool)value; @@ -415,6 +426,15 @@ private AttrValue SetAttrValue(OpDef op_def, AttrDef attr_def, object value) case "list(shape)": attr_value.List.Shape.AddRange((value as Shape[]).Select(x => _MakeShape(x, attr_def))); break; + case "func": + attr_value.Func = _MakeFunc(value, attr_def.Name); + break; + case "list(func)": + attr_value.List.Func.AddRange(_MakeFuncList(value, attr_def.Name)); + break; + case "list(string)": + attr_value.List.S.AddRange((value as IEnumerable).Select(x => ByteString.CopyFromUtf8(x))); + break; default: throw new TypeError($"SetAttrValue: can't not convert attr_def.Type '{attr_def.Type}' to protos."); } @@ -422,6 +442,47 @@ private AttrValue SetAttrValue(OpDef op_def, AttrDef attr_def, object value) return attr_value; } + private NameAttrList _MakeFunc(object func, string arg_name) + { + if(func is NameAttrList attrList) + { + return attrList; + } + NameAttrList fn_attr; + if(func is string funcStr) + { + fn_attr = new NameAttrList() { Name = funcStr }; + } + else if(func is ConcreteFunction concrete) + { + concrete.AddTograph(ops.get_default_graph()); + fn_attr = concrete.AsNameAttrList; + } + else if(func is EagerDefinedFunction eager) + { + eager.AddToGraph(ops.get_default_graph()); + fn_attr = new NameAttrList() { Name = eager.Name }; + } + else + { + throw new TypeError($"Don't know how to convert {func} to a func for argument {arg_name}"); + } + return fn_attr; + } + + private List _MakeFuncList(object funcList, string arg_name) + { + List res = new List(); + if(funcList is IEnumerable enumerable) + { + foreach(var func in enumerable) + { + res.Add(_MakeFunc(func, arg_name)); + } + } + return res; + } + private bool _IsListParameter(ArgDef arg) { if (!String.IsNullOrEmpty(arg.NumberAttr)) diff --git a/src/TensorFlowNET.Core/Operations/Operation.Output.cs b/src/TensorFlowNET.Core/Operations/Operation.Output.cs index 2955a13fa..2329a4786 100644 --- a/src/TensorFlowNET.Core/Operations/Operation.Output.cs +++ b/src/TensorFlowNET.Core/Operations/Operation.Output.cs @@ -34,7 +34,7 @@ public int OutputListLength(string name) return num; } - protected Tensor[] _outputs; + internal Tensor[] _outputs; public virtual Tensor[] outputs => _outputs; public Tensor output => _outputs.FirstOrDefault(); diff --git a/src/TensorFlowNET.Core/Operations/Operation.cs b/src/TensorFlowNET.Core/Operations/Operation.cs index 311f2184f..2105c53fa 100644 --- a/src/TensorFlowNET.Core/Operations/Operation.cs +++ b/src/TensorFlowNET.Core/Operations/Operation.cs @@ -46,9 +46,9 @@ namespace Tensorflow /// public partial class Operation : ITensorOrOperation { - private readonly IntPtr _handle; // _c_op in python + protected IntPtr _handle; // _c_op in python - private readonly Graph _graph; + protected Graph _graph; internal Func _gradient_function; @@ -69,6 +69,7 @@ public partial class Operation : ITensorOrOperation //private OperationDescription _op_desc; public NodeDef node_def => GetNodeDef(); + protected Operation() { } public Operation(IntPtr handle, Graph g = null) { @@ -185,7 +186,42 @@ public void run(FeedItem[] feed_dict = null, Session session = null) } public virtual T get_attr(string name) - => (T)get_attr(name); + { + if (typeof(T).IsValueType) + { + return (T)Convert.ChangeType(get_attr(name), typeof(T)); + } + else + { + return (T)get_attr(name); + } + } + + internal unsafe TF_DataType _get_attr_type(string name) + { + Status status = new(); + TF_DataType result; + c_api.TF_OperationGetAttrType(_handle, name, new IntPtr(&result), status); + status.Check(true); + return result; + } + + internal unsafe long _get_attr_int(string name) + { + long result; + c_api.TF_OperationGetAttrInt(_handle, name, new IntPtr(&result), tf.Status); + tf.Status.Check(true); + return result; + } + + internal unsafe bool _get_attr_bool(string name) + { + Status status = new(); + bool result; + c_api.TF_OperationGetAttrBool(_handle, name, new IntPtr(&result), status); + status.Check(true); + return result; + } public virtual T[] get_attr_list(string name) { @@ -229,7 +265,42 @@ public virtual object get_attr(string name) if(oneof_value == AttrValue.ValueOneofCase.List) { - throw new NotImplementedException($"Unsupported field type in {oneof_value}"); + if (x.List.S is not null && x.List.S.Count > 0) + { + return x.List.S.Select(x => x.ToStringUtf8()).ToArray(); + } + else if (x.List.I is not null && x.List.I.Count > 0) + { + return x.List.I.ToArray(); + } + else if (x.List.F is not null && x.List.F.Count > 0) + { + return x.List.F.ToArray(); + } + else if (x.List.B is not null && x.List.B.Count > 0) + { + return x.List.B.ToArray(); + } + else if (x.List.Shape is not null && x.List.Shape.Count > 0) + { + return x.List.Shape.ToArray(); + } + else if (x.List.Tensor is not null && x.List.Tensor.Count > 0) + { + return x.List.Tensor.ToArray(); + } + else if (x.List.Func is not null && x.List.Func.Count > 0) + { + return x.List.Func.ToArray(); + } + else if (x.List.Type is not null && x.List.Type.Count > 0) + { + return x.List.Type.Select(x => x.as_tf_dtype()).ToArray(); + } + else + { + return null; + } } if(oneof_value == AttrValue.ValueOneofCase.Type) { @@ -366,7 +437,7 @@ internal void _set_attr(string attr_name, AttrValue attr_value) internal void _set_attr_with_buf(string attr_name, Buffer attr_buf) { Status status = new(); - c_api.TFC_SetAttr(graph, _handle, attr_name, attr_buf, status); + c_api.TF_SetAttr(graph, _handle, attr_name, attr_buf, status); status.Check(true); } } diff --git a/src/TensorFlowNET.Core/Operations/Regularizers/L1.cs b/src/TensorFlowNET.Core/Operations/Regularizers/L1.cs new file mode 100644 index 000000000..9e0619454 --- /dev/null +++ b/src/TensorFlowNET.Core/Operations/Regularizers/L1.cs @@ -0,0 +1,33 @@ +using System; + +using Tensorflow.Keras; + +namespace Tensorflow.Operations.Regularizers +{ + public class L1 : IRegularizer + { + float _l1; + private readonly Dictionary _config; + + public string ClassName => "L1"; + public virtual IDictionary Config => _config; + + public L1(float l1 = 0.01f) + { + // l1 = 0.01 if l1 is None else l1 + // validate_float_arg(l1, name = "l1") + // self.l1 = ops.convert_to_tensor(l1) + this._l1 = l1; + + _config = new(); + _config["l1"] = _l1; + } + + + public Tensor Apply(RegularizerArgs args) + { + //return self.l1 * ops.sum(ops.absolute(x)) + return _l1 * math_ops.reduce_sum(math_ops.abs(args.X)); + } + } +} diff --git a/src/TensorFlowNET.Core/Operations/Regularizers/L1L2.cs b/src/TensorFlowNET.Core/Operations/Regularizers/L1L2.cs new file mode 100644 index 000000000..e3af00eb5 --- /dev/null +++ b/src/TensorFlowNET.Core/Operations/Regularizers/L1L2.cs @@ -0,0 +1,48 @@ +using System; + +using Tensorflow.Keras; + +namespace Tensorflow.Operations.Regularizers +{ + public class L1L2 : IRegularizer + { + float _l1; + float _l2; + private readonly Dictionary _config; + + public string ClassName => "L1L2"; + public virtual IDictionary Config => _config; + + public L1L2(float l1 = 0.0f, float l2 = 0.0f) + { + //l1 = 0.0 if l1 is None else l1 + //l2 = 0.0 if l2 is None else l2 + // validate_float_arg(l1, name = "l1") + // validate_float_arg(l2, name = "l2") + + // self.l1 = l1 + // self.l2 = l2 + this._l1 = l1; + this._l2 = l2; + + _config = new(); + _config["l1"] = l1; + _config["l2"] = l2; + } + + public Tensor Apply(RegularizerArgs args) + { + //regularization = ops.convert_to_tensor(0.0, dtype = x.dtype) + //if self.l1: + // regularization += self.l1 * ops.sum(ops.absolute(x)) + //if self.l2: + // regularization += self.l2 * ops.sum(ops.square(x)) + //return regularization + + Tensor regularization = tf.constant(0.0, args.X.dtype); + regularization += _l1 * math_ops.reduce_sum(math_ops.abs(args.X)); + regularization += _l2 * math_ops.reduce_sum(math_ops.square(args.X)); + return regularization; + } + } +} diff --git a/src/TensorFlowNET.Core/Operations/Regularizers/L2.cs b/src/TensorFlowNET.Core/Operations/Regularizers/L2.cs new file mode 100644 index 000000000..6c0e950a9 --- /dev/null +++ b/src/TensorFlowNET.Core/Operations/Regularizers/L2.cs @@ -0,0 +1,33 @@ +using System; + +using Tensorflow.Keras; + +namespace Tensorflow.Operations.Regularizers +{ + public class L2 : IRegularizer + { + float _l2; + private readonly Dictionary _config; + + public string ClassName => "L2"; + public virtual IDictionary Config => _config; + + public L2(float l2 = 0.01f) + { + // l2 = 0.01 if l2 is None else l2 + // validate_float_arg(l2, name = "l2") + // self.l2 = l2 + this._l2 = l2; + + _config = new(); + _config["l2"] = _l2; + } + + + public Tensor Apply(RegularizerArgs args) + { + //return self.l2 * ops.sum(ops.square(x)) + return _l2 * math_ops.reduce_sum(math_ops.square(args.X)); + } + } +} diff --git a/src/TensorFlowNET.Core/Operations/_EagerTensorArray.cs b/src/TensorFlowNET.Core/Operations/_EagerTensorArray.cs index cf1b50af6..591760600 100644 --- a/src/TensorFlowNET.Core/Operations/_EagerTensorArray.cs +++ b/src/TensorFlowNET.Core/Operations/_EagerTensorArray.cs @@ -17,6 +17,8 @@ limitations under the License. using System; using System.Collections.Generic; using System.Linq; +using Tensorflow.Common.Types; +using Tensorflow.Eager; using Tensorflow.Framework; using static Tensorflow.Binding; @@ -37,10 +39,6 @@ public class _EagerTensorArray : TensorArray bool _infer_shape; public override bool infer_shape => _infer_shape; - public bool _dynamic_size; - public Shape _element_shape; - - public List _colocate_with; Tensor _handle; public override Tensor handle => _handle; @@ -48,12 +46,14 @@ public class _EagerTensorArray : TensorArray public override Tensor flow => _flow; bool _clear_after_read; List _tensor_array; + List _previous_read_indices; public _EagerTensorArray(TF_DataType dtype, Tensor size, bool dynamic_size = false, bool clear_after_read = true, string tensor_array_name = null, Tensor handle = null, Tensor flow = null, bool infer_shape = true, Shape? element_shape = null, bool colocate_with_first_write_call = true, string name = null) { + _size = size; _flow = constant_op.constant(0); _infer_shape = infer_shape; _element_shape = element_shape ?? Shape.Null; @@ -61,16 +61,20 @@ public _EagerTensorArray(TF_DataType dtype, Tensor size, bool dynamic_size = fal _dtype = dtype.as_base_dtype(); _dynamic_size = dynamic_size; _clear_after_read = clear_after_read; - _tensor_array = new List(); + _tensor_array = Enumerable.Repeat(null, size.numpy()).ToList(); + _previous_read_indices = new(); } public override TensorArray unstack(Tensor value, string name = null) { - return tf_with(ops.name_scope(name, "TensorArrayUnstack", new { _handle, value }), delegate + var tensors = array_ops.unstack(value, name: name); + if(tensors.Length > _tensor_array.Count && !_dynamic_size) { - var num_elements = array_ops.shape(value)[0]; - return scatter(indices: math_ops.range(0, num_elements), value: value, name: name); - }); + throw new ValueError($"Cannot unstack {tensors.Length} tensors into a TensorArray of static size {_tensor_array.Count}"); + } + _tensor_array = tensors.ToList(); + // TODO(Rinne): revise the implementation. Here we should return `parent()`. + return this; } public TensorArray scatter(Tensor indices, Tensor value, string name = null) @@ -103,7 +107,19 @@ public TensorArray scatter(Tensor indices, Tensor value, string name = null) return ta; });*/ - throw new NotImplementedException(""); + //if (indices is EagerTensor) + //{ + // indices = indices as EagerTensor; + // indices = indices.numpy(); + //} + + //foreach (var (index, val) in zip(indices.ToArray(), array_ops.unstack(value))) + //{ + // this.write(index, val); + //} + //return base; + //throw new NotImplementedException(""); + return this; } public void _merge_element_shape(Shape shape) @@ -116,9 +132,19 @@ public void _maybe_colocate_with(Tensor value) _colocate_with.Add(value); } + private Tensor _maybe_zero(int ix) + { + var val = _tensor_array[ix]; + if(val is null) + { + val = _tensor_array[ix] = array_ops.zeros(_element_shape, _dtype); + } + return val; + } + public override Tensor read(T index, string name = null) { - int index_int = -1; + int index_int; if (index is int int_index) index_int = int_index; else if (index is Tensor tensor_index) @@ -126,27 +152,75 @@ public override Tensor read(T index, string name = null) else throw new ValueError(""); + if(index_int >= _tensor_array.Count) + { + throw new OutOfRangeError($"Tried to read from index {index_int} but array size is: {_tensor_array.Count} "); + } + + var res = _tensor_array[index_int]; + if(res is null) + { + if (_previous_read_indices.Contains(index_int)) + { + throw new InvalidArgumentError($"Could not read index {index_int} twice because it was cleared after " + + $"a previous read (perhaps try setting clear_after_read = false?)"); + } + else + { + res = _maybe_zero(index_int); + } + } + if (_clear_after_read) { _tensor_array[index_int] = null; + _previous_read_indices.Add(index_int); } - - return _tensor_array[index_int]; + return res; } public override TensorArray write(Tensor index, Tensor value, string name = null) { - if (_infer_shape) - _element_shape = _element_shape.merge_with(value.shape); - _tensor_array.add(value); - return this; + int index_int; + if(index is EagerTensor eager) + { + return write(eager.numpy(), value, name); + } + throw new InvalidArgumentError("The index is supposed to be an EagerTensor"); } public override TensorArray write(int index, T value, string name = null) { - var value_tensor = ops.convert_to_tensor(value, preferred_dtype: _dtype, name: "value"); - var index_tensor = ops.convert_to_tensor(index, name: "index"); - return write(index_tensor, value_tensor, name: name); + int size = _tensor_array.Count; + if(index >= size) + { + if (!_dynamic_size) + { + throw new OutOfRangeError($"Tried to write to index {index} but array is not resizeable and size " + + $"is: {size} "); + } + _tensor_array.AddRange(Enumerable.Repeat(null, index - size + 1)); + } + + Tensor tensor = ops.convert_to_tensor(value, preferred_dtype: _dtype, name: "value"); + + if(_dtype != tensor.dtype) + { + throw new InvalidArgumentError($"TensorArray dtype is {_dtype.as_python_name()} but Op is " + + $"trying to write dtype {tensor.dtype.as_python_name()} "); + } + + if (!_element_shape.is_compatible_with(tensor.shape)) + { + throw new ValueError($"Incompatible shape for value ({tensor.shape}), expected ({_element_shape})"); + } + + if (_infer_shape) + { + _element_shape = _element_shape.merge_with(tensor.shape); + } + _tensor_array[index] = tensor; + return this; } private Tensor size(string name = null) @@ -156,11 +230,26 @@ private Tensor size(string name = null) public override Tensor stack(string name = null) { - ops.colocate_with(_handle); - return tf_with(ops.name_scope(name, "TensorArrayStack", new { _handle }), delegate + if(_tensor_array.Count > 0) + { + for(int i = 0; i < _tensor_array.Count; i++) + { + _maybe_zero(i); + } + } + if(_tensor_array.Count == 0 && _element_shape.IsFullyDefined) + { + return ops.convert_to_tensor(new Shape(new long[] { 0 }.Concat(_element_shape.dims).ToArray()), name: name, dtype: _dtype); + } + else { - return gather(math_ops.range(0, size()), name: name); - }); + return ops.convert_to_tensor(_tensor_array, name: name, dtype: _dtype); + } + //ops.colocate_with(_handle); + //return tf_with(ops.name_scope(name, "TensorArrayStack", new { _handle }), delegate + //{ + // return gather(math_ops.range(0, size()), name: name); + //}); } public override Tensor gather(Tensor indices, string name = null) diff --git a/src/TensorFlowNET.Core/Operations/_GraphTensorArray.cs b/src/TensorFlowNET.Core/Operations/_GraphTensorArray.cs index 16870e9f6..2384e8146 100644 --- a/src/TensorFlowNET.Core/Operations/_GraphTensorArray.cs +++ b/src/TensorFlowNET.Core/Operations/_GraphTensorArray.cs @@ -16,7 +16,10 @@ limitations under the License. using System; using System.Collections.Generic; +using System.Diagnostics; using System.Linq; +using Tensorflow.Common.Types; +using Tensorflow.Eager; using static Tensorflow.Binding; namespace Tensorflow.Operations @@ -32,18 +35,18 @@ public class _GraphTensorArray : TensorArray /// first tensor written to it. /// bool _colocate_with_first_write_call; - public bool colocate_with_first_write_call => _colocate_with_first_write_call; + public override bool colocate_with_first_write_call => _colocate_with_first_write_call; bool _infer_shape; - public bool infer_shape => _infer_shape; - public bool _dynamic_size; + public override bool infer_shape => _infer_shape; public List _element_shape; public List _colocate_with; internal Tensor _handle; - public Tensor handle => _handle; + public override Tensor handle => _handle; internal Tensor _flow; + public override Tensor flow => _flow; public _GraphTensorArray(TF_DataType dtype, Tensor size, bool? dynamic_size = null, bool? clear_after_read = null, string tensor_array_name = null, Tensor handle = null, Tensor flow = null, @@ -54,6 +57,7 @@ public _GraphTensorArray(TF_DataType dtype, Tensor size, bool? dynamic_size = nu dynamic_size = dynamic_size ?? false; _dynamic_size = dynamic_size.Value; _dtype = dtype; + _size = size; _colocate_with_first_write_call = colocate_with_first_write_call; if (colocate_with_first_write_call) @@ -146,7 +150,9 @@ public TensorArray scatter(Tensor indices, Tensor value, string name = null) return ta; });*/ - throw new NotImplementedException(""); + + //throw new NotImplementedException(""); + return this; } public void _merge_element_shape(Shape shape) @@ -232,4 +238,173 @@ public override Tensor gather(Tensor indices, string name = null) return value; } } + + public class _GraphTensorArrayV2 : TensorArray + { + internal TF_DataType _dtype; + public override TF_DataType dtype => _dtype; + + /// + /// Used to keep track of what tensors the TensorArray should be + /// colocated with. We choose to colocate the TensorArray with the + /// first tensor written to it. + /// + bool _colocate_with_first_write_call; + public override bool colocate_with_first_write_call => _colocate_with_first_write_call; + + bool _infer_shape; + public override bool infer_shape => _infer_shape; + public Shape _element_shape; + + public List _colocate_with; + + internal Tensor _handle; + public override Tensor handle => _handle; + internal Tensor _flow; + public override Tensor flow => _flow; + + public _GraphTensorArrayV2(TF_DataType dtype, Tensor size, bool? dynamic_size = null, + bool? clear_after_read = null, string tensor_array_name = null, Tensor handle = null, Tensor flow = null, + bool infer_shape = true, Shape? element_shape = null, + bool colocate_with_first_write_call = true, string name = null) + { + Debug.Assert(handle is null); + dynamic_size = dynamic_size ?? false; + _dynamic_size = dynamic_size.Value; + _size = size; + + if(flow is not null && flow.dtype != dtypes.variant) + { + throw new TypeError($"Expected `flow` to be a variant tensor, but received `{flow.dtype}` instead"); + } + if(flow is null && size is null) + { + throw new ValueError("Argument `size` must be provided if argument `flow` is not provided."); + } + if(flow is not null && size is not null) + { + throw new ValueError("Cannot provide both `flow` and `size` arguments at the same time."); + } + if(flow is not null && element_shape is not null) + { + throw new ValueError("Cannot provide both `flow` and `element_shape` arguments at the same time."); + } + + _dtype = dtype; + + _element_shape = element_shape; + _infer_shape = infer_shape; + tf_with(ops.name_scope(name, "TensorArrayV2", new object[] { size, flow }), scope => + { + if (flow is null) + { + _flow = list_ops.tensor_list_reserve(element_shape, size, dtype, scope.scope_name); + } + else + { + _flow = flow; + } + }); + + _colocate_with_first_write_call = false; + _colocate_with = null; + } + + public override TensorArray unstack(Tensor value, string name = null) + { + return tf_with(ops.name_scope(name, "TensorArrayUnstack", new { _flow, value }), delegate + { + value = ops.convert_to_tensor(value, preferred_dtype: _dtype, name: "value"); + Debug.Assert(value.dtype == _dtype); + var flow_out = list_ops.tensor_list_from_tensor(value, value.shape.dims.Skip(1).ToArray()); + return tensor_array_ops.build_ta_with_new_flow(this, flow_out); + }); + } + + public TensorArray scatter(Tensor indices, Tensor value, string name = null) + { + return tf_with(ops.name_scope(name, "TensorArrayScatter", new { _flow, value, indices }), delegate + { + value = ops.convert_to_tensor(value, preferred_dtype: _dtype, name: "value"); + Debug.Assert(value.dtype == _dtype); + var flow_out = list_ops.tensor_list_scatter(value, indices, _element_shape, _flow); + return tensor_array_ops.build_ta_with_new_flow(this, flow_out); + }); + } + + public override Tensor read(T index, string name = null) + { + if(index is Tensor tensor) + { + return read(tensor, name); + } + else + { + throw new TypeError("Please use non-generic method instead."); + } + } + + public Tensor read(Tensor index, string name = null) + { + return tf_with(tf.name_scope(name, "TensorArrayV2Read", new object[] { _flow, index }), scope => + { + return list_ops.tensor_list_get_item(_flow, index, _dtype, _element_shape, name); + }); + } + + public override TensorArray write(Tensor index, Tensor value, string name = null) + { + return tf_with(ops.name_scope(name, "TensorArrayV2Write", new { _flow, index, value }), delegate + { + value = ops.convert_to_tensor(value, preferred_dtype: _dtype, name: "value"); + Debug.Assert(value.dtype == _dtype); + var flow_out = list_ops.tensor_list_set_item(_flow, index, value, _dynamic_size, name); + + return tensor_array_ops.build_ta_with_new_flow(this, flow_out); + }); + } + + public override TensorArray write(int index, T value, string name = null) + { + var value_tensor = ops.convert_to_tensor(value, preferred_dtype: _dtype, name: "value"); + var index_tensor = ops.convert_to_tensor(index, name: "index"); + return write(index_tensor, value_tensor); + } + + private Tensor size(string name = null) + { + if(!_dynamic_size && _size is not null) + { + return ops.convert_to_tensor(_size, dtypes.int32); + } + else + { + return gen_list_ops.tensor_list_length(_flow, name); + } + } + + public override Tensor stack(string name = null) + { + return tf_with(ops.name_scope(name, "TensorArrayV2Stack", _flow), delegate + { + int ta_size; + if(!_dynamic_size && (_size is not null)) + { + var size_tensor = tensor_util.constant_value(_size); + ta_size = size_tensor is null ? -1 : (int)size_tensor; + } + else + { + ta_size = -1; + } + var value = list_ops.tensor_list_stack(_flow, _dtype, ta_size, _element_shape); + return value; + }); + } + + public override Tensor gather(Tensor indices, string name = null) + { + return list_ops.tensor_list_gather(_flow, indices, _dtype, _element_shape, name); + } + } } diff --git a/src/TensorFlowNET.Core/Operations/array_ops.cs b/src/TensorFlowNET.Core/Operations/array_ops.cs index 2767e8219..548a885ed 100644 --- a/src/TensorFlowNET.Core/Operations/array_ops.cs +++ b/src/TensorFlowNET.Core/Operations/array_ops.cs @@ -22,12 +22,13 @@ limitations under the License. using Tensorflow.Eager; using Tensorflow.Framework; using static Tensorflow.Binding; +using System.Diagnostics; namespace Tensorflow { public class array_ops { - public static Tensor placeholder_with_default(T input, int[] shape, string name = null) + public static Tensor placeholder_with_default(Tensor input, int[] shape, string name = null) => gen_array_ops.placeholder_with_default(input, shape, name); /// @@ -83,8 +84,13 @@ public static Tensor zeros(Shape shape, TF_DataType dtype = TF_DataType.TF_FLOAT // var shape_tensor = constant_op._tensor_shape_tensor_conversion_function(shape); Tensor zeros = dtype switch { + TF_DataType.TF_BOOL => constant(false), TF_DataType.TF_DOUBLE => constant(0d), TF_DataType.TF_FLOAT => constant(0f), + TF_DataType.TF_INT64 => constant(0L), + TF_DataType.TF_UINT64 => constant((ulong)0), + TF_DataType.TF_INT32 => constant(0), + TF_DataType.TF_UINT32 => constant((uint)0), TF_DataType.TF_INT8 => constant((sbyte)0), TF_DataType.TF_UINT8 => constant((byte)0), _ => constant(0) @@ -107,9 +113,15 @@ public static Tensor zeros(Shape shape, TF_DataType dtype = TF_DataType.TF_FLOAT return _constant_if_small(0.0F, shape, dtype, name); case TF_DataType.TF_INT64: return _constant_if_small(0L, shape, dtype, name); + case TF_DataType.TF_UINT64: + return _constant_if_small(0, shape, dtype, name); case TF_DataType.TF_INT32: return _constant_if_small(0, shape, dtype, name); + case TF_DataType.TF_UINT32: + return _constant_if_small(0, shape, dtype, name); case TF_DataType.TF_INT8: + return _constant_if_small(0, shape, dtype, name); + case TF_DataType.TF_UINT8: return _constant_if_small(0, shape, dtype, name); default: throw new TypeError("can't find type for zeros"); @@ -118,6 +130,27 @@ public static Tensor zeros(Shape shape, TF_DataType dtype = TF_DataType.TF_FLOAT } } + public static Tensor zeros(Tensors shape, TF_DataType dtype = TF_DataType.TF_FLOAT, string name = null) + { + dtype = dtype.as_base_dtype(); + Tensor shapeTensor; + if(shape.Length > 1) + { + shapeTensor = ops.convert_to_tensor(shape, dtypes.int32); + if (shapeTensor.ndim > 1) + { + shapeTensor = array_ops.reshape(shapeTensor, new Shape(-1)); + } + } + else + { + shapeTensor = shape[0]; + } + var output = fill(shapeTensor, array_ops.constant(0, dtype), name); + Debug.Assert(output.dtype.as_base_dtype() == dtype); + return output; + } + public static Tensor boolean_mask(T1 tensor, T2 mask, string name = "boolean_mask", int axis = 0) { return tf_with(ops.name_scope(name, values: new { tensor, mask }), delegate @@ -132,7 +165,12 @@ public static Tensor boolean_mask(T1 tensor, T2 mask, string name = "boo if (ndims_mask < 1) throw new ValueError("mask cannot be scalar."); - var leading_size = gen_math_ops.prod(shape(tensor_tensor)[$"{axis}:{axis + ndims_mask}"], new[] { 0 }); + var leading_size = gen_math_ops.prod(shape(tensor_tensor)[$"{axis}:{axis + ndims_mask}"], ops.convert_to_tensor(new[] { 0 })); + if (leading_size.rank == 0) + { + leading_size = expand_dims(leading_size, 0); + } + var shape1 = concat(new[] { shape(tensor_tensor)[$":{axis}"], @@ -152,8 +190,8 @@ public static Tensor boolean_mask(T1 tensor, T2 mask, string name = "boo private static Tensor _apply_mask_1d(Tensor reshaped_tensor, Tensor mask, int axis = 0) { - var indices = squeeze(where(mask), axis: new[] { 1 }); - return gather(reshaped_tensor, indices, axis: axis); + var indices = squeeze(where_v2(mask), axis: new[] { 1 }); + return gather(reshaped_tensor, indices, axis: ops.convert_to_tensor(axis)); } public static Tensor zeros(Tensor shape, TF_DataType dtype = TF_DataType.TF_FLOAT, string name = null) @@ -271,6 +309,10 @@ public static Tensor _autopacking_helper(IEnumerable list_or_tuple, TF_D { elems_as_tensors.Add(tensor); } + else if (elem is KerasTensor kt) + { + elems_as_tensors.Add(kt); + } else { var elem_tensor = constant_op.constant(elem, dtype: dtype, name: i.ToString()); @@ -293,7 +335,7 @@ public static Tensor _autopacking_helper(IEnumerable list_or_tuple, TF_D } public static Tensor expand_dims(Tensor input, int axis = -1, string name = null) - => gen_array_ops.expand_dims(input, axis, name); + => gen_array_ops.expand_dims(input, ops.convert_to_tensor(axis), name); /// /// Creates a tensor filled with a scalar value. @@ -304,7 +346,10 @@ public static Tensor expand_dims(Tensor input, int axis = -1, string name = null /// Optional string. The name of the output `tf.Tensor`. /// A `tf.Tensor` with shape `dims` and the same dtype as `value`. public static Tensor fill(Shape dims, T value, string name = null) - => gen_array_ops.fill(dims, value, name: name); + => gen_array_ops.fill(dims, ops.convert_to_tensor(value), name: name); + + public static Tensor fill(Tensor dims, T value, string name = null) + => gen_array_ops.fill(dims, ops.convert_to_tensor(value), name: name); /// /// Returns the rank of a tensor. @@ -368,7 +413,20 @@ public static Tensor reshape(Tensor tensor, Shape shape, string name = null) => gen_array_ops.reshape(tensor, shape, name: name); public static Tensor reshape(Tensor tensor, object[] shape, string name = null) - => gen_array_ops.reshape(tensor, shape, name: name); + { + var dims = shape_utils.from_object_array(shape); + return gen_array_ops.reshape(tensor, dims, name: name); + } + + public static Tensor reverse(Tensor tensor, Tensor axis, string name = null) + => tf.Context.ExecuteOp("ReverseV2", name, new ExecuteOpArgs(tensor, axis) + { + GetGradientAttrs = (op) => new + { + T = op.get_attr("T"), + Tidx = op.get_attr("Tidx") + } + }); private static Tensor ones_like_impl(T tensor, TF_DataType dtype, string name, bool optimize = true) { @@ -389,6 +447,10 @@ public static Tensor ones(Tensor shape, TF_DataType dtype = TF_DataType.TF_FLOAT return tf_with(ops.name_scope(name, "ones", new { shape }), scope => { name = scope; + if (shape._shape_tuple().Length == 0) + { + shape = reshape(shape, new Shape(-1)); + } var output = gen_array_ops.fill(shape, constant_op.constant(1.0f, dtype: dtype), name: name); return output; }); @@ -466,7 +528,11 @@ public static Tensor one_hot(Tensor indices, Tensor depth, } public static (Tensor, Tensor) unique(Tensor x, TF_DataType out_idx = TF_DataType.TF_INT32, string name = null) - => gen_array_ops.unique(x, out_idx: out_idx, name: name); + { + var res = gen_array_ops.unique(x, out_idx: out_idx, name: name); + Debug.Assert(res.Length == 2); + return (res[0], res[1]); + } public static Tensor stack(Tensor[] values, int axis = 0, string name = "stack") { @@ -492,12 +558,12 @@ public static Tensor where(Tensor condition, object x = null, object y = null, s { name = scope; condition = ops.convert_to_tensor(condition, preferred_dtype: dtypes.@bool, name: "condition"); - return gen_array_ops.where(condition: condition, name: name); + return gen_array_ops.where(condition, name: name); }); } else if (x != null && y != null) { - return gen_array_ops.select(condition, x, y, name); + return gen_math_ops.select(condition, ops.convert_to_tensor(x), ops.convert_to_tensor(y), name); } else { @@ -505,7 +571,6 @@ public static Tensor where(Tensor condition, object x = null, object y = null, s } } - public static Tensor where_v2(Tensor condition, object x = null, object y = null, string name = null) { if (x == null && y == null) @@ -514,18 +579,19 @@ public static Tensor where_v2(Tensor condition, object x = null, object y = null { name = scope; condition = ops.convert_to_tensor(condition, preferred_dtype: dtypes.@bool, name: "condition"); - return gen_array_ops.where(condition: condition, name: name); + return gen_array_ops.where(condition, name: name); }); } else if (x != null && y != null) { - return gen_array_ops.select_v2(condition, x, y, name); + return gen_math_ops.select_v2(condition, ops.convert_to_tensor(x), ops.convert_to_tensor(y), name); } else { throw new ValueError("x and y must both be non-None or both be None."); } } + /// /// Returns the shape of a tensor. /// @@ -563,7 +629,17 @@ public static Tensor shape_internal(Tensor input, string name = null, bool optim } } - return gen_array_ops.shape(input, name: name, out_type: out_type); + return tf.Context.ExecuteOp("Shape", name, new ExecuteOpArgs(input) + { + GetGradientAttrs = (op) => new + { + T = op.get_attr("T"), + out_type = op.get_attr("out_type") + } + }.SetAttributes(new + { + out_type + })).First(); }); } @@ -597,6 +673,18 @@ public static Tensor tile(Tensor input, Tensor multiples, string name = null) } }); + /*public static Tensor tile(Tensor input, Shape multiples, string name = null) + { + return tf.Context.ExecuteOp("Tile", name, new ExecuteOpArgs(input, multiples) + { + GetGradientAttrs = (op) => new + { + T = op.get_attr("T"), + Tmultiples = op.get_attr("Tmultiples") + } + }); + }*/ + public static Tensor zeros_like(Tensor tensor, TF_DataType dtype = TF_DataType.DtInvalid, string name = null, bool optimize = true) { return tf_with(ops.name_scope(name, "zeros_like", new Tensor[] { tensor }), scope => @@ -634,7 +722,12 @@ public static Tensor zeros_like(Tensor tensor, TF_DataType dtype = TF_DataType.D /// /// public static Tensor stop_gradient(Tensor input, string name = null) - => tf.Context.ExecuteOp("StopGradient", name, new ExecuteOpArgs(input)); + { + var tape = tf.GradientTape().stop_recording(); + var result = gen_array_ops.stop_gradient(input, name); + tape.StartRecord(); + return result; + } /// /// Extracts a strided slice of a tensor (generalized python array indexing). @@ -658,23 +751,26 @@ public static Tensor strided_slice(Tensor input_, Tensor begin, Tensor end, int new_axis_mask = 0, int shrink_axis_mask = 0, string name = null) - { - var op = gen_array_ops.strided_slice( - input: input_, - begin: begin, - end: end, - strides: strides, - begin_mask: begin_mask, - end_mask: end_mask, - ellipsis_mask: ellipsis_mask, - new_axis_mask: new_axis_mask, - shrink_axis_mask: shrink_axis_mask, - name: name); - - string parent_name = name; - - return op; - } + => tf.Context.ExecuteOp("StridedSlice", name, new ExecuteOpArgs(input_, begin, end, strides) + { + GetGradientAttrs = (op) => new + { + T = op.get_attr("T"), + Index = op.get_attr("Index"), + begin_mask = op.get_attr("begin_mask"), + end_mask = op.get_attr("end_mask"), + ellipsis_mask = op.get_attr("ellipsis_mask"), + new_axis_mask = op.get_attr("new_axis_mask"), + shrink_axis_mask = op.get_attr("shrink_axis_mask") + } + }.SetAttributes(new + { + begin_mask, + end_mask, + ellipsis_mask, + new_axis_mask, + shrink_axis_mask + })); /// /// Returns the gradient of `StridedSlice`. @@ -738,7 +834,7 @@ public static Tensor strided_slice_grad(Tensor shape, Tensor begin, Tensor end, /// A `Tensor`. Has the same type as `input`. /// Contains the same data as `input`, but has one or more dimensions of /// size 1 removed. - public static Tensor squeeze(Tensor input, int[] axis = null, string name = null) + public static Tensor squeeze(Tensor input, Axis axis = null, string name = null) => gen_array_ops.squeeze(input, axis, name); public static Tensor identity(Tensor input, string name = null) @@ -847,28 +943,14 @@ public static Tensor broadcast_static_shape(Tensor shape_x, Tensor shape_y) /// /// /// - public static Tensor concat(Tensor[] values, int axis, string name = "concat") - { - if (values.Length == 1) // Degenerate case of one tensor. - { - return tf_with(ops.name_scope(name), scope => - { - var t = ops.convert_to_tensor(axis, name: "concat_dim", dtype: TF_DataType.TF_INT32); - return identity(values[0], name: scope); - }); - } - - return gen_array_ops.concat_v2(values, axis, name: name); - } - public static Tensor concat(Tensor[] values, Tensor axis, string name = "concat") { - return gen_array_ops.concat_v2(values, axis, name: name); + return tf.Context.ExecuteOp("ConcatV2", name, new ExecuteOpArgs(values, axis)); } public static Tensor concat(object[] values, int axis, string name = "concat") { - return gen_array_ops.concat_v2(values, axis, name: name); + return tf.Context.ExecuteOp("ConcatV2", name, new ExecuteOpArgs(values, axis)); } /// @@ -886,19 +968,34 @@ public static Tensor concat(object[] values, int axis, string name = "concat") /// /// An integer. The number of batch dimensions. Must be less than or equal to rank(indices). /// - public static Tensor gather(T1 @params, T2 indices, string name = null, int axis = 0, int batch_dims = 0) + public static Tensor gather(Tensor @params, Tensor indices, string name = null, Tensor axis = null, int batch_dims = 0) { - if (axis != 0) - return gen_array_ops.gather_v2(@params, indices, axis, name: name); - - if (@params is ResourceVariable variable && - indices is Tensor indices_tensor) - return variable.sparse_read(indices_tensor, name); + if (axis is null) + axis = tf.convert_to_tensor(batch_dims); + if(tensor_util.constant_value(axis) != 0) + { + return gen_array_ops.gather_v2(@params, indices, axis, batch_dims: batch_dims, name: name); + } return gen_array_ops.gather_v2(@params, indices, axis, name: name); } - public static Tensor transpose(T1 a, Axis perm, string name = "transpose", bool conjugate = false) + public static Tensor gather(Tensor @params, Tensor indices, int axis, string name = null, int batch_dims = 0) + => gather(@params, indices, name, ops.convert_to_tensor(axis), batch_dims); + + public static Tensor gather(ResourceVariable @params, Tensor indices, string name = null, Tensor axis = null, int batch_dims = 0) + { + if (axis is null) + axis = tf.convert_to_tensor(batch_dims); + if (tensor_util.constant_value(axis) != 0) + { + throw new NotImplementedException(); + } + + return @params.sparse_read(indices, name); + } + + public static Tensor transpose(T1 a, Axis perm = null, string name = "transpose", bool conjugate = false) { return tf_with(ops.name_scope(name, "transpose", new { a }), scope => { @@ -921,55 +1018,77 @@ public static Tensor transpose(Tensor a, Tensor perm, string name = "transpose", }); } - public static Tensor[] split(Tensor value, Tensor size_splits, int axis, int num = -1, - string name = "split") + /// + /// Transposes last two dimensions of tensor `a`. + /// For example: + /// python + /// x = tf.constant([[1, 2, 3], [4, 5, 6]]) + /// tf.matrix_transpose(x) # [[1, 4], + /// # [2, 5], + /// # [3, 6]] + /// + /// Matrix with two batch dimensions. + /// x.shape is [1, 2, 3, 4] + /// tf.linalg.matrix_transpose(x) is shape [1, 2, 4, 3] + /// + /// + /// + /// + /// + /// + public static Tensor matrix_transpose(Tensor a, string name = "matrix_transpose", bool conjugate = false) { - if (num == -1) - num = (int)size_splits.shape[0]; - - return gen_array_ops.split_v(value, size_splits, axis, num, name: name); + return tf_with(ops.name_scope(name, "transpose", new { a }), scope => + { + var a_shape = a.shape; + var ndims = a.shape.ndim; + Axis perm; + if(ndims != 0) + { + if (ndims < 2) + { + throw new ValueError("Argument `a` should be a (batch) matrix with rank " + + $">= 2. Received `a` = {a} with shape: {a_shape}"); + } + perm = new Axis(Enumerable.Range(0, ndims - 2).Concat(new int[] { ndims - 1, ndims - 2 }).ToArray()); + } + else + { + var a_rank = a.rank; + perm = new Axis(Enumerable.Range(0, a_rank - 2).Concat(new int[] { a_rank - 1, a_rank - 2 }).ToArray()); + } + return transpose(a, perm:perm, conjugate:conjugate); + }); } - public static Tensor[] split(Tensor value, int num_split, T axis, + public static Tensor[] split(Tensor value, int num_or_size_splits, Tensor axis = null, string name = "split") { - var size_splits = ops.convert_to_tensor(num_split); + return gen_array_ops.split(split_dim: axis, value: value, num_split: num_or_size_splits, name); + } - if (tf.Context.executing_eagerly()) + public static Tensor[] split(Tensor value, int[] num_or_size_splits, Tensor axis = null, int num = -1, + string name = "split") + { + if(num_or_size_splits.Length == 0) { - return split_eager_fallback(axis, value, num_split: num_split, name: name, ctx: tf.Context); + throw new ValueError("Rank-0 tensors are not supported as the num_or_size_splits argument to split."); } + var size_splits = ops.convert_to_tensor(num_or_size_splits); - var _op = tf.OpDefLib._apply_op_helper("Split", name, new { split_dim = axis, value, num_split }); - return _op.outputs; - } - - private static Tensor[] split_eager_fallback(Ta axis, Tv value, int num_split, string name, Context ctx = null) - { - var (_attr_T, input) = tf.Runner.ArgsToMatchingEager(ctx, args: new object[] { value }); - var axis_tensor = ops.convert_to_tensor(axis, dtype: TF_DataType.TF_INT32); - var _inputs_flat = new List { axis_tensor }; - _inputs_flat.AddRange(input); - var _attrs = new object[] { "num_split", num_split, "T", _attr_T }; + if(num == -1) + { + num = (int)size_splits.shape[0]; + } - return tf.Runner.Execute(ctx, "Split", num_split, _inputs_flat.ToArray(), _attrs, name: name); + return gen_array_ops.split_v(value: value, size_splits: size_splits, split_dim: axis, num_split: num, name: name); } public static Tensor slice(Tensor input, Tensor[] begin, Tensor[] size, string name = null) - => gen_array_ops.slice(input, begin, size, name: name); - - public static Tensor slice(Tensor input, Tb begin, Ts size, string name = null) - => gen_array_ops.slice(input, begin, size, name: name); + => gen_array_ops.slice(input, ops.convert_to_tensor(begin), ops.convert_to_tensor(size), name: name); public static Tensor slice(Tensor input, Tensor begin, Tensor size, string name = null) - => tf.Context.ExecuteOp("Slice", name, new ExecuteOpArgs(input, begin, size) - { - GetGradientAttrs = (op) => new - { - T = op.get_attr("T"), - Index = op.get_attr("Index") - } - }); + => gen_array_ops.slice(input, begin, size, name: name); public static Tensor stack(object values, int axis = 0, string name = "stack") @@ -1025,5 +1144,18 @@ public static Tensor placeholder(TF_DataType dtype, Shape shape = null, string n var _op = tf.OpDefLib._apply_op_helper("Placeholder", name: name, args: new { dtype, shape }); return _op.output; } + + public static int get_positive_axis(int axis, int ndims=-100, string axis_name="axis", string ndims_name= "ndims") + { + if(ndims != -100) + { + if (axis >= 0 && axis < ndims) return axis; + else if (-ndims <= axis && axis < 0) return axis + ndims; + else throw new ValueError($"{axis_name}={axis} out of bounds:expected {-ndims}<={axis_name}<{ndims}"); + + } else if(axis < 0) throw new ValueError($"{axis_name}={axis} may only be negative if {ndims_name} is statically known."); + return axis; + } + } } diff --git a/src/TensorFlowNET.Core/Operations/control_flow_ops.cs b/src/TensorFlowNET.Core/Operations/control_flow_ops.cs index 862b636fd..efd9aba35 100644 --- a/src/TensorFlowNET.Core/Operations/control_flow_ops.cs +++ b/src/TensorFlowNET.Core/Operations/control_flow_ops.cs @@ -675,16 +675,17 @@ public static Tensor ZerosLikeOutsideLoop(Operation op, int index) } } - public static Tensor[] while_loop(Func cond, - Func body, - Tensor[] loop_vars, + public static Tensors while_loop(Func cond, + Func body, + Tensors loop_vars, int parallel_iterations = 10, string name = null) { var executing_eagerly = tf.Context.executing_eagerly(); if (!executing_eagerly) { - throw new NotImplementedException(""); + return while_v2.while_loop(cond, body, loop_vars, parallel_iterations: parallel_iterations, + name: name); } return tf_with(ops.name_scope("name", "while"), delegate diff --git a/src/TensorFlowNET.Core/Operations/control_flow_util.py.cs b/src/TensorFlowNET.Core/Operations/control_flow_util.py.cs index c88911194..536d4e3c2 100644 --- a/src/TensorFlowNET.Core/Operations/control_flow_util.py.cs +++ b/src/TensorFlowNET.Core/Operations/control_flow_util.py.cs @@ -16,12 +16,20 @@ limitations under the License. using System; using System.Linq; +using Tensorflow.Functions; +using Tensorflow.Graphs; using Tensorflow.Operations; +using static Tensorflow.Binding; namespace Tensorflow { public class control_flow_util { + public static readonly bool ENABLE_CONTROL_FLOW_V2 = !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("TF_ENABLE_CONTROL_FLOW_V2")) && Environment.GetEnvironmentVariable("TF_ENABLE_CONTROL_FLOW_V2") != "0" || + (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("TF_ENABLE_CONTROL_FLOW_V2")) && Environment.GetEnvironmentVariable("TF_ENABLE_CONTROL_FLOW_V2") != "0") || + (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("TF_ENABLE_COND_V2")) && Environment.GetEnvironmentVariable("TF_ENABLE_COND_V2") != "0") || + (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("TF_ENABLE_WHILE_V2")) && Environment.GetEnvironmentVariable("TF_ENABLE_WHILE_V2") != "0") || + (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("TF_ENABLE_TENSOR_ARRAY_V2")) && Environment.GetEnvironmentVariable("TF_ENABLE_TENSOR_ARRAY_V2") != "0"); /// /// Return true if `op` is an Exit. /// @@ -196,5 +204,74 @@ public static WhileContext GetContainingWhileContext(ControlFlowContext ctxt, Co } return null; } + + public static bool EnableControlFlowV2(Graph graph) + { + return ENABLE_CONTROL_FLOW_V2 || graph.building_function && (graph is not FuncGraph func || func.captures.Length == 0); + + } + + public static string create_new_tf_function(FuncGraph func_graph) + { + var func = new EagerDefinedFunction(func_graph.Name, func_graph, func_graph.Inputs, func_graph.Outputs, new Dictionary()); + func.AddToGraph(func_graph); + return func_graph.Name; + } + + public static (Operation, Tensor[]) get_op_and_outputs(Tensor[] inputs) + { + if(inputs.Length == 0) + { + return (null, new Tensor[0]); + } + else + { + return (inputs[0], inputs); + } + } + + public static Tensor[] run_as_function_for_tape_gradients(Func make_op, Tensor[] inputs) + { + if(gradients_util.PossibleTapeGradientTypes(inputs) == gradients_util.POSSIBLE_GRADIENT_TYPES_HIGHER_ORDER + && !(ops.get_default_graph().building_function)) + { + throw new NotImplementedException(); + } + else + { + return make_op(inputs); + } + } + + public static string unique_fn_name(string scope, string name) + { + return $"{scope}{name}_{ops.uid()}".Replace("/", "_"); + } + + public static bool output_all_intermediates() + { + if (in_defun()) + { + return false; + } + if(tf.Context.FunctionCallOptions.ExecutorType == "SINGLE_THREADED_EXECUTOR") + { + return false; + } + // TODO(Rinne): check this after refactoring keras building. + return false; + } + + public static bool in_defun() + { + if (tf.Context.executing_eagerly()) + { + return false; + } + + var graph = ops.get_default_graph(); + // TODO(Rinne): CondBranchFuncGraph, WhileBodyFuncGraph, WhileCondFuncGraph + return graph is FuncGraph; + } } } diff --git a/src/TensorFlowNET.Core/Operations/dataset_ops.cs b/src/TensorFlowNET.Core/Operations/dataset_ops.cs index c7e627772..061fb95e3 100644 --- a/src/TensorFlowNET.Core/Operations/dataset_ops.cs +++ b/src/TensorFlowNET.Core/Operations/dataset_ops.cs @@ -233,7 +233,7 @@ public Tensor anonymous_iterator_v3(TF_DataType[] output_types, Shape[] output_s { try { - var result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo("AnonymousIteratorV3", name) + var result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(tf.Context, "AnonymousIteratorV3", name) { attrs = attrs }); @@ -250,7 +250,7 @@ public Tensor anonymous_iterator_v3(TF_DataType[] output_types, Shape[] output_s public Tensor anonymous_iterator_v3_eager_fallback(TF_DataType[] output_types, Shape[] output_shapes, string name, Context ctx) { object[] attrs = new object[] { output_types, output_shapes }; - var result = execute.quick_execute("AnonymousIteratorV3", 1, new Tensor[] { }, attrs, ctx, name); + var result = _execute.quick_execute("AnonymousIteratorV3", 1, new Tensor[] { }, attrs, ctx, name); return result[0]; } diff --git a/src/TensorFlowNET.Core/Operations/gen_array_ops.cs b/src/TensorFlowNET.Core/Operations/gen_array_ops.cs index 1dc6504ab..8367c2f94 100644 --- a/src/TensorFlowNET.Core/Operations/gen_array_ops.cs +++ b/src/TensorFlowNET.Core/Operations/gen_array_ops.cs @@ -1,543 +1,10806 @@ -/***************************************************************************** - Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. +/*Wrappers around TensorFlow ops. This file is MACHINE GENERATED! Do not edit.*/ - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -******************************************************************************/ - -using System; -using System.Linq; -using Tensorflow.Contexts; using Tensorflow.Eager; +using Tensorflow.Contexts; +using Tensorflow.Exceptions; using static Tensorflow.Binding; -namespace Tensorflow +namespace Tensorflow; + +public static class gen_array_ops { - public static class gen_array_ops + /// + /// + /// + /// + /// + /// + /// + public static Tensor batch_matrix_band_part(Tensor input, Tensor num_lower, Tensor num_upper, string? name = null) { - public static Tensor batch_to_space_nd(T input, int[] block_shape, int[,] crops, string name = null) + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) { - var _op = tf.OpDefLib._apply_op_helper("BatchToSpaceND", name: name, args: new { input, block_shape, crops }); - - return _op.output; + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "BatchMatrixBandPart", name) { args = new object[] { input, num_lower, num_upper }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return batch_matrix_band_part_eager_fallback(input, num_lower, num_upper, name: name, ctx: _ctx); + } + catch (Exception) + { + } } - - public static Tensor check_numerics(Tensor tensor, string message, string name = null) + Dictionary keywords = new(); + keywords["input"] = input; + keywords["num_lower"] = num_lower; + keywords["num_upper"] = num_upper; + var _op = tf.OpDefLib._apply_op_helper("BatchMatrixBandPart", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) { - var _op = tf.OpDefLib._apply_op_helper("CheckNumerics", name: name, args: new { tensor, message }); - - return _op.output; + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("BatchMatrixBandPart", _op.inputs, _attrs, _result); } + return _result[0]; + } - /// - /// Concatenates tensors along one dimension. - /// - /// - /// - /// - /// - public static Tensor concat_v2(T[] values, Ta axis, string name = null) - => tf.Context.ExecuteOp("ConcatV2", name, new ExecuteOpArgs(values, axis)); - - public static Tensor concat_v2(Tensor[] values, Tensor axis, string name = null) + public static Tensor batch_matrix_band_part_eager_fallback(Tensor input, Tensor num_lower, Tensor num_upper, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, num_lower, num_upper }; + object[] _attrs = new object[] { "T", input.dtype }; + var _result = _execute.execute("BatchMatrixBandPart", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("BatchMatrixBandPart", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// + /// + /// + /// + public static Tensor batch_matrix_diag(Tensor diagonal, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) { - if (tf.Context.executing_eagerly()) + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "BatchMatrixDiag", name) { args = new object[] { diagonal }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return batch_matrix_diag_eager_fallback(diagonal, name: name, ctx: _ctx); + } + catch (Exception) { - return concat_v2_eager_fallback(values, axis, name, tf.Context); } - - var _op = tf.OpDefLib._apply_op_helper("ConcatV2", name: name, args: new { values, axis }); - return _op.output; } - - public static Tensor concat_v2(Tensor[] values, int axis, string name = null) - => tf.Context.ExecuteOp("ConcatV2", name, new ExecuteOpArgs(values, axis)); - - private static Tensor concat_v2_eager_fallback(T1[] values, T2 axis, string name, Context ctx) + Dictionary keywords = new(); + keywords["diagonal"] = diagonal; + var _op = tf.OpDefLib._apply_op_helper("BatchMatrixDiag", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) { - var _attr_N = len(values); - var (_attr_T, input) = tf.Runner.ArgsToMatchingEager(ctx, args: values.Select(x => (object)x).ToArray()); - var (_attr_Tidx, axis1) = tf.Runner.ArgsToMatchingEager(ctx, default_dtype: tf.int32, args: new object[] { axis }); - var _inputs_flat = input.concat(axis1); - var _attrs = new object[] { "N", _attr_N, "T", _attr_T, "Tidx", _attr_Tidx }; - - return tf.Runner.Execute(ctx, "ConcatV2", 1, _inputs_flat, _attrs, name: name)[0]; + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("BatchMatrixDiag", _op.inputs, _attrs, _result); } + return _result[0]; + } - public static Tensor[] concat_offset(Tensor concat_dim, Tensor[] shape, string name = null) + public static Tensor batch_matrix_diag_eager_fallback(Tensor diagonal, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { diagonal }; + object[] _attrs = new object[] { "T", diagonal.dtype }; + var _result = _execute.execute("BatchMatrixDiag", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) { - var _op = tf.OpDefLib._apply_op_helper("ConcatOffset", name: name, args: new { concat_dim, shape }); - - return _op.outputs; + _execute.record_gradient("BatchMatrixDiag", _inputs_flat, _attrs, _result); } - - /// - /// Returns a diagonal tensor with a given diagonal values. - /// - /// - /// Rank k tensor where k is at most 1. - /// - /// - /// If specified, the created operation in the graph will be this one, otherwise it will be named 'Diag'. - /// - /// - /// The Operation can be fetched from the resulting Tensor, by fetching the Operation property from the result. - /// - /// - /// Given a diagonal, this operation returns a tensor with the diagonal and - /// everything else padded with zeros. The diagonal is computed as follows: - /// - /// Assume diagonal has dimensions [D1,..., Dk], then the output is a tensor of - /// rank 2k with dimensions [D1,..., Dk, D1,..., Dk] where: - /// - /// output[i1,..., ik, i1,..., ik] = diagonal[i1, ..., ik] and 0 everywhere else. - /// - /// For example: - /// - /// - /// # 'diagonal' is [1, 2, 3, 4] - /// tf.diag(diagonal) ==&gt; [[1, 0, 0, 0] - /// [0, 2, 0, 0] - /// [0, 0, 3, 0] - /// [0, 0, 0, 4]] - /// - /// - public static Tensor diag(Tensor diagonal, string name = null) - => tf.Context.ExecuteOp("Diag", name, new ExecuteOpArgs(diagonal)); - - public static Tensor diag_part(Tensor diagonal, string name = null) - => tf.Context.ExecuteOp("DiagPart", name, new ExecuteOpArgs(diagonal)); - - public static Tensor expand_dims(Tensor input, int axis, string name = null) - => tf.Context.ExecuteOp("ExpandDims", name, new ExecuteOpArgs(input, axis) - .SetAttributes(new { dim = axis })); - - public static Tensor gather_v2(T1 @params, T2 indices, int axis, int batch_dims = 0, string name = null) + return _result[0]; + } + /// + /// + /// + /// + /// + public static Tensor batch_matrix_diag_part(Tensor input, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "BatchMatrixDiagPart", name) { args = new object[] { input }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return batch_matrix_diag_part_eager_fallback(input, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + var _op = tf.OpDefLib._apply_op_helper("BatchMatrixDiagPart", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) { - var result = tf.Context.ExecuteOp("GatherV2", name, new ExecuteOpArgs( - @params, - indices, - axis).SetAttributes(new { batch_dims })); - return result [0]; + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("BatchMatrixDiagPart", _op.inputs, _attrs, _result); } + return _result[0]; + } - private static Tensor gather_v2_eager_fallback(object @params, object indices, int axis, string name, Context ctx) + public static Tensor batch_matrix_diag_part_eager_fallback(Tensor input, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input }; + object[] _attrs = new object[] { "T", input.dtype }; + var _result = _execute.execute("BatchMatrixDiagPart", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) { - var (_attr_T, param) = tf.Runner.ArgsToMatchingEager(ctx, args: new[] { @params }); - var (_attr_Tindice, indice) = tf.Runner.ArgsToMatchingEager(ctx, default_dtype: tf.int32, args: new[] { indices }); - var (_attr_Taxis, axiss) = tf.Runner.ArgsToMatchingEager(ctx, default_dtype: tf.int32, args: new object[] { axis }); - var _inputs_flat = param.concat(indice).concat(axiss); - var _attrs = new object[] { "batch_dims", 0, "Tparams", _attr_T, "Tindices", _attr_Tindice, "Taxis", _attr_Taxis }; - - var results = tf.Runner.Execute(ctx, "GatherV2", 1, _inputs_flat, _attrs, name: name); - if (tf.Runner.MustRecordGradient()) - tf.Runner.RecordGradient("GatherV2", _inputs_flat, _attrs, results); - return results[0]; + _execute.record_gradient("BatchMatrixDiagPart", _inputs_flat, _attrs, _result); } - - - public static Tensor pad(Tensor input, Tensor paddings, string name = null) + return _result[0]; + } + /// + /// + /// + /// + /// + /// + public static Tensor batch_matrix_set_diag(Tensor input, Tensor diagonal, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) { - if (tf.Context.executing_eagerly()) + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "BatchMatrixSetDiag", name) { args = new object[] { input, diagonal }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return batch_matrix_set_diag_eager_fallback(input, diagonal, name: name, ctx: _ctx); + } + catch (Exception) { - /*var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "Pad", name, - null, - input, paddings); - return results[0];*/ - return pad_eager_fallback(input, paddings, name: name, ctx: tf.Context); } - - var _op = tf.OpDefLib._apply_op_helper("Pad", name: name, args: new { input, paddings }); - - return _op.output; } - - private static Tensor pad_eager_fallback(Tensor inputs, Tensor padding, string name = null, Context ctx = null) + Dictionary keywords = new(); + keywords["input"] = input; + keywords["diagonal"] = diagonal; + var _op = tf.OpDefLib._apply_op_helper("BatchMatrixSetDiag", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) { - var (_attr_T, input) = tf.Runner.ArgsToMatchingEager(ctx, args: new[] { inputs }); - var (_attr_Tpaddings, paddings) = tf.Runner.ArgsToMatchingEager(ctx, default_dtype: tf.int32, args: new[] { padding }); - var _inputs_flat = input.concat(paddings); - var _attrs = new object[] { "T", _attr_T, "Tpaddings", _attr_Tpaddings }; - - var results = tf.Runner.Execute(ctx, "Pad", 1, _inputs_flat, _attrs, name: name); - if (tf.Runner.MustRecordGradient()) - tf.Runner.RecordGradient("Pad", _inputs_flat, _attrs, results); - return results[0]; + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("BatchMatrixSetDiag", _op.inputs, _attrs, _result); } + return _result[0]; + } - public static Tensor pack(Tensor[] values, int axis = 0, string name = null) - => tf.Context.ExecuteOp("Pack", name, new ExecuteOpArgs() + public static Tensor batch_matrix_set_diag_eager_fallback(Tensor input, Tensor diagonal, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, diagonal }; + object[] _attrs = new object[] { "T", input.dtype }; + var _result = _execute.execute("BatchMatrixSetDiag", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("BatchMatrixSetDiag", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// BatchToSpace for 4-D tensors of type T. + /// + /// + /// + /// This is a legacy version of the more general BatchToSpaceND. + /// + /// Rearranges (permutes) data from batch into blocks of spatial data, followed by + /// cropping. This is the reverse transformation of SpaceToBatch. More specifically, + /// this op outputs a copy of the input tensor where values from the `batch` + /// dimension are moved in spatial blocks to the `height` and `width` dimensions, + /// followed by cropping along the `height` and `width` dimensions. + /// + /// + /// + /// + /// + /// + public static Tensor batch_to_space(Tensor input, Tensor crops, int block_size = 0, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try { - OpInputArgs = new object[] { values } - }.SetAttributes(new { axis })); - - /// - /// Return a tensor with the same shape and contents as the input tensor or value. - /// - /// - /// - public static Tensor identity(Tensor input, string name = null) - => tf.Context.ExecuteOp("Identity", name, new ExecuteOpArgs(input)); - - public static Tensor invert_permutation(Tensor x, string name = null) + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "BatchToSpace", name) { args = new object[] { input, crops }, attrs = new Dictionary() { ["block_size"] = block_size } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return batch_to_space_eager_fallback(input, crops, block_size: block_size, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["crops"] = crops; + keywords["block_size"] = block_size; + var _op = tf.OpDefLib._apply_op_helper("BatchToSpace", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) { - var _op = tf.OpDefLib._apply_op_helper("InvertPermutation", name, new { x }); - - return _op.outputs[0]; + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "block_size", _op._get_attr_int("block_size"), "Tidx", _op._get_attr_type("Tidx") }; + _execute.record_gradient("BatchToSpace", _op.inputs, _attrs, _result); } + return _result[0]; + } - public static Tensor log(Tensor x, string name = null) - => tf.Context.ExecuteOp("Log", name, new ExecuteOpArgs(x)); - - - public static Tensor rank(Tensor input, string name = null) - => tf.Context.ExecuteOp("Rank", name, new ExecuteOpArgs(input)); - - /// - /// Creates a tensor filled with a scalar value. - /// - /// A `Tensor`. - /// A `Tensor`. 0-D (scalar). Value to fill the returned tensor. - /// A name for the operation (optional). - /// A `Tensor`. Has the same type as `value`. - public static Tensor fill(Tensor dims, T value, string name = null) + public static Tensor batch_to_space_eager_fallback(Tensor input, Tensor crops, int block_size, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, crops }; + object[] _attrs = new object[] { "T", input.dtype, "block_size", block_size, "Tidx", crops.dtype }; + var _result = _execute.execute("BatchToSpace", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("BatchToSpace", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// BatchToSpace for N-D tensors of type T. + /// + /// + /// + /// This operation reshapes the "batch" dimension 0 into `M + 1` dimensions of shape + /// `block_shape + [batch]`, interleaves these blocks back into the grid defined by + /// the spatial dimensions `[1, ..., M]`, to obtain a result with the same rank as + /// the input. The spatial dimensions of this intermediate result are then + /// optionally cropped according to `crops` to produce the output. This is the + /// reverse of SpaceToBatch. See below for a precise description. + /// + /// + /// + /// + /// + /// + public static Tensor batch_to_space_nd(Tensor input, Tensor block_shape, Tensor crops, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) { - var ctx = tf.Context; - if (ctx.executing_eagerly()) + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "BatchToSpaceND", name) { args = new object[] { input, block_shape, crops }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) { - try - { - var _result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo("Fill", name, dims, value)); - return _result[0]; - } - catch (Exception) - { - - } - try - { - return fill_eager_fallback(dims, value as Tensor, name, ctx); - } - catch (Exception) - { - - } } - Dictionary attrs = new Dictionary(); - attrs["dims"] = dims; - attrs["value"] = value; - var result = tf.OpDefLib._apply_op_helper("Fill", name, attrs); - if (execute.must_record_gradient()) + try + { + return batch_to_space_nd_eager_fallback(input, block_shape, crops, name: name, ctx: _ctx); + } + catch (Exception) { - throw new NotImplementedException(); } - return result.output; } - - public static Tensor fill_eager_fallback(Tensor dims, Tensor value, string name, Context ctx) + Dictionary keywords = new(); + keywords["input"] = input; + keywords["block_shape"] = block_shape; + keywords["crops"] = crops; + var _op = tf.OpDefLib._apply_op_helper("BatchToSpaceND", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) { - object[] attrs = new object[] { "T", dims.dtype.as_datatype_enum(), "index_type", dims.dtype.as_datatype_enum() }; - var _result = execute.executes("Fill", 1, new Tensor[] { dims, value }, attrs, ctx, name); + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "Tblock_shape", _op._get_attr_type("Tblock_shape"), "Tcrops", _op._get_attr_type("Tcrops") }; + _execute.record_gradient("BatchToSpaceND", _op.inputs, _attrs, _result); + } + return _result[0]; + } - if (execute.must_record_gradient()) + public static Tensor batch_to_space_nd_eager_fallback(Tensor input, Tensor block_shape, Tensor crops, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, block_shape, crops }; + object[] _attrs = new object[] { "T", input.dtype, "Tblock_shape", block_shape.dtype, "Tcrops", crops.dtype }; + var _result = _execute.execute("BatchToSpaceND", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("BatchToSpaceND", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Bitcasts a tensor from one type to another without copying data. + /// + /// + /// + /// Given a tensor `input`, this operation returns a tensor that has the same buffer + /// data as `input` with datatype `type`. + /// + /// If the input datatype `T` is larger than the output datatype `type` then the + /// shape changes from [...] to [..., sizeof(`T`)/sizeof(`type`)]. + /// + /// If `T` is smaller than `type`, the operator requires that the rightmost + /// dimension be equal to sizeof(`type`)/sizeof(`T`). The shape then goes from + /// [..., sizeof(`type`)/sizeof(`T`)] to [...]. + /// + /// tf.bitcast() and tf.cast() work differently when real dtype is casted as a complex dtype + /// (e.g. tf.complex64 or tf.complex128) as tf.cast() make imaginary part 0 while tf.bitcast() + /// gives module error. + /// For example, + /// + /// Example 1: + /// + /// >>> a = [1., 2., 3.] + /// >>> equality_bitcast = tf.bitcast(a, tf.complex128) + /// Traceback (most recent call last): + /// ... + /// InvalidArgumentError: Cannot bitcast from 1 to 18 [Op:Bitcast] + /// >>> equality_cast = tf.cast(a, tf.complex128) + /// >>> print(equality_cast) + /// tf.Tensor([1.+0.j 2.+0.j 3.+0.j], shape=(3,), dtype=complex128) + /// + /// Example 2: + /// + /// >>> tf.bitcast(tf.constant(0xffffffff, dtype=tf.uint32), tf.uint8) + /// + /// + /// Example 3: + /// + /// >>> x = [1., 2., 3.] + /// >>> y = [0., 2., 3.] + /// >>> equality= tf.equal(x,y) + /// >>> equality_cast = tf.cast(equality,tf.float32) + /// >>> equality_bitcast = tf.bitcast(equality_cast,tf.uint8) + /// >>> print(equality) + /// tf.Tensor([False True True], shape=(3,), dtype=bool) + /// >>> print(equality_cast) + /// tf.Tensor([0. 1. 1.], shape=(3,), dtype=float32) + /// >>> print(equality_bitcast) + /// tf.Tensor( + /// [[ 0 0 0 0] + /// [ 0 0 128 63] + /// [ 0 0 128 63]], shape=(3, 4), dtype=uint8) + /// + /// *NOTE*: Bitcast is implemented as a low-level cast, so machines with different + /// endian orderings will give different results. + /// + /// + /// + /// + /// + public static Tensor bitcast(Tensor input, TF_DataType type, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Bitcast", name) { args = new object[] { input }, attrs = new Dictionary() { ["type"] = type } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return bitcast_eager_fallback(input, type: type, name: name, ctx: _ctx); + } + catch (Exception) { - throw new NotImplementedException(); } - return _result[0]; } - //=> tf.Context.ExecuteOp("Fill", name, new ExecuteOpArgs(dims, value)); - - /// - /// Return the reduction indices for computing gradients of s0 op s1 with broadcast. - /// - /// A `Tensor`. Must be one of the following types: `int32`, `int64`. - /// A `Tensor`. Must have the same type as `s0`. - /// A name for the operation (optional). - /// A tuple of `Tensor` objects (r0, r1). - public static (Tensor, Tensor) broadcast_gradient_args(Tensor s0, Tensor s1, string name = "") + Dictionary keywords = new(); + keywords["input"] = input; + keywords["type"] = type; + var _op = tf.OpDefLib._apply_op_helper("Bitcast", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) { - var results = tf.Context.ExecuteOp("BroadcastGradientArgs", name, new ExecuteOpArgs(s0, s1)); - return (results[0], results[1]); + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "type", _op._get_attr_type("type") }; + _execute.record_gradient("Bitcast", _op.inputs, _attrs, _result); } + return _result[0]; + } - public static Tensor reverse(Tensor tensor, T axis, string name = null) + public static Tensor bitcast_eager_fallback(Tensor input, TF_DataType type, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input }; + object[] _attrs = new object[] { "T", input.dtype, "type", type }; + var _result = _execute.execute("Bitcast", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) { - var _op = tf.OpDefLib._apply_op_helper("ReverseV2", name, new { tensor, axis }); - return _op.output; + _execute.record_gradient("Bitcast", _inputs_flat, _attrs, _result); } - - public static Tensor reshape(Tensor tensor, T shape, string name = null) - => tf.Context.ExecuteOp("Reshape", name, new ExecuteOpArgs(tensor, shape)); - - public static Tensor reshape(Tensor tensor, object[] shape, string name = null) - => tf.Context.ExecuteOp("Reshape", name, new ExecuteOpArgs(tensor, shape)); - - private static Tensor reshape_eager_fallback(Tensor tensor, object[] shape, string name, Context ctx) + return _result[0]; + } + /// + /// Return the shape of s0 op s1 with broadcast. + /// + /// + /// + /// Given `s0` and `s1`, tensors that represent shapes, compute `r0`, the + /// broadcasted shape. `s0`, `s1` and `r0` are all integer vectors. + /// + /// + /// + /// + /// + public static Tensor broadcast_args(Tensor s0, Tensor s1, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) { - var (_attr_T, _input) = tf.Runner.ArgsToMatchingEager(ctx, args: new[] { tensor }); - var (_attr_Tshape, _input_shape) = tf.Runner.ArgsToMatchingEager(ctx, args: new object[] { shape }, default_dtype: TF_DataType.TF_INT32); - var _inputs_flat = new[] { _input[0], _input_shape[0] }; - var _attrs = new object[] { "T", _attr_T, "Tshape", _attr_Tshape }; - - var results = tf.Runner.Execute(ctx, "Reshape", 1, _inputs_flat, _attrs, name: name); - if (tf.Runner.MustRecordGradient()) + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "BroadcastArgs", name) { args = new object[] { s0, s1 }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return broadcast_args_eager_fallback(s0, s1, name: name, ctx: _ctx); + } + catch (Exception) { - tf.Runner.RecordGradient("Reshape", _inputs_flat, _attrs, results); } - return results[0]; } - - /// - /// Finds unique elements in a 1-D tensor. - /// - /// - /// - /// - /// - public static (Tensor, Tensor) unique(Tensor x, TF_DataType out_idx = TF_DataType.TF_INT32, string name = null) + Dictionary keywords = new(); + keywords["s0"] = s0; + keywords["s1"] = s1; + var _op = tf.OpDefLib._apply_op_helper("BroadcastArgs", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) { - var _op = tf.OpDefLib._apply_op_helper("Unique", name, new { x, out_idx }); - // TODO - //var _result = _UniqueOutput._make(_op.outputs); - return (_op.outputs[0], _op.outputs[1]); + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("BroadcastArgs", _op.inputs, _attrs, _result); } + return _result[0]; + } - public static Tensor[] unpack(Tensor value, int num, int axis = 0, string name = null) - => tf.Context.ExecuteOp("Unpack", name, new ExecuteOpArgs(value, num) - .SetAttributes(new { axis, num })); - - public static Tensor where(Tensor condition, string name = null) + public static Tensor broadcast_args_eager_fallback(Tensor s0, Tensor s1, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { s0, s1 }; + object[] _attrs = new object[] { "T", s0.dtype }; + var _result = _execute.execute("BroadcastArgs", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) { - var _op = tf.OpDefLib._apply_op_helper("Where", name, new { input = condition }); - return _op.output; + _execute.record_gradient("BroadcastArgs", _inputs_flat, _attrs, _result); } - - public static Tensor one_hot(Tensor indices, Tensor depth, - Tensor on_value = null, - Tensor off_value = null, - TF_DataType dtype = TF_DataType.DtInvalid, - int axis = -1, - string name = null) - => tf.Context.ExecuteOp("OneHot", name, new ExecuteOpArgs(indices, depth, on_value, off_value) - .SetAttributes(new { axis })); - - /// - /// A placeholder op that passes through `input` when its output is not fed. - /// - /// The default value to produce when output is not fed. - /// - /// - /// - public static Tensor placeholder_with_default(T input, int[] shape, string name = null) + return _result[0]; + } + /// + /// Return the reduction indices for computing gradients of s0 op s1 with broadcast. + /// + /// + /// + /// This is typically used by gradient computations for a broadcasting operation. + /// + /// + /// + /// + /// + public static Tensor[] broadcast_gradient_args(Tensor s0, Tensor s1, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "BroadcastGradientArgs", name) { args = new object[] { s0, s1 }, attrs = new Dictionary() { } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return broadcast_gradient_args_eager_fallback(s0, s1, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["s0"] = s0; + keywords["s1"] = s1; + var _op = tf.OpDefLib._apply_op_helper("BroadcastGradientArgs", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) { - var _op = tf.OpDefLib._apply_op_helper("PlaceholderWithDefault", name, new { input, shape, name }); - return _op.outputs[0]; + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("BroadcastGradientArgs", _op.inputs, _attrs, _result); } + return _result; + } - public static Tensor select(Tensor condition, Tx x, Ty y, string name = null) - => tf.Context.ExecuteOp("Select", name, new ExecuteOpArgs(condition, x, y)); - - public static Tensor select_v2(Tensor condition, Tx x, Ty y, string name = null) - => tf.Context.ExecuteOp("SelectV2", name, new ExecuteOpArgs(condition, x, y)); - - public static Tensor scatter_nd(Tensor indices, Tensor updates, Tensor[] shape, string name = null) + public static Tensor[] broadcast_gradient_args_eager_fallback(Tensor s0, Tensor s1, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { s0, s1 }; + object[] _attrs = new object[] { "T", s0.dtype }; + var _result = _execute.execute("BroadcastGradientArgs", 2, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) { - var _op = tf.OpDefLib._apply_op_helper("ScatterNd", name, new { indices, updates, shape }); - return _op.outputs[0]; + _execute.record_gradient("BroadcastGradientArgs", _inputs_flat, _attrs, _result); } - - public static Tensor shape(Tensor input, TF_DataType out_type = TF_DataType.TF_INT32, string name = null) - => tf.Context.ExecuteOp("Shape", name, new ExecuteOpArgs(input) - .SetAttributes(new { out_type })); - - /// - /// Returns shape of tensors. - /// - /// - /// - /// - /// - public static Tensor[] shape_n(Tensor[] input, TF_DataType out_type = TF_DataType.TF_INT32, string name = null) - => tf.Context.ExecuteOp("ShapeN", name, new ExecuteOpArgs() + return _result; + } + /// + /// Broadcast an array for a compatible shape. + /// + /// + /// + /// Broadcasting is the process of making arrays to have compatible shapes + /// for arithmetic operations. Two shapes are compatible if for each + /// dimension pair they are either equal or one of them is one. + /// + /// For example: + /// + /// >>> x = tf.constant([[1, 2, 3]]) # Shape (1, 3,) + /// >>> y = tf.broadcast_to(x, [2, 3]) + /// >>> print(y) + /// tf.Tensor( + /// [[1 2 3] + /// [1 2 3]], shape=(2, 3), dtype=int32) + /// + /// In the above example, the input Tensor with the shape of `[1, 3]` + /// is broadcasted to output Tensor with shape of `[2, 3]`. + /// + /// When broadcasting, if a tensor has fewer axes than necessary its shape is + /// padded on the left with ones. So this gives the same result as the previous + /// example: + /// + /// >>> x = tf.constant([1, 2, 3]) # Shape (3,) + /// >>> y = tf.broadcast_to(x, [2, 3]) + /// + /// + /// When doing broadcasted operations such as multiplying a tensor + /// by a scalar, broadcasting (usually) confers some time or space + /// benefit, as the broadcasted tensor is never materialized. + /// + /// However, `broadcast_to` does not carry with it any such benefits. + /// The newly-created tensor takes the full memory of the broadcasted + /// shape. (In a graph context, `broadcast_to` might be fused to + /// subsequent operation and then be optimized away, however.) + /// + /// + /// + /// + /// + public static Tensor broadcast_to(Tensor input, Tensor shape, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try { - OpInputArgs = new object[] { input } - }.SetAttributes(new { out_type })); - - public static Tensor size(Tensor input, TF_DataType out_type = TF_DataType.TF_INT32, string name = null) + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "BroadcastTo", name) { args = new object[] { input, shape }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return broadcast_to_eager_fallback(input, shape, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["shape"] = shape; + var _op = tf.OpDefLib._apply_op_helper("BroadcastTo", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) { - var _op = tf.OpDefLib._apply_op_helper("Size", name, new { input, out_type }); - return _op.outputs[0]; + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "Tidx", _op._get_attr_type("Tidx") }; + _execute.record_gradient("BroadcastTo", _op.inputs, _attrs, _result); } + return _result[0]; + } - public static Tensor slice(Tensor input, Tensor[] begin, Tensor[] size, string name = null) + public static Tensor broadcast_to_eager_fallback(Tensor input, Tensor shape, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, shape }; + object[] _attrs = new object[] { "T", input.dtype, "Tidx", shape.dtype }; + var _result = _execute.execute("BroadcastTo", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("BroadcastTo", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Checks a tensor for NaN and Inf values. + /// + /// + /// + /// When run, reports an `InvalidArgument` error if `tensor` has any values + /// that are not a number (NaN) or infinity (Inf). Otherwise, returns the input + /// tensor. + /// + /// Example usage: + /// + /// ``` python + /// a = tf.Variable(1.0) + /// tf.debugging.check_numerics(a, message='') + /// + /// b = tf.Variable(np.nan) + /// try: + /// tf.debugging.check_numerics(b, message='Checking b') + /// except Exception as e: + /// assert "Checking b : Tensor had NaN values" in e.message + /// + /// c = tf.Variable(np.inf) + /// try: + /// tf.debugging.check_numerics(c, message='Checking c') + /// except Exception as e: + /// assert "Checking c : Tensor had Inf values" in e.message + /// ``` + /// + /// + /// + /// + /// + /// + /// Prefix of the error message. + /// + /// + /// + public static Tensor check_numerics(Tensor tensor, string message, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) { - if (tf.executing_eagerly()) + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "CheckNumerics", name) { args = new object[] { tensor }, attrs = new Dictionary() { ["message"] = message } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return check_numerics_eager_fallback(tensor, message: message, name: name, ctx: _ctx); + } + catch (Exception) { - var result = slice_eager_fallback(input, begin, size, name, tf.Context); - return result; } - - var _op = tf.OpDefLib._apply_op_helper("Slice", name, new { input, begin, size }); - return _op.outputs[0]; } - - private static Tensor slice_eager_fallback(Tensor inputs, Tensor[] begin, Tensor[] size, string name, Context ctx) + Dictionary keywords = new(); + keywords["tensor"] = tensor; + keywords["message"] = message; + var _op = tf.OpDefLib._apply_op_helper("CheckNumerics", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) { - var (_attr_T, input) = tf.Runner.ArgsToMatchingEager(ctx, args: new[] { inputs }); - var (_attr_Tidx, _inputs_Index) = tf.Runner.ArgsToMatchingEager(ctx, args: new object[] { begin, size }); - var _inputs_flat = input.concat(_inputs_Index); - var _attrs = new object[] { "T", _attr_T, "Index", _attr_Tidx }; + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "message", _op.get_attr("message") }; + _execute.record_gradient("CheckNumerics", _op.inputs, _attrs, _result); + } + return _result[0]; + } - var results = tf.Runner.Execute(ctx, "Slice", 1, _inputs_flat, _attrs, name: name); - if (tf.Runner.MustRecordGradient()) + public static Tensor check_numerics_eager_fallback(Tensor tensor, string message, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { tensor }; + object[] _attrs = new object[] { "T", tensor.dtype, "message", message }; + var _result = _execute.execute("CheckNumerics", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("CheckNumerics", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Checks a tensor for NaN, -Inf and +Inf values. + /// + /// + /// + /// When run, reports an `InvalidArgument` error if `tensor` has any values + /// that are not a number (NaN) or infinity (Inf). Otherwise, returns the input + /// tensor. Unlike CheckNumerics (V1), CheckNumericsV2 distinguishes -Inf and +Inf + /// in the errors it throws. + /// + /// + /// + /// + /// + /// Prefix of the error message. + /// + /// + /// + public static Tensor check_numerics_v2(Tensor tensor, string message, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "CheckNumericsV2", name) { args = new object[] { tensor }, attrs = new Dictionary() { ["message"] = message } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return check_numerics_v2_eager_fallback(tensor, message: message, name: name, ctx: _ctx); + } + catch (Exception) { - tf.Runner.RecordGradient("Slice", _inputs_flat, _attrs, results); } - return results[0]; } + Dictionary keywords = new(); + keywords["tensor"] = tensor; + keywords["message"] = message; + var _op = tf.OpDefLib._apply_op_helper("CheckNumericsV2", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "message", _op.get_attr("message") }; + _execute.record_gradient("CheckNumericsV2", _op.inputs, _attrs, _result); + } + return _result[0]; + } - public static Tensor slice(Tensor input, Tb begin, Ts size, string name = null) + public static Tensor check_numerics_v2_eager_fallback(Tensor tensor, string message, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { tensor }; + object[] _attrs = new object[] { "T", tensor.dtype, "message", message }; + var _result = _execute.execute("CheckNumericsV2", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) { - if (tf.executing_eagerly()) + _execute.record_gradient("CheckNumericsV2", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Concatenates tensors along one dimension. + /// + /// + /// + /// + public static Tensor concat(Tensor concat_dim, Tensors values, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Concat", name) { args = new object[] { concat_dim, values }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return concat_eager_fallback(concat_dim, values, name: name, ctx: _ctx); + } + catch (Exception) { - var outputs = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo("Slice", name, input, begin, size)); - return outputs[0]; } - - var _op = tf.OpDefLib._apply_op_helper("Slice", name, new { input, begin, size }); - return _op.outputs[0]; } - - public static Tensor[] split_v(Tensor value, Tensor size_splits, - int axis, int num_split, string name = null) - => tf.Context.ExecuteOp("SplitV", name, new ExecuteOpArgs(value, size_splits, axis) - .SetAttributes(new { num_split })); - - public static Tensor tile(Tensor input, Tensor multiples, string name = null) - => tf.Context.ExecuteOp("Tile", name, new ExecuteOpArgs(input, multiples)); - - public static Tensor tile(Tensor input, object[] multiples, string name = null) - => tf.Context.ExecuteOp("Tile", name, new ExecuteOpArgs(input, multiples)); - - public static Tensor transpose(Tensor x, T1 perm, string name = null) - => tf.Context.ExecuteOp("Transpose", name, new ExecuteOpArgs(x, perm)); - - public static Tensor ones_like(Tensor x, string name = null) - => tf.Context.ExecuteOp("OnesLike", name, new ExecuteOpArgs(x)); - - public static Tensor zeros_like(Tensor x, string name = null) - => tf.Context.ExecuteOp("ZerosLike", name, new ExecuteOpArgs(x)); - - public static Tensor stop_gradient(Tensor x, string name = null) + Dictionary keywords = new(); + keywords["concat_dim"] = concat_dim; + keywords["values"] = values; + var _op = tf.OpDefLib._apply_op_helper("Concat", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) { - var _op = tf.OpDefLib._apply_op_helper("StopGradient", name, args: new { input = x, name }); - - return _op.output; + object[] _attrs = new object[] { "N", _op._get_attr_int("N"), "T", _op._get_attr_type("T") }; + _execute.record_gradient("Concat", _op.inputs, _attrs, _result); } + return _result[0]; + } - public static Tensor strided_slice(Tensor input, Tensor begin, Tensor end, Tensor strides, - long begin_mask = 0, - long end_mask = 0, - long ellipsis_mask = 0, - long new_axis_mask = 0, - long shrink_axis_mask = 0, - string name = null) - => tf.Context.ExecuteOp("StridedSlice", name, new ExecuteOpArgs(input, begin, end, strides) - .SetAttributes(new - { - begin_mask, - end_mask, - ellipsis_mask, - new_axis_mask, - shrink_axis_mask - })); - - public static Tensor resource_strided_slice_assign(Tensor input, Tensor begin, Tensor end, Tensor strides, Tensor value, - int begin_mask = 0, - int end_mask = 0, - int ellipsis_mask = 0, - int new_axis_mask = 0, - int shrink_axis_mask = 0, - string name = null) - => tf.Context.ExecuteOp("ResourceStridedSliceAssign", name, new ExecuteOpArgs(input, begin, end, strides, value) - .SetAttributes(new - { - begin_mask, - end_mask, - ellipsis_mask, - new_axis_mask, - shrink_axis_mask - })); - - public static Tensor strided_slice(Tensor input, T[] begin, T[] end, T[] strides, - int begin_mask = 0, - int end_mask = 0, - int ellipsis_mask = 0, - int new_axis_mask = 0, - int shrink_axis_mask = 0, - string name = null) - { - var _op = tf.OpDefLib._apply_op_helper("StridedSlice", name, new - { - input, - begin, - end, - strides, - begin_mask, - end_mask, - ellipsis_mask, - new_axis_mask, - shrink_axis_mask - }); - - return _op.outputs[0]; - } - - /// - /// Removes dimensions of size 1 from the shape of a tensor. - /// Given a tensor `input`, this operation returns a tensor of the same type with - /// all dimensions of size 1 removed.If you don't want to remove all size 1 - /// dimensions, you can remove specific size 1 dimensions by specifying - /// `axis`. - /// - /// A `Tensor`. The `input` to squeeze. - /// An optional list of `ints`. Defaults to `[]`. If specified, only squeezes the dimensions listed. - /// A name for the operation (optional). - /// A `Tensor`. Has the same type as `input`. - public static Tensor squeeze(Tensor input, int[] axis = null, string name = null) - => tf.Context.ExecuteOp("Squeeze", name, new ExecuteOpArgs(input) - .SetAttributes(new { squeeze_dims = axis })); - - /// - /// Return the shape of s0 op s1 with broadcast. - /// Given `s0` and `s1`, tensors that represent shapes, compute `r0`, the - /// broadcasted shape. `s0`, `s1` and `r0` are all integer vectors. - /// - /// A `Tensor`. Must be one of the following types: `int32`, `int64`. - /// A `Tensor`. Must have the same type as `s0`. - /// A name for the operation (optional). - /// `Tensor`. Has the same type as `s0`. - public static Tensor broadcast_args(Tensor s0, Tensor s1, string name = null) - => tf.Context.ExecuteOp("BroadcastArgs", name, new ExecuteOpArgs(s0, s1)); - - /// - /// Broadcast an array for a compatible shape. - /// - /// - /// - /// - /// - public static Tensor broadcast_to(Tensor input, T shape, string name = null) - => tf.Context.ExecuteOp("BroadcastTo", name, new ExecuteOpArgs(input, shape)); + public static Tensor concat_eager_fallback(Tensor concat_dim, Tensors values, string name, Context ctx) + { + List _inputs_flat_list = new(); + _inputs_flat_list.Add(concat_dim); + _inputs_flat_list.AddRange(values); + var _inputs_flat = _inputs_flat_list.ToArray(); + object[] _attrs = new object[] { "N", values.Length, "T", values.dtype }; + var _result = _execute.execute("Concat", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Concat", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes offsets of concat inputs within its output. + /// + /// + /// + /// For example: + /// + /// >>> x = [2, 2, 7] + /// >>> y = [2, 3, 7] + /// >>> z = [2, 9, 7] + /// >>> offsets = concat_offset(1, [x, y, z]) + /// >>> [list(off.numpy()) for off in offsets] + /// [[0, 0, 0], [0, 2, 0], [0, 5, 0]] + /// + /// This is typically used by gradient computations for a concat operation. + /// + /// + /// + /// + /// + public static Tensor[] concat_offset(Tensor concat_dim, Tensors shape, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "ConcatOffset", name) { args = new object[] { concat_dim, shape }, attrs = new Dictionary() { } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return concat_offset_eager_fallback(concat_dim, shape, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["concat_dim"] = concat_dim; + keywords["shape"] = shape; + var _op = tf.OpDefLib._apply_op_helper("ConcatOffset", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "N", _op._get_attr_int("N") }; + _execute.record_gradient("ConcatOffset", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] concat_offset_eager_fallback(Tensor concat_dim, Tensors shape, string name, Context ctx) + { + List _inputs_flat_list = new(); + _inputs_flat_list.Add(concat_dim); + _inputs_flat_list.AddRange(shape); + var _inputs_flat = _inputs_flat_list.ToArray(); + object[] _attrs = new object[] { "N", shape.Length }; + var _result = _execute.execute("ConcatOffset", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("ConcatOffset", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// Concatenates tensors along one dimension. + /// + /// + /// + /// + public static Tensor concat_v2(Tensors values, Tensor axis, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "ConcatV2", name) { args = new object[] { values, axis }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return concat_v2_eager_fallback(values, axis, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["values"] = values; + keywords["axis"] = axis; + var _op = tf.OpDefLib._apply_op_helper("ConcatV2", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "N", _op._get_attr_int("N"), "T", _op._get_attr_type("T"), "Tidx", _op._get_attr_type("Tidx") }; + _execute.record_gradient("ConcatV2", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor concat_v2_eager_fallback(Tensors values, Tensor axis, string name, Context ctx) + { + List _inputs_flat_list = new(); + _inputs_flat_list.AddRange(values); + _inputs_flat_list.Add(axis); + var _inputs_flat = _inputs_flat_list.ToArray(); + object[] _attrs = new object[] { "N", values.Length, "T", values.dtype, "Tidx", axis.dtype }; + var _result = _execute.execute("ConcatV2", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("ConcatV2", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Shuffle dimensions of x according to a permutation and conjugate the result. + /// + /// + /// + /// The output `y` has the same rank as `x`. The shapes of `x` and `y` satisfy: + /// `y.shape[i] == x.shape[perm[i]] for i in [0, 1, ..., rank(x) - 1]` + /// `y[i,j,k,...,s,t,u] == conj(x[perm[i], perm[j], perm[k],...,perm[s], perm[t], perm[u]])` + /// + /// + /// + /// + /// + public static Tensor conjugate_transpose(Tensor x, Tensor perm, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "ConjugateTranspose", name) { args = new object[] { x, perm }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return conjugate_transpose_eager_fallback(x, perm, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + keywords["perm"] = perm; + var _op = tf.OpDefLib._apply_op_helper("ConjugateTranspose", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "Tperm", _op._get_attr_type("Tperm") }; + _execute.record_gradient("ConjugateTranspose", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor conjugate_transpose_eager_fallback(Tensor x, Tensor perm, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x, perm }; + object[] _attrs = new object[] { "T", x.dtype, "Tperm", perm.dtype }; + var _result = _execute.execute("ConjugateTranspose", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("ConjugateTranspose", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns a constant tensor. + /// + /// + /// + /// Attr `value` is the tensor to return. + /// + /// + /// + /// + public static Tensor _const(TensorProto value, TF_DataType dtype, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Const", name) { args = new object[] { }, attrs = new Dictionary() { ["value"] = value, ["dtype"] = dtype } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return const_eager_fallback(value: value, dtype: dtype, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["value"] = value; + keywords["dtype"] = dtype; + var _op = tf.OpDefLib._apply_op_helper("Const", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "value", _op.get_attr("value"), "dtype", _op._get_attr_type("dtype") }; + _execute.record_gradient("Const", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor const_eager_fallback(TensorProto value, TF_DataType dtype, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { }; + object[] _attrs = new object[] { "value", value, "dtype", dtype }; + var _result = _execute.execute("Const", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Const", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Identity op for gradient debugging. + /// + /// + /// + /// This op is hidden from public in Python. It is used by TensorFlow Debugger to + /// register gradient tensors for gradient debugging. + /// This op operates on non-reference-type tensors. + /// + /// + /// + /// + public static Tensor debug_gradient_identity(Tensor input, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "DebugGradientIdentity", name) { args = new object[] { input }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return debug_gradient_identity_eager_fallback(input, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + var _op = tf.OpDefLib._apply_op_helper("DebugGradientIdentity", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("DebugGradientIdentity", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor debug_gradient_identity_eager_fallback(Tensor input, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input }; + object[] _attrs = new object[] { "T", input.dtype }; + var _result = _execute.execute("DebugGradientIdentity", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("DebugGradientIdentity", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Identity op for gradient debugging. + /// + /// + /// + /// This op is hidden from public in Python. It is used by TensorFlow Debugger to + /// register gradient tensors for gradient debugging. + /// This op operates on reference-type tensors. + /// + /// + /// + /// + public static Tensor debug_gradient_ref_identity(Tensor input, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + throw new RuntimeError("debug_gradient_ref_identity op does not support eager execution. Arg input is a ref."); + } + Dictionary keywords = new(); + keywords["input"] = input; + var _op = tf.OpDefLib._apply_op_helper("DebugGradientRefIdentity", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("DebugGradientRefIdentity", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor debug_gradient_ref_identity_eager_fallback(Tensor input, string name, Context ctx) + { + throw new RuntimeError($"debug_gradient_ref_identity op does not support eager execution. Arg 'input' is a ref."); + } + /// + /// Makes a copy of `x`. + /// + /// + /// + public static Tensor deep_copy(Tensor x, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "DeepCopy", name) { args = new object[] { x }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return deep_copy_eager_fallback(x, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + var _op = tf.OpDefLib._apply_op_helper("DeepCopy", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("DeepCopy", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor deep_copy_eager_fallback(Tensor x, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("DeepCopy", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("DeepCopy", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// DepthToSpace for tensors of type T. + /// + /// + /// + /// Rearranges data from depth into blocks of spatial data. + /// This is the reverse transformation of SpaceToDepth. More specifically, + /// this op outputs a copy of the input tensor where values from the `depth` + /// dimension are moved in spatial blocks to the `height` and `width` dimensions. + /// The attr `block_size` indicates the input block size and how the data is moved. + /// + /// * Chunks of data of size `block_size * block_size` from depth are rearranged + /// into non-overlapping blocks of size `block_size x block_size` + /// * The width of the output tensor is `input_depth * block_size`, whereas the + /// height is `input_height * block_size`. + /// * The Y, X coordinates within each block of the output image are determined + /// by the high order component of the input channel index. + /// * The depth of the input tensor must be divisible by + /// `block_size * block_size`. + /// + /// The `data_format` attr specifies the layout of the input and output tensors + /// with the following options: + /// "NHWC": `[ batch, height, width, channels ]` + /// "NCHW": `[ batch, channels, height, width ]` + /// "NCHW_VECT_C": + /// `qint8 [ batch, channels / 4, height, width, 4 ]` + /// + /// It is useful to consider the operation as transforming a 6-D Tensor. + /// e.g. for data_format = NHWC, + /// Each element in the input tensor can be specified via 6 coordinates, + /// ordered by decreasing memory layout significance as: + /// n,iY,iX,bY,bX,oC (where n=batch index, iX, iY means X or Y coordinates + /// within the input image, bX, bY means coordinates + /// within the output block, oC means output channels). + /// The output would be the input transposed to the following layout: + /// n,iY,bY,iX,bX,oC + /// + /// This operation is useful for resizing the activations between convolutions + /// (but keeping all data), e.g. instead of pooling. It is also useful for training + /// purely convolutional models. + /// + /// For example, given an input of shape `[1, 1, 1, 4]`, data_format = "NHWC" and + /// block_size = 2: + /// + /// ``` + /// x = [[[[1, 2, 3, 4]]]] + /// + /// ``` + /// + /// This operation will output a tensor of shape `[1, 2, 2, 1]`: + /// + /// ``` + /// [[[[1], [2]], + /// [[3], [4]]]] + /// ``` + /// + /// Here, the input has a batch of 1 and each batch element has shape `[1, 1, 4]`, + /// the corresponding output will have 2x2 elements and will have a depth of + /// 1 channel (1 = `4 / (block_size * block_size)`). + /// The output element shape is `[2, 2, 1]`. + /// + /// For an input tensor with larger depth, here of shape `[1, 1, 1, 12]`, e.g. + /// + /// ``` + /// x = [[[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]]]] + /// ``` + /// + /// This operation, for block size of 2, will return the following tensor of shape + /// `[1, 2, 2, 3]` + /// + /// ``` + /// [[[[1, 2, 3], [4, 5, 6]], + /// [[7, 8, 9], [10, 11, 12]]]] + /// + /// ``` + /// + /// Similarly, for the following input of shape `[1 2 2 4]`, and a block size of 2: + /// + /// ``` + /// x = [[[[1, 2, 3, 4], + /// [5, 6, 7, 8]], + /// [[9, 10, 11, 12], + /// [13, 14, 15, 16]]]] + /// ``` + /// + /// the operator will return the following tensor of shape `[1 4 4 1]`: + /// + /// ``` + /// x = [[[ [1], [2], [5], [6]], + /// [ [3], [4], [7], [8]], + /// [ [9], [10], [13], [14]], + /// [ [11], [12], [15], [16]]]] + /// + /// ``` + /// + /// + /// + /// + /// + /// The size of the spatial block, same as in Space2Depth. + /// + /// + /// + /// + public static Tensor depth_to_space(Tensor input, int block_size = 0, string data_format = "NHWC", string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "DepthToSpace", name) { args = new object[] { input }, attrs = new Dictionary() { ["block_size"] = block_size, ["data_format"] = data_format } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return depth_to_space_eager_fallback(input, block_size: block_size, data_format: data_format, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + if (data_format is null) + { + data_format = "NHWC"; + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["block_size"] = block_size; + keywords["data_format"] = data_format; + var _op = tf.OpDefLib._apply_op_helper("DepthToSpace", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "block_size", _op._get_attr_int("block_size"), "data_format", _op.get_attr("data_format") }; + _execute.record_gradient("DepthToSpace", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor depth_to_space_eager_fallback(Tensor input, int block_size, string data_format, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input }; + object[] _attrs = new object[] { "T", input.dtype, "block_size", block_size, "data_format", data_format }; + var _result = _execute.execute("DepthToSpace", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("DepthToSpace", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Dequantize the 'input' tensor into a float or bfloat16 Tensor. + /// + /// + /// + /// [min_range, max_range] are scalar floats that specify the range for + /// the output. The 'mode' attribute controls exactly which calculations are + /// used to convert the float values to their quantized equivalents. + /// + /// In 'MIN_COMBINED' mode, each value of the tensor will undergo the following: + /// + /// ``` + /// if T == qint8: in[i] += (range(T) + 1)/ 2.0 + /// out[i] = min_range + (in[i]* (max_range - min_range) / range(T)) + /// ``` + /// here `range(T) = numeric_limits::max() - numeric_limits::min()` + /// + /// *MIN_COMBINED Mode Example* + /// + /// If the input comes from a QuantizedRelu6, the output type is + /// quint8 (range of 0-255) but the possible range of QuantizedRelu6 is + /// 0-6. The min_range and max_range values are therefore 0.0 and 6.0. + /// Dequantize on quint8 will take each value, cast to float, and multiply + /// by 6 / 255. + /// Note that if quantizedtype is qint8, the operation will additionally add + /// each value by 128 prior to casting. + /// + /// If the mode is 'MIN_FIRST', then this approach is used: + /// + /// ```c++ + /// num_discrete_values = 1 << (# of bits in T) + /// range_adjust = num_discrete_values / (num_discrete_values - 1) + /// range = (range_max - range_min) * range_adjust + /// range_scale = range / num_discrete_values + /// const double offset_input = static_cast(input) - lowest_quantized; + /// result = range_min + ((input - numeric_limits::min()) * range_scale) + /// ``` + /// + /// If the mode is `SCALED`, dequantization is performed by multiplying each + /// input value by a scaling_factor. (Thus an input of 0 always maps to 0.0). + /// + /// The scaling_factor is determined from `min_range`, `max_range`, and + /// `narrow_range` in a way that is compatible with `QuantizeAndDequantize{V2|V3}` + /// and `QuantizeV2`, using the following algorithm: + /// + /// ```c++ + /// + /// const int min_expected_T = std::numeric_limits::min() + + /// (narrow_range ? 1 : 0); + /// const int max_expected_T = std::numeric_limits::max(); + /// const float max_expected_T = std::numeric_limits::max(); + /// + /// const float scale_factor = + /// (std::numeric_limits::min() == 0) ? (max_range / max_expected_T) + /// : std::max(min_range / min_expected_T, + /// max_range / max_expected_T); + /// ``` + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// Type of the output tensor. Currently Dequantize supports float and bfloat16. + /// If 'dtype' is 'bfloat16', it only supports 'MIN_COMBINED' mode. + /// + /// + /// + public static Tensor dequantize(Tensor input, Tensor min_range, Tensor max_range, string mode = "MIN_COMBINED", bool narrow_range = false, int axis = -1, TF_DataType dtype = TF_DataType.TF_FLOAT, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Dequantize", name) { args = new object[] { input, min_range, max_range }, attrs = new Dictionary() { ["mode"] = mode, ["narrow_range"] = narrow_range, ["axis"] = axis, ["dtype"] = dtype } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return dequantize_eager_fallback(input, min_range, max_range, mode: mode, narrow_range: narrow_range, axis: axis, dtype: dtype, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + if (mode is null) + { + mode = "MIN_COMBINED"; + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["min_range"] = min_range; + keywords["max_range"] = max_range; + keywords["mode"] = mode; + keywords["narrow_range"] = narrow_range; + keywords["axis"] = axis; + keywords["dtype"] = dtype; + var _op = tf.OpDefLib._apply_op_helper("Dequantize", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "mode", _op.get_attr("mode"), "narrow_range", _op._get_attr_bool("narrow_range"), "axis", _op._get_attr_int("axis"), "dtype", _op._get_attr_type("dtype") }; + _execute.record_gradient("Dequantize", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor dequantize_eager_fallback(Tensor input, Tensor min_range, Tensor max_range, string mode, bool narrow_range, int axis, TF_DataType dtype, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, min_range, max_range }; + object[] _attrs = new object[] { "T", input.dtype, "mode", mode, "narrow_range", narrow_range, "axis", axis, "dtype", dtype }; + var _result = _execute.execute("Dequantize", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Dequantize", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns a diagonal tensor with a given diagonal values. + /// + /// + /// + /// Given a `diagonal`, this operation returns a tensor with the `diagonal` and + /// everything else padded with zeros. The diagonal is computed as follows: + /// + /// Assume `diagonal` has dimensions [D1,..., Dk], then the output is a tensor of + /// rank 2k with dimensions [D1,..., Dk, D1,..., Dk] where: + /// + /// `output[i1,..., ik, i1,..., ik] = diagonal[i1, ..., ik]` and 0 everywhere else. + /// + /// For example: + /// + /// ``` + /// # 'diagonal' is [1, 2, 3, 4] + /// tf.diag(diagonal) ==> [[1, 0, 0, 0] + /// [0, 2, 0, 0] + /// [0, 0, 3, 0] + /// [0, 0, 0, 4]] + /// ``` + /// + /// + /// + /// + public static Tensor diag(Tensor diagonal, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Diag", name) { args = new object[] { diagonal }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return diag_eager_fallback(diagonal, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["diagonal"] = diagonal; + var _op = tf.OpDefLib._apply_op_helper("Diag", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Diag", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor diag_eager_fallback(Tensor diagonal, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { diagonal }; + object[] _attrs = new object[] { "T", diagonal.dtype }; + var _result = _execute.execute("Diag", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Diag", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns the diagonal part of the tensor. + /// + /// + /// + /// This operation returns a tensor with the `diagonal` part + /// of the `input`. The `diagonal` part is computed as follows: + /// + /// Assume `input` has dimensions `[D1,..., Dk, D1,..., Dk]`, then the output is a + /// tensor of rank `k` with dimensions `[D1,..., Dk]` where: + /// + /// `diagonal[i1,..., ik] = input[i1, ..., ik, i1,..., ik]`. + /// + /// For example: + /// + /// ``` + /// # 'input' is [[1, 0, 0, 0] + /// [0, 2, 0, 0] + /// [0, 0, 3, 0] + /// [0, 0, 0, 4]] + /// + /// tf.diag_part(input) ==> [1, 2, 3, 4] + /// ``` + /// + /// + /// + /// + public static Tensor diag_part(Tensor input, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "DiagPart", name) { args = new object[] { input }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return diag_part_eager_fallback(input, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + var _op = tf.OpDefLib._apply_op_helper("DiagPart", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("DiagPart", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor diag_part_eager_fallback(Tensor input, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input }; + object[] _attrs = new object[] { "T", input.dtype }; + var _result = _execute.execute("DiagPart", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("DiagPart", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes the (possibly normalized) Levenshtein Edit Distance. + /// + /// + /// + /// The inputs are variable-length sequences provided by SparseTensors + /// (hypothesis_indices, hypothesis_values, hypothesis_shape) + /// and + /// (truth_indices, truth_values, truth_shape). + /// + /// The inputs are: + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// boolean (if true, edit distances are normalized by length of truth). + /// + /// The output is: + /// + /// + /// + public static Tensor edit_distance(Tensor hypothesis_indices, Tensor hypothesis_values, Tensor hypothesis_shape, Tensor truth_indices, Tensor truth_values, Tensor truth_shape, bool normalize = true, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "EditDistance", name) { args = new object[] { hypothesis_indices, hypothesis_values, hypothesis_shape, truth_indices, truth_values, truth_shape }, attrs = new Dictionary() { ["normalize"] = normalize } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return edit_distance_eager_fallback(hypothesis_indices, hypothesis_values, hypothesis_shape, truth_indices, truth_values, truth_shape, normalize: normalize, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["hypothesis_indices"] = hypothesis_indices; + keywords["hypothesis_values"] = hypothesis_values; + keywords["hypothesis_shape"] = hypothesis_shape; + keywords["truth_indices"] = truth_indices; + keywords["truth_values"] = truth_values; + keywords["truth_shape"] = truth_shape; + keywords["normalize"] = normalize; + var _op = tf.OpDefLib._apply_op_helper("EditDistance", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "normalize", _op._get_attr_bool("normalize"), "T", _op._get_attr_type("T") }; + _execute.record_gradient("EditDistance", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor edit_distance_eager_fallback(Tensor hypothesis_indices, Tensor hypothesis_values, Tensor hypothesis_shape, Tensor truth_indices, Tensor truth_values, Tensor truth_shape, bool normalize, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { hypothesis_indices, hypothesis_values, hypothesis_shape, truth_indices, truth_values, truth_shape }; + object[] _attrs = new object[] { "normalize", normalize, "T", hypothesis_values.dtype }; + var _result = _execute.execute("EditDistance", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("EditDistance", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// + /// + /// + /// + /// + /// + public static Tensor empty(Tensor shape, TF_DataType dtype, bool init = false, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Empty", name) { args = new object[] { shape }, attrs = new Dictionary() { ["dtype"] = dtype, ["init"] = init } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return empty_eager_fallback(shape, dtype: dtype, init: init, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["shape"] = shape; + keywords["dtype"] = dtype; + keywords["init"] = init; + var _op = tf.OpDefLib._apply_op_helper("Empty", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "dtype", _op._get_attr_type("dtype"), "init", _op._get_attr_bool("init") }; + _execute.record_gradient("Empty", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor empty_eager_fallback(Tensor shape, TF_DataType dtype, bool init, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { shape }; + object[] _attrs = new object[] { "dtype", dtype, "init", init }; + var _result = _execute.execute("Empty", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Empty", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Ensures that the tensor's shape matches the expected shape. + /// + /// + /// + /// Raises an error if the input tensor's shape does not match the specified shape. + /// Returns the input tensor otherwise. + /// + /// + /// + /// + /// + /// The expected (possibly partially specified) shape of the input tensor. + /// + /// + /// + public static Tensor ensure_shape(Tensor input, Shape shape, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "EnsureShape", name) { args = new object[] { input }, attrs = new Dictionary() { ["shape"] = shape } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return ensure_shape_eager_fallback(input, shape: shape, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["shape"] = shape; + var _op = tf.OpDefLib._apply_op_helper("EnsureShape", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "shape", _op.get_attr("shape"), "T", _op._get_attr_type("T") }; + _execute.record_gradient("EnsureShape", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor ensure_shape_eager_fallback(Tensor input, Shape shape, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input }; + object[] _attrs = new object[] { "shape", shape, "T", input.dtype }; + var _result = _execute.execute("EnsureShape", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("EnsureShape", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Inserts a dimension of 1 into a tensor's shape. + /// + /// + /// + /// Given a tensor `input`, this operation inserts a dimension of 1 at the + /// dimension index `dim` of `input`'s shape. The dimension index `dim` starts at + /// zero; if you specify a negative number for `dim` it is counted backward from + /// the end. + /// + /// This operation is useful if you want to add a batch dimension to a single + /// element. For example, if you have a single image of shape `[height, width, + /// channels]`, you can make it a batch of 1 image with `expand_dims(image, 0)`, + /// which will make the shape `[1, height, width, channels]`. + /// + /// Other examples: + /// + /// ``` + /// # 't' is a tensor of shape [2] + /// shape(expand_dims(t, 0)) ==> [1, 2] + /// shape(expand_dims(t, 1)) ==> [2, 1] + /// shape(expand_dims(t, -1)) ==> [2, 1] + /// + /// # 't2' is a tensor of shape [2, 3, 5] + /// shape(expand_dims(t2, 0)) ==> [1, 2, 3, 5] + /// shape(expand_dims(t2, 2)) ==> [2, 3, 1, 5] + /// shape(expand_dims(t2, 3)) ==> [2, 3, 5, 1] + /// ``` + /// + /// This operation requires that: + /// + /// `-1-input.dims() <= dim <= input.dims()` + /// + /// This operation is related to `squeeze()`, which removes dimensions of + /// size 1. + /// + /// + /// + /// + /// + public static Tensor expand_dims(Tensor input, Tensor dim, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "ExpandDims", name) { args = new object[] { input, dim }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return expand_dims_eager_fallback(input, dim, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["dim"] = dim; + var _op = tf.OpDefLib._apply_op_helper("ExpandDims", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "Tdim", _op._get_attr_type("Tdim") }; + _execute.record_gradient("ExpandDims", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor expand_dims_eager_fallback(Tensor input, Tensor dim, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, dim }; + object[] _attrs = new object[] { "T", input.dtype, "Tdim", dim.dtype }; + var _result = _execute.execute("ExpandDims", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("ExpandDims", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Extract `patches` from `images` and put them in the "depth" output dimension. + /// + /// + /// + /// + /// The size of the sliding window for each dimension of `images`. + /// + /// + /// + /// + /// How far the centers of two consecutive patches are in + /// the images. Must be: `[1, stride_rows, stride_cols, 1]`. + /// + /// + /// + /// + /// Must be: `[1, rate_rows, rate_cols, 1]`. This is the + /// input stride, specifying how far two consecutive patch samples are in the + /// input. Equivalent to extracting patches with + /// `patch_sizes_eff = patch_sizes + (patch_sizes - 1) * (rates - 1)`, followed by + /// subsampling them spatially by a factor of `rates`. This is equivalent to + /// `rate` in dilated (a.k.a. Atrous) convolutions. + /// + /// + /// + /// + /// The type of padding algorithm to use. + /// + /// + /// + public static Tensor extract_image_patches(Tensor images, int[] ksizes, int[] strides, int[] rates, string padding, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "ExtractImagePatches", name) { args = new object[] { images }, attrs = new Dictionary() { ["ksizes"] = ksizes, ["strides"] = strides, ["rates"] = rates, ["padding"] = padding } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return extract_image_patches_eager_fallback(images, ksizes: ksizes, strides: strides, rates: rates, padding: padding, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["images"] = images; + keywords["ksizes"] = ksizes; + keywords["strides"] = strides; + keywords["rates"] = rates; + keywords["padding"] = padding; + var _op = tf.OpDefLib._apply_op_helper("ExtractImagePatches", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "ksizes", _op.get_attr("ksizes"), "strides", _op.get_attr("strides"), "rates", _op.get_attr("rates"), "T", _op._get_attr_type("T"), "padding", _op.get_attr("padding") }; + _execute.record_gradient("ExtractImagePatches", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor extract_image_patches_eager_fallback(Tensor images, int[] ksizes, int[] strides, int[] rates, string padding, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { images }; + object[] _attrs = new object[] { "ksizes", ksizes, "strides", strides, "rates", rates, "T", images.dtype, "padding", padding }; + var _result = _execute.execute("ExtractImagePatches", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("ExtractImagePatches", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Extract `patches` from `input` and put them in the `"depth"` output dimension. 3D extension of `extract_image_patches`. + /// + /// + /// + /// + /// The size of the sliding window for each dimension of `input`. + /// + /// + /// + /// + /// 1-D of length 5. How far the centers of two consecutive patches are in + /// `input`. Must be: `[1, stride_planes, stride_rows, stride_cols, 1]`. + /// + /// + /// + /// + /// The type of padding algorithm to use. + /// + /// The size-related attributes are specified as follows: + /// + /// ```python + /// ksizes = [1, ksize_planes, ksize_rows, ksize_cols, 1] + /// strides = [1, stride_planes, strides_rows, strides_cols, 1] + /// ``` + /// + /// + /// + public static Tensor extract_volume_patches(Tensor input, int[] ksizes, int[] strides, string padding, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "ExtractVolumePatches", name) { args = new object[] { input }, attrs = new Dictionary() { ["ksizes"] = ksizes, ["strides"] = strides, ["padding"] = padding } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return extract_volume_patches_eager_fallback(input, ksizes: ksizes, strides: strides, padding: padding, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["ksizes"] = ksizes; + keywords["strides"] = strides; + keywords["padding"] = padding; + var _op = tf.OpDefLib._apply_op_helper("ExtractVolumePatches", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "ksizes", _op.get_attr("ksizes"), "strides", _op.get_attr("strides"), "T", _op._get_attr_type("T"), "padding", _op.get_attr("padding") }; + _execute.record_gradient("ExtractVolumePatches", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor extract_volume_patches_eager_fallback(Tensor input, int[] ksizes, int[] strides, string padding, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input }; + object[] _attrs = new object[] { "ksizes", ksizes, "strides", strides, "T", input.dtype, "padding", padding }; + var _result = _execute.execute("ExtractVolumePatches", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("ExtractVolumePatches", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Fake-quantize the 'inputs' tensor, type float to 'outputs' tensor of same type. + /// + /// + /// + /// Attributes + /// + /// * `[min; max]` define the clamping range for the `inputs` data. + /// * `inputs` values are quantized into the quantization range ( + /// `[0; 2^num_bits - 1]` when `narrow_range` is false and `[1; 2^num_bits - 1]` + /// when it is true) and then de-quantized and output as floats in `[min; max]` + /// interval. + /// * `num_bits` is the bitwidth of the quantization; between 2 and 16, inclusive. + /// + /// Before quantization, `min` and `max` values are adjusted with the following + /// logic. + /// It is suggested to have `min <= 0 <= max`. If `0` is not in the range of values, + /// the behavior can be unexpected: + /// + /// * If `0 < min < max`: `min_adj = 0` and `max_adj = max - min`. + /// * If `min < max < 0`: `min_adj = min - max` and `max_adj = 0`. + /// * If `min <= 0 <= max`: `scale = (max - min) / (2^num_bits - 1) `, + /// `min_adj = scale * round(min / scale)` and `max_adj = max + min_adj - min`. + /// + /// Quantization is called fake since the output is still in floating point. + /// + /// + /// + /// + /// + /// + /// + /// + public static Tensor fake_quant_with_min_max_args(Tensor inputs, float min = -6f, float max = 6f, int num_bits = 8, bool narrow_range = false, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "FakeQuantWithMinMaxArgs", name) { args = new object[] { inputs }, attrs = new Dictionary() { ["min"] = min, ["max"] = max, ["num_bits"] = num_bits, ["narrow_range"] = narrow_range } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return fake_quant_with_min_max_args_eager_fallback(inputs, min: min, max: max, num_bits: num_bits, narrow_range: narrow_range, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["inputs"] = inputs; + keywords["min"] = min; + keywords["max"] = max; + keywords["num_bits"] = num_bits; + keywords["narrow_range"] = narrow_range; + var _op = tf.OpDefLib._apply_op_helper("FakeQuantWithMinMaxArgs", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "min", _op.get_attr("min"), "max", _op.get_attr("max"), "num_bits", _op._get_attr_int("num_bits"), "narrow_range", _op._get_attr_bool("narrow_range") }; + _execute.record_gradient("FakeQuantWithMinMaxArgs", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor fake_quant_with_min_max_args_eager_fallback(Tensor inputs, float min, float max, int num_bits, bool narrow_range, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { inputs }; + object[] _attrs = new object[] { "min", min, "max", max, "num_bits", num_bits, "narrow_range", narrow_range }; + var _result = _execute.execute("FakeQuantWithMinMaxArgs", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("FakeQuantWithMinMaxArgs", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Compute gradients for a FakeQuantWithMinMaxArgs operation. + /// + /// + /// + /// + /// + /// + /// + /// + public static Tensor fake_quant_with_min_max_args_gradient(Tensor gradients, Tensor inputs, float min = -6f, float max = 6f, int num_bits = 8, bool narrow_range = false, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "FakeQuantWithMinMaxArgsGradient", name) { args = new object[] { gradients, inputs }, attrs = new Dictionary() { ["min"] = min, ["max"] = max, ["num_bits"] = num_bits, ["narrow_range"] = narrow_range } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return fake_quant_with_min_max_args_gradient_eager_fallback(gradients, inputs, min: min, max: max, num_bits: num_bits, narrow_range: narrow_range, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["gradients"] = gradients; + keywords["inputs"] = inputs; + keywords["min"] = min; + keywords["max"] = max; + keywords["num_bits"] = num_bits; + keywords["narrow_range"] = narrow_range; + var _op = tf.OpDefLib._apply_op_helper("FakeQuantWithMinMaxArgsGradient", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "min", _op.get_attr("min"), "max", _op.get_attr("max"), "num_bits", _op._get_attr_int("num_bits"), "narrow_range", _op._get_attr_bool("narrow_range") }; + _execute.record_gradient("FakeQuantWithMinMaxArgsGradient", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor fake_quant_with_min_max_args_gradient_eager_fallback(Tensor gradients, Tensor inputs, float min, float max, int num_bits, bool narrow_range, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { gradients, inputs }; + object[] _attrs = new object[] { "min", min, "max", max, "num_bits", num_bits, "narrow_range", narrow_range }; + var _result = _execute.execute("FakeQuantWithMinMaxArgsGradient", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("FakeQuantWithMinMaxArgsGradient", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Fake-quantize the 'inputs' tensor of type float via global float scalars + /// + /// + /// + /// Fake-quantize the `inputs` tensor of type float via global float scalars + /// `min` and `max` to `outputs` tensor of same shape as `inputs`. + /// + /// Attributes + /// + /// * `[min; max]` define the clamping range for the `inputs` data. + /// * `inputs` values are quantized into the quantization range ( + /// `[0; 2^num_bits - 1]` when `narrow_range` is false and `[1; 2^num_bits - 1]` + /// when it is true) and then de-quantized and output as floats in `[min; max]` + /// interval. + /// * `num_bits` is the bitwidth of the quantization; between 2 and 16, inclusive. + /// + /// Before quantization, `min` and `max` values are adjusted with the following + /// logic. + /// It is suggested to have `min <= 0 <= max`. If `0` is not in the range of values, + /// the behavior can be unexpected: + /// + /// * If `0 < min < max`: `min_adj = 0` and `max_adj = max - min`. + /// * If `min < max < 0`: `min_adj = min - max` and `max_adj = 0`. + /// * If `min <= 0 <= max`: `scale = (max - min) / (2^num_bits - 1) `, + /// `min_adj = scale * round(min / scale)` and `max_adj = max + min_adj - min`. + /// + /// This operation has a gradient and thus allows for training `min` and `max` + /// values. + /// + /// + /// + /// + /// + /// + /// + /// + public static Tensor fake_quant_with_min_max_vars(Tensor inputs, Tensor min, Tensor max, int num_bits = 8, bool narrow_range = false, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "FakeQuantWithMinMaxVars", name) { args = new object[] { inputs, min, max }, attrs = new Dictionary() { ["num_bits"] = num_bits, ["narrow_range"] = narrow_range } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return fake_quant_with_min_max_vars_eager_fallback(inputs, min, max, num_bits: num_bits, narrow_range: narrow_range, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["inputs"] = inputs; + keywords["min"] = min; + keywords["max"] = max; + keywords["num_bits"] = num_bits; + keywords["narrow_range"] = narrow_range; + var _op = tf.OpDefLib._apply_op_helper("FakeQuantWithMinMaxVars", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "num_bits", _op._get_attr_int("num_bits"), "narrow_range", _op._get_attr_bool("narrow_range") }; + _execute.record_gradient("FakeQuantWithMinMaxVars", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor fake_quant_with_min_max_vars_eager_fallback(Tensor inputs, Tensor min, Tensor max, int num_bits, bool narrow_range, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { inputs, min, max }; + object[] _attrs = new object[] { "num_bits", num_bits, "narrow_range", narrow_range }; + var _result = _execute.execute("FakeQuantWithMinMaxVars", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("FakeQuantWithMinMaxVars", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Compute gradients for a FakeQuantWithMinMaxVars operation. + /// + /// + /// + /// + /// + /// + /// + /// The bitwidth of the quantization; between 2 and 8, inclusive. + /// + /// + /// + /// + /// Whether to quantize into 2^num_bits - 1 distinct values. + /// + /// + /// + public static Tensor[] fake_quant_with_min_max_vars_gradient(Tensor gradients, Tensor inputs, Tensor min, Tensor max, int num_bits = 8, bool narrow_range = false, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "FakeQuantWithMinMaxVarsGradient", name) { args = new object[] { gradients, inputs, min, max }, attrs = new Dictionary() { ["num_bits"] = num_bits, ["narrow_range"] = narrow_range } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return fake_quant_with_min_max_vars_gradient_eager_fallback(gradients, inputs, min, max, num_bits: num_bits, narrow_range: narrow_range, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["gradients"] = gradients; + keywords["inputs"] = inputs; + keywords["min"] = min; + keywords["max"] = max; + keywords["num_bits"] = num_bits; + keywords["narrow_range"] = narrow_range; + var _op = tf.OpDefLib._apply_op_helper("FakeQuantWithMinMaxVarsGradient", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "num_bits", _op._get_attr_int("num_bits"), "narrow_range", _op._get_attr_bool("narrow_range") }; + _execute.record_gradient("FakeQuantWithMinMaxVarsGradient", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] fake_quant_with_min_max_vars_gradient_eager_fallback(Tensor gradients, Tensor inputs, Tensor min, Tensor max, int num_bits, bool narrow_range, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { gradients, inputs, min, max }; + object[] _attrs = new object[] { "num_bits", num_bits, "narrow_range", narrow_range }; + var _result = _execute.execute("FakeQuantWithMinMaxVarsGradient", 3, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("FakeQuantWithMinMaxVarsGradient", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// Fake-quantize the 'inputs' tensor of type float via per-channel floats + /// + /// + /// + /// Fake-quantize the `inputs` tensor of type float per-channel and one of the + /// shapes: `[d]`, `[b, d]` `[b, h, w, d]` via per-channel floats `min` and `max` + /// of shape `[d]` to `outputs` tensor of same shape as `inputs`. + /// + /// Attributes + /// + /// * `[min; max]` define the clamping range for the `inputs` data. + /// * `inputs` values are quantized into the quantization range ( + /// `[0; 2^num_bits - 1]` when `narrow_range` is false and `[1; 2^num_bits - 1]` + /// when it is true) and then de-quantized and output as floats in `[min; max]` + /// interval. + /// * `num_bits` is the bitwidth of the quantization; between 2 and 16, inclusive. + /// + /// Before quantization, `min` and `max` values are adjusted with the following + /// logic. + /// It is suggested to have `min <= 0 <= max`. If `0` is not in the range of values, + /// the behavior can be unexpected: + /// + /// * If `0 < min < max`: `min_adj = 0` and `max_adj = max - min`. + /// * If `min < max < 0`: `min_adj = min - max` and `max_adj = 0`. + /// * If `min <= 0 <= max`: `scale = (max - min) / (2^num_bits - 1) `, + /// `min_adj = scale * round(min / scale)` and `max_adj = max + min_adj - min`. + /// + /// This operation has a gradient and thus allows for training `min` and `max` + /// values. + /// + /// + /// + /// + /// + /// + /// + /// + public static Tensor fake_quant_with_min_max_vars_per_channel(Tensor inputs, Tensor min, Tensor max, int num_bits = 8, bool narrow_range = false, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "FakeQuantWithMinMaxVarsPerChannel", name) { args = new object[] { inputs, min, max }, attrs = new Dictionary() { ["num_bits"] = num_bits, ["narrow_range"] = narrow_range } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return fake_quant_with_min_max_vars_per_channel_eager_fallback(inputs, min, max, num_bits: num_bits, narrow_range: narrow_range, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["inputs"] = inputs; + keywords["min"] = min; + keywords["max"] = max; + keywords["num_bits"] = num_bits; + keywords["narrow_range"] = narrow_range; + var _op = tf.OpDefLib._apply_op_helper("FakeQuantWithMinMaxVarsPerChannel", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "num_bits", _op._get_attr_int("num_bits"), "narrow_range", _op._get_attr_bool("narrow_range") }; + _execute.record_gradient("FakeQuantWithMinMaxVarsPerChannel", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor fake_quant_with_min_max_vars_per_channel_eager_fallback(Tensor inputs, Tensor min, Tensor max, int num_bits, bool narrow_range, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { inputs, min, max }; + object[] _attrs = new object[] { "num_bits", num_bits, "narrow_range", narrow_range }; + var _result = _execute.execute("FakeQuantWithMinMaxVarsPerChannel", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("FakeQuantWithMinMaxVarsPerChannel", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Compute gradients for a FakeQuantWithMinMaxVarsPerChannel operation. + /// + /// + /// + /// + /// + /// + /// + /// The bitwidth of the quantization; between 2 and 16, inclusive. + /// + /// + /// + /// + /// Whether to quantize into 2^num_bits - 1 distinct values. + /// + /// + /// + public static Tensor[] fake_quant_with_min_max_vars_per_channel_gradient(Tensor gradients, Tensor inputs, Tensor min, Tensor max, int num_bits = 8, bool narrow_range = false, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "FakeQuantWithMinMaxVarsPerChannelGradient", name) { args = new object[] { gradients, inputs, min, max }, attrs = new Dictionary() { ["num_bits"] = num_bits, ["narrow_range"] = narrow_range } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return fake_quant_with_min_max_vars_per_channel_gradient_eager_fallback(gradients, inputs, min, max, num_bits: num_bits, narrow_range: narrow_range, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["gradients"] = gradients; + keywords["inputs"] = inputs; + keywords["min"] = min; + keywords["max"] = max; + keywords["num_bits"] = num_bits; + keywords["narrow_range"] = narrow_range; + var _op = tf.OpDefLib._apply_op_helper("FakeQuantWithMinMaxVarsPerChannelGradient", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "num_bits", _op._get_attr_int("num_bits"), "narrow_range", _op._get_attr_bool("narrow_range") }; + _execute.record_gradient("FakeQuantWithMinMaxVarsPerChannelGradient", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] fake_quant_with_min_max_vars_per_channel_gradient_eager_fallback(Tensor gradients, Tensor inputs, Tensor min, Tensor max, int num_bits, bool narrow_range, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { gradients, inputs, min, max }; + object[] _attrs = new object[] { "num_bits", num_bits, "narrow_range", narrow_range }; + var _result = _execute.execute("FakeQuantWithMinMaxVarsPerChannelGradient", 3, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("FakeQuantWithMinMaxVarsPerChannelGradient", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// Creates a tensor filled with a scalar value. + /// + /// + /// + /// This operation creates a tensor of shape `dims` and fills it with `value`. + /// + /// For example: + /// + /// ``` + /// # Output tensor has shape [2, 3]. + /// fill([2, 3], 9) ==> [[9, 9, 9] + /// [9, 9, 9]] + /// ``` + /// + /// `tf.fill` differs from `tf.constant` in a few ways: + /// + /// * `tf.fill` only supports scalar contents, whereas `tf.constant` supports + /// Tensor values. + /// * `tf.fill` creates an Op in the computation graph that constructs the actual + /// Tensor value at runtime. This is in contrast to `tf.constant` which embeds + /// the entire Tensor into the graph with a `Const` node. + /// * Because `tf.fill` evaluates at graph runtime, it supports dynamic shapes + /// based on other runtime Tensors, unlike `tf.constant`. + /// + /// + /// + /// + /// + public static Tensor fill(Tensor dims, Tensor value, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Fill", name) { args = new object[] { dims, value }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return fill_eager_fallback(dims, value, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["dims"] = dims; + keywords["value"] = value; + var _op = tf.OpDefLib._apply_op_helper("Fill", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "index_type", _op._get_attr_type("index_type") }; + _execute.record_gradient("Fill", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor fill_eager_fallback(Tensor dims, Tensor value, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { dims, value }; + object[] _attrs = new object[] { "T", value.dtype, "index_type", dims.dtype }; + var _result = _execute.execute("Fill", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Fill", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Generates fingerprint values. + /// + /// + /// + /// Generates fingerprint values of `data`. + /// + /// Fingerprint op considers the first dimension of `data` as the batch dimension, + /// and `output[i]` contains the fingerprint value generated from contents in + /// `data[i, ...]` for all `i`. + /// + /// Fingerprint op writes fingerprint values as byte arrays. For example, the + /// default method `farmhash64` generates a 64-bit fingerprint value at a time. + /// This 8-byte value is written out as an `uint8` array of size 8, in little-endian + /// order. + /// + /// For example, suppose that `data` has data type `DT_INT32` and shape (2, 3, 4), + /// and that the fingerprint method is `farmhash64`. In this case, the output shape + /// is (2, 8), where 2 is the batch dimension size of `data`, and 8 is the size of + /// each fingerprint value in bytes. `output[0, :]` is generated from 12 integers in + /// `data[0, :, :]` and similarly `output[1, :]` is generated from other 12 integers + /// in `data[1, :, :]`. + /// + /// Note that this op fingerprints the raw underlying buffer, and it does not + /// fingerprint Tensor's metadata such as data type and/or shape. For example, the + /// fingerprint values are invariant under reshapes and bitcasts as long as the + /// batch dimension remain the same: + /// + /// ``` + /// Fingerprint(data) == Fingerprint(Reshape(data, ...)) + /// Fingerprint(data) == Fingerprint(Bitcast(data, ...)) + /// ``` + /// + /// For string data, one should expect `Fingerprint(data) != + /// Fingerprint(ReduceJoin(data))` in general. + /// + /// + /// + /// + /// + public static Tensor fingerprint(Tensor data, Tensor method, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Fingerprint", name) { args = new object[] { data, method }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return fingerprint_eager_fallback(data, method, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["data"] = data; + keywords["method"] = method; + var _op = tf.OpDefLib._apply_op_helper("Fingerprint", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Fingerprint", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor fingerprint_eager_fallback(Tensor data, Tensor method, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { data, method }; + object[] _attrs = new object[] { "T", data.dtype }; + var _result = _execute.execute("Fingerprint", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Fingerprint", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Gather slices from `params` according to `indices`. + /// + /// + /// + /// `indices` must be an integer tensor of any dimension (usually 0-D or 1-D). + /// Produces an output tensor with shape `indices.shape + params.shape[1:]` where: + /// + /// ```python + /// # Scalar indices + /// output[:, ..., :] = params[indices, :, ... :] + /// + /// # Vector indices + /// output[i, :, ..., :] = params[indices[i], :, ... :] + /// + /// # Higher rank indices + /// output[i, ..., j, :, ... :] = params[indices[i, ..., j], :, ..., :] + /// ``` + /// + /// If `indices` is a permutation and `len(indices) == params.shape[0]` then + /// this operation will permute `params` accordingly. + /// + /// `validate_indices`: DEPRECATED. If this operation is assigned to CPU, values in + /// `indices` are always validated to be within range. If assigned to GPU, + /// out-of-bound indices result in safe but unspecified behavior, which may include + /// raising an error. + /// + ///
+ /// + ///
+ /// + ///
+ /// + /// + /// + /// + public static Tensor gather(Tensor params_, Tensor indices, bool validate_indices = true, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Gather", name) { args = new object[] { params_, indices }, attrs = new Dictionary() { ["validate_indices"] = validate_indices } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return gather_eager_fallback(params_, indices, validate_indices: validate_indices, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["params"] = params_; + keywords["indices"] = indices; + keywords["validate_indices"] = validate_indices; + var _op = tf.OpDefLib._apply_op_helper("Gather", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "validate_indices", _op._get_attr_bool("validate_indices"), "Tparams", _op._get_attr_type("Tparams"), "Tindices", _op._get_attr_type("Tindices") }; + _execute.record_gradient("Gather", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor gather_eager_fallback(Tensor params_, Tensor indices, bool validate_indices, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { params_, indices }; + object[] _attrs = new object[] { "validate_indices", validate_indices, "Tparams", params_.dtype, "Tindices", indices.dtype }; + var _result = _execute.execute("Gather", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Gather", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Gather slices from `params` into a Tensor with shape specified by `indices`. + /// + /// + /// + /// `indices` is a K-dimensional integer tensor, best thought of as a + /// (K-1)-dimensional tensor of indices into `params`, where each element defines a + /// slice of `params`: + /// + /// output[\(i_0, ..., i_{K-2}\)] = params[indices[\(i_0, ..., i_{K-2}\)]] + /// + /// Whereas in `tf.gather` `indices` defines slices into the `axis` + /// dimension of `params`, in `tf.gather_nd`, `indices` defines slices into the + /// first `N` dimensions of `params`, where `N = indices.shape[-1]`. + /// + /// The last dimension of `indices` can be at most the rank of + /// `params`: + /// + /// indices.shape[-1] <= params.rank + /// + /// The last dimension of `indices` corresponds to elements + /// (if `indices.shape[-1] == params.rank`) or slices + /// (if `indices.shape[-1] < params.rank`) along dimension `indices.shape[-1]` + /// of `params`. The output tensor has shape + /// + /// indices.shape[:-1] + params.shape[indices.shape[-1]:] + /// + /// Note that on CPU, if an out of bound index is found, an error is returned. + /// On GPU, if an out of bound index is found, a 0 is stored in the + /// corresponding output value. + /// + /// Some examples below. + /// + /// Simple indexing into a matrix: + /// + /// ```python + /// indices = [[0, 0], [1, 1]] + /// params = [['a', 'b'], ['c', 'd']] + /// output = ['a', 'd'] + /// ``` + /// + /// Slice indexing into a matrix: + /// + /// ```python + /// indices = [[1], [0]] + /// params = [['a', 'b'], ['c', 'd']] + /// output = [['c', 'd'], ['a', 'b']] + /// ``` + /// + /// Indexing into a 3-tensor: + /// + /// ```python + /// indices = [[1]] + /// params = [[['a0', 'b0'], ['c0', 'd0']], + /// [['a1', 'b1'], ['c1', 'd1']]] + /// output = [[['a1', 'b1'], ['c1', 'd1']]] + /// + /// + /// indices = [[0, 1], [1, 0]] + /// params = [[['a0', 'b0'], ['c0', 'd0']], + /// [['a1', 'b1'], ['c1', 'd1']]] + /// output = [['c0', 'd0'], ['a1', 'b1']] + /// + /// + /// indices = [[0, 0, 1], [1, 0, 1]] + /// params = [[['a0', 'b0'], ['c0', 'd0']], + /// [['a1', 'b1'], ['c1', 'd1']]] + /// output = ['b0', 'b1'] + /// ``` + /// + /// Batched indexing into a matrix: + /// + /// ```python + /// indices = [[[0, 0]], [[0, 1]]] + /// params = [['a', 'b'], ['c', 'd']] + /// output = [['a'], ['b']] + /// ``` + /// + /// Batched slice indexing into a matrix: + /// + /// ```python + /// indices = [[[1]], [[0]]] + /// params = [['a', 'b'], ['c', 'd']] + /// output = [[['c', 'd']], [['a', 'b']]] + /// ``` + /// + /// Batched indexing into a 3-tensor: + /// + /// ```python + /// indices = [[[1]], [[0]]] + /// params = [[['a0', 'b0'], ['c0', 'd0']], + /// [['a1', 'b1'], ['c1', 'd1']]] + /// output = [[[['a1', 'b1'], ['c1', 'd1']]], + /// [[['a0', 'b0'], ['c0', 'd0']]]] + /// + /// indices = [[[0, 1], [1, 0]], [[0, 0], [1, 1]]] + /// params = [[['a0', 'b0'], ['c0', 'd0']], + /// [['a1', 'b1'], ['c1', 'd1']]] + /// output = [[['c0', 'd0'], ['a1', 'b1']], + /// [['a0', 'b0'], ['c1', 'd1']]] + /// + /// + /// indices = [[[0, 0, 1], [1, 0, 1]], [[0, 1, 1], [1, 1, 0]]] + /// params = [[['a0', 'b0'], ['c0', 'd0']], + /// [['a1', 'b1'], ['c1', 'd1']]] + /// output = [['b0', 'b1'], ['d0', 'c1']] + /// ``` + /// + /// See also `tf.gather` and `tf.batch_gather`. + /// + /// + /// + /// + /// + public static Tensor gather_nd(Tensor params_, Tensor indices, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "GatherNd", name) { args = new object[] { params_, indices }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return gather_nd_eager_fallback(params_, indices, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["params"] = params_; + keywords["indices"] = indices; + var _op = tf.OpDefLib._apply_op_helper("GatherNd", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "Tparams", _op._get_attr_type("Tparams"), "Tindices", _op._get_attr_type("Tindices") }; + _execute.record_gradient("GatherNd", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor gather_nd_eager_fallback(Tensor params_, Tensor indices, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { params_, indices }; + object[] _attrs = new object[] { "Tparams", params_.dtype, "Tindices", indices.dtype }; + var _result = _execute.execute("GatherNd", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("GatherNd", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Gather slices from `params` axis `axis` according to `indices`. + /// + /// + /// + /// `indices` must be an integer tensor of any dimension (usually 0-D or 1-D). + /// Produces an output tensor with shape `params.shape[:axis] + + /// indices.shape[batch_dims:] + params.shape[axis + 1:]` where: + /// + /// ```python + /// # Scalar indices (output is rank(params) - 1). + /// output[a_0, ..., a_n, b_0, ..., b_n] = + /// params[a_0, ..., a_n, indices, b_0, ..., b_n] + /// + /// # Vector indices (output is rank(params)). + /// output[a_0, ..., a_n, i, b_0, ..., b_n] = + /// params[a_0, ..., a_n, indices[i], b_0, ..., b_n] + /// + /// # Higher rank indices (output is rank(params) + rank(indices) - 1). + /// output[a_0, ..., a_n, i, ..., j, b_0, ... b_n] = + /// params[a_0, ..., a_n, indices[i, ..., j], b_0, ..., b_n] + /// ``` + /// + ///
+ /// + ///
+ /// + /// Note that on CPU, if an out of bound index is found, an error is returned. + /// On GPU, if an out of bound index is found, a 0 is stored in the + /// corresponding output value. + /// + /// See also `tf.batch_gather` and `tf.gather_nd`. + /// + ///
+ /// + /// + /// + /// + /// + public static Tensor gather_v2(Tensor params_, Tensor indices, Tensor axis, int batch_dims = 0, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "GatherV2", name) { args = new object[] { params_, indices, axis }, attrs = new Dictionary() { ["batch_dims"] = batch_dims } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return gather_v2_eager_fallback(params_, indices, axis, batch_dims: batch_dims, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["params"] = params_; + keywords["indices"] = indices; + keywords["axis"] = axis; + keywords["batch_dims"] = batch_dims; + var _op = tf.OpDefLib._apply_op_helper("GatherV2", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "batch_dims", _op._get_attr_int("batch_dims"), "Tparams", _op._get_attr_type("Tparams"), "Tindices", _op._get_attr_type("Tindices"), "Taxis", _op._get_attr_type("Taxis") }; + _execute.record_gradient("GatherV2", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor gather_v2_eager_fallback(Tensor params_, Tensor indices, Tensor axis, int batch_dims, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { params_, indices, axis }; + object[] _attrs = new object[] { "batch_dims", batch_dims, "Tparams", params_.dtype, "Tindices", indices.dtype, "Taxis", axis.dtype }; + var _result = _execute.execute("GatherV2", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("GatherV2", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Gives a guarantee to the TF runtime that the input tensor is a constant. + /// + /// + /// + /// The runtime is then free to make optimizations based on this. + /// + /// Only accepts value typed tensors as inputs and rejects resource variable handles + /// as input. + /// + /// Returns the input tensor without modification. + /// + /// + /// + /// + public static Tensor guarantee_const(Tensor input, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "GuaranteeConst", name) { args = new object[] { input }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return guarantee_const_eager_fallback(input, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + var _op = tf.OpDefLib._apply_op_helper("GuaranteeConst", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("GuaranteeConst", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor guarantee_const_eager_fallback(Tensor input, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input }; + object[] _attrs = new object[] { "T", input.dtype }; + var _result = _execute.execute("GuaranteeConst", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("GuaranteeConst", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Return a tensor with the same shape and contents as the input tensor or value. + /// + /// + /// + public static Tensor identity(Tensor input, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Identity", name) { args = new object[] { input }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return identity_eager_fallback(input, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + var _op = tf.OpDefLib._apply_op_helper("Identity", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Identity", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor identity_eager_fallback(Tensor input, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input }; + object[] _attrs = new object[] { "T", input.dtype }; + var _result = _execute.execute("Identity", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Identity", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns a list of tensors with the same shapes and contents as the input + /// + /// + /// + /// tensors. + /// + /// This op can be used to override the gradient for complicated functions. For + /// example, suppose y = f(x) and we wish to apply a custom function g for backprop + /// such that dx = g(dy). In Python, + /// + /// ```python + /// with tf.get_default_graph().gradient_override_map( + /// {'IdentityN': 'OverrideGradientWithG'}): + /// y, _ = identity_n([f(x), x]) + /// + /// @tf.RegisterGradient('OverrideGradientWithG') + /// def ApplyG(op, dy, _): + /// return [None, g(dy)] # Do not backprop to f(x). + /// ``` + /// + /// + /// + /// + public static Tensor[] identity_n(Tensors input, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "IdentityN", name) { args = new object[] { input }, attrs = new Dictionary() { } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return identity_n_eager_fallback(input, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + var _op = tf.OpDefLib._apply_op_helper("IdentityN", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op.get_attr("T") }; + _execute.record_gradient("IdentityN", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] identity_n_eager_fallback(Tensor input, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input }; + object[] _attrs = new object[] { }; + var _result = _execute.execute("IdentityN", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("IdentityN", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// Returns immutable tensor from memory region. + /// + /// + /// + /// The current implementation memmaps the tensor from a file. + /// + /// + /// + /// + /// Type of the returned tensor. + /// + /// + /// + /// + /// Shape of the returned tensor. + /// + /// + /// + /// + /// Name of readonly memory region used by the tensor, see + /// NewReadOnlyMemoryRegionFromFile in tensorflow::Env. + /// + /// + /// + public static Tensor immutable_const(TF_DataType dtype, Shape shape, string memory_region_name, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "ImmutableConst", name) { args = new object[] { }, attrs = new Dictionary() { ["dtype"] = dtype, ["shape"] = shape, ["memory_region_name"] = memory_region_name } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return immutable_const_eager_fallback(dtype: dtype, shape: shape, memory_region_name: memory_region_name, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["dtype"] = dtype; + keywords["shape"] = shape; + keywords["memory_region_name"] = memory_region_name; + var _op = tf.OpDefLib._apply_op_helper("ImmutableConst", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "dtype", _op._get_attr_type("dtype"), "shape", _op.get_attr("shape"), "memory_region_name", _op.get_attr("memory_region_name") }; + _execute.record_gradient("ImmutableConst", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor immutable_const_eager_fallback(TF_DataType dtype, Shape shape, string memory_region_name, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { }; + object[] _attrs = new object[] { "dtype", dtype, "shape", shape, "memory_region_name", memory_region_name }; + var _result = _execute.execute("ImmutableConst", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("ImmutableConst", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// + /// + /// + /// + /// + /// + public static Tensor inplace_add(Tensor x, Tensor i, Tensor v, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "InplaceAdd", name) { args = new object[] { x, i, v }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return inplace_add_eager_fallback(x, i, v, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + keywords["i"] = i; + keywords["v"] = v; + var _op = tf.OpDefLib._apply_op_helper("InplaceAdd", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("InplaceAdd", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor inplace_add_eager_fallback(Tensor x, Tensor i, Tensor v, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x, i, v }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("InplaceAdd", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("InplaceAdd", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// + /// + /// + /// + /// + /// + public static Tensor inplace_sub(Tensor x, Tensor i, Tensor v, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "InplaceSub", name) { args = new object[] { x, i, v }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return inplace_sub_eager_fallback(x, i, v, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + keywords["i"] = i; + keywords["v"] = v; + var _op = tf.OpDefLib._apply_op_helper("InplaceSub", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("InplaceSub", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor inplace_sub_eager_fallback(Tensor x, Tensor i, Tensor v, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x, i, v }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("InplaceSub", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("InplaceSub", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// + /// + /// + /// + /// + /// + public static Tensor inplace_update(Tensor x, Tensor i, Tensor v, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "InplaceUpdate", name) { args = new object[] { x, i, v }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return inplace_update_eager_fallback(x, i, v, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + keywords["i"] = i; + keywords["v"] = v; + var _op = tf.OpDefLib._apply_op_helper("InplaceUpdate", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("InplaceUpdate", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor inplace_update_eager_fallback(Tensor x, Tensor i, Tensor v, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x, i, v }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("InplaceUpdate", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("InplaceUpdate", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes the inverse permutation of a tensor. + /// + /// + /// + /// This operation computes the inverse of an index permutation. It takes a 1-D + /// integer tensor `x`, which represents the indices of a zero-based array, and + /// swaps each value with its index position. In other words, for an output tensor + /// `y` and an input tensor `x`, this operation computes the following: + /// + /// `y[x[i]] = i for i in [0, 1, ..., len(x) - 1]` + /// + /// The values must include 0. There can be no duplicate values or negative values. + /// + /// For example: + /// + /// ``` + /// # tensor `x` is [3, 4, 0, 2, 1] + /// invert_permutation(x) ==> [2, 4, 3, 0, 1] + /// ``` + /// + /// + /// + /// + public static Tensor invert_permutation(Tensor x, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "InvertPermutation", name) { args = new object[] { x }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return invert_permutation_eager_fallback(x, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + var _op = tf.OpDefLib._apply_op_helper("InvertPermutation", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("InvertPermutation", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor invert_permutation_eager_fallback(Tensor x, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("InvertPermutation", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("InvertPermutation", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes the difference between two lists of numbers or strings. + /// + /// + /// + /// Given a list `x` and a list `y`, this operation returns a list `out` that + /// represents all values that are in `x` but not in `y`. The returned list `out` + /// is sorted in the same order that the numbers appear in `x` (duplicates are + /// preserved). This operation also returns a list `idx` that represents the + /// position of each `out` element in `x`. In other words: + /// + /// `out[i] = x[idx[i]] for i in [0, 1, ..., len(out) - 1]` + /// + /// For example, given this input: + /// + /// ``` + /// x = [1, 2, 3, 4, 5, 6] + /// y = [1, 3, 5] + /// ``` + /// + /// This operation would return: + /// + /// ``` + /// out ==> [2, 4, 6] + /// idx ==> [1, 3, 5] + /// ``` + /// + /// + /// + /// + /// + /// + public static Tensor[] list_diff(Tensor x, Tensor y, TF_DataType out_idx = TF_DataType.TF_INT32, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "ListDiff", name) { args = new object[] { x, y }, attrs = new Dictionary() { ["out_idx"] = out_idx } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return list_diff_eager_fallback(x, y, out_idx: out_idx, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + keywords["y"] = y; + keywords["out_idx"] = out_idx; + var _op = tf.OpDefLib._apply_op_helper("ListDiff", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "out_idx", _op._get_attr_type("out_idx") }; + _execute.record_gradient("ListDiff", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] list_diff_eager_fallback(Tensor x, Tensor y, TF_DataType out_idx, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x, y }; + object[] _attrs = new object[] { "T", x.dtype, "out_idx", out_idx }; + var _result = _execute.execute("ListDiff", 2, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("ListDiff", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// Applies lower_bound(sorted_search_values, values) along each row. + /// + /// + /// + /// Each set of rows with the same index in (sorted_inputs, values) is treated + /// independently. The resulting row is the equivalent of calling + /// `np.searchsorted(sorted_inputs, values, side='left')`. + /// + /// The result is not a global index to the entire + /// `Tensor`, but rather just the index in the last dimension. + /// + /// A 2-D example: + /// sorted_sequence = [[0, 3, 9, 9, 10], + /// [1, 2, 3, 4, 5]] + /// values = [[2, 4, 9], + /// [0, 2, 6]] + /// + /// result = LowerBound(sorted_sequence, values) + /// + /// result == [[1, 2, 2], + /// [0, 1, 5]] + /// + /// + /// + /// + /// + /// + public static Tensor lower_bound(Tensor sorted_inputs, Tensor values, TF_DataType out_type = TF_DataType.TF_INT32, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "LowerBound", name) { args = new object[] { sorted_inputs, values }, attrs = new Dictionary() { ["out_type"] = out_type } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return lower_bound_eager_fallback(sorted_inputs, values, out_type: out_type, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["sorted_inputs"] = sorted_inputs; + keywords["values"] = values; + keywords["out_type"] = out_type; + var _op = tf.OpDefLib._apply_op_helper("LowerBound", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "out_type", _op._get_attr_type("out_type") }; + _execute.record_gradient("LowerBound", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor lower_bound_eager_fallback(Tensor sorted_inputs, Tensor values, TF_DataType out_type, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { sorted_inputs, values }; + object[] _attrs = new object[] { "T", sorted_inputs.dtype, "out_type", out_type }; + var _result = _execute.execute("LowerBound", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("LowerBound", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Copy a tensor setting everything outside a central band in each innermost matrix to zero. + /// + /// + /// + /// The `band` part is computed as follows: + /// Assume `input` has `k` dimensions `[I, J, K, ..., M, N]`, then the output is a + /// tensor with the same shape where + /// + /// `band[i, j, k, ..., m, n] = in_band(m, n) * input[i, j, k, ..., m, n]`. + /// + /// The indicator function + /// + /// `in_band(m, n) = (num_lower < 0 || (m-n) <= num_lower)) && + /// (num_upper < 0 || (n-m) <= num_upper)`. + /// + /// For example: + /// + /// ``` + /// # if 'input' is [[ 0, 1, 2, 3] + /// # [-1, 0, 1, 2] + /// # [-2, -1, 0, 1] + /// # [-3, -2, -1, 0]], + /// + /// tf.linalg.band_part(input, 1, -1) ==> [[ 0, 1, 2, 3] + /// [-1, 0, 1, 2] + /// [ 0, -1, 0, 1] + /// [ 0, 0, -1, 0]], + /// + /// tf.linalg.band_part(input, 2, 1) ==> [[ 0, 1, 0, 0] + /// [-1, 0, 1, 0] + /// [-2, -1, 0, 1] + /// [ 0, -2, -1, 0]] + /// ``` + /// + /// Useful special cases: + /// + /// ``` + /// tf.linalg.band_part(input, 0, -1) ==> Upper triangular part. + /// tf.linalg.band_part(input, -1, 0) ==> Lower triangular part. + /// tf.linalg.band_part(input, 0, 0) ==> Diagonal. + /// ``` + /// + /// + /// + /// + /// + /// + public static Tensor matrix_band_part(Tensor input, Tensor num_lower, Tensor num_upper, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "MatrixBandPart", name) { args = new object[] { input, num_lower, num_upper }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return matrix_band_part_eager_fallback(input, num_lower, num_upper, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["num_lower"] = num_lower; + keywords["num_upper"] = num_upper; + var _op = tf.OpDefLib._apply_op_helper("MatrixBandPart", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "Tindex", _op._get_attr_type("Tindex") }; + _execute.record_gradient("MatrixBandPart", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor matrix_band_part_eager_fallback(Tensor input, Tensor num_lower, Tensor num_upper, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, num_lower, num_upper }; + object[] _attrs = new object[] { "T", input.dtype, "Tindex", num_lower.dtype }; + var _result = _execute.execute("MatrixBandPart", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("MatrixBandPart", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns a batched diagonal tensor with a given batched diagonal values. + /// + /// + /// + /// Given a `diagonal`, this operation returns a tensor with the `diagonal` and + /// everything else padded with zeros. The diagonal is computed as follows: + /// + /// Assume `diagonal` has `k` dimensions `[I, J, K, ..., N]`, then the output is a + /// tensor of rank `k+1` with dimensions [I, J, K, ..., N, N]` where: + /// + /// `output[i, j, k, ..., m, n] = 1{m=n} * diagonal[i, j, k, ..., n]`. + /// + /// For example: + /// + /// ``` + /// # 'diagonal' is [[1, 2, 3, 4], [5, 6, 7, 8]] + /// + /// and diagonal.shape = (2, 4) + /// + /// tf.matrix_diag(diagonal) ==> [[[1, 0, 0, 0] + /// [0, 2, 0, 0] + /// [0, 0, 3, 0] + /// [0, 0, 0, 4]], + /// [[5, 0, 0, 0] + /// [0, 6, 0, 0] + /// [0, 0, 7, 0] + /// [0, 0, 0, 8]]] + /// + /// which has shape (2, 4, 4) + /// ``` + /// + /// + /// + /// + public static Tensor matrix_diag(Tensor diagonal, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "MatrixDiag", name) { args = new object[] { diagonal }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return matrix_diag_eager_fallback(diagonal, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["diagonal"] = diagonal; + var _op = tf.OpDefLib._apply_op_helper("MatrixDiag", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("MatrixDiag", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor matrix_diag_eager_fallback(Tensor diagonal, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { diagonal }; + object[] _attrs = new object[] { "T", diagonal.dtype }; + var _result = _execute.execute("MatrixDiag", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("MatrixDiag", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns the batched diagonal part of a batched tensor. + /// + /// + /// + /// This operation returns a tensor with the `diagonal` part + /// of the batched `input`. The `diagonal` part is computed as follows: + /// + /// Assume `input` has `k` dimensions `[I, J, K, ..., M, N]`, then the output is a + /// tensor of rank `k - 1` with dimensions `[I, J, K, ..., min(M, N)]` where: + /// + /// `diagonal[i, j, k, ..., n] = input[i, j, k, ..., n, n]`. + /// + /// The input must be at least a matrix. + /// + /// For example: + /// + /// ``` + /// # 'input' is [[[1, 0, 0, 0] + /// [0, 2, 0, 0] + /// [0, 0, 3, 0] + /// [0, 0, 0, 4]], + /// [[5, 0, 0, 0] + /// [0, 6, 0, 0] + /// [0, 0, 7, 0] + /// [0, 0, 0, 8]]] + /// + /// and input.shape = (2, 4, 4) + /// + /// tf.matrix_diag_part(input) ==> [[1, 2, 3, 4], [5, 6, 7, 8]] + /// + /// which has shape (2, 4) + /// ``` + /// + /// + /// + /// + public static Tensor matrix_diag_part(Tensor input, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "MatrixDiagPart", name) { args = new object[] { input }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return matrix_diag_part_eager_fallback(input, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + var _op = tf.OpDefLib._apply_op_helper("MatrixDiagPart", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("MatrixDiagPart", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor matrix_diag_part_eager_fallback(Tensor input, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input }; + object[] _attrs = new object[] { "T", input.dtype }; + var _result = _execute.execute("MatrixDiagPart", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("MatrixDiagPart", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns the batched diagonal part of a batched tensor. + /// + /// + /// + /// Returns a tensor with the `k[0]`-th to `k[1]`-th diagonals of the batched + /// `input`. + /// + /// Assume `input` has `r` dimensions `[I, J, ..., L, M, N]`. + /// Let `max_diag_len` be the maximum length among all diagonals to be extracted, + /// `max_diag_len = min(M + min(k[1], 0), N + min(-k[0], 0))` + /// Let `num_diags` be the number of diagonals to extract, + /// `num_diags = k[1] - k[0] + 1`. + /// + /// If `num_diags == 1`, the output tensor is of rank `r - 1` with shape + /// `[I, J, ..., L, max_diag_len]` and values: + /// + /// ``` + /// diagonal[i, j, ..., l, n] + /// = input[i, j, ..., l, n+y, n+x] ; if 0 <= n+y < M and 0 <= n+x < N, + /// padding_value ; otherwise. + /// ``` + /// where `y = max(-k[1], 0)`, `x = max(k[1], 0)`. + /// + /// Otherwise, the output tensor has rank `r` with dimensions + /// `[I, J, ..., L, num_diags, max_diag_len]` with values: + /// + /// ``` + /// diagonal[i, j, ..., l, m, n] + /// = input[i, j, ..., l, n+y, n+x] ; if 0 <= n+y < M and 0 <= n+x < N, + /// padding_value ; otherwise. + /// ``` + /// where `d = k[1] - m`, `y = max(-d, 0)`, and `x = max(d, 0)`. + /// + /// The input must be at least a matrix. + /// + /// For example: + /// + /// ``` + /// input = np.array([[[1, 2, 3, 4], # Input shape: (2, 3, 4) + /// [5, 6, 7, 8], + /// [9, 8, 7, 6]], + /// [[5, 4, 3, 2], + /// [1, 2, 3, 4], + /// [5, 6, 7, 8]]]) + /// + /// # A main diagonal from each batch. + /// tf.matrix_diag_part(input) ==> [[1, 6, 7], # Output shape: (2, 3) + /// [5, 2, 7]] + /// + /// # A superdiagonal from each batch. + /// tf.matrix_diag_part(input, k = 1) + /// ==> [[2, 7, 6], # Output shape: (2, 3) + /// [4, 3, 8]] + /// + /// # A tridiagonal band from each batch. + /// tf.matrix_diag_part(input, k = (-1, 1)) + /// ==> [[[2, 7, 6], # Output shape: (2, 3, 3) + /// [1, 6, 7], + /// [5, 8, 0]], + /// [[4, 3, 8], + /// [5, 2, 7], + /// [1, 6, 0]]] + /// + /// # Padding value = 9 + /// tf.matrix_diag_part(input, k = (1, 3), padding_value = 9) + /// ==> [[[4, 9, 9], # Output shape: (2, 3, 3) + /// [3, 8, 9], + /// [2, 7, 6]], + /// [[2, 9, 9], + /// [3, 4, 9], + /// [4, 3, 8]]] + /// ``` + /// + /// + /// + /// + /// + /// + public static Tensor matrix_diag_part_v2(Tensor input, Tensor k, Tensor padding_value, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "MatrixDiagPartV2", name) { args = new object[] { input, k, padding_value }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return matrix_diag_part_v2_eager_fallback(input, k, padding_value, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["k"] = k; + keywords["padding_value"] = padding_value; + var _op = tf.OpDefLib._apply_op_helper("MatrixDiagPartV2", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("MatrixDiagPartV2", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor matrix_diag_part_v2_eager_fallback(Tensor input, Tensor k, Tensor padding_value, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, k, padding_value }; + object[] _attrs = new object[] { "T", input.dtype }; + var _result = _execute.execute("MatrixDiagPartV2", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("MatrixDiagPartV2", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns the batched diagonal part of a batched tensor. + /// + /// + /// + /// Returns a tensor with the `k[0]`-th to `k[1]`-th diagonals of the batched + /// `input`. + /// + /// Assume `input` has `r` dimensions `[I, J, ..., L, M, N]`. + /// Let `max_diag_len` be the maximum length among all diagonals to be extracted, + /// `max_diag_len = min(M + min(k[1], 0), N + min(-k[0], 0))` + /// Let `num_diags` be the number of diagonals to extract, + /// `num_diags = k[1] - k[0] + 1`. + /// + /// If `num_diags == 1`, the output tensor is of rank `r - 1` with shape + /// `[I, J, ..., L, max_diag_len]` and values: + /// + /// ``` + /// diagonal[i, j, ..., l, n] + /// = input[i, j, ..., l, n+y, n+x] ; if 0 <= n+y < M and 0 <= n+x < N, + /// padding_value ; otherwise. + /// ``` + /// where `y = max(-k[1], 0)`, `x = max(k[1], 0)`. + /// + /// Otherwise, the output tensor has rank `r` with dimensions + /// `[I, J, ..., L, num_diags, max_diag_len]` with values: + /// + /// ``` + /// diagonal[i, j, ..., l, m, n] + /// = input[i, j, ..., l, n+y, n+x] ; if 0 <= n+y < M and 0 <= n+x < N, + /// padding_value ; otherwise. + /// ``` + /// where `d = k[1] - m`, `y = max(-d, 0) - offset`, and `x = max(d, 0) - offset`. + /// + /// `offset` is zero except when the alignment of the diagonal is to the right. + /// ``` + /// offset = max_diag_len - diag_len(d) ; if (`align` in {RIGHT_LEFT, RIGHT_RIGHT} + /// and `d >= 0`) or + /// (`align` in {LEFT_RIGHT, RIGHT_RIGHT} + /// and `d <= 0`) + /// 0 ; otherwise + /// ``` + /// where `diag_len(d) = min(cols - max(d, 0), rows + min(d, 0))`. + /// + /// The input must be at least a matrix. + /// + /// For example: + /// + /// ``` + /// input = np.array([[[1, 2, 3, 4], # Input shape: (2, 3, 4) + /// [5, 6, 7, 8], + /// [9, 8, 7, 6]], + /// [[5, 4, 3, 2], + /// [1, 2, 3, 4], + /// [5, 6, 7, 8]]]) + /// + /// # A main diagonal from each batch. + /// tf.matrix_diag_part(input) ==> [[1, 6, 7], # Output shape: (2, 3) + /// [5, 2, 7]] + /// + /// # A superdiagonal from each batch. + /// tf.matrix_diag_part(input, k = 1) + /// ==> [[2, 7, 6], # Output shape: (2, 3) + /// [4, 3, 8]] + /// + /// # A band from each batch. + /// tf.matrix_diag_part(input, k = (-1, 2)) + /// ==> [[[0, 3, 8], # Output shape: (2, 4, 3) + /// [2, 7, 6], + /// [1, 6, 7], + /// [5, 8, 0]], + /// [[0, 3, 4], + /// [4, 3, 8], + /// [5, 2, 7], + /// [1, 6, 0]]] + /// + /// # LEFT_RIGHT alignment. + /// tf.matrix_diag_part(input, k = (-1, 2), align="LEFT_RIGHT") + /// ==> [[[3, 8, 0], # Output shape: (2, 4, 3) + /// [2, 7, 6], + /// [1, 6, 7], + /// [0, 5, 8]], + /// [[3, 4, 0], + /// [4, 3, 8], + /// [5, 2, 7], + /// [0, 1, 6]]] + /// + /// # max_diag_len can be shorter than the main diagonal. + /// tf.matrix_diag_part(input, k = (-2, -1)) + /// ==> [[[5, 8], + /// [9, 0]], + /// [[1, 6], + /// [5, 0]]] + /// + /// # padding_value = 9 + /// tf.matrix_diag_part(input, k = (1, 3), padding_value = 9) + /// ==> [[[9, 9, 4], # Output shape: (2, 3, 3) + /// [9, 3, 8], + /// [2, 7, 6]], + /// [[9, 9, 2], + /// [9, 3, 4], + /// [4, 3, 8]]] + /// + /// ``` + /// + /// + /// + /// + /// + /// + /// + /// Some diagonals are shorter than `max_diag_len` and need to be padded. `align` is + /// a string specifying how superdiagonals and subdiagonals should be aligned, + /// respectively. There are four possible alignments: "RIGHT_LEFT" (default), + /// "LEFT_RIGHT", "LEFT_LEFT", and "RIGHT_RIGHT". "RIGHT_LEFT" aligns superdiagonals + /// to the right (left-pads the row) and subdiagonals to the left (right-pads the + /// row). It is the packing format LAPACK uses. cuSPARSE uses "LEFT_RIGHT", which is + /// the opposite alignment. + /// + /// + /// + public static Tensor matrix_diag_part_v3(Tensor input, Tensor k, Tensor padding_value, string align = "RIGHT_LEFT", string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "MatrixDiagPartV3", name) { args = new object[] { input, k, padding_value }, attrs = new Dictionary() { ["align"] = align } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return matrix_diag_part_v3_eager_fallback(input, k, padding_value, align: align, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + if (align is null) + { + align = "RIGHT_LEFT"; + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["k"] = k; + keywords["padding_value"] = padding_value; + keywords["align"] = align; + var _op = tf.OpDefLib._apply_op_helper("MatrixDiagPartV3", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "align", _op.get_attr("align") }; + _execute.record_gradient("MatrixDiagPartV3", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor matrix_diag_part_v3_eager_fallback(Tensor input, Tensor k, Tensor padding_value, string align, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, k, padding_value }; + object[] _attrs = new object[] { "T", input.dtype, "align", align }; + var _result = _execute.execute("MatrixDiagPartV3", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("MatrixDiagPartV3", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns a batched diagonal tensor with given batched diagonal values. + /// + /// + /// + /// Returns a tensor with the contents in `diagonal` as `k[0]`-th to `k[1]`-th + /// diagonals of a matrix, with everything else padded with `padding`. `num_rows` + /// and `num_cols` specify the dimension of the innermost matrix of the output. If + /// both are not specified, the op assumes the innermost matrix is square and infers + /// its size from `k` and the innermost dimension of `diagonal`. If only one of them + /// is specified, the op assumes the unspecified value is the smallest possible + /// based on other criteria. + /// + /// Let `diagonal` have `r` dimensions `[I, J, ..., L, M, N]`. The output tensor has + /// rank `r+1` with shape `[I, J, ..., L, M, num_rows, num_cols]` when only one + /// diagonal is given (`k` is an integer or `k[0] == k[1]`). Otherwise, it has rank + /// `r` with shape `[I, J, ..., L, num_rows, num_cols]`. + /// + /// The second innermost dimension of `diagonal` has double meaning. + /// When `k` is scalar or `k[0] == k[1]`, `M` is part of the batch size + /// [I, J, ..., M], and the output tensor is: + /// + /// ``` + /// output[i, j, ..., l, m, n] + /// = diagonal[i, j, ..., l, n-max(d_upper, 0)] ; if n - m == d_upper + /// padding_value ; otherwise + /// ``` + /// + /// Otherwise, `M` is treated as the number of diagonals for the matrix in the + /// same batch (`M = k[1]-k[0]+1`), and the output tensor is: + /// + /// ``` + /// output[i, j, ..., l, m, n] + /// = diagonal[i, j, ..., l, diag_index, index_in_diag] ; if k[0] <= d <= k[1] + /// padding_value ; otherwise + /// ``` + /// where `d = n - m`, `diag_index = k[1] - d`, and `index_in_diag = n - max(d, 0)`. + /// + /// For example: + /// + /// ``` + /// # The main diagonal. + /// diagonal = np.array([[1, 2, 3, 4], # Input shape: (2, 4) + /// [5, 6, 7, 8]]) + /// tf.matrix_diag(diagonal) ==> [[[1, 0, 0, 0], # Output shape: (2, 4, 4) + /// [0, 2, 0, 0], + /// [0, 0, 3, 0], + /// [0, 0, 0, 4]], + /// [[5, 0, 0, 0], + /// [0, 6, 0, 0], + /// [0, 0, 7, 0], + /// [0, 0, 0, 8]]] + /// + /// # A superdiagonal (per batch). + /// diagonal = np.array([[1, 2, 3], # Input shape: (2, 3) + /// [4, 5, 6]]) + /// tf.matrix_diag(diagonal, k = 1) + /// ==> [[[0, 1, 0, 0], # Output shape: (2, 4, 4) + /// [0, 0, 2, 0], + /// [0, 0, 0, 3], + /// [0, 0, 0, 0]], + /// [[0, 4, 0, 0], + /// [0, 0, 5, 0], + /// [0, 0, 0, 6], + /// [0, 0, 0, 0]]] + /// + /// # A band of diagonals. + /// diagonals = np.array([[[1, 2, 3], # Input shape: (2, 2, 3) + /// [4, 5, 0]], + /// [[6, 7, 9], + /// [9, 1, 0]]]) + /// tf.matrix_diag(diagonals, k = (-1, 0)) + /// ==> [[[1, 0, 0], # Output shape: (2, 3, 3) + /// [4, 2, 0], + /// [0, 5, 3]], + /// [[6, 0, 0], + /// [9, 7, 0], + /// [0, 1, 9]]] + /// + /// # Rectangular matrix. + /// diagonal = np.array([1, 2]) # Input shape: (2) + /// tf.matrix_diag(diagonal, k = -1, num_rows = 3, num_cols = 4) + /// ==> [[0, 0, 0, 0], # Output shape: (3, 4) + /// [1, 0, 0, 0], + /// [0, 2, 0, 0]] + /// + /// # Rectangular matrix with inferred num_cols and padding_value = 9. + /// tf.matrix_diag(diagonal, k = -1, num_rows = 3, padding_value = 9) + /// ==> [[9, 9], # Output shape: (3, 2) + /// [1, 9], + /// [9, 2]] + /// ``` + /// + /// + /// + /// + /// + /// + /// + /// + public static Tensor matrix_diag_v2(Tensor diagonal, Tensor k, Tensor num_rows, Tensor num_cols, Tensor padding_value, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "MatrixDiagV2", name) { args = new object[] { diagonal, k, num_rows, num_cols, padding_value }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return matrix_diag_v2_eager_fallback(diagonal, k, num_rows, num_cols, padding_value, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["diagonal"] = diagonal; + keywords["k"] = k; + keywords["num_rows"] = num_rows; + keywords["num_cols"] = num_cols; + keywords["padding_value"] = padding_value; + var _op = tf.OpDefLib._apply_op_helper("MatrixDiagV2", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("MatrixDiagV2", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor matrix_diag_v2_eager_fallback(Tensor diagonal, Tensor k, Tensor num_rows, Tensor num_cols, Tensor padding_value, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { diagonal, k, num_rows, num_cols, padding_value }; + object[] _attrs = new object[] { "T", diagonal.dtype }; + var _result = _execute.execute("MatrixDiagV2", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("MatrixDiagV2", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns a batched diagonal tensor with given batched diagonal values. + /// + /// + /// + /// Returns a tensor with the contents in `diagonal` as `k[0]`-th to `k[1]`-th + /// diagonals of a matrix, with everything else padded with `padding`. `num_rows` + /// and `num_cols` specify the dimension of the innermost matrix of the output. If + /// both are not specified, the op assumes the innermost matrix is square and infers + /// its size from `k` and the innermost dimension of `diagonal`. If only one of them + /// is specified, the op assumes the unspecified value is the smallest possible + /// based on other criteria. + /// + /// Let `diagonal` have `r` dimensions `[I, J, ..., L, M, N]`. The output tensor has + /// rank `r+1` with shape `[I, J, ..., L, M, num_rows, num_cols]` when only one + /// diagonal is given (`k` is an integer or `k[0] == k[1]`). Otherwise, it has rank + /// `r` with shape `[I, J, ..., L, num_rows, num_cols]`. + /// + /// The second innermost dimension of `diagonal` has double meaning. + /// When `k` is scalar or `k[0] == k[1]`, `M` is part of the batch size + /// [I, J, ..., M], and the output tensor is: + /// + /// ``` + /// output[i, j, ..., l, m, n] + /// = diagonal[i, j, ..., l, n-max(d_upper, 0)] ; if n - m == d_upper + /// padding_value ; otherwise + /// ``` + /// + /// Otherwise, `M` is treated as the number of diagonals for the matrix in the + /// same batch (`M = k[1]-k[0]+1`), and the output tensor is: + /// + /// ``` + /// output[i, j, ..., l, m, n] + /// = diagonal[i, j, ..., l, diag_index, index_in_diag] ; if k[0] <= d <= k[1] + /// padding_value ; otherwise + /// ``` + /// where `d = n - m`, `diag_index = [k] - d`, and + /// `index_in_diag = n - max(d, 0) + offset`. + /// + /// `offset` is zero except when the alignment of the diagonal is to the right. + /// ``` + /// offset = max_diag_len - diag_len(d) ; if (`align` in {RIGHT_LEFT, RIGHT_RIGHT} + /// and `d >= 0`) or + /// (`align` in {LEFT_RIGHT, RIGHT_RIGHT} + /// and `d <= 0`) + /// 0 ; otherwise + /// ``` + /// where `diag_len(d) = min(cols - max(d, 0), rows + min(d, 0))`. + /// + /// For example: + /// + /// ``` + /// # The main diagonal. + /// diagonal = np.array([[1, 2, 3, 4], # Input shape: (2, 4) + /// [5, 6, 7, 8]]) + /// tf.matrix_diag(diagonal) ==> [[[1, 0, 0, 0], # Output shape: (2, 4, 4) + /// [0, 2, 0, 0], + /// [0, 0, 3, 0], + /// [0, 0, 0, 4]], + /// [[5, 0, 0, 0], + /// [0, 6, 0, 0], + /// [0, 0, 7, 0], + /// [0, 0, 0, 8]]] + /// + /// # A superdiagonal (per batch). + /// diagonal = np.array([[1, 2, 3], # Input shape: (2, 3) + /// [4, 5, 6]]) + /// tf.matrix_diag(diagonal, k = 1) + /// ==> [[[0, 1, 0, 0], # Output shape: (2, 4, 4) + /// [0, 0, 2, 0], + /// [0, 0, 0, 3], + /// [0, 0, 0, 0]], + /// [[0, 4, 0, 0], + /// [0, 0, 5, 0], + /// [0, 0, 0, 6], + /// [0, 0, 0, 0]]] + /// + /// # A tridiagonal band (per batch). + /// diagonals = np.array([[[0, 8, 9], # Input shape: (2, 2, 3) + /// [1, 2, 3], + /// [4, 5, 0]], + /// [[0, 2, 3], + /// [6, 7, 9], + /// [9, 1, 0]]]) + /// tf.matrix_diag(diagonals, k = (-1, 1)) + /// ==> [[[1, 8, 0], # Output shape: (2, 3, 3) + /// [4, 2, 9], + /// [0, 5, 3]], + /// [[6, 2, 0], + /// [9, 7, 3], + /// [0, 1, 9]]] + /// + /// # LEFT_RIGHT alignment. + /// diagonals = np.array([[[8, 9, 0], # Input shape: (2, 2, 3) + /// [1, 2, 3], + /// [0, 4, 5]], + /// [[2, 3, 0], + /// [6, 7, 9], + /// [0, 9, 1]]]) + /// tf.matrix_diag(diagonals, k = (-1, 1), align="LEFT_RIGHT") + /// ==> [[[1, 8, 0], # Output shape: (2, 3, 3) + /// [4, 2, 9], + /// [0, 5, 3]], + /// [[6, 2, 0], + /// [9, 7, 3], + /// [0, 1, 9]]] + /// + /// # Rectangular matrix. + /// diagonal = np.array([1, 2]) # Input shape: (2) + /// tf.matrix_diag(diagonal, k = -1, num_rows = 3, num_cols = 4) + /// ==> [[0, 0, 0, 0], # Output shape: (3, 4) + /// [1, 0, 0, 0], + /// [0, 2, 0, 0]] + /// + /// # Rectangular matrix with inferred num_cols and padding_value = 9. + /// tf.matrix_diag(diagonal, k = -1, num_rows = 3, padding_value = 9) + /// ==> [[9, 9], # Output shape: (3, 2) + /// [1, 9], + /// [9, 2]] + /// + /// ``` + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// Some diagonals are shorter than `max_diag_len` and need to be padded. `align` is + /// a string specifying how superdiagonals and subdiagonals should be aligned, + /// respectively. There are four possible alignments: "RIGHT_LEFT" (default), + /// "LEFT_RIGHT", "LEFT_LEFT", and "RIGHT_RIGHT". "RIGHT_LEFT" aligns superdiagonals + /// to the right (left-pads the row) and subdiagonals to the left (right-pads the + /// row). It is the packing format LAPACK uses. cuSPARSE uses "LEFT_RIGHT", which is + /// the opposite alignment. + /// + /// + /// + public static Tensor matrix_diag_v3(Tensor diagonal, Tensor k, Tensor num_rows, Tensor num_cols, Tensor padding_value, string align = "RIGHT_LEFT", string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "MatrixDiagV3", name) { args = new object[] { diagonal, k, num_rows, num_cols, padding_value }, attrs = new Dictionary() { ["align"] = align } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return matrix_diag_v3_eager_fallback(diagonal, k, num_rows, num_cols, padding_value, align: align, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + if (align is null) + { + align = "RIGHT_LEFT"; + } + Dictionary keywords = new(); + keywords["diagonal"] = diagonal; + keywords["k"] = k; + keywords["num_rows"] = num_rows; + keywords["num_cols"] = num_cols; + keywords["padding_value"] = padding_value; + keywords["align"] = align; + var _op = tf.OpDefLib._apply_op_helper("MatrixDiagV3", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "align", _op.get_attr("align") }; + _execute.record_gradient("MatrixDiagV3", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor matrix_diag_v3_eager_fallback(Tensor diagonal, Tensor k, Tensor num_rows, Tensor num_cols, Tensor padding_value, string align, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { diagonal, k, num_rows, num_cols, padding_value }; + object[] _attrs = new object[] { "T", diagonal.dtype, "align", align }; + var _result = _execute.execute("MatrixDiagV3", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("MatrixDiagV3", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns a batched matrix tensor with new batched diagonal values. + /// + /// + /// + /// Given `input` and `diagonal`, this operation returns a tensor with the + /// same shape and values as `input`, except for the main diagonal of the + /// innermost matrices. These will be overwritten by the values in `diagonal`. + /// + /// The output is computed as follows: + /// + /// Assume `input` has `k+1` dimensions `[I, J, K, ..., M, N]` and `diagonal` has + /// `k` dimensions `[I, J, K, ..., min(M, N)]`. Then the output is a + /// tensor of rank `k+1` with dimensions `[I, J, K, ..., M, N]` where: + /// + /// * `output[i, j, k, ..., m, n] = diagonal[i, j, k, ..., n]` for `m == n`. + /// * `output[i, j, k, ..., m, n] = input[i, j, k, ..., m, n]` for `m != n`. + /// + /// + /// + /// + /// + public static Tensor matrix_set_diag(Tensor input, Tensor diagonal, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "MatrixSetDiag", name) { args = new object[] { input, diagonal }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return matrix_set_diag_eager_fallback(input, diagonal, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["diagonal"] = diagonal; + var _op = tf.OpDefLib._apply_op_helper("MatrixSetDiag", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("MatrixSetDiag", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor matrix_set_diag_eager_fallback(Tensor input, Tensor diagonal, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, diagonal }; + object[] _attrs = new object[] { "T", input.dtype }; + var _result = _execute.execute("MatrixSetDiag", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("MatrixSetDiag", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns a batched matrix tensor with new batched diagonal values. + /// + /// + /// + /// Given `input` and `diagonal`, this operation returns a tensor with the + /// same shape and values as `input`, except for the specified diagonals of the + /// innermost matrices. These will be overwritten by the values in `diagonal`. + /// + /// `input` has `r+1` dimensions `[I, J, ..., L, M, N]`. When `k` is scalar or + /// `k[0] == k[1]`, `diagonal` has `r` dimensions `[I, J, ..., L, max_diag_len]`. + /// Otherwise, it has `r+1` dimensions `[I, J, ..., L, num_diags, max_diag_len]`. + /// `num_diags` is the number of diagonals, `num_diags = k[1] - k[0] + 1`. + /// `max_diag_len` is the longest diagonal in the range `[k[0], k[1]]`, + /// `max_diag_len = min(M + min(k[1], 0), N + min(-k[0], 0))` + /// + /// The output is a tensor of rank `k+1` with dimensions `[I, J, ..., L, M, N]`. + /// If `k` is scalar or `k[0] == k[1]`: + /// + /// ``` + /// output[i, j, ..., l, m, n] + /// = diagonal[i, j, ..., l, n-max(k[1], 0)] ; if n - m == k[1] + /// input[i, j, ..., l, m, n] ; otherwise + /// ``` + /// + /// Otherwise, + /// + /// ``` + /// output[i, j, ..., l, m, n] + /// = diagonal[i, j, ..., l, diag_index, index_in_diag] ; if k[0] <= d <= k[1] + /// input[i, j, ..., l, m, n] ; otherwise + /// ``` + /// where `d = n - m`, `diag_index = k[1] - d`, and `index_in_diag = n - max(d, 0)`. + /// + /// For example: + /// + /// ``` + /// # The main diagonal. + /// input = np.array([[[7, 7, 7, 7], # Input shape: (2, 3, 4) + /// [7, 7, 7, 7], + /// [7, 7, 7, 7]], + /// [[7, 7, 7, 7], + /// [7, 7, 7, 7], + /// [7, 7, 7, 7]]]) + /// diagonal = np.array([[1, 2, 3], # Diagonal shape: (2, 3) + /// [4, 5, 6]]) + /// tf.matrix_set_diag(diagonal) ==> [[[1, 7, 7, 7], # Output shape: (2, 3, 4) + /// [7, 2, 7, 7], + /// [7, 7, 3, 7]], + /// [[4, 7, 7, 7], + /// [7, 5, 7, 7], + /// [7, 7, 6, 7]]] + /// + /// # A superdiagonal (per batch). + /// tf.matrix_set_diag(diagonal, k = 1) + /// ==> [[[7, 1, 7, 7], # Output shape: (2, 3, 4) + /// [7, 7, 2, 7], + /// [7, 7, 7, 3]], + /// [[7, 4, 7, 7], + /// [7, 7, 5, 7], + /// [7, 7, 7, 6]]] + /// + /// # A band of diagonals. + /// diagonals = np.array([[[1, 2, 3], # Diagonal shape: (2, 2, 3) + /// [4, 5, 0]], + /// [[6, 1, 2], + /// [3, 4, 0]]]) + /// tf.matrix_set_diag(diagonals, k = (-1, 0)) + /// ==> [[[1, 7, 7, 7], # Output shape: (2, 3, 4) + /// [4, 2, 7, 7], + /// [0, 5, 3, 7]], + /// [[6, 7, 7, 7], + /// [3, 1, 7, 7], + /// [7, 4, 2, 7]]] + /// + /// ``` + /// + /// + /// + /// + /// + /// + public static Tensor matrix_set_diag_v2(Tensor input, Tensor diagonal, Tensor k, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "MatrixSetDiagV2", name) { args = new object[] { input, diagonal, k }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return matrix_set_diag_v2_eager_fallback(input, diagonal, k, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["diagonal"] = diagonal; + keywords["k"] = k; + var _op = tf.OpDefLib._apply_op_helper("MatrixSetDiagV2", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("MatrixSetDiagV2", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor matrix_set_diag_v2_eager_fallback(Tensor input, Tensor diagonal, Tensor k, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, diagonal, k }; + object[] _attrs = new object[] { "T", input.dtype }; + var _result = _execute.execute("MatrixSetDiagV2", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("MatrixSetDiagV2", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns a batched matrix tensor with new batched diagonal values. + /// + /// + /// + /// Given `input` and `diagonal`, this operation returns a tensor with the + /// same shape and values as `input`, except for the specified diagonals of the + /// innermost matrices. These will be overwritten by the values in `diagonal`. + /// + /// `input` has `r+1` dimensions `[I, J, ..., L, M, N]`. When `k` is scalar or + /// `k[0] == k[1]`, `diagonal` has `r` dimensions `[I, J, ..., L, max_diag_len]`. + /// Otherwise, it has `r+1` dimensions `[I, J, ..., L, num_diags, max_diag_len]`. + /// `num_diags` is the number of diagonals, `num_diags = k[1] - k[0] + 1`. + /// `max_diag_len` is the longest diagonal in the range `[k[0], k[1]]`, + /// `max_diag_len = min(M + min(k[1], 0), N + min(-k[0], 0))` + /// + /// The output is a tensor of rank `k+1` with dimensions `[I, J, ..., L, M, N]`. + /// If `k` is scalar or `k[0] == k[1]`: + /// + /// ``` + /// output[i, j, ..., l, m, n] + /// = diagonal[i, j, ..., l, n-max(k[1], 0)] ; if n - m == k[1] + /// input[i, j, ..., l, m, n] ; otherwise + /// ``` + /// + /// Otherwise, + /// + /// ``` + /// output[i, j, ..., l, m, n] + /// = diagonal[i, j, ..., l, diag_index, index_in_diag] ; if k[0] <= d <= k[1] + /// input[i, j, ..., l, m, n] ; otherwise + /// ``` + /// where `d = n - m`, `diag_index = k[1] - d`, and + /// `index_in_diag = n - max(d, 0) + offset`. + /// + /// `offset` is zero except when the alignment of the diagonal is to the right. + /// ``` + /// offset = max_diag_len - diag_len(d) ; if (`align` in {RIGHT_LEFT, RIGHT_RIGHT} + /// and `d >= 0`) or + /// (`align` in {LEFT_RIGHT, RIGHT_RIGHT} + /// and `d <= 0`) + /// 0 ; otherwise + /// ``` + /// where `diag_len(d) = min(cols - max(d, 0), rows + min(d, 0))`. + /// + /// For example: + /// + /// ``` + /// # The main diagonal. + /// input = np.array([[[7, 7, 7, 7], # Input shape: (2, 3, 4) + /// [7, 7, 7, 7], + /// [7, 7, 7, 7]], + /// [[7, 7, 7, 7], + /// [7, 7, 7, 7], + /// [7, 7, 7, 7]]]) + /// diagonal = np.array([[1, 2, 3], # Diagonal shape: (2, 3) + /// [4, 5, 6]]) + /// tf.matrix_set_diag(input, diagonal) + /// ==> [[[1, 7, 7, 7], # Output shape: (2, 3, 4) + /// [7, 2, 7, 7], + /// [7, 7, 3, 7]], + /// [[4, 7, 7, 7], + /// [7, 5, 7, 7], + /// [7, 7, 6, 7]]] + /// + /// # A superdiagonal (per batch). + /// tf.matrix_set_diag(input, diagonal, k = 1) + /// ==> [[[7, 1, 7, 7], # Output shape: (2, 3, 4) + /// [7, 7, 2, 7], + /// [7, 7, 7, 3]], + /// [[7, 4, 7, 7], + /// [7, 7, 5, 7], + /// [7, 7, 7, 6]]] + /// + /// # A band of diagonals. + /// diagonals = np.array([[[0, 9, 1], # Diagonal shape: (2, 4, 3) + /// [6, 5, 8], + /// [1, 2, 3], + /// [4, 5, 0]], + /// [[0, 1, 2], + /// [5, 6, 4], + /// [6, 1, 2], + /// [3, 4, 0]]]) + /// tf.matrix_set_diag(input, diagonals, k = (-1, 2)) + /// ==> [[[1, 6, 9, 7], # Output shape: (2, 3, 4) + /// [4, 2, 5, 1], + /// [7, 5, 3, 8]], + /// [[6, 5, 1, 7], + /// [3, 1, 6, 2], + /// [7, 4, 2, 4]]] + /// + /// # LEFT_RIGHT alignment. + /// diagonals = np.array([[[9, 1, 0], # Diagonal shape: (2, 4, 3) + /// [6, 5, 8], + /// [1, 2, 3], + /// [0, 4, 5]], + /// [[1, 2, 0], + /// [5, 6, 4], + /// [6, 1, 2], + /// [0, 3, 4]]]) + /// tf.matrix_set_diag(input, diagonals, k = (-1, 2), align="LEFT_RIGHT") + /// ==> [[[1, 6, 9, 7], # Output shape: (2, 3, 4) + /// [4, 2, 5, 1], + /// [7, 5, 3, 8]], + /// [[6, 5, 1, 7], + /// [3, 1, 6, 2], + /// [7, 4, 2, 4]]] + /// + /// ``` + /// + /// + /// + /// + /// + /// + /// + /// Some diagonals are shorter than `max_diag_len` and need to be padded. `align` is + /// a string specifying how superdiagonals and subdiagonals should be aligned, + /// respectively. There are four possible alignments: "RIGHT_LEFT" (default), + /// "LEFT_RIGHT", "LEFT_LEFT", and "RIGHT_RIGHT". "RIGHT_LEFT" aligns superdiagonals + /// to the right (left-pads the row) and subdiagonals to the left (right-pads the + /// row). It is the packing format LAPACK uses. cuSPARSE uses "LEFT_RIGHT", which is + /// the opposite alignment. + /// + /// + /// + public static Tensor matrix_set_diag_v3(Tensor input, Tensor diagonal, Tensor k, string align = "RIGHT_LEFT", string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "MatrixSetDiagV3", name) { args = new object[] { input, diagonal, k }, attrs = new Dictionary() { ["align"] = align } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return matrix_set_diag_v3_eager_fallback(input, diagonal, k, align: align, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + if (align is null) + { + align = "RIGHT_LEFT"; + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["diagonal"] = diagonal; + keywords["k"] = k; + keywords["align"] = align; + var _op = tf.OpDefLib._apply_op_helper("MatrixSetDiagV3", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "align", _op.get_attr("align") }; + _execute.record_gradient("MatrixSetDiagV3", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor matrix_set_diag_v3_eager_fallback(Tensor input, Tensor diagonal, Tensor k, string align, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, diagonal, k }; + object[] _attrs = new object[] { "T", input.dtype, "align", align }; + var _result = _execute.execute("MatrixSetDiagV3", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("MatrixSetDiagV3", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Pads a tensor with mirrored values. + /// + /// + /// + /// This operation pads a `input` with mirrored values according to the `paddings` + /// you specify. `paddings` is an integer tensor with shape `[n, 2]`, where n is + /// the rank of `input`. For each dimension D of `input`, `paddings[D, 0]` indicates + /// how many values to add before the contents of `input` in that dimension, and + /// `paddings[D, 1]` indicates how many values to add after the contents of `input` + /// in that dimension. Both `paddings[D, 0]` and `paddings[D, 1]` must be no greater + /// than `input.dim_size(D)` (or `input.dim_size(D) - 1`) if `copy_border` is true + /// (if false, respectively). + /// + /// The padded size of each dimension D of the output is: + /// + /// `paddings(D, 0) + input.dim_size(D) + paddings(D, 1)` + /// + /// For example: + /// + /// ``` + /// # 't' is [[1, 2, 3], [4, 5, 6]]. + /// # 'paddings' is [[1, 1]], [2, 2]]. + /// # 'mode' is SYMMETRIC. + /// # rank of 't' is 2. + /// pad(t, paddings) ==> [[2, 1, 1, 2, 3, 3, 2] + /// [2, 1, 1, 2, 3, 3, 2] + /// [5, 4, 4, 5, 6, 6, 5] + /// [5, 4, 4, 5, 6, 6, 5]] + /// ``` + /// + /// + /// + /// + /// + /// + /// Either `REFLECT` or `SYMMETRIC`. In reflect mode the padded regions + /// do not include the borders, while in symmetric mode the padded regions + /// do include the borders. For example, if `input` is `[1, 2, 3]` and `paddings` + /// is `[0, 2]`, then the output is `[1, 2, 3, 2, 1]` in reflect mode, and + /// it is `[1, 2, 3, 3, 2]` in symmetric mode. + /// + /// + /// + public static Tensor mirror_pad(Tensor input, Tensor paddings, string mode, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "MirrorPad", name) { args = new object[] { input, paddings }, attrs = new Dictionary() { ["mode"] = mode } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return mirror_pad_eager_fallback(input, paddings, mode: mode, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["paddings"] = paddings; + keywords["mode"] = mode; + var _op = tf.OpDefLib._apply_op_helper("MirrorPad", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "Tpaddings", _op._get_attr_type("Tpaddings"), "mode", _op.get_attr("mode") }; + _execute.record_gradient("MirrorPad", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor mirror_pad_eager_fallback(Tensor input, Tensor paddings, string mode, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, paddings }; + object[] _attrs = new object[] { "T", input.dtype, "Tpaddings", paddings.dtype, "mode", mode }; + var _result = _execute.execute("MirrorPad", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("MirrorPad", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Gradient op for `MirrorPad` op. This op folds a mirror-padded tensor. + /// + /// + /// + /// This operation folds the padded areas of `input` by `MirrorPad` according to the + /// `paddings` you specify. `paddings` must be the same as `paddings` argument + /// given to the corresponding `MirrorPad` op. + /// + /// The folded size of each dimension D of the output is: + /// + /// `input.dim_size(D) - paddings(D, 0) - paddings(D, 1)` + /// + /// For example: + /// + /// ``` + /// # 't' is [[1, 2, 3], [4, 5, 6], [7, 8, 9]]. + /// # 'paddings' is [[0, 1]], [0, 1]]. + /// # 'mode' is SYMMETRIC. + /// # rank of 't' is 2. + /// pad(t, paddings) ==> [[ 1, 5] + /// [11, 28]] + /// ``` + /// + /// + /// + /// + /// + /// + /// The mode used in the `MirrorPad` op. + /// + /// + /// + public static Tensor mirror_pad_grad(Tensor input, Tensor paddings, string mode, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "MirrorPadGrad", name) { args = new object[] { input, paddings }, attrs = new Dictionary() { ["mode"] = mode } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return mirror_pad_grad_eager_fallback(input, paddings, mode: mode, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["paddings"] = paddings; + keywords["mode"] = mode; + var _op = tf.OpDefLib._apply_op_helper("MirrorPadGrad", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "Tpaddings", _op._get_attr_type("Tpaddings"), "mode", _op.get_attr("mode") }; + _execute.record_gradient("MirrorPadGrad", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor mirror_pad_grad_eager_fallback(Tensor input, Tensor paddings, string mode, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, paddings }; + object[] _attrs = new object[] { "T", input.dtype, "Tpaddings", paddings.dtype, "mode", mode }; + var _result = _execute.execute("MirrorPadGrad", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("MirrorPadGrad", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns a one-hot tensor. + /// + /// + /// + /// The locations represented by indices in `indices` take value `on_value`, + /// while all other locations take value `off_value`. + /// + /// If the input `indices` is rank `N`, the output will have rank `N+1`, + /// The new axis is created at dimension `axis` (default: the new axis is + /// appended at the end). + /// + /// If `indices` is a scalar the output shape will be a vector of length `depth`. + /// + /// If `indices` is a vector of length `features`, the output shape will be: + /// ``` + /// features x depth if axis == -1 + /// depth x features if axis == 0 + /// ``` + /// + /// If `indices` is a matrix (batch) with shape `[batch, features]`, + /// the output shape will be: + /// ``` + /// batch x features x depth if axis == -1 + /// batch x depth x features if axis == 1 + /// depth x batch x features if axis == 0 + /// ``` + /// + /// + /// Examples + /// ========= + /// + /// Suppose that + /// ``` + /// indices = [0, 2, -1, 1] + /// depth = 3 + /// on_value = 5.0 + /// off_value = 0.0 + /// axis = -1 + /// ``` + /// + /// Then output is `[4 x 3]`: + /// ``` + /// output = + /// [5.0 0.0 0.0] // one_hot(0) + /// [0.0 0.0 5.0] // one_hot(2) + /// [0.0 0.0 0.0] // one_hot(-1) + /// [0.0 5.0 0.0] // one_hot(1) + /// ``` + /// + /// Suppose that + /// ``` + /// indices = [0, 2, -1, 1] + /// depth = 3 + /// on_value = 0.0 + /// off_value = 3.0 + /// axis = 0 + /// ``` + /// + /// Then output is `[3 x 4]`: + /// ``` + /// output = + /// [0.0 3.0 3.0 3.0] + /// [3.0 3.0 3.0 0.0] + /// [3.0 3.0 3.0 3.0] + /// [3.0 0.0 3.0 3.0] + /// // ^ one_hot(0) + /// // ^ one_hot(2) + /// // ^ one_hot(-1) + /// // ^ one_hot(1) + /// ``` + /// + /// Suppose that + /// ``` + /// indices = [[0, 2], [1, -1]] + /// depth = 3 + /// on_value = 1.0 + /// off_value = 0.0 + /// axis = -1 + /// ``` + /// + /// Then output is `[2 x 2 x 3]`: + /// ``` + /// output = + /// [ + /// [1.0, 0.0, 0.0] // one_hot(0) + /// [0.0, 0.0, 1.0] // one_hot(2) + /// ][ + /// [0.0, 1.0, 0.0] // one_hot(1) + /// [0.0, 0.0, 0.0] // one_hot(-1) + /// ] + /// ``` + /// + /// + /// + /// + /// + /// + /// + /// + /// The axis to fill (default: -1, a new inner-most axis). + /// + /// + /// + public static Tensor one_hot(Tensor indices, Tensor depth, Tensor on_value, Tensor off_value, int axis = -1, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "OneHot", name) { args = new object[] { indices, depth, on_value, off_value }, attrs = new Dictionary() { ["axis"] = axis } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return one_hot_eager_fallback(indices, depth, on_value, off_value, axis: axis, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["indices"] = indices; + keywords["depth"] = depth; + keywords["on_value"] = on_value; + keywords["off_value"] = off_value; + keywords["axis"] = axis; + var _op = tf.OpDefLib._apply_op_helper("OneHot", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "axis", _op._get_attr_int("axis"), "T", _op._get_attr_type("T"), "TI", _op._get_attr_type("TI") }; + _execute.record_gradient("OneHot", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor one_hot_eager_fallback(Tensor indices, Tensor depth, Tensor on_value, Tensor off_value, int axis, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { indices, depth, on_value, off_value }; + object[] _attrs = new object[] { "axis", axis, "T", on_value.dtype, "TI", indices.dtype }; + var _result = _execute.execute("OneHot", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("OneHot", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns a tensor of ones with the same shape and type as x. + /// + /// + /// + public static Tensor ones_like(Tensor x, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "OnesLike", name) { args = new object[] { x }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return ones_like_eager_fallback(x, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + var _op = tf.OpDefLib._apply_op_helper("OnesLike", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("OnesLike", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor ones_like_eager_fallback(Tensor x, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("OnesLike", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("OnesLike", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Packs a list of `N` rank-`R` tensors into one rank-`(R+1)` tensor. + /// + /// + /// + /// Packs the `N` tensors in `values` into a tensor with rank one higher than each + /// tensor in `values`, by packing them along the `axis` dimension. + /// Given a list of tensors of shape `(A, B, C)`; + /// + /// if `axis == 0` then the `output` tensor will have the shape `(N, A, B, C)`. + /// if `axis == 1` then the `output` tensor will have the shape `(A, N, B, C)`. + /// Etc. + /// + /// For example: + /// + /// ``` + /// # 'x' is [1, 4] + /// # 'y' is [2, 5] + /// # 'z' is [3, 6] + /// pack([x, y, z]) => [[1, 4], [2, 5], [3, 6]] # Pack along first dim. + /// pack([x, y, z], axis=1) => [[1, 2, 3], [4, 5, 6]] + /// ``` + /// + /// This is the opposite of `unpack`. + /// + /// + /// + /// + /// + /// Dimension along which to pack. Negative values wrap around, so the + /// valid range is `[-(R+1), R+1)`. + /// + /// + /// + public static Tensor pack(Tensors values, int axis = 0, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Pack", name) { args = new object[] { values }, attrs = new Dictionary() { ["axis"] = axis } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return pack_eager_fallback(values, axis: axis, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["values"] = values; + keywords["axis"] = axis; + var _op = tf.OpDefLib._apply_op_helper("Pack", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "N", _op._get_attr_int("N"), "T", _op._get_attr_type("T"), "axis", _op._get_attr_int("axis") }; + _execute.record_gradient("Pack", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor pack_eager_fallback(Tensors values, int axis, string name, Context ctx) + { + List _inputs_flat_list = new(); + _inputs_flat_list.AddRange(values); + var _inputs_flat = _inputs_flat_list.ToArray(); + object[] _attrs = new object[] { "N", values.Length, "T", values.dtype, "axis", axis }; + var _result = _execute.execute("Pack", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Pack", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Pads a tensor with zeros. + /// + /// + /// + /// This operation pads a `input` with zeros according to the `paddings` you + /// specify. `paddings` is an integer tensor with shape `[Dn, 2]`, where n is the + /// rank of `input`. For each dimension D of `input`, `paddings[D, 0]` indicates + /// how many zeros to add before the contents of `input` in that dimension, and + /// `paddings[D, 1]` indicates how many zeros to add after the contents of `input` + /// in that dimension. + /// + /// The padded size of each dimension D of the output is: + /// + /// `paddings(D, 0) + input.dim_size(D) + paddings(D, 1)` + /// + /// For example: + /// + /// ``` + /// # 't' is [[1, 1], [2, 2]] + /// # 'paddings' is [[1, 1], [2, 2]] + /// # rank of 't' is 2 + /// pad(t, paddings) ==> [[0, 0, 0, 0, 0, 0] + /// [0, 0, 1, 1, 0, 0] + /// [0, 0, 2, 2, 0, 0] + /// [0, 0, 0, 0, 0, 0]] + /// ``` + /// + /// + /// + /// + /// + /// + public static Tensor pad(Tensor input, Tensor paddings, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Pad", name) { args = new object[] { input, paddings }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return pad_eager_fallback(input, paddings, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["paddings"] = paddings; + var _op = tf.OpDefLib._apply_op_helper("Pad", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "Tpaddings", _op._get_attr_type("Tpaddings") }; + _execute.record_gradient("Pad", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor pad_eager_fallback(Tensor input, Tensor paddings, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, paddings }; + object[] _attrs = new object[] { "T", input.dtype, "Tpaddings", paddings.dtype }; + var _result = _execute.execute("Pad", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Pad", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Pads a tensor. + /// + /// + /// + /// This operation pads `input` according to the `paddings` and `constant_values` + /// you specify. `paddings` is an integer tensor with shape `[Dn, 2]`, where n is + /// the rank of `input`. For each dimension D of `input`, `paddings[D, 0]` indicates + /// how many padding values to add before the contents of `input` in that dimension, + /// and `paddings[D, 1]` indicates how many padding values to add after the contents + /// of `input` in that dimension. `constant_values` is a scalar tensor of the same + /// type as `input` that indicates the value to use for padding `input`. + /// + /// The padded size of each dimension D of the output is: + /// + /// `paddings(D, 0) + input.dim_size(D) + paddings(D, 1)` + /// + /// For example: + /// + /// ``` + /// # 't' is [[1, 1], [2, 2]] + /// # 'paddings' is [[1, 1], [2, 2]] + /// # 'constant_values' is 0 + /// # rank of 't' is 2 + /// pad(t, paddings) ==> [[0, 0, 0, 0, 0, 0] + /// [0, 0, 1, 1, 0, 0] + /// [0, 0, 2, 2, 0, 0] + /// [0, 0, 0, 0, 0, 0]] + /// ``` + /// + /// + /// + /// + /// + /// + public static Tensor pad_v2(Tensor input, Tensor paddings, Tensor constant_values, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "PadV2", name) { args = new object[] { input, paddings, constant_values }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return pad_v2_eager_fallback(input, paddings, constant_values, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["paddings"] = paddings; + keywords["constant_values"] = constant_values; + var _op = tf.OpDefLib._apply_op_helper("PadV2", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "Tpaddings", _op._get_attr_type("Tpaddings") }; + _execute.record_gradient("PadV2", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor pad_v2_eager_fallback(Tensor input, Tensor paddings, Tensor constant_values, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, paddings, constant_values }; + object[] _attrs = new object[] { "T", input.dtype, "Tpaddings", paddings.dtype }; + var _result = _execute.execute("PadV2", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("PadV2", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Concatenates a list of `N` tensors along the first dimension. + /// + /// + /// + /// The input tensors are all required to have size 1 in the first dimension. + /// + /// For example: + /// + /// ``` + /// # 'x' is [[1, 4]] + /// # 'y' is [[2, 5]] + /// # 'z' is [[3, 6]] + /// parallel_concat([x, y, z]) => [[1, 4], [2, 5], [3, 6]] # Pack along first dim. + /// ``` + /// + /// The difference between concat and parallel_concat is that concat requires all + /// of the inputs be computed before the operation will begin but doesn't require + /// that the input shapes be known during graph construction. Parallel concat + /// will copy pieces of the input into the output as they become available, in + /// some situations this can provide a performance benefit. + /// + /// + /// + /// + /// + /// the final shape of the result; should be equal to the shapes of any input + /// but with the number of input values in the first dimension. + /// + /// + /// + public static Tensor parallel_concat(Tensors values, Shape shape, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "ParallelConcat", name) { args = new object[] { values }, attrs = new Dictionary() { ["shape"] = shape } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return parallel_concat_eager_fallback(values, shape: shape, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["values"] = values; + keywords["shape"] = shape; + var _op = tf.OpDefLib._apply_op_helper("ParallelConcat", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "N", _op._get_attr_int("N"), "T", _op._get_attr_type("T"), "shape", _op.get_attr("shape") }; + _execute.record_gradient("ParallelConcat", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor parallel_concat_eager_fallback(Tensors values, Shape shape, string name, Context ctx) + { + List _inputs_flat_list = new(); + _inputs_flat_list.AddRange(values); + var _inputs_flat = _inputs_flat_list.ToArray(); + object[] _attrs = new object[] { "N", values.Length, "T", values.dtype, "shape", shape }; + var _result = _execute.execute("ParallelConcat", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("ParallelConcat", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// A placeholder op for a value that will be fed into the computation. + /// + /// + /// + /// N.B. This operation will fail with an error if it is executed. It is + /// intended as a way to represent a value that will always be fed, and to + /// provide attrs that enable the fed value to be checked at runtime. + /// + /// + /// + /// + /// The type of elements in the tensor. + /// + /// + /// + /// + /// (Optional) The shape of the tensor. If the shape has 0 dimensions, the + /// shape is unconstrained. + /// + /// + /// + public static Tensor placeholder(TF_DataType dtype, Shape shape = null, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Placeholder", name) { args = new object[] { }, attrs = new Dictionary() { ["dtype"] = dtype, ["shape"] = shape } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return placeholder_eager_fallback(dtype: dtype, shape: shape, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["dtype"] = dtype; + keywords["shape"] = shape; + var _op = tf.OpDefLib._apply_op_helper("Placeholder", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "dtype", _op._get_attr_type("dtype"), "shape", _op.get_attr("shape") }; + _execute.record_gradient("Placeholder", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor placeholder_eager_fallback(TF_DataType dtype, Shape shape, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { }; + object[] _attrs = new object[] { "dtype", dtype, "shape", shape }; + var _result = _execute.execute("Placeholder", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Placeholder", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// A placeholder op for a value that will be fed into the computation. + /// + /// + /// + /// N.B. This operation will fail with an error if it is executed. It is + /// intended as a way to represent a value that will always be fed, and to + /// provide attrs that enable the fed value to be checked at runtime. + /// + /// + /// + /// + /// The type of elements in the tensor. + /// + /// + /// + /// + /// The shape of the tensor. The shape can be any partially-specified + /// shape. To be unconstrained, pass in a shape with unknown rank. + /// + /// + /// + public static Tensor placeholder_v2(TF_DataType dtype, Shape shape, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "PlaceholderV2", name) { args = new object[] { }, attrs = new Dictionary() { ["dtype"] = dtype, ["shape"] = shape } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return placeholder_v2_eager_fallback(dtype: dtype, shape: shape, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["dtype"] = dtype; + keywords["shape"] = shape; + var _op = tf.OpDefLib._apply_op_helper("PlaceholderV2", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "dtype", _op._get_attr_type("dtype"), "shape", _op.get_attr("shape") }; + _execute.record_gradient("PlaceholderV2", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor placeholder_v2_eager_fallback(TF_DataType dtype, Shape shape, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { }; + object[] _attrs = new object[] { "dtype", dtype, "shape", shape }; + var _result = _execute.execute("PlaceholderV2", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("PlaceholderV2", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// A placeholder op that passes through `input` when its output is not fed. + /// + /// + /// + /// + /// The (possibly partial) shape of the tensor. + /// + /// + /// + public static Tensor placeholder_with_default(Tensor input, Shape shape, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "PlaceholderWithDefault", name) { args = new object[] { input }, attrs = new Dictionary() { ["shape"] = shape } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return placeholder_with_default_eager_fallback(input, shape: shape, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["shape"] = shape; + var _op = tf.OpDefLib._apply_op_helper("PlaceholderWithDefault", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "dtype", _op._get_attr_type("dtype"), "shape", _op.get_attr("shape") }; + _execute.record_gradient("PlaceholderWithDefault", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor placeholder_with_default_eager_fallback(Tensor input, Shape shape, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input }; + object[] _attrs = new object[] { "dtype", input.dtype, "shape", shape }; + var _result = _execute.execute("PlaceholderWithDefault", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("PlaceholderWithDefault", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// An identity op that triggers an error if a gradient is requested. + /// + /// + /// + /// When executed in a graph, this op outputs its input tensor as-is. + /// + /// When building ops to compute gradients, the TensorFlow gradient system + /// will return an error when trying to lookup the gradient of this op, + /// because no gradient must ever be registered for this function. This + /// op exists to prevent subtle bugs from silently returning unimplemented + /// gradients in some corner cases. + /// + /// + /// + /// + /// + /// Will be printed in the error when anyone tries to differentiate + /// this operation. + /// + /// + /// + public static Tensor prevent_gradient(Tensor input, string message = "", string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "PreventGradient", name) { args = new object[] { input }, attrs = new Dictionary() { ["message"] = message } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return prevent_gradient_eager_fallback(input, message: message, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + if (message is null) + { + message = ""; + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["message"] = message; + var _op = tf.OpDefLib._apply_op_helper("PreventGradient", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "message", _op.get_attr("message") }; + _execute.record_gradient("PreventGradient", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor prevent_gradient_eager_fallback(Tensor input, string message, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input }; + object[] _attrs = new object[] { "T", input.dtype, "message", message }; + var _result = _execute.execute("PreventGradient", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("PreventGradient", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Use QuantizeAndDequantizeV2 instead. + /// + /// + /// + /// + /// + /// + /// + /// + public static Tensor quantize_and_dequantize(Tensor input, bool signed_input = true, int num_bits = 8, bool range_given = false, float input_min = 0f, float input_max = 0f, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "QuantizeAndDequantize", name) { args = new object[] { input }, attrs = new Dictionary() { ["signed_input"] = signed_input, ["num_bits"] = num_bits, ["range_given"] = range_given, ["input_min"] = input_min, ["input_max"] = input_max } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return quantize_and_dequantize_eager_fallback(input, signed_input: signed_input, num_bits: num_bits, range_given: range_given, input_min: input_min, input_max: input_max, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["signed_input"] = signed_input; + keywords["num_bits"] = num_bits; + keywords["range_given"] = range_given; + keywords["input_min"] = input_min; + keywords["input_max"] = input_max; + var _op = tf.OpDefLib._apply_op_helper("QuantizeAndDequantize", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "signed_input", _op._get_attr_bool("signed_input"), "num_bits", _op._get_attr_int("num_bits"), "range_given", _op._get_attr_bool("range_given"), "input_min", _op.get_attr("input_min"), "input_max", _op.get_attr("input_max"), "T", _op._get_attr_type("T") }; + _execute.record_gradient("QuantizeAndDequantize", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor quantize_and_dequantize_eager_fallback(Tensor input, bool signed_input, int num_bits, bool range_given, float input_min, float input_max, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input }; + object[] _attrs = new object[] { "signed_input", signed_input, "num_bits", num_bits, "range_given", range_given, "input_min", input_min, "input_max", input_max, "T", input.dtype }; + var _result = _execute.execute("QuantizeAndDequantize", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("QuantizeAndDequantize", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Quantizes then dequantizes a tensor. + /// + /// + /// + /// This op simulates the precision loss from the quantized forward pass by: + /// + /// 1. Quantizing the tensor to fixed point numbers, which should match the target + /// quantization method when it is used in inference. + /// 2. Dequantizing it back to floating point numbers for the following ops, most + /// likely matmul. + /// + /// There are different ways to quantize. This version uses only scaling, so 0.0 + /// maps to 0. + /// + /// From the specified 'num_bits' in the quantized output type, it determines + /// minimum and maximum representable quantized values. + /// + /// e.g. + /// + /// * [-128, 127] for signed, num_bits = 8, or + /// * [0, 255] for unsigned, num_bits = 8. + /// + /// If range_given == False, the initial input_min, input_max will be determined + /// automatically as the minimum and maximum values in the input tensor, otherwise + /// the specified values of input_min, input_max are used. + /// + /// Note: If the input_min, input_max are specified, they do not need to equal the + /// actual minimum and maximum values in the tensor. e.g. in some cases it may be + /// beneficial to specify these values such that the low probability extremes of the + /// input distribution are clipped. + /// + /// This op determines the maximum scale_factor that would map the initial + /// [input_min, input_max] range to a range that lies within the representable + /// quantized range. + /// + /// It determines the scale from one of input_min and input_max, then updates the + /// other one to maximize the representable range. + /// + /// e.g. + /// + /// * if the output is signed, num_bits = 8, [input_min, input_max] = [-10.0, + /// 5.0]: it would use a scale_factor of -128 / -10.0 = 12.8 In this case, it + /// would update input_max to be 127 / 12.8 = 9.921875 + /// * if the output is signed, num_bits = 8, [input_min, input_max] = [-10.0, + /// 10.0]: it would use a scale_factor of 127 / 10.0 = 12.7 In this case, it + /// would update input_min to be 128.0 / 12.7 = -10.07874 + /// * if the output is unsigned, input_min is forced to be 0, and only the + /// specified input_max is used. + /// + /// After determining the scale_factor and updating the input range, it applies the + /// following to each value in the 'input' tensor. + /// + /// output = round(clamp(value, input_min, input_max) * scale_factor) / scale_factor. + /// + /// The above round function rounds the value based on the given round_mode. + /// + /// + /// + /// + /// + /// + /// + /// + /// Whether the quantization is signed or unsigned. (actually this parameter should + /// have been called `signed_output`) + /// + /// + /// + /// + /// The bitwidth of the quantization. + /// + /// + /// + /// + /// Whether the range is given or should be determined from the `input` tensor. + /// + /// + /// + /// + /// The 'round_mode' attribute controls which rounding tie-breaking algorithm is + /// used when rounding float values to their quantized equivalents. The following + /// rounding modes are currently supported: + /// + /// * HALF_TO_EVEN: this is the default round_mode. + /// * HALF_UP: round towards positive. In this mode 7.5 rounds up to 8 and -7.5 + /// rounds up to -7. + /// + /// + /// + /// + /// + /// If True, then the absolute value of the quantized minimum value is the same as + /// the quantized maximum value, instead of 1 greater. + /// i.e. for 8 bit quantization, the minimum value is -127 instead of -128. + /// + /// + /// + /// + /// If specified, this axis is treated as a channel or slice axis, and a separate + /// quantization range is used for each channel or slice along this axis. + /// + /// + /// + public static Tensor quantize_and_dequantize_v2(Tensor input, Tensor input_min, Tensor input_max, bool signed_input = true, int num_bits = 8, bool range_given = false, string round_mode = "HALF_TO_EVEN", bool narrow_range = false, int axis = -1, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "QuantizeAndDequantizeV2", name) { args = new object[] { input, input_min, input_max }, attrs = new Dictionary() { ["signed_input"] = signed_input, ["num_bits"] = num_bits, ["range_given"] = range_given, ["round_mode"] = round_mode, ["narrow_range"] = narrow_range, ["axis"] = axis } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return quantize_and_dequantize_v2_eager_fallback(input, input_min, input_max, signed_input: signed_input, num_bits: num_bits, range_given: range_given, round_mode: round_mode, narrow_range: narrow_range, axis: axis, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + if (round_mode is null) + { + round_mode = "HALF_TO_EVEN"; + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["input_min"] = input_min; + keywords["input_max"] = input_max; + keywords["signed_input"] = signed_input; + keywords["num_bits"] = num_bits; + keywords["range_given"] = range_given; + keywords["round_mode"] = round_mode; + keywords["narrow_range"] = narrow_range; + keywords["axis"] = axis; + var _op = tf.OpDefLib._apply_op_helper("QuantizeAndDequantizeV2", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "signed_input", _op._get_attr_bool("signed_input"), "num_bits", _op._get_attr_int("num_bits"), "range_given", _op._get_attr_bool("range_given"), "T", _op._get_attr_type("T"), "round_mode", _op.get_attr("round_mode"), "narrow_range", _op._get_attr_bool("narrow_range"), "axis", _op._get_attr_int("axis") }; + _execute.record_gradient("QuantizeAndDequantizeV2", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor quantize_and_dequantize_v2_eager_fallback(Tensor input, Tensor input_min, Tensor input_max, bool signed_input, int num_bits, bool range_given, string round_mode, bool narrow_range, int axis, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, input_min, input_max }; + object[] _attrs = new object[] { "signed_input", signed_input, "num_bits", num_bits, "range_given", range_given, "T", input.dtype, "round_mode", round_mode, "narrow_range", narrow_range, "axis", axis }; + var _result = _execute.execute("QuantizeAndDequantizeV2", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("QuantizeAndDequantizeV2", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Quantizes then dequantizes a tensor. + /// + /// + /// + /// This is almost identical to QuantizeAndDequantizeV2, except that num_bits is a + /// tensor, so its value can change during training. + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + public static Tensor quantize_and_dequantize_v3(Tensor input, Tensor input_min, Tensor input_max, Tensor num_bits, bool signed_input = true, bool range_given = true, bool narrow_range = false, int axis = -1, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "QuantizeAndDequantizeV3", name) { args = new object[] { input, input_min, input_max, num_bits }, attrs = new Dictionary() { ["signed_input"] = signed_input, ["range_given"] = range_given, ["narrow_range"] = narrow_range, ["axis"] = axis } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return quantize_and_dequantize_v3_eager_fallback(input, input_min, input_max, num_bits, signed_input: signed_input, range_given: range_given, narrow_range: narrow_range, axis: axis, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["input_min"] = input_min; + keywords["input_max"] = input_max; + keywords["num_bits"] = num_bits; + keywords["signed_input"] = signed_input; + keywords["range_given"] = range_given; + keywords["narrow_range"] = narrow_range; + keywords["axis"] = axis; + var _op = tf.OpDefLib._apply_op_helper("QuantizeAndDequantizeV3", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "signed_input", _op._get_attr_bool("signed_input"), "range_given", _op._get_attr_bool("range_given"), "T", _op._get_attr_type("T"), "narrow_range", _op._get_attr_bool("narrow_range"), "axis", _op._get_attr_int("axis") }; + _execute.record_gradient("QuantizeAndDequantizeV3", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor quantize_and_dequantize_v3_eager_fallback(Tensor input, Tensor input_min, Tensor input_max, Tensor num_bits, bool signed_input, bool range_given, bool narrow_range, int axis, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, input_min, input_max, num_bits }; + object[] _attrs = new object[] { "signed_input", signed_input, "range_given", range_given, "T", input.dtype, "narrow_range", narrow_range, "axis", axis }; + var _result = _execute.execute("QuantizeAndDequantizeV3", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("QuantizeAndDequantizeV3", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Quantizes then dequantizes a tensor. + /// + /// + /// + /// This is almost identical to QuantizeAndDequantizeV2, except that it returns a + /// gradient of 1 for inputs that are within the quantization range, or 0 otherwise. + /// + /// + /// + /// + /// + /// + /// + /// Whether the quantization is signed or unsigned. (actually this parameter should + /// have been called `signed_output`) + /// + /// + /// + /// + /// The bitwidth of the quantization. + /// + /// + /// + /// + /// Whether the range is given or should be determined from the `input` tensor. + /// + /// + /// + /// + /// The 'round_mode' attribute controls which rounding tie-breaking algorithm is + /// used when rounding float values to their quantized equivalents. The following + /// rounding modes are currently supported: + /// + /// * HALF_TO_EVEN: this is the default round_mode. + /// * HALF_UP: round towards positive. In this mode 7.5 rounds up to 8 and -7.5 + /// rounds up to -7. + /// + /// + /// + /// + /// + /// If True, then the absolute value of the quantized minimum value is the same as + /// the quantized maximum value, instead of 1 greater. + /// i.e. for 8 bit quantization, the minimum value is -127 instead of -128. + /// + /// + /// + /// + /// If specified, this axis is treated as a channel or slice axis, and a separate + /// quantization range is used for each channel or slice along this axis. + /// + /// + /// + public static Tensor quantize_and_dequantize_v4(Tensor input, Tensor input_min, Tensor input_max, bool signed_input = true, int num_bits = 8, bool range_given = false, string round_mode = "HALF_TO_EVEN", bool narrow_range = false, int axis = -1, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "QuantizeAndDequantizeV4", name) { args = new object[] { input, input_min, input_max }, attrs = new Dictionary() { ["signed_input"] = signed_input, ["num_bits"] = num_bits, ["range_given"] = range_given, ["round_mode"] = round_mode, ["narrow_range"] = narrow_range, ["axis"] = axis } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return quantize_and_dequantize_v4_eager_fallback(input, input_min, input_max, signed_input: signed_input, num_bits: num_bits, range_given: range_given, round_mode: round_mode, narrow_range: narrow_range, axis: axis, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + if (round_mode is null) + { + round_mode = "HALF_TO_EVEN"; + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["input_min"] = input_min; + keywords["input_max"] = input_max; + keywords["signed_input"] = signed_input; + keywords["num_bits"] = num_bits; + keywords["range_given"] = range_given; + keywords["round_mode"] = round_mode; + keywords["narrow_range"] = narrow_range; + keywords["axis"] = axis; + var _op = tf.OpDefLib._apply_op_helper("QuantizeAndDequantizeV4", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "signed_input", _op._get_attr_bool("signed_input"), "num_bits", _op._get_attr_int("num_bits"), "range_given", _op._get_attr_bool("range_given"), "T", _op._get_attr_type("T"), "round_mode", _op.get_attr("round_mode"), "narrow_range", _op._get_attr_bool("narrow_range"), "axis", _op._get_attr_int("axis") }; + _execute.record_gradient("QuantizeAndDequantizeV4", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor quantize_and_dequantize_v4_eager_fallback(Tensor input, Tensor input_min, Tensor input_max, bool signed_input, int num_bits, bool range_given, string round_mode, bool narrow_range, int axis, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, input_min, input_max }; + object[] _attrs = new object[] { "signed_input", signed_input, "num_bits", num_bits, "range_given", range_given, "T", input.dtype, "round_mode", round_mode, "narrow_range", narrow_range, "axis", axis }; + var _result = _execute.execute("QuantizeAndDequantizeV4", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("QuantizeAndDequantizeV4", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Quantize the 'input' tensor of type float to 'output' tensor of type 'T'. + /// + /// + /// + /// [min_range, max_range] are scalar floats that specify the range for + /// the 'input' data. The 'mode' attribute controls exactly which calculations are + /// used to convert the float values to their quantized equivalents. The + /// 'round_mode' attribute controls which rounding tie-breaking algorithm is used + /// when rounding float values to their quantized equivalents. + /// + /// In 'MIN_COMBINED' mode, each value of the tensor will undergo the following: + /// + /// ``` + /// out[i] = (in[i] - min_range) * range(T) / (max_range - min_range) + /// if T == qint8: out[i] -= (range(T) + 1) / 2.0 + /// ``` + /// + /// here `range(T) = numeric_limits::max() - numeric_limits::min()` + /// + /// *MIN_COMBINED Mode Example* + /// + /// Assume the input is type float and has a possible range of [0.0, 6.0] and the + /// output type is quint8 ([0, 255]). The min_range and max_range values should be + /// specified as 0.0 and 6.0. Quantizing from float to quint8 will multiply each + /// value of the input by 255/6 and cast to quint8. + /// + /// If the output type was qint8 ([-128, 127]), the operation will additionally + /// subtract each value by 128 prior to casting, so that the range of values aligns + /// with the range of qint8. + /// + /// If the mode is 'MIN_FIRST', then this approach is used: + /// + /// ``` + /// num_discrete_values = 1 << (# of bits in T) + /// range_adjust = num_discrete_values / (num_discrete_values - 1) + /// range = (range_max - range_min) * range_adjust + /// range_scale = num_discrete_values / range + /// quantized = round(input * range_scale) - round(range_min * range_scale) + + /// numeric_limits::min() + /// quantized = max(quantized, numeric_limits::min()) + /// quantized = min(quantized, numeric_limits::max()) + /// ``` + /// + /// The biggest difference between this and MIN_COMBINED is that the minimum range + /// is rounded first, before it's subtracted from the rounded value. With + /// MIN_COMBINED, a small bias is introduced where repeated iterations of quantizing + /// and dequantizing will introduce a larger and larger error. + /// + /// *SCALED mode Example* + /// + /// `SCALED` mode matches the quantization approach used in + /// `QuantizeAndDequantize{V2|V3}`. + /// + /// If the mode is `SCALED`, the quantization is performed by multiplying each + /// input value by a scaling_factor. + /// The scaling_factor is determined from `min_range` and `max_range` to be as large + /// as possible such that the range from `min_range` to `max_range` is representable + /// within values of type T. + /// + /// ```c++ + /// + /// const int min_T = std::numeric_limits::min(); + /// const int max_T = std::numeric_limits::max(); + /// const float max_float = std::numeric_limits::max(); + /// + /// const float scale_factor_from_min_side = + /// (min_T * min_range > 0) ? min_T / min_range : max_float; + /// const float scale_factor_from_max_side = + /// (max_T * max_range > 0) ? max_T / max_range : max_float; + /// + /// const float scale_factor = std::min(scale_factor_from_min_side, + /// scale_factor_from_max_side); + /// ``` + /// + /// We next use the scale_factor to adjust min_range and max_range as follows: + /// + /// ```c++ + /// min_range = min_T / scale_factor; + /// max_range = max_T / scale_factor; + /// ``` + /// + /// + /// e.g. if T = qint8, and initially min_range = -10, and max_range = 9, we would + /// compare -128/-10.0 = 12.8 to 127/9.0 = 14.11, and set scaling_factor = 12.8 + /// In this case, min_range would remain -10, but max_range would be adjusted to + /// 127 / 12.8 = 9.921875 + /// + /// So we will quantize input values in the range (-10, 9.921875) to (-128, 127). + /// + /// The input tensor can now be quantized by clipping values to the range + /// `min_range` to `max_range`, then multiplying by scale_factor as follows: + /// + /// ```c++ + /// result = round(min(max_range, max(min_range, input)) * scale_factor) + /// ``` + /// + /// The adjusted `min_range` and `max_range` are returned as outputs 2 and 3 of + /// this operation. These outputs should be used as the range for any further + /// calculations. + /// + /// + /// *narrow_range (bool) attribute* + /// + /// If true, we do not use the minimum quantized value. + /// i.e. for int8 the quantized output, it would be restricted to the range + /// -127..127 instead of the full -128..127 range. + /// This is provided for compatibility with certain inference backends. + /// (Only applies to SCALED mode) + /// + /// + /// *axis (int) attribute* + /// + /// An optional `axis` attribute can specify a dimension index of the input tensor, + /// such that quantization ranges will be calculated and applied separately for each + /// slice of the tensor along that dimension. This is useful for per-channel + /// quantization. + /// + /// If axis is specified, min_range and max_range + /// + /// if `axis`=None, per-tensor quantization is performed as normal. + /// + /// + /// *ensure_minimum_range (float) attribute* + /// + /// Ensures the minimum quantization range is at least this value. + /// The legacy default value for this is 0.01, but it is strongly suggested to + /// set it to 0 for new uses. + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + public static Tensor[] quantize_v2(Tensor input, Tensor min_range, Tensor max_range, TF_DataType T, string mode = "MIN_COMBINED", string round_mode = "HALF_AWAY_FROM_ZERO", bool narrow_range = false, int axis = -1, float ensure_minimum_range = 0.01f, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "QuantizeV2", name) { args = new object[] { input, min_range, max_range }, attrs = new Dictionary() { ["T"] = T, ["mode"] = mode, ["round_mode"] = round_mode, ["narrow_range"] = narrow_range, ["axis"] = axis, ["ensure_minimum_range"] = ensure_minimum_range } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return quantize_v2_eager_fallback(input, min_range, max_range, T: T, mode: mode, round_mode: round_mode, narrow_range: narrow_range, axis: axis, ensure_minimum_range: ensure_minimum_range, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + if (mode is null) + { + mode = "MIN_COMBINED"; + } + if (round_mode is null) + { + round_mode = "HALF_AWAY_FROM_ZERO"; + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["min_range"] = min_range; + keywords["max_range"] = max_range; + keywords["T"] = T; + keywords["mode"] = mode; + keywords["round_mode"] = round_mode; + keywords["narrow_range"] = narrow_range; + keywords["axis"] = axis; + keywords["ensure_minimum_range"] = ensure_minimum_range; + var _op = tf.OpDefLib._apply_op_helper("QuantizeV2", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "mode", _op.get_attr("mode"), "round_mode", _op.get_attr("round_mode"), "narrow_range", _op._get_attr_bool("narrow_range"), "axis", _op._get_attr_int("axis"), "ensure_minimum_range", _op.get_attr("ensure_minimum_range") }; + _execute.record_gradient("QuantizeV2", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] quantize_v2_eager_fallback(Tensor input, Tensor min_range, Tensor max_range, TF_DataType T, string mode, string round_mode, bool narrow_range, int axis, float ensure_minimum_range, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, min_range, max_range }; + object[] _attrs = new object[] { "T", T, "mode", mode, "round_mode", round_mode, "narrow_range", narrow_range, "axis", axis, "ensure_minimum_range", ensure_minimum_range }; + var _result = _execute.execute("QuantizeV2", 3, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("QuantizeV2", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// Concatenates quantized tensors along one dimension. + /// + /// + /// + /// + /// + /// + public static Tensor[] quantized_concat(Tensor concat_dim, Tensors values, Tensors input_mins, Tensors input_maxes, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "QuantizedConcat", name) { args = new object[] { concat_dim, values, input_mins, input_maxes }, attrs = new Dictionary() { } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return quantized_concat_eager_fallback(concat_dim, values, input_mins, input_maxes, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["concat_dim"] = concat_dim; + keywords["values"] = values; + keywords["input_mins"] = input_mins; + keywords["input_maxes"] = input_maxes; + var _op = tf.OpDefLib._apply_op_helper("QuantizedConcat", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "N", _op._get_attr_int("N"), "T", _op._get_attr_type("T") }; + _execute.record_gradient("QuantizedConcat", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] quantized_concat_eager_fallback(Tensor concat_dim, Tensors values, Tensors input_mins, Tensors input_maxes, string name, Context ctx) + { + List _inputs_flat_list = new(); + _inputs_flat_list.Add(concat_dim); + _inputs_flat_list.AddRange(values); + _inputs_flat_list.AddRange(input_mins); + _inputs_flat_list.AddRange(input_maxes); + var _inputs_flat = _inputs_flat_list.ToArray(); + object[] _attrs = new object[] { "N", values.Length, "T", values.dtype }; + var _result = _execute.execute("QuantizedConcat", 3, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("QuantizedConcat", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// Quantized Instance normalization. + /// + /// + /// + /// + /// + /// + /// If True, `given_y_min` and `given_y_min` + /// and `given_y_max` are used as the output range. Otherwise, + /// the implementation computes the output range. + /// + /// + /// + /// + /// Output in `y_min` if `output_range_given` is True. + /// + /// + /// + /// + /// Output in `y_max` if `output_range_given` is True. + /// + /// + /// + /// + /// A small float number to avoid dividing by 0. + /// + /// + /// + /// + /// Minimum value of `y_max - y_min` + /// + /// + /// + public static Tensor[] quantized_instance_norm(Tensor x, Tensor x_min, Tensor x_max, bool output_range_given = false, float given_y_min = 0f, float given_y_max = 0f, float variance_epsilon = 1E-05f, float min_separation = 0.001f, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "QuantizedInstanceNorm", name) { args = new object[] { x, x_min, x_max }, attrs = new Dictionary() { ["output_range_given"] = output_range_given, ["given_y_min"] = given_y_min, ["given_y_max"] = given_y_max, ["variance_epsilon"] = variance_epsilon, ["min_separation"] = min_separation } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return quantized_instance_norm_eager_fallback(x, x_min, x_max, output_range_given: output_range_given, given_y_min: given_y_min, given_y_max: given_y_max, variance_epsilon: variance_epsilon, min_separation: min_separation, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + keywords["x_min"] = x_min; + keywords["x_max"] = x_max; + keywords["output_range_given"] = output_range_given; + keywords["given_y_min"] = given_y_min; + keywords["given_y_max"] = given_y_max; + keywords["variance_epsilon"] = variance_epsilon; + keywords["min_separation"] = min_separation; + var _op = tf.OpDefLib._apply_op_helper("QuantizedInstanceNorm", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "output_range_given", _op._get_attr_bool("output_range_given"), "given_y_min", _op.get_attr("given_y_min"), "given_y_max", _op.get_attr("given_y_max"), "variance_epsilon", _op.get_attr("variance_epsilon"), "min_separation", _op.get_attr("min_separation") }; + _execute.record_gradient("QuantizedInstanceNorm", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] quantized_instance_norm_eager_fallback(Tensor x, Tensor x_min, Tensor x_max, bool output_range_given, float given_y_min, float given_y_max, float variance_epsilon, float min_separation, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x, x_min, x_max }; + object[] _attrs = new object[] { "T", x.dtype, "output_range_given", output_range_given, "given_y_min", given_y_min, "given_y_max", given_y_max, "variance_epsilon", variance_epsilon, "min_separation", min_separation }; + var _result = _execute.execute("QuantizedInstanceNorm", 3, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("QuantizedInstanceNorm", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// Reshapes a quantized tensor as per the Reshape op. + /// + /// + /// + /// ``` + /// + /// + /// + /// + /// + /// + /// + public static Tensor[] quantized_reshape(Tensor tensor, Tensor shape, Tensor input_min, Tensor input_max, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "QuantizedReshape", name) { args = new object[] { tensor, shape, input_min, input_max }, attrs = new Dictionary() { } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return quantized_reshape_eager_fallback(tensor, shape, input_min, input_max, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["tensor"] = tensor; + keywords["shape"] = shape; + keywords["input_min"] = input_min; + keywords["input_max"] = input_max; + var _op = tf.OpDefLib._apply_op_helper("QuantizedReshape", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "Tshape", _op._get_attr_type("Tshape") }; + _execute.record_gradient("QuantizedReshape", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] quantized_reshape_eager_fallback(Tensor tensor, Tensor shape, Tensor input_min, Tensor input_max, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { tensor, shape, input_min, input_max }; + object[] _attrs = new object[] { "T", tensor.dtype, "Tshape", shape.dtype }; + var _result = _execute.execute("QuantizedReshape", 3, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("QuantizedReshape", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// Returns the rank of a tensor. + /// + /// + /// + /// This operation returns an integer representing the rank of `input`. + /// + /// For example: + /// + /// ``` + /// # 't' is [[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]] + /// # shape of tensor 't' is [2, 2, 3] + /// rank(t) ==> 3 + /// ``` + /// + /// **Note**: The rank of a tensor is not the same as the rank of a matrix. The rank + /// of a tensor is the number of indices required to uniquely select each element + /// of the tensor. Rank is also known as "order", "degree", or "ndims." + /// + /// + /// + /// + public static Tensor rank(Tensor input, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Rank", name) { args = new object[] { input }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return rank_eager_fallback(input, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + var _op = tf.OpDefLib._apply_op_helper("Rank", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Rank", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor rank_eager_fallback(Tensor input, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input }; + object[] _attrs = new object[] { "T", input.dtype }; + var _result = _execute.execute("Rank", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Rank", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Return the same ref tensor as the input ref tensor. + /// + /// + /// + public static Tensor ref_identity(Tensor input, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + throw new RuntimeError("ref_identity op does not support eager execution. Arg input is a ref."); + } + Dictionary keywords = new(); + keywords["input"] = input; + var _op = tf.OpDefLib._apply_op_helper("RefIdentity", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("RefIdentity", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor ref_identity_eager_fallback(Tensor input, string name, Context ctx) + { + throw new RuntimeError($"ref_identity op does not support eager execution. Arg 'input' is a ref."); + } + /// + /// Reshapes a tensor. + /// + /// + /// + /// Given `tensor`, this operation returns a tensor that has the same values + /// as `tensor` with shape `shape`. + /// + /// If one component of 1-D tensor `shape` is the special value -1, the size of that + /// dimension is computed so that the total size remains constant. In particular, a + /// `shape` of `[-1]` flattens into 1-D. At most one component of `shape` may be + /// unknown. + /// + /// The `shape` must be 1-D and the operation returns a tensor with shape + /// `shape` filled with the values of `tensor`. In this case, the number of elements + /// implied by `shape` must be the same as the number of elements in `tensor`. + /// + /// It is an error if `shape` is not 1-D. + /// + /// For example: + /// + /// ``` + /// # tensor 't' is [1, 2, 3, 4, 5, 6, 7, 8, 9] + /// # tensor 't' has shape [9] + /// reshape(t, [3, 3]) ==> [[1, 2, 3], + /// [4, 5, 6], + /// [7, 8, 9]] + /// + /// # tensor 't' is [[[1, 1], [2, 2]], + /// # [[3, 3], [4, 4]]] + /// # tensor 't' has shape [2, 2, 2] + /// reshape(t, [2, 4]) ==> [[1, 1, 2, 2], + /// [3, 3, 4, 4]] + /// + /// # tensor 't' is [[[1, 1, 1], + /// # [2, 2, 2]], + /// # [[3, 3, 3], + /// # [4, 4, 4]], + /// # [[5, 5, 5], + /// # [6, 6, 6]]] + /// # tensor 't' has shape [3, 2, 3] + /// # pass '[-1]' to flatten 't' + /// reshape(t, [-1]) ==> [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6] + /// + /// # -1 can also be used to infer the shape + /// + /// # -1 is inferred to be 9: + /// reshape(t, [2, -1]) ==> [[1, 1, 1, 2, 2, 2, 3, 3, 3], + /// [4, 4, 4, 5, 5, 5, 6, 6, 6]] + /// # -1 is inferred to be 2: + /// reshape(t, [-1, 9]) ==> [[1, 1, 1, 2, 2, 2, 3, 3, 3], + /// [4, 4, 4, 5, 5, 5, 6, 6, 6]] + /// # -1 is inferred to be 3: + /// reshape(t, [ 2, -1, 3]) ==> [[[1, 1, 1], + /// [2, 2, 2], + /// [3, 3, 3]], + /// [[4, 4, 4], + /// [5, 5, 5], + /// [6, 6, 6]]] + /// + /// # tensor 't' is [7] + /// # shape `[]` reshapes to a scalar + /// reshape(t, []) ==> 7 + /// ``` + /// + /// + /// + /// + /// + public static Tensor reshape(Tensor tensor, Tensor shape, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Reshape", name) { args = new object[] { tensor, shape }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return reshape_eager_fallback(tensor, shape, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["tensor"] = tensor; + keywords["shape"] = shape; + var _op = tf.OpDefLib._apply_op_helper("Reshape", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "Tshape", _op._get_attr_type("Tshape") }; + _execute.record_gradient("Reshape", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor reshape_eager_fallback(Tensor tensor, Tensor shape, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { tensor, shape }; + object[] _attrs = new object[] { "T", tensor.dtype, "Tshape", shape.dtype }; + var _result = _execute.execute("Reshape", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Reshape", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Assign `value` to the sliced l-value reference of `ref`. + /// + /// + /// + /// The values of `value` are assigned to the positions in the variable + /// `ref` that are selected by the slice parameters. The slice parameters + /// `begin, `end`, `strides`, etc. work exactly as in `StridedSlice`. + /// + /// NOTE this op currently does not support broadcasting and so `value`'s + /// shape must be exactly the shape produced by the slice of `ref`. + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + public static Operation resource_strided_slice_assign(Tensor ref_, Tensor begin, Tensor end, Tensor strides, Tensor value, int begin_mask = 0, int end_mask = 0, int ellipsis_mask = 0, int new_axis_mask = 0, int shrink_axis_mask = 0, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "ResourceStridedSliceAssign", name) { args = new object[] { ref_, begin, end, strides, value }, attrs = new Dictionary() { ["begin_mask"] = begin_mask, ["end_mask"] = end_mask, ["ellipsis_mask"] = ellipsis_mask, ["new_axis_mask"] = new_axis_mask, ["shrink_axis_mask"] = shrink_axis_mask } }); + return null; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return resource_strided_slice_assign_eager_fallback(ref_, begin, end, strides, value, begin_mask: begin_mask, end_mask: end_mask, ellipsis_mask: ellipsis_mask, new_axis_mask: new_axis_mask, shrink_axis_mask: shrink_axis_mask, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["ref"] = ref_; + keywords["begin"] = begin; + keywords["end"] = end; + keywords["strides"] = strides; + keywords["value"] = value; + keywords["begin_mask"] = begin_mask; + keywords["end_mask"] = end_mask; + keywords["ellipsis_mask"] = ellipsis_mask; + keywords["new_axis_mask"] = new_axis_mask; + keywords["shrink_axis_mask"] = shrink_axis_mask; + var _op = tf.OpDefLib._apply_op_helper("ResourceStridedSliceAssign", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "Index", _op._get_attr_type("Index"), "begin_mask", _op._get_attr_int("begin_mask"), "end_mask", _op._get_attr_int("end_mask"), "ellipsis_mask", _op._get_attr_int("ellipsis_mask"), "new_axis_mask", _op._get_attr_int("new_axis_mask"), "shrink_axis_mask", _op._get_attr_int("shrink_axis_mask") }; + _execute.record_gradient("ResourceStridedSliceAssign", _op.inputs, _attrs, _result); + } + return _op; + } + + public static Operation resource_strided_slice_assign_eager_fallback(Tensor ref_, Tensor begin, Tensor end, Tensor strides, Tensor value, int begin_mask, int end_mask, int ellipsis_mask, int new_axis_mask, int shrink_axis_mask, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { ref_, begin, end, strides, value }; + object[] _attrs = new object[] { "T", value.dtype, "Index", begin.dtype, "begin_mask", begin_mask, "end_mask", end_mask, "ellipsis_mask", ellipsis_mask, "new_axis_mask", new_axis_mask, "shrink_axis_mask", shrink_axis_mask }; + var _result = _execute.execute("ResourceStridedSliceAssign", 0, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("ResourceStridedSliceAssign", _inputs_flat, _attrs, _result); + } + return null; + } + /// + /// Reverses specific dimensions of a tensor. + /// + /// + /// + /// Given a `tensor`, and a `bool` tensor `dims` representing the dimensions + /// of `tensor`, this operation reverses each dimension i of `tensor` where + /// `dims[i]` is `True`. + /// + /// `tensor` can have up to 8 dimensions. The number of dimensions + /// of `tensor` must equal the number of elements in `dims`. In other words: + /// + /// `rank(tensor) = size(dims)` + /// + /// For example: + /// + /// ``` + /// # tensor 't' is [[[[ 0, 1, 2, 3], + /// # [ 4, 5, 6, 7], + /// # [ 8, 9, 10, 11]], + /// # [[12, 13, 14, 15], + /// # [16, 17, 18, 19], + /// # [20, 21, 22, 23]]]] + /// # tensor 't' shape is [1, 2, 3, 4] + /// + /// # 'dims' is [False, False, False, True] + /// reverse(t, dims) ==> [[[[ 3, 2, 1, 0], + /// [ 7, 6, 5, 4], + /// [ 11, 10, 9, 8]], + /// [[15, 14, 13, 12], + /// [19, 18, 17, 16], + /// [23, 22, 21, 20]]]] + /// + /// # 'dims' is [False, True, False, False] + /// reverse(t, dims) ==> [[[[12, 13, 14, 15], + /// [16, 17, 18, 19], + /// [20, 21, 22, 23] + /// [[ 0, 1, 2, 3], + /// [ 4, 5, 6, 7], + /// [ 8, 9, 10, 11]]]] + /// + /// # 'dims' is [False, False, True, False] + /// reverse(t, dims) ==> [[[[8, 9, 10, 11], + /// [4, 5, 6, 7], + /// [0, 1, 2, 3]] + /// [[20, 21, 22, 23], + /// [16, 17, 18, 19], + /// [12, 13, 14, 15]]]] + /// ``` + /// + /// + /// + /// + /// + public static Tensor reverse(Tensor tensor, Tensor dims, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Reverse", name) { args = new object[] { tensor, dims }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return reverse_eager_fallback(tensor, dims, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["tensor"] = tensor; + keywords["dims"] = dims; + var _op = tf.OpDefLib._apply_op_helper("Reverse", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Reverse", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor reverse_eager_fallback(Tensor tensor, Tensor dims, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { tensor, dims }; + object[] _attrs = new object[] { "T", tensor.dtype }; + var _result = _execute.execute("Reverse", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Reverse", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Reverses variable length slices. + /// + /// + /// + /// This op first slices `input` along the dimension `batch_dim`, and for each + /// slice `i`, reverses the first `seq_lengths[i]` elements along + /// the dimension `seq_dim`. + /// + /// The elements of `seq_lengths` must obey `seq_lengths[i] <= input.dims[seq_dim]`, + /// and `seq_lengths` must be a vector of length `input.dims[batch_dim]`. + /// + /// The output slice `i` along dimension `batch_dim` is then given by input + /// slice `i`, with the first `seq_lengths[i]` slices along dimension + /// `seq_dim` reversed. + /// + /// For example: + /// + /// ``` + /// # Given this: + /// batch_dim = 0 + /// seq_dim = 1 + /// input.dims = (4, 8, ...) + /// seq_lengths = [7, 2, 3, 5] + /// + /// # then slices of input are reversed on seq_dim, but only up to seq_lengths: + /// output[0, 0:7, :, ...] = input[0, 7:0:-1, :, ...] + /// output[1, 0:2, :, ...] = input[1, 2:0:-1, :, ...] + /// output[2, 0:3, :, ...] = input[2, 3:0:-1, :, ...] + /// output[3, 0:5, :, ...] = input[3, 5:0:-1, :, ...] + /// + /// # while entries past seq_lens are copied through: + /// output[0, 7:, :, ...] = input[0, 7:, :, ...] + /// output[1, 2:, :, ...] = input[1, 2:, :, ...] + /// output[2, 3:, :, ...] = input[2, 3:, :, ...] + /// output[3, 2:, :, ...] = input[3, 2:, :, ...] + /// ``` + /// + /// In contrast, if: + /// + /// ``` + /// # Given this: + /// batch_dim = 2 + /// seq_dim = 0 + /// input.dims = (8, ?, 4, ...) + /// seq_lengths = [7, 2, 3, 5] + /// + /// # then slices of input are reversed on seq_dim, but only up to seq_lengths: + /// output[0:7, :, 0, :, ...] = input[7:0:-1, :, 0, :, ...] + /// output[0:2, :, 1, :, ...] = input[2:0:-1, :, 1, :, ...] + /// output[0:3, :, 2, :, ...] = input[3:0:-1, :, 2, :, ...] + /// output[0:5, :, 3, :, ...] = input[5:0:-1, :, 3, :, ...] + /// + /// # while entries past seq_lens are copied through: + /// output[7:, :, 0, :, ...] = input[7:, :, 0, :, ...] + /// output[2:, :, 1, :, ...] = input[2:, :, 1, :, ...] + /// output[3:, :, 2, :, ...] = input[3:, :, 2, :, ...] + /// output[2:, :, 3, :, ...] = input[2:, :, 3, :, ...] + /// ``` + /// + /// + /// + /// + /// + /// + /// The dimension which is partially reversed. + /// + /// + /// + /// + /// The dimension along which reversal is performed. + /// + /// + /// + public static Tensor reverse_sequence(Tensor input, Tensor seq_lengths, int seq_dim = 0, int batch_dim = 0, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "ReverseSequence", name) { args = new object[] { input, seq_lengths }, attrs = new Dictionary() { ["seq_dim"] = seq_dim, ["batch_dim"] = batch_dim } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return reverse_sequence_eager_fallback(input, seq_lengths, seq_dim: seq_dim, batch_dim: batch_dim, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["seq_lengths"] = seq_lengths; + keywords["seq_dim"] = seq_dim; + keywords["batch_dim"] = batch_dim; + var _op = tf.OpDefLib._apply_op_helper("ReverseSequence", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "seq_dim", _op._get_attr_int("seq_dim"), "batch_dim", _op._get_attr_int("batch_dim"), "T", _op._get_attr_type("T"), "Tlen", _op._get_attr_type("Tlen") }; + _execute.record_gradient("ReverseSequence", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor reverse_sequence_eager_fallback(Tensor input, Tensor seq_lengths, int seq_dim, int batch_dim, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, seq_lengths }; + object[] _attrs = new object[] { "seq_dim", seq_dim, "batch_dim", batch_dim, "T", input.dtype, "Tlen", seq_lengths.dtype }; + var _result = _execute.execute("ReverseSequence", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("ReverseSequence", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Reverses specific dimensions of a tensor. + /// + /// + /// + /// Given a `tensor`, and a `int32` tensor `axis` representing the set of + /// dimensions of `tensor` to reverse. This operation reverses each dimension + /// `i` for which there exists `j` s.t. `axis[j] == i`. + /// + /// `tensor` can have up to 8 dimensions. The number of dimensions specified + /// in `axis` may be 0 or more entries. If an index is specified more than + /// once, a InvalidArgument error is raised. + /// + /// For example: + /// + /// ``` + /// # tensor 't' is [[[[ 0, 1, 2, 3], + /// # [ 4, 5, 6, 7], + /// # [ 8, 9, 10, 11]], + /// # [[12, 13, 14, 15], + /// # [16, 17, 18, 19], + /// # [20, 21, 22, 23]]]] + /// # tensor 't' shape is [1, 2, 3, 4] + /// + /// # 'dims' is [3] or 'dims' is [-1] + /// reverse(t, dims) ==> [[[[ 3, 2, 1, 0], + /// [ 7, 6, 5, 4], + /// [ 11, 10, 9, 8]], + /// [[15, 14, 13, 12], + /// [19, 18, 17, 16], + /// [23, 22, 21, 20]]]] + /// + /// # 'dims' is '[1]' (or 'dims' is '[-3]') + /// reverse(t, dims) ==> [[[[12, 13, 14, 15], + /// [16, 17, 18, 19], + /// [20, 21, 22, 23] + /// [[ 0, 1, 2, 3], + /// [ 4, 5, 6, 7], + /// [ 8, 9, 10, 11]]]] + /// + /// # 'dims' is '[2]' (or 'dims' is '[-2]') + /// reverse(t, dims) ==> [[[[8, 9, 10, 11], + /// [4, 5, 6, 7], + /// [0, 1, 2, 3]] + /// [[20, 21, 22, 23], + /// [16, 17, 18, 19], + /// [12, 13, 14, 15]]]] + /// ``` + /// + /// + /// + /// + /// + public static Tensor reverse_v2(Tensor tensor, Tensor axis, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "ReverseV2", name) { args = new object[] { tensor, axis }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return reverse_v2_eager_fallback(tensor, axis, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["tensor"] = tensor; + keywords["axis"] = axis; + var _op = tf.OpDefLib._apply_op_helper("ReverseV2", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "Tidx", _op._get_attr_type("Tidx"), "T", _op._get_attr_type("T") }; + _execute.record_gradient("ReverseV2", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor reverse_v2_eager_fallback(Tensor tensor, Tensor axis, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { tensor, axis }; + object[] _attrs = new object[] { "Tidx", axis.dtype, "T", tensor.dtype }; + var _result = _execute.execute("ReverseV2", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("ReverseV2", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Scatters `updates` into a tensor of shape `shape` according to `indices`. + /// + /// + /// + /// Scatter sparse `updates` according to individual values at the specified + /// `indices`. This op returns an output tensor with the `shape` you specify. This + /// op is the inverse of the `tf.gather_nd` operator which extracts values or slices + /// from a given tensor. + /// + /// This operation is similar to `tf.tensor_scatter_nd_add`, except that the tensor + /// is zero-initialized. Calling `tf.scatter_nd(indices, updates, shape)` + /// is identical to calling + /// `tf.tensor_scatter_nd_add(tf.zeros(shape, updates.dtype), indices, updates)` + /// + /// If `indices` contains duplicates, the associated `updates` are accumulated + /// (summed) into the output tensor. + /// + /// **WARNING**: For floating-point data types, the output may be nondeterministic. + /// This is because the order in which the updates are applied is nondeterministic + /// and when floating-point numbers are added in different orders the resulting + /// numerical approximation error can be slightly different. However, the output + /// will be deterministic if op determinism is enabled via + /// `tf.config.experimental.enable_op_determinism`. + /// + /// `indices` is an integer tensor containing indices into the output tensor. The + /// last dimension of `indices` can be at most the rank of `shape`: + /// + /// indices.shape[-1] <= shape.rank + /// + /// The last dimension of `indices` corresponds to indices of elements + /// (if `indices.shape[-1] = shape.rank`) or slices + /// (if `indices.shape[-1] < shape.rank`) along dimension `indices.shape[-1]` of + /// `shape`. + /// + /// `updates` is a tensor with shape: + /// + /// indices.shape[:-1] + shape[indices.shape[-1]:] + /// + /// The simplest form of the scatter op is to insert individual elements in + /// a tensor by index. Consider an example where you want to insert 4 scattered + /// elements in a rank-1 tensor with 8 elements. + /// + ///
+ /// + ///
+ /// + /// In Python, this scatter operation would look like this: + /// + /// ```python + /// indices = tf.constant([[4], [3], [1], [7]]) + /// updates = tf.constant([9, 10, 11, 12]) + /// shape = tf.constant([8]) + /// scatter = tf.scatter_nd(indices, updates, shape) + /// print(scatter) + /// ``` + /// + /// The resulting tensor would look like this: + /// + /// [0, 11, 0, 10, 9, 0, 0, 12] + /// + /// You can also insert entire slices of a higher rank tensor all at once. For + /// example, you can insert two slices in the first dimension of a rank-3 tensor + /// with two matrices of new values. + /// + ///
+ /// + ///
+ /// + /// In Python, this scatter operation would look like this: + /// + /// ```python + /// indices = tf.constant([[1], [3]]) + /// updates = tf.constant([[[5, 5, 5, 5], [6, 6, 6, 6], + /// [7, 7, 7, 7], [8, 8, 8, 8]], + /// [[5, 5, 5, 5], [6, 6, 6, 6], + /// [7, 7, 7, 7], [8, 8, 8, 8]]]) + /// shape = tf.constant([4, 4, 4]) + /// scatter = tf.scatter_nd(indices, updates, shape) + /// print(scatter) + /// ``` + /// + /// The resulting tensor would look like this: + /// + /// [[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], + /// [[5, 5, 5, 5], [6, 6, 6, 6], [7, 7, 7, 7], [8, 8, 8, 8]], + /// [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], + /// [[5, 5, 5, 5], [6, 6, 6, 6], [7, 7, 7, 7], [8, 8, 8, 8]]] + /// + /// Note that on CPU, if an out of bound index is found, an error is returned. + /// On GPU, if an out of bound index is found, the index is ignored. + /// + ///
+ /// + /// + /// + /// + public static Tensor scatter_nd(Tensor indices, Tensor updates, Tensor shape, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "ScatterNd", name) { args = new object[] { indices, updates, shape }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return scatter_nd_eager_fallback(indices, updates, shape, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["indices"] = indices; + keywords["updates"] = updates; + keywords["shape"] = shape; + var _op = tf.OpDefLib._apply_op_helper("ScatterNd", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "Tindices", _op._get_attr_type("Tindices") }; + _execute.record_gradient("ScatterNd", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor scatter_nd_eager_fallback(Tensor indices, Tensor updates, Tensor shape, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { indices, updates, shape }; + object[] _attrs = new object[] { "T", updates.dtype, "Tindices", indices.dtype }; + var _result = _execute.execute("ScatterNd", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("ScatterNd", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Applies sparse addition to `input` using individual values or slices + /// + /// + /// + /// from `updates` according to indices `indices`. The updates are non-aliasing: + /// `input` is only modified in-place if no other operations will use it. + /// Otherwise, a copy of `input` is made. This operation has a gradient with + /// respect to both `input` and `updates`. + /// + /// `input` is a `Tensor` with rank `P` and `indices` is a `Tensor` of rank `Q`. + /// + /// `indices` must be integer tensor, containing indices into `input`. + /// It must be shape \([d_0, ..., d_{Q-2}, K]\) where `0 < K <= P`. + /// + /// The innermost dimension of `indices` (with length `K`) corresponds to + /// indices into elements (if `K = P`) or `(P-K)`-dimensional slices + /// (if `K < P`) along the `K`th dimension of `input`. + /// + /// `updates` is `Tensor` of rank `Q-1+P-K` with shape: + /// + /// $$[d_0, ..., d_{Q-2}, input.shape[K], ..., input.shape[P-1]].$$ + /// + /// For example, say we want to add 4 scattered elements to a rank-1 tensor to 8 + /// elements. In Python, that addition would look like this: + /// + /// input = tf.constant([1, 2, 3, 4, 5, 6, 7, 8]) + /// indices = tf.constant([[4], [3], [1], [7]]) + /// updates = tf.constant([9, 10, 11, 12]) + /// output = tf.scatter_nd_non_aliasing_add(input, indices, updates) + /// with tf.Session() as sess: + /// print(sess.run(output)) + /// + /// The resulting value `output` would look like this: + /// + /// [1, 13, 3, 14, 14, 6, 7, 20] + /// + /// See `tf.scatter_nd` for more details about how to make updates to slices. + /// + /// + /// + /// + /// + /// + public static Tensor scatter_nd_non_aliasing_add(Tensor input, Tensor indices, Tensor updates, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "ScatterNdNonAliasingAdd", name) { args = new object[] { input, indices, updates }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return scatter_nd_non_aliasing_add_eager_fallback(input, indices, updates, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["indices"] = indices; + keywords["updates"] = updates; + var _op = tf.OpDefLib._apply_op_helper("ScatterNdNonAliasingAdd", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "Tindices", _op._get_attr_type("Tindices") }; + _execute.record_gradient("ScatterNdNonAliasingAdd", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor scatter_nd_non_aliasing_add_eager_fallback(Tensor input, Tensor indices, Tensor updates, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, indices, updates }; + object[] _attrs = new object[] { "T", input.dtype, "Tindices", indices.dtype }; + var _result = _execute.execute("ScatterNdNonAliasingAdd", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("ScatterNdNonAliasingAdd", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns the shape of a tensor. + /// + /// + /// + /// This operation returns a 1-D integer tensor representing the shape of `input`. + /// + /// For example: + /// + /// ``` + /// # 't' is [[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]] + /// shape(t) ==> [2, 2, 3] + /// ``` + /// + /// + /// + /// + /// + public static Tensor shape(Tensor input, TF_DataType out_type = TF_DataType.TF_INT32, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Shape", name) { args = new object[] { input }, attrs = new Dictionary() { ["out_type"] = out_type } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return shape_eager_fallback(input, out_type: out_type, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["out_type"] = out_type; + var _op = tf.OpDefLib._apply_op_helper("Shape", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "out_type", _op._get_attr_type("out_type") }; + _execute.record_gradient("Shape", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor shape_eager_fallback(Tensor input, TF_DataType out_type, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input }; + object[] _attrs = new object[] { "T", input.dtype, "out_type", out_type }; + var _result = _execute.execute("Shape", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Shape", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns shape of tensors. + /// + /// + /// + /// This operation returns N 1-D integer tensors representing shape of `input[i]s`. + /// + /// + /// + /// + /// + public static Tensor[] shape_n(Tensors input, TF_DataType out_type = TF_DataType.TF_INT32, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "ShapeN", name) { args = new object[] { input }, attrs = new Dictionary() { ["out_type"] = out_type } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return shape_n_eager_fallback(input, out_type: out_type, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["out_type"] = out_type; + var _op = tf.OpDefLib._apply_op_helper("ShapeN", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "N", _op._get_attr_int("N"), "T", _op._get_attr_type("T"), "out_type", _op._get_attr_type("out_type") }; + _execute.record_gradient("ShapeN", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] shape_n_eager_fallback(Tensors input, TF_DataType out_type, string name, Context ctx) + { + List _inputs_flat_list = new(); + _inputs_flat_list.AddRange(input); + var _inputs_flat = _inputs_flat_list.ToArray(); + object[] _attrs = new object[] { "N", input.Length, "T", input.dtype, "out_type", out_type }; + var _result = _execute.execute("ShapeN", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("ShapeN", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// Returns the size of a tensor. + /// + /// + /// + /// This operation returns an integer representing the number of elements in + /// `input`. + /// + /// For example: + /// + /// ``` + /// # 't' is [[[1, 1,, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]]] + /// size(t) ==> 12 + /// ``` + /// + /// + /// + /// + /// + public static Tensor size(Tensor input, TF_DataType out_type = TF_DataType.TF_INT32, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Size", name) { args = new object[] { input }, attrs = new Dictionary() { ["out_type"] = out_type } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return size_eager_fallback(input, out_type: out_type, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["out_type"] = out_type; + var _op = tf.OpDefLib._apply_op_helper("Size", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "out_type", _op._get_attr_type("out_type") }; + _execute.record_gradient("Size", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor size_eager_fallback(Tensor input, TF_DataType out_type, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input }; + object[] _attrs = new object[] { "T", input.dtype, "out_type", out_type }; + var _result = _execute.execute("Size", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Size", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Return a slice from 'input'. + /// + /// + /// + /// The output tensor is a tensor with dimensions described by 'size' + /// whose values are extracted from 'input' starting at the offsets in + /// 'begin'. + /// + /// *Requirements*: + /// 0 <= begin[i] <= begin[i] + size[i] <= Di for i in [0, n) + /// + /// + /// + /// + /// + /// + public static Tensor slice(Tensor input, Tensor begin, Tensor size, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Slice", name) { args = new object[] { input, begin, size }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return slice_eager_fallback(input, begin, size, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["begin"] = begin; + keywords["size"] = size; + var _op = tf.OpDefLib._apply_op_helper("Slice", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "Index", _op._get_attr_type("Index") }; + _execute.record_gradient("Slice", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor slice_eager_fallback(Tensor input, Tensor begin, Tensor size, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, begin, size }; + object[] _attrs = new object[] { "T", input.dtype, "Index", begin.dtype }; + var _result = _execute.execute("Slice", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Slice", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns a copy of the input tensor. + /// + /// + /// + public static Tensor snapshot(Tensor input, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Snapshot", name) { args = new object[] { input }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return snapshot_eager_fallback(input, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + var _op = tf.OpDefLib._apply_op_helper("Snapshot", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Snapshot", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor snapshot_eager_fallback(Tensor input, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input }; + object[] _attrs = new object[] { "T", input.dtype }; + var _result = _execute.execute("Snapshot", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Snapshot", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// SpaceToBatch for 4-D tensors of type T. + /// + /// + /// + /// This is a legacy version of the more general SpaceToBatchND. + /// + /// Zero-pads and then rearranges (permutes) blocks of spatial data into batch. + /// More specifically, this op outputs a copy of the input tensor where values from + /// the `height` and `width` dimensions are moved to the `batch` dimension. After + /// the zero-padding, both `height` and `width` of the input must be divisible by the + /// block size. + /// + /// The attr `block_size` must be greater than one. It indicates the block size. + /// + /// * Non-overlapping blocks of size `block_size x block size` in the height and + /// width dimensions are rearranged into the batch dimension at each location. + /// * The batch of the output tensor is `batch * block_size * block_size`. + /// * Both height_pad and width_pad must be divisible by block_size. + /// + /// The shape of the output will be: + /// + /// [batch*block_size*block_size, height_pad/block_size, width_pad/block_size, + /// depth] + /// + /// Some examples: + /// + /// (1) For the following input of shape `[1, 2, 2, 1]` and block_size of 2: + /// + /// ``` + /// x = [[[[1], [2]], [[3], [4]]]] + /// ``` + /// + /// The output tensor has shape `[4, 1, 1, 1]` and value: + /// + /// ``` + /// [[[[1]]], [[[2]]], [[[3]]], [[[4]]]] + /// ``` + /// + /// (2) For the following input of shape `[1, 2, 2, 3]` and block_size of 2: + /// + /// ``` + /// x = [[[[1, 2, 3], [4, 5, 6]], + /// [[7, 8, 9], [10, 11, 12]]]] + /// ``` + /// + /// The output tensor has shape `[4, 1, 1, 3]` and value: + /// + /// ``` + /// [[[[1, 2, 3]]], [[[4, 5, 6]]], [[[7, 8, 9]]], [[[10, 11, 12]]]] + /// ``` + /// + /// (3) For the following input of shape `[1, 4, 4, 1]` and block_size of 2: + /// + /// ``` + /// x = [[[[1], [2], [3], [4]], + /// [[5], [6], [7], [8]], + /// [[9], [10], [11], [12]], + /// [[13], [14], [15], [16]]]] + /// ``` + /// + /// The output tensor has shape `[4, 2, 2, 1]` and value: + /// + /// ``` + /// x = [[[[1], [3]], [[9], [11]]], + /// [[[2], [4]], [[10], [12]]], + /// [[[5], [7]], [[13], [15]]], + /// [[[6], [8]], [[14], [16]]]] + /// ``` + /// + /// (4) For the following input of shape `[2, 2, 4, 1]` and block_size of 2: + /// + /// ``` + /// x = [[[[1], [2], [3], [4]], + /// [[5], [6], [7], [8]]], + /// [[[9], [10], [11], [12]], + /// [[13], [14], [15], [16]]]] + /// ``` + /// + /// The output tensor has shape `[8, 1, 2, 1]` and value: + /// + /// ``` + /// x = [[[[1], [3]]], [[[9], [11]]], [[[2], [4]]], [[[10], [12]]], + /// [[[5], [7]]], [[[13], [15]]], [[[6], [8]]], [[[14], [16]]]] + /// ``` + /// + /// Among others, this operation is useful for reducing atrous convolution into + /// regular convolution. + /// + /// + /// + /// + /// + /// + public static Tensor space_to_batch(Tensor input, Tensor paddings, int block_size = 0, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "SpaceToBatch", name) { args = new object[] { input, paddings }, attrs = new Dictionary() { ["block_size"] = block_size } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return space_to_batch_eager_fallback(input, paddings, block_size: block_size, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["paddings"] = paddings; + keywords["block_size"] = block_size; + var _op = tf.OpDefLib._apply_op_helper("SpaceToBatch", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "Tpaddings", _op._get_attr_type("Tpaddings"), "block_size", _op._get_attr_int("block_size") }; + _execute.record_gradient("SpaceToBatch", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor space_to_batch_eager_fallback(Tensor input, Tensor paddings, int block_size, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, paddings }; + object[] _attrs = new object[] { "T", input.dtype, "Tpaddings", paddings.dtype, "block_size", block_size }; + var _result = _execute.execute("SpaceToBatch", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("SpaceToBatch", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// SpaceToBatch for N-D tensors of type T. + /// + /// + /// + /// This operation divides "spatial" dimensions `[1, ..., M]` of the input into a + /// grid of blocks of shape `block_shape`, and interleaves these blocks with the + /// "batch" dimension (0) such that in the output, the spatial dimensions + /// `[1, ..., M]` correspond to the position within the grid, and the batch + /// dimension combines both the position within a spatial block and the original + /// batch position. Prior to division into blocks, the spatial dimensions of the + /// input are optionally zero padded according to `paddings`. See below for a + /// precise description. + /// + /// This operation is equivalent to the following steps: + /// + /// 1. Zero-pad the start and end of dimensions `[1, ..., M]` of the + /// input according to `paddings` to produce `padded` of shape `padded_shape`. + /// + /// 2. Reshape `padded` to `reshaped_padded` of shape: + /// + /// [batch] + + /// [padded_shape[1] / block_shape[0], + /// block_shape[0], + /// ..., + /// padded_shape[M] / block_shape[M-1], + /// block_shape[M-1]] + + /// remaining_shape + /// + /// 3. Permute dimensions of `reshaped_padded` to produce + /// `permuted_reshaped_padded` of shape: + /// + /// block_shape + + /// [batch] + + /// [padded_shape[1] / block_shape[0], + /// ..., + /// padded_shape[M] / block_shape[M-1]] + + /// remaining_shape + /// + /// 4. Reshape `permuted_reshaped_padded` to flatten `block_shape` into the batch + /// dimension, producing an output tensor of shape: + /// + /// [batch * prod(block_shape)] + + /// [padded_shape[1] / block_shape[0], + /// ..., + /// padded_shape[M] / block_shape[M-1]] + + /// remaining_shape + /// + /// Some examples: + /// + /// (1) For the following input of shape `[1, 2, 2, 1]`, `block_shape = [2, 2]`, and + /// `paddings = [[0, 0], [0, 0]]`: + /// + /// ``` + /// x = [[[[1], [2]], [[3], [4]]]] + /// ``` + /// + /// The output tensor has shape `[4, 1, 1, 1]` and value: + /// + /// ``` + /// [[[[1]]], [[[2]]], [[[3]]], [[[4]]]] + /// ``` + /// + /// (2) For the following input of shape `[1, 2, 2, 3]`, `block_shape = [2, 2]`, and + /// `paddings = [[0, 0], [0, 0]]`: + /// + /// ``` + /// x = [[[[1, 2, 3], [4, 5, 6]], + /// [[7, 8, 9], [10, 11, 12]]]] + /// ``` + /// + /// The output tensor has shape `[4, 1, 1, 3]` and value: + /// + /// ``` + /// [[[[1, 2, 3]]], [[[4, 5, 6]]], [[[7, 8, 9]]], [[[10, 11, 12]]]] + /// ``` + /// + /// (3) For the following input of shape `[1, 4, 4, 1]`, `block_shape = [2, 2]`, and + /// `paddings = [[0, 0], [0, 0]]`: + /// + /// ``` + /// x = [[[[1], [2], [3], [4]], + /// [[5], [6], [7], [8]], + /// [[9], [10], [11], [12]], + /// [[13], [14], [15], [16]]]] + /// ``` + /// + /// The output tensor has shape `[4, 2, 2, 1]` and value: + /// + /// ``` + /// x = [[[[1], [3]], [[9], [11]]], + /// [[[2], [4]], [[10], [12]]], + /// [[[5], [7]], [[13], [15]]], + /// [[[6], [8]], [[14], [16]]]] + /// ``` + /// + /// (4) For the following input of shape `[2, 2, 4, 1]`, block_shape = `[2, 2]`, and + /// paddings = `[[0, 0], [2, 0]]`: + /// + /// ``` + /// x = [[[[1], [2], [3], [4]], + /// [[5], [6], [7], [8]]], + /// [[[9], [10], [11], [12]], + /// [[13], [14], [15], [16]]]] + /// ``` + /// + /// The output tensor has shape `[8, 1, 3, 1]` and value: + /// + /// ``` + /// x = [[[[0], [1], [3]]], [[[0], [9], [11]]], + /// [[[0], [2], [4]]], [[[0], [10], [12]]], + /// [[[0], [5], [7]]], [[[0], [13], [15]]], + /// [[[0], [6], [8]]], [[[0], [14], [16]]]] + /// ``` + /// + /// Among others, this operation is useful for reducing atrous convolution into + /// regular convolution. + /// + /// + /// + /// + /// + /// + public static Tensor space_to_batch_nd(Tensor input, Tensor block_shape, Tensor paddings, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "SpaceToBatchND", name) { args = new object[] { input, block_shape, paddings }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return space_to_batch_nd_eager_fallback(input, block_shape, paddings, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["block_shape"] = block_shape; + keywords["paddings"] = paddings; + var _op = tf.OpDefLib._apply_op_helper("SpaceToBatchND", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "Tblock_shape", _op._get_attr_type("Tblock_shape"), "Tpaddings", _op._get_attr_type("Tpaddings") }; + _execute.record_gradient("SpaceToBatchND", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor space_to_batch_nd_eager_fallback(Tensor input, Tensor block_shape, Tensor paddings, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, block_shape, paddings }; + object[] _attrs = new object[] { "T", input.dtype, "Tblock_shape", block_shape.dtype, "Tpaddings", paddings.dtype }; + var _result = _execute.execute("SpaceToBatchND", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("SpaceToBatchND", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// SpaceToDepth for tensors of type T. + /// + /// + /// + /// Rearranges blocks of spatial data, into depth. More specifically, + /// this op outputs a copy of the input tensor where values from the `height` + /// and `width` dimensions are moved to the `depth` dimension. + /// The attr `block_size` indicates the input block size. + /// + /// * Non-overlapping blocks of size `block_size x block size` are rearranged + /// into depth at each location. + /// * The depth of the output tensor is `block_size * block_size * input_depth`. + /// * The Y, X coordinates within each block of the input become the high order + /// component of the output channel index. + /// * The input tensor's height and width must be divisible by block_size. + /// + /// The `data_format` attr specifies the layout of the input and output tensors + /// with the following options: + /// "NHWC": `[ batch, height, width, channels ]` + /// "NCHW": `[ batch, channels, height, width ]` + /// "NCHW_VECT_C": + /// `qint8 [ batch, channels / 4, height, width, 4 ]` + /// + /// It is useful to consider the operation as transforming a 6-D Tensor. + /// e.g. for data_format = NHWC, + /// Each element in the input tensor can be specified via 6 coordinates, + /// ordered by decreasing memory layout significance as: + /// n,oY,bY,oX,bX,iC (where n=batch index, oX, oY means X or Y coordinates + /// within the output image, bX, bY means coordinates + /// within the input block, iC means input channels). + /// The output would be a transpose to the following layout: + /// n,oY,oX,bY,bX,iC + /// + /// This operation is useful for resizing the activations between convolutions + /// (but keeping all data), e.g. instead of pooling. It is also useful for training + /// purely convolutional models. + /// + /// For example, given an input of shape `[1, 2, 2, 1]`, data_format = "NHWC" and + /// block_size = 2: + /// + /// ``` + /// x = [[[[1], [2]], + /// [[3], [4]]]] + /// ``` + /// + /// This operation will output a tensor of shape `[1, 1, 1, 4]`: + /// + /// ``` + /// [[[[1, 2, 3, 4]]]] + /// ``` + /// + /// Here, the input has a batch of 1 and each batch element has shape `[2, 2, 1]`, + /// the corresponding output will have a single element (i.e. width and height are + /// both 1) and will have a depth of 4 channels (1 * block_size * block_size). + /// The output element shape is `[1, 1, 4]`. + /// + /// For an input tensor with larger depth, here of shape `[1, 2, 2, 3]`, e.g. + /// + /// ``` + /// x = [[[[1, 2, 3], [4, 5, 6]], + /// [[7, 8, 9], [10, 11, 12]]]] + /// ``` + /// + /// This operation, for block_size of 2, will return the following tensor of shape + /// `[1, 1, 1, 12]` + /// + /// ``` + /// [[[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]]]] + /// ``` + /// + /// Similarly, for the following input of shape `[1 4 4 1]`, and a block size of 2: + /// + /// ``` + /// x = [[[[1], [2], [5], [6]], + /// [[3], [4], [7], [8]], + /// [[9], [10], [13], [14]], + /// [[11], [12], [15], [16]]]] + /// ``` + /// + /// the operator will return the following tensor of shape `[1 2 2 4]`: + /// + /// ``` + /// x = [[[[1, 2, 3, 4], + /// [5, 6, 7, 8]], + /// [[9, 10, 11, 12], + /// [13, 14, 15, 16]]]] + /// ``` + /// + /// + /// + /// + /// + /// The size of the spatial block. + /// + /// + /// + /// + public static Tensor space_to_depth(Tensor input, int block_size = 0, string data_format = "NHWC", string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "SpaceToDepth", name) { args = new object[] { input }, attrs = new Dictionary() { ["block_size"] = block_size, ["data_format"] = data_format } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return space_to_depth_eager_fallback(input, block_size: block_size, data_format: data_format, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + if (data_format is null) + { + data_format = "NHWC"; + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["block_size"] = block_size; + keywords["data_format"] = data_format; + var _op = tf.OpDefLib._apply_op_helper("SpaceToDepth", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "block_size", _op._get_attr_int("block_size"), "data_format", _op.get_attr("data_format") }; + _execute.record_gradient("SpaceToDepth", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor space_to_depth_eager_fallback(Tensor input, int block_size, string data_format, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input }; + object[] _attrs = new object[] { "T", input.dtype, "block_size", block_size, "data_format", data_format }; + var _result = _execute.execute("SpaceToDepth", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("SpaceToDepth", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Splits a tensor into `num_split` tensors along one dimension. + /// + /// + /// + /// + /// + /// The number of ways to split. Must evenly divide + /// `value.shape[split_dim]`. + /// + /// + /// + public static Tensor[] split(Tensor split_dim, Tensor value, int num_split = 0, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Split", name) { args = new object[] { split_dim, value }, attrs = new Dictionary() { ["num_split"] = num_split } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return split_eager_fallback(split_dim, value, num_split: num_split, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["split_dim"] = split_dim; + keywords["value"] = value; + keywords["num_split"] = num_split; + var _op = tf.OpDefLib._apply_op_helper("Split", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "num_split", _op._get_attr_int("num_split"), "T", _op._get_attr_type("T") }; + _execute.record_gradient("Split", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] split_eager_fallback(Tensor split_dim, Tensor value, int num_split, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { split_dim, value }; + object[] _attrs = new object[] { "num_split", num_split, "T", value.dtype }; + var _result = _execute.execute("Split", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Split", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// Splits a tensor into `num_split` tensors along one dimension. + /// + /// + /// + /// + /// + /// + public static Tensor[] split_v(Tensor value, Tensor size_splits, Tensor split_dim, int num_split = 0, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "SplitV", name) { args = new object[] { value, size_splits, split_dim }, attrs = new Dictionary() { ["num_split"] = num_split } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return split_v_eager_fallback(value, size_splits, split_dim, num_split: num_split, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["value"] = value; + keywords["size_splits"] = size_splits; + keywords["split_dim"] = split_dim; + keywords["num_split"] = num_split; + var _op = tf.OpDefLib._apply_op_helper("SplitV", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "num_split", _op._get_attr_int("num_split"), "T", _op._get_attr_type("T"), "Tlen", _op._get_attr_type("Tlen") }; + _execute.record_gradient("SplitV", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] split_v_eager_fallback(Tensor value, Tensor size_splits, Tensor split_dim, int num_split, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { value, size_splits, split_dim }; + object[] _attrs = new object[] { "num_split", num_split, "T", value.dtype, "Tlen", size_splits.dtype }; + var _result = _execute.execute("SplitV", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("SplitV", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// Removes dimensions of size 1 from the shape of a tensor. + /// + /// + /// + /// Given a tensor `input`, this operation returns a tensor of the same type with + /// all dimensions of size 1 removed. If you don't want to remove all size 1 + /// dimensions, you can remove specific size 1 dimensions by specifying + /// `squeeze_dims`. + /// + /// For example: + /// + /// ``` + /// # 't' is a tensor of shape [1, 2, 1, 3, 1, 1] + /// shape(squeeze(t)) ==> [2, 3] + /// ``` + /// + /// Or, to remove specific size 1 dimensions: + /// + /// ``` + /// # 't' is a tensor of shape [1, 2, 1, 3, 1, 1] + /// shape(squeeze(t, [2, 4])) ==> [1, 2, 3, 1] + /// ``` + /// + /// + /// + /// + /// + /// If specified, only squeezes the dimensions listed. The dimension + /// index starts at 0. It is an error to squeeze a dimension that is not 1. Must + /// be in the range `[-rank(input), rank(input))`. + /// + /// + /// + public static Tensor squeeze(Tensor input, int[] squeeze_dims = null, string? name = null) + { + var _ctx = tf.Context; + if (squeeze_dims is null) + { + squeeze_dims = new int[] { }; + } + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Squeeze", name) { args = new object[] { input }, attrs = new Dictionary() { ["squeeze_dims"] = squeeze_dims } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return squeeze_eager_fallback(input, squeeze_dims: squeeze_dims, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["squeeze_dims"] = squeeze_dims; + var _op = tf.OpDefLib._apply_op_helper("Squeeze", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "squeeze_dims", _op.get_attr("squeeze_dims") }; + _execute.record_gradient("Squeeze", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor squeeze_eager_fallback(Tensor input, int[] squeeze_dims, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input }; + object[] _attrs = new object[] { "T", input.dtype, "squeeze_dims", squeeze_dims }; + var _result = _execute.execute("Squeeze", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Squeeze", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Stops gradient computation. + /// + /// + /// + /// When executed in a graph, this op outputs its input tensor as-is. + /// + /// When building ops to compute gradients, this op prevents the contribution of + /// its inputs to be taken into account. Normally, the gradient generator adds ops + /// to a graph to compute the derivatives of a specified 'loss' by recursively + /// finding out inputs that contributed to its computation. If you insert this op + /// in the graph it inputs are masked from the gradient generator. They are not + /// taken into account for computing gradients. + /// + /// This is useful any time you want to compute a value with TensorFlow but need + /// to pretend that the value was a constant. For example, the softmax function + /// for a vector x can be written as + /// + /// ```python + /// + /// def softmax(x): + /// numerator = tf.exp(x) + /// denominator = tf.reduce_sum(numerator) + /// return numerator / denominator + /// ``` + /// + /// This however is susceptible to overflow if the values in x are large. An + /// alternative more stable way is to subtract the maximum of x from each of the + /// values. + /// + /// ```python + /// + /// def stable_softmax(x): + /// z = x - tf.reduce_max(x) + /// numerator = tf.exp(z) + /// denominator = tf.reduce_sum(numerator) + /// return numerator / denominator + /// ``` + /// + /// However, when we backprop through the softmax to x, we dont want to backprop + /// through the `tf.reduce_max(x)` (if the max values are not unique then the + /// gradient could flow to the wrong input) calculation and treat that as a + /// constant. Therefore, we should write this out as + /// + /// ```python + /// + /// def stable_softmax(x): + /// z = x - tf.stop_gradient(tf.reduce_max(x)) + /// numerator = tf.exp(z) + /// denominator = tf.reduce_sum(numerator) + /// return numerator / denominator + /// ``` + /// + /// Some other examples include: + /// + /// * The *EM* algorithm where the *M-step* should not involve backpropagation + /// through the output of the *E-step*. + /// * Contrastive divergence training of Boltzmann machines where, when + /// differentiating the energy function, the training must not backpropagate + /// through the graph that generated the samples from the model. + /// * Adversarial training, where no backprop should happen through the adversarial + /// example generation process. + /// + /// + /// + /// + public static Tensor stop_gradient(Tensor input, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "StopGradient", name) { args = new object[] { input }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return stop_gradient_eager_fallback(input, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + var _op = tf.OpDefLib._apply_op_helper("StopGradient", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("StopGradient", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor stop_gradient_eager_fallback(Tensor input, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input }; + object[] _attrs = new object[] { "T", input.dtype }; + var _result = _execute.execute("StopGradient", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("StopGradient", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Return a strided slice from `input`. + /// + /// + /// + /// Note, most python users will want to use the Python `Tensor.__getitem__` + /// or `Variable.__getitem__` rather than this op directly. + /// + /// The goal of this op is to produce a new tensor with a subset of + /// the elements from the `n` dimensional `input` tensor. The subset is chosen using + /// a sequence of `m` sparse range specifications encoded into the arguments + /// of this function. Note, in some cases + /// `m` could be equal to `n`, but this need not be the case. Each + /// range specification entry can be one of the following: + /// + /// - An ellipsis (...). Ellipses are used to imply zero or more + /// dimensions of full-dimension selection and are produced using + /// `ellipsis_mask`. For example, `foo[...]` is the identity slice. + /// + /// - A new axis. This is used to insert a new shape=1 dimension and is + /// produced using `new_axis_mask`. For example, `foo[:, ...]` where + /// `foo` is shape `(3, 4)` produces a `(1, 3, 4)` tensor. + /// + /// + /// - A range `begin:end:stride`. This is used to specify how much to choose from + /// a given dimension. `stride` can be any integer but 0. `begin` is an integer + /// which represents the index of the first value to select while `end` represents + /// the index of the last value to select. The number of values selected in each + /// dimension is `end - begin` if `stride > 0` and `begin - end` if `stride < 0`. + /// `begin` and `end` can be negative where `-1` is the last element, `-2` is + /// the second to last. `begin_mask` controls whether to replace the explicitly + /// given `begin` with an implicit effective value of `0` if `stride > 0` and + /// `-1` if `stride < 0`. `end_mask` is analogous but produces the number + /// required to create the largest open interval. For example, given a shape + /// `(3,)` tensor `foo[:]`, the effective `begin` and `end` are `0` and `3`. Do + /// not assume this is equivalent to `foo[0:-1]` which has an effective `begin` + /// and `end` of `0` and `2`. Another example is `foo[-2::-1]` which reverses the + /// first dimension of a tensor while dropping the last two (in the original + /// order elements). For example `foo = [1,2,3,4]; foo[-2::-1]` is `[4,3]`. + /// + /// - A single index. This is used to keep only elements that have a given + /// index. For example (`foo[2, :]` on a shape `(5,6)` tensor produces a + /// shape `(6,)` tensor. This is encoded in `begin` and `end` and + /// `shrink_axis_mask`. + /// + /// Each conceptual range specification is encoded in the op's argument. This + /// encoding is best understand by considering a non-trivial example. In + /// particular, + /// `foo[1, 2:4, None, ..., :-3:-1, :]` will be encoded as + /// + /// ``` + /// begin = [1, 2, x, x, 0, x] # x denotes don't care (usually 0) + /// end = [2, 4, x, x, -3, x] + /// strides = [1, 1, x, x, -1, 1] + /// begin_mask = 1<<4 | 1<<5 = 48 + /// end_mask = 1<<5 = 32 + /// ellipsis_mask = 1<<3 = 8 + /// new_axis_mask = 1<<2 = 4 + /// shrink_axis_mask = 1<<0 = 1 + /// ``` + /// + /// In this case if `foo.shape` is (5, 5, 5, 5, 5, 5) the final shape of + /// the slice becomes (2, 1, 5, 5, 2, 5). + /// Let us walk step by step through each argument specification. + /// + /// 1. The first argument in the example slice is turned into `begin = 1` and + /// `end = begin + 1 = 2`. To disambiguate from the original spec `2:4` we + /// also set the appropriate bit in `shrink_axis_mask`. + /// + /// 2. `2:4` is contributes 2, 4, 1 to begin, end, and stride. All masks have + /// zero bits contributed. + /// + /// 3. None is a synonym for `tf.newaxis`. This means insert a dimension of size 1 + /// dimension in the final shape. Dummy values are contributed to begin, + /// end and stride, while the new_axis_mask bit is set. + /// + /// 4. `...` grab the full ranges from as many dimensions as needed to + /// fully specify a slice for every dimension of the input shape. + /// + /// 5. `:-3:-1` shows the use of negative indices. A negative index `i` associated + /// with a dimension that has shape `s` is converted to a positive index + /// `s + i`. So `-1` becomes `s-1` (i.e. the last element). This conversion + /// is done internally so begin, end and strides receive x, -3, and -1. + /// The appropriate begin_mask bit is set to indicate the start range is the + /// full range (ignoring the x). + /// + /// 6. `:` indicates that the entire contents of the corresponding dimension + /// is selected. This is equivalent to `::` or `0::1`. begin, end, and strides + /// receive 0, 0, and 1, respectively. The appropriate bits in `begin_mask` and + /// `end_mask` are also set. + /// + /// *Requirements*: + /// `0 != strides[i] for i in [0, m)` + /// `ellipsis_mask must be a power of two (only one ellipsis)` + /// + /// + /// + /// + /// + /// + /// + /// + /// a bitmask where a bit i being 1 means to ignore the begin + /// value and instead use the largest interval possible. At runtime + /// begin[i] will be replaced with `[0, n-1)` if `stride[i] > 0` or + /// `[-1, n-1]` if `stride[i] < 0` + /// + /// + /// + /// + /// analogous to `begin_mask` + /// + /// + /// + /// + /// a bitmask where bit `i` being 1 means the `i`th + /// position is actually an ellipsis. One bit at most can be 1. + /// If `ellipsis_mask == 0`, then an implicit ellipsis mask of `1 << (m+1)` + /// is provided. This means that `foo[3:5] == foo[3:5, ...]`. An ellipsis + /// implicitly creates as many range specifications as necessary to fully + /// specify the sliced range for every dimension. For example for a 4-dimensional + /// tensor `foo` the slice `foo[2, ..., 5:8]` implies `foo[2, :, :, 5:8]`. + /// + /// + /// + /// + /// a bitmask where bit `i` being 1 means the `i`th + /// specification creates a new shape 1 dimension. For example + /// `foo[:4, tf.newaxis, :2]` would produce a shape `(4, 1, 2)` tensor. + /// + /// + /// + /// + /// a bitmask where bit `i` implies that the `i`th + /// specification should shrink the dimensionality. begin and end + /// must imply a slice of size 1 in the dimension. For example in + /// python one might do `foo[:, 3, :]` which would result in + /// `shrink_axis_mask` being 2. + /// + /// + /// + public static Tensor strided_slice(Tensor input, Tensor begin, Tensor end, Tensor strides, int begin_mask = 0, int end_mask = 0, int ellipsis_mask = 0, int new_axis_mask = 0, int shrink_axis_mask = 0, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "StridedSlice", name) { args = new object[] { input, begin, end, strides }, attrs = new Dictionary() { ["begin_mask"] = begin_mask, ["end_mask"] = end_mask, ["ellipsis_mask"] = ellipsis_mask, ["new_axis_mask"] = new_axis_mask, ["shrink_axis_mask"] = shrink_axis_mask } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return strided_slice_eager_fallback(input, begin, end, strides, begin_mask: begin_mask, end_mask: end_mask, ellipsis_mask: ellipsis_mask, new_axis_mask: new_axis_mask, shrink_axis_mask: shrink_axis_mask, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["begin"] = begin; + keywords["end"] = end; + keywords["strides"] = strides; + keywords["begin_mask"] = begin_mask; + keywords["end_mask"] = end_mask; + keywords["ellipsis_mask"] = ellipsis_mask; + keywords["new_axis_mask"] = new_axis_mask; + keywords["shrink_axis_mask"] = shrink_axis_mask; + var _op = tf.OpDefLib._apply_op_helper("StridedSlice", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "Index", _op._get_attr_type("Index"), "begin_mask", _op._get_attr_int("begin_mask"), "end_mask", _op._get_attr_int("end_mask"), "ellipsis_mask", _op._get_attr_int("ellipsis_mask"), "new_axis_mask", _op._get_attr_int("new_axis_mask"), "shrink_axis_mask", _op._get_attr_int("shrink_axis_mask") }; + _execute.record_gradient("StridedSlice", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor strided_slice_eager_fallback(Tensor input, Tensor begin, Tensor end, Tensor strides, int begin_mask, int end_mask, int ellipsis_mask, int new_axis_mask, int shrink_axis_mask, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, begin, end, strides }; + object[] _attrs = new object[] { "T", input.dtype, "Index", begin.dtype, "begin_mask", begin_mask, "end_mask", end_mask, "ellipsis_mask", ellipsis_mask, "new_axis_mask", new_axis_mask, "shrink_axis_mask", shrink_axis_mask }; + var _result = _execute.execute("StridedSlice", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("StridedSlice", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Assign `value` to the sliced l-value reference of `ref`. + /// + /// + /// + /// The values of `value` are assigned to the positions in the variable + /// `ref` that are selected by the slice parameters. The slice parameters + /// `begin`, `end`, `strides`, etc. work exactly as in `StridedSlice`. + /// + /// NOTE this op currently does not support broadcasting and so `value`'s + /// shape must be exactly the shape produced by the slice of `ref`. + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + public static Tensor strided_slice_assign(Tensor ref_, Tensor begin, Tensor end, Tensor strides, Tensor value, int begin_mask = 0, int end_mask = 0, int ellipsis_mask = 0, int new_axis_mask = 0, int shrink_axis_mask = 0, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + throw new RuntimeError("strided_slice_assign op does not support eager execution. Arg ref is a ref."); + } + Dictionary keywords = new(); + keywords["ref"] = ref_; + keywords["begin"] = begin; + keywords["end"] = end; + keywords["strides"] = strides; + keywords["value"] = value; + keywords["begin_mask"] = begin_mask; + keywords["end_mask"] = end_mask; + keywords["ellipsis_mask"] = ellipsis_mask; + keywords["new_axis_mask"] = new_axis_mask; + keywords["shrink_axis_mask"] = shrink_axis_mask; + var _op = tf.OpDefLib._apply_op_helper("StridedSliceAssign", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "Index", _op._get_attr_type("Index"), "begin_mask", _op._get_attr_int("begin_mask"), "end_mask", _op._get_attr_int("end_mask"), "ellipsis_mask", _op._get_attr_int("ellipsis_mask"), "new_axis_mask", _op._get_attr_int("new_axis_mask"), "shrink_axis_mask", _op._get_attr_int("shrink_axis_mask") }; + _execute.record_gradient("StridedSliceAssign", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor strided_slice_assign_eager_fallback(Tensor ref_, Tensor begin, Tensor end, Tensor strides, Tensor value, int begin_mask, int end_mask, int ellipsis_mask, int new_axis_mask, int shrink_axis_mask, string name, Context ctx) + { + throw new RuntimeError($"strided_slice_assign op does not support eager execution. Arg 'ref' is a ref."); + } + /// + /// Returns the gradient of `StridedSlice`. + /// + /// + /// + /// Since `StridedSlice` cuts out pieces of its `input` which is size + /// `shape`, its gradient will have the same shape (which is passed here + /// as `shape`). The gradient will be zero in any element that the slice + /// does not select. + /// + /// Arguments are the same as StridedSliceGrad with the exception that + /// `dy` is the input gradient to be propagated and `shape` is the + /// shape of `StridedSlice`'s `input`. + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + public static Tensor strided_slice_grad(Tensor shape, Tensor begin, Tensor end, Tensor strides, Tensor dy, int begin_mask = 0, int end_mask = 0, int ellipsis_mask = 0, int new_axis_mask = 0, int shrink_axis_mask = 0, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "StridedSliceGrad", name) { args = new object[] { shape, begin, end, strides, dy }, attrs = new Dictionary() { ["begin_mask"] = begin_mask, ["end_mask"] = end_mask, ["ellipsis_mask"] = ellipsis_mask, ["new_axis_mask"] = new_axis_mask, ["shrink_axis_mask"] = shrink_axis_mask } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return strided_slice_grad_eager_fallback(shape, begin, end, strides, dy, begin_mask: begin_mask, end_mask: end_mask, ellipsis_mask: ellipsis_mask, new_axis_mask: new_axis_mask, shrink_axis_mask: shrink_axis_mask, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["shape"] = shape; + keywords["begin"] = begin; + keywords["end"] = end; + keywords["strides"] = strides; + keywords["dy"] = dy; + keywords["begin_mask"] = begin_mask; + keywords["end_mask"] = end_mask; + keywords["ellipsis_mask"] = ellipsis_mask; + keywords["new_axis_mask"] = new_axis_mask; + keywords["shrink_axis_mask"] = shrink_axis_mask; + var _op = tf.OpDefLib._apply_op_helper("StridedSliceGrad", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "Index", _op._get_attr_type("Index"), "begin_mask", _op._get_attr_int("begin_mask"), "end_mask", _op._get_attr_int("end_mask"), "ellipsis_mask", _op._get_attr_int("ellipsis_mask"), "new_axis_mask", _op._get_attr_int("new_axis_mask"), "shrink_axis_mask", _op._get_attr_int("shrink_axis_mask") }; + _execute.record_gradient("StridedSliceGrad", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor strided_slice_grad_eager_fallback(Tensor shape, Tensor begin, Tensor end, Tensor strides, Tensor dy, int begin_mask, int end_mask, int ellipsis_mask, int new_axis_mask, int shrink_axis_mask, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { shape, begin, end, strides, dy }; + object[] _attrs = new object[] { "T", dy.dtype, "Index", shape.dtype, "begin_mask", begin_mask, "end_mask", end_mask, "ellipsis_mask", ellipsis_mask, "new_axis_mask", new_axis_mask, "shrink_axis_mask", shrink_axis_mask }; + var _result = _execute.execute("StridedSliceGrad", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("StridedSliceGrad", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Adds sparse `updates` to an existing tensor according to `indices`. + /// + /// + /// + /// This operation creates a new tensor by adding sparse `updates` to the passed + /// in `tensor`. + /// This operation is very similar to `tf.compat.v1.scatter_nd_add`, except that the + /// updates are added onto an existing tensor (as opposed to a variable). If the + /// memory for the existing tensor cannot be re-used, a copy is made and updated. + /// + /// `indices` is an integer tensor containing indices into a new tensor of shape + /// `tensor.shape`. The last dimension of `indices` can be at most the rank of + /// `tensor.shape`: + /// + /// ``` + /// indices.shape[-1] <= tensor.shape.rank + /// ``` + /// + /// The last dimension of `indices` corresponds to indices into elements + /// (if `indices.shape[-1] = tensor.shape.rank`) or slices + /// (if `indices.shape[-1] < tensor.shape.rank`) along dimension + /// `indices.shape[-1]` of `tensor.shape`. `updates` is a tensor with shape + /// + /// ``` + /// indices.shape[:-1] + tensor.shape[indices.shape[-1]:] + /// ``` + /// + /// The simplest form of `tensor_scatter_nd_add` is to add individual elements to a + /// tensor by index. For example, say we want to add 4 elements in a rank-1 + /// tensor with 8 elements. + /// + /// In Python, this scatter add operation would look like this: + /// + /// >>> indices = tf.constant([[4], [3], [1], [7]]) + /// >>> updates = tf.constant([9, 10, 11, 12]) + /// >>> tensor = tf.ones([8], dtype=tf.int32) + /// >>> updated = tf.tensor_scatter_nd_add(tensor, indices, updates) + /// >>> updated + /// + /// + /// We can also, insert entire slices of a higher rank tensor all at once. For + /// example, if we wanted to insert two slices in the first dimension of a + /// rank-3 tensor with two matrices of new values. + /// + /// In Python, this scatter add operation would look like this: + /// + /// >>> indices = tf.constant([[0], [2]]) + /// >>> updates = tf.constant([[[5, 5, 5, 5], [6, 6, 6, 6], + /// ... [7, 7, 7, 7], [8, 8, 8, 8]], + /// ... [[5, 5, 5, 5], [6, 6, 6, 6], + /// ... [7, 7, 7, 7], [8, 8, 8, 8]]]) + /// >>> tensor = tf.ones([4, 4, 4],dtype=tf.int32) + /// >>> updated = tf.tensor_scatter_nd_add(tensor, indices, updates) + /// >>> updated + /// + /// + /// Note: on CPU, if an out of bound index is found, an error is returned. + /// On GPU, if an out of bound index is found, the index is ignored. + /// + /// + /// + /// + /// + /// + public static Tensor tensor_scatter_add(Tensor tensor, Tensor indices, Tensor updates, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "TensorScatterAdd", name) { args = new object[] { tensor, indices, updates }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return tensor_scatter_add_eager_fallback(tensor, indices, updates, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["tensor"] = tensor; + keywords["indices"] = indices; + keywords["updates"] = updates; + var _op = tf.OpDefLib._apply_op_helper("TensorScatterAdd", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "Tindices", _op._get_attr_type("Tindices") }; + _execute.record_gradient("TensorScatterAdd", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor tensor_scatter_add_eager_fallback(Tensor tensor, Tensor indices, Tensor updates, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { tensor, indices, updates }; + object[] _attrs = new object[] { "T", tensor.dtype, "Tindices", indices.dtype }; + var _result = _execute.execute("TensorScatterAdd", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("TensorScatterAdd", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Apply a sparse update to a tensor taking the element-wise maximum. + /// + /// + /// + /// Returns a new tensor copied from `tensor` whose values are element-wise maximum between + /// tensor and updates according to the indices. + /// + /// >>> tensor = [0, 0, 0, 0, 0, 0, 0, 0] + /// >>> indices = [[1], [4], [5]] + /// >>> updates = [1, -1, 1] + /// >>> tf.tensor_scatter_nd_max(tensor, indices, updates).numpy() + /// array([0, 1, 0, 0, 0, 1, 0, 0], dtype=int32) + /// + /// Refer to `tf.tensor_scatter_nd_update` for more details. + /// + /// + /// + /// + /// + /// + public static Tensor tensor_scatter_max(Tensor tensor, Tensor indices, Tensor updates, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "TensorScatterMax", name) { args = new object[] { tensor, indices, updates }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return tensor_scatter_max_eager_fallback(tensor, indices, updates, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["tensor"] = tensor; + keywords["indices"] = indices; + keywords["updates"] = updates; + var _op = tf.OpDefLib._apply_op_helper("TensorScatterMax", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "Tindices", _op._get_attr_type("Tindices") }; + _execute.record_gradient("TensorScatterMax", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor tensor_scatter_max_eager_fallback(Tensor tensor, Tensor indices, Tensor updates, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { tensor, indices, updates }; + object[] _attrs = new object[] { "T", tensor.dtype, "Tindices", indices.dtype }; + var _result = _execute.execute("TensorScatterMax", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("TensorScatterMax", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// + /// + /// + /// + /// + /// + public static Tensor tensor_scatter_min(Tensor tensor, Tensor indices, Tensor updates, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "TensorScatterMin", name) { args = new object[] { tensor, indices, updates }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return tensor_scatter_min_eager_fallback(tensor, indices, updates, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["tensor"] = tensor; + keywords["indices"] = indices; + keywords["updates"] = updates; + var _op = tf.OpDefLib._apply_op_helper("TensorScatterMin", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "Tindices", _op._get_attr_type("Tindices") }; + _execute.record_gradient("TensorScatterMin", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor tensor_scatter_min_eager_fallback(Tensor tensor, Tensor indices, Tensor updates, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { tensor, indices, updates }; + object[] _attrs = new object[] { "T", tensor.dtype, "Tindices", indices.dtype }; + var _result = _execute.execute("TensorScatterMin", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("TensorScatterMin", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Subtracts sparse `updates` from an existing tensor according to `indices`. + /// + /// + /// + /// This operation creates a new tensor by subtracting sparse `updates` from the + /// passed in `tensor`. + /// This operation is very similar to `tf.scatter_nd_sub`, except that the updates + /// are subtracted from an existing tensor (as opposed to a variable). If the memory + /// for the existing tensor cannot be re-used, a copy is made and updated. + /// + /// `indices` is an integer tensor containing indices into a new tensor of shape + /// `shape`. The last dimension of `indices` can be at most the rank of `shape`: + /// + /// indices.shape[-1] <= shape.rank + /// + /// The last dimension of `indices` corresponds to indices into elements + /// (if `indices.shape[-1] = shape.rank`) or slices + /// (if `indices.shape[-1] < shape.rank`) along dimension `indices.shape[-1]` of + /// `shape`. `updates` is a tensor with shape + /// + /// indices.shape[:-1] + shape[indices.shape[-1]:] + /// + /// The simplest form of tensor_scatter_sub is to subtract individual elements + /// from a tensor by index. For example, say we want to insert 4 scattered elements + /// in a rank-1 tensor with 8 elements. + /// + /// In Python, this scatter subtract operation would look like this: + /// + /// ```python + /// indices = tf.constant([[4], [3], [1], [7]]) + /// updates = tf.constant([9, 10, 11, 12]) + /// tensor = tf.ones([8], dtype=tf.int32) + /// updated = tf.tensor_scatter_nd_sub(tensor, indices, updates) + /// print(updated) + /// ``` + /// + /// The resulting tensor would look like this: + /// + /// [1, -10, 1, -9, -8, 1, 1, -11] + /// + /// We can also, insert entire slices of a higher rank tensor all at once. For + /// example, if we wanted to insert two slices in the first dimension of a + /// rank-3 tensor with two matrices of new values. + /// + /// In Python, this scatter add operation would look like this: + /// + /// ```python + /// indices = tf.constant([[0], [2]]) + /// updates = tf.constant([[[5, 5, 5, 5], [6, 6, 6, 6], + /// [7, 7, 7, 7], [8, 8, 8, 8]], + /// [[5, 5, 5, 5], [6, 6, 6, 6], + /// [7, 7, 7, 7], [8, 8, 8, 8]]]) + /// tensor = tf.ones([4, 4, 4],dtype=tf.int32) + /// updated = tf.tensor_scatter_nd_sub(tensor, indices, updates) + /// print(updated) + /// ``` + /// + /// The resulting tensor would look like this: + /// + /// [[[-4, -4, -4, -4], [-5, -5, -5, -5], [-6, -6, -6, -6], [-7, -7, -7, -7]], + /// [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]], + /// [[-4, -4, -4, -4], [-5, -5, -5, -5], [-6, -6, -6, -6], [-7, -7, -7, -7]], + /// [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]] + /// + /// Note that on CPU, if an out of bound index is found, an error is returned. + /// On GPU, if an out of bound index is found, the index is ignored. + /// + /// + /// + /// + /// + /// + public static Tensor tensor_scatter_sub(Tensor tensor, Tensor indices, Tensor updates, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "TensorScatterSub", name) { args = new object[] { tensor, indices, updates }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return tensor_scatter_sub_eager_fallback(tensor, indices, updates, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["tensor"] = tensor; + keywords["indices"] = indices; + keywords["updates"] = updates; + var _op = tf.OpDefLib._apply_op_helper("TensorScatterSub", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "Tindices", _op._get_attr_type("Tindices") }; + _execute.record_gradient("TensorScatterSub", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor tensor_scatter_sub_eager_fallback(Tensor tensor, Tensor indices, Tensor updates, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { tensor, indices, updates }; + object[] _attrs = new object[] { "T", tensor.dtype, "Tindices", indices.dtype }; + var _result = _execute.execute("TensorScatterSub", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("TensorScatterSub", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Scatter `updates` into an existing tensor according to `indices`. + /// + /// + /// + /// This operation creates a new tensor by applying sparse `updates` to the passed + /// in `tensor`. + /// This operation is very similar to `tf.scatter_nd`, except that the updates are + /// scattered onto an existing tensor (as opposed to a zero-tensor). If the memory + /// for the existing tensor cannot be re-used, a copy is made and updated. + /// + /// If `indices` contains duplicates, then we pick the last update for the index. + /// + /// If an out of bound index is found on CPU, an error is returned. + /// + /// **WARNING**: There are some GPU specific semantics for this operation. + /// - If an out of bound index is found, the index is ignored. + /// - The order in which updates are applied is nondeterministic, so the output + /// will be nondeterministic if `indices` contains duplicates. + /// + /// `indices` is an integer tensor containing indices into a new tensor of shape + /// `shape`. + /// + /// * `indices` must have at least 2 axes: `(num_updates, index_depth)`. + /// * The last axis of `indices` is how deep to index into `tensor` so this index + /// depth must be less than the rank of `tensor`: `indices.shape[-1] <= tensor.ndim` + /// + /// if `indices.shape[-1] = tensor.rank` this Op indexes and updates scalar elements. + /// if `indices.shape[-1] < tensor.rank` it indexes and updates slices of the input + /// `tensor`. + /// + /// Each `update` has a rank of `tensor.rank - indices.shape[-1]`. + /// The overall shape of `updates` is: + /// + /// ``` + /// indices.shape[:-1] + tensor.shape[indices.shape[-1]:] + /// ``` + /// + /// For usage examples see the python [tf.tensor_scatter_nd_update]( + /// https://www.tensorflow.org/api_docs/python/tf/tensor_scatter_nd_update) function + /// + /// + /// + /// + /// + /// + /// + public static Tensor tensor_scatter_update(Tensor tensor, Tensor indices, Tensor updates, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "TensorScatterUpdate", name) { args = new object[] { tensor, indices, updates }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return tensor_scatter_update_eager_fallback(tensor, indices, updates, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["tensor"] = tensor; + keywords["indices"] = indices; + keywords["updates"] = updates; + var _op = tf.OpDefLib._apply_op_helper("TensorScatterUpdate", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "Tindices", _op._get_attr_type("Tindices") }; + _execute.record_gradient("TensorScatterUpdate", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor tensor_scatter_update_eager_fallback(Tensor tensor, Tensor indices, Tensor updates, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { tensor, indices, updates }; + object[] _attrs = new object[] { "T", tensor.dtype, "Tindices", indices.dtype }; + var _result = _execute.execute("TensorScatterUpdate", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("TensorScatterUpdate", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Assign `value` to the sliced l-value reference of `input`. + /// + /// + /// + /// The values of `value` are assigned to the positions in the tensor `input` that + /// are selected by the slice parameters. The slice parameters `begin` `end` + /// `strides` etc. work exactly as in `StridedSlice`. + /// + /// NOTE this op currently does not support broadcasting and so `value`'s shape + /// must be exactly the shape produced by the slice of `input`. + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + public static Tensor tensor_strided_slice_update(Tensor input, Tensor begin, Tensor end, Tensor strides, Tensor value, int begin_mask = 0, int end_mask = 0, int ellipsis_mask = 0, int new_axis_mask = 0, int shrink_axis_mask = 0, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "TensorStridedSliceUpdate", name) { args = new object[] { input, begin, end, strides, value }, attrs = new Dictionary() { ["begin_mask"] = begin_mask, ["end_mask"] = end_mask, ["ellipsis_mask"] = ellipsis_mask, ["new_axis_mask"] = new_axis_mask, ["shrink_axis_mask"] = shrink_axis_mask } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return tensor_strided_slice_update_eager_fallback(input, begin, end, strides, value, begin_mask: begin_mask, end_mask: end_mask, ellipsis_mask: ellipsis_mask, new_axis_mask: new_axis_mask, shrink_axis_mask: shrink_axis_mask, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["begin"] = begin; + keywords["end"] = end; + keywords["strides"] = strides; + keywords["value"] = value; + keywords["begin_mask"] = begin_mask; + keywords["end_mask"] = end_mask; + keywords["ellipsis_mask"] = ellipsis_mask; + keywords["new_axis_mask"] = new_axis_mask; + keywords["shrink_axis_mask"] = shrink_axis_mask; + var _op = tf.OpDefLib._apply_op_helper("TensorStridedSliceUpdate", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "Index", _op._get_attr_type("Index"), "begin_mask", _op._get_attr_int("begin_mask"), "end_mask", _op._get_attr_int("end_mask"), "ellipsis_mask", _op._get_attr_int("ellipsis_mask"), "new_axis_mask", _op._get_attr_int("new_axis_mask"), "shrink_axis_mask", _op._get_attr_int("shrink_axis_mask") }; + _execute.record_gradient("TensorStridedSliceUpdate", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor tensor_strided_slice_update_eager_fallback(Tensor input, Tensor begin, Tensor end, Tensor strides, Tensor value, int begin_mask, int end_mask, int ellipsis_mask, int new_axis_mask, int shrink_axis_mask, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, begin, end, strides, value }; + object[] _attrs = new object[] { "T", input.dtype, "Index", begin.dtype, "begin_mask", begin_mask, "end_mask", end_mask, "ellipsis_mask", ellipsis_mask, "new_axis_mask", new_axis_mask, "shrink_axis_mask", shrink_axis_mask }; + var _result = _execute.execute("TensorStridedSliceUpdate", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("TensorStridedSliceUpdate", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Constructs a tensor by tiling a given tensor. + /// + /// + /// + /// This operation creates a new tensor by replicating `input` `multiples` times. + /// The output tensor's i'th dimension has `input.dims(i) * multiples[i]` elements, + /// and the values of `input` are replicated `multiples[i]` times along the 'i'th + /// dimension. For example, tiling `[a b c d]` by `[2]` produces + /// `[a b c d a b c d]`. + /// + /// >>> a = tf.constant([[1,2,3],[4,5,6]], tf.int32) + /// >>> b = tf.constant([1,2], tf.int32) + /// >>> tf.tile(a, b) + /// + /// >>> c = tf.constant([2,1], tf.int32) + /// >>> tf.tile(a, c) + /// + /// >>> d = tf.constant([2,2], tf.int32) + /// >>> tf.tile(a, d) + /// + /// + /// + /// + /// + /// + public static Tensor tile(Tensor input, Tensor multiples, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Tile", name) { args = new object[] { input, multiples }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return tile_eager_fallback(input, multiples, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["multiples"] = multiples; + var _op = tf.OpDefLib._apply_op_helper("Tile", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "Tmultiples", _op._get_attr_type("Tmultiples") }; + _execute.record_gradient("Tile", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor tile_eager_fallback(Tensor input, Tensor multiples, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, multiples }; + object[] _attrs = new object[] { "T", input.dtype, "Tmultiples", multiples.dtype }; + var _result = _execute.execute("Tile", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Tile", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns the gradient of `Tile`. + /// + /// + /// + /// Since `Tile` takes an input and repeats the input `multiples` times + /// along each dimension, `TileGrad` takes in `multiples` and aggregates + /// each repeated tile of `input` into `output`. + /// + /// + /// + /// + /// + public static Tensor tile_grad(Tensor input, Tensor multiples, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "TileGrad", name) { args = new object[] { input, multiples }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return tile_grad_eager_fallback(input, multiples, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["multiples"] = multiples; + var _op = tf.OpDefLib._apply_op_helper("TileGrad", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("TileGrad", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor tile_grad_eager_fallback(Tensor input, Tensor multiples, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, multiples }; + object[] _attrs = new object[] { "T", input.dtype }; + var _result = _execute.execute("TileGrad", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("TileGrad", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Shuffle dimensions of x according to a permutation. + /// + /// + /// + /// The output `y` has the same rank as `x`. The shapes of `x` and `y` satisfy: + /// `y.shape[i] == x.shape[perm[i]] for i in [0, 1, ..., rank(x) - 1]` + /// + /// + /// + /// + /// + public static Tensor transpose(Tensor x, Tensor perm, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Transpose", name) { args = new object[] { x, perm }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return transpose_eager_fallback(x, perm, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + keywords["perm"] = perm; + var _op = tf.OpDefLib._apply_op_helper("Transpose", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "Tperm", _op._get_attr_type("Tperm") }; + _execute.record_gradient("Transpose", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor transpose_eager_fallback(Tensor x, Tensor perm, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x, perm }; + object[] _attrs = new object[] { "T", x.dtype, "Tperm", perm.dtype }; + var _result = _execute.execute("Transpose", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Transpose", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Finds unique elements in a 1-D tensor. + /// + /// + /// + /// This operation returns a tensor `y` containing all of the unique elements of `x` + /// sorted in the same order that they occur in `x`; `x` does not need to be sorted. + /// This operation also returns a tensor `idx` the same size as `x` that contains + /// the index of each value of `x` in the unique output `y`. In other words: + /// + /// `y[idx[i]] = x[i] for i in [0, 1,...,rank(x) - 1]` + /// + /// Examples: + /// + /// ``` + /// # tensor 'x' is [1, 1, 2, 4, 4, 4, 7, 8, 8] + /// y, idx = unique(x) + /// y ==> [1, 2, 4, 7, 8] + /// idx ==> [0, 0, 1, 2, 2, 2, 3, 4, 4] + /// ``` + /// + /// ``` + /// # tensor 'x' is [4, 5, 1, 2, 3, 3, 4, 5] + /// y, idx = unique(x) + /// y ==> [4, 5, 1, 2, 3] + /// idx ==> [0, 1, 2, 3, 4, 4, 0, 1] + /// ``` + /// + /// + /// + /// + /// + public static Tensor[] unique(Tensor x, TF_DataType out_idx = TF_DataType.TF_INT32, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Unique", name) { args = new object[] { x }, attrs = new Dictionary() { ["out_idx"] = out_idx } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return unique_eager_fallback(x, out_idx: out_idx, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + keywords["out_idx"] = out_idx; + var _op = tf.OpDefLib._apply_op_helper("Unique", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "out_idx", _op._get_attr_type("out_idx") }; + _execute.record_gradient("Unique", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] unique_eager_fallback(Tensor x, TF_DataType out_idx, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x }; + object[] _attrs = new object[] { "T", x.dtype, "out_idx", out_idx }; + var _result = _execute.execute("Unique", 2, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Unique", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// Finds unique elements along an axis of a tensor. + /// + /// + /// + /// This operation either returns a tensor `y` containing unique elements + /// along the `axis` of a tensor. The returned unique elements is sorted + /// in the same order as they occur along `axis` in `x`. + /// This operation also returns a tensor `idx` that is the same size as + /// the number of the elements in `x` along the `axis` dimension. It + /// contains the index in the unique output `y`. + /// In other words, for an `1-D` tensor `x` with `axis = None: + /// + /// `y[idx[i]] = x[i] for i in [0, 1,...,rank(x) - 1]` + /// + /// For example: + /// + /// ``` + /// # tensor 'x' is [1, 1, 2, 4, 4, 4, 7, 8, 8] + /// y, idx = unique(x) + /// y ==> [1, 2, 4, 7, 8] + /// idx ==> [0, 0, 1, 2, 2, 2, 3, 4, 4] + /// ``` + /// + /// For an `2-D` tensor `x` with `axis = 0`: + /// + /// ``` + /// # tensor 'x' is [[1, 0, 0], + /// # [1, 0, 0], + /// # [2, 0, 0]] + /// y, idx = unique(x, axis=0) + /// y ==> [[1, 0, 0], + /// [2, 0, 0]] + /// idx ==> [0, 0, 1] + /// ``` + /// + /// For an `2-D` tensor `x` with `axis = 1`: + /// + /// ``` + /// # tensor 'x' is [[1, 0, 0], + /// # [1, 0, 0], + /// # [2, 0, 0]] + /// y, idx = unique(x, axis=1) + /// y ==> [[1, 0], + /// [1, 0], + /// [2, 0]] + /// idx ==> [0, 1, 1] + /// ``` + /// + /// + /// + /// + /// + /// + public static Tensor[] unique_v2(Tensor x, Tensor axis, TF_DataType out_idx = TF_DataType.TF_INT32, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "UniqueV2", name) { args = new object[] { x, axis }, attrs = new Dictionary() { ["out_idx"] = out_idx } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return unique_v2_eager_fallback(x, axis, out_idx: out_idx, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + keywords["axis"] = axis; + keywords["out_idx"] = out_idx; + var _op = tf.OpDefLib._apply_op_helper("UniqueV2", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "Taxis", _op._get_attr_type("Taxis"), "out_idx", _op._get_attr_type("out_idx") }; + _execute.record_gradient("UniqueV2", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] unique_v2_eager_fallback(Tensor x, Tensor axis, TF_DataType out_idx, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x, axis }; + object[] _attrs = new object[] { "T", x.dtype, "Taxis", axis.dtype, "out_idx", out_idx }; + var _result = _execute.execute("UniqueV2", 2, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("UniqueV2", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// Finds unique elements in a 1-D tensor. + /// + /// + /// + /// This operation returns a tensor `y` containing all of the unique elements of `x` + /// sorted in the same order that they occur in `x`. This operation also returns a + /// tensor `idx` the same size as `x` that contains the index of each value of `x` + /// in the unique output `y`. Finally, it returns a third tensor `count` that + /// contains the count of each element of `y` in `x`. In other words: + /// + /// `y[idx[i]] = x[i] for i in [0, 1,...,rank(x) - 1]` + /// + /// For example: + /// + /// ``` + /// # tensor 'x' is [1, 1, 2, 4, 4, 4, 7, 8, 8] + /// y, idx, count = unique_with_counts(x) + /// y ==> [1, 2, 4, 7, 8] + /// idx ==> [0, 0, 1, 2, 2, 2, 3, 4, 4] + /// count ==> [2, 1, 3, 1, 2] + /// ``` + /// + /// + /// + /// + /// + public static Tensor[] unique_with_counts(Tensor x, TF_DataType out_idx = TF_DataType.TF_INT32, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "UniqueWithCounts", name) { args = new object[] { x }, attrs = new Dictionary() { ["out_idx"] = out_idx } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return unique_with_counts_eager_fallback(x, out_idx: out_idx, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + keywords["out_idx"] = out_idx; + var _op = tf.OpDefLib._apply_op_helper("UniqueWithCounts", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "out_idx", _op._get_attr_type("out_idx") }; + _execute.record_gradient("UniqueWithCounts", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] unique_with_counts_eager_fallback(Tensor x, TF_DataType out_idx, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x }; + object[] _attrs = new object[] { "T", x.dtype, "out_idx", out_idx }; + var _result = _execute.execute("UniqueWithCounts", 3, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("UniqueWithCounts", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// Finds unique elements along an axis of a tensor. + /// + /// + /// + /// This operation either returns a tensor `y` containing unique elements + /// along the `axis` of a tensor. The returned unique elements is sorted + /// in the same order as they occur along `axis` in `x`. + /// This operation also returns a tensor `idx` and a tensor `count` + /// that are the same size as the number of the elements in `x` along the + /// `axis` dimension. The `idx` contains the index in the unique output `y` + /// and the `count` contains the count in the unique output `y`. + /// In other words, for an `1-D` tensor `x` with `axis = None: + /// + /// `y[idx[i]] = x[i] for i in [0, 1,...,rank(x) - 1]` + /// + /// For example: + /// + /// ``` + /// x = tf.constant([1, 1, 2, 4, 4, 4, 7, 8, 8]) + /// y, idx, count = UniqueWithCountsV2(x, axis = [0]) + /// y ==> [1, 2, 4, 7, 8] + /// idx ==> [0, 0, 1, 2, 2, 2, 3, 4, 4] + /// count ==> [2, 1, 3, 1, 2] + /// ``` + /// + /// For a `2-D` tensor `x` with `axis = 0`: + /// + /// ``` + /// x = tf.constant([[1, 0, 0], + /// [1, 0, 0], + /// [2, 0, 0]]) + /// y, idx, count = UniqueWithCountsV2(x, axis=[0]) + /// y ==> [[1, 0, 0], + /// [2, 0, 0]] + /// idx ==> [0, 0, 1] + /// count ==> [2, 1] + /// ``` + /// + /// For a `2-D` tensor `x` with `axis = 1`: + /// + /// ``` + /// x = tf.constant([[1, 0, 0], + /// [1, 0, 0], + /// [2, 0, 0]]) + /// y, idx, count = UniqueWithCountsV2(x, axis=[1]) + /// y ==> [[1, 0], + /// [1, 0], + /// [2, 0]] + /// idx ==> [0, 1, 1] + /// count ==> [1, 2] + /// ``` + /// + /// + /// + /// + /// + /// + public static Tensor[] unique_with_counts_v2(Tensor x, Tensor axis, TF_DataType out_idx = TF_DataType.TF_INT32, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "UniqueWithCountsV2", name) { args = new object[] { x, axis }, attrs = new Dictionary() { ["out_idx"] = out_idx } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return unique_with_counts_v2_eager_fallback(x, axis, out_idx: out_idx, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + keywords["axis"] = axis; + keywords["out_idx"] = out_idx; + var _op = tf.OpDefLib._apply_op_helper("UniqueWithCountsV2", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "Taxis", _op._get_attr_type("Taxis"), "out_idx", _op._get_attr_type("out_idx") }; + _execute.record_gradient("UniqueWithCountsV2", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] unique_with_counts_v2_eager_fallback(Tensor x, Tensor axis, TF_DataType out_idx, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x, axis }; + object[] _attrs = new object[] { "T", x.dtype, "Taxis", axis.dtype, "out_idx", out_idx }; + var _result = _execute.execute("UniqueWithCountsV2", 3, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("UniqueWithCountsV2", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// Unpacks a given dimension of a rank-`R` tensor into `num` rank-`(R-1)` tensors. + /// + /// + /// + /// Unpacks `num` tensors from `value` by chipping it along the `axis` dimension. + /// For example, given a tensor of shape `(A, B, C, D)`; + /// + /// If `axis == 0` then the i'th tensor in `output` is the slice `value[i, :, :, :]` + /// and each tensor in `output` will have shape `(B, C, D)`. (Note that the + /// dimension unpacked along is gone, unlike `split`). + /// + /// If `axis == 1` then the i'th tensor in `output` is the slice `value[:, i, :, :]` + /// and each tensor in `output` will have shape `(A, C, D)`. + /// Etc. + /// + /// This is the opposite of `pack`. + /// + /// + /// + /// + /// + /// + /// Dimension along which to unpack. Negative values wrap around, so the + /// valid range is `[-R, R)`. + /// + /// + /// + public static Tensor[] unpack(Tensor value, int num = 0, int axis = 0, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Unpack", name) { args = new object[] { value }, attrs = new Dictionary() { ["num"] = num, ["axis"] = axis } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return unpack_eager_fallback(value, num: num, axis: axis, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["value"] = value; + keywords["num"] = num; + keywords["axis"] = axis; + var _op = tf.OpDefLib._apply_op_helper("Unpack", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "num", _op._get_attr_int("num"), "T", _op._get_attr_type("T"), "axis", _op._get_attr_int("axis") }; + _execute.record_gradient("Unpack", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] unpack_eager_fallback(Tensor value, int num, int axis, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { value }; + object[] _attrs = new object[] { "num", num, "T", value.dtype, "axis", axis }; + var _result = _execute.execute("Unpack", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Unpack", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// Converts an array of flat indices into a tuple of coordinate arrays. + /// + /// + /// + /// + /// Example: + /// + /// ``` + /// y = tf.unravel_index(indices=[2, 5, 7], dims=[3, 3]) + /// # 'dims' represent a hypothetical (3, 3) tensor of indices: + /// # [[0, 1, *2*], + /// # [3, 4, *5*], + /// # [6, *7*, 8]] + /// # For each entry from 'indices', this operation returns + /// # its coordinates (marked with '*'), such as + /// # 2 ==> (0, 2) + /// # 5 ==> (1, 2) + /// # 7 ==> (2, 1) + /// y ==> [[0, 1, 2], [2, 2, 1]] + /// ``` + /// + /// @compatibility(numpy) + /// Equivalent to np.unravel_index + /// @end_compatibility + /// + /// + /// + /// + /// + public static Tensor unravel_index(Tensor indices, Tensor dims, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "UnravelIndex", name) { args = new object[] { indices, dims }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return unravel_index_eager_fallback(indices, dims, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["indices"] = indices; + keywords["dims"] = dims; + var _op = tf.OpDefLib._apply_op_helper("UnravelIndex", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "Tidx", _op._get_attr_type("Tidx") }; + _execute.record_gradient("UnravelIndex", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor unravel_index_eager_fallback(Tensor indices, Tensor dims, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { indices, dims }; + object[] _attrs = new object[] { "Tidx", indices.dtype }; + var _result = _execute.execute("UnravelIndex", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("UnravelIndex", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Applies upper_bound(sorted_search_values, values) along each row. + /// + /// + /// + /// Each set of rows with the same index in (sorted_inputs, values) is treated + /// independently. The resulting row is the equivalent of calling + /// `np.searchsorted(sorted_inputs, values, side='right')`. + /// + /// The result is not a global index to the entire + /// `Tensor`, but rather just the index in the last dimension. + /// + /// A 2-D example: + /// sorted_sequence = [[0, 3, 9, 9, 10], + /// [1, 2, 3, 4, 5]] + /// values = [[2, 4, 9], + /// [0, 2, 6]] + /// + /// result = UpperBound(sorted_sequence, values) + /// + /// result == [[1, 2, 4], + /// [0, 2, 5]] + /// + /// + /// + /// + /// + /// + public static Tensor upper_bound(Tensor sorted_inputs, Tensor values, TF_DataType out_type = TF_DataType.TF_INT32, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "UpperBound", name) { args = new object[] { sorted_inputs, values }, attrs = new Dictionary() { ["out_type"] = out_type } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return upper_bound_eager_fallback(sorted_inputs, values, out_type: out_type, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["sorted_inputs"] = sorted_inputs; + keywords["values"] = values; + keywords["out_type"] = out_type; + var _op = tf.OpDefLib._apply_op_helper("UpperBound", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "out_type", _op._get_attr_type("out_type") }; + _execute.record_gradient("UpperBound", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor upper_bound_eager_fallback(Tensor sorted_inputs, Tensor values, TF_DataType out_type, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { sorted_inputs, values }; + object[] _attrs = new object[] { "T", sorted_inputs.dtype, "out_type", out_type }; + var _result = _execute.execute("UpperBound", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("UpperBound", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns locations of nonzero / true values in a tensor. + /// + /// + /// + /// This operation returns the coordinates of true elements in `input`. The + /// coordinates are returned in a 2-D tensor where the first dimension (rows) + /// represents the number of true elements, and the second dimension (columns) + /// represents the coordinates of the true elements. Keep in mind, the shape of + /// the output tensor can vary depending on how many true values there are in + /// `input`. Indices are output in row-major order. + /// + /// For example: + /// + /// ``` + /// # 'input' tensor is [[True, False] + /// # [True, False]] + /// # 'input' has two true values, so output has two coordinates. + /// # 'input' has rank of 2, so coordinates have two indices. + /// where(input) ==> [[0, 0], + /// [1, 0]] + /// + /// # `input` tensor is [[[True, False] + /// # [True, False]] + /// # [[False, True] + /// # [False, True]] + /// # [[False, False] + /// # [False, True]]] + /// # 'input' has 5 true values, so output has 5 coordinates. + /// # 'input' has rank of 3, so coordinates have three indices. + /// where(input) ==> [[0, 0, 0], + /// [0, 1, 0], + /// [1, 0, 1], + /// [1, 1, 1], + /// [2, 1, 1]] + /// + /// # `input` tensor is [[[1.5, 0.0] + /// # [-0.5, 0.0]] + /// # [[0.0, 0.25] + /// # [0.0, 0.75]] + /// # [[0.0, 0.0] + /// # [0.0, 0.01]]] + /// # 'input' has 5 nonzero values, so output has 5 coordinates. + /// # 'input' has rank of 3, so coordinates have three indices. + /// where(input) ==> [[0, 0, 0], + /// [0, 1, 0], + /// [1, 0, 1], + /// [1, 1, 1], + /// [2, 1, 1]] + /// + /// # `input` tensor is [[[1.5 + 0.0j, 0.0 + 0.0j] + /// # [0.0 + 0.5j, 0.0 + 0.0j]] + /// # [[0.0 + 0.0j, 0.25 + 1.5j] + /// # [0.0 + 0.0j, 0.75 + 0.0j]] + /// # [[0.0 + 0.0j, 0.0 + 0.0j] + /// # [0.0 + 0.0j, 0.01 + 0.0j]]] + /// # 'input' has 5 nonzero magnitude values, so output has 5 coordinates. + /// # 'input' has rank of 3, so coordinates have three indices. + /// where(input) ==> [[0, 0, 0], + /// [0, 1, 0], + /// [1, 0, 1], + /// [1, 1, 1], + /// [2, 1, 1]] + /// ``` + /// + /// + /// + /// + public static Tensor where(Tensor input, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Where", name) { args = new object[] { input }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return where_eager_fallback(input, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + var _op = tf.OpDefLib._apply_op_helper("Where", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Where", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor where_eager_fallback(Tensor input, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input }; + object[] _attrs = new object[] { "T", input.dtype }; + var _result = _execute.execute("Where", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Where", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns a tensor of zeros with the same shape and type as x. + /// + /// + /// + public static Tensor zeros_like(Tensor x, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "ZerosLike", name) { args = new object[] { x }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return zeros_like_eager_fallback(x, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + var _op = tf.OpDefLib._apply_op_helper("ZerosLike", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("ZerosLike", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor zeros_like_eager_fallback(Tensor x, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("ZerosLike", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("ZerosLike", _inputs_flat, _attrs, _result); + } + return _result[0]; } } diff --git a/src/TensorFlowNET.Core/Operations/gen_functional_ops.cs b/src/TensorFlowNET.Core/Operations/gen_functional_ops.cs index bb84ac390..6ec426f58 100644 --- a/src/TensorFlowNET.Core/Operations/gen_functional_ops.cs +++ b/src/TensorFlowNET.Core/Operations/gen_functional_ops.cs @@ -1,128 +1,1089 @@ -using System; -using System.Collections.Generic; -using System.Text; -using System.Xml.Linq; -using Tensorflow.Contexts; +/*Wrappers around TensorFlow ops. This file is MACHINE GENERATED! Do not edit.*/ + using Tensorflow.Eager; -using Tensorflow.Functions; +using Tensorflow.Contexts; +using Tensorflow.Exceptions; using static Tensorflow.Binding; -namespace Tensorflow.Operations +namespace Tensorflow; + +public static class gen_functional_ops { - public class gen_functional_ops + /// + /// An n-way switch statement which calls a single branch function. + /// + /// + /// + /// An n-way switch statement, implementing the following: + /// ``` + /// switch (branch_index) { + /// case 0: + /// output = branches[0](input); + /// break; + /// case 1: + /// output = branches[1](input); + /// break; + /// ... + /// case [[nbranches-1]]: + /// default: + /// output = branches[nbranches-1](input); + /// break; + /// } + /// ``` + /// + /// + /// + /// + /// + /// A list of output types. + /// + /// + /// + /// A list of functions each of which takes 'inputs' and returns a list of + /// tensors, whose types are the same as what every other branch returns. + /// + /// + /// + /// + public static Tensor[] _case(Tensor branch_index, Tensors input, TF_DataType[] Tout, object[] branches, Shape[] output_shapes, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Case", name) { args = new object[] { branch_index, input }, attrs = new Dictionary() { ["Tout"] = Tout, ["branches"] = branches, ["output_shapes"] = output_shapes } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return case_eager_fallback(branch_index, input, Tout: Tout, branches: branches, output_shapes: output_shapes, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["branch_index"] = branch_index; + keywords["input"] = input; + keywords["Tout"] = Tout; + keywords["branches"] = branches; + keywords["output_shapes"] = output_shapes; + var _op = tf.OpDefLib._apply_op_helper("Case", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "Tin", _op.get_attr("Tin"), "Tout", _op.get_attr("Tout"), "branches", _op.get_attr("branches"), "output_shapes", _op.get_attr("output_shapes") }; + _execute.record_gradient("Case", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] case_eager_fallback(Tensor branch_index, Tensor input, TF_DataType[] Tout, object[] branches, Shape[] output_shapes, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { branch_index, input }; + object[] _attrs = new object[] { "branches", branches, "output_shapes", output_shapes }; + var _result = _execute.execute("Case", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Case", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// Return the index of device the op runs. + /// + /// + /// + /// Given a list of device names, this operation returns the index of the device + /// this op runs. The length of the list is returned in two cases: + /// (1) Device does not exist in the given device list. + /// (2) It is in XLA compilation. + /// + /// + /// + /// + public static Tensor device_index(string[] device_names, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "DeviceIndex", name) { args = new object[] { }, attrs = new Dictionary() { ["device_names"] = device_names } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return device_index_eager_fallback(device_names: device_names, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["device_names"] = device_names; + var _op = tf.OpDefLib._apply_op_helper("DeviceIndex", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "device_names", _op.get_attr("device_names") }; + _execute.record_gradient("DeviceIndex", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor device_index_eager_fallback(string[] device_names, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { }; + object[] _attrs = new object[] { "device_names", device_names }; + var _result = _execute.execute("DeviceIndex", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("DeviceIndex", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// ~~%~~ This op is used as a placeholder in If branch functions. It doesn't provide a~~%~~ valid output when run, so must either be removed (e.g. replaced with a~~%~~ function input) or guaranteed not to be used (e.g. if mirroring an~~%~~ intermediate output needed for the gradient computation of the other branch).~~%~~ + /// + /// + /// The type of the output. + /// + /// + /// + /// The purported shape of the output. This is only used for shape inference; + /// the output will not necessarily have this shape. Can be a partial shape. + /// + /// + /// + public static Tensor fake_param(TF_DataType dtype, Shape shape, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "FakeParam", name) { args = new object[] { }, attrs = new Dictionary() { ["dtype"] = dtype, ["shape"] = shape } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return fake_param_eager_fallback(dtype: dtype, shape: shape, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["dtype"] = dtype; + keywords["shape"] = shape; + var _op = tf.OpDefLib._apply_op_helper("FakeParam", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "dtype", _op._get_attr_type("dtype"), "shape", _op.get_attr("shape") }; + _execute.record_gradient("FakeParam", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor fake_param_eager_fallback(TF_DataType dtype, Shape shape, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { }; + object[] _attrs = new object[] { "dtype", dtype, "shape", shape }; + var _result = _execute.execute("FakeParam", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("FakeParam", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Applies a for loop. + /// + /// + /// + /// ```python + /// output = input; + /// for i in range(start, limit, delta) + /// output = body(i, output); + /// ``` + /// + /// + /// + /// + /// + /// + /// + /// + /// A function that takes a list of tensors (int32, T) and returns another + /// list of tensors (T). + /// + /// + /// + public static Tensor[] _for(Tensor start, Tensor limit, Tensor delta, Tensors input, object body, string? name = null) { - public static Tensor[] partitioned_call(Tensors args, TF_DataType[] tout, EagerDefinedFunction f, - string config = "", string config_proto = "", string executor_type = "", string name = null) + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) { - var ctx = tf.Context; - if (ctx.executing_eagerly()) + try { - try - { - return tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo("PartitionedCall", name, - args, tout, f, config, config_proto, executor_type)); - } - catch (Exception) - { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "For", name) { args = new object[] { start, limit, delta, input }, attrs = new Dictionary() { ["body"] = body } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return for_eager_fallback(start, limit, delta, input, body: body, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["start"] = start; + keywords["limit"] = limit; + keywords["delta"] = delta; + keywords["input"] = input; + keywords["body"] = body; + var _op = tf.OpDefLib._apply_op_helper("For", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op.get_attr("T"), "body", _op.get_attr("body") }; + _execute.record_gradient("For", _op.inputs, _attrs, _result); + } + return _result; + } - } + public static Tensor[] for_eager_fallback(Tensor start, Tensor limit, Tensor delta, Tensor input, object body, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { start, limit, delta, input }; + object[] _attrs = new object[] { "body", body }; + var _result = _execute.execute("For", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("For", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// output = cond ? then_branch(input) : else_branch(input) + /// + /// + /// + /// + /// A list of output types. + /// + /// + /// + /// A function that takes 'inputs' and returns a list of tensors, whose + /// types are the same as what else_branch returns. + /// + /// + /// + /// + /// A function that takes 'inputs' and returns a list of tensors, whose + /// types are the same as what then_branch returns. + /// + /// + /// + /// + public static Tensor[] _if(Tensor cond, Tensors input, TF_DataType[] Tout, object then_branch, object else_branch, Shape[] output_shapes, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "If", name) { args = new object[] { cond, input }, attrs = new Dictionary() { ["Tout"] = Tout, ["then_branch"] = then_branch, ["else_branch"] = else_branch, ["output_shapes"] = output_shapes } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return if_eager_fallback(cond, input, Tout: Tout, then_branch: then_branch, else_branch: else_branch, output_shapes: output_shapes, name: name, ctx: _ctx); } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["cond"] = cond; + keywords["input"] = input; + keywords["Tout"] = Tout; + keywords["then_branch"] = then_branch; + keywords["else_branch"] = else_branch; + keywords["output_shapes"] = output_shapes; + var _op = tf.OpDefLib._apply_op_helper("If", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "Tcond", _op._get_attr_type("Tcond"), "Tin", _op.get_attr("Tin"), "Tout", _op.get_attr("Tout"), "then_branch", _op.get_attr("then_branch"), "else_branch", _op.get_attr("else_branch"), "output_shapes", _op.get_attr("output_shapes") }; + _execute.record_gradient("If", _op.inputs, _attrs, _result); + } + return _result; + } - if (config is null) + public static Tensor[] if_eager_fallback(Tensor cond, Tensor input, TF_DataType[] Tout, object then_branch, object else_branch, Shape[] output_shapes, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { cond, input }; + object[] _attrs = new object[] { "Tcond", cond.dtype, "then_branch", then_branch, "else_branch", else_branch, "output_shapes", output_shapes }; + var _result = _execute.execute("If", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("If", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// returns `f(inputs)`, where `f`'s body is placed and partitioned. + /// + /// + /// + /// Asynchronously executes a function, potentially across multiple devices but + /// within a single process. The kernel places and partitions a given function's + /// underlying graph, and executes each of the partitioned subgraphs as a function. + /// + /// + /// + /// + /// A list of output types. + /// + /// + /// + /// A function that takes 'args', a list of tensors, and returns 'output', + /// another list of tensors. Input and output types are specified by 'Tin' + /// and 'Tout'. The function body of f will be placed and partitioned across + /// devices, setting this op apart from the regular Call op. + /// + /// + /// + /// + /// + /// + public static Tensor[] partitioned_call(Tensors args, TF_DataType[] Tout, object f, string config = "", string config_proto = "", string executor_type = "", string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "PartitionedCall", name) { args = new object[] { args }, attrs = new Dictionary() { ["Tout"] = Tout, ["f"] = f, ["config"] = config, ["config_proto"] = config_proto, ["executor_type"] = executor_type } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return partitioned_call_eager_fallback(args, Tout: Tout, f: f, config: config, config_proto: config_proto, executor_type: executor_type, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + if (config is null) + { + config = ""; + } + if (config_proto is null) + { + config_proto = ""; + } + if (executor_type is null) + { + executor_type = ""; + } + Dictionary keywords = new(); + keywords["args"] = args; + keywords["Tout"] = Tout; + keywords["f"] = f; + keywords["config"] = config; + keywords["config_proto"] = config_proto; + keywords["executor_type"] = executor_type; + var _op = tf.OpDefLib._apply_op_helper("PartitionedCall", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "Tin", _op.get_attr("Tin"), "Tout", _op.get_attr("Tout"), "f", _op.get_attr("f"), "config", _op.get_attr("config"), "config_proto", _op.get_attr("config_proto"), "executor_type", _op.get_attr("executor_type") }; + _execute.record_gradient("PartitionedCall", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] partitioned_call_eager_fallback(Tensor args, TF_DataType[] Tout, object f, string config, string config_proto, string executor_type, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { args }; + object[] _attrs = new object[] { "f", f, "config", config, "config_proto", config_proto, "executor_type", executor_type }; + var _result = _execute.execute("PartitionedCall", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("PartitionedCall", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// Runs function `f` on a remote device indicated by `target`. + /// + /// + /// + /// + /// + /// The type list for the return values. + /// + /// + /// + /// + /// The function to run remotely. + /// + /// + /// + public static Tensor[] remote_call(Tensor target, Tensors args, TF_DataType[] Tout, object f, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "RemoteCall", name) { args = new object[] { target, args }, attrs = new Dictionary() { ["Tout"] = Tout, ["f"] = f } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) { - config = ""; + throw ex; } - if (config_proto is null) + catch (Exception) { - config_proto = ""; } - if (executor_type is null) + try { - executor_type = ""; + return remote_call_eager_fallback(target, args, Tout: Tout, f: f, name: name, ctx: _ctx); } - Dictionary kwargs = new(); - kwargs["args"] = args; - kwargs["Tout"] = tout; - kwargs["f"] = f; - kwargs["config"] = config; - kwargs["config_proto"] = config_proto; - kwargs["executor_type"] = executor_type; - var output = tf.OpDefLib._apply_op_helper("PartitionedCall", - name, kwargs); - var result = output.outputs; - if (execute.must_record_gradient()) + catch (Exception) { - throw new NotImplementedException(); } - return result; } + Dictionary keywords = new(); + keywords["target"] = target; + keywords["args"] = args; + keywords["Tout"] = Tout; + keywords["f"] = f; + var _op = tf.OpDefLib._apply_op_helper("RemoteCall", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "Tin", _op.get_attr("Tin"), "Tout", _op.get_attr("Tout"), "f", _op.get_attr("f") }; + _execute.record_gradient("RemoteCall", _op.inputs, _attrs, _result); + } + return _result; + } - public static Tensor[] partitioned_call_eager_fallback(Tensors args, TF_DataType[] tout, EagerDefinedFunction f, - string config, string config_proto, string executor_type, string name, Context ctx) + public static Tensor[] remote_call_eager_fallback(Tensor target, Tensor args, TF_DataType[] Tout, object f, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { target, args }; + object[] _attrs = new object[] { "f", f }; + var _result = _execute.execute("RemoteCall", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) { - // TODO(Rinne): implement it. - throw new NotImplementedException(); - if(config is null) + _execute.record_gradient("RemoteCall", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// returns `f(inputs)`, where `f`'s body is placed and partitioned. + /// + /// + /// + /// A list of output types. + /// + /// + /// + /// A function that takes 'args', a list of tensors, and returns 'output', + /// another list of tensors. Input and output types are specified by 'Tin' + /// and 'Tout'. The function body of f will be placed and partitioned across + /// devices, setting this op apart from the regular Call op. This op is + /// stateful. + /// + /// + /// + /// + /// + /// + public static Tensor[] stateful_partitioned_call(Tensors args, TF_DataType[] Tout, object f, string config = "", string config_proto = "", string executor_type = "", string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try { - config = ""; + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "StatefulPartitionedCall", name) { args = new object[] { args }, attrs = new Dictionary() { ["Tout"] = Tout, ["f"] = f, ["config"] = config, ["config_proto"] = config_proto, ["executor_type"] = executor_type } }); + return _fast_path_result; } - if(config_proto is null) + catch (NotOkStatusException ex) { - config_proto = ""; + throw ex; } - if(executor_type is null) + catch (Exception) { - executor_type = ""; } - object[] attrs = new object[] + try { + return stateful_partitioned_call_eager_fallback(args, Tout: Tout, f: f, config: config, config_proto: config_proto, executor_type: executor_type, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + if (config is null) + { + config = ""; + } + if (config_proto is null) + { + config_proto = ""; + } + if (executor_type is null) + { + executor_type = ""; + } + Dictionary keywords = new(); + keywords["args"] = args; + keywords["Tout"] = Tout; + keywords["f"] = f; + keywords["config"] = config; + keywords["config_proto"] = config_proto; + keywords["executor_type"] = executor_type; + var _op = tf.OpDefLib._apply_op_helper("StatefulPartitionedCall", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "Tin", _op.get_attr("Tin"), "Tout", _op.get_attr("Tout"), "f", _op.get_attr("f"), "config", _op.get_attr("config"), "config_proto", _op.get_attr("config_proto"), "executor_type", _op.get_attr("executor_type") }; + _execute.record_gradient("StatefulPartitionedCall", _op.inputs, _attrs, _result); + } + return _result; + } - }; + public static Tensor[] stateful_partitioned_call_eager_fallback(Tensor args, TF_DataType[] Tout, object f, string config, string config_proto, string executor_type, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { args }; + object[] _attrs = new object[] { "f", f, "config", config, "config_proto", config_proto, "executor_type", executor_type }; + var _result = _execute.execute("StatefulPartitionedCall", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("StatefulPartitionedCall", _inputs_flat, _attrs, _result); } + return _result; + } + /// + /// An n-way switch statement which calls a single branch function. + /// + /// + /// + /// An n-way switch statement, implementing the following: + /// ``` + /// switch (branch_index) { + /// case 0: + /// output = branches[0](input); + /// break; + /// case 1: + /// output = branches[1](input); + /// break; + /// ... + /// case [[nbranches-1]]: + /// default: + /// output = branches[nbranches-1](input); + /// break; + /// } + /// ``` + /// + /// This should only be used when the none of branches has stateful ops. + /// + /// + /// + /// + /// + /// A list of output types. + /// + /// + /// + /// A list of functions each of which takes 'inputs' and returns a list of + /// tensors, whose types are the same as what every other branch returns. + /// + /// + /// + /// + public static Tensor[] stateless_case(Tensor branch_index, Tensors input, TF_DataType[] Tout, object[] branches, Shape[] output_shapes, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "StatelessCase", name) { args = new object[] { branch_index, input }, attrs = new Dictionary() { ["Tout"] = Tout, ["branches"] = branches, ["output_shapes"] = output_shapes } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return stateless_case_eager_fallback(branch_index, input, Tout: Tout, branches: branches, output_shapes: output_shapes, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["branch_index"] = branch_index; + keywords["input"] = input; + keywords["Tout"] = Tout; + keywords["branches"] = branches; + keywords["output_shapes"] = output_shapes; + var _op = tf.OpDefLib._apply_op_helper("StatelessCase", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "Tin", _op.get_attr("Tin"), "Tout", _op.get_attr("Tout"), "branches", _op.get_attr("branches"), "output_shapes", _op.get_attr("output_shapes") }; + _execute.record_gradient("StatelessCase", _op.inputs, _attrs, _result); + } + return _result; + } - public static Tensor[] symbolic_gradient(Tensor[] input, TF_DataType[] Tout, NameAttrList f, string name = null) + public static Tensor[] stateless_case_eager_fallback(Tensor branch_index, Tensor input, TF_DataType[] Tout, object[] branches, Shape[] output_shapes, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { branch_index, input }; + object[] _attrs = new object[] { "branches", branches, "output_shapes", output_shapes }; + var _result = _execute.execute("StatelessCase", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) { - var ctx = tf.Context; - if (ctx.executing_eagerly()) + _execute.record_gradient("StatelessCase", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// output = cond ? then_branch(input) : else_branch(input) + /// + /// + /// + /// + /// A list of output types. + /// + /// + /// + /// A function that takes 'inputs' and returns a list of tensors, whose + /// types are the same as what else_branch returns. + /// + /// + /// + /// + /// A function that takes 'inputs' and returns a list of tensors, whose + /// types are the same as what then_branch returns. + /// + /// + /// + /// + public static Tensor[] stateless_if(Tensor cond, Tensors input, TF_DataType[] Tout, object then_branch, object else_branch, Shape[] output_shapes, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "StatelessIf", name) { args = new object[] { cond, input }, attrs = new Dictionary() { ["Tout"] = Tout, ["then_branch"] = then_branch, ["else_branch"] = else_branch, ["output_shapes"] = output_shapes } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try { - try - { - var _result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo( - "SymbolicGradient", name, input, Tout, f)); - return _result; - } - catch (Exception) - { + return stateless_if_eager_fallback(cond, input, Tout: Tout, then_branch: then_branch, else_branch: else_branch, output_shapes: output_shapes, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["cond"] = cond; + keywords["input"] = input; + keywords["Tout"] = Tout; + keywords["then_branch"] = then_branch; + keywords["else_branch"] = else_branch; + keywords["output_shapes"] = output_shapes; + var _op = tf.OpDefLib._apply_op_helper("StatelessIf", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "Tcond", _op._get_attr_type("Tcond"), "Tin", _op.get_attr("Tin"), "Tout", _op.get_attr("Tout"), "then_branch", _op.get_attr("then_branch"), "else_branch", _op.get_attr("else_branch"), "output_shapes", _op.get_attr("output_shapes") }; + _execute.record_gradient("StatelessIf", _op.inputs, _attrs, _result); + } + return _result; + } - } + public static Tensor[] stateless_if_eager_fallback(Tensor cond, Tensor input, TF_DataType[] Tout, object then_branch, object else_branch, Shape[] output_shapes, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { cond, input }; + object[] _attrs = new object[] { "Tcond", cond.dtype, "then_branch", then_branch, "else_branch", else_branch, "output_shapes", output_shapes }; + var _result = _execute.execute("StatelessIf", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("StatelessIf", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// output = input; While (Cond(output)) { output = Body(output) } + /// + /// + /// + /// + /// A function takes 'input' and returns a tensor. If the tensor is + /// a scalar of non-boolean, the scalar is converted to a boolean + /// according to the following rule: if the scalar is a numerical + /// value, non-zero means True and zero means False; if the scalar is + /// a string, non-empty means True and empty means False. If the + /// tensor is not a scalar, non-emptiness means True and False + /// otherwise. + /// + /// This should only be used when the while condition and body functions + /// do not have stateful ops. + /// + /// + /// + /// + /// A function that takes a list of tensors and returns another + /// list of tensors. Both lists have the same types as specified + /// by T. + /// + /// + /// + /// + /// + public static Tensor[] stateless_while(Tensors input, object cond, object body, Shape[] output_shapes, int parallel_iterations = 10, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "StatelessWhile", name) { args = new object[] { input }, attrs = new Dictionary() { ["cond"] = cond, ["body"] = body, ["output_shapes"] = output_shapes, ["parallel_iterations"] = parallel_iterations } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return stateless_while_eager_fallback(input, cond: cond, body: body, output_shapes: output_shapes, parallel_iterations: parallel_iterations, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["cond"] = cond; + keywords["body"] = body; + keywords["output_shapes"] = output_shapes; + keywords["parallel_iterations"] = parallel_iterations; + var _op = tf.OpDefLib._apply_op_helper("StatelessWhile", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op.get_attr("T"), "cond", _op.get_attr("cond"), "body", _op.get_attr("body"), "output_shapes", _op.get_attr("output_shapes"), "parallel_iterations", _op._get_attr_int("parallel_iterations") }; + _execute.record_gradient("StatelessWhile", _op.inputs, _attrs, _result); + } + return _result; + } - try - { - return symbolic_gradient_eager_fallback(input, Tout, f, name, ctx); - } - catch (Exception) - { + public static Tensor[] stateless_while_eager_fallback(Tensor input, object cond, object body, Shape[] output_shapes, int parallel_iterations, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input }; + object[] _attrs = new object[] { "cond", cond, "body", body, "output_shapes", output_shapes, "parallel_iterations", parallel_iterations }; + var _result = _execute.execute("StatelessWhile", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("StatelessWhile", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// Computes the gradient function for function f via backpropagation. + /// + /// + /// + /// + /// the type list for the input list. + /// + /// + /// + /// + /// The function we want to compute the gradient for. + /// + /// The function 'f' must be a numerical function which takes N inputs and + /// produces M outputs. Its gradient function 'g', which is computed by + /// this SymbolicGradient op is a function taking N + M inputs and + /// produces N outputs. + /// + /// I.e. if we have + /// (y1, y2, ..., y_M) = f(x1, x2, ..., x_N), + /// then, g is + /// (dL/dx1, dL/dx2, ..., dL/dx_N) = g(x1, x2, ..., x_N, + /// dL/dy1, dL/dy2, ..., dL/dy_M), + /// + /// where L is a scalar-value function of (x1, x2, ..., xN) (e.g., the + /// loss function). dL/dx_i is the partial derivative of L with respect + /// to x_i. + /// + /// (Needs some math expert to say the comment above better.) + /// + /// + /// + public static Tensor[] symbolic_gradient(Tensors input, TF_DataType[] Tout, object f, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "SymbolicGradient", name) { args = new object[] { input }, attrs = new Dictionary() { ["Tout"] = Tout, ["f"] = f } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return symbolic_gradient_eager_fallback(input, Tout: Tout, f: f, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["Tout"] = Tout; + keywords["f"] = f; + var _op = tf.OpDefLib._apply_op_helper("SymbolicGradient", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "Tin", _op.get_attr("Tin"), "Tout", _op.get_attr("Tout"), "f", _op.get_attr("f") }; + _execute.record_gradient("SymbolicGradient", _op.inputs, _attrs, _result); + } + return _result; + } - } + public static Tensor[] symbolic_gradient_eager_fallback(Tensor input, TF_DataType[] Tout, object f, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input }; + object[] _attrs = new object[] { "f", f }; + var _result = _execute.execute("SymbolicGradient", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("SymbolicGradient", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// Converts a tensor to a scalar predicate. + /// + /// + /// + /// Converts a tensor to a scalar predicate with the following rules: + /// + /// - For 0D tensors, truthiness is determined by comparing against a "zero" + /// value. For numerical types it is the obvious zero. For strings it is the + /// empty string. + /// + /// - For >0D tensors, truthiness is determined by looking at the number of + /// elements. If has zero elements, then the result is false. Otherwise the + /// result is true. + /// + /// This matches the behavior of If and While for determining if a tensor counts + /// as true/false for a branch condition. + /// + /// + /// + /// + public static Tensor to_bool(Tensor input, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "ToBool", name) { args = new object[] { input }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; } - var op = tf.OpDefLib._apply_op_helper("SymbolicGradient", name, new object[] { input, Tout, f }); - var result = op.outputs; - if (execute.must_record_gradient()) + catch (NotOkStatusException ex) { - throw new NotImplementedException(); + throw ex; } - return result; + catch (Exception) + { + } + try + { + return to_bool_eager_fallback(input, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + var _op = tf.OpDefLib._apply_op_helper("ToBool", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("ToBool", _op.inputs, _attrs, _result); } + return _result[0]; + } - public static Tensor[] symbolic_gradient_eager_fallback(Tensor[] input, TF_DataType[] Tout, NameAttrList f, string name, Context ctx) + public static Tensor to_bool_eager_fallback(Tensor input, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input }; + object[] _attrs = new object[] { "T", input.dtype }; + var _result = _execute.execute("ToBool", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) { - object[] attrs = new object[] { "Tin", input, "Tout", Tout, "f", f }; - var result = execute.executes("SymbolicGradient", Tout.Length, input, attrs, ctx, name); - if (execute.must_record_gradient()) + _execute.record_gradient("ToBool", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// output = input; While (Cond(output)) { output = Body(output) } + /// + /// + /// + /// + /// A function takes 'input' and returns a tensor. If the tensor is + /// a scalar of non-boolean, the scalar is converted to a boolean + /// according to the following rule: if the scalar is a numerical + /// value, non-zero means True and zero means False; if the scalar is + /// a string, non-empty means True and empty means False. If the + /// tensor is not a scalar, non-emptiness means True and False + /// otherwise. + /// + /// + /// + /// + /// A function that takes a list of tensors and returns another + /// list of tensors. Both lists have the same types as specified + /// by T. + /// + /// + /// + /// + /// + public static Tensor[] _while(Tensors input, object cond, object body, Shape[] output_shapes, int parallel_iterations = 10, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "While", name) { args = new object[] { input }, attrs = new Dictionary() { ["cond"] = cond, ["body"] = body, ["output_shapes"] = output_shapes, ["parallel_iterations"] = parallel_iterations } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return while_eager_fallback(input, cond: cond, body: body, output_shapes: output_shapes, parallel_iterations: parallel_iterations, name: name, ctx: _ctx); + } + catch (Exception) { - throw new NotImplementedException(); } - return result; } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["cond"] = cond; + keywords["body"] = body; + keywords["output_shapes"] = output_shapes; + keywords["parallel_iterations"] = parallel_iterations; + var _op = tf.OpDefLib._apply_op_helper("While", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op.get_attr("T"), "cond", _op.get_attr("cond"), "body", _op.get_attr("body"), "output_shapes", _op.get_attr("output_shapes"), "parallel_iterations", _op._get_attr_int("parallel_iterations") }; + _execute.record_gradient("While", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] while_eager_fallback(Tensor input, object cond, object body, Shape[] output_shapes, int parallel_iterations, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input }; + object[] _attrs = new object[] { "cond", cond, "body", body, "output_shapes", output_shapes, "parallel_iterations", parallel_iterations }; + var _result = _execute.execute("While", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("While", _inputs_flat, _attrs, _result); + } + return _result; } } diff --git a/src/TensorFlowNET.Core/Operations/gen_image_ops.cs b/src/TensorFlowNET.Core/Operations/gen_image_ops.cs index 9240b5905..cbe661ae5 100644 --- a/src/TensorFlowNET.Core/Operations/gen_image_ops.cs +++ b/src/TensorFlowNET.Core/Operations/gen_image_ops.cs @@ -16,18 +16,312 @@ limitations under the License. using System; using System.Linq; +using Tensorflow.Eager; using static Tensorflow.Binding; +using Tensorflow.Exceptions; +using Tensorflow.Contexts; +using System.Xml.Linq; +using Google.Protobuf; namespace Tensorflow { public class gen_image_ops { + public static Tensor adjust_contrastv2(Tensor images, Tensor contrast_factor, string name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "AdjustContrastv2", name) { + args = new object[] { images, contrast_factor }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return adjust_contrastv2_eager_fallback(images, contrast_factor, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["images"] = images; + keywords["contrast_factor"] = contrast_factor; + var _op = tf.OpDefLib._apply_op_helper("AdjustContrastv2", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("AdjustContrastv2", _op.inputs, _attrs, _result); + } + return _result[0]; + } + public static Tensor adjust_contrastv2(Tensor image, float contrast_factor, string name = null) + { + return adjust_contrastv2(image, tf.convert_to_tensor(contrast_factor), name: name); + } + + public static Tensor adjust_contrastv2_eager_fallback(Tensor images, Tensor contrast_factor, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { images, contrast_factor}; + object[] _attrs = new object[] { "T", images.dtype }; + var _result = _execute.execute("AdjustContrastv2", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("AdjustContrastv2", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + + public static Tensor adjust_hue(Tensor images, Tensor delta, string name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "AdjustHue", name) { + args = new object[] { images, delta }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return adjust_hue_eager_fallback(images, delta, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["images"] = images; + keywords["delta"] = delta; + var _op = tf.OpDefLib._apply_op_helper("AdjustHue", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("AdjustHue", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor adjust_hue(Tensor images, float delta, string name = null) + => adjust_hue(images, delta, name: name); + + public static Tensor adjust_hue_eager_fallback(Tensor images, Tensor delta, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { images, delta}; + object[] _attrs = new object[] { "T", images.dtype }; + var _result = _execute.execute("AdjustHue", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("AdjustHue", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + + public static Tensor adjust_saturation(Tensor images, Tensor scale, string name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "AdjustSaturation", name) + { + args = new object[] { images, scale }, + attrs = new Dictionary() { } + }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return adjust_hue_eager_fallback(images, scale, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["images"] = images; + keywords["scale"] = scale; + var _op = tf.OpDefLib._apply_op_helper("AdjustSaturation", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("AdjustSaturation", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor adjust_saturation(Tensor images, float scale, string name = null) + => adjust_saturation(images, ops.convert_to_tensor(scale), name: name); + + public static Tensor adjust_saturation_eager_fallback(Tensor images, Tensor scale, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { images, scale }; + object[] _attrs = new object[] { "T", images.dtype }; + var _result = _execute.execute("AdjustSaturation", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("AdjustSaturation", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + public static (Tensor, Tensor, Tensor, Tensor) combined_non_max_suppression(Tensor boxes, Tensor scores, Tensor max_output_size_per_class, Tensor max_total_size, - Tensor iou_threshold, Tensor score_threshold, bool pad_per_class, bool clip_boxes) + Tensor iou_threshold, Tensor score_threshold, bool pad_per_class = false, bool clip_boxes = true, string name = null) { - throw new NotImplementedException("combined_non_max_suppression"); + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "CombinedNonMaxSuppression", name){ + args = new object[] { + boxes, scores, max_output_size_per_class, max_total_size, iou_threshold, score_threshold, + "pad_per_class", pad_per_class, "clip_boxes", clip_boxes}, + attrs = new Dictionary() { }}); + return (_fast_path_result[0], _fast_path_result[1], _fast_path_result[2], _fast_path_result[3]); + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return combined_non_max_suppression_eager_fallback( + boxes, scores, max_output_size_per_class, max_total_size, iou_threshold, + score_threshold, pad_per_class, clip_boxes, name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["boxes"] = boxes; + keywords["scores"] = scores; + keywords["max_output_size_per_class"] = max_output_size_per_class; + keywords["max_total_size"] = max_total_size; + keywords["iou_threshold"] = iou_threshold; + keywords["score_threshold"] = score_threshold; + keywords["pad_per_class"] = pad_per_class; + keywords["clip_boxes"] = clip_boxes; + + var _op = tf.OpDefLib._apply_op_helper("CombinedNonMaxSuppression", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "pad_per_class", _op._get_attr_type("pad_per_class") ,"clip_boxes", _op._get_attr_type("clip_boxes")}; + _execute.record_gradient("CombinedNonMaxSuppression", _op.inputs, _attrs, _result); + } + return (_result[0], _result[1], _result[2], _result[3]); } + public static (Tensor, Tensor, Tensor, Tensor) combined_non_max_suppression_eager_fallback(Tensor boxes, Tensor scores, Tensor max_output_size_per_class, Tensor max_total_size, + Tensor iou_threshold, Tensor score_threshold, bool pad_per_class, bool clip_boxes, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { boxes, scores, max_output_size_per_class, max_total_size, iou_threshold, score_threshold }; + object[] _attrs = new object[] { "pad_per_class", pad_per_class, "clip_boxes", clip_boxes }; + var _result = _execute.execute("CombinedNonMaxSuppression", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("CombinedNonMaxSuppression", _inputs_flat, _attrs, _result); + } + return (_result[0], _result[1], _result[2], _result[3]); + } + + public static Tensor crop_and_resize(Tensor image, Tensor boxes, Tensor box_ind, Tensor crop_size, string method = "bilinear", float extrapolation_value = 0f, string name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "CropAndResize", name) { + args = new object[] { + image, boxes, box_ind, crop_size, "method", method, "extrapolation_value", extrapolation_value }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return crop_and_resize_eager_fallback( + image, boxes, box_ind, crop_size, method: method, extrapolation_value: extrapolation_value, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["image"] = image; + keywords["boxes"] = boxes; + keywords["box_ind"] = box_ind; + keywords["crop_size"] = crop_size; + keywords["method"] = method; + keywords["extrapolation_value"] = extrapolation_value; + var _op = tf.OpDefLib._apply_op_helper("CropAndResize", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") ,"method", _op._get_attr_type("method") , + "extrapolation_value", _op.get_attr("extrapolation_value")}; + _execute.record_gradient("CropAndResize", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor crop_and_resize_eager_fallback(Tensor image, Tensor boxes, Tensor box_ind, Tensor crop_size, string method, float extrapolation_value, string name, Context ctx) + { + if (method is null) + method = "bilinear"; + //var method_cpmpat = ByteString.CopyFromUtf8(method ?? string.Empty); + //var extrapolation_value_float = (float)extrapolation_value; + + Tensor[] _inputs_flat = new Tensor[] { image, boxes, box_ind, crop_size, tf.convert_to_tensor(method), tf.convert_to_tensor(extrapolation_value) }; + object[] _attrs = new object[] { "T", image.dtype }; + var _result = _execute.execute("CropAndResize", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("CropAndResize", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + + public static Tensor convert_image_dtype(Tensor image, TF_DataType dtype, bool saturate = false, string name = null) { if (dtype == image.dtype) diff --git a/src/TensorFlowNET.Core/Operations/gen_io_ops.cs b/src/TensorFlowNET.Core/Operations/gen_io_ops.cs new file mode 100644 index 000000000..0b92ff360 --- /dev/null +++ b/src/TensorFlowNET.Core/Operations/gen_io_ops.cs @@ -0,0 +1,2096 @@ +/*Wrappers around TensorFlow ops. This file is MACHINE GENERATED! Do not edit.*/ + +using Tensorflow.Eager; +using Tensorflow.Contexts; +using Tensorflow.Exceptions; +using static Tensorflow.Binding; + +namespace Tensorflow; + +public static class gen_io_ops +{ + /// + /// A Reader that outputs fixed-length records from a file. + /// + /// + /// + /// Number of bytes in the header, defaults to 0. + /// + /// + /// + /// + /// Number of bytes in the record. + /// + /// + /// + /// + /// Number of bytes in the footer, defaults to 0. + /// + /// + /// + /// + /// Number of bytes to hop before each read. Default of 0 means using + /// record_bytes. + /// + /// + /// + /// + /// If non-empty, this reader is placed in the given container. + /// Otherwise, a default container is used. + /// + /// + /// + /// + /// If non-empty, this reader is named in the given bucket + /// with this shared_name. Otherwise, the node name is used instead. + /// + /// + /// + public static Tensor fixed_length_record_reader(int header_bytes = 0, int record_bytes = 0, int footer_bytes = 0, int hop_bytes = 0, string container = "", string shared_name = "", string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "FixedLengthRecordReader", name) { args = new object[] { }, attrs = new Dictionary() { ["header_bytes"] = header_bytes, ["record_bytes"] = record_bytes, ["footer_bytes"] = footer_bytes, ["hop_bytes"] = hop_bytes, ["container"] = container, ["shared_name"] = shared_name } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return fixed_length_record_reader_eager_fallback(header_bytes: header_bytes, record_bytes: record_bytes, footer_bytes: footer_bytes, hop_bytes: hop_bytes, container: container, shared_name: shared_name, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + if (container is null) + { + container = ""; + } + if (shared_name is null) + { + shared_name = ""; + } + Dictionary keywords = new(); + keywords["header_bytes"] = header_bytes; + keywords["record_bytes"] = record_bytes; + keywords["footer_bytes"] = footer_bytes; + keywords["hop_bytes"] = hop_bytes; + keywords["container"] = container; + keywords["shared_name"] = shared_name; + var _op = tf.OpDefLib._apply_op_helper("FixedLengthRecordReader", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "header_bytes", _op._get_attr_int("header_bytes"), "record_bytes", _op._get_attr_int("record_bytes"), "footer_bytes", _op._get_attr_int("footer_bytes"), "hop_bytes", _op._get_attr_int("hop_bytes"), "container", _op.get_attr("container"), "shared_name", _op.get_attr("shared_name") }; + _execute.record_gradient("FixedLengthRecordReader", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor fixed_length_record_reader_eager_fallback(int header_bytes, int record_bytes, int footer_bytes, int hop_bytes, string container, string shared_name, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { }; + object[] _attrs = new object[] { "header_bytes", header_bytes, "record_bytes", record_bytes, "footer_bytes", footer_bytes, "hop_bytes", hop_bytes, "container", container, "shared_name", shared_name }; + var _result = _execute.execute("FixedLengthRecordReader", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("FixedLengthRecordReader", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// A Reader that outputs fixed-length records from a file. + /// + /// + /// + /// Number of bytes in the header, defaults to 0. + /// + /// + /// + /// + /// Number of bytes in the record. + /// + /// + /// + /// + /// Number of bytes in the footer, defaults to 0. + /// + /// + /// + /// + /// Number of bytes to hop before each read. Default of 0 means using + /// record_bytes. + /// + /// + /// + /// + /// If non-empty, this reader is placed in the given container. + /// Otherwise, a default container is used. + /// + /// + /// + /// + /// If non-empty, this reader is named in the given bucket + /// with this shared_name. Otherwise, the node name is used instead. + /// + /// + /// + /// + /// The type of encoding for the file. Currently ZLIB and GZIP + /// are supported. Defaults to none. + /// + /// + /// + public static Tensor fixed_length_record_reader_v2(int header_bytes = 0, int record_bytes = 0, int footer_bytes = 0, int hop_bytes = 0, string container = "", string shared_name = "", string encoding = "", string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "FixedLengthRecordReaderV2", name) { args = new object[] { }, attrs = new Dictionary() { ["header_bytes"] = header_bytes, ["record_bytes"] = record_bytes, ["footer_bytes"] = footer_bytes, ["hop_bytes"] = hop_bytes, ["container"] = container, ["shared_name"] = shared_name, ["encoding"] = encoding } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return fixed_length_record_reader_v2_eager_fallback(header_bytes: header_bytes, record_bytes: record_bytes, footer_bytes: footer_bytes, hop_bytes: hop_bytes, container: container, shared_name: shared_name, encoding: encoding, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + if (container is null) + { + container = ""; + } + if (shared_name is null) + { + shared_name = ""; + } + if (encoding is null) + { + encoding = ""; + } + Dictionary keywords = new(); + keywords["header_bytes"] = header_bytes; + keywords["record_bytes"] = record_bytes; + keywords["footer_bytes"] = footer_bytes; + keywords["hop_bytes"] = hop_bytes; + keywords["container"] = container; + keywords["shared_name"] = shared_name; + keywords["encoding"] = encoding; + var _op = tf.OpDefLib._apply_op_helper("FixedLengthRecordReaderV2", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "header_bytes", _op._get_attr_int("header_bytes"), "record_bytes", _op._get_attr_int("record_bytes"), "footer_bytes", _op._get_attr_int("footer_bytes"), "hop_bytes", _op._get_attr_int("hop_bytes"), "container", _op.get_attr("container"), "shared_name", _op.get_attr("shared_name"), "encoding", _op.get_attr("encoding") }; + _execute.record_gradient("FixedLengthRecordReaderV2", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor fixed_length_record_reader_v2_eager_fallback(int header_bytes, int record_bytes, int footer_bytes, int hop_bytes, string container, string shared_name, string encoding, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { }; + object[] _attrs = new object[] { "header_bytes", header_bytes, "record_bytes", record_bytes, "footer_bytes", footer_bytes, "hop_bytes", hop_bytes, "container", container, "shared_name", shared_name, "encoding", encoding }; + var _result = _execute.execute("FixedLengthRecordReaderV2", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("FixedLengthRecordReaderV2", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// A Reader that outputs the queued work as both the key and value. + /// + /// + /// + /// To use, enqueue strings in a Queue. ReaderRead will take the front + /// work string and output (work, work). + /// + /// + /// + /// + /// If non-empty, this reader is placed in the given container. + /// Otherwise, a default container is used. + /// + /// + /// + /// + /// If non-empty, this reader is named in the given bucket + /// with this shared_name. Otherwise, the node name is used instead. + /// + /// + /// + public static Tensor identity_reader(string container = "", string shared_name = "", string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "IdentityReader", name) { args = new object[] { }, attrs = new Dictionary() { ["container"] = container, ["shared_name"] = shared_name } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return identity_reader_eager_fallback(container: container, shared_name: shared_name, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + if (container is null) + { + container = ""; + } + if (shared_name is null) + { + shared_name = ""; + } + Dictionary keywords = new(); + keywords["container"] = container; + keywords["shared_name"] = shared_name; + var _op = tf.OpDefLib._apply_op_helper("IdentityReader", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "container", _op.get_attr("container"), "shared_name", _op.get_attr("shared_name") }; + _execute.record_gradient("IdentityReader", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor identity_reader_eager_fallback(string container, string shared_name, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { }; + object[] _attrs = new object[] { "container", container, "shared_name", shared_name }; + var _result = _execute.execute("IdentityReader", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("IdentityReader", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// A Reader that outputs the queued work as both the key and value. + /// + /// + /// + /// To use, enqueue strings in a Queue. ReaderRead will take the front + /// work string and output (work, work). + /// + /// + /// + /// + /// If non-empty, this reader is placed in the given container. + /// Otherwise, a default container is used. + /// + /// + /// + /// + /// If non-empty, this reader is named in the given bucket + /// with this shared_name. Otherwise, the node name is used instead. + /// + /// + /// + public static Tensor identity_reader_v2(string container = "", string shared_name = "", string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "IdentityReaderV2", name) { args = new object[] { }, attrs = new Dictionary() { ["container"] = container, ["shared_name"] = shared_name } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return identity_reader_v2_eager_fallback(container: container, shared_name: shared_name, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + if (container is null) + { + container = ""; + } + if (shared_name is null) + { + shared_name = ""; + } + Dictionary keywords = new(); + keywords["container"] = container; + keywords["shared_name"] = shared_name; + var _op = tf.OpDefLib._apply_op_helper("IdentityReaderV2", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "container", _op.get_attr("container"), "shared_name", _op.get_attr("shared_name") }; + _execute.record_gradient("IdentityReaderV2", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor identity_reader_v2_eager_fallback(string container, string shared_name, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { }; + object[] _attrs = new object[] { "container", container, "shared_name", shared_name }; + var _result = _execute.execute("IdentityReaderV2", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("IdentityReaderV2", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns the set of files matching one or more glob patterns. + /// + /// + /// + /// Note that this routine only supports wildcard characters in the + /// basename portion of the pattern, not in the directory portion. + /// Note also that the order of filenames returned is deterministic. + /// + /// + /// + /// + public static Tensor matching_files(Tensor pattern, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "MatchingFiles", name) { args = new object[] { pattern }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return matching_files_eager_fallback(pattern, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["pattern"] = pattern; + var _op = tf.OpDefLib._apply_op_helper("MatchingFiles", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { }; + _execute.record_gradient("MatchingFiles", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor matching_files_eager_fallback(Tensor pattern, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { pattern }; + object[] _attrs = new object[] { }; + var _result = _execute.execute("MatchingFiles", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("MatchingFiles", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Reads and outputs the entire contents of the input filename. + /// + /// + /// + public static Tensor read_file(Tensor filename, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "ReadFile", name) { args = new object[] { filename }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return read_file_eager_fallback(filename, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["filename"] = filename; + var _op = tf.OpDefLib._apply_op_helper("ReadFile", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { }; + _execute.record_gradient("ReadFile", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor read_file_eager_fallback(Tensor filename, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { filename }; + object[] _attrs = new object[] { }; + var _result = _execute.execute("ReadFile", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("ReadFile", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns the number of records this Reader has produced. + /// + /// + /// + /// This is the same as the number of ReaderRead executions that have + /// succeeded. + /// + /// + /// + /// + public static Tensor reader_num_records_produced(Tensor reader_handle, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + throw new RuntimeError("reader_num_records_produced op does not support eager execution. Arg reader_handle is a ref."); + } + Dictionary keywords = new(); + keywords["reader_handle"] = reader_handle; + var _op = tf.OpDefLib._apply_op_helper("ReaderNumRecordsProduced", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { }; + _execute.record_gradient("ReaderNumRecordsProduced", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor reader_num_records_produced_eager_fallback(Tensor reader_handle, string name, Context ctx) + { + throw new RuntimeError($"reader_num_records_produced op does not support eager execution. Arg 'reader_handle' is a ref."); + } + /// + /// Returns the number of records this Reader has produced. + /// + /// + /// + /// This is the same as the number of ReaderRead executions that have + /// succeeded. + /// + /// + /// + /// + public static Tensor reader_num_records_produced_v2(Tensor reader_handle, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "ReaderNumRecordsProducedV2", name) { args = new object[] { reader_handle }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return reader_num_records_produced_v2_eager_fallback(reader_handle, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["reader_handle"] = reader_handle; + var _op = tf.OpDefLib._apply_op_helper("ReaderNumRecordsProducedV2", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { }; + _execute.record_gradient("ReaderNumRecordsProducedV2", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor reader_num_records_produced_v2_eager_fallback(Tensor reader_handle, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { reader_handle }; + object[] _attrs = new object[] { }; + var _result = _execute.execute("ReaderNumRecordsProducedV2", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("ReaderNumRecordsProducedV2", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns the number of work units this Reader has finished processing. + /// + /// + /// + public static Tensor reader_num_work_units_completed(Tensor reader_handle, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + throw new RuntimeError("reader_num_work_units_completed op does not support eager execution. Arg reader_handle is a ref."); + } + Dictionary keywords = new(); + keywords["reader_handle"] = reader_handle; + var _op = tf.OpDefLib._apply_op_helper("ReaderNumWorkUnitsCompleted", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { }; + _execute.record_gradient("ReaderNumWorkUnitsCompleted", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor reader_num_work_units_completed_eager_fallback(Tensor reader_handle, string name, Context ctx) + { + throw new RuntimeError($"reader_num_work_units_completed op does not support eager execution. Arg 'reader_handle' is a ref."); + } + /// + /// Returns the number of work units this Reader has finished processing. + /// + /// + /// + public static Tensor reader_num_work_units_completed_v2(Tensor reader_handle, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "ReaderNumWorkUnitsCompletedV2", name) { args = new object[] { reader_handle }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return reader_num_work_units_completed_v2_eager_fallback(reader_handle, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["reader_handle"] = reader_handle; + var _op = tf.OpDefLib._apply_op_helper("ReaderNumWorkUnitsCompletedV2", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { }; + _execute.record_gradient("ReaderNumWorkUnitsCompletedV2", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor reader_num_work_units_completed_v2_eager_fallback(Tensor reader_handle, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { reader_handle }; + object[] _attrs = new object[] { }; + var _result = _execute.execute("ReaderNumWorkUnitsCompletedV2", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("ReaderNumWorkUnitsCompletedV2", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns the next record (key, value pair) produced by a Reader. + /// + /// + /// + /// Will dequeue from the input queue if necessary (e.g. when the + /// Reader needs to start reading from a new file since it has finished + /// with the previous file). + /// + /// + /// + /// + /// + public static Tensor[] reader_read(Tensor reader_handle, Tensor queue_handle, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + throw new RuntimeError("reader_read op does not support eager execution. Arg reader_handle is a ref."); + } + Dictionary keywords = new(); + keywords["reader_handle"] = reader_handle; + keywords["queue_handle"] = queue_handle; + var _op = tf.OpDefLib._apply_op_helper("ReaderRead", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { }; + _execute.record_gradient("ReaderRead", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] reader_read_eager_fallback(Tensor reader_handle, Tensor queue_handle, string name, Context ctx) + { + throw new RuntimeError($"reader_read op does not support eager execution. Arg 'reader_handle' is a ref."); + } + /// + /// Returns up to `num_records` (key, value) pairs produced by a Reader. + /// + /// + /// + /// Will dequeue from the input queue if necessary (e.g. when the + /// Reader needs to start reading from a new file since it has finished + /// with the previous file). + /// It may return less than `num_records` even before the last batch. + /// + /// + /// + /// + /// + /// + public static Tensor[] reader_read_up_to(Tensor reader_handle, Tensor queue_handle, Tensor num_records, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + throw new RuntimeError("reader_read_up_to op does not support eager execution. Arg reader_handle is a ref."); + } + Dictionary keywords = new(); + keywords["reader_handle"] = reader_handle; + keywords["queue_handle"] = queue_handle; + keywords["num_records"] = num_records; + var _op = tf.OpDefLib._apply_op_helper("ReaderReadUpTo", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { }; + _execute.record_gradient("ReaderReadUpTo", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] reader_read_up_to_eager_fallback(Tensor reader_handle, Tensor queue_handle, Tensor num_records, string name, Context ctx) + { + throw new RuntimeError($"reader_read_up_to op does not support eager execution. Arg 'reader_handle' is a ref."); + } + /// + /// Returns up to `num_records` (key, value) pairs produced by a Reader. + /// + /// + /// + /// Will dequeue from the input queue if necessary (e.g. when the + /// Reader needs to start reading from a new file since it has finished + /// with the previous file). + /// It may return less than `num_records` even before the last batch. + /// + /// + /// + /// + /// + /// + public static Tensor[] reader_read_up_to_v2(Tensor reader_handle, Tensor queue_handle, Tensor num_records, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "ReaderReadUpToV2", name) { args = new object[] { reader_handle, queue_handle, num_records }, attrs = new Dictionary() { } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return reader_read_up_to_v2_eager_fallback(reader_handle, queue_handle, num_records, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["reader_handle"] = reader_handle; + keywords["queue_handle"] = queue_handle; + keywords["num_records"] = num_records; + var _op = tf.OpDefLib._apply_op_helper("ReaderReadUpToV2", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { }; + _execute.record_gradient("ReaderReadUpToV2", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] reader_read_up_to_v2_eager_fallback(Tensor reader_handle, Tensor queue_handle, Tensor num_records, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { reader_handle, queue_handle, num_records }; + object[] _attrs = new object[] { }; + var _result = _execute.execute("ReaderReadUpToV2", 2, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("ReaderReadUpToV2", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// Returns the next record (key, value pair) produced by a Reader. + /// + /// + /// + /// Will dequeue from the input queue if necessary (e.g. when the + /// Reader needs to start reading from a new file since it has finished + /// with the previous file). + /// + /// + /// + /// + /// + public static Tensor[] reader_read_v2(Tensor reader_handle, Tensor queue_handle, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "ReaderReadV2", name) { args = new object[] { reader_handle, queue_handle }, attrs = new Dictionary() { } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return reader_read_v2_eager_fallback(reader_handle, queue_handle, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["reader_handle"] = reader_handle; + keywords["queue_handle"] = queue_handle; + var _op = tf.OpDefLib._apply_op_helper("ReaderReadV2", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { }; + _execute.record_gradient("ReaderReadV2", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] reader_read_v2_eager_fallback(Tensor reader_handle, Tensor queue_handle, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { reader_handle, queue_handle }; + object[] _attrs = new object[] { }; + var _result = _execute.execute("ReaderReadV2", 2, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("ReaderReadV2", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// Restore a Reader to its initial clean state. + /// + /// + /// + public static Operation reader_reset(Tensor reader_handle, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + throw new RuntimeError("reader_reset op does not support eager execution. Arg reader_handle is a ref."); + } + Dictionary keywords = new(); + keywords["reader_handle"] = reader_handle; + var _op = tf.OpDefLib._apply_op_helper("ReaderReset", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { }; + _execute.record_gradient("ReaderReset", _op.inputs, _attrs, _result); + } + return _op; + } + + public static Operation reader_reset_eager_fallback(Tensor reader_handle, string name, Context ctx) + { + throw new RuntimeError($"reader_reset op does not support eager execution. Arg 'reader_handle' is a ref."); + } + /// + /// Restore a Reader to its initial clean state. + /// + /// + /// + public static Operation reader_reset_v2(Tensor reader_handle, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "ReaderResetV2", name) { args = new object[] { reader_handle }, attrs = new Dictionary() { } }); + return null; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return reader_reset_v2_eager_fallback(reader_handle, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["reader_handle"] = reader_handle; + var _op = tf.OpDefLib._apply_op_helper("ReaderResetV2", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { }; + _execute.record_gradient("ReaderResetV2", _op.inputs, _attrs, _result); + } + return _op; + } + + public static Operation reader_reset_v2_eager_fallback(Tensor reader_handle, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { reader_handle }; + object[] _attrs = new object[] { }; + var _result = _execute.execute("ReaderResetV2", 0, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("ReaderResetV2", _inputs_flat, _attrs, _result); + } + return null; + } + /// + /// Restore a reader to a previously saved state. + /// + /// + /// + /// Not all Readers support being restored, so this can produce an + /// Unimplemented error. + /// + /// + /// + /// + /// + public static Operation reader_restore_state(Tensor reader_handle, Tensor state, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + throw new RuntimeError("reader_restore_state op does not support eager execution. Arg reader_handle is a ref."); + } + Dictionary keywords = new(); + keywords["reader_handle"] = reader_handle; + keywords["state"] = state; + var _op = tf.OpDefLib._apply_op_helper("ReaderRestoreState", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { }; + _execute.record_gradient("ReaderRestoreState", _op.inputs, _attrs, _result); + } + return _op; + } + + public static Operation reader_restore_state_eager_fallback(Tensor reader_handle, Tensor state, string name, Context ctx) + { + throw new RuntimeError($"reader_restore_state op does not support eager execution. Arg 'reader_handle' is a ref."); + } + /// + /// Restore a reader to a previously saved state. + /// + /// + /// + /// Not all Readers support being restored, so this can produce an + /// Unimplemented error. + /// + /// + /// + /// + /// + public static Operation reader_restore_state_v2(Tensor reader_handle, Tensor state, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "ReaderRestoreStateV2", name) { args = new object[] { reader_handle, state }, attrs = new Dictionary() { } }); + return null; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return reader_restore_state_v2_eager_fallback(reader_handle, state, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["reader_handle"] = reader_handle; + keywords["state"] = state; + var _op = tf.OpDefLib._apply_op_helper("ReaderRestoreStateV2", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { }; + _execute.record_gradient("ReaderRestoreStateV2", _op.inputs, _attrs, _result); + } + return _op; + } + + public static Operation reader_restore_state_v2_eager_fallback(Tensor reader_handle, Tensor state, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { reader_handle, state }; + object[] _attrs = new object[] { }; + var _result = _execute.execute("ReaderRestoreStateV2", 0, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("ReaderRestoreStateV2", _inputs_flat, _attrs, _result); + } + return null; + } + /// + /// Produce a string tensor that encodes the state of a Reader. + /// + /// + /// + /// Not all Readers support being serialized, so this can produce an + /// Unimplemented error. + /// + /// + /// + /// + public static Tensor reader_serialize_state(Tensor reader_handle, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + throw new RuntimeError("reader_serialize_state op does not support eager execution. Arg reader_handle is a ref."); + } + Dictionary keywords = new(); + keywords["reader_handle"] = reader_handle; + var _op = tf.OpDefLib._apply_op_helper("ReaderSerializeState", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { }; + _execute.record_gradient("ReaderSerializeState", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor reader_serialize_state_eager_fallback(Tensor reader_handle, string name, Context ctx) + { + throw new RuntimeError($"reader_serialize_state op does not support eager execution. Arg 'reader_handle' is a ref."); + } + /// + /// Produce a string tensor that encodes the state of a Reader. + /// + /// + /// + /// Not all Readers support being serialized, so this can produce an + /// Unimplemented error. + /// + /// + /// + /// + public static Tensor reader_serialize_state_v2(Tensor reader_handle, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "ReaderSerializeStateV2", name) { args = new object[] { reader_handle }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return reader_serialize_state_v2_eager_fallback(reader_handle, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["reader_handle"] = reader_handle; + var _op = tf.OpDefLib._apply_op_helper("ReaderSerializeStateV2", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { }; + _execute.record_gradient("ReaderSerializeStateV2", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor reader_serialize_state_v2_eager_fallback(Tensor reader_handle, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { reader_handle }; + object[] _attrs = new object[] { }; + var _result = _execute.execute("ReaderSerializeStateV2", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("ReaderSerializeStateV2", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Restores a tensor from checkpoint files. + /// + /// + /// + /// Reads a tensor stored in one or several files. If there are several files (for + /// instance because a tensor was saved as slices), `file_pattern` may contain + /// wildcard symbols (`*` and `?`) in the filename portion only, not in the + /// directory portion. + /// + /// If a `file_pattern` matches several files, `preferred_shard` can be used to hint + /// in which file the requested tensor is likely to be found. This op will first + /// open the file at index `preferred_shard` in the list of matching files and try + /// to restore tensors from that file. Only if some tensors or tensor slices are + /// not found in that first file, then the Op opens all the files. Setting + /// `preferred_shard` to match the value passed as the `shard` input + /// of a matching `Save` Op may speed up Restore. This attribute only affects + /// performance, not correctness. The default value -1 means files are processed in + /// order. + /// + /// See also `RestoreSlice`. + /// + /// + /// + /// + /// + /// + /// The type of the tensor to be restored. + /// + /// + /// + /// + /// Index of file to open first if multiple files match + /// `file_pattern`. + /// + /// + /// + public static Tensor restore(Tensor file_pattern, Tensor tensor_name, TF_DataType dt, int preferred_shard = -1, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Restore", name) { args = new object[] { file_pattern, tensor_name }, attrs = new Dictionary() { ["dt"] = dt, ["preferred_shard"] = preferred_shard } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return restore_eager_fallback(file_pattern, tensor_name, dt: dt, preferred_shard: preferred_shard, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["file_pattern"] = file_pattern; + keywords["tensor_name"] = tensor_name; + keywords["dt"] = dt; + keywords["preferred_shard"] = preferred_shard; + var _op = tf.OpDefLib._apply_op_helper("Restore", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "dt", _op._get_attr_type("dt"), "preferred_shard", _op._get_attr_int("preferred_shard") }; + _execute.record_gradient("Restore", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor restore_eager_fallback(Tensor file_pattern, Tensor tensor_name, TF_DataType dt, int preferred_shard, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { file_pattern, tensor_name }; + object[] _attrs = new object[] { "dt", dt, "preferred_shard", preferred_shard }; + var _result = _execute.execute("Restore", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Restore", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Restores a tensor from checkpoint files. + /// + /// + /// + /// This is like `Restore` except that restored tensor can be listed as filling + /// only a slice of a larger tensor. `shape_and_slice` specifies the shape of the + /// larger tensor and the slice that the restored tensor covers. + /// + /// The `shape_and_slice` input has the same format as the + /// elements of the `shapes_and_slices` input of the `SaveSlices` op. + /// + /// + /// + /// + /// + /// + /// + /// The type of the tensor to be restored. + /// + /// + /// + /// + /// Index of file to open first if multiple files match + /// `file_pattern`. See the documentation for `Restore`. + /// + /// + /// + public static Tensor restore_slice(Tensor file_pattern, Tensor tensor_name, Tensor shape_and_slice, TF_DataType dt, int preferred_shard = -1, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "RestoreSlice", name) { args = new object[] { file_pattern, tensor_name, shape_and_slice }, attrs = new Dictionary() { ["dt"] = dt, ["preferred_shard"] = preferred_shard } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return restore_slice_eager_fallback(file_pattern, tensor_name, shape_and_slice, dt: dt, preferred_shard: preferred_shard, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["file_pattern"] = file_pattern; + keywords["tensor_name"] = tensor_name; + keywords["shape_and_slice"] = shape_and_slice; + keywords["dt"] = dt; + keywords["preferred_shard"] = preferred_shard; + var _op = tf.OpDefLib._apply_op_helper("RestoreSlice", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "dt", _op._get_attr_type("dt"), "preferred_shard", _op._get_attr_int("preferred_shard") }; + _execute.record_gradient("RestoreSlice", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor restore_slice_eager_fallback(Tensor file_pattern, Tensor tensor_name, Tensor shape_and_slice, TF_DataType dt, int preferred_shard, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { file_pattern, tensor_name, shape_and_slice }; + object[] _attrs = new object[] { "dt", dt, "preferred_shard", preferred_shard }; + var _result = _execute.execute("RestoreSlice", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("RestoreSlice", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Restores tensors from a V2 checkpoint. + /// + /// + /// + /// For backward compatibility with the V1 format, this Op currently allows + /// restoring from a V1 checkpoint as well: + /// - This Op first attempts to find the V2 index file pointed to by "prefix", and + /// if found proceed to read it as a V2 checkpoint; + /// - Otherwise the V1 read path is invoked. + /// Relying on this behavior is not recommended, as the ability to fall back to read + /// V1 might be deprecated and eventually removed. + /// + /// By default, restores the named tensors in full. If the caller wishes to restore + /// specific slices of stored tensors, "shape_and_slices" should be non-empty + /// strings and correspondingly well-formed. + /// + /// Callers must ensure all the named tensors are indeed stored in the checkpoint. + /// + /// + /// + /// + /// + /// + /// + /// shape {N}. The list of expected dtype for the tensors. Must match + /// those stored in the checkpoint. + /// + /// + /// + public static Tensor[] restore_v2(Tensor prefix, Tensor tensor_names, Tensor shape_and_slices, TF_DataType[] dtypes, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "RestoreV2", name) { args = new object[] { prefix, tensor_names, shape_and_slices }, attrs = new Dictionary() { ["dtypes"] = dtypes } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return restore_v2_eager_fallback(prefix, tensor_names, shape_and_slices, dtypes: dtypes, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["prefix"] = prefix; + keywords["tensor_names"] = tensor_names; + keywords["shape_and_slices"] = shape_and_slices; + keywords["dtypes"] = dtypes; + var _op = tf.OpDefLib._apply_op_helper("RestoreV2", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "dtypes", _op.get_attr("dtypes") }; + _execute.record_gradient("RestoreV2", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] restore_v2_eager_fallback(Tensor prefix, Tensor tensor_names, Tensor shape_and_slices, TF_DataType[] dtypes, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { prefix, tensor_names, shape_and_slices }; + object[] _attrs = new object[] { }; + var _result = _execute.execute("RestoreV2", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("RestoreV2", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// Saves the input tensors to disk. + /// + /// + /// + /// The size of `tensor_names` must match the number of tensors in `data`. `data[i]` + /// is written to `filename` with name `tensor_names[i]`. + /// + /// See also `SaveSlices`. + /// + /// + /// + /// + /// + /// + public static Operation save(Tensor filename, Tensor tensor_names, Tensors data, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Save", name) { args = new object[] { filename, tensor_names, data }, attrs = new Dictionary() { } }); + return null; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return save_eager_fallback(filename, tensor_names, data, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["filename"] = filename; + keywords["tensor_names"] = tensor_names; + keywords["data"] = data; + var _op = tf.OpDefLib._apply_op_helper("Save", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op.get_attr("T") }; + _execute.record_gradient("Save", _op.inputs, _attrs, _result); + } + return _op; + } + + public static Operation save_eager_fallback(Tensor filename, Tensor tensor_names, Tensor data, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { filename, tensor_names, data }; + object[] _attrs = new object[] { }; + var _result = _execute.execute("Save", 0, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Save", _inputs_flat, _attrs, _result); + } + return null; + } + /// + /// Saves input tensors slices to disk. + /// + /// + /// + /// This is like `Save` except that tensors can be listed in the saved file as being + /// a slice of a larger tensor. `shapes_and_slices` specifies the shape of the + /// larger tensor and the slice that this tensor covers. `shapes_and_slices` must + /// have as many elements as `tensor_names`. + /// + /// Elements of the `shapes_and_slices` input must either be: + /// + /// * The empty string, in which case the corresponding tensor is + /// saved normally. + /// * A string of the form `dim0 dim1 ... dimN-1 slice-spec` where the + /// `dimI` are the dimensions of the larger tensor and `slice-spec` + /// specifies what part is covered by the tensor to save. + /// + /// `slice-spec` itself is a `:`-separated list: `slice0:slice1:...:sliceN-1` + /// where each `sliceI` is either: + /// + /// * The string `-` meaning that the slice covers all indices of this dimension + /// * `start,length` where `start` and `length` are integers. In that + /// case the slice covers `length` indices starting at `start`. + /// + /// See also `Save`. + /// + /// + /// + /// + /// + /// + /// + public static Operation save_slices(Tensor filename, Tensor tensor_names, Tensor shapes_and_slices, Tensors data, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "SaveSlices", name) { args = new object[] { filename, tensor_names, shapes_and_slices, data }, attrs = new Dictionary() { } }); + return null; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return save_slices_eager_fallback(filename, tensor_names, shapes_and_slices, data, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["filename"] = filename; + keywords["tensor_names"] = tensor_names; + keywords["shapes_and_slices"] = shapes_and_slices; + keywords["data"] = data; + var _op = tf.OpDefLib._apply_op_helper("SaveSlices", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op.get_attr("T") }; + _execute.record_gradient("SaveSlices", _op.inputs, _attrs, _result); + } + return _op; + } + + public static Operation save_slices_eager_fallback(Tensor filename, Tensor tensor_names, Tensor shapes_and_slices, Tensor data, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { filename, tensor_names, shapes_and_slices, data }; + object[] _attrs = new object[] { }; + var _result = _execute.execute("SaveSlices", 0, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("SaveSlices", _inputs_flat, _attrs, _result); + } + return null; + } + /// + /// Saves tensors in V2 checkpoint format. + /// + /// + /// + /// By default, saves the named tensors in full. If the caller wishes to save + /// specific slices of full tensors, "shape_and_slices" should be non-empty strings + /// and correspondingly well-formed. + /// + /// + /// + /// + /// + /// + /// + public static Operation save_v2(Tensor prefix, Tensor tensor_names, Tensor shape_and_slices, Tensors tensors, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "SaveV2", name) { args = new object[] { prefix, tensor_names, shape_and_slices, tensors }, attrs = new Dictionary() { } }); + return null; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return save_v2_eager_fallback(prefix, tensor_names, shape_and_slices, tensors, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["prefix"] = prefix; + keywords["tensor_names"] = tensor_names; + keywords["shape_and_slices"] = shape_and_slices; + keywords["tensors"] = tensors; + var _op = tf.OpDefLib._apply_op_helper("SaveV2", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "dtypes", _op.get_attr("dtypes") }; + _execute.record_gradient("SaveV2", _op.inputs, _attrs, _result); + } + return _op; + } + + public static Operation save_v2_eager_fallback(Tensor prefix, Tensor tensor_names, Tensor shape_and_slices, Tensor tensors, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { prefix, tensor_names, shape_and_slices, tensors }; + object[] _attrs = new object[] { }; + var _result = _execute.execute("SaveV2", 0, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("SaveV2", _inputs_flat, _attrs, _result); + } + return null; + } + /// + /// Generate a sharded filename. The filename is printf formatted as + /// + /// + /// + /// %s-%05d-of-%05d, basename, shard, num_shards. + /// + /// + /// + /// + /// + /// + public static Tensor sharded_filename(Tensor basename, Tensor shard, Tensor num_shards, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "ShardedFilename", name) { args = new object[] { basename, shard, num_shards }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return sharded_filename_eager_fallback(basename, shard, num_shards, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["basename"] = basename; + keywords["shard"] = shard; + keywords["num_shards"] = num_shards; + var _op = tf.OpDefLib._apply_op_helper("ShardedFilename", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { }; + _execute.record_gradient("ShardedFilename", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor sharded_filename_eager_fallback(Tensor basename, Tensor shard, Tensor num_shards, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { basename, shard, num_shards }; + object[] _attrs = new object[] { }; + var _result = _execute.execute("ShardedFilename", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("ShardedFilename", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Generate a glob pattern matching all sharded file names. + /// + /// + /// + /// + public static Tensor sharded_filespec(Tensor basename, Tensor num_shards, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "ShardedFilespec", name) { args = new object[] { basename, num_shards }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return sharded_filespec_eager_fallback(basename, num_shards, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["basename"] = basename; + keywords["num_shards"] = num_shards; + var _op = tf.OpDefLib._apply_op_helper("ShardedFilespec", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { }; + _execute.record_gradient("ShardedFilespec", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor sharded_filespec_eager_fallback(Tensor basename, Tensor num_shards, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { basename, num_shards }; + object[] _attrs = new object[] { }; + var _result = _execute.execute("ShardedFilespec", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("ShardedFilespec", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// A Reader that outputs the lines of a file delimited by '\n'. + /// + /// + /// + /// Number of lines to skip from the beginning of every file. + /// + /// + /// + /// + /// If non-empty, this reader is placed in the given container. + /// Otherwise, a default container is used. + /// + /// + /// + /// + /// If non-empty, this reader is named in the given bucket + /// with this shared_name. Otherwise, the node name is used instead. + /// + /// + /// + public static Tensor text_line_reader(int skip_header_lines = 0, string container = "", string shared_name = "", string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "TextLineReader", name) { args = new object[] { }, attrs = new Dictionary() { ["skip_header_lines"] = skip_header_lines, ["container"] = container, ["shared_name"] = shared_name } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return text_line_reader_eager_fallback(skip_header_lines: skip_header_lines, container: container, shared_name: shared_name, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + if (container is null) + { + container = ""; + } + if (shared_name is null) + { + shared_name = ""; + } + Dictionary keywords = new(); + keywords["skip_header_lines"] = skip_header_lines; + keywords["container"] = container; + keywords["shared_name"] = shared_name; + var _op = tf.OpDefLib._apply_op_helper("TextLineReader", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "skip_header_lines", _op._get_attr_int("skip_header_lines"), "container", _op.get_attr("container"), "shared_name", _op.get_attr("shared_name") }; + _execute.record_gradient("TextLineReader", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor text_line_reader_eager_fallback(int skip_header_lines, string container, string shared_name, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { }; + object[] _attrs = new object[] { "skip_header_lines", skip_header_lines, "container", container, "shared_name", shared_name }; + var _result = _execute.execute("TextLineReader", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("TextLineReader", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// A Reader that outputs the lines of a file delimited by '\n'. + /// + /// + /// + /// Number of lines to skip from the beginning of every file. + /// + /// + /// + /// + /// If non-empty, this reader is placed in the given container. + /// Otherwise, a default container is used. + /// + /// + /// + /// + /// If non-empty, this reader is named in the given bucket + /// with this shared_name. Otherwise, the node name is used instead. + /// + /// + /// + public static Tensor text_line_reader_v2(int skip_header_lines = 0, string container = "", string shared_name = "", string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "TextLineReaderV2", name) { args = new object[] { }, attrs = new Dictionary() { ["skip_header_lines"] = skip_header_lines, ["container"] = container, ["shared_name"] = shared_name } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return text_line_reader_v2_eager_fallback(skip_header_lines: skip_header_lines, container: container, shared_name: shared_name, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + if (container is null) + { + container = ""; + } + if (shared_name is null) + { + shared_name = ""; + } + Dictionary keywords = new(); + keywords["skip_header_lines"] = skip_header_lines; + keywords["container"] = container; + keywords["shared_name"] = shared_name; + var _op = tf.OpDefLib._apply_op_helper("TextLineReaderV2", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "skip_header_lines", _op._get_attr_int("skip_header_lines"), "container", _op.get_attr("container"), "shared_name", _op.get_attr("shared_name") }; + _execute.record_gradient("TextLineReaderV2", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor text_line_reader_v2_eager_fallback(int skip_header_lines, string container, string shared_name, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { }; + object[] _attrs = new object[] { "skip_header_lines", skip_header_lines, "container", container, "shared_name", shared_name }; + var _result = _execute.execute("TextLineReaderV2", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("TextLineReaderV2", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// A Reader that outputs the entire contents of a file as a value. + /// + /// + /// + /// To use, enqueue filenames in a Queue. The output of ReaderRead will + /// be a filename (key) and the contents of that file (value). + /// + /// + /// + /// + /// If non-empty, this reader is placed in the given container. + /// Otherwise, a default container is used. + /// + /// + /// + /// + /// If non-empty, this reader is named in the given bucket + /// with this shared_name. Otherwise, the node name is used instead. + /// + /// + /// + public static Tensor whole_file_reader(string container = "", string shared_name = "", string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "WholeFileReader", name) { args = new object[] { }, attrs = new Dictionary() { ["container"] = container, ["shared_name"] = shared_name } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return whole_file_reader_eager_fallback(container: container, shared_name: shared_name, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + if (container is null) + { + container = ""; + } + if (shared_name is null) + { + shared_name = ""; + } + Dictionary keywords = new(); + keywords["container"] = container; + keywords["shared_name"] = shared_name; + var _op = tf.OpDefLib._apply_op_helper("WholeFileReader", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "container", _op.get_attr("container"), "shared_name", _op.get_attr("shared_name") }; + _execute.record_gradient("WholeFileReader", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor whole_file_reader_eager_fallback(string container, string shared_name, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { }; + object[] _attrs = new object[] { "container", container, "shared_name", shared_name }; + var _result = _execute.execute("WholeFileReader", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("WholeFileReader", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// A Reader that outputs the entire contents of a file as a value. + /// + /// + /// + /// To use, enqueue filenames in a Queue. The output of ReaderRead will + /// be a filename (key) and the contents of that file (value). + /// + /// + /// + /// + /// If non-empty, this reader is placed in the given container. + /// Otherwise, a default container is used. + /// + /// + /// + /// + /// If non-empty, this reader is named in the given bucket + /// with this shared_name. Otherwise, the node name is used instead. + /// + /// + /// + public static Tensor whole_file_reader_v2(string container = "", string shared_name = "", string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "WholeFileReaderV2", name) { args = new object[] { }, attrs = new Dictionary() { ["container"] = container, ["shared_name"] = shared_name } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return whole_file_reader_v2_eager_fallback(container: container, shared_name: shared_name, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + if (container is null) + { + container = ""; + } + if (shared_name is null) + { + shared_name = ""; + } + Dictionary keywords = new(); + keywords["container"] = container; + keywords["shared_name"] = shared_name; + var _op = tf.OpDefLib._apply_op_helper("WholeFileReaderV2", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "container", _op.get_attr("container"), "shared_name", _op.get_attr("shared_name") }; + _execute.record_gradient("WholeFileReaderV2", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor whole_file_reader_v2_eager_fallback(string container, string shared_name, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { }; + object[] _attrs = new object[] { "container", container, "shared_name", shared_name }; + var _result = _execute.execute("WholeFileReaderV2", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("WholeFileReaderV2", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Writes `contents` to the file at input `filename`. + /// + /// + /// + /// Creates the file and recursively creates directory if it does not exist. + /// + /// + /// + /// + /// + public static Operation write_file(Tensor filename, Tensor contents, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "WriteFile", name) { args = new object[] { filename, contents }, attrs = new Dictionary() { } }); + return null; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return write_file_eager_fallback(filename, contents, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["filename"] = filename; + keywords["contents"] = contents; + var _op = tf.OpDefLib._apply_op_helper("WriteFile", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { }; + _execute.record_gradient("WriteFile", _op.inputs, _attrs, _result); + } + return _op; + } + + public static Operation write_file_eager_fallback(Tensor filename, Tensor contents, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { filename, contents }; + object[] _attrs = new object[] { }; + var _result = _execute.execute("WriteFile", 0, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("WriteFile", _inputs_flat, _attrs, _result); + } + return null; + } +} diff --git a/src/TensorFlowNET.Core/Operations/gen_list_ops.cs b/src/TensorFlowNET.Core/Operations/gen_list_ops.cs new file mode 100644 index 000000000..59c783b24 --- /dev/null +++ b/src/TensorFlowNET.Core/Operations/gen_list_ops.cs @@ -0,0 +1,1308 @@ +/*Wrappers around TensorFlow ops. This file is MACHINE GENERATED! Do not edit.*/ + +using Tensorflow.Eager; +using Tensorflow.Contexts; +using Tensorflow.Exceptions; +using static Tensorflow.Binding; + +namespace Tensorflow; + +public static class gen_list_ops +{ + /// + /// Creates and returns an empty tensor list. + /// + /// + /// + /// All list elements must be tensors of dtype element_dtype and shape compatible + /// with element_shape. + /// + /// handle: an empty tensor list. + /// element_dtype: the type of elements in the list. + /// element_shape: a shape compatible with that of elements in the list. + /// + /// + /// + /// + /// + /// + public static Tensor empty_tensor_list(Tensor element_shape, Tensor max_num_elements, TF_DataType element_dtype, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "EmptyTensorList", name) { args = new object[] { element_shape, max_num_elements }, attrs = new Dictionary() { ["element_dtype"] = element_dtype } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return empty_tensor_list_eager_fallback(element_shape, max_num_elements, element_dtype: element_dtype, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["element_shape"] = element_shape; + keywords["max_num_elements"] = max_num_elements; + keywords["element_dtype"] = element_dtype; + var _op = tf.OpDefLib._apply_op_helper("EmptyTensorList", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "element_dtype", _op._get_attr_type("element_dtype"), "shape_type", _op._get_attr_type("shape_type") }; + _execute.record_gradient("EmptyTensorList", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor empty_tensor_list_eager_fallback(Tensor element_shape, Tensor max_num_elements, TF_DataType element_dtype, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { element_shape, max_num_elements }; + object[] _attrs = new object[] { "element_dtype", element_dtype, "shape_type", element_shape.dtype }; + var _result = _execute.execute("EmptyTensorList", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("EmptyTensorList", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Concats all tensors in the list along the 0th dimension. + /// + /// + /// + /// Requires that all tensors have the same shape except the first dimension. + /// + /// input_handle: The input list. + /// tensor: The concated result. + /// lengths: Output tensor containing sizes of the 0th dimension of tensors in the list, used for computing the gradient. + /// + /// + /// + /// + /// + /// + /// + public static Tensor[] tensor_list_concat(Tensor input_handle, TF_DataType element_dtype, Shape element_shape = null, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "TensorListConcat", name) { args = new object[] { input_handle }, attrs = new Dictionary() { ["element_dtype"] = element_dtype, ["element_shape"] = element_shape } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return tensor_list_concat_eager_fallback(input_handle, element_dtype: element_dtype, element_shape: element_shape, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input_handle"] = input_handle; + keywords["element_dtype"] = element_dtype; + keywords["element_shape"] = element_shape; + var _op = tf.OpDefLib._apply_op_helper("TensorListConcat", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "element_dtype", _op._get_attr_type("element_dtype"), "element_shape", _op.get_attr("element_shape") }; + _execute.record_gradient("TensorListConcat", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] tensor_list_concat_eager_fallback(Tensor input_handle, TF_DataType element_dtype, Shape element_shape, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input_handle }; + object[] _attrs = new object[] { "element_dtype", element_dtype, "element_shape", element_shape }; + var _result = _execute.execute("TensorListConcat", 2, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("TensorListConcat", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// + /// + /// + /// + /// + /// + public static Tensor tensor_list_concat_lists(Tensor input_a, Tensor input_b, TF_DataType element_dtype, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "TensorListConcatLists", name) { args = new object[] { input_a, input_b }, attrs = new Dictionary() { ["element_dtype"] = element_dtype } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return tensor_list_concat_lists_eager_fallback(input_a, input_b, element_dtype: element_dtype, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input_a"] = input_a; + keywords["input_b"] = input_b; + keywords["element_dtype"] = element_dtype; + var _op = tf.OpDefLib._apply_op_helper("TensorListConcatLists", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "element_dtype", _op._get_attr_type("element_dtype") }; + _execute.record_gradient("TensorListConcatLists", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor tensor_list_concat_lists_eager_fallback(Tensor input_a, Tensor input_b, TF_DataType element_dtype, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input_a, input_b }; + object[] _attrs = new object[] { "element_dtype", element_dtype }; + var _result = _execute.execute("TensorListConcatLists", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("TensorListConcatLists", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Concats all tensors in the list along the 0th dimension. + /// + /// + /// + /// Requires that all tensors have the same shape except the first dimension. + /// + /// input_handle: The input list. + /// element_shape: The shape of the uninitialized elements in the list. If the first + /// dimension is not -1, it is assumed that all list elements have the same + /// leading dim. + /// leading_dims: The list of leading dims of uninitialized list elements. Used if + /// the leading dim of input_handle.element_shape or the element_shape input arg + /// is not already set. + /// tensor: The concated result. + /// lengths: Output tensor containing sizes of the 0th dimension of tensors in the list, used for computing the gradient. + /// + /// + /// + /// + /// + /// + /// + /// + public static Tensor[] tensor_list_concat_v2(Tensor input_handle, Tensor element_shape, Tensor leading_dims, TF_DataType element_dtype, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "TensorListConcatV2", name) { args = new object[] { input_handle, element_shape, leading_dims }, attrs = new Dictionary() { ["element_dtype"] = element_dtype } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return tensor_list_concat_v2_eager_fallback(input_handle, element_shape, leading_dims, element_dtype: element_dtype, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input_handle"] = input_handle; + keywords["element_shape"] = element_shape; + keywords["leading_dims"] = leading_dims; + keywords["element_dtype"] = element_dtype; + var _op = tf.OpDefLib._apply_op_helper("TensorListConcatV2", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "element_dtype", _op._get_attr_type("element_dtype"), "shape_type", _op._get_attr_type("shape_type") }; + _execute.record_gradient("TensorListConcatV2", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] tensor_list_concat_v2_eager_fallback(Tensor input_handle, Tensor element_shape, Tensor leading_dims, TF_DataType element_dtype, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input_handle, element_shape, leading_dims }; + object[] _attrs = new object[] { "element_dtype", element_dtype, "shape_type", element_shape.dtype }; + var _result = _execute.execute("TensorListConcatV2", 2, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("TensorListConcatV2", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// The shape of the elements of the given list, as a tensor. + /// + /// + /// + /// input_handle: the list + /// element_shape: the shape of elements of the list + /// + /// + /// + /// + /// + public static Tensor tensor_list_element_shape(Tensor input_handle, TF_DataType shape_type, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "TensorListElementShape", name) { args = new object[] { input_handle }, attrs = new Dictionary() { ["shape_type"] = shape_type } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return tensor_list_element_shape_eager_fallback(input_handle, shape_type: shape_type, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input_handle"] = input_handle; + keywords["shape_type"] = shape_type; + var _op = tf.OpDefLib._apply_op_helper("TensorListElementShape", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "shape_type", _op._get_attr_type("shape_type") }; + _execute.record_gradient("TensorListElementShape", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor tensor_list_element_shape_eager_fallback(Tensor input_handle, TF_DataType shape_type, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input_handle }; + object[] _attrs = new object[] { "shape_type", shape_type }; + var _result = _execute.execute("TensorListElementShape", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("TensorListElementShape", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Creates a TensorList which, when stacked, has the value of `tensor`. + /// + /// + /// + /// Each tensor in the result list corresponds to one row of the input tensor. + /// + /// tensor: The input tensor. + /// output_handle: The list. + /// + /// + /// + /// + /// + public static Tensor tensor_list_from_tensor(Tensor tensor, Tensor element_shape, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "TensorListFromTensor", name) { args = new object[] { tensor, element_shape }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return tensor_list_from_tensor_eager_fallback(tensor, element_shape, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["tensor"] = tensor; + keywords["element_shape"] = element_shape; + var _op = tf.OpDefLib._apply_op_helper("TensorListFromTensor", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "element_dtype", _op._get_attr_type("element_dtype"), "shape_type", _op._get_attr_type("shape_type") }; + _execute.record_gradient("TensorListFromTensor", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor tensor_list_from_tensor_eager_fallback(Tensor tensor, Tensor element_shape, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { tensor, element_shape }; + object[] _attrs = new object[] { "element_dtype", tensor.dtype, "shape_type", element_shape.dtype }; + var _result = _execute.execute("TensorListFromTensor", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("TensorListFromTensor", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Creates a Tensor by indexing into the TensorList. + /// + /// + /// + /// Each row in the produced Tensor corresponds to the element in the TensorList + /// specified by the given index (see `tf.gather`). + /// + /// input_handle: The input tensor list. + /// indices: The indices used to index into the list. + /// values: The tensor. + /// + /// + /// + /// + /// + /// + /// + public static Tensor tensor_list_gather(Tensor input_handle, Tensor indices, Tensor element_shape, TF_DataType element_dtype, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "TensorListGather", name) { args = new object[] { input_handle, indices, element_shape }, attrs = new Dictionary() { ["element_dtype"] = element_dtype } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return tensor_list_gather_eager_fallback(input_handle, indices, element_shape, element_dtype: element_dtype, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input_handle"] = input_handle; + keywords["indices"] = indices; + keywords["element_shape"] = element_shape; + keywords["element_dtype"] = element_dtype; + var _op = tf.OpDefLib._apply_op_helper("TensorListGather", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "element_dtype", _op._get_attr_type("element_dtype") }; + _execute.record_gradient("TensorListGather", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor tensor_list_gather_eager_fallback(Tensor input_handle, Tensor indices, Tensor element_shape, TF_DataType element_dtype, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input_handle, indices, element_shape }; + object[] _attrs = new object[] { "element_dtype", element_dtype }; + var _result = _execute.execute("TensorListGather", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("TensorListGather", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// + /// + /// + /// + /// + /// + /// + public static Tensor tensor_list_get_item(Tensor input_handle, Tensor index, Tensor element_shape, TF_DataType element_dtype, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "TensorListGetItem", name) { args = new object[] { input_handle, index, element_shape }, attrs = new Dictionary() { ["element_dtype"] = element_dtype } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return tensor_list_get_item_eager_fallback(input_handle, index, element_shape, element_dtype: element_dtype, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input_handle"] = input_handle; + keywords["index"] = index; + keywords["element_shape"] = element_shape; + keywords["element_dtype"] = element_dtype; + var _op = tf.OpDefLib._apply_op_helper("TensorListGetItem", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "element_dtype", _op._get_attr_type("element_dtype") }; + _execute.record_gradient("TensorListGetItem", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor tensor_list_get_item_eager_fallback(Tensor input_handle, Tensor index, Tensor element_shape, TF_DataType element_dtype, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input_handle, index, element_shape }; + object[] _attrs = new object[] { "element_dtype", element_dtype }; + var _result = _execute.execute("TensorListGetItem", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("TensorListGetItem", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns the number of tensors in the input tensor list. + /// + /// + /// + /// input_handle: the input list + /// length: the number of tensors in the list + /// + /// + /// + /// + public static Tensor tensor_list_length(Tensor input_handle, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "TensorListLength", name) { args = new object[] { input_handle }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return tensor_list_length_eager_fallback(input_handle, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input_handle"] = input_handle; + var _op = tf.OpDefLib._apply_op_helper("TensorListLength", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { }; + _execute.record_gradient("TensorListLength", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor tensor_list_length_eager_fallback(Tensor input_handle, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input_handle }; + object[] _attrs = new object[] { }; + var _result = _execute.execute("TensorListLength", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("TensorListLength", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns the last element of the input list as well as a list with all but that element. + /// + /// + /// + /// Fails if the list is empty. + /// + /// input_handle: the input list + /// tensor: the withdrawn last element of the list + /// element_dtype: the type of elements in the list + /// element_shape: the shape of the output tensor + /// + /// + /// + /// + /// + /// + public static Tensor[] tensor_list_pop_back(Tensor input_handle, Tensor element_shape, TF_DataType element_dtype, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "TensorListPopBack", name) { args = new object[] { input_handle, element_shape }, attrs = new Dictionary() { ["element_dtype"] = element_dtype } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return tensor_list_pop_back_eager_fallback(input_handle, element_shape, element_dtype: element_dtype, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input_handle"] = input_handle; + keywords["element_shape"] = element_shape; + keywords["element_dtype"] = element_dtype; + var _op = tf.OpDefLib._apply_op_helper("TensorListPopBack", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "element_dtype", _op._get_attr_type("element_dtype") }; + _execute.record_gradient("TensorListPopBack", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] tensor_list_pop_back_eager_fallback(Tensor input_handle, Tensor element_shape, TF_DataType element_dtype, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input_handle, element_shape }; + object[] _attrs = new object[] { "element_dtype", element_dtype }; + var _result = _execute.execute("TensorListPopBack", 2, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("TensorListPopBack", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// Returns a list which has the passed-in `Tensor` as last element and the other elements of the given list in `input_handle`. + /// + /// + /// + /// tensor: The tensor to put on the list. + /// input_handle: The old list. + /// output_handle: A list with the elements of the old list followed by tensor. + /// element_dtype: the type of elements in the list. + /// element_shape: a shape compatible with that of elements in the list. + /// + /// + /// + /// + /// + public static Tensor tensor_list_push_back(Tensor input_handle, Tensor tensor, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "TensorListPushBack", name) { args = new object[] { input_handle, tensor }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return tensor_list_push_back_eager_fallback(input_handle, tensor, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input_handle"] = input_handle; + keywords["tensor"] = tensor; + var _op = tf.OpDefLib._apply_op_helper("TensorListPushBack", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "element_dtype", _op._get_attr_type("element_dtype") }; + _execute.record_gradient("TensorListPushBack", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor tensor_list_push_back_eager_fallback(Tensor input_handle, Tensor tensor, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input_handle, tensor }; + object[] _attrs = new object[] { "element_dtype", tensor.dtype }; + var _result = _execute.execute("TensorListPushBack", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("TensorListPushBack", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// + /// + /// + /// + /// + public static Tensor tensor_list_push_back_batch(Tensor input_handles, Tensor tensor, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "TensorListPushBackBatch", name) { args = new object[] { input_handles, tensor }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return tensor_list_push_back_batch_eager_fallback(input_handles, tensor, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input_handles"] = input_handles; + keywords["tensor"] = tensor; + var _op = tf.OpDefLib._apply_op_helper("TensorListPushBackBatch", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "element_dtype", _op._get_attr_type("element_dtype") }; + _execute.record_gradient("TensorListPushBackBatch", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor tensor_list_push_back_batch_eager_fallback(Tensor input_handles, Tensor tensor, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input_handles, tensor }; + object[] _attrs = new object[] { "element_dtype", tensor.dtype }; + var _result = _execute.execute("TensorListPushBackBatch", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("TensorListPushBackBatch", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// List of the given size with empty elements. + /// + /// + /// + /// element_shape: the shape of the future elements of the list + /// num_elements: the number of elements to reserve + /// handle: the output list + /// element_dtype: the desired type of elements in the list. + /// + /// + /// + /// + /// + /// + public static Tensor tensor_list_reserve(Tensor element_shape, Tensor num_elements, TF_DataType element_dtype, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "TensorListReserve", name) { args = new object[] { element_shape, num_elements }, attrs = new Dictionary() { ["element_dtype"] = element_dtype } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return tensor_list_reserve_eager_fallback(element_shape, num_elements, element_dtype: element_dtype, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["element_shape"] = element_shape; + keywords["num_elements"] = num_elements; + keywords["element_dtype"] = element_dtype; + var _op = tf.OpDefLib._apply_op_helper("TensorListReserve", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "element_dtype", _op._get_attr_type("element_dtype"), "shape_type", _op._get_attr_type("shape_type") }; + _execute.record_gradient("TensorListReserve", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor tensor_list_reserve_eager_fallback(Tensor element_shape, Tensor num_elements, TF_DataType element_dtype, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { element_shape, num_elements }; + object[] _attrs = new object[] { "element_dtype", element_dtype, "shape_type", element_shape.dtype }; + var _result = _execute.execute("TensorListReserve", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("TensorListReserve", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Resizes the list. + /// + /// + /// + /// + /// input_handle: the input list + /// size: size of the output list + /// + /// + /// + /// + /// + /// + public static Tensor tensor_list_resize(Tensor input_handle, Tensor size, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "TensorListResize", name) { args = new object[] { input_handle, size }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return tensor_list_resize_eager_fallback(input_handle, size, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input_handle"] = input_handle; + keywords["size"] = size; + var _op = tf.OpDefLib._apply_op_helper("TensorListResize", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { }; + _execute.record_gradient("TensorListResize", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor tensor_list_resize_eager_fallback(Tensor input_handle, Tensor size, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input_handle, size }; + object[] _attrs = new object[] { }; + var _result = _execute.execute("TensorListResize", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("TensorListResize", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Creates a TensorList by indexing into a Tensor. + /// + /// + /// + /// Each member of the TensorList corresponds to one row of the input tensor, + /// specified by the given index (see `tf.gather`). + /// + /// tensor: The input tensor. + /// indices: The indices used to index into the list. + /// element_shape: The shape of the elements in the list (can be less specified than + /// the shape of the tensor). + /// output_handle: The TensorList. + /// + /// + /// + /// + /// + /// + public static Tensor tensor_list_scatter(Tensor tensor, Tensor indices, Tensor element_shape, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "TensorListScatter", name) { args = new object[] { tensor, indices, element_shape }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return tensor_list_scatter_eager_fallback(tensor, indices, element_shape, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["tensor"] = tensor; + keywords["indices"] = indices; + keywords["element_shape"] = element_shape; + var _op = tf.OpDefLib._apply_op_helper("TensorListScatter", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "element_dtype", _op._get_attr_type("element_dtype"), "shape_type", _op._get_attr_type("shape_type") }; + _execute.record_gradient("TensorListScatter", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor tensor_list_scatter_eager_fallback(Tensor tensor, Tensor indices, Tensor element_shape, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { tensor, indices, element_shape }; + object[] _attrs = new object[] { "element_dtype", tensor.dtype, "shape_type", element_shape.dtype }; + var _result = _execute.execute("TensorListScatter", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("TensorListScatter", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Scatters tensor at indices in an input list. + /// + /// + /// + /// Each member of the TensorList corresponds to one row of the input tensor, + /// specified by the given index (see `tf.gather`). + /// + /// input_handle: The list to scatter into. + /// tensor: The input tensor. + /// indices: The indices used to index into the list. + /// output_handle: The TensorList. + /// + /// + /// + /// + /// + /// + public static Tensor tensor_list_scatter_into_existing_list(Tensor input_handle, Tensor tensor, Tensor indices, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "TensorListScatterIntoExistingList", name) { args = new object[] { input_handle, tensor, indices }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return tensor_list_scatter_into_existing_list_eager_fallback(input_handle, tensor, indices, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input_handle"] = input_handle; + keywords["tensor"] = tensor; + keywords["indices"] = indices; + var _op = tf.OpDefLib._apply_op_helper("TensorListScatterIntoExistingList", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "element_dtype", _op._get_attr_type("element_dtype") }; + _execute.record_gradient("TensorListScatterIntoExistingList", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor tensor_list_scatter_into_existing_list_eager_fallback(Tensor input_handle, Tensor tensor, Tensor indices, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input_handle, tensor, indices }; + object[] _attrs = new object[] { "element_dtype", tensor.dtype }; + var _result = _execute.execute("TensorListScatterIntoExistingList", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("TensorListScatterIntoExistingList", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Creates a TensorList by indexing into a Tensor. + /// + /// + /// + /// Each member of the TensorList corresponds to one row of the input tensor, + /// specified by the given index (see `tf.gather`). + /// + /// tensor: The input tensor. + /// indices: The indices used to index into the list. + /// element_shape: The shape of the elements in the list (can be less specified than + /// the shape of the tensor). + /// num_elements: The size of the output list. Must be large enough to accommodate + /// the largest index in indices. If -1, the list is just large enough to include + /// the largest index in indices. + /// output_handle: The TensorList. + /// + /// + /// + /// + /// + /// + /// + public static Tensor tensor_list_scatter_v2(Tensor tensor, Tensor indices, Tensor element_shape, Tensor num_elements, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "TensorListScatterV2", name) { args = new object[] { tensor, indices, element_shape, num_elements }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return tensor_list_scatter_v2_eager_fallback(tensor, indices, element_shape, num_elements, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["tensor"] = tensor; + keywords["indices"] = indices; + keywords["element_shape"] = element_shape; + keywords["num_elements"] = num_elements; + var _op = tf.OpDefLib._apply_op_helper("TensorListScatterV2", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "element_dtype", _op._get_attr_type("element_dtype"), "shape_type", _op._get_attr_type("shape_type") }; + _execute.record_gradient("TensorListScatterV2", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor tensor_list_scatter_v2_eager_fallback(Tensor tensor, Tensor indices, Tensor element_shape, Tensor num_elements, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { tensor, indices, element_shape, num_elements }; + object[] _attrs = new object[] { "element_dtype", tensor.dtype, "shape_type", element_shape.dtype }; + var _result = _execute.execute("TensorListScatterV2", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("TensorListScatterV2", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// + /// + /// + /// + /// + /// + public static Tensor tensor_list_set_item(Tensor input_handle, Tensor index, Tensor item, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "TensorListSetItem", name) { args = new object[] { input_handle, index, item }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return tensor_list_set_item_eager_fallback(input_handle, index, item, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input_handle"] = input_handle; + keywords["index"] = index; + keywords["item"] = item; + var _op = tf.OpDefLib._apply_op_helper("TensorListSetItem", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "element_dtype", _op._get_attr_type("element_dtype") }; + _execute.record_gradient("TensorListSetItem", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor tensor_list_set_item_eager_fallback(Tensor input_handle, Tensor index, Tensor item, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input_handle, index, item }; + object[] _attrs = new object[] { "element_dtype", item.dtype }; + var _result = _execute.execute("TensorListSetItem", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("TensorListSetItem", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Splits a tensor into a list. + /// + /// + /// + /// list[i] corresponds to lengths[i] tensors from the input tensor. + /// The tensor must have rank at least 1 and contain exactly sum(lengths) elements. + /// + /// tensor: The input tensor. + /// element_shape: A shape compatible with that of elements in the tensor. + /// lengths: Vector of sizes of the 0th dimension of tensors in the list. + /// output_handle: The list. + /// + /// + /// + /// + /// + /// + public static Tensor tensor_list_split(Tensor tensor, Tensor element_shape, Tensor lengths, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "TensorListSplit", name) { args = new object[] { tensor, element_shape, lengths }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return tensor_list_split_eager_fallback(tensor, element_shape, lengths, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["tensor"] = tensor; + keywords["element_shape"] = element_shape; + keywords["lengths"] = lengths; + var _op = tf.OpDefLib._apply_op_helper("TensorListSplit", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "element_dtype", _op._get_attr_type("element_dtype"), "shape_type", _op._get_attr_type("shape_type") }; + _execute.record_gradient("TensorListSplit", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor tensor_list_split_eager_fallback(Tensor tensor, Tensor element_shape, Tensor lengths, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { tensor, element_shape, lengths }; + object[] _attrs = new object[] { "element_dtype", tensor.dtype, "shape_type", element_shape.dtype }; + var _result = _execute.execute("TensorListSplit", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("TensorListSplit", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Stacks all tensors in the list. + /// + /// + /// + /// Requires that all tensors have the same shape. + /// + /// input_handle: the input list + /// tensor: the gathered result + /// num_elements: optional. If not -1, the number of elements in the list. + /// + /// + /// + /// + /// + /// + /// + /// + public static Tensor tensor_list_stack(Tensor input_handle, Tensor element_shape, TF_DataType element_dtype, int num_elements = -1, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "TensorListStack", name) { args = new object[] { input_handle, element_shape }, attrs = new Dictionary() { ["element_dtype"] = element_dtype, ["num_elements"] = num_elements } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return tensor_list_stack_eager_fallback(input_handle, element_shape, element_dtype: element_dtype, num_elements: num_elements, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input_handle"] = input_handle; + keywords["element_shape"] = element_shape; + keywords["element_dtype"] = element_dtype; + keywords["num_elements"] = num_elements; + var _op = tf.OpDefLib._apply_op_helper("TensorListStack", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "element_dtype", _op._get_attr_type("element_dtype"), "num_elements", _op._get_attr_int("num_elements") }; + _execute.record_gradient("TensorListStack", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor tensor_list_stack_eager_fallback(Tensor input_handle, Tensor element_shape, TF_DataType element_dtype, int num_elements, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input_handle, element_shape }; + object[] _attrs = new object[] { "element_dtype", element_dtype, "num_elements", num_elements }; + var _result = _execute.execute("TensorListStack", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("TensorListStack", _inputs_flat, _attrs, _result); + } + return _result[0]; + } +} diff --git a/src/TensorFlowNET.Core/Operations/gen_logging_ops.cs b/src/TensorFlowNET.Core/Operations/gen_logging_ops.cs index 03159aaa1..d2907f090 100644 --- a/src/TensorFlowNET.Core/Operations/gen_logging_ops.cs +++ b/src/TensorFlowNET.Core/Operations/gen_logging_ops.cs @@ -26,7 +26,7 @@ public static Operation assert(Tensor condition, object[] data, long summarize = if (tf.Context.executing_eagerly()) { var results = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo( - "Assert", name, + tf.Context, "Assert", name, new object[] { condition, data, summarize })); return results[0]; diff --git a/src/TensorFlowNET.Core/Operations/gen_math_ops.cs b/src/TensorFlowNET.Core/Operations/gen_math_ops.cs index 564abbd0f..a8152a11e 100644 --- a/src/TensorFlowNET.Core/Operations/gen_math_ops.cs +++ b/src/TensorFlowNET.Core/Operations/gen_math_ops.cs @@ -1,569 +1,10072 @@ -/***************************************************************************** - Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -******************************************************************************/ - -using System; -using System.Collections; -using System.Collections.Generic; -using System.Linq; +/*Wrappers around TensorFlow ops. This file is MACHINE GENERATED! Do not edit.*/ + +using Tensorflow.Eager; using Tensorflow.Contexts; +using Tensorflow.Exceptions; using static Tensorflow.Binding; -namespace Tensorflow +namespace Tensorflow; + +public static class gen_math_ops { - public static partial class gen_math_ops - { - public static Tensor _all(Tensor input, Tensor axis, bool keep_dims = false, string name = null) - { - var _op = tf.OpDefLib._apply_op_helper("All", name, args: new { input, reduction_indices = axis, keep_dims = keep_dims }); - - return _op.outputs[0]; - } - - /// - /// Add all input tensors element wise. - /// - /// - /// - /// - public static Tensor add_n(Tensor[] inputs, string name = null) - => tf.Context.ExecuteOp("AddN", name, new ExecuteOpArgs() - { - OpInputArgs = new object[] { inputs } - }); - - /// - /// Returns the index with the largest value across dimensions of a tensor. - /// - /// - /// - /// - /// - /// - public static Tensor arg_max(Tensor input, Axis dimension, TF_DataType output_type = TF_DataType.TF_INT64, string name = null) - => tf.Context.ExecuteOp("ArgMax", name, new ExecuteOpArgs(input, dimension) - .SetAttributes(new { output_type })); - - - /// - /// Returns the index with the smallest value across dimensions of a tensor. - /// - /// - /// - /// - /// - /// - public static Tensor arg_min(Tensor input, int dimension, TF_DataType output_type = TF_DataType.TF_INT64, string name = null) - => tf.Context.ExecuteOp("ArgMin", name, new ExecuteOpArgs(input, dimension) - .SetAttributes(new { output_type })); - - /// - /// Computes Psi, the derivative of Lgamma (the log of the absolute value of - /// `Gamma(x)`), element-wise. - /// - /// - /// - /// - public static Tensor digamma(Tensor x, string name = null) - => tf.OpDefLib._apply_op_helper("Digamma", name, args: new { x }).output; - - /// - /// Returns 0 if the denominator is zero. - /// - /// - /// - /// - /// - /// - /// If specified, the created operation in the graph will be this one, otherwise it will be named 'DivNoNan'. - /// - /// - /// The Operation can be fetched from the resulting Tensor, by fetching the Operation property from the result. - /// - /// - /// - /// *NOTE*: DivNoNan supports broadcasting. More about broadcasting - /// [here](http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html) - /// - public static Tensor div_no_nan(Tensor x, Tensor y, string name = null) - => tf.Context.ExecuteOp("DivNoNan", name, new ExecuteOpArgs(x, y)); - - public static Tensor mean(Tensor input, int axis, bool keep_dims = false, string name = null) - => mean(input, ops.convert_to_tensor(axis), keep_dims: keep_dims, name: name); - - /// - /// Computes the mean of elements across dimensions of a tensor. - /// Reduces `input` along the dimensions given in `axis`. Unless - /// `keep_dims` is true, the rank of the tensor is reduced by 1 for each entry in - /// `axis`. If `keep_dims` is true, the reduced dimensions are retained with length 1. - /// - /// A `Tensor`. Must be one of the following types: - /// `float32`, `float64`, `int32`, `uint8`, `int16`, `int8`, `complex64`, `int64`, `qint8`, `quint8`, `qint32`, `bfloat16`, `uint16`, `complex128`, `half`, `uint32`, `uint64`. - /// The tensor to reduce. - /// A `Tensor`. Must be one of the following types: `int32`, `int64`. The dimensions to reduce. - /// An optional `bool`. Defaults to `False`. If true, retain reduced dimensions with length 1. - /// A name for the operation (optional). - /// A `Tensor`. Has the same type as `input`. - public static Tensor mean(Tensor input, Tensor axis, bool keep_dims = false, string name = null) - => tf.Context.ExecuteOp("Mean", name, new ExecuteOpArgs(input, axis) - { - GetGradientAttrs = (op) => new - { - T = op.get_attr("T"), - Tidx = op.get_attr("Tidx"), - keep_dims = op.get_attr("keep_dims") - } - }.SetAttributes(new { keep_dims, reduction_indices = axis })); - - public static Tensor mean(Tensor[] inputs, Tensor axis, bool keep_dims = false, string name = null) - { - if (tf.Context.executing_eagerly()) - { - return mean_eager_fallback(inputs, axis, keep_dims: keep_dims, name: name, ctx: tf.Context); - } - - var _op = tf.OpDefLib._apply_op_helper("Mean", name, args: new { inputs, reduction_indices = axis, keep_dims = keep_dims }); - - return _op.output; - } - - private static Tensor mean_eager_fallback(Tensor[] inputs, Tensor axis, bool keep_dims = false, string name = null, Context ctx = null) - { - var (_attr_T, input) = tf.Runner.ArgsToMatchingEager(ctx, args: new[] { inputs }); - var (_attr_Tidx, axis1) = tf.Runner.ArgsToMatchingEager(ctx, default_dtype: tf.int32, args: new[] { axis }); - var _inputs_flat = input.concat(axis1); - var _attrs = new object[] { "keep_dims", keep_dims, "T", _attr_T, "Tidx", _attr_Tidx }; - - return tf.Runner.Execute(ctx, "Mean", 1, _inputs_flat, _attrs, name: name)[0]; - } - - public static Tensor prod(T1 input, T2 axis, bool keep_dims = false, string name = null) - => tf.Context.ExecuteOp("Prod", name, - new ExecuteOpArgs(input, axis).SetAttributes(new { keep_dims, reduction_indices = axis })); - - private static Tensor prod_eager_fallback(Tensor input_t, int[] axis, bool keep_dims, string name, Context ctx = null) - { - var (_attr_T, input) = tf.Runner.ArgsToMatchingEager(ctx, args: new[] { input_t }); - var (_attr_Tidx, axis1) = tf.Runner.ArgsToMatchingEager(ctx, default_dtype: tf.int32, args: new[] { axis }); - var _inputs_flat = input.concat(axis1); - var _attrs = new object[] { "keep_dims", keep_dims, "T", _attr_T, "Tidx", _attr_Tidx }; - - return tf.Runner.Execute(ctx, "Prod", 1, _inputs_flat, _attrs, name: name)[0]; - } - - public static Tensor acos(Tensor x, string name = null) - => tf.Context.ExecuteOp("Acos", name, new ExecuteOpArgs(x)); - - public static Tensor asin(Tensor x, string name = null) - => tf.Context.ExecuteOp("Asin", name, new ExecuteOpArgs(x)); - - public static Tensor add(Tensor x, Tensor y, string name = null) - => tf.Context.ExecuteOp("Add", name, new ExecuteOpArgs(x, y)); - - public static Tensor add(Tx x, Ty y, string name = null) - => tf.Context.ExecuteOp("Add", name, new ExecuteOpArgs(x, y)); - - public static Tensor add_v2(Tx x, Ty y, string name = null) - => tf.Context.ExecuteOp("AddV2", name, new ExecuteOpArgs(x, y)); - - public static Tensor atan(Tensor x, string name = null) - => tf.Context.ExecuteOp("Atan", name, new ExecuteOpArgs(x)); - - public static Tensor ceil(Tensor x, string name = null) - => tf.Context.ExecuteOp("Ceil", name, new ExecuteOpArgs(x)); - - public static Tensor sin(Tensor x, string name = null) - => tf.Context.ExecuteOp("Sin", name, new ExecuteOpArgs(x)); - - /// - /// Computes sigmoid of x element-wise. - /// - /// - /// - /// - /// If specified, the created operation in the graph will be this one, otherwise it will be named 'Sigmoid'. - /// - /// - /// The Operation can be fetched from the resulting Tensor, by fetching the Operation property from the result. - /// - /// - /// Specifically, y = 1 / (1 + exp(-x)). - /// - public static Tensor sigmoid(Tensor x, string name = "Sigmoid") - => tf.Context.ExecuteOp("Sigmoid", name, new ExecuteOpArgs(x)); + /// + /// Computes the absolute value of a tensor. + /// + /// + /// + /// Given a tensor `x`, this operation returns a tensor containing the absolute + /// value of each element in `x`. For example, if x is an input element and y is + /// an output element, this operation computes \(y = |x|\). + /// + /// + /// + /// + public static Tensor abs(Tensor x, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Abs", name) { args = new object[] { x }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return abs_eager_fallback(x, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + var _op = tf.OpDefLib._apply_op_helper("Abs", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Abs", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor abs_eager_fallback(Tensor x, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("Abs", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Abs", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns the element-wise sum of a list of tensors. + /// + /// + /// + /// `tf.accumulate_n_v2` performs the same operation as `tf.add_n`, but does not + /// wait for all of its inputs to be ready before beginning to sum. This can + /// save memory if inputs are ready at different times, since minimum temporary + /// storage is proportional to the output size rather than the inputs size. + /// + /// Unlike the original `accumulate_n`, `accumulate_n_v2` is differentiable. + /// + /// Returns a `Tensor` of same shape and type as the elements of `inputs`. + /// + /// + /// + /// + /// + /// Shape of elements of `inputs`. + /// + /// + /// + public static Tensor accumulate_nv2(Tensors inputs, Shape shape, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "AccumulateNV2", name) { args = new object[] { inputs }, attrs = new Dictionary() { ["shape"] = shape } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return accumulate_nv2_eager_fallback(inputs, shape: shape, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["inputs"] = inputs; + keywords["shape"] = shape; + var _op = tf.OpDefLib._apply_op_helper("AccumulateNV2", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "N", _op._get_attr_int("N"), "T", _op._get_attr_type("T"), "shape", _op.get_attr("shape") }; + _execute.record_gradient("AccumulateNV2", _op.inputs, _attrs, _result); + } + return _result[0]; + } - /// - /// Computes the gradient of the sigmoid of x wrt its input. - /// - /// - /// - /// - /// - /// - /// If specified, the created operation in the graph will be this one, otherwise it will be named 'SigmoidGrad'. - /// - /// - /// The Operation can be fetched from the resulting Tensor, by fetching the Operation property from the result. - /// - /// - /// Specifically, grad = dy * y * (1 - y), where y = sigmoid(x), and - /// dy is the corresponding input gradient. - /// - public static Tensor sigmoid_grad(Tensor y, Tensor dy, string name = "SigmoidGrad") - => tf.Context.ExecuteOp("SigmoidGrad", name, new ExecuteOpArgs(y, dy)); + public static Tensor accumulate_nv2_eager_fallback(Tensors inputs, Shape shape, string name, Context ctx) + { + List _inputs_flat_list = new(); + _inputs_flat_list.AddRange(inputs); + var _inputs_flat = _inputs_flat_list.ToArray(); + object[] _attrs = new object[] { "N", inputs.Length, "T", inputs.dtype, "shape", shape }; + var _result = _execute.execute("AccumulateNV2", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("AccumulateNV2", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes acos of x element-wise. + /// + /// + /// + /// + /// Provided an input tensor, the `tf.math.acos` operation returns the inverse cosine of each element of the tensor. If `y = tf.math.cos(x)` then, `x = tf.math.acos(y)`. + /// + /// Input range is `[-1, 1]` and the output has a range of `[0, pi]`. + /// + /// + /// + /// + /// + public static Tensor acos(Tensor x, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Acos", name) { args = new object[] { x }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return acos_eager_fallback(x, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + var _op = tf.OpDefLib._apply_op_helper("Acos", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Acos", _op.inputs, _attrs, _result); + } + return _result[0]; + } - public static Tensor sign(T x, string name = "Sign") - => tf.Context.ExecuteOp("Sign", name, new ExecuteOpArgs(x)); + public static Tensor acos_eager_fallback(Tensor x, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("Acos", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Acos", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes inverse hyperbolic cosine of x element-wise. + /// + /// + /// + /// Given an input tensor, the function computes inverse hyperbolic cosine of every element. + /// Input range is `[1, inf]`. It returns `nan` if the input lies outside the range. + /// + /// ```python + /// x = tf.constant([-2, -0.5, 1, 1.2, 200, 10000, float("inf")]) + /// tf.math.acosh(x) ==> [nan nan 0. 0.62236255 5.9914584 9.903487 inf] + /// ``` + /// + /// + /// + /// + public static Tensor acosh(Tensor x, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Acosh", name) { args = new object[] { x }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return acosh_eager_fallback(x, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + var _op = tf.OpDefLib._apply_op_helper("Acosh", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Acosh", _op.inputs, _attrs, _result); + } + return _result[0]; + } - public static Tensor sinh(Tensor x, string name = null) - => tf.Context.ExecuteOp("Sinh", name, new ExecuteOpArgs(x)); + public static Tensor acosh_eager_fallback(Tensor x, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("Acosh", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Acosh", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns x + y element-wise. + /// + /// + /// + /// *NOTE*: `Add` supports broadcasting. `AddN` does not. More about broadcasting + /// [here](http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html) + /// + /// Given two input tensors, the `tf.add` operation computes the sum for every element in the tensor. + /// + /// Both input and output have a range `(-inf, inf)`. + /// + /// + /// + /// + /// + /// + public static Tensor add(Tensor x, Tensor y, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Add", name) { args = new object[] { x, y }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return add_eager_fallback(x, y, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + keywords["y"] = y; + var _op = tf.OpDefLib._apply_op_helper("Add", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Add", _op.inputs, _attrs, _result); + } + return _result[0]; + } - public static Tensor cos(T x, string name = null) - => tf.Context.ExecuteOp("Cos", name, new ExecuteOpArgs(x)); + public static Tensor add_eager_fallback(Tensor x, Tensor y, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x, y }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("Add", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Add", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Add all input tensors element wise. + /// + /// + /// + /// Inputs must be of same size and shape. + /// + /// ```python + /// x = [9, 7, 10] + /// tf.math.add_n(x) ==> 26 + /// ``` + /// + /// + /// + /// + public static Tensor add_n(Tensors inputs, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "AddN", name) { args = new object[] { inputs }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return add_n_eager_fallback(inputs, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["inputs"] = inputs; + var _op = tf.OpDefLib._apply_op_helper("AddN", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "N", _op._get_attr_int("N"), "T", _op._get_attr_type("T") }; + _execute.record_gradient("AddN", _op.inputs, _attrs, _result); + } + return _result[0]; + } - public static Tensor cosh(Tensor x, string name = null) - => tf.Context.ExecuteOp("Cosh", name, new ExecuteOpArgs(x)); + public static Tensor add_n_eager_fallback(Tensors inputs, string name, Context ctx) + { + List _inputs_flat_list = new(); + _inputs_flat_list.AddRange(inputs); + var _inputs_flat = _inputs_flat_list.ToArray(); + object[] _attrs = new object[] { "N", inputs.Length, "T", inputs.dtype }; + var _result = _execute.execute("AddN", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("AddN", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns x + y element-wise. + /// + /// + /// + /// *NOTE*: `Add` supports broadcasting. `AddN` does not. More about broadcasting + /// [here](http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html) + /// + /// + /// + /// + /// + public static Tensor add_v2(Tensor x, Tensor y, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "AddV2", name) { args = new object[] { x, y }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return add_v2_eager_fallback(x, y, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + keywords["y"] = y; + var _op = tf.OpDefLib._apply_op_helper("AddV2", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("AddV2", _op.inputs, _attrs, _result); + } + return _result[0]; + } - /// - /// Computes the sum along segments of a tensor. - /// - /// - /// - /// - /// - /// - public static Tensor unsorted_segment_sum(Tensor data, Tensor segment_ids, Tensor num_segments, string name = null) + public static Tensor add_v2_eager_fallback(Tensor x, Tensor y, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x, y }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("AddV2", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("AddV2", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes the "logical and" of elements across dimensions of a tensor. + /// + /// + /// + /// Reduces `input` along the dimensions given in `reduction_indices`. Unless + /// `keep_dims` is true, the rank of the tensor is reduced by 1 for each entry in + /// `reduction_indices`. If `keep_dims` is true, the reduced dimensions are + /// retained with length 1. + /// + /// + /// + /// + /// + /// + /// If true, retain reduced dimensions with length 1. + /// + /// + /// + public static Tensor all(Tensor input, Tensor reduction_indices, bool keep_dims = false, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) { - var _op = tf.OpDefLib._apply_op_helper("UnsortedSegmentSum", name, new { data, segment_ids, num_segments }); - return _op.outputs[0]; + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "All", name) { args = new object[] { input, reduction_indices }, attrs = new Dictionary() { ["keep_dims"] = keep_dims } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return all_eager_fallback(input, reduction_indices, keep_dims: keep_dims, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["reduction_indices"] = reduction_indices; + keywords["keep_dims"] = keep_dims; + var _op = tf.OpDefLib._apply_op_helper("All", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "keep_dims", _op._get_attr_bool("keep_dims"), "Tidx", _op._get_attr_type("Tidx") }; + _execute.record_gradient("All", _op.inputs, _attrs, _result); } + return _result[0]; + } - public static Tensor tan(Tensor x, string name = null) - => tf.Context.ExecuteOp("Tan", name, new ExecuteOpArgs(x)); - - public static Tensor tanh(Tensor x, string name = null) - => tf.Context.ExecuteOp("Tanh", name, new ExecuteOpArgs(x)); - - /// - /// Computes the gradient for the tanh of `x` wrt its input. - /// - /// - /// - /// - /// - public static Tensor tanh_grad(Tensor y, Tensor dy, string name = null) - => tf.Context.ExecuteOp("TanhGrad", name, new ExecuteOpArgs(y, dy)); - - public static Tensor floor(Tensor x, string name = null) - { - var _op = tf.OpDefLib._apply_op_helper("Floor", name, args: new { x }); - - return _op.outputs[0]; - } - - public static Tensor _clip_by_value(Tensor t, Tensor clip_value_min, Tensor clip_value_max, string name = null) - { - var _op = tf.OpDefLib._apply_op_helper("ClipByValue", name, args: new { t, clip_value_min, clip_value_max }); - - return _op.outputs[0]; - } - - public static Tensor greater(Tx x, Ty y, string name = null) - => tf.Context.ExecuteOp("Greater", name, new ExecuteOpArgs(x, y)); - - /// - /// Computes the log of the absolute value of `Gamma(x)` element-wise. - /// - /// - /// A `Tensor`. Must be one of the following types: `bfloat16`, `half`, `float32`, `float64`. - /// - /// - /// - /// - /// The Operation can be fetched from the resulting Tensor, by fetching the Operation property from the result. - /// - public static Tensor lgamma(Tensor x, string name = null) - => tf.Context.ExecuteOp("Lgamma", name, new ExecuteOpArgs(x)); - - - public static Tensor greater_equal(Tx x, Ty y, string name = null) - => tf.Context.ExecuteOp("GreaterEqual", name, new ExecuteOpArgs(x, y)); - - public static Tensor less(Tx x, Ty y, string name = null) - => tf.Context.ExecuteOp("Less", name, new ExecuteOpArgs(x, y)); - - public static Tensor less_equal(Tx x, Ty y, string name = null) - => tf.Context.ExecuteOp("LessEqual", name, new ExecuteOpArgs(x, y)); - - public static Tensor log1p(Tensor x, string name = null) - => tf.Context.ExecuteOp("Log1p", name, new ExecuteOpArgs(x)); - - public static Tensor logical_and(T x, T y, string name = null) - => tf.Context.ExecuteOp("LogicalAnd", name, new ExecuteOpArgs(x, y)); - - public static Tensor logical_not(Tensor x, string name = null) - => tf.Context.ExecuteOp("LogicalNot", name, new ExecuteOpArgs(x)); - - public static Tensor logical_or(Tensor x, Tensor y, string name = null) - => tf.Context.ExecuteOp("LogicalOr", name, new ExecuteOpArgs(x, y)); - - public static Tensor logical_xor(Tensor x, Tensor y, string name = "LogicalXor") - { - return logical_and( - logical_or(x, y), - logical_not(logical_and(x, y)), - name); - } + public static Tensor all_eager_fallback(Tensor input, Tensor reduction_indices, bool keep_dims, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, reduction_indices }; + object[] _attrs = new object[] { "keep_dims", keep_dims, "Tidx", reduction_indices.dtype }; + var _result = _execute.execute("All", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("All", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns the argument of a complex number. + /// + /// + /// + /// Given a tensor `input` of complex numbers, this operation returns a tensor of + /// type `float` that is the argument of each element in `input`. All elements in + /// `input` must be complex numbers of the form \(a + bj\), where *a* + /// is the real part and *b* is the imaginary part. + /// + /// The argument returned by this operation is of the form \(atan2(b, a)\). + /// + /// For example: + /// + /// ``` + /// # tensor 'input' is [-2.25 + 4.75j, 3.25 + 5.75j] + /// tf.angle(input) ==> [2.0132, 1.056] + /// ``` + /// + /// @compatibility(numpy) + /// Equivalent to np.angle. + /// @end_compatibility + /// + /// + /// + /// + /// + public static Tensor angle(Tensor input, TF_DataType Tout = TF_DataType.TF_FLOAT, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Angle", name) { args = new object[] { input }, attrs = new Dictionary() { ["Tout"] = Tout } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return angle_eager_fallback(input, Tout: Tout, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["Tout"] = Tout; + var _op = tf.OpDefLib._apply_op_helper("Angle", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "Tout", _op._get_attr_type("Tout") }; + _execute.record_gradient("Angle", _op.inputs, _attrs, _result); + } + return _result[0]; + } - public static Tensor squared_difference(Tensor x, Tensor y, string name = null) - => tf.Context.ExecuteOp("SquaredDifference", name, new ExecuteOpArgs(x, y)); + public static Tensor angle_eager_fallback(Tensor input, TF_DataType Tout, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input }; + object[] _attrs = new object[] { "T", input.dtype, "Tout", Tout }; + var _result = _execute.execute("Angle", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Angle", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes the "logical or" of elements across dimensions of a tensor. + /// + /// + /// + /// Reduces `input` along the dimensions given in `reduction_indices`. Unless + /// `keep_dims` is true, the rank of the tensor is reduced by 1 for each entry in + /// `reduction_indices`. If `keep_dims` is true, the reduced dimensions are + /// retained with length 1. + /// + /// + /// + /// + /// + /// + /// If true, retain reduced dimensions with length 1. + /// + /// + /// + public static Tensor any(Tensor input, Tensor reduction_indices, bool keep_dims = false, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Any", name) { args = new object[] { input, reduction_indices }, attrs = new Dictionary() { ["keep_dims"] = keep_dims } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return any_eager_fallback(input, reduction_indices, keep_dims: keep_dims, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["reduction_indices"] = reduction_indices; + keywords["keep_dims"] = keep_dims; + var _op = tf.OpDefLib._apply_op_helper("Any", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "keep_dims", _op._get_attr_bool("keep_dims"), "Tidx", _op._get_attr_type("Tidx") }; + _execute.record_gradient("Any", _op.inputs, _attrs, _result); + } + return _result[0]; + } - /// - /// Computes square of x element-wise. - /// - /// A `Tensor`. Must be one of the following types: `bfloat16`, `half`, `float32`, `float64`, `int32`, `int64`, `complex64`, `complex128`. - /// A name for the operation (optional). - /// A `Tensor`. Has the same type as `x`. - public static Tensor square(Tensor x, string name = null) - => tf.Context.ExecuteOp("Square", name, new ExecuteOpArgs(x)); + public static Tensor any_eager_fallback(Tensor input, Tensor reduction_indices, bool keep_dims, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, reduction_indices }; + object[] _attrs = new object[] { "keep_dims", keep_dims, "Tidx", reduction_indices.dtype }; + var _result = _execute.execute("Any", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Any", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns the truth value of abs(x-y) < tolerance element-wise. + /// + /// + /// + /// + /// + public static Tensor approximate_equal(Tensor x, Tensor y, float tolerance = 1E-05f, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "ApproximateEqual", name) { args = new object[] { x, y }, attrs = new Dictionary() { ["tolerance"] = tolerance } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return approximate_equal_eager_fallback(x, y, tolerance: tolerance, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + keywords["y"] = y; + keywords["tolerance"] = tolerance; + var _op = tf.OpDefLib._apply_op_helper("ApproximateEqual", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "tolerance", _op.get_attr("tolerance") }; + _execute.record_gradient("ApproximateEqual", _op.inputs, _attrs, _result); + } + return _result[0]; + } - /// - /// Returns which elements of x are finite. - /// - /// A `Tensor`. Must be one of the following types: `bfloat16`, `half`, `float32`, `float64`. - /// A name for the operation (optional). - /// A `Tensor` of type `bool`. - public static Tensor is_finite(Tensor x, string name = null) - => tf.Context.ExecuteOp("IsFinite", name, new ExecuteOpArgs(x)); + public static Tensor approximate_equal_eager_fallback(Tensor x, Tensor y, float tolerance, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x, y }; + object[] _attrs = new object[] { "T", x.dtype, "tolerance", tolerance }; + var _result = _execute.execute("ApproximateEqual", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("ApproximateEqual", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns the index with the largest value across dimensions of a tensor. + /// + /// + /// + /// Note that in case of ties the identity of the return value is not guaranteed. + /// + /// Usage: + /// ```python + /// import tensorflow as tf + /// a = [1, 10, 26.9, 2.8, 166.32, 62.3] + /// b = tf.math.argmax(input = a) + /// c = tf.keras.backend.eval(b) + /// # c = 4 + /// # here a[4] = 166.32 which is the largest element of a across axis 0 + /// ``` + /// + /// + /// + /// + /// + /// + public static Tensor arg_max(Tensor input, Tensor dimension, TF_DataType output_type = TF_DataType.TF_INT64, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "ArgMax", name) { args = new object[] { input, dimension }, attrs = new Dictionary() { ["output_type"] = output_type } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return arg_max_eager_fallback(input, dimension, output_type: output_type, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["dimension"] = dimension; + keywords["output_type"] = output_type; + var _op = tf.OpDefLib._apply_op_helper("ArgMax", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "Tidx", _op._get_attr_type("Tidx"), "output_type", _op._get_attr_type("output_type") }; + _execute.record_gradient("ArgMax", _op.inputs, _attrs, _result); + } + return _result[0]; + } - public static Tensor is_nan(Tensor x, string name = null) - => tf.Context.ExecuteOp("IsNan", name, new ExecuteOpArgs(x)); - - - /// - /// Computes exponential of x element-wise. \\(y = e^x\\). - /// - /// A `Tensor`. Must be one of the following types: `bfloat16`, `half`, `float32`, `float64`, `complex64`, `complex128`. - /// A name for the operation (optional). - /// A `Tensor`. Has the same type as `x`. - public static Tensor exp(Tensor x, string name = null) - => tf.Context.ExecuteOp("Exp", name, new ExecuteOpArgs(x)); - - /// - /// Computes natural logarithm of x element-wise. - /// - /// A `Tensor`. Must be one of the following types: `bfloat16`, `half`, `float32`, `float64`, `complex64`, `complex128`. - /// name: A name for the operation (optional). - /// A `Tensor`. Has the same type as `x`. - public static Tensor log(Tensor x, string name = null) - => tf.Context.ExecuteOp("Log", name, new ExecuteOpArgs(x)); - - public static Tensor softplus(Tensor features, string name = null) - => tf.Context.ExecuteOp("Softplus", name, new ExecuteOpArgs(features)); - - public static Tensor cast(Tensor x, TF_DataType DstT, bool Truncate = false, string name = null) - => tf.Context.ExecuteOp("Cast", name, new ExecuteOpArgs(x) - .SetAttributes(new { DstT, Truncate })); - - public static Tensor neg(Tensor x, string name = null) - => tf.Context.ExecuteOp("Neg", name, new ExecuteOpArgs(x)); - - public static Tensor sqrt(Tensor x, string name = null) - => tf.Context.ExecuteOp("Sqrt", name, new ExecuteOpArgs(x)); - - public static Tensor sub(Tensor x, Tensor y, string name = null) - => tf.Context.ExecuteOp("Sub", name, new ExecuteOpArgs(x, y)); - - public static Tensor sub(Tx x, Ty y, string name = null) - => tf.Context.ExecuteOp("Sub", name, new ExecuteOpArgs(x, y)); - - /// - /// Returns the truth value of (x == y) element-wise. - /// - /// - /// - /// - /// - public static Tensor equal(Tx x, Ty y, bool incompatible_shape_error = true, string name = null) - => tf.Context.ExecuteOp("Equal", name, new ExecuteOpArgs(x, y) - .SetAttributes(new - { - incompatible_shape_error - })); - - /// - /// Returns the truth value of (x != y) element-wise. - /// - /// The type of the x. - /// The type of the y. - /// The x. - /// The y. - /// The name. - /// - public static Tensor not_equal(Tx x, Ty y, string name = null) - => tf.Context.ExecuteOp("NotEqual", name, new ExecuteOpArgs(x, y)); - - public static Tensor atan2(Tensor y, Tensor x, string name = null) - => tf.Context.ExecuteOp("Atan2", name, new ExecuteOpArgs(y, x)); - - public static Tensor mul(Tx x, Ty y, string name = null) - => tf.Context.ExecuteOp("Mul", name, new ExecuteOpArgs(x, y)); - - public static Tensor mul_no_nan(Tx x, Ty y, string name = null) - { - var _op = tf.OpDefLib._apply_op_helper("MulNoNan", name, args: new { x, y }); - - return _op.outputs[0]; - } - - public static Tensor real_div(Tensor x, Tensor y, string name = null) - => tf.Context.ExecuteOp("RealDiv", name, new ExecuteOpArgs(x, y)); - - public static Tensor reciprocal(Tensor x, string name = null) - => tf.Context.ExecuteOp("Reciprocal", name, new ExecuteOpArgs(x)); - - public static Tensor floor_mod(Tensor x, Tensor y, string name = null) - => tf.Context.ExecuteOp("FloorMod", name, new ExecuteOpArgs(x, y)); - - public static Tensor floor_div(Tensor x, Tensor y, string name = null) - => tf.Context.ExecuteOp("FloorDiv", name, new ExecuteOpArgs(x, y)); - - /// - /// Multiply the matrix "a" by the matrix "b". - /// - /// - /// - /// - /// - /// - /// - public static Tensor mat_mul(Tensor a, Tensor b, bool transpose_a = false, bool transpose_b = false, string name = null) - => tf.Context.ExecuteOp("MatMul", name, new ExecuteOpArgs(a, b) - .SetAttributes(new - { - transpose_a, - transpose_b - })); - - /// - /// Returns the max of x and y (i.e. x > y ? x : y) element-wise. - /// - /// - /// - /// - /// - public static Tensor maximum(T1 x, T2 y, string name = null) - => tf.Context.ExecuteOp("Maximum", name, new ExecuteOpArgs(x, y)); - - public static Tensor minimum(T1 x, T2 y, string name = null) - => tf.Context.ExecuteOp("Minimum", name, new ExecuteOpArgs(x, y)); - - public static Tensor _abs(Tensor x, string name = null) - => tf.Context.ExecuteOp("Abs", name, new ExecuteOpArgs(x)); - - public static Tensor _any(Tx input, Ty axis, bool keep_dims = false, string name = null) - { - var _op = tf.OpDefLib._apply_op_helper("Any", name, new { input, reduction_indices = axis, keep_dims }); - - return _op.outputs[0]; - } - - public static Tensor _max(Tx input, Ty axis, bool keep_dims = false, string name = null) - => tf.Context.ExecuteOp("Max", name, new ExecuteOpArgs(input, axis) - { - GetGradientAttrs = (op) => new - { - T = op.get_attr("T"), - keep_dims = op.get_attr("keep_dims"), - Tidx = op.get_attr("Tidx") - } - }.SetAttributes(new { keep_dims, reduction_indices = axis })); - - public static Tensor _min(Tx input, Ty axis, bool keep_dims = false, string name = null) - => tf.Context.ExecuteOp("Min", name, new ExecuteOpArgs(input, axis) - { - GetGradientAttrs = (op) => new - { - T = op.get_attr("T"), - keep_dims = op.get_attr("keep_dims"), - Tidx = op.get_attr("Tidx") - } - }.SetAttributes(new { keep_dims, reduction_indices = axis })); - - public static Tensor pow(Tx x, Ty y, string name = null) - => tf.Context.ExecuteOp("Pow", name, new ExecuteOpArgs(x, y)); - - public static Tensor _sum(Tx input, Ty axis = default, bool keep_dims = false, string name = null) - => tf.Context.ExecuteOp("Sum", name, - new ExecuteOpArgs(input, axis).SetAttributes(new { keep_dims, reduction_indices = axis })); - - private static Tensor _sum_eager_fallback(Tensor[] inputs, Tensor axis, bool keep_dims = false, string name = null, Context ctx = null) - { - var (_attr_T, input) = tf.Runner.ArgsToMatchingEager(ctx, args: new[] { inputs }); - var (_attr_Tidx, axis1) = tf.Runner.ArgsToMatchingEager(ctx, tf.int32, new[] { axis }); - var _inputs_flat = input.concat(axis1); - var _attrs = new object[] { "keep_dims", keep_dims, "T", _attr_T, "Tidx", _attr_Tidx }; - - return tf.Runner.Execute(ctx, "Sum", 1, _inputs_flat, _attrs, name: name)[0]; - } - - /// - /// Creates a sequence of numbers. - /// - /// - /// - /// - /// - /// - public static Tensor range(Tensor start, Tensor limit, Tensor delta, string name = null) - => tf.Context.ExecuteOp("Range", name, new ExecuteOpArgs(start, limit, delta)); - - /// - /// Rounds the values of a tensor to the nearest integer, element-wise. - /// - /// - /// - /// - /// If specified, the created operation in the graph will be this one, otherwise it will be named 'Round'. - /// - /// - /// The Operation can be fetched from the resulting Tensor, by fetching the Operation property from the result. - /// - /// - /// Rounds half to even. Also known as bankers rounding. If you want to round - /// according to the current system rounding mode use std::cint. - /// - public static Tensor round(Tensor x, string name = "Round") - => tf.Context.ExecuteOp("Round", name, new ExecuteOpArgs(x)); - - /// - /// Computes reciprocal of square root of x element-wise. - /// - /// - /// - /// - public static Tensor rsqrt(Tensor x, string name = null) - => tf.Context.ExecuteOp("Rsqrt", name, new ExecuteOpArgs(x)); - - /// - /// Returns the fraction of zeros in value. - /// - /// A tensor of numeric type. - /// A name for the operation (optional). - /// The fraction of zeros in value, with type float32. - public static Tensor zero_fraction(Tensor value, string name = null) - => tf.Context.ExecuteOp("zero_fraction", name, new ExecuteOpArgs(value)); + public static Tensor arg_max_eager_fallback(Tensor input, Tensor dimension, TF_DataType output_type, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, dimension }; + object[] _attrs = new object[] { "T", input.dtype, "Tidx", dimension.dtype, "output_type", output_type }; + var _result = _execute.execute("ArgMax", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("ArgMax", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns the index with the smallest value across dimensions of a tensor. + /// + /// + /// + /// Note that in case of ties the identity of the return value is not guaranteed. + /// + /// Usage: + /// ```python + /// import tensorflow as tf + /// a = [1, 10, 26.9, 2.8, 166.32, 62.3] + /// b = tf.math.argmin(input = a) + /// c = tf.keras.backend.eval(b) + /// # c = 0 + /// # here a[0] = 1 which is the smallest element of a across axis 0 + /// ``` + /// + /// + /// + /// + /// + /// + public static Tensor arg_min(Tensor input, Tensor dimension, TF_DataType output_type = TF_DataType.TF_INT64, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "ArgMin", name) { args = new object[] { input, dimension }, attrs = new Dictionary() { ["output_type"] = output_type } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return arg_min_eager_fallback(input, dimension, output_type: output_type, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["dimension"] = dimension; + keywords["output_type"] = output_type; + var _op = tf.OpDefLib._apply_op_helper("ArgMin", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "Tidx", _op._get_attr_type("Tidx"), "output_type", _op._get_attr_type("output_type") }; + _execute.record_gradient("ArgMin", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor arg_min_eager_fallback(Tensor input, Tensor dimension, TF_DataType output_type, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, dimension }; + object[] _attrs = new object[] { "T", input.dtype, "Tidx", dimension.dtype, "output_type", output_type }; + var _result = _execute.execute("ArgMin", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("ArgMin", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes the trignometric inverse sine of x element-wise. + /// + /// + /// + /// The `tf.math.asin` operation returns the inverse of `tf.math.sin`, such that + /// if `y = tf.math.sin(x)` then, `x = tf.math.asin(y)`. + /// + /// **Note**: The output of `tf.math.asin` will lie within the invertible range + /// of sine, i.e [-pi/2, pi/2]. + /// + /// For example: + /// + /// ```python + /// # Note: [1.047, 0.785] ~= [(pi/3), (pi/4)] + /// x = tf.constant([1.047, 0.785]) + /// y = tf.math.sin(x) # [0.8659266, 0.7068252] + /// + /// tf.math.asin(y) # [1.047, 0.785] = x + /// ``` + /// + /// + /// + /// + /// + public static Tensor asin(Tensor x, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Asin", name) { args = new object[] { x }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return asin_eager_fallback(x, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + var _op = tf.OpDefLib._apply_op_helper("Asin", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Asin", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor asin_eager_fallback(Tensor x, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("Asin", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Asin", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes inverse hyperbolic sine of x element-wise. + /// + /// + /// + /// Given an input tensor, this function computes inverse hyperbolic sine + /// for every element in the tensor. Both input and output has a range of + /// `[-inf, inf]`. + /// + /// ```python + /// x = tf.constant([-float("inf"), -2, -0.5, 1, 1.2, 200, 10000, float("inf")]) + /// tf.math.asinh(x) ==> [-inf -1.4436355 -0.4812118 0.8813736 1.0159732 5.991471 9.903487 inf] + /// ``` + /// + /// + /// + /// + public static Tensor asinh(Tensor x, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Asinh", name) { args = new object[] { x }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return asinh_eager_fallback(x, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + var _op = tf.OpDefLib._apply_op_helper("Asinh", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Asinh", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor asinh_eager_fallback(Tensor x, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("Asinh", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Asinh", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes the trignometric inverse tangent of x element-wise. + /// + /// + /// + /// The `tf.math.atan` operation returns the inverse of `tf.math.tan`, such that + /// if `y = tf.math.tan(x)` then, `x = tf.math.atan(y)`. + /// + /// **Note**: The output of `tf.math.atan` will lie within the invertible range + /// of tan, i.e (-pi/2, pi/2). + /// + /// For example: + /// + /// ```python + /// # Note: [1.047, 0.785] ~= [(pi/3), (pi/4)] + /// x = tf.constant([1.047, 0.785]) + /// y = tf.math.tan(x) # [1.731261, 0.99920404] + /// + /// tf.math.atan(y) # [1.047, 0.785] = x + /// ``` + /// + /// + /// + /// + /// + public static Tensor atan(Tensor x, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Atan", name) { args = new object[] { x }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return atan_eager_fallback(x, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + var _op = tf.OpDefLib._apply_op_helper("Atan", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Atan", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor atan_eager_fallback(Tensor x, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("Atan", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Atan", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes arctangent of `y/x` element-wise, respecting signs of the arguments. + /// + /// + /// + /// This is the angle \( heta in [-pi, pi] \) such that + /// \[ x = r cos( heta) \] + /// and + /// \[ y = r sin( heta) \] + /// where \(r = sqrt{x^2 + y^2} \). + /// + /// For example: + /// + /// >>> x = [1., 1.] + /// >>> y = [1., -1.] + /// >>> print((tf.math.atan2(y,x) * (180 / np.pi)).numpy()) + /// [ 45. -45.] + /// + /// + /// + /// + /// + /// + /// + public static Tensor atan2(Tensor y, Tensor x, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Atan2", name) { args = new object[] { y, x }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return atan2_eager_fallback(y, x, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["y"] = y; + keywords["x"] = x; + var _op = tf.OpDefLib._apply_op_helper("Atan2", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Atan2", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor atan2_eager_fallback(Tensor y, Tensor x, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { y, x }; + object[] _attrs = new object[] { "T", y.dtype }; + var _result = _execute.execute("Atan2", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Atan2", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes inverse hyperbolic tangent of x element-wise. + /// + /// + /// + /// Given an input tensor, this function computes inverse hyperbolic tangent + /// for every element in the tensor. Input range is `[-1,1]` and output range is + /// `[-inf, inf]`. If input is `-1`, output will be `-inf` and if the + /// input is `1`, output will be `inf`. Values outside the range will have + /// `nan` as output. + /// + /// ```python + /// x = tf.constant([-float("inf"), -1, -0.5, 1, 0, 0.5, 10, float("inf")]) + /// tf.math.atanh(x) ==> [nan -inf -0.54930615 inf 0. 0.54930615 nan nan] + /// ``` + /// + /// + /// + /// + public static Tensor atanh(Tensor x, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Atanh", name) { args = new object[] { x }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return atanh_eager_fallback(x, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + var _op = tf.OpDefLib._apply_op_helper("Atanh", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Atanh", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor atanh_eager_fallback(Tensor x, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("Atanh", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Atanh", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Multiplies slices of two tensors in batches. + /// + /// + /// + /// Multiplies all slices of `Tensor` `x` and `y` (each slice can be + /// viewed as an element of a batch), and arranges the individual results + /// in a single output tensor of the same batch size. Each of the + /// individual slices can optionally be adjointed (to adjoint a matrix + /// means to transpose and conjugate it) before multiplication by setting + /// the `adj_x` or `adj_y` flag to `True`, which are by default `False`. + /// + /// The input tensors `x` and `y` are 2-D or higher with shape `[..., r_x, c_x]` + /// and `[..., r_y, c_y]`. + /// + /// The output tensor is 2-D or higher with shape `[..., r_o, c_o]`, where: + /// + /// r_o = c_x if adj_x else r_x + /// c_o = r_y if adj_y else c_y + /// + /// It is computed as: + /// + /// output[..., :, :] = matrix(x[..., :, :]) * matrix(y[..., :, :]) + /// + /// + /// + /// + /// + /// + /// If `True`, adjoint the slices of `x`. Defaults to `False`. + /// + /// + /// + /// + /// If `True`, adjoint the slices of `y`. Defaults to `False`. + /// + /// + /// + public static Tensor batch_mat_mul(Tensor x, Tensor y, bool adj_x = false, bool adj_y = false, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "BatchMatMul", name) { args = new object[] { x, y }, attrs = new Dictionary() { ["adj_x"] = adj_x, ["adj_y"] = adj_y } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return batch_mat_mul_eager_fallback(x, y, adj_x: adj_x, adj_y: adj_y, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + keywords["y"] = y; + keywords["adj_x"] = adj_x; + keywords["adj_y"] = adj_y; + var _op = tf.OpDefLib._apply_op_helper("BatchMatMul", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "adj_x", _op._get_attr_bool("adj_x"), "adj_y", _op._get_attr_bool("adj_y") }; + _execute.record_gradient("BatchMatMul", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor batch_mat_mul_eager_fallback(Tensor x, Tensor y, bool adj_x, bool adj_y, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x, y }; + object[] _attrs = new object[] { "T", x.dtype, "adj_x", adj_x, "adj_y", adj_y }; + var _result = _execute.execute("BatchMatMul", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("BatchMatMul", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Multiplies slices of two tensors in batches. + /// + /// + /// + /// Multiplies all slices of `Tensor` `x` and `y` (each slice can be + /// viewed as an element of a batch), and arranges the individual results + /// in a single output tensor of the same batch size. Each of the + /// individual slices can optionally be adjointed (to adjoint a matrix + /// means to transpose and conjugate it) before multiplication by setting + /// the `adj_x` or `adj_y` flag to `True`, which are by default `False`. + /// + /// The input tensors `x` and `y` are 2-D or higher with shape `[..., r_x, c_x]` + /// and `[..., r_y, c_y]`. + /// + /// The output tensor is 2-D or higher with shape `[..., r_o, c_o]`, where: + /// + /// r_o = c_x if adj_x else r_x + /// c_o = r_y if adj_y else c_y + /// + /// It is computed as: + /// + /// output[..., :, :] = matrix(x[..., :, :]) * matrix(y[..., :, :]) + /// + /// *NOTE*: `BatchMatMulV2` supports broadcasting in the batch dimensions. More + /// about broadcasting + /// [here](http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html). + /// + /// + /// + /// + /// + /// + /// + /// If `True`, adjoint the slices of `x`. Defaults to `False`. + /// + /// + /// + /// + /// If `True`, adjoint the slices of `y`. Defaults to `False`. + /// + /// + /// + public static Tensor batch_mat_mul_v2(Tensor x, Tensor y, bool adj_x = false, bool adj_y = false, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "BatchMatMulV2", name) { args = new object[] { x, y }, attrs = new Dictionary() { ["adj_x"] = adj_x, ["adj_y"] = adj_y } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return batch_mat_mul_v2_eager_fallback(x, y, adj_x: adj_x, adj_y: adj_y, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + keywords["y"] = y; + keywords["adj_x"] = adj_x; + keywords["adj_y"] = adj_y; + var _op = tf.OpDefLib._apply_op_helper("BatchMatMulV2", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "adj_x", _op._get_attr_bool("adj_x"), "adj_y", _op._get_attr_bool("adj_y") }; + _execute.record_gradient("BatchMatMulV2", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor batch_mat_mul_v2_eager_fallback(Tensor x, Tensor y, bool adj_x, bool adj_y, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x, y }; + object[] _attrs = new object[] { "T", x.dtype, "adj_x", adj_x, "adj_y", adj_y }; + var _result = _execute.execute("BatchMatMulV2", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("BatchMatMulV2", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Multiplies slices of two tensors in batches. + /// + /// + /// + /// Multiplies all slices of `Tensor` `x` and `y` (each slice can be + /// viewed as an element of a batch), and arranges the individual results + /// in a single output tensor of the same batch size. Each of the + /// individual slices can optionally be adjointed (to adjoint a matrix + /// means to transpose and conjugate it) before multiplication by setting + /// the `adj_x` or `adj_y` flag to `True`, which are by default `False`. + /// + /// The input tensors `x` and `y` are 2-D or higher with shape `[..., r_x, c_x]` + /// and `[..., r_y, c_y]`. + /// + /// The output tensor is 2-D or higher with shape `[..., r_o, c_o]`, where: + /// + /// r_o = c_x if adj_x else r_x + /// c_o = r_y if adj_y else c_y + /// + /// It is computed as: + /// + /// output[..., :, :] = matrix(x[..., :, :]) * matrix(y[..., :, :]) + /// + /// *NOTE*: `BatchMatMulV3` supports broadcasting in the batch dimensions. More + /// about broadcasting + /// [here](http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html). + /// + /// + /// + /// + /// + /// + /// + /// If not spcified, Tout is the same type to input type. + /// + /// + /// + /// + /// If `True`, adjoint the slices of `x`. Defaults to `False`. + /// + /// + /// + /// + /// If `True`, adjoint the slices of `y`. Defaults to `False`. + /// + /// + /// + public static Tensor batch_mat_mul_v3(Tensor x, Tensor y, TF_DataType Tout, bool adj_x = false, bool adj_y = false, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "BatchMatMulV3", name) { args = new object[] { x, y }, attrs = new Dictionary() { ["Tout"] = Tout, ["adj_x"] = adj_x, ["adj_y"] = adj_y } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return batch_mat_mul_v3_eager_fallback(x, y, Tout: Tout, adj_x: adj_x, adj_y: adj_y, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + keywords["y"] = y; + keywords["Tout"] = Tout; + keywords["adj_x"] = adj_x; + keywords["adj_y"] = adj_y; + var _op = tf.OpDefLib._apply_op_helper("BatchMatMulV3", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "Ta", _op._get_attr_type("Ta"), "Tb", _op._get_attr_type("Tb"), "Tout", _op._get_attr_type("Tout"), "adj_x", _op._get_attr_bool("adj_x"), "adj_y", _op._get_attr_bool("adj_y") }; + _execute.record_gradient("BatchMatMulV3", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor batch_mat_mul_v3_eager_fallback(Tensor x, Tensor y, TF_DataType Tout, bool adj_x, bool adj_y, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x, y }; + object[] _attrs = new object[] { "Ta", x.dtype, "Tb", y.dtype, "Tout", Tout, "adj_x", adj_x, "adj_y", adj_y }; + var _result = _execute.execute("BatchMatMulV3", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("BatchMatMulV3", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Compute the regularized incomplete beta integral \\(I_x(a, b)\\). + /// + /// + /// + /// The regularized incomplete beta integral is defined as: + /// + /// + /// \(I_x(a, b) = rac{B(x; a, b)}{B(a, b)}\) + /// + /// where + /// + /// + /// \(B(x; a, b) = int_0^x t^{a-1} (1 - t)^{b-1} dt\) + /// + /// + /// is the incomplete beta function and \(B(a, b)\) is the *complete* + /// beta function. + /// + /// + /// + /// + /// + /// + public static Tensor betainc(Tensor a, Tensor b, Tensor x, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Betainc", name) { args = new object[] { a, b, x }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return betainc_eager_fallback(a, b, x, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["a"] = a; + keywords["b"] = b; + keywords["x"] = x; + var _op = tf.OpDefLib._apply_op_helper("Betainc", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Betainc", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor betainc_eager_fallback(Tensor a, Tensor b, Tensor x, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { a, b, x }; + object[] _attrs = new object[] { "T", a.dtype }; + var _result = _execute.execute("Betainc", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Betainc", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Counts the number of occurrences of each value in an integer array. + /// + /// + /// + /// Outputs a vector with length `size` and the same dtype as `weights`. If + /// `weights` are empty, then index `i` stores the number of times the value `i` is + /// counted in `arr`. If `weights` are non-empty, then index `i` stores the sum of + /// the value in `weights` at each index where the corresponding value in `arr` is + /// `i`. + /// + /// Values in `arr` outside of the range [0, size) are ignored. + /// + /// + /// + /// + /// + /// + public static Tensor bincount(Tensor arr, Tensor size, Tensor weights, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Bincount", name) { args = new object[] { arr, size, weights }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return bincount_eager_fallback(arr, size, weights, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["arr"] = arr; + keywords["size"] = size; + keywords["weights"] = weights; + var _op = tf.OpDefLib._apply_op_helper("Bincount", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Bincount", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor bincount_eager_fallback(Tensor arr, Tensor size, Tensor weights, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { arr, size, weights }; + object[] _attrs = new object[] { "T", weights.dtype }; + var _result = _execute.execute("Bincount", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Bincount", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Bucketizes 'input' based on 'boundaries'. + /// + /// + /// + /// For example, if the inputs are + /// boundaries = [0, 10, 100] + /// input = [[-5, 10000] + /// [150, 10] + /// [5, 100]] + /// + /// then the output will be + /// output = [[0, 3] + /// [3, 2] + /// [1, 3]] + /// + /// + /// + /// + /// + /// A sorted list of floats gives the boundary of the buckets. + /// + /// + /// + public static Tensor bucketize(Tensor input, float[] boundaries, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Bucketize", name) { args = new object[] { input }, attrs = new Dictionary() { ["boundaries"] = boundaries } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return bucketize_eager_fallback(input, boundaries: boundaries, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["boundaries"] = boundaries; + var _op = tf.OpDefLib._apply_op_helper("Bucketize", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "boundaries", _op.get_attr("boundaries") }; + _execute.record_gradient("Bucketize", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor bucketize_eager_fallback(Tensor input, float[] boundaries, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input }; + object[] _attrs = new object[] { "T", input.dtype, "boundaries", boundaries }; + var _result = _execute.execute("Bucketize", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Bucketize", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Cast x of type SrcT to y of DstT. + /// + /// + /// + /// + /// + public static Tensor cast(Tensor x, TF_DataType DstT, bool Truncate = false, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Cast", name) { args = new object[] { x }, attrs = new Dictionary() { ["DstT"] = DstT, ["Truncate"] = Truncate } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return cast_eager_fallback(x, DstT: DstT, Truncate: Truncate, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + keywords["DstT"] = DstT; + keywords["Truncate"] = Truncate; + var _op = tf.OpDefLib._apply_op_helper("Cast", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "SrcT", _op._get_attr_type("SrcT"), "DstT", _op._get_attr_type("DstT"), "Truncate", _op._get_attr_bool("Truncate") }; + _execute.record_gradient("Cast", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor cast_eager_fallback(Tensor x, TF_DataType DstT, bool Truncate, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x }; + object[] _attrs = new object[] { "SrcT", x.dtype, "DstT", DstT, "Truncate", Truncate }; + var _result = _execute.execute("Cast", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Cast", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns element-wise smallest integer not less than x. + /// + /// + /// + public static Tensor ceil(Tensor x, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Ceil", name) { args = new object[] { x }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return ceil_eager_fallback(x, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + var _op = tf.OpDefLib._apply_op_helper("Ceil", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Ceil", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor ceil_eager_fallback(Tensor x, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("Ceil", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Ceil", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Clips tensor values to a specified min and max. + /// + /// + /// + /// Given a tensor `t`, this operation returns a tensor of the same type and + /// shape as `t` with its values clipped to `clip_value_min` and `clip_value_max`. + /// Any values less than `clip_value_min` are set to `clip_value_min`. Any values + /// greater than `clip_value_max` are set to `clip_value_max`. + /// + /// + /// + /// + /// + /// + public static Tensor clip_by_value(Tensor t, Tensor clip_value_min, Tensor clip_value_max, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "ClipByValue", name) { args = new object[] { t, clip_value_min, clip_value_max }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return clip_by_value_eager_fallback(t, clip_value_min, clip_value_max, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["t"] = t; + keywords["clip_value_min"] = clip_value_min; + keywords["clip_value_max"] = clip_value_max; + var _op = tf.OpDefLib._apply_op_helper("ClipByValue", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("ClipByValue", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor clip_by_value_eager_fallback(Tensor t, Tensor clip_value_min, Tensor clip_value_max, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { t, clip_value_min, clip_value_max }; + object[] _attrs = new object[] { "T", t.dtype }; + var _result = _execute.execute("ClipByValue", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("ClipByValue", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Converts two real numbers to a complex number. + /// + /// + /// + /// Given a tensor `real` representing the real part of a complex number, and a + /// tensor `imag` representing the imaginary part of a complex number, this + /// operation returns complex numbers elementwise of the form \(a + bj\), where + /// *a* represents the `real` part and *b* represents the `imag` part. + /// + /// The input tensors `real` and `imag` must have the same shape. + /// + /// For example: + /// + /// ``` + /// # tensor 'real' is [2.25, 3.25] + /// # tensor `imag` is [4.75, 5.75] + /// tf.complex(real, imag) ==> [[2.25 + 4.75j], [3.25 + 5.75j]] + /// ``` + /// + /// + /// + /// + /// + /// + public static Tensor complex(Tensor real, Tensor imag, TF_DataType Tout = TF_DataType.TF_COMPLEX64, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Complex", name) { args = new object[] { real, imag }, attrs = new Dictionary() { ["Tout"] = Tout } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return complex_eager_fallback(real, imag, Tout: Tout, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["real"] = real; + keywords["imag"] = imag; + keywords["Tout"] = Tout; + var _op = tf.OpDefLib._apply_op_helper("Complex", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "Tout", _op._get_attr_type("Tout") }; + _execute.record_gradient("Complex", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor complex_eager_fallback(Tensor real, Tensor imag, TF_DataType Tout, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { real, imag }; + object[] _attrs = new object[] { "T", real.dtype, "Tout", Tout }; + var _result = _execute.execute("Complex", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Complex", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes the complex absolute value of a tensor. + /// + /// + /// + /// Given a tensor `x` of complex numbers, this operation returns a tensor of type + /// `float` or `double` that is the absolute value of each element in `x`. All + /// elements in `x` must be complex numbers of the form \(a + bj\). The absolute + /// value is computed as \( sqrt{a^2 + b^2}\). + /// + /// For example: + /// + /// >>> x = tf.complex(3.0, 4.0) + /// >>> print((tf.raw_ops.ComplexAbs(x=x, Tout=tf.dtypes.float32, name=None)).numpy()) + /// 5.0 + /// + /// + /// + /// + /// + /// + public static Tensor complex_abs(Tensor x, TF_DataType Tout = TF_DataType.TF_FLOAT, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "ComplexAbs", name) { args = new object[] { x }, attrs = new Dictionary() { ["Tout"] = Tout } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return complex_abs_eager_fallback(x, Tout: Tout, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + keywords["Tout"] = Tout; + var _op = tf.OpDefLib._apply_op_helper("ComplexAbs", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "Tout", _op._get_attr_type("Tout") }; + _execute.record_gradient("ComplexAbs", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor complex_abs_eager_fallback(Tensor x, TF_DataType Tout, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x }; + object[] _attrs = new object[] { "T", x.dtype, "Tout", Tout }; + var _result = _execute.execute("ComplexAbs", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("ComplexAbs", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns the complex conjugate of a complex number. + /// + /// + /// + /// Given a tensor `input` of complex numbers, this operation returns a tensor of + /// complex numbers that are the complex conjugate of each element in `input`. The + /// complex numbers in `input` must be of the form \(a + bj\), where *a* is the + /// real part and *b* is the imaginary part. + /// + /// The complex conjugate returned by this operation is of the form \(a - bj\). + /// + /// For example: + /// + /// ``` + /// # tensor 'input' is [-2.25 + 4.75j, 3.25 + 5.75j] + /// tf.conj(input) ==> [-2.25 - 4.75j, 3.25 - 5.75j] + /// ``` + /// + /// + /// + /// + public static Tensor conj(Tensor input, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Conj", name) { args = new object[] { input }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return conj_eager_fallback(input, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + var _op = tf.OpDefLib._apply_op_helper("Conj", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Conj", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor conj_eager_fallback(Tensor input, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input }; + object[] _attrs = new object[] { "T", input.dtype }; + var _result = _execute.execute("Conj", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Conj", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes cos of x element-wise. + /// + /// + /// + /// Given an input tensor, this function computes cosine of every + /// element in the tensor. Input range is `(-inf, inf)` and + /// output range is `[-1,1]`. If input lies outside the boundary, `nan` + /// is returned. + /// + /// ```python + /// x = tf.constant([-float("inf"), -9, -0.5, 1, 1.2, 200, 10000, float("inf")]) + /// tf.math.cos(x) ==> [nan -0.91113025 0.87758255 0.5403023 0.36235774 0.48718765 -0.95215535 nan] + /// ``` + /// + /// + /// + /// + public static Tensor cos(Tensor x, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Cos", name) { args = new object[] { x }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return cos_eager_fallback(x, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + var _op = tf.OpDefLib._apply_op_helper("Cos", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Cos", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor cos_eager_fallback(Tensor x, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("Cos", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Cos", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes hyperbolic cosine of x element-wise. + /// + /// + /// + /// Given an input tensor, this function computes hyperbolic cosine of every + /// element in the tensor. Input range is `[-inf, inf]` and output range + /// is `[1, inf]`. + /// + /// ```python + /// x = tf.constant([-float("inf"), -9, -0.5, 1, 1.2, 2, 10, float("inf")]) + /// tf.math.cosh(x) ==> [inf 4.0515420e+03 1.1276259e+00 1.5430807e+00 1.8106556e+00 3.7621956e+00 1.1013233e+04 inf] + /// ``` + /// + /// + /// + /// + public static Tensor cosh(Tensor x, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Cosh", name) { args = new object[] { x }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return cosh_eager_fallback(x, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + var _op = tf.OpDefLib._apply_op_helper("Cosh", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Cosh", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor cosh_eager_fallback(Tensor x, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("Cosh", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Cosh", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Compute the pairwise cross product. + /// + /// + /// + /// `a` and `b` must be the same shape; they can either be simple 3-element vectors, + /// or any shape where the innermost dimension is 3. In the latter case, each pair + /// of corresponding 3-element vectors is cross-multiplied independently. + /// + /// + /// + /// + /// + public static Tensor cross(Tensor a, Tensor b, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Cross", name) { args = new object[] { a, b }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return cross_eager_fallback(a, b, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["a"] = a; + keywords["b"] = b; + var _op = tf.OpDefLib._apply_op_helper("Cross", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Cross", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor cross_eager_fallback(Tensor a, Tensor b, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { a, b }; + object[] _attrs = new object[] { "T", a.dtype }; + var _result = _execute.execute("Cross", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Cross", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Compute the cumulative product of the tensor `x` along `axis`. + /// + /// + /// + /// By default, this op performs an inclusive cumprod, which means that the first + /// element of the input is identical to the first element of the output: + /// + /// ```python + /// tf.cumprod([a, b, c]) # => [a, a * b, a * b * c] + /// ``` + /// + /// By setting the `exclusive` kwarg to `True`, an exclusive cumprod is + /// performed instead: + /// + /// ```python + /// tf.cumprod([a, b, c], exclusive=True) # => [1, a, a * b] + /// ``` + /// + /// By setting the `reverse` kwarg to `True`, the cumprod is performed in the + /// opposite direction: + /// + /// ```python + /// tf.cumprod([a, b, c], reverse=True) # => [a * b * c, b * c, c] + /// ``` + /// + /// This is more efficient than using separate `tf.reverse` ops. + /// + /// The `reverse` and `exclusive` kwargs can also be combined: + /// + /// ```python + /// tf.cumprod([a, b, c], exclusive=True, reverse=True) # => [b * c, c, 1] + /// ``` + /// + /// + /// + /// + /// + /// + /// If `True`, perform exclusive cumprod. + /// + /// + /// + /// + /// A `bool` (default: False). + /// + /// + /// + public static Tensor cumprod(Tensor x, Tensor axis, bool exclusive = false, bool reverse = false, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Cumprod", name) { args = new object[] { x, axis }, attrs = new Dictionary() { ["exclusive"] = exclusive, ["reverse"] = reverse } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return cumprod_eager_fallback(x, axis, exclusive: exclusive, reverse: reverse, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + keywords["axis"] = axis; + keywords["exclusive"] = exclusive; + keywords["reverse"] = reverse; + var _op = tf.OpDefLib._apply_op_helper("Cumprod", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "exclusive", _op._get_attr_bool("exclusive"), "reverse", _op._get_attr_bool("reverse"), "T", _op._get_attr_type("T"), "Tidx", _op._get_attr_type("Tidx") }; + _execute.record_gradient("Cumprod", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor cumprod_eager_fallback(Tensor x, Tensor axis, bool exclusive, bool reverse, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x, axis }; + object[] _attrs = new object[] { "exclusive", exclusive, "reverse", reverse, "T", x.dtype, "Tidx", axis.dtype }; + var _result = _execute.execute("Cumprod", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Cumprod", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Compute the cumulative sum of the tensor `x` along `axis`. + /// + /// + /// + /// By default, this op performs an inclusive cumsum, which means that the first + /// element of the input is identical to the first element of the output: + /// + /// ```python + /// tf.cumsum([a, b, c]) # => [a, a + b, a + b + c] + /// ``` + /// + /// By setting the `exclusive` kwarg to `True`, an exclusive cumsum is + /// performed instead: + /// + /// ```python + /// tf.cumsum([a, b, c], exclusive=True) # => [0, a, a + b] + /// ``` + /// + /// By setting the `reverse` kwarg to `True`, the cumsum is performed in the + /// opposite direction: + /// + /// ```python + /// tf.cumsum([a, b, c], reverse=True) # => [a + b + c, b + c, c] + /// ``` + /// + /// This is more efficient than using separate `tf.reverse` ops. + /// + /// The `reverse` and `exclusive` kwargs can also be combined: + /// + /// ```python + /// tf.cumsum([a, b, c], exclusive=True, reverse=True) # => [b + c, c, 0] + /// ``` + /// + /// + /// + /// + /// + /// + /// If `True`, perform exclusive cumsum. + /// + /// + /// + /// + /// A `bool` (default: False). + /// + /// + /// + public static Tensor cumsum(Tensor x, Tensor axis, bool exclusive = false, bool reverse = false, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Cumsum", name) { args = new object[] { x, axis }, attrs = new Dictionary() { ["exclusive"] = exclusive, ["reverse"] = reverse } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return cumsum_eager_fallback(x, axis, exclusive: exclusive, reverse: reverse, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + keywords["axis"] = axis; + keywords["exclusive"] = exclusive; + keywords["reverse"] = reverse; + var _op = tf.OpDefLib._apply_op_helper("Cumsum", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "exclusive", _op._get_attr_bool("exclusive"), "reverse", _op._get_attr_bool("reverse"), "T", _op._get_attr_type("T"), "Tidx", _op._get_attr_type("Tidx") }; + _execute.record_gradient("Cumsum", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor cumsum_eager_fallback(Tensor x, Tensor axis, bool exclusive, bool reverse, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x, axis }; + object[] _attrs = new object[] { "exclusive", exclusive, "reverse", reverse, "T", x.dtype, "Tidx", axis.dtype }; + var _result = _execute.execute("Cumsum", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Cumsum", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Compute the cumulative product of the tensor `x` along `axis`. + /// + /// + /// + /// By default, this op performs an inclusive cumulative log-sum-exp, + /// which means that the first + /// element of the input is identical to the first element of the output: + /// ```python + /// tf.math.cumulative_logsumexp([a, b, c]) # => [a, log(exp(a) + exp(b)), log(exp(a) + exp(b) + exp(c))] + /// ``` + /// + /// By setting the `exclusive` kwarg to `True`, an exclusive cumulative log-sum-exp is + /// performed instead: + /// ```python + /// tf.cumulative_logsumexp([a, b, c], exclusive=True) # => [-inf, a, log(exp(a) * exp(b))] + /// ``` + /// Note that the neutral element of the log-sum-exp operation is `-inf`, + /// however, for performance reasons, the minimal value representable by the + /// floating point type is used instead. + /// + /// By setting the `reverse` kwarg to `True`, the cumulative log-sum-exp is performed in the + /// opposite direction. + /// + /// + /// + /// + /// + /// + /// If `True`, perform exclusive cumulative log-sum-exp. + /// + /// + /// + /// + /// A `bool` (default: False). + /// + /// + /// + public static Tensor cumulative_logsumexp(Tensor x, Tensor axis, bool exclusive = false, bool reverse = false, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "CumulativeLogsumexp", name) { args = new object[] { x, axis }, attrs = new Dictionary() { ["exclusive"] = exclusive, ["reverse"] = reverse } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return cumulative_logsumexp_eager_fallback(x, axis, exclusive: exclusive, reverse: reverse, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + keywords["axis"] = axis; + keywords["exclusive"] = exclusive; + keywords["reverse"] = reverse; + var _op = tf.OpDefLib._apply_op_helper("CumulativeLogsumexp", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "exclusive", _op._get_attr_bool("exclusive"), "reverse", _op._get_attr_bool("reverse"), "T", _op._get_attr_type("T"), "Tidx", _op._get_attr_type("Tidx") }; + _execute.record_gradient("CumulativeLogsumexp", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor cumulative_logsumexp_eager_fallback(Tensor x, Tensor axis, bool exclusive, bool reverse, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x, axis }; + object[] _attrs = new object[] { "exclusive", exclusive, "reverse", reverse, "T", x.dtype, "Tidx", axis.dtype }; + var _result = _execute.execute("CumulativeLogsumexp", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("CumulativeLogsumexp", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Counts the number of occurrences of each value in an integer array. + /// + /// + /// + /// Outputs a vector with length `size` and the same dtype as `weights`. If + /// `weights` are empty, then index `i` stores the number of times the value `i` is + /// counted in `arr`. If `weights` are non-empty, then index `i` stores the sum of + /// the value in `weights` at each index where the corresponding value in `arr` is + /// `i`. + /// + /// Values in `arr` outside of the range [0, size) are ignored. + /// + /// + /// + /// + /// + /// + /// + /// bool; Whether the kernel should count the appearance or number of occurrences. + /// + /// + /// + public static Tensor dense_bincount(Tensor input, Tensor size, Tensor weights, bool binary_output = false, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "DenseBincount", name) { args = new object[] { input, size, weights }, attrs = new Dictionary() { ["binary_output"] = binary_output } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return dense_bincount_eager_fallback(input, size, weights, binary_output: binary_output, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["size"] = size; + keywords["weights"] = weights; + keywords["binary_output"] = binary_output; + var _op = tf.OpDefLib._apply_op_helper("DenseBincount", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "Tidx", _op._get_attr_type("Tidx"), "T", _op._get_attr_type("T"), "binary_output", _op._get_attr_bool("binary_output") }; + _execute.record_gradient("DenseBincount", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor dense_bincount_eager_fallback(Tensor input, Tensor size, Tensor weights, bool binary_output, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, size, weights }; + object[] _attrs = new object[] { "Tidx", input.dtype, "T", weights.dtype, "binary_output", binary_output }; + var _result = _execute.execute("DenseBincount", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("DenseBincount", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes Psi, the derivative of Lgamma (the log of the absolute value of + /// + /// + /// + /// `Gamma(x)`), element-wise. + /// + /// + /// + /// + public static Tensor digamma(Tensor x, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Digamma", name) { args = new object[] { x }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return digamma_eager_fallback(x, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + var _op = tf.OpDefLib._apply_op_helper("Digamma", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Digamma", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor digamma_eager_fallback(Tensor x, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("Digamma", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Digamma", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns x / y element-wise. + /// + /// + /// + /// *NOTE*: `Div` supports broadcasting. More about broadcasting + /// [here](http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html) + /// + /// + /// + /// + /// + public static Tensor div(Tensor x, Tensor y, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Div", name) { args = new object[] { x, y }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return div_eager_fallback(x, y, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + keywords["y"] = y; + var _op = tf.OpDefLib._apply_op_helper("Div", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Div", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor div_eager_fallback(Tensor x, Tensor y, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x, y }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("Div", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Div", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns 0 if the denominator is zero. + /// + /// + /// + /// + /// *NOTE*: `DivNoNan` supports broadcasting. More about broadcasting + /// [here](http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html) + /// + /// + /// + /// + /// + public static Tensor div_no_nan(Tensor x, Tensor y, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "DivNoNan", name) { args = new object[] { x, y }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return div_no_nan_eager_fallback(x, y, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + keywords["y"] = y; + var _op = tf.OpDefLib._apply_op_helper("DivNoNan", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("DivNoNan", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor div_no_nan_eager_fallback(Tensor x, Tensor y, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x, y }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("DivNoNan", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("DivNoNan", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns the truth value of (x == y) element-wise. + /// + /// + /// + /// *NOTE*: `Equal` supports broadcasting. More about broadcasting + /// [here](http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html) + /// + /// ```python + /// x = tf.constant([2, 4]) + /// y = tf.constant(2) + /// tf.math.equal(x, y) ==> array([True, False]) + /// + /// x = tf.constant([2, 4]) + /// y = tf.constant([2, 4]) + /// tf.math.equal(x, y) ==> array([True, True]) + /// ``` + /// + /// + /// + /// + /// + /// + public static Tensor equal(Tensor x, Tensor y, bool incompatible_shape_error = true, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Equal", name) { args = new object[] { x, y }, attrs = new Dictionary() { ["incompatible_shape_error"] = incompatible_shape_error } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return equal_eager_fallback(x, y, incompatible_shape_error: incompatible_shape_error, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + keywords["y"] = y; + keywords["incompatible_shape_error"] = incompatible_shape_error; + var _op = tf.OpDefLib._apply_op_helper("Equal", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "incompatible_shape_error", _op._get_attr_bool("incompatible_shape_error") }; + _execute.record_gradient("Equal", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor equal_eager_fallback(Tensor x, Tensor y, bool incompatible_shape_error, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x, y }; + object[] _attrs = new object[] { "T", x.dtype, "incompatible_shape_error", incompatible_shape_error }; + var _result = _execute.execute("Equal", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Equal", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes the [Gauss error function](https://en.wikipedia.org/wiki/Error_function) of `x` element-wise. In statistics, for non-negative values of $x$, the error function has the following interpretation: for a random variable $Y$ that is normally distributed with mean 0 and variance $1/\sqrt{2}$, $erf(x)$ is the probability that $Y$ falls in the range $[−x, x]$. + /// + /// + /// + public static Tensor erf(Tensor x, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Erf", name) { args = new object[] { x }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return erf_eager_fallback(x, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + var _op = tf.OpDefLib._apply_op_helper("Erf", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Erf", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor erf_eager_fallback(Tensor x, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("Erf", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Erf", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes the complementary error function of `x` element-wise. + /// + /// + /// + public static Tensor erfc(Tensor x, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Erfc", name) { args = new object[] { x }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return erfc_eager_fallback(x, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + var _op = tf.OpDefLib._apply_op_helper("Erfc", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Erfc", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor erfc_eager_fallback(Tensor x, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("Erfc", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Erfc", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// + /// + /// + /// + public static Tensor erfinv(Tensor x, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Erfinv", name) { args = new object[] { x }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return erfinv_eager_fallback(x, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + var _op = tf.OpDefLib._apply_op_helper("Erfinv", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Erfinv", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor erfinv_eager_fallback(Tensor x, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("Erfinv", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Erfinv", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes the euclidean norm of elements across dimensions of a tensor. + /// + /// + /// + /// Reduces `input` along the dimensions given in `reduction_indices`. Unless + /// `keep_dims` is true, the rank of the tensor is reduced by 1 for each entry in + /// `reduction_indices`. If `keep_dims` is true, the reduced dimensions are + /// retained with length 1. + /// + /// + /// + /// + /// + /// + /// If true, retain reduced dimensions with length 1. + /// + /// + /// + public static Tensor euclidean_norm(Tensor input, Tensor reduction_indices, bool keep_dims = false, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "EuclideanNorm", name) { args = new object[] { input, reduction_indices }, attrs = new Dictionary() { ["keep_dims"] = keep_dims } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return euclidean_norm_eager_fallback(input, reduction_indices, keep_dims: keep_dims, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["reduction_indices"] = reduction_indices; + keywords["keep_dims"] = keep_dims; + var _op = tf.OpDefLib._apply_op_helper("EuclideanNorm", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "keep_dims", _op._get_attr_bool("keep_dims"), "T", _op._get_attr_type("T"), "Tidx", _op._get_attr_type("Tidx") }; + _execute.record_gradient("EuclideanNorm", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor euclidean_norm_eager_fallback(Tensor input, Tensor reduction_indices, bool keep_dims, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, reduction_indices }; + object[] _attrs = new object[] { "keep_dims", keep_dims, "T", input.dtype, "Tidx", reduction_indices.dtype }; + var _result = _execute.execute("EuclideanNorm", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("EuclideanNorm", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes exponential of x element-wise. \\(y = e^x\\). + /// + /// + /// + /// This function computes the exponential of every element in the input tensor. + /// i.e. `exp(x)` or `e^(x)`, where `x` is the input tensor. + /// `e` denotes Euler's number and is approximately equal to 2.718281. + /// Output is positive for any real input. + /// + /// ```python + /// x = tf.constant(2.0) + /// tf.math.exp(x) ==> 7.389056 + /// + /// x = tf.constant([2.0, 8.0]) + /// tf.math.exp(x) ==> array([7.389056, 2980.958], dtype=float32) + /// ``` + /// + /// For complex numbers, the exponential value is calculated as follows: + /// + /// ``` + /// e^(x+iy) = e^x * e^iy = e^x * (cos y + i sin y) + /// ``` + /// + /// Let's consider complex number 1+1j as an example. + /// e^1 * (cos 1 + i sin 1) = 2.7182818284590 * (0.54030230586+0.8414709848j) + /// + /// ```python + /// x = tf.constant(1 + 1j) + /// tf.math.exp(x) ==> 1.4686939399158851+2.2873552871788423j + /// ``` + /// + /// + /// + /// + public static Tensor exp(Tensor x, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Exp", name) { args = new object[] { x }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return exp_eager_fallback(x, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + var _op = tf.OpDefLib._apply_op_helper("Exp", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Exp", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor exp_eager_fallback(Tensor x, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("Exp", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Exp", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes `exp(x) - 1` element-wise. + /// + /// + /// + /// i.e. `exp(x) - 1` or `e^(x) - 1`, where `x` is the input tensor. + /// `e` denotes Euler's number and is approximately equal to 2.718281. + /// + /// ```python + /// x = tf.constant(2.0) + /// tf.math.expm1(x) ==> 6.389056 + /// + /// x = tf.constant([2.0, 8.0]) + /// tf.math.expm1(x) ==> array([6.389056, 2979.958], dtype=float32) + /// + /// x = tf.constant(1 + 1j) + /// tf.math.expm1(x) ==> (0.46869393991588515+2.2873552871788423j) + /// ``` + /// + /// + /// + /// + public static Tensor expm1(Tensor x, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Expm1", name) { args = new object[] { x }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return expm1_eager_fallback(x, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + var _op = tf.OpDefLib._apply_op_helper("Expm1", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Expm1", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor expm1_eager_fallback(Tensor x, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("Expm1", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Expm1", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns element-wise largest integer not greater than x. + /// + /// + /// + public static Tensor floor(Tensor x, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Floor", name) { args = new object[] { x }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return floor_eager_fallback(x, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + var _op = tf.OpDefLib._apply_op_helper("Floor", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Floor", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor floor_eager_fallback(Tensor x, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("Floor", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Floor", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns x // y element-wise. + /// + /// + /// + /// *NOTE*: `FloorDiv` supports broadcasting. More about broadcasting + /// [here](http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html) + /// + /// + /// + /// + /// + public static Tensor floor_div(Tensor x, Tensor y, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "FloorDiv", name) { args = new object[] { x, y }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return floor_div_eager_fallback(x, y, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + keywords["y"] = y; + var _op = tf.OpDefLib._apply_op_helper("FloorDiv", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("FloorDiv", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor floor_div_eager_fallback(Tensor x, Tensor y, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x, y }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("FloorDiv", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("FloorDiv", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns element-wise remainder of division. + /// + /// + /// + /// This follows Python semantics in that the + /// result here is consistent with a flooring divide. E.g. + /// `floor(x / y) * y + floormod(x, y) = x`, regardless of the signs of x and y. + /// + /// *NOTE*: `FloorMod` supports broadcasting. More about broadcasting + /// [here](http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html) + /// + /// + /// + /// + /// + public static Tensor floor_mod(Tensor x, Tensor y, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "FloorMod", name) { args = new object[] { x, y }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return floor_mod_eager_fallback(x, y, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + keywords["y"] = y; + var _op = tf.OpDefLib._apply_op_helper("FloorMod", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("FloorMod", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor floor_mod_eager_fallback(Tensor x, Tensor y, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x, y }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("FloorMod", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("FloorMod", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns the truth value of (x > y) element-wise. + /// + /// + /// + /// *NOTE*: `Greater` supports broadcasting. More about broadcasting + /// [here](http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html) + /// + /// Example: + /// + /// ```python + /// x = tf.constant([5, 4, 6]) + /// y = tf.constant([5, 2, 5]) + /// tf.math.greater(x, y) ==> [False, True, True] + /// + /// x = tf.constant([5, 4, 6]) + /// y = tf.constant([5]) + /// tf.math.greater(x, y) ==> [False, False, True] + /// ``` + /// + /// + /// + /// + /// + public static Tensor greater(Tensor x, Tensor y, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Greater", name) { args = new object[] { x, y }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return greater_eager_fallback(x, y, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + keywords["y"] = y; + var _op = tf.OpDefLib._apply_op_helper("Greater", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Greater", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor greater_eager_fallback(Tensor x, Tensor y, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x, y }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("Greater", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Greater", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns the truth value of (x >= y) element-wise. + /// + /// + /// + /// *NOTE*: `GreaterEqual` supports broadcasting. More about broadcasting + /// [here](http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html) + /// + /// Example: + /// + /// ```python + /// x = tf.constant([5, 4, 6, 7]) + /// y = tf.constant([5, 2, 5, 10]) + /// tf.math.greater_equal(x, y) ==> [True, True, True, False] + /// + /// x = tf.constant([5, 4, 6, 7]) + /// y = tf.constant([5]) + /// tf.math.greater_equal(x, y) ==> [True, False, True, True] + /// ``` + /// + /// + /// + /// + /// + public static Tensor greater_equal(Tensor x, Tensor y, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "GreaterEqual", name) { args = new object[] { x, y }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return greater_equal_eager_fallback(x, y, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + keywords["y"] = y; + var _op = tf.OpDefLib._apply_op_helper("GreaterEqual", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("GreaterEqual", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor greater_equal_eager_fallback(Tensor x, Tensor y, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x, y }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("GreaterEqual", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("GreaterEqual", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Return histogram of values. + /// + /// + /// + /// Given the tensor `values`, this operation returns a rank 1 histogram counting + /// the number of entries in `values` that fall into every bin. The bins are + /// equal width and determined by the arguments `value_range` and `nbins`. + /// + /// ```python + /// # Bins will be: (-inf, 1), [1, 2), [2, 3), [3, 4), [4, inf) + /// nbins = 5 + /// value_range = [0.0, 5.0] + /// new_values = [-1.0, 0.0, 1.5, 2.0, 5.0, 15] + /// + /// with tf.get_default_session() as sess: + /// hist = tf.histogram_fixed_width(new_values, value_range, nbins=5) + /// variables.global_variables_initializer().run() + /// sess.run(hist) => [2, 1, 1, 0, 2] + /// ``` + /// + /// + /// + /// + /// + /// + /// + public static Tensor histogram_fixed_width(Tensor values, Tensor value_range, Tensor nbins, TF_DataType dtype = TF_DataType.TF_INT32, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "HistogramFixedWidth", name) { args = new object[] { values, value_range, nbins }, attrs = new Dictionary() { ["dtype"] = dtype } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return histogram_fixed_width_eager_fallback(values, value_range, nbins, dtype: dtype, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["values"] = values; + keywords["value_range"] = value_range; + keywords["nbins"] = nbins; + keywords["dtype"] = dtype; + var _op = tf.OpDefLib._apply_op_helper("HistogramFixedWidth", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "dtype", _op._get_attr_type("dtype") }; + _execute.record_gradient("HistogramFixedWidth", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor histogram_fixed_width_eager_fallback(Tensor values, Tensor value_range, Tensor nbins, TF_DataType dtype, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { values, value_range, nbins }; + object[] _attrs = new object[] { "T", values.dtype, "dtype", dtype }; + var _result = _execute.execute("HistogramFixedWidth", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("HistogramFixedWidth", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Compute the lower regularized incomplete Gamma function `P(a, x)`. + /// + /// + /// + /// The lower regularized incomplete Gamma function is defined as: + /// + /// + /// \(P(a, x) = gamma(a, x) / Gamma(a) = 1 - Q(a, x)\) + /// + /// where + /// + /// \(gamma(a, x) = \int_{0}^{x} t^{a-1} exp(-t) dt\) + /// + /// is the lower incomplete Gamma function. + /// + /// Note, above `Q(a, x)` (`Igammac`) is the upper regularized complete + /// Gamma function. + /// + /// + /// + /// + /// + public static Tensor igamma(Tensor a, Tensor x, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Igamma", name) { args = new object[] { a, x }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return igamma_eager_fallback(a, x, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["a"] = a; + keywords["x"] = x; + var _op = tf.OpDefLib._apply_op_helper("Igamma", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Igamma", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor igamma_eager_fallback(Tensor a, Tensor x, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { a, x }; + object[] _attrs = new object[] { "T", a.dtype }; + var _result = _execute.execute("Igamma", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Igamma", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes the gradient of `igamma(a, x)` wrt `a`. + /// + /// + /// + /// + public static Tensor igamma_grad_a(Tensor a, Tensor x, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "IgammaGradA", name) { args = new object[] { a, x }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return igamma_grad_a_eager_fallback(a, x, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["a"] = a; + keywords["x"] = x; + var _op = tf.OpDefLib._apply_op_helper("IgammaGradA", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("IgammaGradA", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor igamma_grad_a_eager_fallback(Tensor a, Tensor x, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { a, x }; + object[] _attrs = new object[] { "T", a.dtype }; + var _result = _execute.execute("IgammaGradA", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("IgammaGradA", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Compute the upper regularized incomplete Gamma function `Q(a, x)`. + /// + /// + /// + /// The upper regularized incomplete Gamma function is defined as: + /// + /// \(Q(a, x) = Gamma(a, x) / Gamma(a) = 1 - P(a, x)\) + /// + /// where + /// + /// \(Gamma(a, x) = int_{x}^{infty} t^{a-1} exp(-t) dt\) + /// + /// is the upper incomplete Gamma function. + /// + /// Note, above `P(a, x)` (`Igamma`) is the lower regularized complete + /// Gamma function. + /// + /// + /// + /// + /// + public static Tensor igammac(Tensor a, Tensor x, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Igammac", name) { args = new object[] { a, x }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return igammac_eager_fallback(a, x, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["a"] = a; + keywords["x"] = x; + var _op = tf.OpDefLib._apply_op_helper("Igammac", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Igammac", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor igammac_eager_fallback(Tensor a, Tensor x, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { a, x }; + object[] _attrs = new object[] { "T", a.dtype }; + var _result = _execute.execute("Igammac", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Igammac", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns the imaginary part of a complex number. + /// + /// + /// + /// Given a tensor `input` of complex numbers, this operation returns a tensor of + /// type `float` that is the imaginary part of each element in `input`. All + /// elements in `input` must be complex numbers of the form \(a + bj\), where *a* + /// is the real part and *b* is the imaginary part returned by this operation. + /// + /// For example: + /// + /// ``` + /// # tensor 'input' is [-2.25 + 4.75j, 3.25 + 5.75j] + /// tf.imag(input) ==> [4.75, 5.75] + /// ``` + /// + /// + /// + /// + /// + public static Tensor imag(Tensor input, TF_DataType Tout = TF_DataType.TF_FLOAT, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Imag", name) { args = new object[] { input }, attrs = new Dictionary() { ["Tout"] = Tout } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return imag_eager_fallback(input, Tout: Tout, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["Tout"] = Tout; + var _op = tf.OpDefLib._apply_op_helper("Imag", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "Tout", _op._get_attr_type("Tout") }; + _execute.record_gradient("Imag", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor imag_eager_fallback(Tensor input, TF_DataType Tout, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input }; + object[] _attrs = new object[] { "T", input.dtype, "Tout", Tout }; + var _result = _execute.execute("Imag", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Imag", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes the reciprocal of x element-wise. + /// + /// + /// + /// I.e., \(y = 1 / x\). + /// + /// + /// + /// + public static Tensor inv(Tensor x, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Inv", name) { args = new object[] { x }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return inv_eager_fallback(x, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + var _op = tf.OpDefLib._apply_op_helper("Inv", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Inv", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor inv_eager_fallback(Tensor x, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("Inv", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Inv", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes the gradient for the inverse of `x` wrt its input. + /// + /// + /// + /// Specifically, `grad = -dy * y*y`, where `y = 1/x`, and `dy` + /// is the corresponding input gradient. + /// + /// + /// + /// + /// + public static Tensor inv_grad(Tensor y, Tensor dy, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "InvGrad", name) { args = new object[] { y, dy }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return inv_grad_eager_fallback(y, dy, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["y"] = y; + keywords["dy"] = dy; + var _op = tf.OpDefLib._apply_op_helper("InvGrad", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("InvGrad", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor inv_grad_eager_fallback(Tensor y, Tensor dy, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { y, dy }; + object[] _attrs = new object[] { "T", y.dtype }; + var _result = _execute.execute("InvGrad", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("InvGrad", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns which elements of x are finite. + /// + /// + /// + /// @compatibility(numpy) + /// Equivalent to np.isfinite + /// @end_compatibility + /// + /// Example: + /// + /// ```python + /// x = tf.constant([5.0, 4.8, 6.8, np.inf, np.nan]) + /// tf.math.is_finite(x) ==> [True, True, True, False, False] + /// ``` + /// + /// + /// + /// + public static Tensor is_finite(Tensor x, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "IsFinite", name) { args = new object[] { x }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return is_finite_eager_fallback(x, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + var _op = tf.OpDefLib._apply_op_helper("IsFinite", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("IsFinite", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor is_finite_eager_fallback(Tensor x, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("IsFinite", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("IsFinite", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns which elements of x are Inf. + /// + /// + /// + /// @compatibility(numpy) + /// Equivalent to np.isinf + /// @end_compatibility + /// + /// Example: + /// + /// ```python + /// x = tf.constant([5.0, np.inf, 6.8, np.inf]) + /// tf.math.is_inf(x) ==> [False, True, False, True] + /// ``` + /// + /// + /// + /// + public static Tensor is_inf(Tensor x, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "IsInf", name) { args = new object[] { x }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return is_inf_eager_fallback(x, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + var _op = tf.OpDefLib._apply_op_helper("IsInf", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("IsInf", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor is_inf_eager_fallback(Tensor x, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("IsInf", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("IsInf", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns which elements of x are NaN. + /// + /// + /// + /// @compatibility(numpy) + /// Equivalent to np.isnan + /// @end_compatibility + /// + /// Example: + /// + /// ```python + /// x = tf.constant([5.0, np.nan, 6.8, np.nan, np.inf]) + /// tf.math.is_nan(x) ==> [False, True, False, True, False] + /// ``` + /// + /// + /// + /// + public static Tensor is_nan(Tensor x, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "IsNan", name) { args = new object[] { x }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return is_nan_eager_fallback(x, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + var _op = tf.OpDefLib._apply_op_helper("IsNan", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("IsNan", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor is_nan_eager_fallback(Tensor x, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("IsNan", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("IsNan", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns the truth value of (x < y) element-wise. + /// + /// + /// + /// *NOTE*: `Less` supports broadcasting. More about broadcasting + /// [here](http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html) + /// + /// Example: + /// + /// ```python + /// x = tf.constant([5, 4, 6]) + /// y = tf.constant([5]) + /// tf.math.less(x, y) ==> [False, True, False] + /// + /// x = tf.constant([5, 4, 6]) + /// y = tf.constant([5, 6, 7]) + /// tf.math.less(x, y) ==> [False, True, True] + /// ``` + /// + /// + /// + /// + /// + public static Tensor less(Tensor x, Tensor y, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Less", name) { args = new object[] { x, y }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return less_eager_fallback(x, y, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + keywords["y"] = y; + var _op = tf.OpDefLib._apply_op_helper("Less", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Less", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor less_eager_fallback(Tensor x, Tensor y, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x, y }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("Less", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Less", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns the truth value of (x <= y) element-wise. + /// + /// + /// + /// *NOTE*: `LessEqual` supports broadcasting. More about broadcasting + /// [here](http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html) + /// + /// Example: + /// + /// ```python + /// x = tf.constant([5, 4, 6]) + /// y = tf.constant([5]) + /// tf.math.less_equal(x, y) ==> [True, True, False] + /// + /// x = tf.constant([5, 4, 6]) + /// y = tf.constant([5, 6, 6]) + /// tf.math.less_equal(x, y) ==> [True, True, True] + /// ``` + /// + /// + /// + /// + /// + public static Tensor less_equal(Tensor x, Tensor y, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "LessEqual", name) { args = new object[] { x, y }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return less_equal_eager_fallback(x, y, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + keywords["y"] = y; + var _op = tf.OpDefLib._apply_op_helper("LessEqual", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("LessEqual", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor less_equal_eager_fallback(Tensor x, Tensor y, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x, y }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("LessEqual", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("LessEqual", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes the log of the absolute value of `Gamma(x)` element-wise. + /// + /// + /// + /// For positive numbers, this function computes log((input - 1)!) for every element in the tensor. + /// `lgamma(5) = log((5-1)!) = log(4!) = log(24) = 3.1780539` + /// + /// Example: + /// + /// ```python + /// x = tf.constant([0, 0.5, 1, 4.5, -4, -5.6]) + /// tf.math.lgamma(x) ==> [inf, 0.5723649, 0., 2.4537368, inf, -4.6477685] + /// ``` + /// + /// + /// + /// + public static Tensor lgamma(Tensor x, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Lgamma", name) { args = new object[] { x }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return lgamma_eager_fallback(x, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + var _op = tf.OpDefLib._apply_op_helper("Lgamma", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Lgamma", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor lgamma_eager_fallback(Tensor x, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("Lgamma", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Lgamma", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Generates values in an interval. + /// + /// + /// + /// A sequence of `num` evenly-spaced values are generated beginning at `start`. + /// If `num > 1`, the values in the sequence increase by `stop - start / num - 1`, + /// so that the last one is exactly `stop`. + /// + /// For example: + /// + /// ``` + /// tf.linspace(10.0, 12.0, 3, name="linspace") => [ 10.0 11.0 12.0] + /// ``` + /// + /// + /// + /// + /// + /// + public static Tensor lin_space(Tensor start, Tensor stop, Tensor num, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "LinSpace", name) { args = new object[] { start, stop, num }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return lin_space_eager_fallback(start, stop, num, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["start"] = start; + keywords["stop"] = stop; + keywords["num"] = num; + var _op = tf.OpDefLib._apply_op_helper("LinSpace", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "Tidx", _op._get_attr_type("Tidx") }; + _execute.record_gradient("LinSpace", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor lin_space_eager_fallback(Tensor start, Tensor stop, Tensor num, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { start, stop, num }; + object[] _attrs = new object[] { "T", start.dtype, "Tidx", num.dtype }; + var _result = _execute.execute("LinSpace", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("LinSpace", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes natural logarithm of x element-wise. + /// + /// + /// + /// I.e., \(y = log_e x\). + /// + /// Example: + /// + /// ```python + /// x = tf.constant([0, 0.5, 1, 5]) + /// tf.math.log(x) ==> [-inf, -0.6931472, 0. , 1.609438] + /// ``` + /// + /// + /// + /// + public static Tensor log(Tensor x, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Log", name) { args = new object[] { x }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return log_eager_fallback(x, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + var _op = tf.OpDefLib._apply_op_helper("Log", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Log", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor log_eager_fallback(Tensor x, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("Log", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Log", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes natural logarithm of (1 + x) element-wise. + /// + /// + /// + /// I.e., \(y = log_e (1 + x)\). + /// + /// Example: + /// + /// ```python + /// x = tf.constant([0, 0.5, 1, 5]) + /// tf.math.log1p(x) ==> [0., 0.4054651, 0.6931472, 1.7917595] + /// ``` + /// + /// + /// + /// + public static Tensor log1p(Tensor x, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Log1p", name) { args = new object[] { x }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return log1p_eager_fallback(x, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + var _op = tf.OpDefLib._apply_op_helper("Log1p", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Log1p", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor log1p_eager_fallback(Tensor x, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("Log1p", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Log1p", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns the truth value of x AND y element-wise. + /// + /// + /// + /// *NOTE*: `LogicalAnd` supports broadcasting. More about broadcasting + /// [here](http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html) + /// + /// + /// + /// + /// + public static Tensor logical_and(Tensor x, Tensor y, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "LogicalAnd", name) { args = new object[] { x, y }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return logical_and_eager_fallback(x, y, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + keywords["y"] = y; + var _op = tf.OpDefLib._apply_op_helper("LogicalAnd", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { }; + _execute.record_gradient("LogicalAnd", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor logical_and_eager_fallback(Tensor x, Tensor y, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x, y }; + object[] _attrs = new object[] { }; + var _result = _execute.execute("LogicalAnd", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("LogicalAnd", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns the truth value of `NOT x` element-wise. + /// + /// + /// + public static Tensor logical_not(Tensor x, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "LogicalNot", name) { args = new object[] { x }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return logical_not_eager_fallback(x, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + var _op = tf.OpDefLib._apply_op_helper("LogicalNot", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { }; + _execute.record_gradient("LogicalNot", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor logical_not_eager_fallback(Tensor x, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x }; + object[] _attrs = new object[] { }; + var _result = _execute.execute("LogicalNot", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("LogicalNot", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns the truth value of x OR y element-wise. + /// + /// + /// + /// *NOTE*: `LogicalOr` supports broadcasting. More about broadcasting + /// [here](http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html) + /// + /// + /// + /// + /// + public static Tensor logical_or(Tensor x, Tensor y, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "LogicalOr", name) { args = new object[] { x, y }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return logical_or_eager_fallback(x, y, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + keywords["y"] = y; + var _op = tf.OpDefLib._apply_op_helper("LogicalOr", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { }; + _execute.record_gradient("LogicalOr", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor logical_or_eager_fallback(Tensor x, Tensor y, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x, y }; + object[] _attrs = new object[] { }; + var _result = _execute.execute("LogicalOr", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("LogicalOr", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Multiply the matrix "a" by the matrix "b". + /// + /// + /// + /// The inputs must be two-dimensional matrices and the inner dimension of + /// "a" (after being transposed if transpose_a is true) must match the + /// outer dimension of "b" (after being transposed if transposed_b is + /// true). + /// + /// *Note*: The default kernel implementation for MatMul on GPUs uses + /// cublas. + /// + /// + /// + /// + /// + /// + /// If true, "a" is transposed before multiplication. + /// + /// + /// + /// + /// If true, "b" is transposed before multiplication. + /// + /// + /// + public static Tensor mat_mul(Tensor a, Tensor b, bool transpose_a = false, bool transpose_b = false, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "MatMul", name) { args = new object[] { a, b }, attrs = new Dictionary() { ["transpose_a"] = transpose_a, ["transpose_b"] = transpose_b } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return mat_mul_eager_fallback(a, b, transpose_a: transpose_a, transpose_b: transpose_b, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["a"] = a; + keywords["b"] = b; + keywords["transpose_a"] = transpose_a; + keywords["transpose_b"] = transpose_b; + var _op = tf.OpDefLib._apply_op_helper("MatMul", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "transpose_a", _op._get_attr_bool("transpose_a"), "transpose_b", _op._get_attr_bool("transpose_b"), "T", _op._get_attr_type("T") }; + _execute.record_gradient("MatMul", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor mat_mul_eager_fallback(Tensor a, Tensor b, bool transpose_a, bool transpose_b, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { a, b }; + object[] _attrs = new object[] { "transpose_a", transpose_a, "transpose_b", transpose_b, "T", a.dtype }; + var _result = _execute.execute("MatMul", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("MatMul", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes the maximum of elements across dimensions of a tensor. + /// + /// + /// + /// Reduces `input` along the dimensions given in `reduction_indices`. Unless + /// `keep_dims` is true, the rank of the tensor is reduced by 1 for each entry in + /// `reduction_indices`. If `keep_dims` is true, the reduced dimensions are + /// retained with length 1. + /// + /// + /// + /// + /// + /// + /// If true, retain reduced dimensions with length 1. + /// + /// + /// + public static Tensor max(Tensor input, Tensor reduction_indices, bool keep_dims = false, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Max", name) { args = new object[] { input, reduction_indices }, attrs = new Dictionary() { ["keep_dims"] = keep_dims } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return max_eager_fallback(input, reduction_indices, keep_dims: keep_dims, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["reduction_indices"] = reduction_indices; + keywords["keep_dims"] = keep_dims; + var _op = tf.OpDefLib._apply_op_helper("Max", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "keep_dims", _op._get_attr_bool("keep_dims"), "T", _op._get_attr_type("T"), "Tidx", _op._get_attr_type("Tidx") }; + _execute.record_gradient("Max", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor max_eager_fallback(Tensor input, Tensor reduction_indices, bool keep_dims, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, reduction_indices }; + object[] _attrs = new object[] { "keep_dims", keep_dims, "T", input.dtype, "Tidx", reduction_indices.dtype }; + var _result = _execute.execute("Max", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Max", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns the max of x and y (i.e. x > y ? x : y) element-wise. + /// + /// + /// + /// *NOTE*: `Maximum` supports broadcasting. More about broadcasting + /// [here](http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html) + /// + /// + /// + /// + /// + public static Tensor maximum(Tensor x, Tensor y, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Maximum", name) { args = new object[] { x, y }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return maximum_eager_fallback(x, y, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + keywords["y"] = y; + var _op = tf.OpDefLib._apply_op_helper("Maximum", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Maximum", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor maximum_eager_fallback(Tensor x, Tensor y, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x, y }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("Maximum", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Maximum", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes the mean of elements across dimensions of a tensor. + /// + /// + /// + /// Reduces `input` along the dimensions given in `reduction_indices`. Unless + /// `keep_dims` is true, the rank of the tensor is reduced by 1 for each entry in + /// `reduction_indices`. If `keep_dims` is true, the reduced dimensions are + /// retained with length 1. + /// + /// + /// + /// + /// + /// + /// If true, retain reduced dimensions with length 1. + /// + /// + /// + public static Tensor mean(Tensor input, Tensor reduction_indices, bool keep_dims = false, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Mean", name) { args = new object[] { input, reduction_indices }, attrs = new Dictionary() { ["keep_dims"] = keep_dims } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return mean_eager_fallback(input, reduction_indices, keep_dims: keep_dims, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["reduction_indices"] = reduction_indices; + keywords["keep_dims"] = keep_dims; + var _op = tf.OpDefLib._apply_op_helper("Mean", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "keep_dims", _op._get_attr_bool("keep_dims"), "T", _op._get_attr_type("T"), "Tidx", _op._get_attr_type("Tidx") }; + _execute.record_gradient("Mean", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor mean_eager_fallback(Tensor input, Tensor reduction_indices, bool keep_dims, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, reduction_indices }; + object[] _attrs = new object[] { "keep_dims", keep_dims, "T", input.dtype, "Tidx", reduction_indices.dtype }; + var _result = _execute.execute("Mean", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Mean", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes the minimum of elements across dimensions of a tensor. + /// + /// + /// + /// Reduces `input` along the dimensions given in `reduction_indices`. Unless + /// `keep_dims` is true, the rank of the tensor is reduced by 1 for each entry in + /// `reduction_indices`. If `keep_dims` is true, the reduced dimensions are + /// retained with length 1. + /// + /// + /// + /// + /// + /// + /// If true, retain reduced dimensions with length 1. + /// + /// + /// + public static Tensor min(Tensor input, Tensor reduction_indices, bool keep_dims = false, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Min", name) { args = new object[] { input, reduction_indices }, attrs = new Dictionary() { ["keep_dims"] = keep_dims } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return min_eager_fallback(input, reduction_indices, keep_dims: keep_dims, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["reduction_indices"] = reduction_indices; + keywords["keep_dims"] = keep_dims; + var _op = tf.OpDefLib._apply_op_helper("Min", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "keep_dims", _op._get_attr_bool("keep_dims"), "T", _op._get_attr_type("T"), "Tidx", _op._get_attr_type("Tidx") }; + _execute.record_gradient("Min", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor min_eager_fallback(Tensor input, Tensor reduction_indices, bool keep_dims, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, reduction_indices }; + object[] _attrs = new object[] { "keep_dims", keep_dims, "T", input.dtype, "Tidx", reduction_indices.dtype }; + var _result = _execute.execute("Min", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Min", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns the min of x and y (i.e. x < y ? x : y) element-wise. + /// + /// + /// + /// *NOTE*: `Minimum` supports broadcasting. More about broadcasting + /// [here](http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html) + /// + /// + /// + /// + /// + public static Tensor minimum(Tensor x, Tensor y, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Minimum", name) { args = new object[] { x, y }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return minimum_eager_fallback(x, y, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + keywords["y"] = y; + var _op = tf.OpDefLib._apply_op_helper("Minimum", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Minimum", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor minimum_eager_fallback(Tensor x, Tensor y, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x, y }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("Minimum", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Minimum", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns element-wise remainder of division. This emulates C semantics in that + /// + /// + /// + /// the result here is consistent with a truncating divide. E.g. + /// `tf.truncatediv(x, y) * y + truncate_mod(x, y) = x`. + /// + /// *NOTE*: `Mod` supports broadcasting. More about broadcasting + /// [here](http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html) + /// + /// + /// + /// + /// + public static Tensor mod(Tensor x, Tensor y, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Mod", name) { args = new object[] { x, y }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return mod_eager_fallback(x, y, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + keywords["y"] = y; + var _op = tf.OpDefLib._apply_op_helper("Mod", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Mod", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor mod_eager_fallback(Tensor x, Tensor y, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x, y }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("Mod", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Mod", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns x * y element-wise. + /// + /// + /// + /// *NOTE*: `Mul` supports broadcasting. More about broadcasting + /// [here](http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html) + /// + /// + /// + /// + /// + public static Tensor mul(Tensor x, Tensor y, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Mul", name) { args = new object[] { x, y }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return mul_eager_fallback(x, y, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + keywords["y"] = y; + var _op = tf.OpDefLib._apply_op_helper("Mul", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Mul", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor mul_eager_fallback(Tensor x, Tensor y, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x, y }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("Mul", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Mul", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns x * y element-wise. Returns zero if y is zero, even if x if infinite or NaN. + /// + /// + /// + /// *NOTE*: `MulNoNan` supports broadcasting. More about broadcasting + /// [here](http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html) + /// + /// + /// + /// + /// + public static Tensor mul_no_nan(Tensor x, Tensor y, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "MulNoNan", name) { args = new object[] { x, y }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return mul_no_nan_eager_fallback(x, y, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + keywords["y"] = y; + var _op = tf.OpDefLib._apply_op_helper("MulNoNan", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("MulNoNan", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor mul_no_nan_eager_fallback(Tensor x, Tensor y, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x, y }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("MulNoNan", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("MulNoNan", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// + /// + /// + /// + public static Tensor ndtri(Tensor x, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Ndtri", name) { args = new object[] { x }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return ndtri_eager_fallback(x, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + var _op = tf.OpDefLib._apply_op_helper("Ndtri", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Ndtri", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor ndtri_eager_fallback(Tensor x, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("Ndtri", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Ndtri", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes numerical negative value element-wise. + /// + /// + /// + /// I.e., \(y = -x\). + /// + /// + /// + /// + public static Tensor neg(Tensor x, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Neg", name) { args = new object[] { x }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return neg_eager_fallback(x, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + var _op = tf.OpDefLib._apply_op_helper("Neg", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Neg", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor neg_eager_fallback(Tensor x, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("Neg", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Neg", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns the next representable value of `x1` in the direction of `x2`, element-wise. + /// + /// + /// + /// This operation returns the same result as the C++ std::nextafter function. + /// + /// It can also return a subnormal number. + /// + /// @compatibility(cpp) + /// Equivalent to C++ std::nextafter function. + /// @end_compatibility + /// + /// + /// + /// + /// + public static Tensor next_after(Tensor x1, Tensor x2, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "NextAfter", name) { args = new object[] { x1, x2 }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return next_after_eager_fallback(x1, x2, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x1"] = x1; + keywords["x2"] = x2; + var _op = tf.OpDefLib._apply_op_helper("NextAfter", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("NextAfter", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor next_after_eager_fallback(Tensor x1, Tensor x2, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x1, x2 }; + object[] _attrs = new object[] { "T", x1.dtype }; + var _result = _execute.execute("NextAfter", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("NextAfter", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns the truth value of (x != y) element-wise. + /// + /// + /// + /// *NOTE*: `NotEqual` supports broadcasting. More about broadcasting + /// [here](http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html) + /// + /// + /// + /// + /// + /// + public static Tensor not_equal(Tensor x, Tensor y, bool incompatible_shape_error = true, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "NotEqual", name) { args = new object[] { x, y }, attrs = new Dictionary() { ["incompatible_shape_error"] = incompatible_shape_error } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return not_equal_eager_fallback(x, y, incompatible_shape_error: incompatible_shape_error, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + keywords["y"] = y; + keywords["incompatible_shape_error"] = incompatible_shape_error; + var _op = tf.OpDefLib._apply_op_helper("NotEqual", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "incompatible_shape_error", _op._get_attr_bool("incompatible_shape_error") }; + _execute.record_gradient("NotEqual", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor not_equal_eager_fallback(Tensor x, Tensor y, bool incompatible_shape_error, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x, y }; + object[] _attrs = new object[] { "T", x.dtype, "incompatible_shape_error", incompatible_shape_error }; + var _result = _execute.execute("NotEqual", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("NotEqual", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Compute the polygamma function \\(\psi^{(n)}(x)\\). + /// + /// + /// + /// The polygamma function is defined as: + /// + /// + /// \(psi^{(a)}(x) = rac{d^a}{dx^a} psi(x)\) + /// + /// where \(psi(x)\) is the digamma function. + /// The polygamma function is defined only for non-negative integer orders \a\. + /// + /// + /// + /// + /// + public static Tensor polygamma(Tensor a, Tensor x, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Polygamma", name) { args = new object[] { a, x }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return polygamma_eager_fallback(a, x, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["a"] = a; + keywords["x"] = x; + var _op = tf.OpDefLib._apply_op_helper("Polygamma", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Polygamma", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor polygamma_eager_fallback(Tensor a, Tensor x, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { a, x }; + object[] _attrs = new object[] { "T", a.dtype }; + var _result = _execute.execute("Polygamma", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Polygamma", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes the power of one value to another. + /// + /// + /// + /// Given a tensor `x` and a tensor `y`, this operation computes \(x^y\) for + /// corresponding elements in `x` and `y`. For example: + /// + /// ``` + /// # tensor 'x' is [[2, 2]], [3, 3]] + /// # tensor 'y' is [[8, 16], [2, 3]] + /// tf.pow(x, y) ==> [[256, 65536], [9, 27]] + /// ``` + /// + /// + /// + /// + /// + public static Tensor pow(Tensor x, Tensor y, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Pow", name) { args = new object[] { x, y }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return pow_eager_fallback(x, y, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + keywords["y"] = y; + var _op = tf.OpDefLib._apply_op_helper("Pow", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Pow", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor pow_eager_fallback(Tensor x, Tensor y, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x, y }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("Pow", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Pow", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes the product of elements across dimensions of a tensor. + /// + /// + /// + /// Reduces `input` along the dimensions given in `reduction_indices`. Unless + /// `keep_dims` is true, the rank of the tensor is reduced by 1 for each entry in + /// `reduction_indices`. If `keep_dims` is true, the reduced dimensions are + /// retained with length 1. + /// + /// + /// + /// + /// + /// + /// If true, retain reduced dimensions with length 1. + /// + /// + /// + public static Tensor prod(Tensor input, Tensor reduction_indices, bool keep_dims = false, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Prod", name) { args = new object[] { input, reduction_indices }, attrs = new Dictionary() { ["keep_dims"] = keep_dims } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return prod_eager_fallback(input, reduction_indices, keep_dims: keep_dims, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["reduction_indices"] = reduction_indices; + keywords["keep_dims"] = keep_dims; + var _op = tf.OpDefLib._apply_op_helper("Prod", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "keep_dims", _op._get_attr_bool("keep_dims"), "T", _op._get_attr_type("T"), "Tidx", _op._get_attr_type("Tidx") }; + _execute.record_gradient("Prod", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor prod_eager_fallback(Tensor input, Tensor reduction_indices, bool keep_dims, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, reduction_indices }; + object[] _attrs = new object[] { "keep_dims", keep_dims, "T", input.dtype, "Tidx", reduction_indices.dtype }; + var _result = _execute.execute("Prod", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Prod", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Convert the quantized 'input' tensor into a lower-precision 'output', using the + /// + /// + /// + /// actual distribution of the values to maximize the usage of the lower bit depth + /// and adjusting the output min and max ranges accordingly. + /// + /// [input_min, input_max] are scalar floats that specify the range for the float + /// interpretation of the 'input' data. For example, if input_min is -1.0f and + /// input_max is 1.0f, and we are dealing with quint16 quantized data, then a 0 + /// value in the 16-bit data should be interpreted as -1.0f, and a 65535 means 1.0f. + /// + /// This operator tries to squeeze as much precision as possible into an output with + /// a lower bit depth by calculating the actual min and max values found in the + /// data. For example, maybe that quint16 input has no values lower than 16,384 and + /// none higher than 49,152. That means only half the range is actually needed, all + /// the float interpretations are between -0.5f and 0.5f, so if we want to compress + /// the data into a quint8 output, we can use that range rather than the theoretical + /// -1.0f to 1.0f that is suggested by the input min and max. + /// + /// In practice, this is most useful for taking output from operations like + /// QuantizedMatMul that can produce higher bit-depth outputs than their inputs and + /// may have large potential output ranges, but in practice have a distribution of + /// input values that only uses a small fraction of the possible range. By feeding + /// that output into this operator, we can reduce it from 32 bits down to 8 with + /// minimal loss of accuracy. + /// + /// + /// + /// + /// + /// + /// + /// The type of the output. Should be a lower bit depth than Tinput. + /// + /// + /// + public static Tensor[] quantize_down_and_shrink_range(Tensor input, Tensor input_min, Tensor input_max, TF_DataType out_type, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "QuantizeDownAndShrinkRange", name) { args = new object[] { input, input_min, input_max }, attrs = new Dictionary() { ["out_type"] = out_type } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return quantize_down_and_shrink_range_eager_fallback(input, input_min, input_max, out_type: out_type, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["input_min"] = input_min; + keywords["input_max"] = input_max; + keywords["out_type"] = out_type; + var _op = tf.OpDefLib._apply_op_helper("QuantizeDownAndShrinkRange", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "Tinput", _op._get_attr_type("Tinput"), "out_type", _op._get_attr_type("out_type") }; + _execute.record_gradient("QuantizeDownAndShrinkRange", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] quantize_down_and_shrink_range_eager_fallback(Tensor input, Tensor input_min, Tensor input_max, TF_DataType out_type, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, input_min, input_max }; + object[] _attrs = new object[] { "Tinput", input.dtype, "out_type", out_type }; + var _result = _execute.execute("QuantizeDownAndShrinkRange", 3, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("QuantizeDownAndShrinkRange", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// Returns x + y element-wise, working on quantized buffers. + /// + /// + /// + /// + /// + /// + /// + /// + /// + public static Tensor[] quantized_add(Tensor x, Tensor y, Tensor min_x, Tensor max_x, Tensor min_y, Tensor max_y, TF_DataType Toutput = TF_DataType.TF_QINT32, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "QuantizedAdd", name) { args = new object[] { x, y, min_x, max_x, min_y, max_y }, attrs = new Dictionary() { ["Toutput"] = Toutput } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return quantized_add_eager_fallback(x, y, min_x, max_x, min_y, max_y, Toutput: Toutput, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + keywords["y"] = y; + keywords["min_x"] = min_x; + keywords["max_x"] = max_x; + keywords["min_y"] = min_y; + keywords["max_y"] = max_y; + keywords["Toutput"] = Toutput; + var _op = tf.OpDefLib._apply_op_helper("QuantizedAdd", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T1", _op._get_attr_type("T1"), "T2", _op._get_attr_type("T2"), "Toutput", _op._get_attr_type("Toutput") }; + _execute.record_gradient("QuantizedAdd", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] quantized_add_eager_fallback(Tensor x, Tensor y, Tensor min_x, Tensor max_x, Tensor min_y, Tensor max_y, TF_DataType Toutput, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x, y, min_x, max_x, min_y, max_y }; + object[] _attrs = new object[] { "T1", x.dtype, "T2", y.dtype, "Toutput", Toutput }; + var _result = _execute.execute("QuantizedAdd", 3, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("QuantizedAdd", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// Perform a quantized matrix multiplication of `a` by the matrix `b`. + /// + /// + /// + /// The inputs must be two-dimensional matrices and the inner dimension of + /// `a` (after being transposed if `transpose_a` is non-zero) must match the + /// outer dimension of `b` (after being transposed if `transposed_b` is + /// non-zero). + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// If true, `a` is transposed before multiplication. + /// + /// + /// + /// + /// If true, `b` is transposed before multiplication. + /// + /// + /// + /// + /// The type of output produced by activation function + /// following this operation. + /// + /// + /// + public static Tensor[] quantized_mat_mul(Tensor a, Tensor b, Tensor min_a, Tensor max_a, Tensor min_b, Tensor max_b, TF_DataType Toutput = TF_DataType.TF_QINT32, bool transpose_a = false, bool transpose_b = false, TF_DataType Tactivation = TF_DataType.TF_QUINT8, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "QuantizedMatMul", name) { args = new object[] { a, b, min_a, max_a, min_b, max_b }, attrs = new Dictionary() { ["Toutput"] = Toutput, ["transpose_a"] = transpose_a, ["transpose_b"] = transpose_b, ["Tactivation"] = Tactivation } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return quantized_mat_mul_eager_fallback(a, b, min_a, max_a, min_b, max_b, Toutput: Toutput, transpose_a: transpose_a, transpose_b: transpose_b, Tactivation: Tactivation, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["a"] = a; + keywords["b"] = b; + keywords["min_a"] = min_a; + keywords["max_a"] = max_a; + keywords["min_b"] = min_b; + keywords["max_b"] = max_b; + keywords["Toutput"] = Toutput; + keywords["transpose_a"] = transpose_a; + keywords["transpose_b"] = transpose_b; + keywords["Tactivation"] = Tactivation; + var _op = tf.OpDefLib._apply_op_helper("QuantizedMatMul", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T1", _op._get_attr_type("T1"), "T2", _op._get_attr_type("T2"), "Toutput", _op._get_attr_type("Toutput"), "transpose_a", _op._get_attr_bool("transpose_a"), "transpose_b", _op._get_attr_bool("transpose_b"), "Tactivation", _op._get_attr_type("Tactivation") }; + _execute.record_gradient("QuantizedMatMul", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] quantized_mat_mul_eager_fallback(Tensor a, Tensor b, Tensor min_a, Tensor max_a, Tensor min_b, Tensor max_b, TF_DataType Toutput, bool transpose_a, bool transpose_b, TF_DataType Tactivation, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { a, b, min_a, max_a, min_b, max_b }; + object[] _attrs = new object[] { "T1", a.dtype, "T2", b.dtype, "Toutput", Toutput, "transpose_a", transpose_a, "transpose_b", transpose_b, "Tactivation", Tactivation }; + var _result = _execute.execute("QuantizedMatMul", 3, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("QuantizedMatMul", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// Returns x * y element-wise, working on quantized buffers. + /// + /// + /// + /// + /// + /// + /// + /// + /// + public static Tensor[] quantized_mul(Tensor x, Tensor y, Tensor min_x, Tensor max_x, Tensor min_y, Tensor max_y, TF_DataType Toutput = TF_DataType.TF_QINT32, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "QuantizedMul", name) { args = new object[] { x, y, min_x, max_x, min_y, max_y }, attrs = new Dictionary() { ["Toutput"] = Toutput } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return quantized_mul_eager_fallback(x, y, min_x, max_x, min_y, max_y, Toutput: Toutput, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + keywords["y"] = y; + keywords["min_x"] = min_x; + keywords["max_x"] = max_x; + keywords["min_y"] = min_y; + keywords["max_y"] = max_y; + keywords["Toutput"] = Toutput; + var _op = tf.OpDefLib._apply_op_helper("QuantizedMul", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T1", _op._get_attr_type("T1"), "T2", _op._get_attr_type("T2"), "Toutput", _op._get_attr_type("Toutput") }; + _execute.record_gradient("QuantizedMul", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] quantized_mul_eager_fallback(Tensor x, Tensor y, Tensor min_x, Tensor max_x, Tensor min_y, Tensor max_y, TF_DataType Toutput, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x, y, min_x, max_x, min_y, max_y }; + object[] _attrs = new object[] { "T1", x.dtype, "T2", y.dtype, "Toutput", Toutput }; + var _result = _execute.execute("QuantizedMul", 3, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("QuantizedMul", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// Counts the number of occurrences of each value in an integer array. + /// + /// + /// + /// Outputs a vector with length `size` and the same dtype as `weights`. If + /// `weights` are empty, then index `i` stores the number of times the value `i` is + /// counted in `arr`. If `weights` are non-empty, then index `i` stores the sum of + /// the value in `weights` at each index where the corresponding value in `arr` is + /// `i`. + /// + /// Values in `arr` outside of the range [0, size) are ignored. + /// + /// + /// + /// + /// + /// + /// + /// + /// bool; Whether the kernel should count the appearance or number of occurrences. + /// + /// + /// + public static Tensor ragged_bincount(Tensor splits, Tensor values, Tensor size, Tensor weights, bool binary_output = false, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "RaggedBincount", name) { args = new object[] { splits, values, size, weights }, attrs = new Dictionary() { ["binary_output"] = binary_output } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return ragged_bincount_eager_fallback(splits, values, size, weights, binary_output: binary_output, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["splits"] = splits; + keywords["values"] = values; + keywords["size"] = size; + keywords["weights"] = weights; + keywords["binary_output"] = binary_output; + var _op = tf.OpDefLib._apply_op_helper("RaggedBincount", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "Tidx", _op._get_attr_type("Tidx"), "T", _op._get_attr_type("T"), "binary_output", _op._get_attr_bool("binary_output") }; + _execute.record_gradient("RaggedBincount", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor ragged_bincount_eager_fallback(Tensor splits, Tensor values, Tensor size, Tensor weights, bool binary_output, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { splits, values, size, weights }; + object[] _attrs = new object[] { "Tidx", values.dtype, "T", weights.dtype, "binary_output", binary_output }; + var _result = _execute.execute("RaggedBincount", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("RaggedBincount", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Creates a sequence of numbers. + /// + /// + /// + /// This operation creates a sequence of numbers that begins at `start` and + /// extends by increments of `delta` up to but not including `limit`. + /// + /// For example: + /// + /// ``` + /// # 'start' is 3 + /// # 'limit' is 18 + /// # 'delta' is 3 + /// tf.range(start, limit, delta) ==> [3, 6, 9, 12, 15] + /// ``` + /// + /// + /// + /// + /// + /// + public static Tensor range(Tensor start, Tensor limit, Tensor delta, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Range", name) { args = new object[] { start, limit, delta }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return range_eager_fallback(start, limit, delta, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["start"] = start; + keywords["limit"] = limit; + keywords["delta"] = delta; + var _op = tf.OpDefLib._apply_op_helper("Range", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "Tidx", _op._get_attr_type("Tidx") }; + _execute.record_gradient("Range", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor range_eager_fallback(Tensor start, Tensor limit, Tensor delta, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { start, limit, delta }; + object[] _attrs = new object[] { "Tidx", start.dtype }; + var _result = _execute.execute("Range", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Range", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns the real part of a complex number. + /// + /// + /// + /// Given a tensor `input` of complex numbers, this operation returns a tensor of + /// type `float` that is the real part of each element in `input`. All elements in + /// `input` must be complex numbers of the form \(a + bj\), where *a* is the real + /// part returned by this operation and *b* is the imaginary part. + /// + /// For example: + /// + /// ``` + /// # tensor 'input' is [-2.25 + 4.75j, 3.25 + 5.75j] + /// tf.real(input) ==> [-2.25, 3.25] + /// ``` + /// + /// + /// + /// + /// + public static Tensor real(Tensor input, TF_DataType Tout = TF_DataType.TF_FLOAT, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Real", name) { args = new object[] { input }, attrs = new Dictionary() { ["Tout"] = Tout } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return real_eager_fallback(input, Tout: Tout, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["Tout"] = Tout; + var _op = tf.OpDefLib._apply_op_helper("Real", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "Tout", _op._get_attr_type("Tout") }; + _execute.record_gradient("Real", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor real_eager_fallback(Tensor input, TF_DataType Tout, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input }; + object[] _attrs = new object[] { "T", input.dtype, "Tout", Tout }; + var _result = _execute.execute("Real", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Real", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns x / y element-wise for real types. + /// + /// + /// + /// If `x` and `y` are reals, this will return the floating-point division. + /// + /// *NOTE*: `Div` supports broadcasting. More about broadcasting + /// [here](http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html) + /// + /// + /// + /// + /// + public static Tensor real_div(Tensor x, Tensor y, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "RealDiv", name) { args = new object[] { x, y }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return real_div_eager_fallback(x, y, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + keywords["y"] = y; + var _op = tf.OpDefLib._apply_op_helper("RealDiv", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("RealDiv", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor real_div_eager_fallback(Tensor x, Tensor y, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x, y }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("RealDiv", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("RealDiv", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes the reciprocal of x element-wise. + /// + /// + /// + /// I.e., \(y = 1 / x\). + /// + /// + /// + /// + public static Tensor reciprocal(Tensor x, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Reciprocal", name) { args = new object[] { x }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return reciprocal_eager_fallback(x, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + var _op = tf.OpDefLib._apply_op_helper("Reciprocal", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Reciprocal", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor reciprocal_eager_fallback(Tensor x, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("Reciprocal", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Reciprocal", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes the gradient for the inverse of `x` wrt its input. + /// + /// + /// + /// Specifically, `grad = -dy * y*y`, where `y = 1/x`, and `dy` + /// is the corresponding input gradient. + /// + /// + /// + /// + /// + public static Tensor reciprocal_grad(Tensor y, Tensor dy, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "ReciprocalGrad", name) { args = new object[] { y, dy }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return reciprocal_grad_eager_fallback(y, dy, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["y"] = y; + keywords["dy"] = dy; + var _op = tf.OpDefLib._apply_op_helper("ReciprocalGrad", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("ReciprocalGrad", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor reciprocal_grad_eager_fallback(Tensor y, Tensor dy, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { y, dy }; + object[] _attrs = new object[] { "T", y.dtype }; + var _result = _execute.execute("ReciprocalGrad", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("ReciprocalGrad", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes a range that covers the actual values present in a quantized tensor. + /// + /// + /// + /// Given a quantized tensor described by `(input, input_min, input_max)`, outputs a + /// range that covers the actual values present in that tensor. This op is typically + /// used to produce the `requested_output_min` and `requested_output_max` for + /// `Requantize`. + /// + /// + /// + /// + /// + /// + public static Tensor[] requantization_range(Tensor input, Tensor input_min, Tensor input_max, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "RequantizationRange", name) { args = new object[] { input, input_min, input_max }, attrs = new Dictionary() { } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return requantization_range_eager_fallback(input, input_min, input_max, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["input_min"] = input_min; + keywords["input_max"] = input_max; + var _op = tf.OpDefLib._apply_op_helper("RequantizationRange", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "Tinput", _op._get_attr_type("Tinput") }; + _execute.record_gradient("RequantizationRange", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] requantization_range_eager_fallback(Tensor input, Tensor input_min, Tensor input_max, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, input_min, input_max }; + object[] _attrs = new object[] { "Tinput", input.dtype }; + var _result = _execute.execute("RequantizationRange", 2, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("RequantizationRange", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// Computes requantization range per channel. + /// + /// + /// + /// + /// + /// + /// The maximum value of the output that needs to be clipped. + /// Example: set this to 6 for Relu6. + /// + /// + /// + public static Tensor[] requantization_range_per_channel(Tensor input, Tensor input_min, Tensor input_max, float clip_value_max, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "RequantizationRangePerChannel", name) { args = new object[] { input, input_min, input_max }, attrs = new Dictionary() { ["clip_value_max"] = clip_value_max } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return requantization_range_per_channel_eager_fallback(input, input_min, input_max, clip_value_max: clip_value_max, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["input_min"] = input_min; + keywords["input_max"] = input_max; + keywords["clip_value_max"] = clip_value_max; + var _op = tf.OpDefLib._apply_op_helper("RequantizationRangePerChannel", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "clip_value_max", _op.get_attr("clip_value_max") }; + _execute.record_gradient("RequantizationRangePerChannel", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] requantization_range_per_channel_eager_fallback(Tensor input, Tensor input_min, Tensor input_max, float clip_value_max, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, input_min, input_max }; + object[] _attrs = new object[] { "T", input.dtype, "clip_value_max", clip_value_max }; + var _result = _execute.execute("RequantizationRangePerChannel", 2, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("RequantizationRangePerChannel", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// Converts the quantized `input` tensor into a lower-precision `output`. + /// + /// + /// + /// Converts the quantized `input` tensor into a lower-precision `output`, using the + /// output range specified with `requested_output_min` and `requested_output_max`. + /// + /// `[input_min, input_max]` are scalar floats that specify the range for the float + /// interpretation of the `input` data. For example, if `input_min` is -1.0f and + /// `input_max` is 1.0f, and we are dealing with `quint16` quantized data, then a 0 + /// value in the 16-bit data should be interpreted as -1.0f, and a 65535 means 1.0f. + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// The type of the output. Should be a lower bit depth than Tinput. + /// + /// + /// + public static Tensor[] requantize(Tensor input, Tensor input_min, Tensor input_max, Tensor requested_output_min, Tensor requested_output_max, TF_DataType out_type, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Requantize", name) { args = new object[] { input, input_min, input_max, requested_output_min, requested_output_max }, attrs = new Dictionary() { ["out_type"] = out_type } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return requantize_eager_fallback(input, input_min, input_max, requested_output_min, requested_output_max, out_type: out_type, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["input_min"] = input_min; + keywords["input_max"] = input_max; + keywords["requested_output_min"] = requested_output_min; + keywords["requested_output_max"] = requested_output_max; + keywords["out_type"] = out_type; + var _op = tf.OpDefLib._apply_op_helper("Requantize", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "Tinput", _op._get_attr_type("Tinput"), "out_type", _op._get_attr_type("out_type") }; + _execute.record_gradient("Requantize", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] requantize_eager_fallback(Tensor input, Tensor input_min, Tensor input_max, Tensor requested_output_min, Tensor requested_output_max, TF_DataType out_type, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, input_min, input_max, requested_output_min, requested_output_max }; + object[] _attrs = new object[] { "Tinput", input.dtype, "out_type", out_type }; + var _result = _execute.execute("Requantize", 3, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Requantize", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// Requantizes input with min and max values known per channel. + /// + /// + /// + /// + /// + /// + /// + /// + /// The quantized type of output tensor that needs to be converted. + /// + /// + /// + public static Tensor[] requantize_per_channel(Tensor input, Tensor input_min, Tensor input_max, Tensor requested_output_min, Tensor requested_output_max, TF_DataType out_type = TF_DataType.TF_QUINT8, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "RequantizePerChannel", name) { args = new object[] { input, input_min, input_max, requested_output_min, requested_output_max }, attrs = new Dictionary() { ["out_type"] = out_type } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return requantize_per_channel_eager_fallback(input, input_min, input_max, requested_output_min, requested_output_max, out_type: out_type, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["input_min"] = input_min; + keywords["input_max"] = input_max; + keywords["requested_output_min"] = requested_output_min; + keywords["requested_output_max"] = requested_output_max; + keywords["out_type"] = out_type; + var _op = tf.OpDefLib._apply_op_helper("RequantizePerChannel", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "out_type", _op._get_attr_type("out_type") }; + _execute.record_gradient("RequantizePerChannel", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] requantize_per_channel_eager_fallback(Tensor input, Tensor input_min, Tensor input_max, Tensor requested_output_min, Tensor requested_output_max, TF_DataType out_type, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, input_min, input_max, requested_output_min, requested_output_max }; + object[] _attrs = new object[] { "T", input.dtype, "out_type", out_type }; + var _result = _execute.execute("RequantizePerChannel", 3, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("RequantizePerChannel", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// Returns element-wise integer closest to x. + /// + /// + /// + /// If the result is midway between two representable values, + /// the even representable is chosen. + /// For example: + /// + /// ``` + /// rint(-1.5) ==> -2.0 + /// rint(0.5000001) ==> 1.0 + /// rint([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0]) ==> [-2., -2., -0., 0., 2., 2., 2.] + /// ``` + /// + /// + /// + /// + public static Tensor rint(Tensor x, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Rint", name) { args = new object[] { x }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return rint_eager_fallback(x, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + var _op = tf.OpDefLib._apply_op_helper("Rint", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Rint", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor rint_eager_fallback(Tensor x, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("Rint", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Rint", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Rounds the values of a tensor to the nearest integer, element-wise. + /// + /// + /// + /// Rounds half to even. Also known as bankers rounding. If you want to round + /// according to the current system rounding mode use std::cint. + /// + /// + /// + /// + public static Tensor round(Tensor x, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Round", name) { args = new object[] { x }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return round_eager_fallback(x, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + var _op = tf.OpDefLib._apply_op_helper("Round", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Round", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor round_eager_fallback(Tensor x, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("Round", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Round", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes reciprocal of square root of x element-wise. + /// + /// + /// + /// I.e., \(y = 1 / sqrt{x}\). + /// + /// + /// + /// + public static Tensor rsqrt(Tensor x, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Rsqrt", name) { args = new object[] { x }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return rsqrt_eager_fallback(x, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + var _op = tf.OpDefLib._apply_op_helper("Rsqrt", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Rsqrt", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor rsqrt_eager_fallback(Tensor x, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("Rsqrt", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Rsqrt", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes the gradient for the rsqrt of `x` wrt its input. + /// + /// + /// + /// Specifically, `grad = dy * -0.5 * y^3`, where `y = rsqrt(x)`, and `dy` + /// is the corresponding input gradient. + /// + /// + /// + /// + /// + public static Tensor rsqrt_grad(Tensor y, Tensor dy, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "RsqrtGrad", name) { args = new object[] { y, dy }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return rsqrt_grad_eager_fallback(y, dy, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["y"] = y; + keywords["dy"] = dy; + var _op = tf.OpDefLib._apply_op_helper("RsqrtGrad", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("RsqrtGrad", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor rsqrt_grad_eager_fallback(Tensor y, Tensor dy, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { y, dy }; + object[] _attrs = new object[] { "T", y.dtype }; + var _result = _execute.execute("RsqrtGrad", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("RsqrtGrad", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes the maximum along segments of a tensor. + /// + /// + /// + /// Read + /// [the section on segmentation](https://tensorflow.org/api_docs/python/tf/math#Segmentation) + /// for an explanation of segments. + /// + /// Computes a tensor such that + /// \(output_i = max_j(data_j)\) where `max` is over `j` such + /// that `segment_ids[j] == i`. + /// + /// If the max is empty for a given segment ID `i`, `output[i] = 0`. + /// + /// Caution: On CPU, values in `segment_ids` are always validated to be sorted, + /// and an error is thrown for indices that are not increasing. On GPU, this + /// does not throw an error for unsorted indices. On GPU, out-of-order indices + /// result in safe but unspecified behavior, which may include treating + /// out-of-order indices as the same as a smaller following index. + /// + ///
+ /// + ///
+ /// + /// For example: + /// + /// >>> c = tf.constant([[1,2,3,4], [4, 3, 2, 1], [5,6,7,8]]) + /// >>> tf.math.segment_max(c, tf.constant([0, 0, 1])).numpy() + /// array([[4, 3, 3, 4], + /// [5, 6, 7, 8]], dtype=int32) + /// + /// + ///
+ /// + /// + /// + public static Tensor segment_max(Tensor data, Tensor segment_ids, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "SegmentMax", name) { args = new object[] { data, segment_ids }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return segment_max_eager_fallback(data, segment_ids, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["data"] = data; + keywords["segment_ids"] = segment_ids; + var _op = tf.OpDefLib._apply_op_helper("SegmentMax", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "Tindices", _op._get_attr_type("Tindices") }; + _execute.record_gradient("SegmentMax", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor segment_max_eager_fallback(Tensor data, Tensor segment_ids, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { data, segment_ids }; + object[] _attrs = new object[] { "T", data.dtype, "Tindices", segment_ids.dtype }; + var _result = _execute.execute("SegmentMax", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("SegmentMax", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes the mean along segments of a tensor. + /// + /// + /// + /// Read + /// [the section on segmentation](https://tensorflow.org/api_docs/python/tf/math#Segmentation) + /// for an explanation of segments. + /// + /// Computes a tensor such that + /// \(output_i = rac{sum_j data_j}{N}\) where `mean` is + /// over `j` such that `segment_ids[j] == i` and `N` is the total number of + /// values summed. + /// + /// If the mean is empty for a given segment ID `i`, `output[i] = 0`. + /// + /// Caution: On CPU, values in `segment_ids` are always validated to be sorted, + /// and an error is thrown for indices that are not increasing. On GPU, this + /// does not throw an error for unsorted indices. On GPU, out-of-order indices + /// result in safe but unspecified behavior, which may include treating + /// out-of-order indices as a smaller following index when computing the numerator + /// of the mean. + /// + ///
+ /// + ///
+ /// + /// For example: + /// + /// >>> c = tf.constant([[1.0,2,3,4], [4, 3, 2, 1], [5,6,7,8]]) + /// >>> tf.math.segment_mean(c, tf.constant([0, 0, 1])).numpy() + /// array([[2.5, 2.5, 2.5, 2.5], + /// [5., 6., 7., 8.]], dtype=float32) + /// + /// + ///
+ /// + /// + /// + public static Tensor segment_mean(Tensor data, Tensor segment_ids, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "SegmentMean", name) { args = new object[] { data, segment_ids }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return segment_mean_eager_fallback(data, segment_ids, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["data"] = data; + keywords["segment_ids"] = segment_ids; + var _op = tf.OpDefLib._apply_op_helper("SegmentMean", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "Tindices", _op._get_attr_type("Tindices") }; + _execute.record_gradient("SegmentMean", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor segment_mean_eager_fallback(Tensor data, Tensor segment_ids, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { data, segment_ids }; + object[] _attrs = new object[] { "T", data.dtype, "Tindices", segment_ids.dtype }; + var _result = _execute.execute("SegmentMean", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("SegmentMean", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes the minimum along segments of a tensor. + /// + /// + /// + /// Read + /// [the section on segmentation](https://tensorflow.org/api_docs/python/tf/math#Segmentation) + /// for an explanation of segments. + /// + /// Computes a tensor such that + /// \(output_i = min_j(data_j)\) where `min` is over `j` such + /// that `segment_ids[j] == i`. + /// + /// If the min is empty for a given segment ID `i`, `output[i] = 0`. + /// + /// Caution: On CPU, values in `segment_ids` are always validated to be sorted, + /// and an error is thrown for indices that are not increasing. On GPU, this + /// does not throw an error for unsorted indices. On GPU, out-of-order indices + /// result in safe but unspecified behavior, which may include treating + /// out-of-order indices as the same as a smaller following index. + /// + ///
+ /// + ///
+ /// + /// For example: + /// + /// >>> c = tf.constant([[1,2,3,4], [4, 3, 2, 1], [5,6,7,8]]) + /// >>> tf.math.segment_min(c, tf.constant([0, 0, 1])).numpy() + /// array([[1, 2, 2, 1], + /// [5, 6, 7, 8]], dtype=int32) + /// + /// + ///
+ /// + /// + /// + public static Tensor segment_min(Tensor data, Tensor segment_ids, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "SegmentMin", name) { args = new object[] { data, segment_ids }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return segment_min_eager_fallback(data, segment_ids, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["data"] = data; + keywords["segment_ids"] = segment_ids; + var _op = tf.OpDefLib._apply_op_helper("SegmentMin", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "Tindices", _op._get_attr_type("Tindices") }; + _execute.record_gradient("SegmentMin", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor segment_min_eager_fallback(Tensor data, Tensor segment_ids, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { data, segment_ids }; + object[] _attrs = new object[] { "T", data.dtype, "Tindices", segment_ids.dtype }; + var _result = _execute.execute("SegmentMin", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("SegmentMin", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes the product along segments of a tensor. + /// + /// + /// + /// Read + /// [the section on segmentation](https://tensorflow.org/api_docs/python/tf/math#Segmentation) + /// for an explanation of segments. + /// + /// Computes a tensor such that + /// \(output_i = prod_j data_j\) where the product is over `j` such + /// that `segment_ids[j] == i`. + /// + /// If the product is empty for a given segment ID `i`, `output[i] = 1`. + /// + /// Caution: On CPU, values in `segment_ids` are always validated to be sorted, + /// and an error is thrown for indices that are not increasing. On GPU, this + /// does not throw an error for unsorted indices. On GPU, out-of-order indices + /// result in safe but unspecified behavior, which may include treating + /// out-of-order indices as the same as a smaller following index. + /// + ///
+ /// + ///
+ /// + /// For example: + /// + /// >>> c = tf.constant([[1,2,3,4], [4, 3, 2, 1], [5,6,7,8]]) + /// >>> tf.math.segment_prod(c, tf.constant([0, 0, 1])).numpy() + /// array([[4, 6, 6, 4], + /// [5, 6, 7, 8]], dtype=int32) + /// + /// + ///
+ /// + /// + /// + public static Tensor segment_prod(Tensor data, Tensor segment_ids, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "SegmentProd", name) { args = new object[] { data, segment_ids }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return segment_prod_eager_fallback(data, segment_ids, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["data"] = data; + keywords["segment_ids"] = segment_ids; + var _op = tf.OpDefLib._apply_op_helper("SegmentProd", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "Tindices", _op._get_attr_type("Tindices") }; + _execute.record_gradient("SegmentProd", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor segment_prod_eager_fallback(Tensor data, Tensor segment_ids, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { data, segment_ids }; + object[] _attrs = new object[] { "T", data.dtype, "Tindices", segment_ids.dtype }; + var _result = _execute.execute("SegmentProd", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("SegmentProd", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes the sum along segments of a tensor. + /// + /// + /// + /// Read + /// [the section on segmentation](https://tensorflow.org/api_docs/python/tf/math#Segmentation) + /// for an explanation of segments. + /// + /// Computes a tensor such that + /// \(output_i = sum_j data_j\) where sum is over `j` such + /// that `segment_ids[j] == i`. + /// + /// If the sum is empty for a given segment ID `i`, `output[i] = 0`. + /// + /// Caution: On CPU, values in `segment_ids` are always validated to be sorted, + /// and an error is thrown for indices that are not increasing. On GPU, this + /// does not throw an error for unsorted indices. On GPU, out-of-order indices + /// result in safe but unspecified behavior, which may include treating + /// out-of-order indices as the same as a smaller following index. + /// + ///
+ /// + ///
+ /// + /// For example: + /// + /// >>> c = tf.constant([[1,2,3,4], [4, 3, 2, 1], [5,6,7,8]]) + /// >>> tf.math.segment_sum(c, tf.constant([0, 0, 1])).numpy() + /// array([[5, 5, 5, 5], + /// [5, 6, 7, 8]], dtype=int32) + /// + /// + ///
+ /// + /// + /// + public static Tensor segment_sum(Tensor data, Tensor segment_ids, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "SegmentSum", name) { args = new object[] { data, segment_ids }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return segment_sum_eager_fallback(data, segment_ids, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["data"] = data; + keywords["segment_ids"] = segment_ids; + var _op = tf.OpDefLib._apply_op_helper("SegmentSum", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "Tindices", _op._get_attr_type("Tindices") }; + _execute.record_gradient("SegmentSum", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor segment_sum_eager_fallback(Tensor data, Tensor segment_ids, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { data, segment_ids }; + object[] _attrs = new object[] { "T", data.dtype, "Tindices", segment_ids.dtype }; + var _result = _execute.execute("SegmentSum", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("SegmentSum", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Selects elements from `t` or `e`, depending on `condition`. + /// + /// + /// + /// The `t`, and `e` tensors must all have the same shape, and the + /// output will also have that shape. + /// + /// The `condition` tensor must be a scalar if `t` and `e` are scalars. + /// If `t` and `e` are vectors or higher rank, then `condition` must be either a + /// scalar, a vector with size matching the first dimension of `t`, or must have + /// the same shape as `t`. + /// + /// The `condition` tensor acts as a mask that chooses, based on the value at each + /// element, whether the corresponding element / row in the output should be + /// taken from `t` (if true) or `e` (if false). + /// + /// If `condition` is a vector and `t` and `e` are higher rank matrices, then + /// it chooses which row (outer dimension) to copy from `t` and `e`. + /// If `condition` has the same shape as `t` and `e`, then it chooses which + /// element to copy from `t` and `e`. + /// + /// For example: + /// + /// ```python + /// # 'condition' tensor is [[True, False] + /// # [False, True]] + /// # 't' is [[1, 2], + /// # [3, 4]] + /// # 'e' is [[5, 6], + /// # [7, 8]] + /// select(condition, t, e) # => [[1, 6], [7, 4]] + /// + /// + /// # 'condition' tensor is [True, False] + /// # 't' is [[1, 2], + /// # [3, 4]] + /// # 'e' is [[5, 6], + /// # [7, 8]] + /// select(condition, t, e) ==> [[1, 2], + /// [7, 8]] + /// + /// ``` + /// + /// + /// + /// + /// + /// + public static Tensor select(Tensor condition, Tensor t, Tensor e, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Select", name) { args = new object[] { condition, t, e }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return select_eager_fallback(condition, t, e, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["condition"] = condition; + keywords["t"] = t; + keywords["e"] = e; + var _op = tf.OpDefLib._apply_op_helper("Select", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Select", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor select_eager_fallback(Tensor condition, Tensor t, Tensor e, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { condition, t, e }; + object[] _attrs = new object[] { "T", t.dtype }; + var _result = _execute.execute("Select", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Select", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// + /// + /// + /// + /// + /// + public static Tensor select_v2(Tensor condition, Tensor t, Tensor e, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "SelectV2", name) { args = new object[] { condition, t, e }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return select_v2_eager_fallback(condition, t, e, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["condition"] = condition; + keywords["t"] = t; + keywords["e"] = e; + var _op = tf.OpDefLib._apply_op_helper("SelectV2", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("SelectV2", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor select_v2_eager_fallback(Tensor condition, Tensor t, Tensor e, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { condition, t, e }; + object[] _attrs = new object[] { "T", t.dtype }; + var _result = _execute.execute("SelectV2", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("SelectV2", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes sigmoid of `x` element-wise. + /// + /// + /// + /// Specifically, `y = 1 / (1 + exp(-x))`. + /// + /// + /// + /// + public static Tensor sigmoid(Tensor x, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Sigmoid", name) { args = new object[] { x }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return sigmoid_eager_fallback(x, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + var _op = tf.OpDefLib._apply_op_helper("Sigmoid", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Sigmoid", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor sigmoid_eager_fallback(Tensor x, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("Sigmoid", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Sigmoid", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes the gradient of the sigmoid of `x` wrt its input. + /// + /// + /// + /// Specifically, `grad = dy * y * (1 - y)`, where `y = sigmoid(x)`, and + /// `dy` is the corresponding input gradient. + /// + /// + /// + /// + /// + public static Tensor sigmoid_grad(Tensor y, Tensor dy, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "SigmoidGrad", name) { args = new object[] { y, dy }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return sigmoid_grad_eager_fallback(y, dy, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["y"] = y; + keywords["dy"] = dy; + var _op = tf.OpDefLib._apply_op_helper("SigmoidGrad", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("SigmoidGrad", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor sigmoid_grad_eager_fallback(Tensor y, Tensor dy, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { y, dy }; + object[] _attrs = new object[] { "T", y.dtype }; + var _result = _execute.execute("SigmoidGrad", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("SigmoidGrad", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns an element-wise indication of the sign of a number. + /// + /// + /// + /// `y = sign(x) = -1` if `x < 0`; 0 if `x == 0`; 1 if `x > 0`. + /// + /// For complex numbers, `y = sign(x) = x / |x|` if `x != 0`, otherwise `y = 0`. + /// + /// Example usage: + /// >>> tf.math.sign([0., 2., -3.]) + /// + /// + /// + /// + /// + public static Tensor sign(Tensor x, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Sign", name) { args = new object[] { x }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return sign_eager_fallback(x, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + var _op = tf.OpDefLib._apply_op_helper("Sign", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Sign", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor sign_eager_fallback(Tensor x, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("Sign", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Sign", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes sine of x element-wise. + /// + /// + /// + /// Given an input tensor, this function computes sine of every + /// element in the tensor. Input range is `(-inf, inf)` and + /// output range is `[-1,1]`. + /// + /// ```python + /// x = tf.constant([-float("inf"), -9, -0.5, 1, 1.2, 200, 10, float("inf")]) + /// tf.math.sin(x) ==> [nan -0.4121185 -0.47942555 0.84147096 0.9320391 -0.87329733 -0.54402107 nan] + /// ``` + /// + /// + /// + /// + public static Tensor sin(Tensor x, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Sin", name) { args = new object[] { x }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return sin_eager_fallback(x, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + var _op = tf.OpDefLib._apply_op_helper("Sin", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Sin", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor sin_eager_fallback(Tensor x, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("Sin", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Sin", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes hyperbolic sine of x element-wise. + /// + /// + /// + /// Given an input tensor, this function computes hyperbolic sine of every + /// element in the tensor. Input range is `[-inf,inf]` and output range + /// is `[-inf,inf]`. + /// + /// ```python + /// x = tf.constant([-float("inf"), -9, -0.5, 1, 1.2, 2, 10, float("inf")]) + /// tf.math.sinh(x) ==> [-inf -4.0515420e+03 -5.2109528e-01 1.1752012e+00 1.5094614e+00 3.6268604e+00 1.1013232e+04 inf] + /// ``` + /// + /// + /// + /// + public static Tensor sinh(Tensor x, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Sinh", name) { args = new object[] { x }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return sinh_eager_fallback(x, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + var _op = tf.OpDefLib._apply_op_helper("Sinh", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Sinh", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor sinh_eager_fallback(Tensor x, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("Sinh", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Sinh", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Generates points from the Sobol sequence. + /// + /// + /// + /// Creates a Sobol sequence with `num_results` samples. Each sample has dimension + /// `dim`. Skips the first `skip` samples. + /// + /// + /// + /// + /// + /// + /// + /// The type of the sample. One of: `float32` or `float64`. + /// + /// + /// + public static Tensor sobol_sample(Tensor dim, Tensor num_results, Tensor skip, TF_DataType dtype = TF_DataType.TF_FLOAT, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "SobolSample", name) { args = new object[] { dim, num_results, skip }, attrs = new Dictionary() { ["dtype"] = dtype } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return sobol_sample_eager_fallback(dim, num_results, skip, dtype: dtype, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["dim"] = dim; + keywords["num_results"] = num_results; + keywords["skip"] = skip; + keywords["dtype"] = dtype; + var _op = tf.OpDefLib._apply_op_helper("SobolSample", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "dtype", _op._get_attr_type("dtype") }; + _execute.record_gradient("SobolSample", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor sobol_sample_eager_fallback(Tensor dim, Tensor num_results, Tensor skip, TF_DataType dtype, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { dim, num_results, skip }; + object[] _attrs = new object[] { "dtype", dtype }; + var _result = _execute.execute("SobolSample", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("SobolSample", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Counts the number of occurrences of each value in an integer array. + /// + /// + /// + /// Outputs a vector with length `size` and the same dtype as `weights`. If + /// `weights` are empty, then index `i` stores the number of times the value `i` is + /// counted in `arr`. If `weights` are non-empty, then index `i` stores the sum of + /// the value in `weights` at each index where the corresponding value in `arr` is + /// `i`. + /// + /// Values in `arr` outside of the range [0, size) are ignored. + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// bool; Whether the kernel should count the appearance or number of occurrences. + /// + /// + /// + public static Tensor sparse_bincount(Tensor indices, Tensor values, Tensor dense_shape, Tensor size, Tensor weights, bool binary_output = false, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "SparseBincount", name) { args = new object[] { indices, values, dense_shape, size, weights }, attrs = new Dictionary() { ["binary_output"] = binary_output } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return sparse_bincount_eager_fallback(indices, values, dense_shape, size, weights, binary_output: binary_output, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["indices"] = indices; + keywords["values"] = values; + keywords["dense_shape"] = dense_shape; + keywords["size"] = size; + keywords["weights"] = weights; + keywords["binary_output"] = binary_output; + var _op = tf.OpDefLib._apply_op_helper("SparseBincount", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "Tidx", _op._get_attr_type("Tidx"), "T", _op._get_attr_type("T"), "binary_output", _op._get_attr_bool("binary_output") }; + _execute.record_gradient("SparseBincount", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor sparse_bincount_eager_fallback(Tensor indices, Tensor values, Tensor dense_shape, Tensor size, Tensor weights, bool binary_output, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { indices, values, dense_shape, size, weights }; + object[] _attrs = new object[] { "Tidx", values.dtype, "T", weights.dtype, "binary_output", binary_output }; + var _result = _execute.execute("SparseBincount", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("SparseBincount", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Multiply matrix "a" by matrix "b". + /// + /// + /// + /// The inputs must be two-dimensional matrices and the inner dimension of "a" must + /// match the outer dimension of "b". Both "a" and "b" must be `Tensor`s not + /// `SparseTensor`s. This op is optimized for the case where at least one of "a" or + /// "b" is sparse, in the sense that they have a large proportion of zero values. + /// The breakeven for using this versus a dense matrix multiply on one platform was + /// 30% zero values in the sparse matrix. + /// + /// The gradient computation of this operation will only take advantage of sparsity + /// in the input gradient when that gradient comes from a Relu. + /// + /// + /// + /// + /// + /// + /// + /// + /// + public static Tensor sparse_mat_mul(Tensor a, Tensor b, bool transpose_a = false, bool transpose_b = false, bool a_is_sparse = false, bool b_is_sparse = false, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "SparseMatMul", name) { args = new object[] { a, b }, attrs = new Dictionary() { ["transpose_a"] = transpose_a, ["transpose_b"] = transpose_b, ["a_is_sparse"] = a_is_sparse, ["b_is_sparse"] = b_is_sparse } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return sparse_mat_mul_eager_fallback(a, b, transpose_a: transpose_a, transpose_b: transpose_b, a_is_sparse: a_is_sparse, b_is_sparse: b_is_sparse, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["a"] = a; + keywords["b"] = b; + keywords["transpose_a"] = transpose_a; + keywords["transpose_b"] = transpose_b; + keywords["a_is_sparse"] = a_is_sparse; + keywords["b_is_sparse"] = b_is_sparse; + var _op = tf.OpDefLib._apply_op_helper("SparseMatMul", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "transpose_a", _op._get_attr_bool("transpose_a"), "transpose_b", _op._get_attr_bool("transpose_b"), "a_is_sparse", _op._get_attr_bool("a_is_sparse"), "b_is_sparse", _op._get_attr_bool("b_is_sparse"), "Ta", _op._get_attr_type("Ta"), "Tb", _op._get_attr_type("Tb") }; + _execute.record_gradient("SparseMatMul", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor sparse_mat_mul_eager_fallback(Tensor a, Tensor b, bool transpose_a, bool transpose_b, bool a_is_sparse, bool b_is_sparse, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { a, b }; + object[] _attrs = new object[] { "transpose_a", transpose_a, "transpose_b", transpose_b, "a_is_sparse", a_is_sparse, "b_is_sparse", b_is_sparse, "Ta", a.dtype, "Tb", b.dtype }; + var _result = _execute.execute("SparseMatMul", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("SparseMatMul", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes the mean along sparse segments of a tensor. + /// + /// + /// + /// See `tf.sparse.segment_sum` for usage examples. + /// + /// Like `SegmentMean`, but `segment_ids` can have rank less than `data`'s first + /// dimension, selecting a subset of dimension 0, specified by `indices`. + /// + /// + /// + /// + /// + /// + public static Tensor sparse_segment_mean(Tensor data, Tensor indices, Tensor segment_ids, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "SparseSegmentMean", name) { args = new object[] { data, indices, segment_ids }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return sparse_segment_mean_eager_fallback(data, indices, segment_ids, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["data"] = data; + keywords["indices"] = indices; + keywords["segment_ids"] = segment_ids; + var _op = tf.OpDefLib._apply_op_helper("SparseSegmentMean", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "Tidx", _op._get_attr_type("Tidx"), "Tsegmentids", _op._get_attr_type("Tsegmentids") }; + _execute.record_gradient("SparseSegmentMean", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor sparse_segment_mean_eager_fallback(Tensor data, Tensor indices, Tensor segment_ids, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { data, indices, segment_ids }; + object[] _attrs = new object[] { "T", data.dtype, "Tidx", indices.dtype, "Tsegmentids", segment_ids.dtype }; + var _result = _execute.execute("SparseSegmentMean", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("SparseSegmentMean", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes gradients for SparseSegmentMean. + /// + /// + /// + /// Returns tensor "output" with same shape as grad, except for dimension 0 whose + /// value is output_dim0. + /// + /// + /// + /// + /// + /// + /// + public static Tensor sparse_segment_mean_grad(Tensor grad, Tensor indices, Tensor segment_ids, Tensor output_dim0, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "SparseSegmentMeanGrad", name) { args = new object[] { grad, indices, segment_ids, output_dim0 }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return sparse_segment_mean_grad_eager_fallback(grad, indices, segment_ids, output_dim0, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["grad"] = grad; + keywords["indices"] = indices; + keywords["segment_ids"] = segment_ids; + keywords["output_dim0"] = output_dim0; + var _op = tf.OpDefLib._apply_op_helper("SparseSegmentMeanGrad", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "Tidx", _op._get_attr_type("Tidx"), "Tsegmentids", _op._get_attr_type("Tsegmentids") }; + _execute.record_gradient("SparseSegmentMeanGrad", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor sparse_segment_mean_grad_eager_fallback(Tensor grad, Tensor indices, Tensor segment_ids, Tensor output_dim0, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { grad, indices, segment_ids, output_dim0 }; + object[] _attrs = new object[] { "T", grad.dtype, "Tidx", indices.dtype, "Tsegmentids", segment_ids.dtype }; + var _result = _execute.execute("SparseSegmentMeanGrad", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("SparseSegmentMeanGrad", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes the mean along sparse segments of a tensor. + /// + /// + /// + /// Like `SparseSegmentMean`, but allows missing ids in `segment_ids`. If an id is + /// missing, the `output` tensor at that position will be zeroed. + /// + /// Read + /// [the section on segmentation](https://tensorflow.org/api_docs/python/tf/math#Segmentation) + /// for an explanation of segments. + /// + /// + /// + /// + /// + /// + /// + public static Tensor sparse_segment_mean_with_num_segments(Tensor data, Tensor indices, Tensor segment_ids, Tensor num_segments, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "SparseSegmentMeanWithNumSegments", name) { args = new object[] { data, indices, segment_ids, num_segments }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return sparse_segment_mean_with_num_segments_eager_fallback(data, indices, segment_ids, num_segments, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["data"] = data; + keywords["indices"] = indices; + keywords["segment_ids"] = segment_ids; + keywords["num_segments"] = num_segments; + var _op = tf.OpDefLib._apply_op_helper("SparseSegmentMeanWithNumSegments", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "Tidx", _op._get_attr_type("Tidx"), "Tnumsegments", _op._get_attr_type("Tnumsegments"), "Tsegmentids", _op._get_attr_type("Tsegmentids") }; + _execute.record_gradient("SparseSegmentMeanWithNumSegments", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor sparse_segment_mean_with_num_segments_eager_fallback(Tensor data, Tensor indices, Tensor segment_ids, Tensor num_segments, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { data, indices, segment_ids, num_segments }; + object[] _attrs = new object[] { "T", data.dtype, "Tidx", indices.dtype, "Tnumsegments", num_segments.dtype, "Tsegmentids", segment_ids.dtype }; + var _result = _execute.execute("SparseSegmentMeanWithNumSegments", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("SparseSegmentMeanWithNumSegments", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes the sum along sparse segments of a tensor divided by the sqrt of N. + /// + /// + /// + /// N is the size of the segment being reduced. + /// + /// See `tf.sparse.segment_sum` for usage examples. + /// + /// + /// + /// + /// + /// + /// + public static Tensor sparse_segment_sqrt_n(Tensor data, Tensor indices, Tensor segment_ids, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "SparseSegmentSqrtN", name) { args = new object[] { data, indices, segment_ids }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return sparse_segment_sqrt_n_eager_fallback(data, indices, segment_ids, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["data"] = data; + keywords["indices"] = indices; + keywords["segment_ids"] = segment_ids; + var _op = tf.OpDefLib._apply_op_helper("SparseSegmentSqrtN", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "Tidx", _op._get_attr_type("Tidx"), "Tsegmentids", _op._get_attr_type("Tsegmentids") }; + _execute.record_gradient("SparseSegmentSqrtN", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor sparse_segment_sqrt_n_eager_fallback(Tensor data, Tensor indices, Tensor segment_ids, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { data, indices, segment_ids }; + object[] _attrs = new object[] { "T", data.dtype, "Tidx", indices.dtype, "Tsegmentids", segment_ids.dtype }; + var _result = _execute.execute("SparseSegmentSqrtN", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("SparseSegmentSqrtN", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes the sum along sparse segments of a tensor. + /// + /// + /// + /// Read + /// [the section on segmentation](https://tensorflow.org/api_docs/python/tf/math#Segmentation) + /// for an explanation of segments. + /// + /// Like `SegmentSum`, but `segment_ids` can have rank less than `data`'s first + /// dimension, selecting a subset of dimension 0, specified by `indices`. + /// + /// For example: + /// + /// ```python + /// c = tf.constant([[1,2,3,4], [-1,-2,-3,-4], [5,6,7,8]]) + /// + /// # Select two rows, one segment. + /// tf.sparse_segment_sum(c, tf.constant([0, 1]), tf.constant([0, 0])) + /// # => [[0 0 0 0]] + /// + /// # Select two rows, two segment. + /// tf.sparse_segment_sum(c, tf.constant([0, 1]), tf.constant([0, 1])) + /// # => [[ 1 2 3 4] + /// # [-1 -2 -3 -4]] + /// + /// # Select all rows, two segments. + /// tf.sparse_segment_sum(c, tf.constant([0, 1, 2]), tf.constant([0, 0, 1])) + /// # => [[0 0 0 0] + /// # [5 6 7 8]] + /// + /// # Which is equivalent to: + /// tf.segment_sum(c, tf.constant([0, 0, 1])) + /// ``` + /// + /// + /// + /// + /// + /// + public static Tensor sparse_segment_sum(Tensor data, Tensor indices, Tensor segment_ids, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "SparseSegmentSum", name) { args = new object[] { data, indices, segment_ids }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return sparse_segment_sum_eager_fallback(data, indices, segment_ids, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["data"] = data; + keywords["indices"] = indices; + keywords["segment_ids"] = segment_ids; + var _op = tf.OpDefLib._apply_op_helper("SparseSegmentSum", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "Tidx", _op._get_attr_type("Tidx"), "Tsegmentids", _op._get_attr_type("Tsegmentids") }; + _execute.record_gradient("SparseSegmentSum", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor sparse_segment_sum_eager_fallback(Tensor data, Tensor indices, Tensor segment_ids, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { data, indices, segment_ids }; + object[] _attrs = new object[] { "T", data.dtype, "Tidx", indices.dtype, "Tsegmentids", segment_ids.dtype }; + var _result = _execute.execute("SparseSegmentSum", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("SparseSegmentSum", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes gradients for SparseSegmentSum. + /// + /// + /// + /// Returns tensor "output" with same shape as grad, except for dimension 0 whose + /// value is output_dim0. + /// + /// + /// + /// + /// + /// + /// + public static Tensor sparse_segment_sum_grad(Tensor grad, Tensor indices, Tensor segment_ids, Tensor output_dim0, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "SparseSegmentSumGrad", name) { args = new object[] { grad, indices, segment_ids, output_dim0 }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return sparse_segment_sum_grad_eager_fallback(grad, indices, segment_ids, output_dim0, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["grad"] = grad; + keywords["indices"] = indices; + keywords["segment_ids"] = segment_ids; + keywords["output_dim0"] = output_dim0; + var _op = tf.OpDefLib._apply_op_helper("SparseSegmentSumGrad", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "Tidx", _op._get_attr_type("Tidx"), "Tsegmentids", _op._get_attr_type("Tsegmentids") }; + _execute.record_gradient("SparseSegmentSumGrad", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor sparse_segment_sum_grad_eager_fallback(Tensor grad, Tensor indices, Tensor segment_ids, Tensor output_dim0, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { grad, indices, segment_ids, output_dim0 }; + object[] _attrs = new object[] { "T", grad.dtype, "Tidx", indices.dtype, "Tsegmentids", segment_ids.dtype }; + var _result = _execute.execute("SparseSegmentSumGrad", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("SparseSegmentSumGrad", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes the sum along sparse segments of a tensor. + /// + /// + /// + /// Like `SparseSegmentSum`, but allows missing ids in `segment_ids`. If an id is + /// missing, the `output` tensor at that position will be zeroed. + /// + /// Read + /// [the section on segmentation](https://tensorflow.org/api_docs/python/tf/sparse#Segmentation) + /// for an explanation of segments. + /// + /// For example: + /// + /// ```python + /// c = tf.constant([[1,2,3,4], [-1,-2,-3,-4], [5,6,7,8]]) + /// + /// tf.sparse_segment_sum_with_num_segments( + /// c, tf.constant([0, 1]), tf.constant([0, 0]), num_segments=3) + /// # => [[0 0 0 0] + /// # [0 0 0 0] + /// # [0 0 0 0]] + /// + /// tf.sparse_segment_sum_with_num_segments(c, + /// tf.constant([0, 1]), + /// tf.constant([0, 2], + /// num_segments=4)) + /// # => [[ 1 2 3 4] + /// # [ 0 0 0 0] + /// # [-1 -2 -3 -4] + /// # [ 0 0 0 0]] + /// ``` + /// + /// + /// + /// + /// + /// + /// + public static Tensor sparse_segment_sum_with_num_segments(Tensor data, Tensor indices, Tensor segment_ids, Tensor num_segments, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "SparseSegmentSumWithNumSegments", name) { args = new object[] { data, indices, segment_ids, num_segments }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return sparse_segment_sum_with_num_segments_eager_fallback(data, indices, segment_ids, num_segments, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["data"] = data; + keywords["indices"] = indices; + keywords["segment_ids"] = segment_ids; + keywords["num_segments"] = num_segments; + var _op = tf.OpDefLib._apply_op_helper("SparseSegmentSumWithNumSegments", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "Tidx", _op._get_attr_type("Tidx"), "Tnumsegments", _op._get_attr_type("Tnumsegments"), "Tsegmentids", _op._get_attr_type("Tsegmentids") }; + _execute.record_gradient("SparseSegmentSumWithNumSegments", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor sparse_segment_sum_with_num_segments_eager_fallback(Tensor data, Tensor indices, Tensor segment_ids, Tensor num_segments, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { data, indices, segment_ids, num_segments }; + object[] _attrs = new object[] { "T", data.dtype, "Tidx", indices.dtype, "Tnumsegments", num_segments.dtype, "Tsegmentids", segment_ids.dtype }; + var _result = _execute.execute("SparseSegmentSumWithNumSegments", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("SparseSegmentSumWithNumSegments", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes square root of x element-wise. + /// + /// + /// + /// I.e., \(y = sqrt{x} = x^{1/2}\). + /// + /// + /// + /// + public static Tensor sqrt(Tensor x, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Sqrt", name) { args = new object[] { x }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return sqrt_eager_fallback(x, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + var _op = tf.OpDefLib._apply_op_helper("Sqrt", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Sqrt", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor sqrt_eager_fallback(Tensor x, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("Sqrt", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Sqrt", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes the gradient for the sqrt of `x` wrt its input. + /// + /// + /// + /// Specifically, `grad = dy * 0.5 / y`, where `y = sqrt(x)`, and `dy` + /// is the corresponding input gradient. + /// + /// + /// + /// + /// + public static Tensor sqrt_grad(Tensor y, Tensor dy, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "SqrtGrad", name) { args = new object[] { y, dy }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return sqrt_grad_eager_fallback(y, dy, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["y"] = y; + keywords["dy"] = dy; + var _op = tf.OpDefLib._apply_op_helper("SqrtGrad", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("SqrtGrad", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor sqrt_grad_eager_fallback(Tensor y, Tensor dy, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { y, dy }; + object[] _attrs = new object[] { "T", y.dtype }; + var _result = _execute.execute("SqrtGrad", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("SqrtGrad", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes square of x element-wise. + /// + /// + /// + /// I.e., \(y = x * x = x^2\). + /// + /// + /// + /// + public static Tensor square(Tensor x, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Square", name) { args = new object[] { x }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return square_eager_fallback(x, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + var _op = tf.OpDefLib._apply_op_helper("Square", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Square", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor square_eager_fallback(Tensor x, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("Square", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Square", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns conj(x - y)(x - y) element-wise. + /// + /// + /// + /// *NOTE*: `SquaredDifference` supports broadcasting. More about broadcasting + /// [here](http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html) + /// + /// + /// + /// + /// + public static Tensor squared_difference(Tensor x, Tensor y, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "SquaredDifference", name) { args = new object[] { x, y }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return squared_difference_eager_fallback(x, y, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + keywords["y"] = y; + var _op = tf.OpDefLib._apply_op_helper("SquaredDifference", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("SquaredDifference", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor squared_difference_eager_fallback(Tensor x, Tensor y, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x, y }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("SquaredDifference", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("SquaredDifference", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns x - y element-wise. + /// + /// + /// + /// *NOTE*: `Sub` supports broadcasting. More about broadcasting + /// [here](http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html) + /// + /// + /// + /// + /// + public static Tensor sub(Tensor x, Tensor y, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Sub", name) { args = new object[] { x, y }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return sub_eager_fallback(x, y, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + keywords["y"] = y; + var _op = tf.OpDefLib._apply_op_helper("Sub", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Sub", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor sub_eager_fallback(Tensor x, Tensor y, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x, y }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("Sub", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Sub", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes the sum of elements across dimensions of a tensor. + /// + /// + /// + /// Reduces `input` along the dimensions given in `reduction_indices`. Unless + /// `keep_dims` is true, the rank of the tensor is reduced by 1 for each entry in + /// `reduction_indices`. If `keep_dims` is true, the reduced dimensions are + /// retained with length 1. + /// + /// + /// + /// + /// + /// + /// If true, retain reduced dimensions with length 1. + /// + /// + /// + public static Tensor sum(Tensor input, Tensor reduction_indices, bool keep_dims = false, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Sum", name) { args = new object[] { input, reduction_indices }, attrs = new Dictionary() { ["keep_dims"] = keep_dims } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return sum_eager_fallback(input, reduction_indices, keep_dims: keep_dims, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["reduction_indices"] = reduction_indices; + keywords["keep_dims"] = keep_dims; + var _op = tf.OpDefLib._apply_op_helper("Sum", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "keep_dims", _op._get_attr_bool("keep_dims"), "T", _op._get_attr_type("T"), "Tidx", _op._get_attr_type("Tidx") }; + _execute.record_gradient("Sum", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor sum_eager_fallback(Tensor input, Tensor reduction_indices, bool keep_dims, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, reduction_indices }; + object[] _attrs = new object[] { "keep_dims", keep_dims, "T", input.dtype, "Tidx", reduction_indices.dtype }; + var _result = _execute.execute("Sum", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Sum", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes tan of x element-wise. + /// + /// + /// + /// Given an input tensor, this function computes tangent of every + /// element in the tensor. Input range is `(-inf, inf)` and + /// output range is `(-inf, inf)`. If input lies outside the boundary, `nan` + /// is returned. + /// + /// ```python + /// x = tf.constant([-float("inf"), -9, -0.5, 1, 1.2, 200, 10000, float("inf")]) + /// tf.math.tan(x) ==> [nan 0.45231566 -0.5463025 1.5574077 2.572152 -1.7925274 0.32097113 nan] + /// ``` + /// + /// + /// + /// + public static Tensor tan(Tensor x, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Tan", name) { args = new object[] { x }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return tan_eager_fallback(x, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + var _op = tf.OpDefLib._apply_op_helper("Tan", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Tan", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor tan_eager_fallback(Tensor x, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("Tan", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Tan", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes hyperbolic tangent of `x` element-wise. + /// + /// + /// + /// Given an input tensor, this function computes hyperbolic tangent of every + /// element in the tensor. Input range is `[-inf, inf]` and + /// output range is `[-1,1]`. + /// + /// >>> x = tf.constant([-float("inf"), -5, -0.5, 1, 1.2, 2, 3, float("inf")]) + /// >>> tf.math.tanh(x) + /// + /// + /// + /// + /// + /// + public static Tensor tanh(Tensor x, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Tanh", name) { args = new object[] { x }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return tanh_eager_fallback(x, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + var _op = tf.OpDefLib._apply_op_helper("Tanh", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Tanh", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor tanh_eager_fallback(Tensor x, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("Tanh", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Tanh", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes the gradient for the tanh of `x` wrt its input. + /// + /// + /// + /// Specifically, `grad = dy * (1 - y*y)`, where `y = tanh(x)`, and `dy` + /// is the corresponding input gradient. + /// + /// + /// + /// + /// + public static Tensor tanh_grad(Tensor y, Tensor dy, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "TanhGrad", name) { args = new object[] { y, dy }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return tanh_grad_eager_fallback(y, dy, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["y"] = y; + keywords["dy"] = dy; + var _op = tf.OpDefLib._apply_op_helper("TanhGrad", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("TanhGrad", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor tanh_grad_eager_fallback(Tensor y, Tensor dy, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { y, dy }; + object[] _attrs = new object[] { "T", y.dtype }; + var _result = _execute.execute("TanhGrad", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("TanhGrad", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns x / y element-wise for integer types. + /// + /// + /// + /// Truncation designates that negative numbers will round fractional quantities + /// toward zero. I.e. -7 / 5 = -1. This matches C semantics but it is different + /// than Python semantics. See `FloorDiv` for a division function that matches + /// Python Semantics. + /// + /// *NOTE*: `TruncateDiv` supports broadcasting. More about broadcasting + /// [here](http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html) + /// + /// + /// + /// + /// + public static Tensor truncate_div(Tensor x, Tensor y, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "TruncateDiv", name) { args = new object[] { x, y }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return truncate_div_eager_fallback(x, y, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + keywords["y"] = y; + var _op = tf.OpDefLib._apply_op_helper("TruncateDiv", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("TruncateDiv", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor truncate_div_eager_fallback(Tensor x, Tensor y, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x, y }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("TruncateDiv", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("TruncateDiv", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns element-wise remainder of division. This emulates C semantics in that + /// + /// + /// + /// the result here is consistent with a truncating divide. E.g. `truncate(x / y) * + /// y + truncate_mod(x, y) = x`. + /// + /// *NOTE*: `TruncateMod` supports broadcasting. More about broadcasting + /// [here](http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html) + /// + /// + /// + /// + /// + public static Tensor truncate_mod(Tensor x, Tensor y, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "TruncateMod", name) { args = new object[] { x, y }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return truncate_mod_eager_fallback(x, y, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + keywords["y"] = y; + var _op = tf.OpDefLib._apply_op_helper("TruncateMod", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("TruncateMod", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor truncate_mod_eager_fallback(Tensor x, Tensor y, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x, y }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("TruncateMod", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("TruncateMod", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes the maximum along segments of a tensor. + /// + /// + /// + /// Read + /// [the section on segmentation](https://tensorflow.org/api_docs/python/tf/math#Segmentation) + /// for an explanation of segments. + /// + /// This operator is similar to `tf.math.unsorted_segment_sum`, + /// Instead of computing the sum over segments, it computes the maximum such that: + /// + /// \(output_i = max_{j...} data[j...]\) where max is over tuples `j...` such + /// that `segment_ids[j...] == i`. + /// + /// If the maximum is empty for a given segment ID `i`, it outputs the smallest + /// possible value for the specific numeric type, + /// `output[i] = numeric_limits::lowest()`. + /// + /// If the given segment ID `i` is negative, then the corresponding value is + /// dropped, and will not be included in the result. + /// + /// Caution: On CPU, values in `segment_ids` are always validated to be less than + /// `num_segments`, and an error is thrown for out-of-bound indices. On GPU, this + /// does not throw an error for out-of-bound indices. On Gpu, out-of-bound indices + /// result in safe but unspecified behavior, which may include ignoring + /// out-of-bound indices or outputting a tensor with a 0 stored in the first + /// dimension of its shape if `num_segments` is 0. + /// + ///
+ /// + ///
+ /// + /// For example: + /// + /// >>> c = tf.constant([[1,2,3,4], [5,6,7,8], [4,3,2,1]]) + /// >>> tf.math.unsorted_segment_max(c, tf.constant([0, 1, 0]), num_segments=2).numpy() + /// array([[4, 3, 3, 4], + /// [5, 6, 7, 8]], dtype=int32) + /// + /// + ///
+ /// + /// + /// + /// + public static Tensor unsorted_segment_max(Tensor data, Tensor segment_ids, Tensor num_segments, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "UnsortedSegmentMax", name) { args = new object[] { data, segment_ids, num_segments }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return unsorted_segment_max_eager_fallback(data, segment_ids, num_segments, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["data"] = data; + keywords["segment_ids"] = segment_ids; + keywords["num_segments"] = num_segments; + var _op = tf.OpDefLib._apply_op_helper("UnsortedSegmentMax", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "Tindices", _op._get_attr_type("Tindices"), "Tnumsegments", _op._get_attr_type("Tnumsegments") }; + _execute.record_gradient("UnsortedSegmentMax", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor unsorted_segment_max_eager_fallback(Tensor data, Tensor segment_ids, Tensor num_segments, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { data, segment_ids, num_segments }; + object[] _attrs = new object[] { "T", data.dtype, "Tindices", segment_ids.dtype, "Tnumsegments", num_segments.dtype }; + var _result = _execute.execute("UnsortedSegmentMax", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("UnsortedSegmentMax", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes the minimum along segments of a tensor. + /// + /// + /// + /// Read + /// [the section on segmentation](https://tensorflow.org/api_docs/python/tf/math#Segmentation) + /// for an explanation of segments. + /// + /// This operator is similar to `tf.math.unsorted_segment_sum`, + /// Instead of computing the sum over segments, it computes the minimum such that: + /// + /// \(output_i = min_{j...} data_[j...]\) where min is over tuples `j...` such + /// that `segment_ids[j...] == i`. + /// + /// If the minimum is empty for a given segment ID `i`, it outputs the largest + /// possible value for the specific numeric type, + /// `output[i] = numeric_limits::max()`. + /// + /// For example: + /// + /// >>> c = tf.constant([[1,2,3,4], [5,6,7,8], [4,3,2,1]]) + /// >>> tf.math.unsorted_segment_min(c, tf.constant([0, 1, 0]), num_segments=2).numpy() + /// array([[1, 2, 2, 1], + /// [5, 6, 7, 8]], dtype=int32) + /// + /// If the given segment ID `i` is negative, then the corresponding value is + /// dropped, and will not be included in the result. + /// + /// Caution: On CPU, values in `segment_ids` are always validated to be less than + /// `num_segments`, and an error is thrown for out-of-bound indices. On GPU, this + /// does not throw an error for out-of-bound indices. On Gpu, out-of-bound indices + /// result in safe but unspecified behavior, which may include ignoring + /// out-of-bound indices or outputting a tensor with a 0 stored in the first + /// dimension of its shape if `num_segments` is 0. + /// + /// + /// + /// + /// + /// + public static Tensor unsorted_segment_min(Tensor data, Tensor segment_ids, Tensor num_segments, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "UnsortedSegmentMin", name) { args = new object[] { data, segment_ids, num_segments }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return unsorted_segment_min_eager_fallback(data, segment_ids, num_segments, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["data"] = data; + keywords["segment_ids"] = segment_ids; + keywords["num_segments"] = num_segments; + var _op = tf.OpDefLib._apply_op_helper("UnsortedSegmentMin", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "Tindices", _op._get_attr_type("Tindices"), "Tnumsegments", _op._get_attr_type("Tnumsegments") }; + _execute.record_gradient("UnsortedSegmentMin", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor unsorted_segment_min_eager_fallback(Tensor data, Tensor segment_ids, Tensor num_segments, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { data, segment_ids, num_segments }; + object[] _attrs = new object[] { "T", data.dtype, "Tindices", segment_ids.dtype, "Tnumsegments", num_segments.dtype }; + var _result = _execute.execute("UnsortedSegmentMin", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("UnsortedSegmentMin", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes the product along segments of a tensor. + /// + /// + /// + /// Read + /// [the section on segmentation](https://tensorflow.org/api_docs/python/tf/math#Segmentation) + /// for an explanation of segments. + /// + /// This operator is similar to `tf.math.unsorted_segment_sum`, + /// Instead of computing the sum over segments, it computes the product of all + /// entries belonging to a segment such that: + /// + /// \(output_i = prod_{j...} data[j...]\) where the product is over tuples + /// `j...` such that `segment_ids[j...] == i`. + /// + /// For example: + /// + /// >>> c = tf.constant([[1,2,3,4], [5,6,7,8], [4,3,2,1]]) + /// >>> tf.math.unsorted_segment_prod(c, tf.constant([0, 1, 0]), num_segments=2).numpy() + /// array([[4, 6, 6, 4], + /// [5, 6, 7, 8]], dtype=int32) + /// + /// If there is no entry for a given segment ID `i`, it outputs 1. + /// + /// If the given segment ID `i` is negative, then the corresponding value is + /// dropped, and will not be included in the result. + /// Caution: On CPU, values in `segment_ids` are always validated to be less than + /// `num_segments`, and an error is thrown for out-of-bound indices. On GPU, this + /// does not throw an error for out-of-bound indices. On Gpu, out-of-bound indices + /// result in safe but unspecified behavior, which may include ignoring + /// out-of-bound indices or outputting a tensor with a 0 stored in the first + /// dimension of its shape if `num_segments` is 0. + /// + /// + /// + /// + /// + /// + /// + public static Tensor unsorted_segment_prod(Tensor data, Tensor segment_ids, Tensor num_segments, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "UnsortedSegmentProd", name) { args = new object[] { data, segment_ids, num_segments }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return unsorted_segment_prod_eager_fallback(data, segment_ids, num_segments, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["data"] = data; + keywords["segment_ids"] = segment_ids; + keywords["num_segments"] = num_segments; + var _op = tf.OpDefLib._apply_op_helper("UnsortedSegmentProd", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "Tindices", _op._get_attr_type("Tindices"), "Tnumsegments", _op._get_attr_type("Tnumsegments") }; + _execute.record_gradient("UnsortedSegmentProd", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor unsorted_segment_prod_eager_fallback(Tensor data, Tensor segment_ids, Tensor num_segments, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { data, segment_ids, num_segments }; + object[] _attrs = new object[] { "T", data.dtype, "Tindices", segment_ids.dtype, "Tnumsegments", num_segments.dtype }; + var _result = _execute.execute("UnsortedSegmentProd", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("UnsortedSegmentProd", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes the sum along segments of a tensor. + /// + /// + /// + /// Read + /// [the section on segmentation](https://tensorflow.org/api_docs/python/tf/math#Segmentation) + /// for an explanation of segments. + /// + /// Computes a tensor such that + /// \(output[i] = sum_{j...} data[j...]\) where the sum is over tuples `j...` such + /// that `segment_ids[j...] == i`. Unlike `SegmentSum`, `segment_ids` + /// need not be sorted and need not cover all values in the full + /// range of valid values. + /// + /// If the sum is empty for a given segment ID `i`, `output[i] = 0`. + /// If the given segment ID `i` is negative, the value is dropped and will not be + /// added to the sum of the segment. + /// + /// `num_segments` should equal the number of distinct segment IDs. + /// + /// Caution: On CPU, values in `segment_ids` are always validated to be less than + /// `num_segments`, and an error is thrown for out-of-bound indices. On GPU, this + /// does not throw an error for out-of-bound indices. On Gpu, out-of-bound indices + /// result in safe but unspecified behavior, which may include ignoring + /// out-of-bound indices or outputting a tensor with a 0 stored in the first + /// dimension of its shape if `num_segments` is 0. + /// + ///
+ /// + ///
+ /// + /// >>> c = [[1,2,3,4], [5,6,7,8], [4,3,2,1]] + /// >>> tf.math.unsorted_segment_sum(c, [0, 1, 0], num_segments=2).numpy() + /// array([[5, 5, 5, 5], + /// [5, 6, 7, 8]], dtype=int32) + /// + /// + /// + ///
+ /// + /// + /// + /// + public static Tensor unsorted_segment_sum(Tensor data, Tensor segment_ids, Tensor num_segments, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "UnsortedSegmentSum", name) { args = new object[] { data, segment_ids, num_segments }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return unsorted_segment_sum_eager_fallback(data, segment_ids, num_segments, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["data"] = data; + keywords["segment_ids"] = segment_ids; + keywords["num_segments"] = num_segments; + var _op = tf.OpDefLib._apply_op_helper("UnsortedSegmentSum", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "Tindices", _op._get_attr_type("Tindices"), "Tnumsegments", _op._get_attr_type("Tnumsegments") }; + _execute.record_gradient("UnsortedSegmentSum", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor unsorted_segment_sum_eager_fallback(Tensor data, Tensor segment_ids, Tensor num_segments, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { data, segment_ids, num_segments }; + object[] _attrs = new object[] { "T", data.dtype, "Tindices", segment_ids.dtype, "Tnumsegments", num_segments.dtype }; + var _result = _execute.execute("UnsortedSegmentSum", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("UnsortedSegmentSum", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns 0 if x == 0, and x / y otherwise, elementwise. + /// + /// + /// + /// + public static Tensor xdivy(Tensor x, Tensor y, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Xdivy", name) { args = new object[] { x, y }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return xdivy_eager_fallback(x, y, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + keywords["y"] = y; + var _op = tf.OpDefLib._apply_op_helper("Xdivy", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Xdivy", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor xdivy_eager_fallback(Tensor x, Tensor y, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x, y }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("Xdivy", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Xdivy", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns 0 if x == 0, and x * log1p(y) otherwise, elementwise. + /// + /// + /// + /// + public static Tensor xlog1py(Tensor x, Tensor y, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Xlog1py", name) { args = new object[] { x, y }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return xlog1py_eager_fallback(x, y, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + keywords["y"] = y; + var _op = tf.OpDefLib._apply_op_helper("Xlog1py", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Xlog1py", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor xlog1py_eager_fallback(Tensor x, Tensor y, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x, y }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("Xlog1py", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Xlog1py", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns 0 if x == 0, and x * log(y) otherwise, elementwise. + /// + /// + /// + /// + public static Tensor xlogy(Tensor x, Tensor y, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Xlogy", name) { args = new object[] { x, y }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return xlogy_eager_fallback(x, y, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + keywords["y"] = y; + var _op = tf.OpDefLib._apply_op_helper("Xlogy", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Xlogy", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor xlogy_eager_fallback(Tensor x, Tensor y, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x, y }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("Xlogy", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Xlogy", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Compute the Hurwitz zeta function \\(\zeta(x, q)\\). + /// + /// + /// + /// The Hurwitz zeta function is defined as: + /// + /// + /// \(zeta(x, q) = sum_{n=0}^{infty} (q + n)^{-x}\) + /// + /// + /// + /// + /// + public static Tensor zeta(Tensor x, Tensor q, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Zeta", name) { args = new object[] { x, q }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return zeta_eager_fallback(x, q, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["x"] = x; + keywords["q"] = q; + var _op = tf.OpDefLib._apply_op_helper("Zeta", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Zeta", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor zeta_eager_fallback(Tensor x, Tensor q, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x, q }; + object[] _attrs = new object[] { "T", x.dtype }; + var _result = _execute.execute("Zeta", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Zeta", _inputs_flat, _attrs, _result); + } + return _result[0]; } } diff --git a/src/TensorFlowNET.Core/Operations/gen_math_ops.eager.cs b/src/TensorFlowNET.Core/Operations/gen_math_ops.eager.cs deleted file mode 100644 index 8e6e72d12..000000000 --- a/src/TensorFlowNET.Core/Operations/gen_math_ops.eager.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System; -using static Tensorflow.Binding; - -namespace Tensorflow -{ - public static partial class gen_math_ops - { - public static Tensor mul(IntPtr x, IntPtr y, string name = null) - => tf.Context.ExecuteOp("Mul", name, new ExecuteOpArgs(x, y)); - } -} diff --git a/src/TensorFlowNET.Core/Operations/gen_nn_ops.cs b/src/TensorFlowNET.Core/Operations/gen_nn_ops.cs new file mode 100644 index 000000000..59c740c46 --- /dev/null +++ b/src/TensorFlowNET.Core/Operations/gen_nn_ops.cs @@ -0,0 +1,8493 @@ +/*Wrappers around TensorFlow ops. This file is MACHINE GENERATED! Do not edit.*/ + +using Tensorflow.Eager; +using Tensorflow.Contexts; +using Tensorflow.Exceptions; +using static Tensorflow.Binding; + +namespace Tensorflow; + +public static class gen_nn_ops +{ + /// + /// Returns min/max k values and their indices of the input operand in an approximate manner. + /// + /// + /// + /// See https://arxiv.org/abs/2206.14286 for the algorithm details. + /// This op is only optimized on TPU currently. + /// + /// + /// + /// + /// Specifies the number of min/max-k. + /// + /// + /// Integer dimension along which to search. Default: -1. + /// + /// + /// Recall target for the approximation. Range in (0,1] + /// + /// + /// When true, computes max-k; otherwise computes min-k. + /// + /// + /// + /// When set to a positive value, it overrides the size determined by + /// `input[reduction_dim]` for evaluating the recall. This option is useful when + /// the given `input` is only a subset of the overall computation in SPMD or + /// distributed pipelines, where the true input size cannot be deferred by the + /// `input` shape. + /// + /// + /// + /// + /// When true, aggregates approximate results to top-k. When false, returns the + /// approximate results. The number of the approximate results is implementation + /// defined and is greater equals to the specified `k`. + /// + /// + /// + public static Tensor[] approx_top_k(Tensor input, int k = 0, int reduction_dimension = -1, float recall_target = 0.95f, bool is_max_k = true, int reduction_input_size_override = -1, bool aggregate_to_topk = true, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "ApproxTopK", name) { args = new object[] { input }, attrs = new Dictionary() { ["k"] = k, ["reduction_dimension"] = reduction_dimension, ["recall_target"] = recall_target, ["is_max_k"] = is_max_k, ["reduction_input_size_override"] = reduction_input_size_override, ["aggregate_to_topk"] = aggregate_to_topk } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return approx_top_k_eager_fallback(input, k: k, reduction_dimension: reduction_dimension, recall_target: recall_target, is_max_k: is_max_k, reduction_input_size_override: reduction_input_size_override, aggregate_to_topk: aggregate_to_topk, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["k"] = k; + keywords["reduction_dimension"] = reduction_dimension; + keywords["recall_target"] = recall_target; + keywords["is_max_k"] = is_max_k; + keywords["reduction_input_size_override"] = reduction_input_size_override; + keywords["aggregate_to_topk"] = aggregate_to_topk; + var _op = tf.OpDefLib._apply_op_helper("ApproxTopK", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "k", _op._get_attr_int("k"), "reduction_dimension", _op._get_attr_int("reduction_dimension"), "recall_target", _op.get_attr("recall_target"), "is_max_k", _op._get_attr_bool("is_max_k"), "reduction_input_size_override", _op._get_attr_int("reduction_input_size_override"), "aggregate_to_topk", _op._get_attr_bool("aggregate_to_topk"), "T", _op._get_attr_type("T") }; + _execute.record_gradient("ApproxTopK", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] approx_top_k_eager_fallback(Tensor input, int k, int reduction_dimension, float recall_target, bool is_max_k, int reduction_input_size_override, bool aggregate_to_topk, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input }; + object[] _attrs = new object[] { "k", k, "reduction_dimension", reduction_dimension, "recall_target", recall_target, "is_max_k", is_max_k, "reduction_input_size_override", reduction_input_size_override, "aggregate_to_topk", aggregate_to_topk, "T", input.dtype }; + var _result = _execute.execute("ApproxTopK", 2, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("ApproxTopK", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// Performs average pooling on the input. + /// + /// + /// + /// Each entry in `output` is the mean of the corresponding size `ksize` + /// window in `value`. + /// + /// + /// + /// + /// + /// The size of the sliding window for each dimension of `value`. + /// + /// + /// + /// + /// The stride of the sliding window for each dimension of `value`. + /// + /// + /// + /// + /// The type of padding algorithm to use. + /// + /// + /// + /// + /// Specify the data format of the input and output data. With the + /// default format "NHWC", the data is stored in the order of: + /// [batch, in_height, in_width, in_channels]. + /// Alternatively, the format could be "NCHW", the data storage order of: + /// [batch, in_channels, in_height, in_width]. + /// + /// + /// + public static Tensor avg_pool(Tensor value, int[] ksize, int[] strides, string padding, string data_format = "NHWC", string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "AvgPool", name) { args = new object[] { value }, attrs = new Dictionary() { ["ksize"] = ksize, ["strides"] = strides, ["padding"] = padding, ["data_format"] = data_format } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return avg_pool_eager_fallback(value, ksize: ksize, strides: strides, padding: padding, data_format: data_format, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + if (data_format is null) + { + data_format = "NHWC"; + } + Dictionary keywords = new(); + keywords["value"] = value; + keywords["ksize"] = ksize; + keywords["strides"] = strides; + keywords["padding"] = padding; + keywords["data_format"] = data_format; + var _op = tf.OpDefLib._apply_op_helper("AvgPool", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "ksize", _op.get_attr("ksize"), "strides", _op.get_attr("strides"), "padding", _op.get_attr("padding"), "data_format", _op.get_attr("data_format"), "T", _op._get_attr_type("T") }; + _execute.record_gradient("AvgPool", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor avg_pool_eager_fallback(Tensor value, int[] ksize, int[] strides, string padding, string data_format, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { value }; + object[] _attrs = new object[] { "ksize", ksize, "strides", strides, "padding", padding, "data_format", data_format, "T", value.dtype }; + var _result = _execute.execute("AvgPool", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("AvgPool", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Performs 3D average pooling on the input. + /// + /// + /// + /// Each entry in `output` is the mean of the corresponding size `ksize` window in + /// `value`. + /// + /// + /// + /// + /// + /// 1-D tensor of length 5. The size of the window for each dimension of + /// the input tensor. Must have `ksize[0] = ksize[4] = 1`. + /// + /// + /// + /// + /// 1-D tensor of length 5. The stride of the sliding window for each + /// dimension of `input`. Must have `strides[0] = strides[4] = 1`. + /// + /// + /// + /// + /// The type of padding algorithm to use. + /// + /// + /// + /// + /// The data format of the input and output data. With the + /// default format "NDHWC", the data is stored in the order of: + /// [batch, in_depth, in_height, in_width, in_channels]. + /// Alternatively, the format could be "NCDHW", the data storage order is: + /// [batch, in_channels, in_depth, in_height, in_width]. + /// + /// + /// + public static Tensor avg_pool3d(Tensor input, int[] ksize, int[] strides, string padding, string data_format = "NDHWC", string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "AvgPool3D", name) { args = new object[] { input }, attrs = new Dictionary() { ["ksize"] = ksize, ["strides"] = strides, ["padding"] = padding, ["data_format"] = data_format } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return avg_pool3d_eager_fallback(input, ksize: ksize, strides: strides, padding: padding, data_format: data_format, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + if (data_format is null) + { + data_format = "NDHWC"; + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["ksize"] = ksize; + keywords["strides"] = strides; + keywords["padding"] = padding; + keywords["data_format"] = data_format; + var _op = tf.OpDefLib._apply_op_helper("AvgPool3D", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "ksize", _op.get_attr("ksize"), "strides", _op.get_attr("strides"), "padding", _op.get_attr("padding"), "data_format", _op.get_attr("data_format"), "T", _op._get_attr_type("T") }; + _execute.record_gradient("AvgPool3D", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor avg_pool3d_eager_fallback(Tensor input, int[] ksize, int[] strides, string padding, string data_format, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input }; + object[] _attrs = new object[] { "ksize", ksize, "strides", strides, "padding", padding, "data_format", data_format, "T", input.dtype }; + var _result = _execute.execute("AvgPool3D", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("AvgPool3D", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes gradients of average pooling function. + /// + /// + /// + /// + /// + /// 1-D tensor of length 5. The size of the window for each dimension of + /// the input tensor. Must have `ksize[0] = ksize[4] = 1`. + /// + /// + /// + /// + /// 1-D tensor of length 5. The stride of the sliding window for each + /// dimension of `input`. Must have `strides[0] = strides[4] = 1`. + /// + /// + /// + /// + /// The type of padding algorithm to use. + /// + /// + /// + /// + /// The data format of the input and output data. With the + /// default format "NDHWC", the data is stored in the order of: + /// [batch, in_depth, in_height, in_width, in_channels]. + /// Alternatively, the format could be "NCDHW", the data storage order is: + /// [batch, in_channels, in_depth, in_height, in_width]. + /// + /// + /// + public static Tensor avg_pool3d_grad(Tensor orig_input_shape, Tensor grad, int[] ksize, int[] strides, string padding, string data_format = "NDHWC", string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "AvgPool3DGrad", name) { args = new object[] { orig_input_shape, grad }, attrs = new Dictionary() { ["ksize"] = ksize, ["strides"] = strides, ["padding"] = padding, ["data_format"] = data_format } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return avg_pool3d_grad_eager_fallback(orig_input_shape, grad, ksize: ksize, strides: strides, padding: padding, data_format: data_format, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + if (data_format is null) + { + data_format = "NDHWC"; + } + Dictionary keywords = new(); + keywords["orig_input_shape"] = orig_input_shape; + keywords["grad"] = grad; + keywords["ksize"] = ksize; + keywords["strides"] = strides; + keywords["padding"] = padding; + keywords["data_format"] = data_format; + var _op = tf.OpDefLib._apply_op_helper("AvgPool3DGrad", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "ksize", _op.get_attr("ksize"), "strides", _op.get_attr("strides"), "padding", _op.get_attr("padding"), "data_format", _op.get_attr("data_format"), "T", _op._get_attr_type("T") }; + _execute.record_gradient("AvgPool3DGrad", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor avg_pool3d_grad_eager_fallback(Tensor orig_input_shape, Tensor grad, int[] ksize, int[] strides, string padding, string data_format, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { orig_input_shape, grad }; + object[] _attrs = new object[] { "ksize", ksize, "strides", strides, "padding", padding, "data_format", data_format, "T", grad.dtype }; + var _result = _execute.execute("AvgPool3DGrad", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("AvgPool3DGrad", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes gradients of the average pooling function. + /// + /// + /// + /// + /// + /// The size of the sliding window for each dimension of the input. + /// + /// + /// + /// + /// The stride of the sliding window for each dimension of the input. + /// + /// + /// + /// + /// The type of padding algorithm to use. + /// + /// + /// + /// + /// Specify the data format of the input and output data. With the + /// default format "NHWC", the data is stored in the order of: + /// [batch, in_height, in_width, in_channels]. + /// Alternatively, the format could be "NCHW", the data storage order of: + /// [batch, in_channels, in_height, in_width]. + /// + /// + /// + public static Tensor avg_pool_grad(Tensor orig_input_shape, Tensor grad, int[] ksize, int[] strides, string padding, string data_format = "NHWC", string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "AvgPoolGrad", name) { args = new object[] { orig_input_shape, grad }, attrs = new Dictionary() { ["ksize"] = ksize, ["strides"] = strides, ["padding"] = padding, ["data_format"] = data_format } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return avg_pool_grad_eager_fallback(orig_input_shape, grad, ksize: ksize, strides: strides, padding: padding, data_format: data_format, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + if (data_format is null) + { + data_format = "NHWC"; + } + Dictionary keywords = new(); + keywords["orig_input_shape"] = orig_input_shape; + keywords["grad"] = grad; + keywords["ksize"] = ksize; + keywords["strides"] = strides; + keywords["padding"] = padding; + keywords["data_format"] = data_format; + var _op = tf.OpDefLib._apply_op_helper("AvgPoolGrad", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "ksize", _op.get_attr("ksize"), "strides", _op.get_attr("strides"), "padding", _op.get_attr("padding"), "data_format", _op.get_attr("data_format"), "T", _op._get_attr_type("T") }; + _execute.record_gradient("AvgPoolGrad", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor avg_pool_grad_eager_fallback(Tensor orig_input_shape, Tensor grad, int[] ksize, int[] strides, string padding, string data_format, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { orig_input_shape, grad }; + object[] _attrs = new object[] { "ksize", ksize, "strides", strides, "padding", padding, "data_format", data_format, "T", grad.dtype }; + var _result = _execute.execute("AvgPoolGrad", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("AvgPoolGrad", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Batch normalization. + /// + /// + /// + /// This op is deprecated. Prefer `tf.nn.batch_normalization`. + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// A small float number to avoid dividing by 0. + /// + /// + /// + /// + /// A bool indicating whether the resulted tensor + /// needs to be multiplied with gamma. + /// + /// + /// + public static Tensor batch_norm_with_global_normalization(Tensor t, Tensor m, Tensor v, Tensor beta, Tensor gamma, float variance_epsilon, bool scale_after_normalization, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "BatchNormWithGlobalNormalization", name) { args = new object[] { t, m, v, beta, gamma }, attrs = new Dictionary() { ["variance_epsilon"] = variance_epsilon, ["scale_after_normalization"] = scale_after_normalization } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return batch_norm_with_global_normalization_eager_fallback(t, m, v, beta, gamma, variance_epsilon: variance_epsilon, scale_after_normalization: scale_after_normalization, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["t"] = t; + keywords["m"] = m; + keywords["v"] = v; + keywords["beta"] = beta; + keywords["gamma"] = gamma; + keywords["variance_epsilon"] = variance_epsilon; + keywords["scale_after_normalization"] = scale_after_normalization; + var _op = tf.OpDefLib._apply_op_helper("BatchNormWithGlobalNormalization", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "variance_epsilon", _op.get_attr("variance_epsilon"), "scale_after_normalization", _op._get_attr_bool("scale_after_normalization") }; + _execute.record_gradient("BatchNormWithGlobalNormalization", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor batch_norm_with_global_normalization_eager_fallback(Tensor t, Tensor m, Tensor v, Tensor beta, Tensor gamma, float variance_epsilon, bool scale_after_normalization, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { t, m, v, beta, gamma }; + object[] _attrs = new object[] { "T", t.dtype, "variance_epsilon", variance_epsilon, "scale_after_normalization", scale_after_normalization }; + var _result = _execute.execute("BatchNormWithGlobalNormalization", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("BatchNormWithGlobalNormalization", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Gradients for batch normalization. + /// + /// + /// + /// This op is deprecated. See `tf.nn.batch_normalization`. + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// A small float number to avoid dividing by 0. + /// + /// + /// + /// + /// A bool indicating whether the resulted tensor + /// needs to be multiplied with gamma. + /// + /// + /// + public static Tensor[] batch_norm_with_global_normalization_grad(Tensor t, Tensor m, Tensor v, Tensor gamma, Tensor backprop, float variance_epsilon, bool scale_after_normalization, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "BatchNormWithGlobalNormalizationGrad", name) { args = new object[] { t, m, v, gamma, backprop }, attrs = new Dictionary() { ["variance_epsilon"] = variance_epsilon, ["scale_after_normalization"] = scale_after_normalization } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return batch_norm_with_global_normalization_grad_eager_fallback(t, m, v, gamma, backprop, variance_epsilon: variance_epsilon, scale_after_normalization: scale_after_normalization, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["t"] = t; + keywords["m"] = m; + keywords["v"] = v; + keywords["gamma"] = gamma; + keywords["backprop"] = backprop; + keywords["variance_epsilon"] = variance_epsilon; + keywords["scale_after_normalization"] = scale_after_normalization; + var _op = tf.OpDefLib._apply_op_helper("BatchNormWithGlobalNormalizationGrad", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "variance_epsilon", _op.get_attr("variance_epsilon"), "scale_after_normalization", _op._get_attr_bool("scale_after_normalization") }; + _execute.record_gradient("BatchNormWithGlobalNormalizationGrad", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] batch_norm_with_global_normalization_grad_eager_fallback(Tensor t, Tensor m, Tensor v, Tensor gamma, Tensor backprop, float variance_epsilon, bool scale_after_normalization, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { t, m, v, gamma, backprop }; + object[] _attrs = new object[] { "T", t.dtype, "variance_epsilon", variance_epsilon, "scale_after_normalization", scale_after_normalization }; + var _result = _execute.execute("BatchNormWithGlobalNormalizationGrad", 5, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("BatchNormWithGlobalNormalizationGrad", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// Adds `bias` to `value`. + /// + /// + /// + /// This is a special case of `tf.add` where `bias` is restricted to be 1-D. + /// Broadcasting is supported, so `value` may have any number of dimensions. + /// + /// + /// + /// + /// + /// + /// Specify the data format of the input and output data. With the + /// default format "NHWC", the bias tensor will be added to the last dimension + /// of the value tensor. + /// Alternatively, the format could be "NCHW", the data storage order of: + /// [batch, in_channels, in_height, in_width]. + /// The tensor will be added to "in_channels", the third-to-the-last + /// dimension. + /// + /// + /// + public static Tensor bias_add(Tensor value, Tensor bias, string data_format = "NHWC", string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "BiasAdd", name) { args = new object[] { value, bias }, attrs = new Dictionary() { ["data_format"] = data_format } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return bias_add_eager_fallback(value, bias, data_format: data_format, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + if (data_format is null) + { + data_format = "NHWC"; + } + Dictionary keywords = new(); + keywords["value"] = value; + keywords["bias"] = bias; + keywords["data_format"] = data_format; + var _op = tf.OpDefLib._apply_op_helper("BiasAdd", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "data_format", _op.get_attr("data_format") }; + _execute.record_gradient("BiasAdd", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor bias_add_eager_fallback(Tensor value, Tensor bias, string data_format, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { value, bias }; + object[] _attrs = new object[] { "T", value.dtype, "data_format", data_format }; + var _result = _execute.execute("BiasAdd", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("BiasAdd", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// The backward operation for "BiasAdd" on the "bias" tensor. + /// + /// + /// + /// It accumulates all the values from out_backprop into the feature dimension. + /// For NHWC data format, the feature dimension is the last. For NCHW data format, + /// the feature dimension is the third-to-last. + /// + /// + /// + /// + /// + /// Specify the data format of the input and output data. With the + /// default format "NHWC", the bias tensor will be added to the last dimension + /// of the value tensor. + /// Alternatively, the format could be "NCHW", the data storage order of: + /// [batch, in_channels, in_height, in_width]. + /// The tensor will be added to "in_channels", the third-to-the-last + /// dimension. + /// + /// + /// + public static Tensor bias_add_grad(Tensor out_backprop, string data_format = "NHWC", string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "BiasAddGrad", name) { args = new object[] { out_backprop }, attrs = new Dictionary() { ["data_format"] = data_format } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return bias_add_grad_eager_fallback(out_backprop, data_format: data_format, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + if (data_format is null) + { + data_format = "NHWC"; + } + Dictionary keywords = new(); + keywords["out_backprop"] = out_backprop; + keywords["data_format"] = data_format; + var _op = tf.OpDefLib._apply_op_helper("BiasAddGrad", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "data_format", _op.get_attr("data_format") }; + _execute.record_gradient("BiasAddGrad", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor bias_add_grad_eager_fallback(Tensor out_backprop, string data_format, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { out_backprop }; + object[] _attrs = new object[] { "T", out_backprop.dtype, "data_format", data_format }; + var _result = _execute.execute("BiasAddGrad", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("BiasAddGrad", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Adds `bias` to `value`. + /// + /// + /// + /// This is a deprecated version of BiasAdd and will be soon removed. + /// + /// This is a special case of `tf.add` where `bias` is restricted to be 1-D. + /// Broadcasting is supported, so `value` may have any number of dimensions. + /// + /// + /// + /// + /// + public static Tensor bias_add_v1(Tensor value, Tensor bias, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "BiasAddV1", name) { args = new object[] { value, bias }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return bias_add_v1_eager_fallback(value, bias, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["value"] = value; + keywords["bias"] = bias; + var _op = tf.OpDefLib._apply_op_helper("BiasAddV1", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("BiasAddV1", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor bias_add_v1_eager_fallback(Tensor value, Tensor bias, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { value, bias }; + object[] _attrs = new object[] { "T", value.dtype }; + var _result = _execute.execute("BiasAddV1", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("BiasAddV1", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes a 2-D convolution given 4-D `input` and `filter` tensors. + /// + /// + /// + /// Given an input tensor of shape `[batch, in_height, in_width, in_channels]` + /// and a filter / kernel tensor of shape + /// `[filter_height, filter_width, in_channels, out_channels]`, this op + /// performs the following: + /// + /// 1. Flattens the filter to a 2-D matrix with shape + /// `[filter_height * filter_width * in_channels, output_channels]`. + /// 2. Extracts image patches from the input tensor to form a *virtual* + /// tensor of shape `[batch, out_height, out_width, + /// filter_height * filter_width * in_channels]`. + /// 3. For each patch, right-multiplies the filter matrix and the image patch + /// vector. + /// + /// In detail, with the default NHWC format, + /// + /// output[b, i, j, k] = + /// sum_{di, dj, q} input[b, strides[1] * i + di, strides[2] * j + dj, q] * + /// filter[di, dj, q, k] + /// + /// Must have `strides[0] = strides[3] = 1`. For the most common case of the same + /// horizontal and vertices strides, `strides = [1, stride, stride, 1]`. + /// + /// + /// + /// + /// + /// + /// 1-D tensor of length 4. The stride of the sliding window for each + /// dimension of `input`. The dimension order is determined by the value of + /// `data_format`, see below for details. + /// + /// + /// + /// + /// + /// The type of padding algorithm to use. + /// + /// + /// + /// + /// If `padding` is `"EXPLICIT"`, the list of explicit padding amounts. For the ith + /// dimension, the amount of padding inserted before and after the dimension is + /// `explicit_paddings[2 * i]` and `explicit_paddings[2 * i + 1]`, respectively. If + /// `padding` is not `"EXPLICIT"`, `explicit_paddings` must be empty. + /// + /// + /// + /// + /// Specify the data format of the input and output data. With the + /// default format "NHWC", the data is stored in the order of: + /// [batch, height, width, channels]. + /// Alternatively, the format could be "NCHW", the data storage order of: + /// [batch, channels, height, width]. + /// + /// + /// + /// + /// 1-D tensor of length 4. The dilation factor for each dimension of + /// `input`. If set to k > 1, there will be k-1 skipped cells between each + /// filter element on that dimension. The dimension order is determined by the + /// value of `data_format`, see above for details. Dilations in the batch and + /// depth dimensions must be 1. + /// + /// + /// + public static Tensor conv2d(Tensor input, Tensor filter, int[] strides, string padding, bool use_cudnn_on_gpu = true, int[] explicit_paddings = null, string data_format = "NHWC", int[] dilations = null, string? name = null) + { + var _ctx = tf.Context; + if (explicit_paddings is null) + { + explicit_paddings = new int[] { }; + } + if (dilations is null) + { + dilations = new int[] { 1, 1, 1, 1 }; + } + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Conv2D", name) { args = new object[] { input, filter }, attrs = new Dictionary() { ["strides"] = strides, ["use_cudnn_on_gpu"] = use_cudnn_on_gpu, ["padding"] = padding, ["explicit_paddings"] = explicit_paddings, ["data_format"] = data_format, ["dilations"] = dilations } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return conv2d_eager_fallback(input, filter, strides: strides, use_cudnn_on_gpu: use_cudnn_on_gpu, padding: padding, explicit_paddings: explicit_paddings, data_format: data_format, dilations: dilations, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + if (data_format is null) + { + data_format = "NHWC"; + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["filter"] = filter; + keywords["strides"] = strides; + keywords["use_cudnn_on_gpu"] = use_cudnn_on_gpu; + keywords["padding"] = padding; + keywords["explicit_paddings"] = explicit_paddings; + keywords["data_format"] = data_format; + keywords["dilations"] = dilations; + var _op = tf.OpDefLib._apply_op_helper("Conv2D", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "strides", _op.get_attr("strides"), "use_cudnn_on_gpu", _op._get_attr_bool("use_cudnn_on_gpu"), "padding", _op.get_attr("padding"), "explicit_paddings", _op.get_attr("explicit_paddings"), "data_format", _op.get_attr("data_format"), "dilations", _op.get_attr("dilations") }; + _execute.record_gradient("Conv2D", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor conv2d_eager_fallback(Tensor input, Tensor filter, int[] strides, bool use_cudnn_on_gpu, string padding, int[] explicit_paddings, string data_format, int[] dilations, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, filter }; + object[] _attrs = new object[] { "T", input.dtype, "strides", strides, "use_cudnn_on_gpu", use_cudnn_on_gpu, "padding", padding, "explicit_paddings", explicit_paddings, "data_format", data_format, "dilations", dilations }; + var _result = _execute.execute("Conv2D", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Conv2D", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes the gradients of convolution with respect to the filter. + /// + /// + /// + /// + /// + /// + /// The stride of the sliding window for each dimension of the input + /// of the convolution. Must be in the same order as the dimension specified with + /// format. + /// + /// + /// + /// + /// + /// The type of padding algorithm to use. + /// + /// + /// + /// + /// If `padding` is `"EXPLICIT"`, the list of explicit padding amounts. For the ith + /// dimension, the amount of padding inserted before and after the dimension is + /// `explicit_paddings[2 * i]` and `explicit_paddings[2 * i + 1]`, respectively. If + /// `padding` is not `"EXPLICIT"`, `explicit_paddings` must be empty. + /// + /// + /// + /// + /// Specify the data format of the input and output data. With the + /// default format "NHWC", the data is stored in the order of: + /// [batch, in_height, in_width, in_channels]. + /// Alternatively, the format could be "NCHW", the data storage order of: + /// [batch, in_channels, in_height, in_width]. + /// + /// + /// + /// + /// 1-D tensor of length 4. The dilation factor for each dimension of + /// `input`. If set to k > 1, there will be k-1 skipped cells between each filter + /// element on that dimension. The dimension order is determined by the value of + /// `data_format`, see above for details. Dilations in the batch and depth + /// dimensions must be 1. + /// + /// + /// + public static Tensor conv2d_backprop_filter(Tensor input, Tensor filter_sizes, Tensor out_backprop, int[] strides, string padding, bool use_cudnn_on_gpu = true, int[] explicit_paddings = null, string data_format = "NHWC", int[] dilations = null, string? name = null) + { + var _ctx = tf.Context; + if (explicit_paddings is null) + { + explicit_paddings = new int[] { }; + } + if (dilations is null) + { + dilations = new int[] { 1, 1, 1, 1 }; + } + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Conv2DBackpropFilter", name) { args = new object[] { input, filter_sizes, out_backprop }, attrs = new Dictionary() { ["strides"] = strides, ["use_cudnn_on_gpu"] = use_cudnn_on_gpu, ["padding"] = padding, ["explicit_paddings"] = explicit_paddings, ["data_format"] = data_format, ["dilations"] = dilations } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return conv2d_backprop_filter_eager_fallback(input, filter_sizes, out_backprop, strides: strides, use_cudnn_on_gpu: use_cudnn_on_gpu, padding: padding, explicit_paddings: explicit_paddings, data_format: data_format, dilations: dilations, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + if (data_format is null) + { + data_format = "NHWC"; + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["filter_sizes"] = filter_sizes; + keywords["out_backprop"] = out_backprop; + keywords["strides"] = strides; + keywords["use_cudnn_on_gpu"] = use_cudnn_on_gpu; + keywords["padding"] = padding; + keywords["explicit_paddings"] = explicit_paddings; + keywords["data_format"] = data_format; + keywords["dilations"] = dilations; + var _op = tf.OpDefLib._apply_op_helper("Conv2DBackpropFilter", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "strides", _op.get_attr("strides"), "use_cudnn_on_gpu", _op._get_attr_bool("use_cudnn_on_gpu"), "padding", _op.get_attr("padding"), "explicit_paddings", _op.get_attr("explicit_paddings"), "data_format", _op.get_attr("data_format"), "dilations", _op.get_attr("dilations") }; + _execute.record_gradient("Conv2DBackpropFilter", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor conv2d_backprop_filter_eager_fallback(Tensor input, Tensor filter_sizes, Tensor out_backprop, int[] strides, bool use_cudnn_on_gpu, string padding, int[] explicit_paddings, string data_format, int[] dilations, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, filter_sizes, out_backprop }; + object[] _attrs = new object[] { "T", input.dtype, "strides", strides, "use_cudnn_on_gpu", use_cudnn_on_gpu, "padding", padding, "explicit_paddings", explicit_paddings, "data_format", data_format, "dilations", dilations }; + var _result = _execute.execute("Conv2DBackpropFilter", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Conv2DBackpropFilter", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes the gradients of convolution with respect to the input. + /// + /// + /// + /// + /// + /// + /// The stride of the sliding window for each dimension of the input + /// of the convolution. Must be in the same order as the dimension specified with + /// format. + /// + /// + /// + /// + /// + /// The type of padding algorithm to use. + /// + /// + /// + /// + /// If `padding` is `"EXPLICIT"`, the list of explicit padding amounts. For the ith + /// dimension, the amount of padding inserted before and after the dimension is + /// `explicit_paddings[2 * i]` and `explicit_paddings[2 * i + 1]`, respectively. If + /// `padding` is not `"EXPLICIT"`, `explicit_paddings` must be empty. + /// + /// + /// + /// + /// Specify the data format of the input and output data. With the + /// default format "NHWC", the data is stored in the order of: + /// [batch, in_height, in_width, in_channels]. + /// Alternatively, the format could be "NCHW", the data storage order of: + /// [batch, in_channels, in_height, in_width]. + /// + /// + /// + /// + /// 1-D tensor of length 4. The dilation factor for each dimension of + /// `input`. If set to k > 1, there will be k-1 skipped cells between each filter + /// element on that dimension. The dimension order is determined by the value of + /// `data_format`, see above for details. Dilations in the batch and depth + /// dimensions must be 1. + /// + /// + /// + public static Tensor conv2d_backprop_input(Tensor input_sizes, Tensor filter, Tensor out_backprop, int[] strides, string padding, bool use_cudnn_on_gpu = true, int[] explicit_paddings = null, string data_format = "NHWC", int[] dilations = null, string? name = null) + { + var _ctx = tf.Context; + if (explicit_paddings is null) + { + explicit_paddings = new int[] { }; + } + if (dilations is null) + { + dilations = new int[] { 1, 1, 1, 1 }; + } + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Conv2DBackpropInput", name) { args = new object[] { input_sizes, filter, out_backprop }, attrs = new Dictionary() { ["strides"] = strides, ["use_cudnn_on_gpu"] = use_cudnn_on_gpu, ["padding"] = padding, ["explicit_paddings"] = explicit_paddings, ["data_format"] = data_format, ["dilations"] = dilations } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return conv2d_backprop_input_eager_fallback(input_sizes, filter, out_backprop, strides: strides, use_cudnn_on_gpu: use_cudnn_on_gpu, padding: padding, explicit_paddings: explicit_paddings, data_format: data_format, dilations: dilations, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + if (data_format is null) + { + data_format = "NHWC"; + } + Dictionary keywords = new(); + keywords["input_sizes"] = input_sizes; + keywords["filter"] = filter; + keywords["out_backprop"] = out_backprop; + keywords["strides"] = strides; + keywords["use_cudnn_on_gpu"] = use_cudnn_on_gpu; + keywords["padding"] = padding; + keywords["explicit_paddings"] = explicit_paddings; + keywords["data_format"] = data_format; + keywords["dilations"] = dilations; + var _op = tf.OpDefLib._apply_op_helper("Conv2DBackpropInput", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "strides", _op.get_attr("strides"), "use_cudnn_on_gpu", _op._get_attr_bool("use_cudnn_on_gpu"), "padding", _op.get_attr("padding"), "explicit_paddings", _op.get_attr("explicit_paddings"), "data_format", _op.get_attr("data_format"), "dilations", _op.get_attr("dilations") }; + _execute.record_gradient("Conv2DBackpropInput", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor conv2d_backprop_input_eager_fallback(Tensor input_sizes, Tensor filter, Tensor out_backprop, int[] strides, bool use_cudnn_on_gpu, string padding, int[] explicit_paddings, string data_format, int[] dilations, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input_sizes, filter, out_backprop }; + object[] _attrs = new object[] { "T", filter.dtype, "strides", strides, "use_cudnn_on_gpu", use_cudnn_on_gpu, "padding", padding, "explicit_paddings", explicit_paddings, "data_format", data_format, "dilations", dilations }; + var _result = _execute.execute("Conv2DBackpropInput", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Conv2DBackpropInput", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes a 3-D convolution given 5-D `input` and `filter` tensors. + /// + /// + /// + /// In signal processing, cross-correlation is a measure of similarity of + /// two waveforms as a function of a time-lag applied to one of them. This + /// is also known as a sliding dot product or sliding inner-product. + /// + /// Our Conv3D implements a form of cross-correlation. + /// + /// + /// + /// + /// + /// + /// 1-D tensor of length 5. The stride of the sliding window for each + /// dimension of `input`. Must have `strides[0] = strides[4] = 1`. + /// + /// + /// + /// + /// The type of padding algorithm to use. + /// + /// + /// + /// + /// The data format of the input and output data. With the + /// default format "NDHWC", the data is stored in the order of: + /// [batch, in_depth, in_height, in_width, in_channels]. + /// Alternatively, the format could be "NCDHW", the data storage order is: + /// [batch, in_channels, in_depth, in_height, in_width]. + /// + /// + /// + /// + /// 1-D tensor of length 5. The dilation factor for each dimension of + /// `input`. If set to k > 1, there will be k-1 skipped cells between each + /// filter element on that dimension. The dimension order is determined by the + /// value of `data_format`, see above for details. Dilations in the batch and + /// depth dimensions must be 1. + /// + /// + /// + public static Tensor conv3d(Tensor input, Tensor filter, int[] strides, string padding, string data_format = "NDHWC", int[] dilations = null, string? name = null) + { + var _ctx = tf.Context; + if (dilations is null) + { + dilations = new int[] { 1, 1, 1, 1, 1 }; + } + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Conv3D", name) { args = new object[] { input, filter }, attrs = new Dictionary() { ["strides"] = strides, ["padding"] = padding, ["data_format"] = data_format, ["dilations"] = dilations } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return conv3d_eager_fallback(input, filter, strides: strides, padding: padding, data_format: data_format, dilations: dilations, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + if (data_format is null) + { + data_format = "NDHWC"; + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["filter"] = filter; + keywords["strides"] = strides; + keywords["padding"] = padding; + keywords["data_format"] = data_format; + keywords["dilations"] = dilations; + var _op = tf.OpDefLib._apply_op_helper("Conv3D", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "strides", _op.get_attr("strides"), "padding", _op.get_attr("padding"), "data_format", _op.get_attr("data_format"), "dilations", _op.get_attr("dilations") }; + _execute.record_gradient("Conv3D", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor conv3d_eager_fallback(Tensor input, Tensor filter, int[] strides, string padding, string data_format, int[] dilations, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, filter }; + object[] _attrs = new object[] { "T", input.dtype, "strides", strides, "padding", padding, "data_format", data_format, "dilations", dilations }; + var _result = _execute.execute("Conv3D", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Conv3D", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes the gradients of 3-D convolution with respect to the filter. + /// + /// + /// + /// + /// + /// + /// 1-D tensor of length 5. The stride of the sliding window for each + /// dimension of `input`. Must have `strides[0] = strides[4] = 1`. + /// + /// + /// + /// + /// The type of padding algorithm to use. + /// + /// + /// + /// + public static Tensor conv3d_backprop_filter(Tensor input, Tensor filter, Tensor out_backprop, int[] strides, string padding, int[] dilations = null, string? name = null) + { + var _ctx = tf.Context; + if (dilations is null) + { + dilations = new int[] { 1, 1, 1, 1, 1 }; + } + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Conv3DBackpropFilter", name) { args = new object[] { input, filter, out_backprop }, attrs = new Dictionary() { ["strides"] = strides, ["padding"] = padding, ["dilations"] = dilations } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return conv3d_backprop_filter_eager_fallback(input, filter, out_backprop, strides: strides, padding: padding, dilations: dilations, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["filter"] = filter; + keywords["out_backprop"] = out_backprop; + keywords["strides"] = strides; + keywords["padding"] = padding; + keywords["dilations"] = dilations; + var _op = tf.OpDefLib._apply_op_helper("Conv3DBackpropFilter", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "strides", _op.get_attr("strides"), "padding", _op.get_attr("padding"), "dilations", _op.get_attr("dilations") }; + _execute.record_gradient("Conv3DBackpropFilter", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor conv3d_backprop_filter_eager_fallback(Tensor input, Tensor filter, Tensor out_backprop, int[] strides, string padding, int[] dilations, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, filter, out_backprop }; + object[] _attrs = new object[] { "T", input.dtype, "strides", strides, "padding", padding, "dilations", dilations }; + var _result = _execute.execute("Conv3DBackpropFilter", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Conv3DBackpropFilter", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes the gradients of 3-D convolution with respect to the filter. + /// + /// + /// + /// + /// + /// + /// 1-D tensor of length 5. The stride of the sliding window for each + /// dimension of `input`. Must have `strides[0] = strides[4] = 1`. + /// + /// + /// + /// + /// The type of padding algorithm to use. + /// + /// + /// + /// + /// The data format of the input and output data. With the + /// default format "NDHWC", the data is stored in the order of: + /// [batch, in_depth, in_height, in_width, in_channels]. + /// Alternatively, the format could be "NCDHW", the data storage order is: + /// [batch, in_channels, in_depth, in_height, in_width]. + /// + /// + /// + /// + /// 1-D tensor of length 5. The dilation factor for each dimension of + /// `input`. If set to k > 1, there will be k-1 skipped cells between each + /// filter element on that dimension. The dimension order is determined by the + /// value of `data_format`, see above for details. Dilations in the batch and + /// depth dimensions must be 1. + /// + /// + /// + public static Tensor conv3d_backprop_filter_v2(Tensor input, Tensor filter_sizes, Tensor out_backprop, int[] strides, string padding, string data_format = "NDHWC", int[] dilations = null, string? name = null) + { + var _ctx = tf.Context; + if (dilations is null) + { + dilations = new int[] { 1, 1, 1, 1, 1 }; + } + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Conv3DBackpropFilterV2", name) { args = new object[] { input, filter_sizes, out_backprop }, attrs = new Dictionary() { ["strides"] = strides, ["padding"] = padding, ["data_format"] = data_format, ["dilations"] = dilations } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return conv3d_backprop_filter_v2_eager_fallback(input, filter_sizes, out_backprop, strides: strides, padding: padding, data_format: data_format, dilations: dilations, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + if (data_format is null) + { + data_format = "NDHWC"; + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["filter_sizes"] = filter_sizes; + keywords["out_backprop"] = out_backprop; + keywords["strides"] = strides; + keywords["padding"] = padding; + keywords["data_format"] = data_format; + keywords["dilations"] = dilations; + var _op = tf.OpDefLib._apply_op_helper("Conv3DBackpropFilterV2", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "strides", _op.get_attr("strides"), "padding", _op.get_attr("padding"), "data_format", _op.get_attr("data_format"), "dilations", _op.get_attr("dilations") }; + _execute.record_gradient("Conv3DBackpropFilterV2", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor conv3d_backprop_filter_v2_eager_fallback(Tensor input, Tensor filter_sizes, Tensor out_backprop, int[] strides, string padding, string data_format, int[] dilations, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, filter_sizes, out_backprop }; + object[] _attrs = new object[] { "T", input.dtype, "strides", strides, "padding", padding, "data_format", data_format, "dilations", dilations }; + var _result = _execute.execute("Conv3DBackpropFilterV2", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Conv3DBackpropFilterV2", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes the gradients of 3-D convolution with respect to the input. + /// + /// + /// + /// + /// + /// + /// 1-D tensor of length 5. The stride of the sliding window for each + /// dimension of `input`. Must have `strides[0] = strides[4] = 1`. + /// + /// + /// + /// + /// The type of padding algorithm to use. + /// + /// + /// + /// + public static Tensor conv3d_backprop_input(Tensor input, Tensor filter, Tensor out_backprop, int[] strides, string padding, int[] dilations = null, string? name = null) + { + var _ctx = tf.Context; + if (dilations is null) + { + dilations = new int[] { 1, 1, 1, 1, 1 }; + } + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Conv3DBackpropInput", name) { args = new object[] { input, filter, out_backprop }, attrs = new Dictionary() { ["strides"] = strides, ["padding"] = padding, ["dilations"] = dilations } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return conv3d_backprop_input_eager_fallback(input, filter, out_backprop, strides: strides, padding: padding, dilations: dilations, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["filter"] = filter; + keywords["out_backprop"] = out_backprop; + keywords["strides"] = strides; + keywords["padding"] = padding; + keywords["dilations"] = dilations; + var _op = tf.OpDefLib._apply_op_helper("Conv3DBackpropInput", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "strides", _op.get_attr("strides"), "padding", _op.get_attr("padding"), "dilations", _op.get_attr("dilations") }; + _execute.record_gradient("Conv3DBackpropInput", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor conv3d_backprop_input_eager_fallback(Tensor input, Tensor filter, Tensor out_backprop, int[] strides, string padding, int[] dilations, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, filter, out_backprop }; + object[] _attrs = new object[] { "T", input.dtype, "strides", strides, "padding", padding, "dilations", dilations }; + var _result = _execute.execute("Conv3DBackpropInput", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Conv3DBackpropInput", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes the gradients of 3-D convolution with respect to the input. + /// + /// + /// + /// + /// + /// + /// 1-D tensor of length 5. The stride of the sliding window for each + /// dimension of `input`. Must have `strides[0] = strides[4] = 1`. + /// + /// + /// + /// + /// The type of padding algorithm to use. + /// + /// + /// + /// + /// The data format of the input and output data. With the + /// default format "NDHWC", the data is stored in the order of: + /// [batch, in_depth, in_height, in_width, in_channels]. + /// Alternatively, the format could be "NCDHW", the data storage order is: + /// [batch, in_channels, in_depth, in_height, in_width]. + /// + /// + /// + /// + /// 1-D tensor of length 5. The dilation factor for each dimension of + /// `input`. If set to k > 1, there will be k-1 skipped cells between each + /// filter element on that dimension. The dimension order is determined by the + /// value of `data_format`, see above for details. Dilations in the batch and + /// depth dimensions must be 1. + /// + /// + /// + public static Tensor conv3d_backprop_input_v2(Tensor input_sizes, Tensor filter, Tensor out_backprop, int[] strides, string padding, string data_format = "NDHWC", int[] dilations = null, string? name = null) + { + var _ctx = tf.Context; + if (dilations is null) + { + dilations = new int[] { 1, 1, 1, 1, 1 }; + } + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Conv3DBackpropInputV2", name) { args = new object[] { input_sizes, filter, out_backprop }, attrs = new Dictionary() { ["strides"] = strides, ["padding"] = padding, ["data_format"] = data_format, ["dilations"] = dilations } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return conv3d_backprop_input_v2_eager_fallback(input_sizes, filter, out_backprop, strides: strides, padding: padding, data_format: data_format, dilations: dilations, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + if (data_format is null) + { + data_format = "NDHWC"; + } + Dictionary keywords = new(); + keywords["input_sizes"] = input_sizes; + keywords["filter"] = filter; + keywords["out_backprop"] = out_backprop; + keywords["strides"] = strides; + keywords["padding"] = padding; + keywords["data_format"] = data_format; + keywords["dilations"] = dilations; + var _op = tf.OpDefLib._apply_op_helper("Conv3DBackpropInputV2", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "strides", _op.get_attr("strides"), "padding", _op.get_attr("padding"), "data_format", _op.get_attr("data_format"), "dilations", _op.get_attr("dilations"), "Tshape", _op._get_attr_type("Tshape") }; + _execute.record_gradient("Conv3DBackpropInputV2", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor conv3d_backprop_input_v2_eager_fallback(Tensor input_sizes, Tensor filter, Tensor out_backprop, int[] strides, string padding, string data_format, int[] dilations, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input_sizes, filter, out_backprop }; + object[] _attrs = new object[] { "T", filter.dtype, "strides", strides, "padding", padding, "data_format", data_format, "dilations", dilations, "Tshape", input_sizes.dtype }; + var _result = _execute.execute("Conv3DBackpropInputV2", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Conv3DBackpropInputV2", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns the dimension index in the destination data format given the one in + /// + /// + /// + /// the source data format. + /// + /// + /// + /// + /// + /// source data format. + /// + /// + /// + /// + /// destination data format. + /// + /// + /// + public static Tensor data_format_dim_map(Tensor x, string src_format = "NHWC", string dst_format = "NCHW", string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "DataFormatDimMap", name) { args = new object[] { x }, attrs = new Dictionary() { ["src_format"] = src_format, ["dst_format"] = dst_format } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return data_format_dim_map_eager_fallback(x, src_format: src_format, dst_format: dst_format, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + if (src_format is null) + { + src_format = "NHWC"; + } + if (dst_format is null) + { + dst_format = "NCHW"; + } + Dictionary keywords = new(); + keywords["x"] = x; + keywords["src_format"] = src_format; + keywords["dst_format"] = dst_format; + var _op = tf.OpDefLib._apply_op_helper("DataFormatDimMap", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "src_format", _op.get_attr("src_format"), "dst_format", _op.get_attr("dst_format") }; + _execute.record_gradient("DataFormatDimMap", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor data_format_dim_map_eager_fallback(Tensor x, string src_format, string dst_format, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x }; + object[] _attrs = new object[] { "T", x.dtype, "src_format", src_format, "dst_format", dst_format }; + var _result = _execute.execute("DataFormatDimMap", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("DataFormatDimMap", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Permute input tensor from `src_format` to `dst_format`. + /// + /// + /// + /// Given source and destination format strings of length n=4 or 5, the input + /// tensor must be a vector of size n or n-2, or a 2D tensor of shape + /// (n, 2) or (n-2, 2). + /// + /// If the first dimension of the input tensor is n-2, it is assumed that + /// non-spatial dimensions are omitted (i.e `N`, `C`). + /// + /// For example, with `src_format` of `NHWC`, `dst_format` of `NCHW`, and input: + /// ``` + /// [1, 2, 3, 4] + /// ``` + /// , the output will be: + /// ``` + /// [1, 4, 2, 3] + /// ``` + /// With `src_format` of `NDHWC`, `dst_format` of `NCDHW`, and input: + /// ``` + /// [[1, 6], [2, 7], [3, 8], [4, 9], [5, 10]] + /// ``` + /// , the output will be: + /// ``` + /// [[1, 6], [5, 10], [2, 7], [3, 8], [4, 9]] + /// ``` + /// With `src_format` of `NHWC`, `dst_format` of `NCHW`, and input: + /// ``` + /// [1, 2] + /// ``` + /// , the output will be: + /// ``` + /// [1, 2] + /// ``` + /// + /// + /// + /// + /// + /// source data format. + /// + /// + /// + /// + /// destination data format. + /// + /// + /// + public static Tensor data_format_vec_permute(Tensor x, string src_format = "NHWC", string dst_format = "NCHW", string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "DataFormatVecPermute", name) { args = new object[] { x }, attrs = new Dictionary() { ["src_format"] = src_format, ["dst_format"] = dst_format } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return data_format_vec_permute_eager_fallback(x, src_format: src_format, dst_format: dst_format, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + if (src_format is null) + { + src_format = "NHWC"; + } + if (dst_format is null) + { + dst_format = "NCHW"; + } + Dictionary keywords = new(); + keywords["x"] = x; + keywords["src_format"] = src_format; + keywords["dst_format"] = dst_format; + var _op = tf.OpDefLib._apply_op_helper("DataFormatVecPermute", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "src_format", _op.get_attr("src_format"), "dst_format", _op.get_attr("dst_format") }; + _execute.record_gradient("DataFormatVecPermute", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor data_format_vec_permute_eager_fallback(Tensor x, string src_format, string dst_format, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x }; + object[] _attrs = new object[] { "T", x.dtype, "src_format", src_format, "dst_format", dst_format }; + var _result = _execute.execute("DataFormatVecPermute", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("DataFormatVecPermute", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes a 2-D depthwise convolution given 4-D `input` and `filter` tensors. + /// + /// + /// + /// Given an input tensor of shape `[batch, in_height, in_width, in_channels]` + /// and a filter / kernel tensor of shape + /// `[filter_height, filter_width, in_channels, channel_multiplier]`, containing + /// `in_channels` convolutional filters of depth 1, `depthwise_conv2d` applies + /// a different filter to each input channel (expanding from 1 channel to + /// `channel_multiplier` channels for each), then concatenates the results + /// together. Thus, the output has `in_channels * channel_multiplier` channels. + /// + /// ``` + /// for k in 0..in_channels-1 + /// for q in 0..channel_multiplier-1 + /// output[b, i, j, k * channel_multiplier + q] = + /// sum_{di, dj} input[b, strides[1] * i + di, strides[2] * j + dj, k] * + /// filter[di, dj, k, q] + /// ``` + /// + /// Must have `strides[0] = strides[3] = 1`. For the most common case of the same + /// horizontal and vertices strides, `strides = [1, stride, stride, 1]`. + /// + /// + /// + /// + /// + /// + /// 1-D of length 4. The stride of the sliding window for each dimension + /// of `input`. + /// + /// + /// + /// + /// The type of padding algorithm to use. + /// + /// + /// + /// + /// + /// Specify the data format of the input and output data. With the + /// default format "NHWC", the data is stored in the order of: + /// [batch, height, width, channels]. + /// Alternatively, the format could be "NCHW", the data storage order of: + /// [batch, channels, height, width]. + /// + /// + /// + /// + /// 1-D tensor of length 4. The dilation factor for each dimension of + /// `input`. If set to k > 1, there will be k-1 skipped cells between each filter + /// element on that dimension. The dimension order is determined by the value of + /// `data_format`, see above for details. Dilations in the batch and depth + /// dimensions must be 1. + /// + /// + /// + public static Tensor depthwise_conv2d_native(Tensor input, Tensor filter, int[] strides, string padding, int[] explicit_paddings = null, string data_format = "NHWC", int[] dilations = null, string? name = null) + { + var _ctx = tf.Context; + if (explicit_paddings is null) + { + explicit_paddings = new int[] { }; + } + if (dilations is null) + { + dilations = new int[] { 1, 1, 1, 1 }; + } + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "DepthwiseConv2dNative", name) { args = new object[] { input, filter }, attrs = new Dictionary() { ["strides"] = strides, ["padding"] = padding, ["explicit_paddings"] = explicit_paddings, ["data_format"] = data_format, ["dilations"] = dilations } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return depthwise_conv2d_native_eager_fallback(input, filter, strides: strides, padding: padding, explicit_paddings: explicit_paddings, data_format: data_format, dilations: dilations, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + if (data_format is null) + { + data_format = "NHWC"; + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["filter"] = filter; + keywords["strides"] = strides; + keywords["padding"] = padding; + keywords["explicit_paddings"] = explicit_paddings; + keywords["data_format"] = data_format; + keywords["dilations"] = dilations; + var _op = tf.OpDefLib._apply_op_helper("DepthwiseConv2dNative", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "strides", _op.get_attr("strides"), "padding", _op.get_attr("padding"), "explicit_paddings", _op.get_attr("explicit_paddings"), "data_format", _op.get_attr("data_format"), "dilations", _op.get_attr("dilations") }; + _execute.record_gradient("DepthwiseConv2dNative", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor depthwise_conv2d_native_eager_fallback(Tensor input, Tensor filter, int[] strides, string padding, int[] explicit_paddings, string data_format, int[] dilations, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, filter }; + object[] _attrs = new object[] { "T", input.dtype, "strides", strides, "padding", padding, "explicit_paddings", explicit_paddings, "data_format", data_format, "dilations", dilations }; + var _result = _execute.execute("DepthwiseConv2dNative", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("DepthwiseConv2dNative", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes the gradients of depthwise convolution with respect to the filter. + /// + /// + /// + /// + /// + /// + /// The stride of the sliding window for each dimension of the input + /// of the convolution. + /// + /// + /// + /// + /// The type of padding algorithm to use. + /// + /// + /// + /// + /// + /// Specify the data format of the input and output data. With the + /// default format "NHWC", the data is stored in the order of: + /// [batch, height, width, channels]. + /// Alternatively, the format could be "NCHW", the data storage order of: + /// [batch, channels, height, width]. + /// + /// + /// + /// + /// 1-D tensor of length 4. The dilation factor for each dimension of + /// `input`. If set to k > 1, there will be k-1 skipped cells between each filter + /// element on that dimension. The dimension order is determined by the value of + /// `data_format`, see above for details. Dilations in the batch and depth + /// dimensions must be 1. + /// + /// + /// + public static Tensor depthwise_conv2d_native_backprop_filter(Tensor input, Tensor filter_sizes, Tensor out_backprop, int[] strides, string padding, int[] explicit_paddings = null, string data_format = "NHWC", int[] dilations = null, string? name = null) + { + var _ctx = tf.Context; + if (explicit_paddings is null) + { + explicit_paddings = new int[] { }; + } + if (dilations is null) + { + dilations = new int[] { 1, 1, 1, 1 }; + } + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "DepthwiseConv2dNativeBackpropFilter", name) { args = new object[] { input, filter_sizes, out_backprop }, attrs = new Dictionary() { ["strides"] = strides, ["padding"] = padding, ["explicit_paddings"] = explicit_paddings, ["data_format"] = data_format, ["dilations"] = dilations } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return depthwise_conv2d_native_backprop_filter_eager_fallback(input, filter_sizes, out_backprop, strides: strides, padding: padding, explicit_paddings: explicit_paddings, data_format: data_format, dilations: dilations, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + if (data_format is null) + { + data_format = "NHWC"; + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["filter_sizes"] = filter_sizes; + keywords["out_backprop"] = out_backprop; + keywords["strides"] = strides; + keywords["padding"] = padding; + keywords["explicit_paddings"] = explicit_paddings; + keywords["data_format"] = data_format; + keywords["dilations"] = dilations; + var _op = tf.OpDefLib._apply_op_helper("DepthwiseConv2dNativeBackpropFilter", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "strides", _op.get_attr("strides"), "padding", _op.get_attr("padding"), "explicit_paddings", _op.get_attr("explicit_paddings"), "data_format", _op.get_attr("data_format"), "dilations", _op.get_attr("dilations") }; + _execute.record_gradient("DepthwiseConv2dNativeBackpropFilter", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor depthwise_conv2d_native_backprop_filter_eager_fallback(Tensor input, Tensor filter_sizes, Tensor out_backprop, int[] strides, string padding, int[] explicit_paddings, string data_format, int[] dilations, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, filter_sizes, out_backprop }; + object[] _attrs = new object[] { "T", input.dtype, "strides", strides, "padding", padding, "explicit_paddings", explicit_paddings, "data_format", data_format, "dilations", dilations }; + var _result = _execute.execute("DepthwiseConv2dNativeBackpropFilter", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("DepthwiseConv2dNativeBackpropFilter", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes the gradients of depthwise convolution with respect to the input. + /// + /// + /// + /// + /// + /// + /// The stride of the sliding window for each dimension of the input + /// of the convolution. + /// + /// + /// + /// + /// The type of padding algorithm to use. + /// + /// + /// + /// + /// + /// Specify the data format of the input and output data. With the + /// default format "NHWC", the data is stored in the order of: + /// [batch, height, width, channels]. + /// Alternatively, the format could be "NCHW", the data storage order of: + /// [batch, channels, height, width]. + /// + /// + /// + /// + /// 1-D tensor of length 4. The dilation factor for each dimension of + /// `input`. If set to k > 1, there will be k-1 skipped cells between each filter + /// element on that dimension. The dimension order is determined by the value of + /// `data_format`, see above for details. Dilations in the batch and depth + /// dimensions must be 1. + /// + /// + /// + public static Tensor depthwise_conv2d_native_backprop_input(Tensor input_sizes, Tensor filter, Tensor out_backprop, int[] strides, string padding, int[] explicit_paddings = null, string data_format = "NHWC", int[] dilations = null, string? name = null) + { + var _ctx = tf.Context; + if (explicit_paddings is null) + { + explicit_paddings = new int[] { }; + } + if (dilations is null) + { + dilations = new int[] { 1, 1, 1, 1 }; + } + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "DepthwiseConv2dNativeBackpropInput", name) { args = new object[] { input_sizes, filter, out_backprop }, attrs = new Dictionary() { ["strides"] = strides, ["padding"] = padding, ["explicit_paddings"] = explicit_paddings, ["data_format"] = data_format, ["dilations"] = dilations } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return depthwise_conv2d_native_backprop_input_eager_fallback(input_sizes, filter, out_backprop, strides: strides, padding: padding, explicit_paddings: explicit_paddings, data_format: data_format, dilations: dilations, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + if (data_format is null) + { + data_format = "NHWC"; + } + Dictionary keywords = new(); + keywords["input_sizes"] = input_sizes; + keywords["filter"] = filter; + keywords["out_backprop"] = out_backprop; + keywords["strides"] = strides; + keywords["padding"] = padding; + keywords["explicit_paddings"] = explicit_paddings; + keywords["data_format"] = data_format; + keywords["dilations"] = dilations; + var _op = tf.OpDefLib._apply_op_helper("DepthwiseConv2dNativeBackpropInput", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "strides", _op.get_attr("strides"), "padding", _op.get_attr("padding"), "explicit_paddings", _op.get_attr("explicit_paddings"), "data_format", _op.get_attr("data_format"), "dilations", _op.get_attr("dilations") }; + _execute.record_gradient("DepthwiseConv2dNativeBackpropInput", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor depthwise_conv2d_native_backprop_input_eager_fallback(Tensor input_sizes, Tensor filter, Tensor out_backprop, int[] strides, string padding, int[] explicit_paddings, string data_format, int[] dilations, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input_sizes, filter, out_backprop }; + object[] _attrs = new object[] { "T", filter.dtype, "strides", strides, "padding", padding, "explicit_paddings", explicit_paddings, "data_format", data_format, "dilations", dilations }; + var _result = _execute.execute("DepthwiseConv2dNativeBackpropInput", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("DepthwiseConv2dNativeBackpropInput", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes the grayscale dilation of 4-D `input` and 3-D `filter` tensors. + /// + /// + /// + /// The `input` tensor has shape `[batch, in_height, in_width, depth]` and the + /// `filter` tensor has shape `[filter_height, filter_width, depth]`, i.e., each + /// input channel is processed independently of the others with its own structuring + /// function. The `output` tensor has shape + /// `[batch, out_height, out_width, depth]`. The spatial dimensions of the output + /// tensor depend on the `padding` algorithm. We currently only support the default + /// "NHWC" `data_format`. + /// + /// In detail, the grayscale morphological 2-D dilation is the max-sum correlation + /// (for consistency with `conv2d`, we use unmirrored filters): + /// + /// output[b, y, x, c] = + /// max_{dy, dx} input[b, + /// strides[1] * y + rates[1] * dy, + /// strides[2] * x + rates[2] * dx, + /// c] + + /// filter[dy, dx, c] + /// + /// Max-pooling is a special case when the filter has size equal to the pooling + /// kernel size and contains all zeros. + /// + /// Note on duality: The dilation of `input` by the `filter` is equal to the + /// negation of the erosion of `-input` by the reflected `filter`. + /// + /// + /// + /// + /// + /// + /// The stride of the sliding window for each dimension of the input + /// tensor. Must be: `[1, stride_height, stride_width, 1]`. + /// + /// + /// + /// + /// The input stride for atrous morphological dilation. Must be: + /// `[1, rate_height, rate_width, 1]`. + /// + /// + /// + /// + /// The type of padding algorithm to use. + /// + /// + /// + public static Tensor dilation2d(Tensor input, Tensor filter, int[] strides, int[] rates, string padding, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Dilation2D", name) { args = new object[] { input, filter }, attrs = new Dictionary() { ["strides"] = strides, ["rates"] = rates, ["padding"] = padding } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return dilation2d_eager_fallback(input, filter, strides: strides, rates: rates, padding: padding, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["filter"] = filter; + keywords["strides"] = strides; + keywords["rates"] = rates; + keywords["padding"] = padding; + var _op = tf.OpDefLib._apply_op_helper("Dilation2D", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "strides", _op.get_attr("strides"), "rates", _op.get_attr("rates"), "padding", _op.get_attr("padding") }; + _execute.record_gradient("Dilation2D", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor dilation2d_eager_fallback(Tensor input, Tensor filter, int[] strides, int[] rates, string padding, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, filter }; + object[] _attrs = new object[] { "T", input.dtype, "strides", strides, "rates", rates, "padding", padding }; + var _result = _execute.execute("Dilation2D", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Dilation2D", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes the gradient of morphological 2-D dilation with respect to the filter. + /// + /// + /// + /// + /// + /// + /// 1-D of length 4. The stride of the sliding window for each dimension of + /// the input tensor. Must be: `[1, stride_height, stride_width, 1]`. + /// + /// + /// + /// + /// 1-D of length 4. The input stride for atrous morphological dilation. + /// Must be: `[1, rate_height, rate_width, 1]`. + /// + /// + /// + /// + /// The type of padding algorithm to use. + /// + /// + /// + public static Tensor dilation2d_backprop_filter(Tensor input, Tensor filter, Tensor out_backprop, int[] strides, int[] rates, string padding, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Dilation2DBackpropFilter", name) { args = new object[] { input, filter, out_backprop }, attrs = new Dictionary() { ["strides"] = strides, ["rates"] = rates, ["padding"] = padding } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return dilation2d_backprop_filter_eager_fallback(input, filter, out_backprop, strides: strides, rates: rates, padding: padding, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["filter"] = filter; + keywords["out_backprop"] = out_backprop; + keywords["strides"] = strides; + keywords["rates"] = rates; + keywords["padding"] = padding; + var _op = tf.OpDefLib._apply_op_helper("Dilation2DBackpropFilter", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "strides", _op.get_attr("strides"), "rates", _op.get_attr("rates"), "padding", _op.get_attr("padding") }; + _execute.record_gradient("Dilation2DBackpropFilter", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor dilation2d_backprop_filter_eager_fallback(Tensor input, Tensor filter, Tensor out_backprop, int[] strides, int[] rates, string padding, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, filter, out_backprop }; + object[] _attrs = new object[] { "T", input.dtype, "strides", strides, "rates", rates, "padding", padding }; + var _result = _execute.execute("Dilation2DBackpropFilter", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Dilation2DBackpropFilter", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes the gradient of morphological 2-D dilation with respect to the input. + /// + /// + /// + /// + /// + /// + /// 1-D of length 4. The stride of the sliding window for each dimension of + /// the input tensor. Must be: `[1, stride_height, stride_width, 1]`. + /// + /// + /// + /// + /// 1-D of length 4. The input stride for atrous morphological dilation. + /// Must be: `[1, rate_height, rate_width, 1]`. + /// + /// + /// + /// + /// The type of padding algorithm to use. + /// + /// + /// + public static Tensor dilation2d_backprop_input(Tensor input, Tensor filter, Tensor out_backprop, int[] strides, int[] rates, string padding, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Dilation2DBackpropInput", name) { args = new object[] { input, filter, out_backprop }, attrs = new Dictionary() { ["strides"] = strides, ["rates"] = rates, ["padding"] = padding } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return dilation2d_backprop_input_eager_fallback(input, filter, out_backprop, strides: strides, rates: rates, padding: padding, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["filter"] = filter; + keywords["out_backprop"] = out_backprop; + keywords["strides"] = strides; + keywords["rates"] = rates; + keywords["padding"] = padding; + var _op = tf.OpDefLib._apply_op_helper("Dilation2DBackpropInput", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "strides", _op.get_attr("strides"), "rates", _op.get_attr("rates"), "padding", _op.get_attr("padding") }; + _execute.record_gradient("Dilation2DBackpropInput", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor dilation2d_backprop_input_eager_fallback(Tensor input, Tensor filter, Tensor out_backprop, int[] strides, int[] rates, string padding, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, filter, out_backprop }; + object[] _attrs = new object[] { "T", input.dtype, "strides", strides, "rates", rates, "padding", padding }; + var _result = _execute.execute("Dilation2DBackpropInput", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Dilation2DBackpropInput", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes the exponential linear function. + /// + /// + /// + /// The ELU function is defined as: + /// + /// * $ e ^ x - 1 $ if $ x < 0 $ + /// * $ x $ if $ x >= 0 $ + /// + /// Examples: + /// + /// >>> tf.nn.elu(1.0) + /// + /// >>> tf.nn.elu(0.0) + /// + /// >>> tf.nn.elu(-1000.0) + /// + /// + /// See [Fast and Accurate Deep Network Learning by Exponential Linear Units (ELUs) + /// ](http://arxiv.org/abs/1511.07289) + /// + /// + /// + /// + public static Tensor elu(Tensor features, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Elu", name) { args = new object[] { features }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return elu_eager_fallback(features, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["features"] = features; + var _op = tf.OpDefLib._apply_op_helper("Elu", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Elu", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor elu_eager_fallback(Tensor features, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { features }; + object[] _attrs = new object[] { "T", features.dtype }; + var _result = _execute.execute("Elu", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Elu", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes gradients for the exponential linear (Elu) operation. + /// + /// + /// + /// + public static Tensor elu_grad(Tensor gradients, Tensor outputs, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "EluGrad", name) { args = new object[] { gradients, outputs }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return elu_grad_eager_fallback(gradients, outputs, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["gradients"] = gradients; + keywords["outputs"] = outputs; + var _op = tf.OpDefLib._apply_op_helper("EluGrad", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("EluGrad", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor elu_grad_eager_fallback(Tensor gradients, Tensor outputs, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { gradients, outputs }; + object[] _attrs = new object[] { "T", gradients.dtype }; + var _result = _execute.execute("EluGrad", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("EluGrad", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Performs fractional average pooling on the input. + /// + /// + /// + /// Fractional average pooling is similar to Fractional max pooling in the pooling + /// region generation step. The only difference is that after pooling regions are + /// generated, a mean operation is performed instead of a max operation in each + /// pooling region. + /// + /// + /// + /// + /// + /// Pooling ratio for each dimension of `value`, currently only + /// supports row and col dimension and should be >= 1.0. For example, a valid + /// pooling ratio looks like [1.0, 1.44, 1.73, 1.0]. The first and last elements + /// must be 1.0 because we don't allow pooling on batch and channels + /// dimensions. 1.44 and 1.73 are pooling ratio on height and width dimensions + /// respectively. + /// + /// + /// + /// + /// When set to True, generates the pooling sequence in a + /// pseudorandom fashion, otherwise, in a random fashion. Check paper [Benjamin + /// Graham, Fractional Max-Pooling](http://arxiv.org/abs/1412.6071) for + /// difference between pseudorandom and random. + /// + /// + /// + /// + /// When set to True, it means when pooling, the values at the boundary + /// of adjacent pooling cells are used by both cells. For example: + /// + /// `index 0 1 2 3 4` + /// + /// `value 20 5 16 3 7` + /// + /// If the pooling sequence is [0, 2, 4], then 16, at index 2 will be used twice. + /// The result would be [41/3, 26/3] for fractional avg pooling. + /// + /// + /// + /// + /// When set to True, a fixed pooling region will be used when + /// iterating over a FractionalAvgPool node in the computation graph. Mainly used + /// in unit test to make FractionalAvgPool deterministic. + /// + /// + /// + /// + /// If either seed or seed2 are set to be non-zero, the random number + /// generator is seeded by the given seed. Otherwise, it is seeded by a + /// random seed. + /// + /// + /// + /// + /// An second seed to avoid seed collision. + /// + /// + /// + public static Tensor[] fractional_avg_pool(Tensor value, float[] pooling_ratio, bool pseudo_random = false, bool overlapping = false, bool deterministic = false, int seed = 0, int seed2 = 0, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "FractionalAvgPool", name) { args = new object[] { value }, attrs = new Dictionary() { ["pooling_ratio"] = pooling_ratio, ["pseudo_random"] = pseudo_random, ["overlapping"] = overlapping, ["deterministic"] = deterministic, ["seed"] = seed, ["seed2"] = seed2 } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return fractional_avg_pool_eager_fallback(value, pooling_ratio: pooling_ratio, pseudo_random: pseudo_random, overlapping: overlapping, deterministic: deterministic, seed: seed, seed2: seed2, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["value"] = value; + keywords["pooling_ratio"] = pooling_ratio; + keywords["pseudo_random"] = pseudo_random; + keywords["overlapping"] = overlapping; + keywords["deterministic"] = deterministic; + keywords["seed"] = seed; + keywords["seed2"] = seed2; + var _op = tf.OpDefLib._apply_op_helper("FractionalAvgPool", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "pooling_ratio", _op.get_attr("pooling_ratio"), "pseudo_random", _op._get_attr_bool("pseudo_random"), "overlapping", _op._get_attr_bool("overlapping"), "deterministic", _op._get_attr_bool("deterministic"), "seed", _op._get_attr_int("seed"), "seed2", _op._get_attr_int("seed2"), "T", _op._get_attr_type("T") }; + _execute.record_gradient("FractionalAvgPool", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] fractional_avg_pool_eager_fallback(Tensor value, float[] pooling_ratio, bool pseudo_random, bool overlapping, bool deterministic, int seed, int seed2, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { value }; + object[] _attrs = new object[] { "pooling_ratio", pooling_ratio, "pseudo_random", pseudo_random, "overlapping", overlapping, "deterministic", deterministic, "seed", seed, "seed2", seed2, "T", value.dtype }; + var _result = _execute.execute("FractionalAvgPool", 3, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("FractionalAvgPool", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// Computes gradient of the FractionalAvgPool function. + /// + /// + /// + /// Unlike FractionalMaxPoolGrad, we don't need to find arg_max for + /// FractionalAvgPoolGrad, we just need to evenly back-propagate each element of + /// out_backprop to those indices that form the same pooling cell. Therefore, we + /// just need to know the shape of original input tensor, instead of the whole + /// tensor. + /// + /// + /// + /// + /// + /// + /// + /// + /// When set to True, it means when pooling, the values at the boundary + /// of adjacent pooling cells are used by both cells. For example: + /// + /// `index 0 1 2 3 4` + /// + /// `value 20 5 16 3 7` + /// + /// If the pooling sequence is [0, 2, 4], then 16, at index 2 will be used twice. + /// The result would be [41/3, 26/3] for fractional avg pooling. + /// + /// + /// + public static Tensor fractional_avg_pool_grad(Tensor orig_input_tensor_shape, Tensor out_backprop, Tensor row_pooling_sequence, Tensor col_pooling_sequence, bool overlapping = false, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "FractionalAvgPoolGrad", name) { args = new object[] { orig_input_tensor_shape, out_backprop, row_pooling_sequence, col_pooling_sequence }, attrs = new Dictionary() { ["overlapping"] = overlapping } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return fractional_avg_pool_grad_eager_fallback(orig_input_tensor_shape, out_backprop, row_pooling_sequence, col_pooling_sequence, overlapping: overlapping, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["orig_input_tensor_shape"] = orig_input_tensor_shape; + keywords["out_backprop"] = out_backprop; + keywords["row_pooling_sequence"] = row_pooling_sequence; + keywords["col_pooling_sequence"] = col_pooling_sequence; + keywords["overlapping"] = overlapping; + var _op = tf.OpDefLib._apply_op_helper("FractionalAvgPoolGrad", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "overlapping", _op._get_attr_bool("overlapping"), "T", _op._get_attr_type("T") }; + _execute.record_gradient("FractionalAvgPoolGrad", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor fractional_avg_pool_grad_eager_fallback(Tensor orig_input_tensor_shape, Tensor out_backprop, Tensor row_pooling_sequence, Tensor col_pooling_sequence, bool overlapping, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { orig_input_tensor_shape, out_backprop, row_pooling_sequence, col_pooling_sequence }; + object[] _attrs = new object[] { "overlapping", overlapping, "T", out_backprop.dtype }; + var _result = _execute.execute("FractionalAvgPoolGrad", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("FractionalAvgPoolGrad", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Performs fractional max pooling on the input. + /// + /// + /// + /// Fractional max pooling is slightly different than regular max pooling. In + /// regular max pooling, you downsize an input set by taking the maximum value of + /// smaller N x N subsections of the set (often 2x2), and try to reduce the set by + /// a factor of N, where N is an integer. Fractional max pooling, as you might + /// expect from the word "fractional", means that the overall reduction ratio N + /// does not have to be an integer. + /// + /// The sizes of the pooling regions are generated randomly but are fairly uniform. + /// For example, let's look at the height dimension, and the constraints on the + /// list of rows that will be pool boundaries. + /// + /// First we define the following: + /// + /// 1. input_row_length : the number of rows from the input set + /// 2. output_row_length : which will be smaller than the input + /// 3. alpha = input_row_length / output_row_length : our reduction ratio + /// 4. K = floor(alpha) + /// 5. row_pooling_sequence : this is the result list of pool boundary rows + /// + /// Then, row_pooling_sequence should satisfy: + /// + /// 1. a[0] = 0 : the first value of the sequence is 0 + /// 2. a[end] = input_row_length : the last value of the sequence is the size + /// 3. K <= (a[i+1] - a[i]) <= K+1 : all intervals are K or K+1 size + /// 4. length(row_pooling_sequence) = output_row_length+1 + /// + /// For more details on fractional max pooling, see this paper: + /// [Benjamin Graham, Fractional Max-Pooling](http://arxiv.org/abs/1412.6071) + /// + /// + /// + /// + /// + /// Pooling ratio for each dimension of `value`, currently only + /// supports row and col dimension and should be >= 1.0. For example, a valid + /// pooling ratio looks like [1.0, 1.44, 1.73, 1.0]. The first and last elements + /// must be 1.0 because we don't allow pooling on batch and channels + /// dimensions. 1.44 and 1.73 are pooling ratio on height and width dimensions + /// respectively. + /// + /// + /// + /// + /// When set to True, generates the pooling sequence in a + /// pseudorandom fashion, otherwise, in a random fashion. Check paper [Benjamin + /// Graham, Fractional Max-Pooling](http://arxiv.org/abs/1412.6071) for + /// difference between pseudorandom and random. + /// + /// + /// + /// + /// When set to True, it means when pooling, the values at the boundary + /// of adjacent pooling cells are used by both cells. For example: + /// + /// `index 0 1 2 3 4` + /// + /// `value 20 5 16 3 7` + /// + /// If the pooling sequence is [0, 2, 4], then 16, at index 2 will be used twice. + /// The result would be [20, 16] for fractional max pooling. + /// + /// + /// + /// + /// When set to True, a fixed pooling region will be used when + /// iterating over a FractionalMaxPool node in the computation graph. Mainly used + /// in unit test to make FractionalMaxPool deterministic. + /// + /// + /// + /// + /// If either seed or seed2 are set to be non-zero, the random number + /// generator is seeded by the given seed. Otherwise, it is seeded by a + /// random seed. + /// + /// + /// + /// + /// An second seed to avoid seed collision. + /// + /// + /// + public static Tensor[] fractional_max_pool(Tensor value, float[] pooling_ratio, bool pseudo_random = false, bool overlapping = false, bool deterministic = false, int seed = 0, int seed2 = 0, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "FractionalMaxPool", name) { args = new object[] { value }, attrs = new Dictionary() { ["pooling_ratio"] = pooling_ratio, ["pseudo_random"] = pseudo_random, ["overlapping"] = overlapping, ["deterministic"] = deterministic, ["seed"] = seed, ["seed2"] = seed2 } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return fractional_max_pool_eager_fallback(value, pooling_ratio: pooling_ratio, pseudo_random: pseudo_random, overlapping: overlapping, deterministic: deterministic, seed: seed, seed2: seed2, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["value"] = value; + keywords["pooling_ratio"] = pooling_ratio; + keywords["pseudo_random"] = pseudo_random; + keywords["overlapping"] = overlapping; + keywords["deterministic"] = deterministic; + keywords["seed"] = seed; + keywords["seed2"] = seed2; + var _op = tf.OpDefLib._apply_op_helper("FractionalMaxPool", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "pooling_ratio", _op.get_attr("pooling_ratio"), "pseudo_random", _op._get_attr_bool("pseudo_random"), "overlapping", _op._get_attr_bool("overlapping"), "deterministic", _op._get_attr_bool("deterministic"), "seed", _op._get_attr_int("seed"), "seed2", _op._get_attr_int("seed2"), "T", _op._get_attr_type("T") }; + _execute.record_gradient("FractionalMaxPool", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] fractional_max_pool_eager_fallback(Tensor value, float[] pooling_ratio, bool pseudo_random, bool overlapping, bool deterministic, int seed, int seed2, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { value }; + object[] _attrs = new object[] { "pooling_ratio", pooling_ratio, "pseudo_random", pseudo_random, "overlapping", overlapping, "deterministic", deterministic, "seed", seed, "seed2", seed2, "T", value.dtype }; + var _result = _execute.execute("FractionalMaxPool", 3, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("FractionalMaxPool", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// Computes gradient of the FractionalMaxPool function. + /// + /// + /// + /// + /// + /// + /// + /// + /// When set to True, it means when pooling, the values at the boundary + /// of adjacent pooling cells are used by both cells. For example: + /// + /// `index 0 1 2 3 4` + /// + /// `value 20 5 16 3 7` + /// + /// If the pooling sequence is [0, 2, 4], then 16, at index 2 will be used twice. + /// The result would be [20, 16] for fractional max pooling. + /// + /// + /// + public static Tensor fractional_max_pool_grad(Tensor orig_input, Tensor orig_output, Tensor out_backprop, Tensor row_pooling_sequence, Tensor col_pooling_sequence, bool overlapping = false, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "FractionalMaxPoolGrad", name) { args = new object[] { orig_input, orig_output, out_backprop, row_pooling_sequence, col_pooling_sequence }, attrs = new Dictionary() { ["overlapping"] = overlapping } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return fractional_max_pool_grad_eager_fallback(orig_input, orig_output, out_backprop, row_pooling_sequence, col_pooling_sequence, overlapping: overlapping, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["orig_input"] = orig_input; + keywords["orig_output"] = orig_output; + keywords["out_backprop"] = out_backprop; + keywords["row_pooling_sequence"] = row_pooling_sequence; + keywords["col_pooling_sequence"] = col_pooling_sequence; + keywords["overlapping"] = overlapping; + var _op = tf.OpDefLib._apply_op_helper("FractionalMaxPoolGrad", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "overlapping", _op._get_attr_bool("overlapping"), "T", _op._get_attr_type("T") }; + _execute.record_gradient("FractionalMaxPoolGrad", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor fractional_max_pool_grad_eager_fallback(Tensor orig_input, Tensor orig_output, Tensor out_backprop, Tensor row_pooling_sequence, Tensor col_pooling_sequence, bool overlapping, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { orig_input, orig_output, out_backprop, row_pooling_sequence, col_pooling_sequence }; + object[] _attrs = new object[] { "overlapping", overlapping, "T", orig_input.dtype }; + var _result = _execute.execute("FractionalMaxPoolGrad", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("FractionalMaxPoolGrad", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Batch normalization. + /// + /// + /// + /// Note that the size of 4D Tensors are defined by either "NHWC" or "NCHW". + /// The size of 1D Tensors matches the dimension C of the 4D Tensors. + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// A small float number added to the variance of x. + /// + /// + /// + /// + /// + /// The data format for x and y. Either "NHWC" (default) or "NCHW". + /// + /// + /// + /// + /// A bool value to indicate the operation is for training (default) + /// or inference. + /// + /// + /// + public static Tensor[] fused_batch_norm(Tensor x, Tensor scale, Tensor offset, Tensor mean, Tensor variance, float epsilon = 0.0001f, float exponential_avg_factor = 1f, string data_format = "NHWC", bool is_training = true, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "FusedBatchNorm", name) { args = new object[] { x, scale, offset, mean, variance }, attrs = new Dictionary() { ["epsilon"] = epsilon, ["exponential_avg_factor"] = exponential_avg_factor, ["data_format"] = data_format, ["is_training"] = is_training } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return fused_batch_norm_eager_fallback(x, scale, offset, mean, variance, epsilon: epsilon, exponential_avg_factor: exponential_avg_factor, data_format: data_format, is_training: is_training, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + if (data_format is null) + { + data_format = "NHWC"; + } + Dictionary keywords = new(); + keywords["x"] = x; + keywords["scale"] = scale; + keywords["offset"] = offset; + keywords["mean"] = mean; + keywords["variance"] = variance; + keywords["epsilon"] = epsilon; + keywords["exponential_avg_factor"] = exponential_avg_factor; + keywords["data_format"] = data_format; + keywords["is_training"] = is_training; + var _op = tf.OpDefLib._apply_op_helper("FusedBatchNorm", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "epsilon", _op.get_attr("epsilon"), "exponential_avg_factor", _op.get_attr("exponential_avg_factor"), "data_format", _op.get_attr("data_format"), "is_training", _op._get_attr_bool("is_training") }; + _execute.record_gradient("FusedBatchNorm", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] fused_batch_norm_eager_fallback(Tensor x, Tensor scale, Tensor offset, Tensor mean, Tensor variance, float epsilon, float exponential_avg_factor, string data_format, bool is_training, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x, scale, offset, mean, variance }; + object[] _attrs = new object[] { "T", x.dtype, "epsilon", epsilon, "exponential_avg_factor", exponential_avg_factor, "data_format", data_format, "is_training", is_training }; + var _result = _execute.execute("FusedBatchNorm", 5, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("FusedBatchNorm", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// Gradient for batch normalization. + /// + /// + /// + /// Note that the size of 4D Tensors are defined by either "NHWC" or "NCHW". + /// The size of 1D Tensors matches the dimension C of the 4D Tensors. + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// A small float number added to the variance of x. + /// + /// + /// + /// + /// The data format for y_backprop, x, x_backprop. + /// Either "NHWC" (default) or "NCHW". + /// + /// + /// + /// + /// A bool value to indicate the operation is for training (default) + /// or inference. + /// + /// + /// + public static Tensor[] fused_batch_norm_grad(Tensor y_backprop, Tensor x, Tensor scale, Tensor reserve_space_1, Tensor reserve_space_2, float epsilon = 0.0001f, string data_format = "NHWC", bool is_training = true, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "FusedBatchNormGrad", name) { args = new object[] { y_backprop, x, scale, reserve_space_1, reserve_space_2 }, attrs = new Dictionary() { ["epsilon"] = epsilon, ["data_format"] = data_format, ["is_training"] = is_training } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return fused_batch_norm_grad_eager_fallback(y_backprop, x, scale, reserve_space_1, reserve_space_2, epsilon: epsilon, data_format: data_format, is_training: is_training, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + if (data_format is null) + { + data_format = "NHWC"; + } + Dictionary keywords = new(); + keywords["y_backprop"] = y_backprop; + keywords["x"] = x; + keywords["scale"] = scale; + keywords["reserve_space_1"] = reserve_space_1; + keywords["reserve_space_2"] = reserve_space_2; + keywords["epsilon"] = epsilon; + keywords["data_format"] = data_format; + keywords["is_training"] = is_training; + var _op = tf.OpDefLib._apply_op_helper("FusedBatchNormGrad", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "epsilon", _op.get_attr("epsilon"), "data_format", _op.get_attr("data_format"), "is_training", _op._get_attr_bool("is_training") }; + _execute.record_gradient("FusedBatchNormGrad", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] fused_batch_norm_grad_eager_fallback(Tensor y_backprop, Tensor x, Tensor scale, Tensor reserve_space_1, Tensor reserve_space_2, float epsilon, string data_format, bool is_training, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { y_backprop, x, scale, reserve_space_1, reserve_space_2 }; + object[] _attrs = new object[] { "T", y_backprop.dtype, "epsilon", epsilon, "data_format", data_format, "is_training", is_training }; + var _result = _execute.execute("FusedBatchNormGrad", 5, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("FusedBatchNormGrad", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// Gradient for batch normalization. + /// + /// + /// + /// Note that the size of 4D Tensors are defined by either "NHWC" or "NCHW". + /// The size of 1D Tensors matches the dimension C of the 4D Tensors. + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// A small float number added to the variance of x. + /// + /// + /// + /// + /// The data format for y_backprop, x, x_backprop. + /// Either "NHWC" (default) or "NCHW". + /// + /// + /// + /// + /// A bool value to indicate the operation is for training (default) + /// or inference. + /// + /// + /// + public static Tensor[] fused_batch_norm_grad_v2(Tensor y_backprop, Tensor x, Tensor scale, Tensor reserve_space_1, Tensor reserve_space_2, float epsilon = 0.0001f, string data_format = "NHWC", bool is_training = true, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "FusedBatchNormGradV2", name) { args = new object[] { y_backprop, x, scale, reserve_space_1, reserve_space_2 }, attrs = new Dictionary() { ["epsilon"] = epsilon, ["data_format"] = data_format, ["is_training"] = is_training } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return fused_batch_norm_grad_v2_eager_fallback(y_backprop, x, scale, reserve_space_1, reserve_space_2, epsilon: epsilon, data_format: data_format, is_training: is_training, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + if (data_format is null) + { + data_format = "NHWC"; + } + Dictionary keywords = new(); + keywords["y_backprop"] = y_backprop; + keywords["x"] = x; + keywords["scale"] = scale; + keywords["reserve_space_1"] = reserve_space_1; + keywords["reserve_space_2"] = reserve_space_2; + keywords["epsilon"] = epsilon; + keywords["data_format"] = data_format; + keywords["is_training"] = is_training; + var _op = tf.OpDefLib._apply_op_helper("FusedBatchNormGradV2", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "U", _op._get_attr_type("U"), "epsilon", _op.get_attr("epsilon"), "data_format", _op.get_attr("data_format"), "is_training", _op._get_attr_bool("is_training") }; + _execute.record_gradient("FusedBatchNormGradV2", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] fused_batch_norm_grad_v2_eager_fallback(Tensor y_backprop, Tensor x, Tensor scale, Tensor reserve_space_1, Tensor reserve_space_2, float epsilon, string data_format, bool is_training, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { y_backprop, x, scale, reserve_space_1, reserve_space_2 }; + object[] _attrs = new object[] { "T", y_backprop.dtype, "U", reserve_space_1.dtype, "epsilon", epsilon, "data_format", data_format, "is_training", is_training }; + var _result = _execute.execute("FusedBatchNormGradV2", 5, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("FusedBatchNormGradV2", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// Gradient for batch normalization. + /// + /// + /// + /// Note that the size of 4D Tensors are defined by either "NHWC" or "NCHW". + /// The size of 1D Tensors matches the dimension C of the 4D Tensors. + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// A small float number added to the variance of x. + /// + /// + /// + /// + /// The data format for y_backprop, x, x_backprop. + /// Either "NHWC" (default) or "NCHW". + /// + /// + /// + /// + /// A bool value to indicate the operation is for training (default) + /// or inference. + /// + /// + /// + public static Tensor[] fused_batch_norm_grad_v3(Tensor y_backprop, Tensor x, Tensor scale, Tensor reserve_space_1, Tensor reserve_space_2, Tensor reserve_space_3, float epsilon = 0.0001f, string data_format = "NHWC", bool is_training = true, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "FusedBatchNormGradV3", name) { args = new object[] { y_backprop, x, scale, reserve_space_1, reserve_space_2, reserve_space_3 }, attrs = new Dictionary() { ["epsilon"] = epsilon, ["data_format"] = data_format, ["is_training"] = is_training } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return fused_batch_norm_grad_v3_eager_fallback(y_backprop, x, scale, reserve_space_1, reserve_space_2, reserve_space_3, epsilon: epsilon, data_format: data_format, is_training: is_training, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + if (data_format is null) + { + data_format = "NHWC"; + } + Dictionary keywords = new(); + keywords["y_backprop"] = y_backprop; + keywords["x"] = x; + keywords["scale"] = scale; + keywords["reserve_space_1"] = reserve_space_1; + keywords["reserve_space_2"] = reserve_space_2; + keywords["reserve_space_3"] = reserve_space_3; + keywords["epsilon"] = epsilon; + keywords["data_format"] = data_format; + keywords["is_training"] = is_training; + var _op = tf.OpDefLib._apply_op_helper("FusedBatchNormGradV3", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "U", _op._get_attr_type("U"), "epsilon", _op.get_attr("epsilon"), "data_format", _op.get_attr("data_format"), "is_training", _op._get_attr_bool("is_training") }; + _execute.record_gradient("FusedBatchNormGradV3", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] fused_batch_norm_grad_v3_eager_fallback(Tensor y_backprop, Tensor x, Tensor scale, Tensor reserve_space_1, Tensor reserve_space_2, Tensor reserve_space_3, float epsilon, string data_format, bool is_training, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { y_backprop, x, scale, reserve_space_1, reserve_space_2, reserve_space_3 }; + object[] _attrs = new object[] { "T", y_backprop.dtype, "U", reserve_space_1.dtype, "epsilon", epsilon, "data_format", data_format, "is_training", is_training }; + var _result = _execute.execute("FusedBatchNormGradV3", 5, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("FusedBatchNormGradV3", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// Batch normalization. + /// + /// + /// + /// Note that the size of 4D Tensors are defined by either "NHWC" or "NCHW". + /// The size of 1D Tensors matches the dimension C of the 4D Tensors. + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// A small float number added to the variance of x. + /// + /// + /// + /// + /// + /// The data format for x and y. Either "NHWC" (default) or "NCHW". + /// + /// + /// + /// + /// A bool value to indicate the operation is for training (default) + /// or inference. + /// + /// + /// + public static Tensor[] fused_batch_norm_v2(Tensor x, Tensor scale, Tensor offset, Tensor mean, Tensor variance, float epsilon = 0.0001f, float exponential_avg_factor = 1f, string data_format = "NHWC", bool is_training = true, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "FusedBatchNormV2", name) { args = new object[] { x, scale, offset, mean, variance }, attrs = new Dictionary() { ["epsilon"] = epsilon, ["exponential_avg_factor"] = exponential_avg_factor, ["data_format"] = data_format, ["is_training"] = is_training } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return fused_batch_norm_v2_eager_fallback(x, scale, offset, mean, variance, epsilon: epsilon, exponential_avg_factor: exponential_avg_factor, data_format: data_format, is_training: is_training, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + if (data_format is null) + { + data_format = "NHWC"; + } + Dictionary keywords = new(); + keywords["x"] = x; + keywords["scale"] = scale; + keywords["offset"] = offset; + keywords["mean"] = mean; + keywords["variance"] = variance; + keywords["epsilon"] = epsilon; + keywords["exponential_avg_factor"] = exponential_avg_factor; + keywords["data_format"] = data_format; + keywords["is_training"] = is_training; + var _op = tf.OpDefLib._apply_op_helper("FusedBatchNormV2", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "U", _op._get_attr_type("U"), "epsilon", _op.get_attr("epsilon"), "exponential_avg_factor", _op.get_attr("exponential_avg_factor"), "data_format", _op.get_attr("data_format"), "is_training", _op._get_attr_bool("is_training") }; + _execute.record_gradient("FusedBatchNormV2", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] fused_batch_norm_v2_eager_fallback(Tensor x, Tensor scale, Tensor offset, Tensor mean, Tensor variance, float epsilon, float exponential_avg_factor, string data_format, bool is_training, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x, scale, offset, mean, variance }; + object[] _attrs = new object[] { "T", x.dtype, "U", scale.dtype, "epsilon", epsilon, "exponential_avg_factor", exponential_avg_factor, "data_format", data_format, "is_training", is_training }; + var _result = _execute.execute("FusedBatchNormV2", 5, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("FusedBatchNormV2", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// Batch normalization. + /// + /// + /// + /// Note that the size of 4D Tensors are defined by either "NHWC" or "NCHW". + /// The size of 1D Tensors matches the dimension C of the 4D Tensors. + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// A small float number added to the variance of x. + /// + /// + /// + /// + /// + /// The data format for x and y. Either "NHWC" (default) or "NCHW". + /// + /// + /// + /// + /// A bool value to indicate the operation is for training (default) + /// or inference. + /// + /// + /// + public static Tensor[] fused_batch_norm_v3(Tensor x, Tensor scale, Tensor offset, Tensor mean, Tensor variance, float epsilon = 0.0001f, float exponential_avg_factor = 1f, string data_format = "NHWC", bool is_training = true, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "FusedBatchNormV3", name) { args = new object[] { x, scale, offset, mean, variance }, attrs = new Dictionary() { ["epsilon"] = epsilon, ["exponential_avg_factor"] = exponential_avg_factor, ["data_format"] = data_format, ["is_training"] = is_training } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return fused_batch_norm_v3_eager_fallback(x, scale, offset, mean, variance, epsilon: epsilon, exponential_avg_factor: exponential_avg_factor, data_format: data_format, is_training: is_training, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + if (data_format is null) + { + data_format = "NHWC"; + } + Dictionary keywords = new(); + keywords["x"] = x; + keywords["scale"] = scale; + keywords["offset"] = offset; + keywords["mean"] = mean; + keywords["variance"] = variance; + keywords["epsilon"] = epsilon; + keywords["exponential_avg_factor"] = exponential_avg_factor; + keywords["data_format"] = data_format; + keywords["is_training"] = is_training; + var _op = tf.OpDefLib._apply_op_helper("FusedBatchNormV3", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "U", _op._get_attr_type("U"), "epsilon", _op.get_attr("epsilon"), "exponential_avg_factor", _op.get_attr("exponential_avg_factor"), "data_format", _op.get_attr("data_format"), "is_training", _op._get_attr_bool("is_training") }; + _execute.record_gradient("FusedBatchNormV3", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] fused_batch_norm_v3_eager_fallback(Tensor x, Tensor scale, Tensor offset, Tensor mean, Tensor variance, float epsilon, float exponential_avg_factor, string data_format, bool is_training, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { x, scale, offset, mean, variance }; + object[] _attrs = new object[] { "T", x.dtype, "U", scale.dtype, "epsilon", epsilon, "exponential_avg_factor", exponential_avg_factor, "data_format", data_format, "is_training", is_training }; + var _result = _execute.execute("FusedBatchNormV3", 6, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("FusedBatchNormV3", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// Performs a padding as a preprocess during a convolution. + /// + /// + /// + /// Similar to FusedResizeAndPadConv2d, this op allows for an optimized + /// implementation where the spatial padding transformation stage is fused with the + /// im2col lookup, but in this case without the bilinear filtering required for + /// resizing. Fusing the padding prevents the need to write out the intermediate + /// results as whole tensors, reducing memory pressure, and we can get some latency + /// gains by merging the transformation calculations. + /// The data_format attribute for Conv2D isn't supported by this op, and 'NHWC' + /// order is used instead. + /// Internally this op uses a single per-graph scratch buffer, which means that it + /// will block if multiple versions are being run in parallel. This is because this + /// operator is primarily an optimization to minimize memory usage. + /// + /// + /// + /// + /// + /// + /// + /// + /// 1-D of length 4. The stride of the sliding window for each dimension + /// of `input`. Must be in the same order as the dimension specified with format. + /// + /// + /// + /// + /// The type of padding algorithm to use. + /// + /// + /// + public static Tensor fused_pad_conv2d(Tensor input, Tensor paddings, Tensor filter, string mode, int[] strides, string padding, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "FusedPadConv2D", name) { args = new object[] { input, paddings, filter }, attrs = new Dictionary() { ["mode"] = mode, ["strides"] = strides, ["padding"] = padding } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return fused_pad_conv2d_eager_fallback(input, paddings, filter, mode: mode, strides: strides, padding: padding, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["paddings"] = paddings; + keywords["filter"] = filter; + keywords["mode"] = mode; + keywords["strides"] = strides; + keywords["padding"] = padding; + var _op = tf.OpDefLib._apply_op_helper("FusedPadConv2D", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "mode", _op.get_attr("mode"), "strides", _op.get_attr("strides"), "padding", _op.get_attr("padding") }; + _execute.record_gradient("FusedPadConv2D", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor fused_pad_conv2d_eager_fallback(Tensor input, Tensor paddings, Tensor filter, string mode, int[] strides, string padding, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, paddings, filter }; + object[] _attrs = new object[] { "T", input.dtype, "mode", mode, "strides", strides, "padding", padding }; + var _result = _execute.execute("FusedPadConv2D", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("FusedPadConv2D", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Performs a resize and padding as a preprocess during a convolution. + /// + /// + /// + /// It's often possible to do spatial transformations more efficiently as part of + /// the packing stage of a convolution, so this op allows for an optimized + /// implementation where these stages are fused together. This prevents the need to + /// write out the intermediate results as whole tensors, reducing memory pressure, + /// and we can get some latency gains by merging the transformation calculations. + /// The data_format attribute for Conv2D isn't supported by this op, and defaults to + /// 'NHWC' order. + /// Internally this op uses a single per-graph scratch buffer, which means that it + /// will block if multiple versions are being run in parallel. This is because this + /// operator is primarily an optimization to minimize memory usage. + /// + /// + /// + /// + /// + /// + /// + /// + /// If true, the centers of the 4 corner pixels of the input and output tensors are + /// aligned, preserving the values at the corner pixels. Defaults to false. + /// + /// + /// + /// + /// + /// 1-D of length 4. The stride of the sliding window for each dimension + /// of `input`. Must be in the same order as the dimension specified with format. + /// + /// + /// + /// + /// The type of padding algorithm to use. + /// + /// + /// + public static Tensor fused_resize_and_pad_conv2d(Tensor input, Tensor size, Tensor paddings, Tensor filter, string mode, int[] strides, string padding, bool resize_align_corners = false, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "FusedResizeAndPadConv2D", name) { args = new object[] { input, size, paddings, filter }, attrs = new Dictionary() { ["resize_align_corners"] = resize_align_corners, ["mode"] = mode, ["strides"] = strides, ["padding"] = padding } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return fused_resize_and_pad_conv2d_eager_fallback(input, size, paddings, filter, resize_align_corners: resize_align_corners, mode: mode, strides: strides, padding: padding, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["size"] = size; + keywords["paddings"] = paddings; + keywords["filter"] = filter; + keywords["resize_align_corners"] = resize_align_corners; + keywords["mode"] = mode; + keywords["strides"] = strides; + keywords["padding"] = padding; + var _op = tf.OpDefLib._apply_op_helper("FusedResizeAndPadConv2D", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "resize_align_corners", _op._get_attr_bool("resize_align_corners"), "mode", _op.get_attr("mode"), "strides", _op.get_attr("strides"), "padding", _op.get_attr("padding") }; + _execute.record_gradient("FusedResizeAndPadConv2D", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor fused_resize_and_pad_conv2d_eager_fallback(Tensor input, Tensor size, Tensor paddings, Tensor filter, bool resize_align_corners, string mode, int[] strides, string padding, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, size, paddings, filter }; + object[] _attrs = new object[] { "T", input.dtype, "resize_align_corners", resize_align_corners, "mode", mode, "strides", strides, "padding", padding }; + var _result = _execute.execute("FusedResizeAndPadConv2D", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("FusedResizeAndPadConv2D", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Says whether the targets are in the top `K` predictions. + /// + /// + /// + /// This outputs a `batch_size` bool array, an entry `out[i]` is `true` if the + /// prediction for the target class is among the top `k` predictions among + /// all predictions for example `i`. Note that the behavior of `InTopK` differs + /// from the `TopK` op in its handling of ties; if multiple classes have the + /// same prediction value and straddle the top-`k` boundary, all of those + /// classes are considered to be in the top `k`. + /// + /// More formally, let + /// + /// \(predictions_i\) be the predictions for all classes for example `i`, + /// \(targets_i\) be the target class for example `i`, + /// \(out_i\) be the output for example `i`, + /// + /// $$out_i = predictions_{i, targets_i} in TopKIncludingTies(predictions_i)$$ + /// + /// + /// + /// + /// + /// + /// Number of top elements to look at for computing precision. + /// + /// + /// + public static Tensor in_top_k(Tensor predictions, Tensor targets, int k = 0, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "InTopK", name) { args = new object[] { predictions, targets }, attrs = new Dictionary() { ["k"] = k } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return in_top_k_eager_fallback(predictions, targets, k: k, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["predictions"] = predictions; + keywords["targets"] = targets; + keywords["k"] = k; + var _op = tf.OpDefLib._apply_op_helper("InTopK", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "k", _op._get_attr_int("k"), "T", _op._get_attr_type("T") }; + _execute.record_gradient("InTopK", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor in_top_k_eager_fallback(Tensor predictions, Tensor targets, int k, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { predictions, targets }; + object[] _attrs = new object[] { "k", k, "T", targets.dtype }; + var _result = _execute.execute("InTopK", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("InTopK", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Says whether the targets are in the top `K` predictions. + /// + /// + /// + /// This outputs a `batch_size` bool array, an entry `out[i]` is `true` if the + /// prediction for the target class is among the top `k` predictions among + /// all predictions for example `i`. Note that the behavior of `InTopK` differs + /// from the `TopK` op in its handling of ties; if multiple classes have the + /// same prediction value and straddle the top-`k` boundary, all of those + /// classes are considered to be in the top `k`. + /// + /// More formally, let + /// + /// \(predictions_i\) be the predictions for all classes for example `i`, + /// \(targets_i\) be the target class for example `i`, + /// \(out_i\) be the output for example `i`, + /// + /// $$out_i = predictions_{i, targets_i} in TopKIncludingTies(predictions_i)$$ + /// + /// + /// + /// + /// + /// + public static Tensor in_top_kv2(Tensor predictions, Tensor targets, Tensor k, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "InTopKV2", name) { args = new object[] { predictions, targets, k }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return in_top_kv2_eager_fallback(predictions, targets, k, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["predictions"] = predictions; + keywords["targets"] = targets; + keywords["k"] = k; + var _op = tf.OpDefLib._apply_op_helper("InTopKV2", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("InTopKV2", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor in_top_kv2_eager_fallback(Tensor predictions, Tensor targets, Tensor k, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { predictions, targets, k }; + object[] _attrs = new object[] { "T", targets.dtype }; + var _result = _execute.execute("InTopKV2", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("InTopKV2", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Solves a batch of isotonic regression problems. + /// + /// + /// + /// Dtype of output. + /// + /// + public static Tensor[] isotonic_regression(Tensor input, TF_DataType output_dtype = TF_DataType.TF_FLOAT, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "IsotonicRegression", name) { args = new object[] { input }, attrs = new Dictionary() { ["output_dtype"] = output_dtype } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return isotonic_regression_eager_fallback(input, output_dtype: output_dtype, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["output_dtype"] = output_dtype; + var _op = tf.OpDefLib._apply_op_helper("IsotonicRegression", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "output_dtype", _op._get_attr_type("output_dtype") }; + _execute.record_gradient("IsotonicRegression", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] isotonic_regression_eager_fallback(Tensor input, TF_DataType output_dtype, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input }; + object[] _attrs = new object[] { "T", input.dtype, "output_dtype", output_dtype }; + var _result = _execute.execute("IsotonicRegression", 2, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("IsotonicRegression", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// Local Response Normalization. + /// + /// + /// + /// The 4-D `input` tensor is treated as a 3-D array of 1-D vectors (along the last + /// dimension), and each vector is normalized independently. Within a given vector, + /// each component is divided by the weighted, squared sum of inputs within + /// `depth_radius`. In detail, + /// + /// sqr_sum[a, b, c, d] = + /// sum(input[a, b, c, d - depth_radius : d + depth_radius + 1] ** 2) + /// output = input / (bias + alpha * sqr_sum) ** beta + /// + /// For details, see [Krizhevsky et al., ImageNet classification with deep + /// convolutional neural networks (NIPS 2012)](http://papers.nips.cc/paper/4824-imagenet-classification-with-deep-convolutional-neural-networks). + /// + /// + /// + /// + /// + /// 0-D. Half-width of the 1-D normalization window. + /// + /// + /// + /// + /// An offset (usually positive to avoid dividing by 0). + /// + /// + /// + /// + /// A scale factor, usually positive. + /// + /// + /// + /// + /// An exponent. + /// + /// + /// + public static Tensor lrn(Tensor input, int depth_radius = 5, float bias = 1f, float alpha = 1f, float beta = 0.5f, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "LRN", name) { args = new object[] { input }, attrs = new Dictionary() { ["depth_radius"] = depth_radius, ["bias"] = bias, ["alpha"] = alpha, ["beta"] = beta } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return lrn_eager_fallback(input, depth_radius: depth_radius, bias: bias, alpha: alpha, beta: beta, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["depth_radius"] = depth_radius; + keywords["bias"] = bias; + keywords["alpha"] = alpha; + keywords["beta"] = beta; + var _op = tf.OpDefLib._apply_op_helper("LRN", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "depth_radius", _op._get_attr_int("depth_radius"), "bias", _op.get_attr("bias"), "alpha", _op.get_attr("alpha"), "beta", _op.get_attr("beta"), "T", _op._get_attr_type("T") }; + _execute.record_gradient("LRN", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor lrn_eager_fallback(Tensor input, int depth_radius, float bias, float alpha, float beta, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input }; + object[] _attrs = new object[] { "depth_radius", depth_radius, "bias", bias, "alpha", alpha, "beta", beta, "T", input.dtype }; + var _result = _execute.execute("LRN", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("LRN", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes rectified linear: `max(features, features * alpha)`. + /// + /// + /// + /// + public static Tensor leaky_relu(Tensor features, float alpha = 0.2f, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "LeakyRelu", name) { args = new object[] { features }, attrs = new Dictionary() { ["alpha"] = alpha } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return leaky_relu_eager_fallback(features, alpha: alpha, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["features"] = features; + keywords["alpha"] = alpha; + var _op = tf.OpDefLib._apply_op_helper("LeakyRelu", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "alpha", _op.get_attr("alpha"), "T", _op._get_attr_type("T") }; + _execute.record_gradient("LeakyRelu", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor leaky_relu_eager_fallback(Tensor features, float alpha, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { features }; + object[] _attrs = new object[] { "alpha", alpha, "T", features.dtype }; + var _result = _execute.execute("LeakyRelu", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("LeakyRelu", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes rectified linear gradients for a LeakyRelu operation. + /// + /// + /// + /// + /// + public static Tensor leaky_relu_grad(Tensor gradients, Tensor features, float alpha = 0.2f, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "LeakyReluGrad", name) { args = new object[] { gradients, features }, attrs = new Dictionary() { ["alpha"] = alpha } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return leaky_relu_grad_eager_fallback(gradients, features, alpha: alpha, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["gradients"] = gradients; + keywords["features"] = features; + keywords["alpha"] = alpha; + var _op = tf.OpDefLib._apply_op_helper("LeakyReluGrad", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "alpha", _op.get_attr("alpha"), "T", _op._get_attr_type("T") }; + _execute.record_gradient("LeakyReluGrad", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor leaky_relu_grad_eager_fallback(Tensor gradients, Tensor features, float alpha, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { gradients, features }; + object[] _attrs = new object[] { "alpha", alpha, "T", gradients.dtype }; + var _result = _execute.execute("LeakyReluGrad", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("LeakyReluGrad", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes log softmax activations. + /// + /// + /// + /// For each batch `i` and class `j` we have + /// + /// logsoftmax[i, j] = logits[i, j] - log(sum(exp(logits[i]))) + /// + /// + /// + /// + public static Tensor log_softmax(Tensor logits, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "LogSoftmax", name) { args = new object[] { logits }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return log_softmax_eager_fallback(logits, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["logits"] = logits; + var _op = tf.OpDefLib._apply_op_helper("LogSoftmax", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("LogSoftmax", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor log_softmax_eager_fallback(Tensor logits, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { logits }; + object[] _attrs = new object[] { "T", logits.dtype }; + var _result = _execute.execute("LogSoftmax", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("LogSoftmax", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Performs max pooling on the input. + /// + /// + /// + /// + /// The size of the window for each dimension of the input tensor. + /// + /// + /// + /// + /// The stride of the sliding window for each dimension of the + /// input tensor. + /// + /// + /// + /// + /// The type of padding algorithm to use. + /// + /// + /// + /// + /// + /// Specify the data format of the input and output data. With the + /// default format "NHWC", the data is stored in the order of: + /// [batch, in_height, in_width, in_channels]. + /// Alternatively, the format could be "NCHW", the data storage order of: + /// [batch, in_channels, in_height, in_width]. + /// + /// + /// + public static Tensor max_pool(Tensor input, int[] ksize, int[] strides, string padding, int[] explicit_paddings = null, string data_format = "NHWC", string? name = null) + { + var _ctx = tf.Context; + if (explicit_paddings is null) + { + explicit_paddings = new int[] { }; + } + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "MaxPool", name) { args = new object[] { input }, attrs = new Dictionary() { ["ksize"] = ksize, ["strides"] = strides, ["padding"] = padding, ["explicit_paddings"] = explicit_paddings, ["data_format"] = data_format } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return max_pool_eager_fallback(input, ksize: ksize, strides: strides, padding: padding, explicit_paddings: explicit_paddings, data_format: data_format, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + if (data_format is null) + { + data_format = "NHWC"; + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["ksize"] = ksize; + keywords["strides"] = strides; + keywords["padding"] = padding; + keywords["explicit_paddings"] = explicit_paddings; + keywords["data_format"] = data_format; + var _op = tf.OpDefLib._apply_op_helper("MaxPool", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "ksize", _op.get_attr("ksize"), "strides", _op.get_attr("strides"), "padding", _op.get_attr("padding"), "explicit_paddings", _op.get_attr("explicit_paddings"), "data_format", _op.get_attr("data_format") }; + _execute.record_gradient("MaxPool", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor max_pool_eager_fallback(Tensor input, int[] ksize, int[] strides, string padding, int[] explicit_paddings, string data_format, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input }; + object[] _attrs = new object[] { "T", input.dtype, "ksize", ksize, "strides", strides, "padding", padding, "explicit_paddings", explicit_paddings, "data_format", data_format }; + var _result = _execute.execute("MaxPool", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("MaxPool", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Performs 3D max pooling on the input. + /// + /// + /// + /// + /// 1-D tensor of length 5. The size of the window for each dimension of + /// the input tensor. Must have `ksize[0] = ksize[4] = 1`. + /// + /// + /// + /// + /// 1-D tensor of length 5. The stride of the sliding window for each + /// dimension of `input`. Must have `strides[0] = strides[4] = 1`. + /// + /// + /// + /// + /// The type of padding algorithm to use. + /// + /// + /// + /// + /// The data format of the input and output data. With the + /// default format "NDHWC", the data is stored in the order of: + /// [batch, in_depth, in_height, in_width, in_channels]. + /// Alternatively, the format could be "NCDHW", the data storage order is: + /// [batch, in_channels, in_depth, in_height, in_width]. + /// + /// + /// + public static Tensor max_pool3d(Tensor input, int[] ksize, int[] strides, string padding, string data_format = "NDHWC", string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "MaxPool3D", name) { args = new object[] { input }, attrs = new Dictionary() { ["ksize"] = ksize, ["strides"] = strides, ["padding"] = padding, ["data_format"] = data_format } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return max_pool3d_eager_fallback(input, ksize: ksize, strides: strides, padding: padding, data_format: data_format, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + if (data_format is null) + { + data_format = "NDHWC"; + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["ksize"] = ksize; + keywords["strides"] = strides; + keywords["padding"] = padding; + keywords["data_format"] = data_format; + var _op = tf.OpDefLib._apply_op_helper("MaxPool3D", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "ksize", _op.get_attr("ksize"), "strides", _op.get_attr("strides"), "padding", _op.get_attr("padding"), "data_format", _op.get_attr("data_format"), "T", _op._get_attr_type("T") }; + _execute.record_gradient("MaxPool3D", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor max_pool3d_eager_fallback(Tensor input, int[] ksize, int[] strides, string padding, string data_format, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input }; + object[] _attrs = new object[] { "ksize", ksize, "strides", strides, "padding", padding, "data_format", data_format, "T", input.dtype }; + var _result = _execute.execute("MaxPool3D", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("MaxPool3D", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes gradients of 3D max pooling function. + /// + /// + /// + /// + /// + /// + /// 1-D tensor of length 5. The size of the window for each dimension of + /// the input tensor. Must have `ksize[0] = ksize[4] = 1`. + /// + /// + /// + /// + /// 1-D tensor of length 5. The stride of the sliding window for each + /// dimension of `input`. Must have `strides[0] = strides[4] = 1`. + /// + /// + /// + /// + /// The type of padding algorithm to use. + /// + /// + /// + /// + /// The data format of the input and output data. With the + /// default format "NDHWC", the data is stored in the order of: + /// [batch, in_depth, in_height, in_width, in_channels]. + /// Alternatively, the format could be "NCDHW", the data storage order is: + /// [batch, in_channels, in_depth, in_height, in_width]. + /// + /// + /// + public static Tensor max_pool3d_grad(Tensor orig_input, Tensor orig_output, Tensor grad, int[] ksize, int[] strides, string padding, string data_format = "NDHWC", string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "MaxPool3DGrad", name) { args = new object[] { orig_input, orig_output, grad }, attrs = new Dictionary() { ["ksize"] = ksize, ["strides"] = strides, ["padding"] = padding, ["data_format"] = data_format } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return max_pool3d_grad_eager_fallback(orig_input, orig_output, grad, ksize: ksize, strides: strides, padding: padding, data_format: data_format, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + if (data_format is null) + { + data_format = "NDHWC"; + } + Dictionary keywords = new(); + keywords["orig_input"] = orig_input; + keywords["orig_output"] = orig_output; + keywords["grad"] = grad; + keywords["ksize"] = ksize; + keywords["strides"] = strides; + keywords["padding"] = padding; + keywords["data_format"] = data_format; + var _op = tf.OpDefLib._apply_op_helper("MaxPool3DGrad", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "ksize", _op.get_attr("ksize"), "strides", _op.get_attr("strides"), "padding", _op.get_attr("padding"), "data_format", _op.get_attr("data_format"), "T", _op._get_attr_type("T"), "TInput", _op._get_attr_type("TInput") }; + _execute.record_gradient("MaxPool3DGrad", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor max_pool3d_grad_eager_fallback(Tensor orig_input, Tensor orig_output, Tensor grad, int[] ksize, int[] strides, string padding, string data_format, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { orig_input, orig_output, grad }; + object[] _attrs = new object[] { "ksize", ksize, "strides", strides, "padding", padding, "data_format", data_format, "T", grad.dtype, "TInput", orig_input.dtype }; + var _result = _execute.execute("MaxPool3DGrad", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("MaxPool3DGrad", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes second-order gradients of the maxpooling function. + /// + /// + /// + /// + /// + /// + /// 1-D tensor of length 5. The size of the window for each dimension of + /// the input tensor. Must have `ksize[0] = ksize[4] = 1`. + /// + /// + /// + /// + /// 1-D tensor of length 5. The stride of the sliding window for each + /// dimension of `input`. Must have `strides[0] = strides[4] = 1`. + /// + /// + /// + /// + /// The type of padding algorithm to use. + /// + /// + /// + /// + /// The data format of the input and output data. With the + /// default format "NDHWC", the data is stored in the order of: + /// [batch, in_depth, in_height, in_width, in_channels]. + /// Alternatively, the format could be "NCDHW", the data storage order is: + /// [batch, in_channels, in_depth, in_height, in_width]. + /// + /// + /// + public static Tensor max_pool3d_grad_grad(Tensor orig_input, Tensor orig_output, Tensor grad, int[] ksize, int[] strides, string padding, string data_format = "NDHWC", string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "MaxPool3DGradGrad", name) { args = new object[] { orig_input, orig_output, grad }, attrs = new Dictionary() { ["ksize"] = ksize, ["strides"] = strides, ["padding"] = padding, ["data_format"] = data_format } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return max_pool3d_grad_grad_eager_fallback(orig_input, orig_output, grad, ksize: ksize, strides: strides, padding: padding, data_format: data_format, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + if (data_format is null) + { + data_format = "NDHWC"; + } + Dictionary keywords = new(); + keywords["orig_input"] = orig_input; + keywords["orig_output"] = orig_output; + keywords["grad"] = grad; + keywords["ksize"] = ksize; + keywords["strides"] = strides; + keywords["padding"] = padding; + keywords["data_format"] = data_format; + var _op = tf.OpDefLib._apply_op_helper("MaxPool3DGradGrad", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "ksize", _op.get_attr("ksize"), "strides", _op.get_attr("strides"), "padding", _op.get_attr("padding"), "data_format", _op.get_attr("data_format"), "T", _op._get_attr_type("T") }; + _execute.record_gradient("MaxPool3DGradGrad", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor max_pool3d_grad_grad_eager_fallback(Tensor orig_input, Tensor orig_output, Tensor grad, int[] ksize, int[] strides, string padding, string data_format, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { orig_input, orig_output, grad }; + object[] _attrs = new object[] { "ksize", ksize, "strides", strides, "padding", padding, "data_format", data_format, "T", orig_input.dtype }; + var _result = _execute.execute("MaxPool3DGradGrad", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("MaxPool3DGradGrad", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes gradients of the maxpooling function. + /// + /// + /// + /// + /// + /// + /// The size of the window for each dimension of the input tensor. + /// + /// + /// + /// + /// The stride of the sliding window for each dimension of the + /// input tensor. + /// + /// + /// + /// + /// The type of padding algorithm to use. + /// + /// + /// + /// + /// + /// Specify the data format of the input and output data. With the + /// default format "NHWC", the data is stored in the order of: + /// [batch, in_height, in_width, in_channels]. + /// Alternatively, the format could be "NCHW", the data storage order of: + /// [batch, in_channels, in_height, in_width]. + /// + /// + /// + public static Tensor max_pool_grad(Tensor orig_input, Tensor orig_output, Tensor grad, int[] ksize, int[] strides, string padding, int[] explicit_paddings = null, string data_format = "NHWC", string? name = null) + { + var _ctx = tf.Context; + if (explicit_paddings is null) + { + explicit_paddings = new int[] { }; + } + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "MaxPoolGrad", name) { args = new object[] { orig_input, orig_output, grad }, attrs = new Dictionary() { ["ksize"] = ksize, ["strides"] = strides, ["padding"] = padding, ["explicit_paddings"] = explicit_paddings, ["data_format"] = data_format } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return max_pool_grad_eager_fallback(orig_input, orig_output, grad, ksize: ksize, strides: strides, padding: padding, explicit_paddings: explicit_paddings, data_format: data_format, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + if (data_format is null) + { + data_format = "NHWC"; + } + Dictionary keywords = new(); + keywords["orig_input"] = orig_input; + keywords["orig_output"] = orig_output; + keywords["grad"] = grad; + keywords["ksize"] = ksize; + keywords["strides"] = strides; + keywords["padding"] = padding; + keywords["explicit_paddings"] = explicit_paddings; + keywords["data_format"] = data_format; + var _op = tf.OpDefLib._apply_op_helper("MaxPoolGrad", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "ksize", _op.get_attr("ksize"), "strides", _op.get_attr("strides"), "padding", _op.get_attr("padding"), "explicit_paddings", _op.get_attr("explicit_paddings"), "data_format", _op.get_attr("data_format"), "T", _op._get_attr_type("T") }; + _execute.record_gradient("MaxPoolGrad", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor max_pool_grad_eager_fallback(Tensor orig_input, Tensor orig_output, Tensor grad, int[] ksize, int[] strides, string padding, int[] explicit_paddings, string data_format, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { orig_input, orig_output, grad }; + object[] _attrs = new object[] { "ksize", ksize, "strides", strides, "padding", padding, "explicit_paddings", explicit_paddings, "data_format", data_format, "T", orig_input.dtype }; + var _result = _execute.execute("MaxPoolGrad", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("MaxPoolGrad", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes second-order gradients of the maxpooling function. + /// + /// + /// + /// + /// + /// + /// The size of the window for each dimension of the input tensor. + /// + /// + /// + /// + /// The stride of the sliding window for each dimension of the + /// input tensor. + /// + /// + /// + /// + /// The type of padding algorithm to use. + /// + /// + /// + /// + /// Specify the data format of the input and output data. With the + /// default format "NHWC", the data is stored in the order of: + /// [batch, in_height, in_width, in_channels]. + /// Alternatively, the format could be "NCHW", the data storage order of: + /// [batch, in_channels, in_height, in_width]. + /// + /// + /// + public static Tensor max_pool_grad_grad(Tensor orig_input, Tensor orig_output, Tensor grad, int[] ksize, int[] strides, string padding, string data_format = "NHWC", string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "MaxPoolGradGrad", name) { args = new object[] { orig_input, orig_output, grad }, attrs = new Dictionary() { ["ksize"] = ksize, ["strides"] = strides, ["padding"] = padding, ["data_format"] = data_format } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return max_pool_grad_grad_eager_fallback(orig_input, orig_output, grad, ksize: ksize, strides: strides, padding: padding, data_format: data_format, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + if (data_format is null) + { + data_format = "NHWC"; + } + Dictionary keywords = new(); + keywords["orig_input"] = orig_input; + keywords["orig_output"] = orig_output; + keywords["grad"] = grad; + keywords["ksize"] = ksize; + keywords["strides"] = strides; + keywords["padding"] = padding; + keywords["data_format"] = data_format; + var _op = tf.OpDefLib._apply_op_helper("MaxPoolGradGrad", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "ksize", _op.get_attr("ksize"), "strides", _op.get_attr("strides"), "padding", _op.get_attr("padding"), "data_format", _op.get_attr("data_format"), "T", _op._get_attr_type("T") }; + _execute.record_gradient("MaxPoolGradGrad", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor max_pool_grad_grad_eager_fallback(Tensor orig_input, Tensor orig_output, Tensor grad, int[] ksize, int[] strides, string padding, string data_format, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { orig_input, orig_output, grad }; + object[] _attrs = new object[] { "ksize", ksize, "strides", strides, "padding", padding, "data_format", data_format, "T", orig_input.dtype }; + var _result = _execute.execute("MaxPoolGradGrad", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("MaxPoolGradGrad", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes second-order gradients of the maxpooling function. + /// + /// + /// + /// + /// + /// + /// + /// + /// The type of padding algorithm to use. + /// + /// + /// + /// + /// Specify the data format of the input and output data. With the + /// default format "NHWC", the data is stored in the order of: + /// [batch, in_height, in_width, in_channels]. + /// Alternatively, the format could be "NCHW", the data storage order of: + /// [batch, in_channels, in_height, in_width]. + /// + /// + /// + public static Tensor max_pool_grad_grad_v2(Tensor orig_input, Tensor orig_output, Tensor grad, Tensor ksize, Tensor strides, string padding, string data_format = "NHWC", string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "MaxPoolGradGradV2", name) { args = new object[] { orig_input, orig_output, grad, ksize, strides }, attrs = new Dictionary() { ["padding"] = padding, ["data_format"] = data_format } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return max_pool_grad_grad_v2_eager_fallback(orig_input, orig_output, grad, ksize, strides, padding: padding, data_format: data_format, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + if (data_format is null) + { + data_format = "NHWC"; + } + Dictionary keywords = new(); + keywords["orig_input"] = orig_input; + keywords["orig_output"] = orig_output; + keywords["grad"] = grad; + keywords["ksize"] = ksize; + keywords["strides"] = strides; + keywords["padding"] = padding; + keywords["data_format"] = data_format; + var _op = tf.OpDefLib._apply_op_helper("MaxPoolGradGradV2", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "padding", _op.get_attr("padding"), "data_format", _op.get_attr("data_format"), "T", _op._get_attr_type("T") }; + _execute.record_gradient("MaxPoolGradGradV2", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor max_pool_grad_grad_v2_eager_fallback(Tensor orig_input, Tensor orig_output, Tensor grad, Tensor ksize, Tensor strides, string padding, string data_format, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { orig_input, orig_output, grad, ksize, strides }; + object[] _attrs = new object[] { "padding", padding, "data_format", data_format, "T", orig_input.dtype }; + var _result = _execute.execute("MaxPoolGradGradV2", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("MaxPoolGradGradV2", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes second-order gradients of the maxpooling function. + /// + /// + /// + /// + /// + /// + /// The size of the window for each dimension of the input tensor. + /// + /// + /// + /// + /// The stride of the sliding window for each dimension of the + /// input tensor. + /// + /// + /// + /// + /// The type of padding algorithm to use. + /// + /// + /// + /// + /// Whether to include batch dimension in flattened index of `argmax`. + /// + /// + /// + public static Tensor max_pool_grad_grad_with_argmax(Tensor input, Tensor grad, Tensor argmax, int[] ksize, int[] strides, string padding, bool include_batch_in_index = false, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "MaxPoolGradGradWithArgmax", name) { args = new object[] { input, grad, argmax }, attrs = new Dictionary() { ["ksize"] = ksize, ["strides"] = strides, ["padding"] = padding, ["include_batch_in_index"] = include_batch_in_index } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return max_pool_grad_grad_with_argmax_eager_fallback(input, grad, argmax, ksize: ksize, strides: strides, padding: padding, include_batch_in_index: include_batch_in_index, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["grad"] = grad; + keywords["argmax"] = argmax; + keywords["ksize"] = ksize; + keywords["strides"] = strides; + keywords["padding"] = padding; + keywords["include_batch_in_index"] = include_batch_in_index; + var _op = tf.OpDefLib._apply_op_helper("MaxPoolGradGradWithArgmax", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "ksize", _op.get_attr("ksize"), "strides", _op.get_attr("strides"), "padding", _op.get_attr("padding"), "include_batch_in_index", _op._get_attr_bool("include_batch_in_index"), "Targmax", _op._get_attr_type("Targmax"), "T", _op._get_attr_type("T") }; + _execute.record_gradient("MaxPoolGradGradWithArgmax", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor max_pool_grad_grad_with_argmax_eager_fallback(Tensor input, Tensor grad, Tensor argmax, int[] ksize, int[] strides, string padding, bool include_batch_in_index, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, grad, argmax }; + object[] _attrs = new object[] { "ksize", ksize, "strides", strides, "padding", padding, "include_batch_in_index", include_batch_in_index, "Targmax", argmax.dtype, "T", input.dtype }; + var _result = _execute.execute("MaxPoolGradGradWithArgmax", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("MaxPoolGradGradWithArgmax", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes gradients of the maxpooling function. + /// + /// + /// + /// + /// + /// + /// + /// + /// The type of padding algorithm to use. + /// + /// + /// + /// + /// Specify the data format of the input and output data. With the + /// default format "NHWC", the data is stored in the order of: + /// [batch, in_height, in_width, in_channels]. + /// Alternatively, the format could be "NCHW", the data storage order of: + /// [batch, in_channels, in_height, in_width]. + /// + /// + /// + public static Tensor max_pool_grad_v2(Tensor orig_input, Tensor orig_output, Tensor grad, Tensor ksize, Tensor strides, string padding, string data_format = "NHWC", string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "MaxPoolGradV2", name) { args = new object[] { orig_input, orig_output, grad, ksize, strides }, attrs = new Dictionary() { ["padding"] = padding, ["data_format"] = data_format } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return max_pool_grad_v2_eager_fallback(orig_input, orig_output, grad, ksize, strides, padding: padding, data_format: data_format, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + if (data_format is null) + { + data_format = "NHWC"; + } + Dictionary keywords = new(); + keywords["orig_input"] = orig_input; + keywords["orig_output"] = orig_output; + keywords["grad"] = grad; + keywords["ksize"] = ksize; + keywords["strides"] = strides; + keywords["padding"] = padding; + keywords["data_format"] = data_format; + var _op = tf.OpDefLib._apply_op_helper("MaxPoolGradV2", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "padding", _op.get_attr("padding"), "data_format", _op.get_attr("data_format"), "T", _op._get_attr_type("T") }; + _execute.record_gradient("MaxPoolGradV2", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor max_pool_grad_v2_eager_fallback(Tensor orig_input, Tensor orig_output, Tensor grad, Tensor ksize, Tensor strides, string padding, string data_format, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { orig_input, orig_output, grad, ksize, strides }; + object[] _attrs = new object[] { "padding", padding, "data_format", data_format, "T", orig_input.dtype }; + var _result = _execute.execute("MaxPoolGradV2", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("MaxPoolGradV2", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes gradients of the maxpooling function. + /// + /// + /// + /// + /// + /// + /// The size of the window for each dimension of the input tensor. + /// + /// + /// + /// + /// The stride of the sliding window for each dimension of the + /// input tensor. + /// + /// + /// + /// + /// The type of padding algorithm to use. + /// + /// + /// + /// + /// Whether to include batch dimension in flattened index of `argmax`. + /// + /// + /// + public static Tensor max_pool_grad_with_argmax(Tensor input, Tensor grad, Tensor argmax, int[] ksize, int[] strides, string padding, bool include_batch_in_index = false, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "MaxPoolGradWithArgmax", name) { args = new object[] { input, grad, argmax }, attrs = new Dictionary() { ["ksize"] = ksize, ["strides"] = strides, ["padding"] = padding, ["include_batch_in_index"] = include_batch_in_index } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return max_pool_grad_with_argmax_eager_fallback(input, grad, argmax, ksize: ksize, strides: strides, padding: padding, include_batch_in_index: include_batch_in_index, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["grad"] = grad; + keywords["argmax"] = argmax; + keywords["ksize"] = ksize; + keywords["strides"] = strides; + keywords["padding"] = padding; + keywords["include_batch_in_index"] = include_batch_in_index; + var _op = tf.OpDefLib._apply_op_helper("MaxPoolGradWithArgmax", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "ksize", _op.get_attr("ksize"), "strides", _op.get_attr("strides"), "padding", _op.get_attr("padding"), "include_batch_in_index", _op._get_attr_bool("include_batch_in_index"), "Targmax", _op._get_attr_type("Targmax"), "T", _op._get_attr_type("T") }; + _execute.record_gradient("MaxPoolGradWithArgmax", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor max_pool_grad_with_argmax_eager_fallback(Tensor input, Tensor grad, Tensor argmax, int[] ksize, int[] strides, string padding, bool include_batch_in_index, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, grad, argmax }; + object[] _attrs = new object[] { "ksize", ksize, "strides", strides, "padding", padding, "include_batch_in_index", include_batch_in_index, "Targmax", argmax.dtype, "T", input.dtype }; + var _result = _execute.execute("MaxPoolGradWithArgmax", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("MaxPoolGradWithArgmax", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Performs max pooling on the input. + /// + /// + /// + /// + /// + /// + /// The type of padding algorithm to use. + /// + /// + /// + /// + /// Specify the data format of the input and output data. With the + /// default format "NHWC", the data is stored in the order of: + /// [batch, in_height, in_width, in_channels]. + /// Alternatively, the format could be "NCHW", the data storage order of: + /// [batch, in_channels, in_height, in_width]. + /// + /// + /// + public static Tensor max_pool_v2(Tensor input, Tensor ksize, Tensor strides, string padding, string data_format = "NHWC", string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "MaxPoolV2", name) { args = new object[] { input, ksize, strides }, attrs = new Dictionary() { ["padding"] = padding, ["data_format"] = data_format } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return max_pool_v2_eager_fallback(input, ksize, strides, padding: padding, data_format: data_format, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + if (data_format is null) + { + data_format = "NHWC"; + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["ksize"] = ksize; + keywords["strides"] = strides; + keywords["padding"] = padding; + keywords["data_format"] = data_format; + var _op = tf.OpDefLib._apply_op_helper("MaxPoolV2", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "padding", _op.get_attr("padding"), "data_format", _op.get_attr("data_format") }; + _execute.record_gradient("MaxPoolV2", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor max_pool_v2_eager_fallback(Tensor input, Tensor ksize, Tensor strides, string padding, string data_format, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, ksize, strides }; + object[] _attrs = new object[] { "T", input.dtype, "padding", padding, "data_format", data_format }; + var _result = _execute.execute("MaxPoolV2", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("MaxPoolV2", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Performs max pooling on the input and outputs both max values and indices. + /// + /// + /// + /// The indices in `argmax` are flattened, so that a maximum value at position + /// `[b, y, x, c]` becomes flattened index: + /// `(y * width + x) * channels + c` if `include_batch_in_index` is False; + /// `((b * height + y) * width + x) * channels + c` if `include_batch_in_index` is True. + /// + /// The indices returned are always in `[0, height) x [0, width)` before flattening, + /// even if padding is involved and the mathematically correct answer is outside + /// (either negative or too large). This is a bug, but fixing it is difficult to do + /// in a safe backwards compatible way, especially due to flattening. + /// + /// + /// + /// + /// + /// The size of the window for each dimension of the input tensor. + /// + /// + /// + /// + /// The stride of the sliding window for each dimension of the + /// input tensor. + /// + /// + /// + /// + /// + /// The type of padding algorithm to use. + /// + /// + /// + /// + /// Whether to include batch dimension in flattened index of `argmax`. + /// + /// + /// + public static Tensor[] max_pool_with_argmax(Tensor input, int[] ksize, int[] strides, string padding, TF_DataType Targmax = TF_DataType.TF_INT64, bool include_batch_in_index = false, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "MaxPoolWithArgmax", name) { args = new object[] { input }, attrs = new Dictionary() { ["ksize"] = ksize, ["strides"] = strides, ["Targmax"] = Targmax, ["padding"] = padding, ["include_batch_in_index"] = include_batch_in_index } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return max_pool_with_argmax_eager_fallback(input, ksize: ksize, strides: strides, Targmax: Targmax, padding: padding, include_batch_in_index: include_batch_in_index, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["ksize"] = ksize; + keywords["strides"] = strides; + keywords["Targmax"] = Targmax; + keywords["padding"] = padding; + keywords["include_batch_in_index"] = include_batch_in_index; + var _op = tf.OpDefLib._apply_op_helper("MaxPoolWithArgmax", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "ksize", _op.get_attr("ksize"), "strides", _op.get_attr("strides"), "Targmax", _op._get_attr_type("Targmax"), "padding", _op.get_attr("padding"), "include_batch_in_index", _op._get_attr_bool("include_batch_in_index"), "T", _op._get_attr_type("T") }; + _execute.record_gradient("MaxPoolWithArgmax", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] max_pool_with_argmax_eager_fallback(Tensor input, int[] ksize, int[] strides, TF_DataType Targmax, string padding, bool include_batch_in_index, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input }; + object[] _attrs = new object[] { "ksize", ksize, "strides", strides, "Targmax", Targmax, "padding", padding, "include_batch_in_index", include_batch_in_index, "T", input.dtype }; + var _result = _execute.execute("MaxPoolWithArgmax", 2, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("MaxPoolWithArgmax", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// Finds values of the `n`-th order statistic for the last dimension. + /// + /// + /// + /// If the input is a vector (rank-1), finds the entries which is the nth-smallest + /// value in the vector and outputs their values as scalar tensor. + /// + /// For matrices (resp. higher rank input), computes the entries which is the + /// nth-smallest value in each row (resp. vector along the last dimension). Thus, + /// + /// values.shape = input.shape[:-1] + /// + /// + /// + /// + /// + /// + /// When set to True, find the nth-largest value in the vector and vice + /// versa. + /// + /// + /// + public static Tensor nth_element(Tensor input, Tensor n, bool reverse = false, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "NthElement", name) { args = new object[] { input, n }, attrs = new Dictionary() { ["reverse"] = reverse } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return nth_element_eager_fallback(input, n, reverse: reverse, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["n"] = n; + keywords["reverse"] = reverse; + var _op = tf.OpDefLib._apply_op_helper("NthElement", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "reverse", _op._get_attr_bool("reverse"), "T", _op._get_attr_type("T") }; + _execute.record_gradient("NthElement", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor nth_element_eager_fallback(Tensor input, Tensor n, bool reverse, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, n }; + object[] _attrs = new object[] { "reverse", reverse, "T", input.dtype }; + var _result = _execute.execute("NthElement", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("NthElement", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Produces the average pool of the input tensor for quantized types. + /// + /// + /// + /// + /// + /// + /// The size of the window for each dimension of the input tensor. + /// The length must be 4 to match the number of dimensions of the input. + /// + /// + /// + /// + /// The stride of the sliding window for each dimension of the input + /// tensor. The length must be 4 to match the number of dimensions of the input. + /// + /// + /// + /// + /// The type of padding algorithm to use. + /// + /// + /// + public static Tensor[] quantized_avg_pool(Tensor input, Tensor min_input, Tensor max_input, int[] ksize, int[] strides, string padding, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "QuantizedAvgPool", name) { args = new object[] { input, min_input, max_input }, attrs = new Dictionary() { ["ksize"] = ksize, ["strides"] = strides, ["padding"] = padding } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return quantized_avg_pool_eager_fallback(input, min_input, max_input, ksize: ksize, strides: strides, padding: padding, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["min_input"] = min_input; + keywords["max_input"] = max_input; + keywords["ksize"] = ksize; + keywords["strides"] = strides; + keywords["padding"] = padding; + var _op = tf.OpDefLib._apply_op_helper("QuantizedAvgPool", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "ksize", _op.get_attr("ksize"), "strides", _op.get_attr("strides"), "padding", _op.get_attr("padding") }; + _execute.record_gradient("QuantizedAvgPool", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] quantized_avg_pool_eager_fallback(Tensor input, Tensor min_input, Tensor max_input, int[] ksize, int[] strides, string padding, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, min_input, max_input }; + object[] _attrs = new object[] { "T", input.dtype, "ksize", ksize, "strides", strides, "padding", padding }; + var _result = _execute.execute("QuantizedAvgPool", 3, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("QuantizedAvgPool", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// Quantized Batch normalization. + /// + /// + /// + /// This op is deprecated and will be removed in the future. Prefer + /// `tf.nn.batch_normalization`. + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// A small float number to avoid dividing by 0. + /// + /// + /// + /// + /// A bool indicating whether the resulted tensor + /// needs to be multiplied with gamma. + /// + /// + /// + public static Tensor[] quantized_batch_norm_with_global_normalization(Tensor t, Tensor t_min, Tensor t_max, Tensor m, Tensor m_min, Tensor m_max, Tensor v, Tensor v_min, Tensor v_max, Tensor beta, Tensor beta_min, Tensor beta_max, Tensor gamma, Tensor gamma_min, Tensor gamma_max, TF_DataType out_type, float variance_epsilon, bool scale_after_normalization, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "QuantizedBatchNormWithGlobalNormalization", name) { args = new object[] { t, t_min, t_max, m, m_min, m_max, v, v_min, v_max, beta, beta_min, beta_max, gamma, gamma_min, gamma_max }, attrs = new Dictionary() { ["out_type"] = out_type, ["variance_epsilon"] = variance_epsilon, ["scale_after_normalization"] = scale_after_normalization } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return quantized_batch_norm_with_global_normalization_eager_fallback(t, t_min, t_max, m, m_min, m_max, v, v_min, v_max, beta, beta_min, beta_max, gamma, gamma_min, gamma_max, out_type: out_type, variance_epsilon: variance_epsilon, scale_after_normalization: scale_after_normalization, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["t"] = t; + keywords["t_min"] = t_min; + keywords["t_max"] = t_max; + keywords["m"] = m; + keywords["m_min"] = m_min; + keywords["m_max"] = m_max; + keywords["v"] = v; + keywords["v_min"] = v_min; + keywords["v_max"] = v_max; + keywords["beta"] = beta; + keywords["beta_min"] = beta_min; + keywords["beta_max"] = beta_max; + keywords["gamma"] = gamma; + keywords["gamma_min"] = gamma_min; + keywords["gamma_max"] = gamma_max; + keywords["out_type"] = out_type; + keywords["variance_epsilon"] = variance_epsilon; + keywords["scale_after_normalization"] = scale_after_normalization; + var _op = tf.OpDefLib._apply_op_helper("QuantizedBatchNormWithGlobalNormalization", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "Tinput", _op._get_attr_type("Tinput"), "out_type", _op._get_attr_type("out_type"), "variance_epsilon", _op.get_attr("variance_epsilon"), "scale_after_normalization", _op._get_attr_bool("scale_after_normalization") }; + _execute.record_gradient("QuantizedBatchNormWithGlobalNormalization", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] quantized_batch_norm_with_global_normalization_eager_fallback(Tensor t, Tensor t_min, Tensor t_max, Tensor m, Tensor m_min, Tensor m_max, Tensor v, Tensor v_min, Tensor v_max, Tensor beta, Tensor beta_min, Tensor beta_max, Tensor gamma, Tensor gamma_min, Tensor gamma_max, TF_DataType out_type, float variance_epsilon, bool scale_after_normalization, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { t, t_min, t_max, m, m_min, m_max, v, v_min, v_max, beta, beta_min, beta_max, gamma, gamma_min, gamma_max }; + object[] _attrs = new object[] { "Tinput", t.dtype, "out_type", out_type, "variance_epsilon", variance_epsilon, "scale_after_normalization", scale_after_normalization }; + var _result = _execute.execute("QuantizedBatchNormWithGlobalNormalization", 3, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("QuantizedBatchNormWithGlobalNormalization", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// Adds Tensor 'bias' to Tensor 'input' for Quantized types. + /// + /// + /// + /// Broadcasts the values of bias on dimensions 0..N-2 of 'input'. + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + public static Tensor[] quantized_bias_add(Tensor input, Tensor bias, Tensor min_input, Tensor max_input, Tensor min_bias, Tensor max_bias, TF_DataType out_type, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "QuantizedBiasAdd", name) { args = new object[] { input, bias, min_input, max_input, min_bias, max_bias }, attrs = new Dictionary() { ["out_type"] = out_type } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return quantized_bias_add_eager_fallback(input, bias, min_input, max_input, min_bias, max_bias, out_type: out_type, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["bias"] = bias; + keywords["min_input"] = min_input; + keywords["max_input"] = max_input; + keywords["min_bias"] = min_bias; + keywords["max_bias"] = max_bias; + keywords["out_type"] = out_type; + var _op = tf.OpDefLib._apply_op_helper("QuantizedBiasAdd", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T1", _op._get_attr_type("T1"), "T2", _op._get_attr_type("T2"), "out_type", _op._get_attr_type("out_type") }; + _execute.record_gradient("QuantizedBiasAdd", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] quantized_bias_add_eager_fallback(Tensor input, Tensor bias, Tensor min_input, Tensor max_input, Tensor min_bias, Tensor max_bias, TF_DataType out_type, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, bias, min_input, max_input, min_bias, max_bias }; + object[] _attrs = new object[] { "T1", input.dtype, "T2", bias.dtype, "out_type", out_type }; + var _result = _execute.execute("QuantizedBiasAdd", 3, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("QuantizedBiasAdd", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// Computes a 2D convolution given quantized 4D input and filter tensors. + /// + /// + /// + /// The inputs are quantized tensors where the lowest value represents the real + /// number of the associated minimum, and the highest represents the maximum. + /// This means that you can only interpret the quantized output in the same way, by + /// taking the returned minimum and maximum values into account. + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// The stride of the sliding window for each dimension of the input + /// tensor. + /// + /// + /// + /// + /// The type of padding algorithm to use. + /// + /// + /// + /// + /// 1-D tensor of length 4. The dilation factor for each dimension of + /// `input`. If set to k > 1, there will be k-1 skipped cells between each + /// filter element on that dimension. The dimension order is determined by the + /// value of `data_format`, see above for details. Dilations in the batch and + /// depth dimensions must be 1. + /// + /// + /// + public static Tensor[] quantized_conv2d(Tensor input, Tensor filter, Tensor min_input, Tensor max_input, Tensor min_filter, Tensor max_filter, int[] strides, string padding, TF_DataType out_type = TF_DataType.TF_QINT32, int[] dilations = null, string? name = null) + { + var _ctx = tf.Context; + if (dilations is null) + { + dilations = new int[] { 1, 1, 1, 1 }; + } + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "QuantizedConv2D", name) { args = new object[] { input, filter, min_input, max_input, min_filter, max_filter }, attrs = new Dictionary() { ["out_type"] = out_type, ["strides"] = strides, ["padding"] = padding, ["dilations"] = dilations } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return quantized_conv2d_eager_fallback(input, filter, min_input, max_input, min_filter, max_filter, out_type: out_type, strides: strides, padding: padding, dilations: dilations, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["filter"] = filter; + keywords["min_input"] = min_input; + keywords["max_input"] = max_input; + keywords["min_filter"] = min_filter; + keywords["max_filter"] = max_filter; + keywords["out_type"] = out_type; + keywords["strides"] = strides; + keywords["padding"] = padding; + keywords["dilations"] = dilations; + var _op = tf.OpDefLib._apply_op_helper("QuantizedConv2D", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "Tinput", _op._get_attr_type("Tinput"), "Tfilter", _op._get_attr_type("Tfilter"), "out_type", _op._get_attr_type("out_type"), "strides", _op.get_attr("strides"), "padding", _op.get_attr("padding"), "dilations", _op.get_attr("dilations") }; + _execute.record_gradient("QuantizedConv2D", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] quantized_conv2d_eager_fallback(Tensor input, Tensor filter, Tensor min_input, Tensor max_input, Tensor min_filter, Tensor max_filter, TF_DataType out_type, int[] strides, string padding, int[] dilations, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, filter, min_input, max_input, min_filter, max_filter }; + object[] _attrs = new object[] { "Tinput", input.dtype, "Tfilter", filter.dtype, "out_type", out_type, "strides", strides, "padding", padding, "dilations", dilations }; + var _result = _execute.execute("QuantizedConv2D", 3, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("QuantizedConv2D", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + public static Tensor[] quantized_conv2d_and_relu(Tensor input, Tensor filter, Tensor min_input, Tensor max_input, Tensor min_filter, Tensor max_filter, int[] strides, string padding, TF_DataType out_type = TF_DataType.TF_QINT32, int[] dilations = null, int[] padding_list = null, string? name = null) + { + var _ctx = tf.Context; + if (dilations is null) + { + dilations = new int[] { 1, 1, 1, 1 }; + } + if (padding_list is null) + { + padding_list = new int[] { }; + } + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "QuantizedConv2DAndRelu", name) { args = new object[] { input, filter, min_input, max_input, min_filter, max_filter }, attrs = new Dictionary() { ["out_type"] = out_type, ["strides"] = strides, ["padding"] = padding, ["dilations"] = dilations, ["padding_list"] = padding_list } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return quantized_conv2d_and_relu_eager_fallback(input, filter, min_input, max_input, min_filter, max_filter, out_type: out_type, strides: strides, padding: padding, dilations: dilations, padding_list: padding_list, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["filter"] = filter; + keywords["min_input"] = min_input; + keywords["max_input"] = max_input; + keywords["min_filter"] = min_filter; + keywords["max_filter"] = max_filter; + keywords["out_type"] = out_type; + keywords["strides"] = strides; + keywords["padding"] = padding; + keywords["dilations"] = dilations; + keywords["padding_list"] = padding_list; + var _op = tf.OpDefLib._apply_op_helper("QuantizedConv2DAndRelu", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "Tinput", _op._get_attr_type("Tinput"), "Tfilter", _op._get_attr_type("Tfilter"), "out_type", _op._get_attr_type("out_type"), "strides", _op.get_attr("strides"), "padding", _op.get_attr("padding"), "dilations", _op.get_attr("dilations"), "padding_list", _op.get_attr("padding_list") }; + _execute.record_gradient("QuantizedConv2DAndRelu", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] quantized_conv2d_and_relu_eager_fallback(Tensor input, Tensor filter, Tensor min_input, Tensor max_input, Tensor min_filter, Tensor max_filter, TF_DataType out_type, int[] strides, string padding, int[] dilations, int[] padding_list, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, filter, min_input, max_input, min_filter, max_filter }; + object[] _attrs = new object[] { "Tinput", input.dtype, "Tfilter", filter.dtype, "out_type", out_type, "strides", strides, "padding", padding, "dilations", dilations, "padding_list", padding_list }; + var _result = _execute.execute("QuantizedConv2DAndRelu", 3, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("QuantizedConv2DAndRelu", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + public static Tensor[] quantized_conv2d_and_relu_and_requantize(Tensor input, Tensor filter, Tensor min_input, Tensor max_input, Tensor min_filter, Tensor max_filter, Tensor min_freezed_output, Tensor max_freezed_output, int[] strides, string padding, TF_DataType out_type = TF_DataType.TF_QUINT8, int[] dilations = null, int[] padding_list = null, string? name = null) + { + var _ctx = tf.Context; + if (dilations is null) + { + dilations = new int[] { 1, 1, 1, 1 }; + } + if (padding_list is null) + { + padding_list = new int[] { }; + } + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "QuantizedConv2DAndReluAndRequantize", name) { args = new object[] { input, filter, min_input, max_input, min_filter, max_filter, min_freezed_output, max_freezed_output }, attrs = new Dictionary() { ["out_type"] = out_type, ["strides"] = strides, ["padding"] = padding, ["dilations"] = dilations, ["padding_list"] = padding_list } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return quantized_conv2d_and_relu_and_requantize_eager_fallback(input, filter, min_input, max_input, min_filter, max_filter, min_freezed_output, max_freezed_output, out_type: out_type, strides: strides, padding: padding, dilations: dilations, padding_list: padding_list, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["filter"] = filter; + keywords["min_input"] = min_input; + keywords["max_input"] = max_input; + keywords["min_filter"] = min_filter; + keywords["max_filter"] = max_filter; + keywords["min_freezed_output"] = min_freezed_output; + keywords["max_freezed_output"] = max_freezed_output; + keywords["out_type"] = out_type; + keywords["strides"] = strides; + keywords["padding"] = padding; + keywords["dilations"] = dilations; + keywords["padding_list"] = padding_list; + var _op = tf.OpDefLib._apply_op_helper("QuantizedConv2DAndReluAndRequantize", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "Tinput", _op._get_attr_type("Tinput"), "Tfilter", _op._get_attr_type("Tfilter"), "out_type", _op._get_attr_type("out_type"), "strides", _op.get_attr("strides"), "padding", _op.get_attr("padding"), "dilations", _op.get_attr("dilations"), "padding_list", _op.get_attr("padding_list") }; + _execute.record_gradient("QuantizedConv2DAndReluAndRequantize", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] quantized_conv2d_and_relu_and_requantize_eager_fallback(Tensor input, Tensor filter, Tensor min_input, Tensor max_input, Tensor min_filter, Tensor max_filter, Tensor min_freezed_output, Tensor max_freezed_output, TF_DataType out_type, int[] strides, string padding, int[] dilations, int[] padding_list, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, filter, min_input, max_input, min_filter, max_filter, min_freezed_output, max_freezed_output }; + object[] _attrs = new object[] { "Tinput", input.dtype, "Tfilter", filter.dtype, "out_type", out_type, "strides", strides, "padding", padding, "dilations", dilations, "padding_list", padding_list }; + var _result = _execute.execute("QuantizedConv2DAndReluAndRequantize", 3, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("QuantizedConv2DAndReluAndRequantize", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + public static Tensor[] quantized_conv2d_and_requantize(Tensor input, Tensor filter, Tensor min_input, Tensor max_input, Tensor min_filter, Tensor max_filter, Tensor min_freezed_output, Tensor max_freezed_output, int[] strides, string padding, TF_DataType out_type = TF_DataType.TF_QINT8, int[] dilations = null, int[] padding_list = null, string? name = null) + { + var _ctx = tf.Context; + if (dilations is null) + { + dilations = new int[] { 1, 1, 1, 1 }; + } + if (padding_list is null) + { + padding_list = new int[] { }; + } + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "QuantizedConv2DAndRequantize", name) { args = new object[] { input, filter, min_input, max_input, min_filter, max_filter, min_freezed_output, max_freezed_output }, attrs = new Dictionary() { ["out_type"] = out_type, ["strides"] = strides, ["padding"] = padding, ["dilations"] = dilations, ["padding_list"] = padding_list } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return quantized_conv2d_and_requantize_eager_fallback(input, filter, min_input, max_input, min_filter, max_filter, min_freezed_output, max_freezed_output, out_type: out_type, strides: strides, padding: padding, dilations: dilations, padding_list: padding_list, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["filter"] = filter; + keywords["min_input"] = min_input; + keywords["max_input"] = max_input; + keywords["min_filter"] = min_filter; + keywords["max_filter"] = max_filter; + keywords["min_freezed_output"] = min_freezed_output; + keywords["max_freezed_output"] = max_freezed_output; + keywords["out_type"] = out_type; + keywords["strides"] = strides; + keywords["padding"] = padding; + keywords["dilations"] = dilations; + keywords["padding_list"] = padding_list; + var _op = tf.OpDefLib._apply_op_helper("QuantizedConv2DAndRequantize", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "Tinput", _op._get_attr_type("Tinput"), "Tfilter", _op._get_attr_type("Tfilter"), "out_type", _op._get_attr_type("out_type"), "strides", _op.get_attr("strides"), "padding", _op.get_attr("padding"), "dilations", _op.get_attr("dilations"), "padding_list", _op.get_attr("padding_list") }; + _execute.record_gradient("QuantizedConv2DAndRequantize", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] quantized_conv2d_and_requantize_eager_fallback(Tensor input, Tensor filter, Tensor min_input, Tensor max_input, Tensor min_filter, Tensor max_filter, Tensor min_freezed_output, Tensor max_freezed_output, TF_DataType out_type, int[] strides, string padding, int[] dilations, int[] padding_list, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, filter, min_input, max_input, min_filter, max_filter, min_freezed_output, max_freezed_output }; + object[] _attrs = new object[] { "Tinput", input.dtype, "Tfilter", filter.dtype, "out_type", out_type, "strides", strides, "padding", padding, "dilations", dilations, "padding_list", padding_list }; + var _result = _execute.execute("QuantizedConv2DAndRequantize", 3, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("QuantizedConv2DAndRequantize", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// Computes QuantizedConv2D per channel. + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// The quantized type of output tensor that needs to be converted. + /// + /// + /// + /// list of stride values. + /// + /// + /// + /// list of dilation values. + /// + /// + public static Tensor[] quantized_conv2d_per_channel(Tensor input, Tensor filter, Tensor min_input, Tensor max_input, Tensor min_filter, Tensor max_filter, int[] strides, string padding, TF_DataType out_type = TF_DataType.TF_QINT32, int[] dilations = null, string? name = null) + { + var _ctx = tf.Context; + if (dilations is null) + { + dilations = new int[] { 1, 1, 1, 1 }; + } + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "QuantizedConv2DPerChannel", name) { args = new object[] { input, filter, min_input, max_input, min_filter, max_filter }, attrs = new Dictionary() { ["out_type"] = out_type, ["strides"] = strides, ["padding"] = padding, ["dilations"] = dilations } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return quantized_conv2d_per_channel_eager_fallback(input, filter, min_input, max_input, min_filter, max_filter, out_type: out_type, strides: strides, padding: padding, dilations: dilations, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["filter"] = filter; + keywords["min_input"] = min_input; + keywords["max_input"] = max_input; + keywords["min_filter"] = min_filter; + keywords["max_filter"] = max_filter; + keywords["out_type"] = out_type; + keywords["strides"] = strides; + keywords["padding"] = padding; + keywords["dilations"] = dilations; + var _op = tf.OpDefLib._apply_op_helper("QuantizedConv2DPerChannel", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "Tinput", _op._get_attr_type("Tinput"), "Tfilter", _op._get_attr_type("Tfilter"), "out_type", _op._get_attr_type("out_type"), "strides", _op.get_attr("strides"), "padding", _op.get_attr("padding"), "dilations", _op.get_attr("dilations") }; + _execute.record_gradient("QuantizedConv2DPerChannel", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] quantized_conv2d_per_channel_eager_fallback(Tensor input, Tensor filter, Tensor min_input, Tensor max_input, Tensor min_filter, Tensor max_filter, TF_DataType out_type, int[] strides, string padding, int[] dilations, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, filter, min_input, max_input, min_filter, max_filter }; + object[] _attrs = new object[] { "Tinput", input.dtype, "Tfilter", filter.dtype, "out_type", out_type, "strides", strides, "padding", padding, "dilations", dilations }; + var _result = _execute.execute("QuantizedConv2DPerChannel", 3, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("QuantizedConv2DPerChannel", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + public static Tensor[] quantized_conv2d_with_bias(Tensor input, Tensor filter, Tensor bias, Tensor min_input, Tensor max_input, Tensor min_filter, Tensor max_filter, int[] strides, string padding, TF_DataType out_type = TF_DataType.TF_QINT32, int[] dilations = null, int[] padding_list = null, string? name = null) + { + var _ctx = tf.Context; + if (dilations is null) + { + dilations = new int[] { 1, 1, 1, 1 }; + } + if (padding_list is null) + { + padding_list = new int[] { }; + } + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "QuantizedConv2DWithBias", name) { args = new object[] { input, filter, bias, min_input, max_input, min_filter, max_filter }, attrs = new Dictionary() { ["out_type"] = out_type, ["strides"] = strides, ["padding"] = padding, ["dilations"] = dilations, ["padding_list"] = padding_list } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return quantized_conv2d_with_bias_eager_fallback(input, filter, bias, min_input, max_input, min_filter, max_filter, out_type: out_type, strides: strides, padding: padding, dilations: dilations, padding_list: padding_list, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["filter"] = filter; + keywords["bias"] = bias; + keywords["min_input"] = min_input; + keywords["max_input"] = max_input; + keywords["min_filter"] = min_filter; + keywords["max_filter"] = max_filter; + keywords["out_type"] = out_type; + keywords["strides"] = strides; + keywords["padding"] = padding; + keywords["dilations"] = dilations; + keywords["padding_list"] = padding_list; + var _op = tf.OpDefLib._apply_op_helper("QuantizedConv2DWithBias", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "Tinput", _op._get_attr_type("Tinput"), "Tfilter", _op._get_attr_type("Tfilter"), "out_type", _op._get_attr_type("out_type"), "strides", _op.get_attr("strides"), "padding", _op.get_attr("padding"), "dilations", _op.get_attr("dilations"), "padding_list", _op.get_attr("padding_list") }; + _execute.record_gradient("QuantizedConv2DWithBias", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] quantized_conv2d_with_bias_eager_fallback(Tensor input, Tensor filter, Tensor bias, Tensor min_input, Tensor max_input, Tensor min_filter, Tensor max_filter, TF_DataType out_type, int[] strides, string padding, int[] dilations, int[] padding_list, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, filter, bias, min_input, max_input, min_filter, max_filter }; + object[] _attrs = new object[] { "Tinput", input.dtype, "Tfilter", filter.dtype, "out_type", out_type, "strides", strides, "padding", padding, "dilations", dilations, "padding_list", padding_list }; + var _result = _execute.execute("QuantizedConv2DWithBias", 3, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("QuantizedConv2DWithBias", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + public static Tensor[] quantized_conv2d_with_bias_and_relu(Tensor input, Tensor filter, Tensor bias, Tensor min_input, Tensor max_input, Tensor min_filter, Tensor max_filter, int[] strides, string padding, TF_DataType out_type = TF_DataType.TF_QINT32, int[] dilations = null, int[] padding_list = null, string? name = null) + { + var _ctx = tf.Context; + if (dilations is null) + { + dilations = new int[] { 1, 1, 1, 1 }; + } + if (padding_list is null) + { + padding_list = new int[] { }; + } + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "QuantizedConv2DWithBiasAndRelu", name) { args = new object[] { input, filter, bias, min_input, max_input, min_filter, max_filter }, attrs = new Dictionary() { ["out_type"] = out_type, ["strides"] = strides, ["padding"] = padding, ["dilations"] = dilations, ["padding_list"] = padding_list } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return quantized_conv2d_with_bias_and_relu_eager_fallback(input, filter, bias, min_input, max_input, min_filter, max_filter, out_type: out_type, strides: strides, padding: padding, dilations: dilations, padding_list: padding_list, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["filter"] = filter; + keywords["bias"] = bias; + keywords["min_input"] = min_input; + keywords["max_input"] = max_input; + keywords["min_filter"] = min_filter; + keywords["max_filter"] = max_filter; + keywords["out_type"] = out_type; + keywords["strides"] = strides; + keywords["padding"] = padding; + keywords["dilations"] = dilations; + keywords["padding_list"] = padding_list; + var _op = tf.OpDefLib._apply_op_helper("QuantizedConv2DWithBiasAndRelu", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "Tinput", _op._get_attr_type("Tinput"), "Tfilter", _op._get_attr_type("Tfilter"), "out_type", _op._get_attr_type("out_type"), "strides", _op.get_attr("strides"), "padding", _op.get_attr("padding"), "dilations", _op.get_attr("dilations"), "padding_list", _op.get_attr("padding_list") }; + _execute.record_gradient("QuantizedConv2DWithBiasAndRelu", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] quantized_conv2d_with_bias_and_relu_eager_fallback(Tensor input, Tensor filter, Tensor bias, Tensor min_input, Tensor max_input, Tensor min_filter, Tensor max_filter, TF_DataType out_type, int[] strides, string padding, int[] dilations, int[] padding_list, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, filter, bias, min_input, max_input, min_filter, max_filter }; + object[] _attrs = new object[] { "Tinput", input.dtype, "Tfilter", filter.dtype, "out_type", out_type, "strides", strides, "padding", padding, "dilations", dilations, "padding_list", padding_list }; + var _result = _execute.execute("QuantizedConv2DWithBiasAndRelu", 3, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("QuantizedConv2DWithBiasAndRelu", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + public static Tensor[] quantized_conv2d_with_bias_and_relu_and_requantize(Tensor input, Tensor filter, Tensor bias, Tensor min_input, Tensor max_input, Tensor min_filter, Tensor max_filter, Tensor min_freezed_output, Tensor max_freezed_output, int[] strides, string padding, TF_DataType out_type = TF_DataType.TF_QUINT8, int[] dilations = null, int[] padding_list = null, string? name = null) + { + var _ctx = tf.Context; + if (dilations is null) + { + dilations = new int[] { 1, 1, 1, 1 }; + } + if (padding_list is null) + { + padding_list = new int[] { }; + } + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "QuantizedConv2DWithBiasAndReluAndRequantize", name) { args = new object[] { input, filter, bias, min_input, max_input, min_filter, max_filter, min_freezed_output, max_freezed_output }, attrs = new Dictionary() { ["out_type"] = out_type, ["strides"] = strides, ["padding"] = padding, ["dilations"] = dilations, ["padding_list"] = padding_list } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return quantized_conv2d_with_bias_and_relu_and_requantize_eager_fallback(input, filter, bias, min_input, max_input, min_filter, max_filter, min_freezed_output, max_freezed_output, out_type: out_type, strides: strides, padding: padding, dilations: dilations, padding_list: padding_list, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["filter"] = filter; + keywords["bias"] = bias; + keywords["min_input"] = min_input; + keywords["max_input"] = max_input; + keywords["min_filter"] = min_filter; + keywords["max_filter"] = max_filter; + keywords["min_freezed_output"] = min_freezed_output; + keywords["max_freezed_output"] = max_freezed_output; + keywords["out_type"] = out_type; + keywords["strides"] = strides; + keywords["padding"] = padding; + keywords["dilations"] = dilations; + keywords["padding_list"] = padding_list; + var _op = tf.OpDefLib._apply_op_helper("QuantizedConv2DWithBiasAndReluAndRequantize", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "Tinput", _op._get_attr_type("Tinput"), "Tfilter", _op._get_attr_type("Tfilter"), "Tbias", _op._get_attr_type("Tbias"), "out_type", _op._get_attr_type("out_type"), "strides", _op.get_attr("strides"), "padding", _op.get_attr("padding"), "dilations", _op.get_attr("dilations"), "padding_list", _op.get_attr("padding_list") }; + _execute.record_gradient("QuantizedConv2DWithBiasAndReluAndRequantize", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] quantized_conv2d_with_bias_and_relu_and_requantize_eager_fallback(Tensor input, Tensor filter, Tensor bias, Tensor min_input, Tensor max_input, Tensor min_filter, Tensor max_filter, Tensor min_freezed_output, Tensor max_freezed_output, TF_DataType out_type, int[] strides, string padding, int[] dilations, int[] padding_list, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, filter, bias, min_input, max_input, min_filter, max_filter, min_freezed_output, max_freezed_output }; + object[] _attrs = new object[] { "Tinput", input.dtype, "Tfilter", filter.dtype, "Tbias", bias.dtype, "out_type", out_type, "strides", strides, "padding", padding, "dilations", dilations, "padding_list", padding_list }; + var _result = _execute.execute("QuantizedConv2DWithBiasAndReluAndRequantize", 3, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("QuantizedConv2DWithBiasAndReluAndRequantize", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + public static Tensor[] quantized_conv2d_with_bias_and_requantize(Tensor input, Tensor filter, Tensor bias, Tensor min_input, Tensor max_input, Tensor min_filter, Tensor max_filter, Tensor min_freezed_output, Tensor max_freezed_output, int[] strides, string padding, TF_DataType out_type = TF_DataType.TF_QINT8, int[] dilations = null, int[] padding_list = null, string? name = null) + { + var _ctx = tf.Context; + if (dilations is null) + { + dilations = new int[] { 1, 1, 1, 1 }; + } + if (padding_list is null) + { + padding_list = new int[] { }; + } + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "QuantizedConv2DWithBiasAndRequantize", name) { args = new object[] { input, filter, bias, min_input, max_input, min_filter, max_filter, min_freezed_output, max_freezed_output }, attrs = new Dictionary() { ["out_type"] = out_type, ["strides"] = strides, ["padding"] = padding, ["dilations"] = dilations, ["padding_list"] = padding_list } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return quantized_conv2d_with_bias_and_requantize_eager_fallback(input, filter, bias, min_input, max_input, min_filter, max_filter, min_freezed_output, max_freezed_output, out_type: out_type, strides: strides, padding: padding, dilations: dilations, padding_list: padding_list, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["filter"] = filter; + keywords["bias"] = bias; + keywords["min_input"] = min_input; + keywords["max_input"] = max_input; + keywords["min_filter"] = min_filter; + keywords["max_filter"] = max_filter; + keywords["min_freezed_output"] = min_freezed_output; + keywords["max_freezed_output"] = max_freezed_output; + keywords["out_type"] = out_type; + keywords["strides"] = strides; + keywords["padding"] = padding; + keywords["dilations"] = dilations; + keywords["padding_list"] = padding_list; + var _op = tf.OpDefLib._apply_op_helper("QuantizedConv2DWithBiasAndRequantize", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "Tinput", _op._get_attr_type("Tinput"), "Tfilter", _op._get_attr_type("Tfilter"), "Tbias", _op._get_attr_type("Tbias"), "out_type", _op._get_attr_type("out_type"), "strides", _op.get_attr("strides"), "padding", _op.get_attr("padding"), "dilations", _op.get_attr("dilations"), "padding_list", _op.get_attr("padding_list") }; + _execute.record_gradient("QuantizedConv2DWithBiasAndRequantize", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] quantized_conv2d_with_bias_and_requantize_eager_fallback(Tensor input, Tensor filter, Tensor bias, Tensor min_input, Tensor max_input, Tensor min_filter, Tensor max_filter, Tensor min_freezed_output, Tensor max_freezed_output, TF_DataType out_type, int[] strides, string padding, int[] dilations, int[] padding_list, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, filter, bias, min_input, max_input, min_filter, max_filter, min_freezed_output, max_freezed_output }; + object[] _attrs = new object[] { "Tinput", input.dtype, "Tfilter", filter.dtype, "Tbias", bias.dtype, "out_type", out_type, "strides", strides, "padding", padding, "dilations", dilations, "padding_list", padding_list }; + var _result = _execute.execute("QuantizedConv2DWithBiasAndRequantize", 3, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("QuantizedConv2DWithBiasAndRequantize", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + public static Tensor[] quantized_conv2d_with_bias_signed_sum_and_relu_and_requantize(Tensor input, Tensor filter, Tensor bias, Tensor min_input, Tensor max_input, Tensor min_filter, Tensor max_filter, Tensor min_freezed_output, Tensor max_freezed_output, Tensor summand, Tensor min_summand, Tensor max_summand, int[] strides, string padding, TF_DataType out_type = TF_DataType.TF_QUINT8, int[] dilations = null, int[] padding_list = null, string? name = null) + { + var _ctx = tf.Context; + if (dilations is null) + { + dilations = new int[] { 1, 1, 1, 1 }; + } + if (padding_list is null) + { + padding_list = new int[] { }; + } + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "QuantizedConv2DWithBiasSignedSumAndReluAndRequantize", name) { args = new object[] { input, filter, bias, min_input, max_input, min_filter, max_filter, min_freezed_output, max_freezed_output, summand, min_summand, max_summand }, attrs = new Dictionary() { ["out_type"] = out_type, ["strides"] = strides, ["padding"] = padding, ["dilations"] = dilations, ["padding_list"] = padding_list } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return quantized_conv2d_with_bias_signed_sum_and_relu_and_requantize_eager_fallback(input, filter, bias, min_input, max_input, min_filter, max_filter, min_freezed_output, max_freezed_output, summand, min_summand, max_summand, out_type: out_type, strides: strides, padding: padding, dilations: dilations, padding_list: padding_list, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["filter"] = filter; + keywords["bias"] = bias; + keywords["min_input"] = min_input; + keywords["max_input"] = max_input; + keywords["min_filter"] = min_filter; + keywords["max_filter"] = max_filter; + keywords["min_freezed_output"] = min_freezed_output; + keywords["max_freezed_output"] = max_freezed_output; + keywords["summand"] = summand; + keywords["min_summand"] = min_summand; + keywords["max_summand"] = max_summand; + keywords["out_type"] = out_type; + keywords["strides"] = strides; + keywords["padding"] = padding; + keywords["dilations"] = dilations; + keywords["padding_list"] = padding_list; + var _op = tf.OpDefLib._apply_op_helper("QuantizedConv2DWithBiasSignedSumAndReluAndRequantize", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "Tinput", _op._get_attr_type("Tinput"), "Tfilter", _op._get_attr_type("Tfilter"), "Tbias", _op._get_attr_type("Tbias"), "Tsummand", _op._get_attr_type("Tsummand"), "out_type", _op._get_attr_type("out_type"), "strides", _op.get_attr("strides"), "padding", _op.get_attr("padding"), "dilations", _op.get_attr("dilations"), "padding_list", _op.get_attr("padding_list") }; + _execute.record_gradient("QuantizedConv2DWithBiasSignedSumAndReluAndRequantize", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] quantized_conv2d_with_bias_signed_sum_and_relu_and_requantize_eager_fallback(Tensor input, Tensor filter, Tensor bias, Tensor min_input, Tensor max_input, Tensor min_filter, Tensor max_filter, Tensor min_freezed_output, Tensor max_freezed_output, Tensor summand, Tensor min_summand, Tensor max_summand, TF_DataType out_type, int[] strides, string padding, int[] dilations, int[] padding_list, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, filter, bias, min_input, max_input, min_filter, max_filter, min_freezed_output, max_freezed_output, summand, min_summand, max_summand }; + object[] _attrs = new object[] { "Tinput", input.dtype, "Tfilter", filter.dtype, "Tbias", bias.dtype, "Tsummand", summand.dtype, "out_type", out_type, "strides", strides, "padding", padding, "dilations", dilations, "padding_list", padding_list }; + var _result = _execute.execute("QuantizedConv2DWithBiasSignedSumAndReluAndRequantize", 3, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("QuantizedConv2DWithBiasSignedSumAndReluAndRequantize", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + public static Tensor[] quantized_conv2d_with_bias_sum_and_relu(Tensor input, Tensor filter, Tensor bias, Tensor min_input, Tensor max_input, Tensor min_filter, Tensor max_filter, Tensor summand, int[] strides, string padding, TF_DataType out_type = TF_DataType.TF_QINT32, int[] dilations = null, int[] padding_list = null, string? name = null) + { + var _ctx = tf.Context; + if (dilations is null) + { + dilations = new int[] { 1, 1, 1, 1 }; + } + if (padding_list is null) + { + padding_list = new int[] { }; + } + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "QuantizedConv2DWithBiasSumAndRelu", name) { args = new object[] { input, filter, bias, min_input, max_input, min_filter, max_filter, summand }, attrs = new Dictionary() { ["out_type"] = out_type, ["strides"] = strides, ["padding"] = padding, ["dilations"] = dilations, ["padding_list"] = padding_list } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return quantized_conv2d_with_bias_sum_and_relu_eager_fallback(input, filter, bias, min_input, max_input, min_filter, max_filter, summand, out_type: out_type, strides: strides, padding: padding, dilations: dilations, padding_list: padding_list, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["filter"] = filter; + keywords["bias"] = bias; + keywords["min_input"] = min_input; + keywords["max_input"] = max_input; + keywords["min_filter"] = min_filter; + keywords["max_filter"] = max_filter; + keywords["summand"] = summand; + keywords["out_type"] = out_type; + keywords["strides"] = strides; + keywords["padding"] = padding; + keywords["dilations"] = dilations; + keywords["padding_list"] = padding_list; + var _op = tf.OpDefLib._apply_op_helper("QuantizedConv2DWithBiasSumAndRelu", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "Tinput", _op._get_attr_type("Tinput"), "Tfilter", _op._get_attr_type("Tfilter"), "out_type", _op._get_attr_type("out_type"), "strides", _op.get_attr("strides"), "padding", _op.get_attr("padding"), "dilations", _op.get_attr("dilations"), "padding_list", _op.get_attr("padding_list") }; + _execute.record_gradient("QuantizedConv2DWithBiasSumAndRelu", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] quantized_conv2d_with_bias_sum_and_relu_eager_fallback(Tensor input, Tensor filter, Tensor bias, Tensor min_input, Tensor max_input, Tensor min_filter, Tensor max_filter, Tensor summand, TF_DataType out_type, int[] strides, string padding, int[] dilations, int[] padding_list, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, filter, bias, min_input, max_input, min_filter, max_filter, summand }; + object[] _attrs = new object[] { "Tinput", input.dtype, "Tfilter", filter.dtype, "out_type", out_type, "strides", strides, "padding", padding, "dilations", dilations, "padding_list", padding_list }; + var _result = _execute.execute("QuantizedConv2DWithBiasSumAndRelu", 3, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("QuantizedConv2DWithBiasSumAndRelu", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + public static Tensor[] quantized_conv2d_with_bias_sum_and_relu_and_requantize(Tensor input, Tensor filter, Tensor bias, Tensor min_input, Tensor max_input, Tensor min_filter, Tensor max_filter, Tensor min_freezed_output, Tensor max_freezed_output, Tensor summand, Tensor min_summand, Tensor max_summand, int[] strides, string padding, TF_DataType out_type = TF_DataType.TF_QUINT8, int[] dilations = null, int[] padding_list = null, string? name = null) + { + var _ctx = tf.Context; + if (dilations is null) + { + dilations = new int[] { 1, 1, 1, 1 }; + } + if (padding_list is null) + { + padding_list = new int[] { }; + } + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "QuantizedConv2DWithBiasSumAndReluAndRequantize", name) { args = new object[] { input, filter, bias, min_input, max_input, min_filter, max_filter, min_freezed_output, max_freezed_output, summand, min_summand, max_summand }, attrs = new Dictionary() { ["out_type"] = out_type, ["strides"] = strides, ["padding"] = padding, ["dilations"] = dilations, ["padding_list"] = padding_list } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return quantized_conv2d_with_bias_sum_and_relu_and_requantize_eager_fallback(input, filter, bias, min_input, max_input, min_filter, max_filter, min_freezed_output, max_freezed_output, summand, min_summand, max_summand, out_type: out_type, strides: strides, padding: padding, dilations: dilations, padding_list: padding_list, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["filter"] = filter; + keywords["bias"] = bias; + keywords["min_input"] = min_input; + keywords["max_input"] = max_input; + keywords["min_filter"] = min_filter; + keywords["max_filter"] = max_filter; + keywords["min_freezed_output"] = min_freezed_output; + keywords["max_freezed_output"] = max_freezed_output; + keywords["summand"] = summand; + keywords["min_summand"] = min_summand; + keywords["max_summand"] = max_summand; + keywords["out_type"] = out_type; + keywords["strides"] = strides; + keywords["padding"] = padding; + keywords["dilations"] = dilations; + keywords["padding_list"] = padding_list; + var _op = tf.OpDefLib._apply_op_helper("QuantizedConv2DWithBiasSumAndReluAndRequantize", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "Tinput", _op._get_attr_type("Tinput"), "Tfilter", _op._get_attr_type("Tfilter"), "Tbias", _op._get_attr_type("Tbias"), "Tsummand", _op._get_attr_type("Tsummand"), "out_type", _op._get_attr_type("out_type"), "strides", _op.get_attr("strides"), "padding", _op.get_attr("padding"), "dilations", _op.get_attr("dilations"), "padding_list", _op.get_attr("padding_list") }; + _execute.record_gradient("QuantizedConv2DWithBiasSumAndReluAndRequantize", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] quantized_conv2d_with_bias_sum_and_relu_and_requantize_eager_fallback(Tensor input, Tensor filter, Tensor bias, Tensor min_input, Tensor max_input, Tensor min_filter, Tensor max_filter, Tensor min_freezed_output, Tensor max_freezed_output, Tensor summand, Tensor min_summand, Tensor max_summand, TF_DataType out_type, int[] strides, string padding, int[] dilations, int[] padding_list, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, filter, bias, min_input, max_input, min_filter, max_filter, min_freezed_output, max_freezed_output, summand, min_summand, max_summand }; + object[] _attrs = new object[] { "Tinput", input.dtype, "Tfilter", filter.dtype, "Tbias", bias.dtype, "Tsummand", summand.dtype, "out_type", out_type, "strides", strides, "padding", padding, "dilations", dilations, "padding_list", padding_list }; + var _result = _execute.execute("QuantizedConv2DWithBiasSumAndReluAndRequantize", 3, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("QuantizedConv2DWithBiasSumAndReluAndRequantize", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// Computes quantized depthwise Conv2D. + /// + /// + /// + /// + /// + /// + /// + /// + /// The type of the output. + /// + /// + /// List of stride values. + /// + /// + /// + /// List of dilation values. + /// + /// + public static Tensor[] quantized_depthwise_conv2d(Tensor input, Tensor filter, Tensor min_input, Tensor max_input, Tensor min_filter, Tensor max_filter, int[] strides, string padding, TF_DataType out_type = TF_DataType.TF_QINT32, int[] dilations = null, string? name = null) + { + var _ctx = tf.Context; + if (dilations is null) + { + dilations = new int[] { 1, 1, 1, 1 }; + } + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "QuantizedDepthwiseConv2D", name) { args = new object[] { input, filter, min_input, max_input, min_filter, max_filter }, attrs = new Dictionary() { ["out_type"] = out_type, ["strides"] = strides, ["padding"] = padding, ["dilations"] = dilations } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return quantized_depthwise_conv2d_eager_fallback(input, filter, min_input, max_input, min_filter, max_filter, out_type: out_type, strides: strides, padding: padding, dilations: dilations, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["filter"] = filter; + keywords["min_input"] = min_input; + keywords["max_input"] = max_input; + keywords["min_filter"] = min_filter; + keywords["max_filter"] = max_filter; + keywords["out_type"] = out_type; + keywords["strides"] = strides; + keywords["padding"] = padding; + keywords["dilations"] = dilations; + var _op = tf.OpDefLib._apply_op_helper("QuantizedDepthwiseConv2D", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "Tinput", _op._get_attr_type("Tinput"), "Tfilter", _op._get_attr_type("Tfilter"), "out_type", _op._get_attr_type("out_type"), "strides", _op.get_attr("strides"), "padding", _op.get_attr("padding"), "dilations", _op.get_attr("dilations") }; + _execute.record_gradient("QuantizedDepthwiseConv2D", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] quantized_depthwise_conv2d_eager_fallback(Tensor input, Tensor filter, Tensor min_input, Tensor max_input, Tensor min_filter, Tensor max_filter, TF_DataType out_type, int[] strides, string padding, int[] dilations, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, filter, min_input, max_input, min_filter, max_filter }; + object[] _attrs = new object[] { "Tinput", input.dtype, "Tfilter", filter.dtype, "out_type", out_type, "strides", strides, "padding", padding, "dilations", dilations }; + var _result = _execute.execute("QuantizedDepthwiseConv2D", 3, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("QuantizedDepthwiseConv2D", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// Computes quantized depthwise Conv2D with Bias. + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// The type of the output. + /// + /// + /// List of stride values. + /// + /// + /// + /// List of dilation values. + /// + /// + public static Tensor[] quantized_depthwise_conv2d_with_bias(Tensor input, Tensor filter, Tensor bias, Tensor min_input, Tensor max_input, Tensor min_filter, Tensor max_filter, int[] strides, string padding, TF_DataType out_type = TF_DataType.TF_QINT32, int[] dilations = null, string? name = null) + { + var _ctx = tf.Context; + if (dilations is null) + { + dilations = new int[] { 1, 1, 1, 1 }; + } + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "QuantizedDepthwiseConv2DWithBias", name) { args = new object[] { input, filter, bias, min_input, max_input, min_filter, max_filter }, attrs = new Dictionary() { ["out_type"] = out_type, ["strides"] = strides, ["padding"] = padding, ["dilations"] = dilations } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return quantized_depthwise_conv2d_with_bias_eager_fallback(input, filter, bias, min_input, max_input, min_filter, max_filter, out_type: out_type, strides: strides, padding: padding, dilations: dilations, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["filter"] = filter; + keywords["bias"] = bias; + keywords["min_input"] = min_input; + keywords["max_input"] = max_input; + keywords["min_filter"] = min_filter; + keywords["max_filter"] = max_filter; + keywords["out_type"] = out_type; + keywords["strides"] = strides; + keywords["padding"] = padding; + keywords["dilations"] = dilations; + var _op = tf.OpDefLib._apply_op_helper("QuantizedDepthwiseConv2DWithBias", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "Tinput", _op._get_attr_type("Tinput"), "Tfilter", _op._get_attr_type("Tfilter"), "out_type", _op._get_attr_type("out_type"), "strides", _op.get_attr("strides"), "padding", _op.get_attr("padding"), "dilations", _op.get_attr("dilations") }; + _execute.record_gradient("QuantizedDepthwiseConv2DWithBias", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] quantized_depthwise_conv2d_with_bias_eager_fallback(Tensor input, Tensor filter, Tensor bias, Tensor min_input, Tensor max_input, Tensor min_filter, Tensor max_filter, TF_DataType out_type, int[] strides, string padding, int[] dilations, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, filter, bias, min_input, max_input, min_filter, max_filter }; + object[] _attrs = new object[] { "Tinput", input.dtype, "Tfilter", filter.dtype, "out_type", out_type, "strides", strides, "padding", padding, "dilations", dilations }; + var _result = _execute.execute("QuantizedDepthwiseConv2DWithBias", 3, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("QuantizedDepthwiseConv2DWithBias", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// Computes quantized depthwise Conv2D with Bias and Relu. + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// The type of the output. + /// + /// + /// List of stride values. + /// + /// + /// + /// List of dilation values. + /// + /// + /// + public static Tensor[] quantized_depthwise_conv2d_with_bias_and_relu(Tensor input, Tensor filter, Tensor bias, Tensor min_input, Tensor max_input, Tensor min_filter, Tensor max_filter, int[] strides, string padding, TF_DataType out_type = TF_DataType.TF_QINT32, int[] dilations = null, int[] padding_list = null, string? name = null) + { + var _ctx = tf.Context; + if (dilations is null) + { + dilations = new int[] { 1, 1, 1, 1 }; + } + if (padding_list is null) + { + padding_list = new int[] { }; + } + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "QuantizedDepthwiseConv2DWithBiasAndRelu", name) { args = new object[] { input, filter, bias, min_input, max_input, min_filter, max_filter }, attrs = new Dictionary() { ["out_type"] = out_type, ["strides"] = strides, ["padding"] = padding, ["dilations"] = dilations, ["padding_list"] = padding_list } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return quantized_depthwise_conv2d_with_bias_and_relu_eager_fallback(input, filter, bias, min_input, max_input, min_filter, max_filter, out_type: out_type, strides: strides, padding: padding, dilations: dilations, padding_list: padding_list, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["filter"] = filter; + keywords["bias"] = bias; + keywords["min_input"] = min_input; + keywords["max_input"] = max_input; + keywords["min_filter"] = min_filter; + keywords["max_filter"] = max_filter; + keywords["out_type"] = out_type; + keywords["strides"] = strides; + keywords["padding"] = padding; + keywords["dilations"] = dilations; + keywords["padding_list"] = padding_list; + var _op = tf.OpDefLib._apply_op_helper("QuantizedDepthwiseConv2DWithBiasAndRelu", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "Tinput", _op._get_attr_type("Tinput"), "Tfilter", _op._get_attr_type("Tfilter"), "out_type", _op._get_attr_type("out_type"), "strides", _op.get_attr("strides"), "padding", _op.get_attr("padding"), "dilations", _op.get_attr("dilations"), "padding_list", _op.get_attr("padding_list") }; + _execute.record_gradient("QuantizedDepthwiseConv2DWithBiasAndRelu", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] quantized_depthwise_conv2d_with_bias_and_relu_eager_fallback(Tensor input, Tensor filter, Tensor bias, Tensor min_input, Tensor max_input, Tensor min_filter, Tensor max_filter, TF_DataType out_type, int[] strides, string padding, int[] dilations, int[] padding_list, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, filter, bias, min_input, max_input, min_filter, max_filter }; + object[] _attrs = new object[] { "Tinput", input.dtype, "Tfilter", filter.dtype, "out_type", out_type, "strides", strides, "padding", padding, "dilations", dilations, "padding_list", padding_list }; + var _result = _execute.execute("QuantizedDepthwiseConv2DWithBiasAndRelu", 3, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("QuantizedDepthwiseConv2DWithBiasAndRelu", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// Computes quantized depthwise Conv2D with Bias, Relu and Requantize. + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// The type of the output. + /// + /// + /// List of stride values. + /// + /// + /// + /// List of dilation values. + /// + /// + /// + public static Tensor[] quantized_depthwise_conv2d_with_bias_and_relu_and_requantize(Tensor input, Tensor filter, Tensor bias, Tensor min_input, Tensor max_input, Tensor min_filter, Tensor max_filter, Tensor min_freezed_output, Tensor max_freezed_output, int[] strides, string padding, TF_DataType out_type = TF_DataType.TF_QUINT8, int[] dilations = null, int[] padding_list = null, string? name = null) + { + var _ctx = tf.Context; + if (dilations is null) + { + dilations = new int[] { 1, 1, 1, 1 }; + } + if (padding_list is null) + { + padding_list = new int[] { }; + } + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "QuantizedDepthwiseConv2DWithBiasAndReluAndRequantize", name) { args = new object[] { input, filter, bias, min_input, max_input, min_filter, max_filter, min_freezed_output, max_freezed_output }, attrs = new Dictionary() { ["out_type"] = out_type, ["strides"] = strides, ["padding"] = padding, ["dilations"] = dilations, ["padding_list"] = padding_list } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return quantized_depthwise_conv2d_with_bias_and_relu_and_requantize_eager_fallback(input, filter, bias, min_input, max_input, min_filter, max_filter, min_freezed_output, max_freezed_output, out_type: out_type, strides: strides, padding: padding, dilations: dilations, padding_list: padding_list, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["filter"] = filter; + keywords["bias"] = bias; + keywords["min_input"] = min_input; + keywords["max_input"] = max_input; + keywords["min_filter"] = min_filter; + keywords["max_filter"] = max_filter; + keywords["min_freezed_output"] = min_freezed_output; + keywords["max_freezed_output"] = max_freezed_output; + keywords["out_type"] = out_type; + keywords["strides"] = strides; + keywords["padding"] = padding; + keywords["dilations"] = dilations; + keywords["padding_list"] = padding_list; + var _op = tf.OpDefLib._apply_op_helper("QuantizedDepthwiseConv2DWithBiasAndReluAndRequantize", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "Tinput", _op._get_attr_type("Tinput"), "Tfilter", _op._get_attr_type("Tfilter"), "Tbias", _op._get_attr_type("Tbias"), "out_type", _op._get_attr_type("out_type"), "strides", _op.get_attr("strides"), "padding", _op.get_attr("padding"), "dilations", _op.get_attr("dilations"), "padding_list", _op.get_attr("padding_list") }; + _execute.record_gradient("QuantizedDepthwiseConv2DWithBiasAndReluAndRequantize", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] quantized_depthwise_conv2d_with_bias_and_relu_and_requantize_eager_fallback(Tensor input, Tensor filter, Tensor bias, Tensor min_input, Tensor max_input, Tensor min_filter, Tensor max_filter, Tensor min_freezed_output, Tensor max_freezed_output, TF_DataType out_type, int[] strides, string padding, int[] dilations, int[] padding_list, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, filter, bias, min_input, max_input, min_filter, max_filter, min_freezed_output, max_freezed_output }; + object[] _attrs = new object[] { "Tinput", input.dtype, "Tfilter", filter.dtype, "Tbias", bias.dtype, "out_type", out_type, "strides", strides, "padding", padding, "dilations", dilations, "padding_list", padding_list }; + var _result = _execute.execute("QuantizedDepthwiseConv2DWithBiasAndReluAndRequantize", 3, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("QuantizedDepthwiseConv2DWithBiasAndReluAndRequantize", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// ~~%~~Performs a quantized matrix multiplication of `a` by the matrix `b` with bias~~%~~add.~~%~~ + /// + /// + /// + /// The inputs must be two-dimensional matrices and 1D bias vector. And the inner + /// dimension of `a` (after being transposed if `transpose_a` is non-zero) must + /// match the outer dimension of `b` (after being transposed if `transposed_b` is + /// non-zero). Then do broadcast add operation with bias values on the matrix + /// multiplication result. The bias size must match inner dimension of `b`. + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// If true, `a` is transposed before multiplication. + /// + /// + /// If true, `b` is transposed before multiplication. + /// + /// + /// + /// Input data quantization mode. Either MIN_FIRST(default) or SCALED. + /// + /// + /// + public static Tensor[] quantized_mat_mul_with_bias(Tensor a, Tensor b, Tensor bias, Tensor min_a, Tensor max_a, Tensor min_b, Tensor max_b, TF_DataType Toutput = TF_DataType.TF_QINT32, bool transpose_a = false, bool transpose_b = false, string input_quant_mode = "MIN_FIRST", string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "QuantizedMatMulWithBias", name) { args = new object[] { a, b, bias, min_a, max_a, min_b, max_b }, attrs = new Dictionary() { ["Toutput"] = Toutput, ["transpose_a"] = transpose_a, ["transpose_b"] = transpose_b, ["input_quant_mode"] = input_quant_mode } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return quantized_mat_mul_with_bias_eager_fallback(a, b, bias, min_a, max_a, min_b, max_b, Toutput: Toutput, transpose_a: transpose_a, transpose_b: transpose_b, input_quant_mode: input_quant_mode, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + if (input_quant_mode is null) + { + input_quant_mode = "MIN_FIRST"; + } + Dictionary keywords = new(); + keywords["a"] = a; + keywords["b"] = b; + keywords["bias"] = bias; + keywords["min_a"] = min_a; + keywords["max_a"] = max_a; + keywords["min_b"] = min_b; + keywords["max_b"] = max_b; + keywords["Toutput"] = Toutput; + keywords["transpose_a"] = transpose_a; + keywords["transpose_b"] = transpose_b; + keywords["input_quant_mode"] = input_quant_mode; + var _op = tf.OpDefLib._apply_op_helper("QuantizedMatMulWithBias", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T1", _op._get_attr_type("T1"), "T2", _op._get_attr_type("T2"), "Tbias", _op._get_attr_type("Tbias"), "Toutput", _op._get_attr_type("Toutput"), "transpose_a", _op._get_attr_bool("transpose_a"), "transpose_b", _op._get_attr_bool("transpose_b"), "input_quant_mode", _op.get_attr("input_quant_mode") }; + _execute.record_gradient("QuantizedMatMulWithBias", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] quantized_mat_mul_with_bias_eager_fallback(Tensor a, Tensor b, Tensor bias, Tensor min_a, Tensor max_a, Tensor min_b, Tensor max_b, TF_DataType Toutput, bool transpose_a, bool transpose_b, string input_quant_mode, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { a, b, bias, min_a, max_a, min_b, max_b }; + object[] _attrs = new object[] { "T1", a.dtype, "T2", b.dtype, "Tbias", bias.dtype, "Toutput", Toutput, "transpose_a", transpose_a, "transpose_b", transpose_b, "input_quant_mode", input_quant_mode }; + var _result = _execute.execute("QuantizedMatMulWithBias", 3, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("QuantizedMatMulWithBias", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + public static Tensor quantized_mat_mul_with_bias_and_dequantize(Tensor a, Tensor b, Tensor bias, Tensor min_a, Tensor max_a, Tensor min_b, Tensor max_b, Tensor min_freezed_output, Tensor max_freezed_output, TF_DataType Toutput, bool transpose_a = false, bool transpose_b = false, string input_quant_mode = "MIN_FIRST", string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "QuantizedMatMulWithBiasAndDequantize", name) { args = new object[] { a, b, bias, min_a, max_a, min_b, max_b, min_freezed_output, max_freezed_output }, attrs = new Dictionary() { ["Toutput"] = Toutput, ["transpose_a"] = transpose_a, ["transpose_b"] = transpose_b, ["input_quant_mode"] = input_quant_mode } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return quantized_mat_mul_with_bias_and_dequantize_eager_fallback(a, b, bias, min_a, max_a, min_b, max_b, min_freezed_output, max_freezed_output, Toutput: Toutput, transpose_a: transpose_a, transpose_b: transpose_b, input_quant_mode: input_quant_mode, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + if (input_quant_mode is null) + { + input_quant_mode = "MIN_FIRST"; + } + Dictionary keywords = new(); + keywords["a"] = a; + keywords["b"] = b; + keywords["bias"] = bias; + keywords["min_a"] = min_a; + keywords["max_a"] = max_a; + keywords["min_b"] = min_b; + keywords["max_b"] = max_b; + keywords["min_freezed_output"] = min_freezed_output; + keywords["max_freezed_output"] = max_freezed_output; + keywords["Toutput"] = Toutput; + keywords["transpose_a"] = transpose_a; + keywords["transpose_b"] = transpose_b; + keywords["input_quant_mode"] = input_quant_mode; + var _op = tf.OpDefLib._apply_op_helper("QuantizedMatMulWithBiasAndDequantize", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T1", _op._get_attr_type("T1"), "T2", _op._get_attr_type("T2"), "Tbias", _op._get_attr_type("Tbias"), "Toutput", _op._get_attr_type("Toutput"), "transpose_a", _op._get_attr_bool("transpose_a"), "transpose_b", _op._get_attr_bool("transpose_b"), "input_quant_mode", _op.get_attr("input_quant_mode") }; + _execute.record_gradient("QuantizedMatMulWithBiasAndDequantize", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor quantized_mat_mul_with_bias_and_dequantize_eager_fallback(Tensor a, Tensor b, Tensor bias, Tensor min_a, Tensor max_a, Tensor min_b, Tensor max_b, Tensor min_freezed_output, Tensor max_freezed_output, TF_DataType Toutput, bool transpose_a, bool transpose_b, string input_quant_mode, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { a, b, bias, min_a, max_a, min_b, max_b, min_freezed_output, max_freezed_output }; + object[] _attrs = new object[] { "T1", a.dtype, "T2", b.dtype, "Tbias", bias.dtype, "Toutput", Toutput, "transpose_a", transpose_a, "transpose_b", transpose_b, "input_quant_mode", input_quant_mode }; + var _result = _execute.execute("QuantizedMatMulWithBiasAndDequantize", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("QuantizedMatMulWithBiasAndDequantize", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// ~~%~~Perform a quantized matrix multiplication of `a` by the matrix `b` with bias~~%~~add and relu fusion.~~%~~ + /// + /// + /// + /// The inputs must be two-dimensional matrices and 1D bias vector. And the inner + /// dimension of `a` (after being transposed if `transpose_a` is non-zero) must + /// match the outer dimension of `b` (after being transposed if `transposed_b` is + /// non-zero). Then do broadcast add operation with bias values on the matrix + /// multiplication result. The bias size must match inner dimension of `b`. Then do + /// relu activation to get non-negative result. + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// If true, `a` is transposed before multiplication. + /// + /// + /// If true, `b` is transposed before multiplication. + /// + /// + /// + /// Input data quantization mode. Either MIN_FIRST(default) or SCALED. + /// + /// + /// + public static Tensor[] quantized_mat_mul_with_bias_and_relu(Tensor a, Tensor b, Tensor bias, Tensor min_a, Tensor max_a, Tensor min_b, Tensor max_b, TF_DataType Toutput = TF_DataType.TF_QINT32, bool transpose_a = false, bool transpose_b = false, string input_quant_mode = "MIN_FIRST", string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "QuantizedMatMulWithBiasAndRelu", name) { args = new object[] { a, b, bias, min_a, max_a, min_b, max_b }, attrs = new Dictionary() { ["Toutput"] = Toutput, ["transpose_a"] = transpose_a, ["transpose_b"] = transpose_b, ["input_quant_mode"] = input_quant_mode } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return quantized_mat_mul_with_bias_and_relu_eager_fallback(a, b, bias, min_a, max_a, min_b, max_b, Toutput: Toutput, transpose_a: transpose_a, transpose_b: transpose_b, input_quant_mode: input_quant_mode, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + if (input_quant_mode is null) + { + input_quant_mode = "MIN_FIRST"; + } + Dictionary keywords = new(); + keywords["a"] = a; + keywords["b"] = b; + keywords["bias"] = bias; + keywords["min_a"] = min_a; + keywords["max_a"] = max_a; + keywords["min_b"] = min_b; + keywords["max_b"] = max_b; + keywords["Toutput"] = Toutput; + keywords["transpose_a"] = transpose_a; + keywords["transpose_b"] = transpose_b; + keywords["input_quant_mode"] = input_quant_mode; + var _op = tf.OpDefLib._apply_op_helper("QuantizedMatMulWithBiasAndRelu", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T1", _op._get_attr_type("T1"), "T2", _op._get_attr_type("T2"), "Toutput", _op._get_attr_type("Toutput"), "transpose_a", _op._get_attr_bool("transpose_a"), "transpose_b", _op._get_attr_bool("transpose_b"), "input_quant_mode", _op.get_attr("input_quant_mode") }; + _execute.record_gradient("QuantizedMatMulWithBiasAndRelu", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] quantized_mat_mul_with_bias_and_relu_eager_fallback(Tensor a, Tensor b, Tensor bias, Tensor min_a, Tensor max_a, Tensor min_b, Tensor max_b, TF_DataType Toutput, bool transpose_a, bool transpose_b, string input_quant_mode, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { a, b, bias, min_a, max_a, min_b, max_b }; + object[] _attrs = new object[] { "T1", a.dtype, "T2", b.dtype, "Toutput", Toutput, "transpose_a", transpose_a, "transpose_b", transpose_b, "input_quant_mode", input_quant_mode }; + var _result = _execute.execute("QuantizedMatMulWithBiasAndRelu", 3, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("QuantizedMatMulWithBiasAndRelu", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// ~~%~~Perform a quantized matrix multiplication of `a` by the matrix `b` with bias~~%~~add and relu and requantize fusion.~~%~~ + /// + /// + /// + /// The inputs must be two-dimensional matrices and 1D bias vector. And the inner + /// dimension of `a` (after being transposed if `transpose_a` is non-zero) must + /// match the outer dimension of `b` (after being transposed if `transposed_b` is + /// non-zero). Then do broadcast add operation with bias values on the matrix + /// multiplication result. The bias size must match inner dimension of `b`. Then do + /// relu activation to get non-negative result. Then do requantize operation to get + /// final uint8 result. + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// If true, `a` is transposed before multiplication. + /// + /// + /// If true, `b` is transposed before multiplication. + /// + /// + /// + /// Input data quantization mode. Either MIN_FIRST(default) or SCALED. + /// + /// + /// + public static Tensor[] quantized_mat_mul_with_bias_and_relu_and_requantize(Tensor a, Tensor b, Tensor bias, Tensor min_a, Tensor max_a, Tensor min_b, Tensor max_b, Tensor min_freezed_output, Tensor max_freezed_output, TF_DataType Toutput = TF_DataType.TF_QUINT8, bool transpose_a = false, bool transpose_b = false, string input_quant_mode = "MIN_FIRST", string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "QuantizedMatMulWithBiasAndReluAndRequantize", name) { args = new object[] { a, b, bias, min_a, max_a, min_b, max_b, min_freezed_output, max_freezed_output }, attrs = new Dictionary() { ["Toutput"] = Toutput, ["transpose_a"] = transpose_a, ["transpose_b"] = transpose_b, ["input_quant_mode"] = input_quant_mode } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return quantized_mat_mul_with_bias_and_relu_and_requantize_eager_fallback(a, b, bias, min_a, max_a, min_b, max_b, min_freezed_output, max_freezed_output, Toutput: Toutput, transpose_a: transpose_a, transpose_b: transpose_b, input_quant_mode: input_quant_mode, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + if (input_quant_mode is null) + { + input_quant_mode = "MIN_FIRST"; + } + Dictionary keywords = new(); + keywords["a"] = a; + keywords["b"] = b; + keywords["bias"] = bias; + keywords["min_a"] = min_a; + keywords["max_a"] = max_a; + keywords["min_b"] = min_b; + keywords["max_b"] = max_b; + keywords["min_freezed_output"] = min_freezed_output; + keywords["max_freezed_output"] = max_freezed_output; + keywords["Toutput"] = Toutput; + keywords["transpose_a"] = transpose_a; + keywords["transpose_b"] = transpose_b; + keywords["input_quant_mode"] = input_quant_mode; + var _op = tf.OpDefLib._apply_op_helper("QuantizedMatMulWithBiasAndReluAndRequantize", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T1", _op._get_attr_type("T1"), "T2", _op._get_attr_type("T2"), "Tbias", _op._get_attr_type("Tbias"), "Toutput", _op._get_attr_type("Toutput"), "transpose_a", _op._get_attr_bool("transpose_a"), "transpose_b", _op._get_attr_bool("transpose_b"), "input_quant_mode", _op.get_attr("input_quant_mode") }; + _execute.record_gradient("QuantizedMatMulWithBiasAndReluAndRequantize", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] quantized_mat_mul_with_bias_and_relu_and_requantize_eager_fallback(Tensor a, Tensor b, Tensor bias, Tensor min_a, Tensor max_a, Tensor min_b, Tensor max_b, Tensor min_freezed_output, Tensor max_freezed_output, TF_DataType Toutput, bool transpose_a, bool transpose_b, string input_quant_mode, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { a, b, bias, min_a, max_a, min_b, max_b, min_freezed_output, max_freezed_output }; + object[] _attrs = new object[] { "T1", a.dtype, "T2", b.dtype, "Tbias", bias.dtype, "Toutput", Toutput, "transpose_a", transpose_a, "transpose_b", transpose_b, "input_quant_mode", input_quant_mode }; + var _result = _execute.execute("QuantizedMatMulWithBiasAndReluAndRequantize", 3, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("QuantizedMatMulWithBiasAndReluAndRequantize", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + public static Tensor[] quantized_mat_mul_with_bias_and_requantize(Tensor a, Tensor b, Tensor bias, Tensor min_a, Tensor max_a, Tensor min_b, Tensor max_b, Tensor min_freezed_output, Tensor max_freezed_output, TF_DataType Toutput = TF_DataType.TF_QUINT8, bool transpose_a = false, bool transpose_b = false, string input_quant_mode = "MIN_FIRST", string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "QuantizedMatMulWithBiasAndRequantize", name) { args = new object[] { a, b, bias, min_a, max_a, min_b, max_b, min_freezed_output, max_freezed_output }, attrs = new Dictionary() { ["Toutput"] = Toutput, ["transpose_a"] = transpose_a, ["transpose_b"] = transpose_b, ["input_quant_mode"] = input_quant_mode } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return quantized_mat_mul_with_bias_and_requantize_eager_fallback(a, b, bias, min_a, max_a, min_b, max_b, min_freezed_output, max_freezed_output, Toutput: Toutput, transpose_a: transpose_a, transpose_b: transpose_b, input_quant_mode: input_quant_mode, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + if (input_quant_mode is null) + { + input_quant_mode = "MIN_FIRST"; + } + Dictionary keywords = new(); + keywords["a"] = a; + keywords["b"] = b; + keywords["bias"] = bias; + keywords["min_a"] = min_a; + keywords["max_a"] = max_a; + keywords["min_b"] = min_b; + keywords["max_b"] = max_b; + keywords["min_freezed_output"] = min_freezed_output; + keywords["max_freezed_output"] = max_freezed_output; + keywords["Toutput"] = Toutput; + keywords["transpose_a"] = transpose_a; + keywords["transpose_b"] = transpose_b; + keywords["input_quant_mode"] = input_quant_mode; + var _op = tf.OpDefLib._apply_op_helper("QuantizedMatMulWithBiasAndRequantize", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T1", _op._get_attr_type("T1"), "T2", _op._get_attr_type("T2"), "Tbias", _op._get_attr_type("Tbias"), "Toutput", _op._get_attr_type("Toutput"), "transpose_a", _op._get_attr_bool("transpose_a"), "transpose_b", _op._get_attr_bool("transpose_b"), "input_quant_mode", _op.get_attr("input_quant_mode") }; + _execute.record_gradient("QuantizedMatMulWithBiasAndRequantize", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] quantized_mat_mul_with_bias_and_requantize_eager_fallback(Tensor a, Tensor b, Tensor bias, Tensor min_a, Tensor max_a, Tensor min_b, Tensor max_b, Tensor min_freezed_output, Tensor max_freezed_output, TF_DataType Toutput, bool transpose_a, bool transpose_b, string input_quant_mode, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { a, b, bias, min_a, max_a, min_b, max_b, min_freezed_output, max_freezed_output }; + object[] _attrs = new object[] { "T1", a.dtype, "T2", b.dtype, "Tbias", bias.dtype, "Toutput", Toutput, "transpose_a", transpose_a, "transpose_b", transpose_b, "input_quant_mode", input_quant_mode }; + var _result = _execute.execute("QuantizedMatMulWithBiasAndRequantize", 3, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("QuantizedMatMulWithBiasAndRequantize", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// Produces the max pool of the input tensor for quantized types. + /// + /// + /// + /// + /// + /// + /// The size of the window for each dimension of the input tensor. + /// The length must be 4 to match the number of dimensions of the input. + /// + /// + /// + /// + /// The stride of the sliding window for each dimension of the input + /// tensor. The length must be 4 to match the number of dimensions of the input. + /// + /// + /// + /// + /// The type of padding algorithm to use. + /// + /// + /// + public static Tensor[] quantized_max_pool(Tensor input, Tensor min_input, Tensor max_input, int[] ksize, int[] strides, string padding, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "QuantizedMaxPool", name) { args = new object[] { input, min_input, max_input }, attrs = new Dictionary() { ["ksize"] = ksize, ["strides"] = strides, ["padding"] = padding } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return quantized_max_pool_eager_fallback(input, min_input, max_input, ksize: ksize, strides: strides, padding: padding, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["min_input"] = min_input; + keywords["max_input"] = max_input; + keywords["ksize"] = ksize; + keywords["strides"] = strides; + keywords["padding"] = padding; + var _op = tf.OpDefLib._apply_op_helper("QuantizedMaxPool", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "ksize", _op.get_attr("ksize"), "strides", _op.get_attr("strides"), "padding", _op.get_attr("padding") }; + _execute.record_gradient("QuantizedMaxPool", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] quantized_max_pool_eager_fallback(Tensor input, Tensor min_input, Tensor max_input, int[] ksize, int[] strides, string padding, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, min_input, max_input }; + object[] _attrs = new object[] { "T", input.dtype, "ksize", ksize, "strides", strides, "padding", padding }; + var _result = _execute.execute("QuantizedMaxPool", 3, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("QuantizedMaxPool", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// Computes Quantized Rectified Linear: `max(features, 0)` + /// + /// + /// + /// + /// + /// + public static Tensor[] quantized_relu(Tensor features, Tensor min_features, Tensor max_features, TF_DataType out_type = TF_DataType.TF_QUINT8, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "QuantizedRelu", name) { args = new object[] { features, min_features, max_features }, attrs = new Dictionary() { ["out_type"] = out_type } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return quantized_relu_eager_fallback(features, min_features, max_features, out_type: out_type, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["features"] = features; + keywords["min_features"] = min_features; + keywords["max_features"] = max_features; + keywords["out_type"] = out_type; + var _op = tf.OpDefLib._apply_op_helper("QuantizedRelu", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "Tinput", _op._get_attr_type("Tinput"), "out_type", _op._get_attr_type("out_type") }; + _execute.record_gradient("QuantizedRelu", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] quantized_relu_eager_fallback(Tensor features, Tensor min_features, Tensor max_features, TF_DataType out_type, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { features, min_features, max_features }; + object[] _attrs = new object[] { "Tinput", features.dtype, "out_type", out_type }; + var _result = _execute.execute("QuantizedRelu", 3, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("QuantizedRelu", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// Computes Quantized Rectified Linear 6: `min(max(features, 0), 6)` + /// + /// + /// + /// + /// + /// + public static Tensor[] quantized_relu6(Tensor features, Tensor min_features, Tensor max_features, TF_DataType out_type = TF_DataType.TF_QUINT8, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "QuantizedRelu6", name) { args = new object[] { features, min_features, max_features }, attrs = new Dictionary() { ["out_type"] = out_type } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return quantized_relu6_eager_fallback(features, min_features, max_features, out_type: out_type, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["features"] = features; + keywords["min_features"] = min_features; + keywords["max_features"] = max_features; + keywords["out_type"] = out_type; + var _op = tf.OpDefLib._apply_op_helper("QuantizedRelu6", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "Tinput", _op._get_attr_type("Tinput"), "out_type", _op._get_attr_type("out_type") }; + _execute.record_gradient("QuantizedRelu6", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] quantized_relu6_eager_fallback(Tensor features, Tensor min_features, Tensor max_features, TF_DataType out_type, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { features, min_features, max_features }; + object[] _attrs = new object[] { "Tinput", features.dtype, "out_type", out_type }; + var _result = _execute.execute("QuantizedRelu6", 3, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("QuantizedRelu6", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// Computes Quantized Rectified Linear X: `min(max(features, 0), max_value)` + /// + /// + /// + /// + /// + /// + /// + public static Tensor[] quantized_relu_x(Tensor features, Tensor max_value, Tensor min_features, Tensor max_features, TF_DataType out_type = TF_DataType.TF_QUINT8, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "QuantizedReluX", name) { args = new object[] { features, max_value, min_features, max_features }, attrs = new Dictionary() { ["out_type"] = out_type } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return quantized_relu_x_eager_fallback(features, max_value, min_features, max_features, out_type: out_type, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["features"] = features; + keywords["max_value"] = max_value; + keywords["min_features"] = min_features; + keywords["max_features"] = max_features; + keywords["out_type"] = out_type; + var _op = tf.OpDefLib._apply_op_helper("QuantizedReluX", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "Tinput", _op._get_attr_type("Tinput"), "out_type", _op._get_attr_type("out_type") }; + _execute.record_gradient("QuantizedReluX", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] quantized_relu_x_eager_fallback(Tensor features, Tensor max_value, Tensor min_features, Tensor max_features, TF_DataType out_type, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { features, max_value, min_features, max_features }; + object[] _attrs = new object[] { "Tinput", features.dtype, "out_type", out_type }; + var _result = _execute.execute("QuantizedReluX", 3, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("QuantizedReluX", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// Computes rectified linear: `max(features, 0)`. + /// + /// + /// + /// See: https://en.wikipedia.org/wiki/Rectifier_(neural_networks) + /// Example usage: + /// >>> tf.nn.relu([-2., 0., 3.]).numpy() + /// array([0., 0., 3.], dtype=float32) + /// + /// + /// + /// + public static Tensor relu(Tensor features, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Relu", name) { args = new object[] { features }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return relu_eager_fallback(features, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["features"] = features; + var _op = tf.OpDefLib._apply_op_helper("Relu", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Relu", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor relu_eager_fallback(Tensor features, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { features }; + object[] _attrs = new object[] { "T", features.dtype }; + var _result = _execute.execute("Relu", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Relu", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes rectified linear 6: `min(max(features, 0), 6)`. + /// + /// + /// + public static Tensor relu6(Tensor features, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Relu6", name) { args = new object[] { features }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return relu6_eager_fallback(features, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["features"] = features; + var _op = tf.OpDefLib._apply_op_helper("Relu6", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Relu6", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor relu6_eager_fallback(Tensor features, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { features }; + object[] _attrs = new object[] { "T", features.dtype }; + var _result = _execute.execute("Relu6", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Relu6", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes rectified linear gradients for a Relu operation. + /// + /// + /// + /// + public static Tensor relu_grad(Tensor gradients, Tensor features, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "ReluGrad", name) { args = new object[] { gradients, features }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return relu_grad_eager_fallback(gradients, features, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["gradients"] = gradients; + keywords["features"] = features; + var _op = tf.OpDefLib._apply_op_helper("ReluGrad", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("ReluGrad", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor relu_grad_eager_fallback(Tensor gradients, Tensor features, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { gradients, features }; + object[] _attrs = new object[] { "T", gradients.dtype }; + var _result = _execute.execute("ReluGrad", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("ReluGrad", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes scaled exponential linear: `scale * alpha * (exp(features) - 1)` + /// + /// + /// + /// if < 0, `scale * features` otherwise. + /// + /// To be used together with + /// `initializer = tf.variance_scaling_initializer(factor=1.0, mode='FAN_IN')`. + /// For correct dropout, use `tf.contrib.nn.alpha_dropout`. + /// + /// See [Self-Normalizing Neural Networks](https://arxiv.org/abs/1706.02515) + /// + /// + /// + /// + public static Tensor selu(Tensor features, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Selu", name) { args = new object[] { features }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return selu_eager_fallback(features, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["features"] = features; + var _op = tf.OpDefLib._apply_op_helper("Selu", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Selu", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor selu_eager_fallback(Tensor features, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { features }; + object[] _attrs = new object[] { "T", features.dtype }; + var _result = _execute.execute("Selu", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Selu", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes gradients for the scaled exponential linear (Selu) operation. + /// + /// + /// + /// + public static Tensor selu_grad(Tensor gradients, Tensor outputs, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "SeluGrad", name) { args = new object[] { gradients, outputs }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return selu_grad_eager_fallback(gradients, outputs, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["gradients"] = gradients; + keywords["outputs"] = outputs; + var _op = tf.OpDefLib._apply_op_helper("SeluGrad", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("SeluGrad", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor selu_grad_eager_fallback(Tensor gradients, Tensor outputs, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { gradients, outputs }; + object[] _attrs = new object[] { "T", gradients.dtype }; + var _result = _execute.execute("SeluGrad", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("SeluGrad", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes softmax activations. + /// + /// + /// + /// For each batch `i` and class `j` we have + /// + /// $$softmax[i, j] = exp(logits[i, j]) / sum_j(exp(logits[i, j]))$$ + /// + /// + /// + /// + public static Tensor softmax(Tensor logits, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Softmax", name) { args = new object[] { logits }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return softmax_eager_fallback(logits, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["logits"] = logits; + var _op = tf.OpDefLib._apply_op_helper("Softmax", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Softmax", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor softmax_eager_fallback(Tensor logits, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { logits }; + object[] _attrs = new object[] { "T", logits.dtype }; + var _result = _execute.execute("Softmax", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Softmax", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes softmax cross entropy cost and gradients to backpropagate. + /// + /// + /// + /// Inputs are the logits, not probabilities. + /// + /// + /// + /// + /// + public static Tensor[] softmax_cross_entropy_with_logits(Tensor features, Tensor labels, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "SoftmaxCrossEntropyWithLogits", name) { args = new object[] { features, labels }, attrs = new Dictionary() { } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return softmax_cross_entropy_with_logits_eager_fallback(features, labels, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["features"] = features; + keywords["labels"] = labels; + var _op = tf.OpDefLib._apply_op_helper("SoftmaxCrossEntropyWithLogits", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("SoftmaxCrossEntropyWithLogits", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] softmax_cross_entropy_with_logits_eager_fallback(Tensor features, Tensor labels, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { features, labels }; + object[] _attrs = new object[] { "T", features.dtype }; + var _result = _execute.execute("SoftmaxCrossEntropyWithLogits", 2, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("SoftmaxCrossEntropyWithLogits", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// + /// + /// + /// + public static Tensor softplus(Tensor features, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Softplus", name) { args = new object[] { features }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return softplus_eager_fallback(features, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["features"] = features; + var _op = tf.OpDefLib._apply_op_helper("Softplus", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Softplus", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor softplus_eager_fallback(Tensor features, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { features }; + object[] _attrs = new object[] { "T", features.dtype }; + var _result = _execute.execute("Softplus", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Softplus", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes softplus gradients for a softplus operation. + /// + /// + /// + /// + public static Tensor softplus_grad(Tensor gradients, Tensor features, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "SoftplusGrad", name) { args = new object[] { gradients, features }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return softplus_grad_eager_fallback(gradients, features, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["gradients"] = gradients; + keywords["features"] = features; + var _op = tf.OpDefLib._apply_op_helper("SoftplusGrad", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("SoftplusGrad", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor softplus_grad_eager_fallback(Tensor gradients, Tensor features, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { gradients, features }; + object[] _attrs = new object[] { "T", gradients.dtype }; + var _result = _execute.execute("SoftplusGrad", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("SoftplusGrad", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes softsign: `features / (abs(features) + 1)`. + /// + /// + /// + public static Tensor softsign(Tensor features, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "Softsign", name) { args = new object[] { features }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return softsign_eager_fallback(features, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["features"] = features; + var _op = tf.OpDefLib._apply_op_helper("Softsign", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("Softsign", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor softsign_eager_fallback(Tensor features, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { features }; + object[] _attrs = new object[] { "T", features.dtype }; + var _result = _execute.execute("Softsign", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("Softsign", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes softsign gradients for a softsign operation. + /// + /// + /// + /// + public static Tensor softsign_grad(Tensor gradients, Tensor features, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "SoftsignGrad", name) { args = new object[] { gradients, features }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return softsign_grad_eager_fallback(gradients, features, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["gradients"] = gradients; + keywords["features"] = features; + var _op = tf.OpDefLib._apply_op_helper("SoftsignGrad", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T") }; + _execute.record_gradient("SoftsignGrad", _op.inputs, _attrs, _result); + } + return _result[0]; + } + + public static Tensor softsign_grad_eager_fallback(Tensor gradients, Tensor features, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { gradients, features }; + object[] _attrs = new object[] { "T", gradients.dtype }; + var _result = _execute.execute("SoftsignGrad", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("SoftsignGrad", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Computes softmax cross entropy cost and gradients to backpropagate. + /// + /// + /// + /// Unlike `SoftmaxCrossEntropyWithLogits`, this operation does not accept + /// a matrix of label probabilities, but rather a single label per row + /// of features. This label is considered to have probability 1.0 for the + /// given row. + /// + /// Inputs are the logits, not probabilities. + /// + /// + /// + /// + /// + public static Tensor[] sparse_softmax_cross_entropy_with_logits(Tensor features, Tensor labels, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "SparseSoftmaxCrossEntropyWithLogits", name) { args = new object[] { features, labels }, attrs = new Dictionary() { } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return sparse_softmax_cross_entropy_with_logits_eager_fallback(features, labels, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["features"] = features; + keywords["labels"] = labels; + var _op = tf.OpDefLib._apply_op_helper("SparseSoftmaxCrossEntropyWithLogits", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "T", _op._get_attr_type("T"), "Tlabels", _op._get_attr_type("Tlabels") }; + _execute.record_gradient("SparseSoftmaxCrossEntropyWithLogits", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] sparse_softmax_cross_entropy_with_logits_eager_fallback(Tensor features, Tensor labels, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { features, labels }; + object[] _attrs = new object[] { "T", features.dtype, "Tlabels", labels.dtype }; + var _result = _execute.execute("SparseSoftmaxCrossEntropyWithLogits", 2, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("SparseSoftmaxCrossEntropyWithLogits", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// Finds values and indices of the `k` largest elements for the last dimension. + /// + /// + /// + /// If the input is a vector (rank-1), finds the `k` largest entries in the vector + /// and outputs their values and indices as vectors. Thus `values[j]` is the + /// `j`-th largest entry in `input`, and its index is `indices[j]`. + /// + /// For matrices (resp. higher rank input), computes the top `k` entries in each + /// row (resp. vector along the last dimension). Thus, + /// + /// values.shape = indices.shape = input.shape[:-1] + [k] + /// + /// If two elements are equal, the lower-index element appears first. + /// + /// If `k` varies dynamically, use `TopKV2` below. + /// + /// + /// + /// + /// + /// Number of top elements to look for along the last dimension (along each + /// row for matrices). + /// + /// + /// + /// + /// If true the resulting `k` elements will be sorted by the values in + /// descending order. + /// + /// + /// + public static Tensor[] top_k(Tensor input, int k = 0, bool sorted = true, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "TopK", name) { args = new object[] { input }, attrs = new Dictionary() { ["k"] = k, ["sorted"] = sorted } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return top_k_eager_fallback(input, k: k, sorted: sorted, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["k"] = k; + keywords["sorted"] = sorted; + var _op = tf.OpDefLib._apply_op_helper("TopK", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "k", _op._get_attr_int("k"), "sorted", _op._get_attr_bool("sorted"), "T", _op._get_attr_type("T") }; + _execute.record_gradient("TopK", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] top_k_eager_fallback(Tensor input, int k, bool sorted, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input }; + object[] _attrs = new object[] { "k", k, "sorted", sorted, "T", input.dtype }; + var _result = _execute.execute("TopK", 2, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("TopK", _inputs_flat, _attrs, _result); + } + return _result; + } + /// + /// Finds values and indices of the `k` largest elements for the last dimension. + /// + /// + /// + /// If the input is a vector (rank-1), finds the `k` largest entries in the vector + /// and outputs their values and indices as vectors. Thus `values[j]` is the + /// `j`-th largest entry in `input`, and its index is `indices[j]`. + /// + /// For matrices (resp. higher rank input), computes the top `k` entries in each + /// row (resp. vector along the last dimension). Thus, + /// + /// values.shape = indices.shape = input.shape[:-1] + [k] + /// + /// If two elements are equal, the lower-index element appears first. + /// + /// + /// + /// + /// + /// + /// If true the resulting `k` elements will be sorted by the values in + /// descending order. + /// + /// + /// + public static Tensor[] top_kv2(Tensor input, Tensor k, bool sorted = true, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "TopKV2", name) { args = new object[] { input, k }, attrs = new Dictionary() { ["sorted"] = sorted } }); + return _fast_path_result; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return top_kv2_eager_fallback(input, k, sorted: sorted, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["k"] = k; + keywords["sorted"] = sorted; + var _op = tf.OpDefLib._apply_op_helper("TopKV2", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "sorted", _op._get_attr_bool("sorted"), "T", _op._get_attr_type("T") }; + _execute.record_gradient("TopKV2", _op.inputs, _attrs, _result); + } + return _result; + } + + public static Tensor[] top_kv2_eager_fallback(Tensor input, Tensor k, bool sorted, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input, k }; + object[] _attrs = new object[] { "sorted", sorted, "T", input.dtype }; + var _result = _execute.execute("TopKV2", 2, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("TopKV2", _inputs_flat, _attrs, _result); + } + return _result; + } +} diff --git a/src/TensorFlowNET.Core/Operations/gen_ops.cs b/src/TensorFlowNET.Core/Operations/gen_ops.cs index fe67c2b84..5fa4c97dd 100644 --- a/src/TensorFlowNET.Core/Operations/gen_ops.cs +++ b/src/TensorFlowNET.Core/Operations/gen_ops.cs @@ -10055,7 +10055,7 @@ public static Tensor ensure_shape(Tensor input, Shape shape, string name = "Ensu { try { - var _result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo("EnsureShape", name, input, shape)); + var _result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(tf.Context, "EnsureShape", name, input, shape)); return _result[0]; } catch (Exception) @@ -10076,7 +10076,7 @@ public static Tensor ensure_shape(Tensor input, Shape shape, string name = "Ensu dict["input"] = input; dict["shape"] = shape; var op = tf.OpDefLib._apply_op_helper("EnsureShape", name: name, keywords: dict); - if (execute.must_record_gradient()) + if (_execute.must_record_gradient()) { throw new NotImplementedException(); } @@ -10086,9 +10086,9 @@ public static Tensor ensure_shape(Tensor input, Shape shape, string name = "Ensu public static Tensor ensure_shape_eager_fallback(Tensor input, Shape shape, string name, Context ctx) { object[] attrs = new object[4] { "shape", shape, "T", input.dtype.as_datatype_enum() }; - var _result = execute.executes("EnsureShape", 1, new Tensor[] { input }, + var _result = _execute.execute("EnsureShape", 1, new Tensor[] { input }, attrs, ctx, name); - if (execute.must_record_gradient()) + if (_execute.must_record_gradient()) { throw new NotImplementedException(); } @@ -17194,7 +17194,7 @@ public static Operation merge_v2_checkpoints(Tensor[] checkpoint_prefixes, Tenso var ctx = tf.Context; if (ctx.executing_eagerly()) { - var result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo("MergeV2Checkpoints", name, + var result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(tf.Context, "MergeV2Checkpoints", name, checkpoint_prefixes, destination_prefix, "delete_old_dirs", delete_old_dirs, "allow_missing_files", allow_missing_files)); result = null; return null; @@ -24297,7 +24297,7 @@ public static Tensor regex_full_match(Tensor input, Tensor pattern, string name var ctx = tf.Context; if (ctx.executing_eagerly()) { - var result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo("RegexFullMatch", name, input, pattern)); + var result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(tf.Context, "RegexFullMatch", name, input, pattern)); return result[0]; } var dict = new Dictionary(); @@ -27201,7 +27201,7 @@ public static Tensor[] restore_v2(Tensor prefix, string[] tensor_names, string[] Dictionary attrs = new(); attrs["dtypes"] = dtypes; var result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo( - "RestoreV2", name, prefix, tensor_names, shape_and_slices + tf.Context, "RestoreV2", name, prefix, tensor_names, shape_and_slices ) { attrs = attrs }); return result; @@ -27236,9 +27236,9 @@ public static Tensor[] restore_v2_eager_fallback(Tensor prefix, string[] tensor_ var shape_and_slices_tensor = ops.convert_to_tensor(shape_and_slices, TF_DataType.TF_STRING); object[] attrs = new object[] { "dtypes", dtypes }; Tensor[] inputs_flat = new Tensor[] { prefix, tensor_names_tensor, shape_and_slices_tensor }; - var result = execute.quick_execute("RestoreV2", dtypes.Length, inputs_flat, attrs, ctx, name); + var result = _execute.quick_execute("RestoreV2", dtypes.Length, inputs_flat, attrs, ctx, name); - if (execute.must_record_gradient()) + if (_execute.must_record_gradient()) { // TODO(Rinne); record the gradient } @@ -29829,7 +29829,7 @@ public static Tensor sharded_filename(Tensor basename, Tensor shard, Tensor num_ var ctx = tf.Context; if (ctx.executing_eagerly()) { - var result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo("ShardedFilename", name, basename, shard, num_shards)); + var result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(tf.Context, "ShardedFilename", name, basename, shard, num_shards)); return result[0]; } var dict = new Dictionary(); @@ -34759,7 +34759,7 @@ public static Tensor string_join(Tensor[] inputs, string separator = null, strin var ctx = tf.Context; if (ctx.executing_eagerly()) { - var result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo("StringJoin", name, inputs, "separator", separator)); + var result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(tf.Context, "StringJoin", name, inputs, "separator", separator)); return result[0]; } var dict = new Dictionary(); diff --git a/src/TensorFlowNET.Core/Operations/gen_resource_variable_ops.cs b/src/TensorFlowNET.Core/Operations/gen_resource_variable_ops.cs index 330903252..db5f6813c 100644 --- a/src/TensorFlowNET.Core/Operations/gen_resource_variable_ops.cs +++ b/src/TensorFlowNET.Core/Operations/gen_resource_variable_ops.cs @@ -1,158 +1,1523 @@ -/***************************************************************************** - Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -******************************************************************************/ +/*Wrappers around TensorFlow ops. This file is MACHINE GENERATED! Do not edit.*/ +using Tensorflow.Eager; +using Tensorflow.Contexts; +using Tensorflow.Exceptions; using static Tensorflow.Binding; -namespace Tensorflow +namespace Tensorflow; + +public static class gen_resource_variable_ops { - public static class gen_resource_variable_ops + /// + /// Adds a value to the current value of a variable. + /// + /// + /// + /// Any ReadVariableOp with a control dependency on this op is guaranteed to + /// see the incremented value or a subsequent newer one. + /// + /// + /// + /// + /// + public static Operation assign_add_variable_op(Tensor resource, Tensor value, string? name = null) { - public static Operation assign_sub_variable_op(Tensor resource, Tensor value, string name = null) + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) { - if (tf.Context.executing_eagerly()) + try { - tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo( - "AssignSubVariableOp", name, resource, value)); - + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "AssignAddVariableOp", name) { args = new object[] { resource, value }, attrs = new Dictionary() { } }); return null; } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return assign_add_variable_op_eager_fallback(resource, value, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["resource"] = resource; + keywords["value"] = value; + var _op = tf.OpDefLib._apply_op_helper("AssignAddVariableOp", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "dtype", _op._get_attr_type("dtype") }; + _execute.record_gradient("AssignAddVariableOp", _op.inputs, _attrs, _result); + } + return _op; + } - return null; + public static Operation assign_add_variable_op_eager_fallback(Tensor resource, Tensor value, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { resource, value }; + object[] _attrs = new object[] { "dtype", value.dtype }; + var _result = _execute.execute("AssignAddVariableOp", 0, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("AssignAddVariableOp", _inputs_flat, _attrs, _result); } + return null; + } + /// + /// Subtracts a value from the current value of a variable. + /// + /// + /// + /// Any ReadVariableOp with a control dependency on this op is guaranteed to + /// see the decremented value or a subsequent newer one. + /// + /// + /// + /// + /// + public static Operation assign_sub_variable_op(Tensor resource, Tensor value, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "AssignSubVariableOp", name) { args = new object[] { resource, value }, attrs = new Dictionary() { } }); + return null; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return assign_sub_variable_op_eager_fallback(resource, value, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["resource"] = resource; + keywords["value"] = value; + var _op = tf.OpDefLib._apply_op_helper("AssignSubVariableOp", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "dtype", _op._get_attr_type("dtype") }; + _execute.record_gradient("AssignSubVariableOp", _op.inputs, _attrs, _result); + } + return _op; + } - /// - /// Adds a value to the current value of a variable. - /// - /// - /// - /// - /// - public static Operation assign_add_variable_op(Tensor resource, Tensor value, string name = null) + public static Operation assign_sub_variable_op_eager_fallback(Tensor resource, Tensor value, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { resource, value }; + object[] _attrs = new object[] { "dtype", value.dtype }; + var _result = _execute.execute("AssignSubVariableOp", 0, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("AssignSubVariableOp", _inputs_flat, _attrs, _result); + } + return null; + } + /// + /// Assigns a new value to a variable. + /// + /// + /// + /// Any ReadVariableOp with a control dependency on this op is guaranteed to return + /// this value or a subsequent newer value of the variable. + /// + /// + /// + /// + /// + /// + public static Operation assign_variable_op(Tensor resource, Tensor value, bool validate_shape = false, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) { - if (tf.Context.executing_eagerly()) + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "AssignVariableOp", name) { args = new object[] { resource, value }, attrs = new Dictionary() { ["validate_shape"] = validate_shape } }); + return null; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) { - tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo("AssignAddVariableOp", name, - resource, value)); + } + try + { + return assign_variable_op_eager_fallback(resource, value, validate_shape: validate_shape, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["resource"] = resource; + keywords["value"] = value; + keywords["validate_shape"] = validate_shape; + var _op = tf.OpDefLib._apply_op_helper("AssignVariableOp", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "dtype", _op._get_attr_type("dtype"), "validate_shape", _op._get_attr_bool("validate_shape") }; + _execute.record_gradient("AssignVariableOp", _op.inputs, _attrs, _result); + } + return _op; + } + public static Operation assign_variable_op_eager_fallback(Tensor resource, Tensor value, bool validate_shape, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { resource, value }; + object[] _attrs = new object[] { "dtype", value.dtype, "validate_shape", validate_shape }; + var _result = _execute.execute("AssignVariableOp", 0, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("AssignVariableOp", _inputs_flat, _attrs, _result); + } + return null; + } + /// + /// This op consumes a lock created by `MutexLock`. + /// + /// + /// + /// This op exists to consume a tensor created by `MutexLock` (other than + /// direct control dependencies). It should be the only that consumes the tensor, + /// and will raise an error if it is not. Its only purpose is to keep the + /// mutex lock tensor alive until it is consumed by this op. + /// + /// **NOTE**: This operation must run on the same device as its input. This may + /// be enforced via the `colocate_with` mechanism. + /// + /// + /// + /// + public static Operation consume_mutex_lock(Tensor mutex_lock, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "ConsumeMutexLock", name) { args = new object[] { mutex_lock }, attrs = new Dictionary() { } }); return null; } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return consume_mutex_lock_eager_fallback(mutex_lock, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["mutex_lock"] = mutex_lock; + var _op = tf.OpDefLib._apply_op_helper("ConsumeMutexLock", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { }; + _execute.record_gradient("ConsumeMutexLock", _op.inputs, _attrs, _result); + } + return _op; + } - var _op = tf.OpDefLib._apply_op_helper("AssignAddVariableOp", name, new { resource, value }); + public static Operation consume_mutex_lock_eager_fallback(Tensor mutex_lock, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { mutex_lock }; + object[] _attrs = new object[] { }; + var _result = _execute.execute("ConsumeMutexLock", 0, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("ConsumeMutexLock", _inputs_flat, _attrs, _result); + } + return null; + } + /// + /// Deletes the resource specified by the handle. + /// + /// + /// + /// All subsequent operations using the resource will result in a NotFound + /// error status. + /// + /// + /// + /// + /// + /// whether to ignore the error when the resource + /// doesn't exist. + /// + /// + /// + public static Operation destroy_resource_op(Tensor resource, bool ignore_lookup_error = true, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "DestroyResourceOp", name) { args = new object[] { resource }, attrs = new Dictionary() { ["ignore_lookup_error"] = ignore_lookup_error } }); + return null; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return destroy_resource_op_eager_fallback(resource, ignore_lookup_error: ignore_lookup_error, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["resource"] = resource; + keywords["ignore_lookup_error"] = ignore_lookup_error; + var _op = tf.OpDefLib._apply_op_helper("DestroyResourceOp", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "ignore_lookup_error", _op._get_attr_bool("ignore_lookup_error") }; + _execute.record_gradient("DestroyResourceOp", _op.inputs, _attrs, _result); + } + return _op; + } - return _op; + public static Operation destroy_resource_op_eager_fallback(Tensor resource, bool ignore_lookup_error, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { resource }; + object[] _attrs = new object[] { "ignore_lookup_error", ignore_lookup_error }; + var _result = _execute.execute("DestroyResourceOp", 0, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("DestroyResourceOp", _inputs_flat, _attrs, _result); } + return null; + } + /// + /// Turns off the copy-on-read mode. + /// + /// + /// + /// Turns off the copy-on-read mode of a resource variable. If the variable is not in copy-on-read mode, this op has no effect. + /// + /// + /// + /// + public static Operation disable_copy_on_read(Tensor resource, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "DisableCopyOnRead", name) { args = new object[] { resource }, attrs = new Dictionary() { } }); + return null; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return disable_copy_on_read_eager_fallback(resource, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["resource"] = resource; + var _op = tf.OpDefLib._apply_op_helper("DisableCopyOnRead", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { }; + _execute.record_gradient("DisableCopyOnRead", _op.inputs, _attrs, _result); + } + return _op; + } - public static Operation assign_variable_op(Tensor resource, Tensor value, string name = null) + public static Operation disable_copy_on_read_eager_fallback(Tensor resource, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { resource }; + object[] _attrs = new object[] { }; + var _result = _execute.execute("DisableCopyOnRead", 0, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) { - if (tf.Context.executing_eagerly()) + _execute.record_gradient("DisableCopyOnRead", _inputs_flat, _attrs, _result); + } + return null; + } + /// + /// Locks a mutex resource. The output is the lock. So long as the lock tensor + /// + /// + /// + /// is alive, any other request to use `MutexLock` with this mutex will wait. + /// + /// This is particularly useful for creating a critical section when used in + /// conjunction with `MutexLockIdentity`: + /// + /// ```python + /// + /// mutex = mutex_v2( + /// shared_name=handle_name, container=container, name=name) + /// + /// def execute_in_critical_section(fn, *args, **kwargs): + /// lock = gen_resource_variable_ops.mutex_lock(mutex) + /// + /// with ops.control_dependencies([lock]): + /// r = fn(*args, **kwargs) + /// + /// with ops.control_dependencies(nest.flatten(r)): + /// with ops.colocate_with(mutex): + /// ensure_lock_exists = mutex_lock_identity(lock) + /// + /// # Make sure that if any element of r is accessed, all of + /// # them are executed together. + /// r = nest.map_structure(tf.identity, r) + /// + /// with ops.control_dependencies([ensure_lock_exists]): + /// return nest.map_structure(tf.identity, r) + /// ``` + /// + /// While `fn` is running in the critical section, no other functions which wish to + /// use this critical section may run. + /// + /// Often the use case is that two executions of the same graph, in parallel, + /// wish to run `fn`; and we wish to ensure that only one of them executes + /// at a time. This is especially important if `fn` modifies one or more + /// variables at a time. + /// + /// It is also useful if two separate functions must share a resource, but we + /// wish to ensure the usage is exclusive. + /// + /// + /// + /// + public static Tensor mutex_lock(Tensor mutex, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "MutexLock", name) { args = new object[] { mutex }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try { - tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo("AssignVariableOp", name, - resource, value)); + return mutex_lock_eager_fallback(mutex, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["mutex"] = mutex; + var _op = tf.OpDefLib._apply_op_helper("MutexLock", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { }; + _execute.record_gradient("MutexLock", _op.inputs, _attrs, _result); + } + return _result[0]; + } - return null; + public static Tensor mutex_lock_eager_fallback(Tensor mutex, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { mutex }; + object[] _attrs = new object[] { }; + var _result = _execute.execute("MutexLock", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("MutexLock", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Creates a Mutex resource that can be locked by `MutexLock`. + /// + /// + /// + /// If non-empty, this variable is placed in the given container. + /// Otherwise, a default container is used. + /// + /// + /// + /// + /// If non-empty, this variable is named in the given bucket + /// with this shared_name. Otherwise, the node name is used instead. + /// + /// + /// + public static Tensor mutex_v2(string container = "", string shared_name = "", string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "MutexV2", name) { args = new object[] { }, attrs = new Dictionary() { ["container"] = container, ["shared_name"] = shared_name } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { } + try + { + return mutex_v2_eager_fallback(container: container, shared_name: shared_name, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + if (container is null) + { + container = ""; + } + if (shared_name is null) + { + shared_name = ""; + } + Dictionary keywords = new(); + keywords["container"] = container; + keywords["shared_name"] = shared_name; + var _op = tf.OpDefLib._apply_op_helper("MutexV2", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "container", _op.get_attr("container"), "shared_name", _op.get_attr("shared_name") }; + _execute.record_gradient("MutexV2", _op.inputs, _attrs, _result); + } + return _result[0]; + } - var _op = tf.OpDefLib._apply_op_helper("AssignVariableOp", name, new { resource, value }); + public static Tensor mutex_v2_eager_fallback(string container, string shared_name, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { }; + object[] _attrs = new object[] { "container", container, "shared_name", shared_name }; + var _result = _execute.execute("MutexV2", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("MutexV2", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Reads the value of a variable. + /// + /// + /// + /// The tensor returned by this operation is immutable. + /// + /// The value returned by this operation is guaranteed to be influenced by all the + /// writes on which this operation depends directly or indirectly, and to not be + /// influenced by any of the writes which depend directly or indirectly on this + /// operation. + /// + /// + /// + /// + /// + /// the dtype of the value. + /// + /// + /// + public static Tensor read_variable_op(Tensor resource, TF_DataType dtype, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "ReadVariableOp", name) { args = new object[] { resource }, attrs = new Dictionary() { ["dtype"] = dtype } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return read_variable_op_eager_fallback(resource, dtype: dtype, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["resource"] = resource; + keywords["dtype"] = dtype; + var _op = tf.OpDefLib._apply_op_helper("ReadVariableOp", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "dtype", _op._get_attr_type("dtype") }; + _execute.record_gradient("ReadVariableOp", _op.inputs, _attrs, _result); + } + return _result[0]; + } - return _op; + public static Tensor read_variable_op_eager_fallback(Tensor resource, TF_DataType dtype, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { resource }; + object[] _attrs = new object[] { "dtype", dtype }; + var _result = _execute.execute("ReadVariableOp", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("ReadVariableOp", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Gather slices from the variable pointed to by `resource` according to `indices`. + /// + /// + /// + /// `indices` must be an integer tensor of any dimension (usually 0-D or 1-D). + /// Produces an output tensor with shape `indices.shape + params.shape[1:]` where: + /// + /// ```python + /// # Scalar indices + /// output[:, ..., :] = params[indices, :, ... :] + /// + /// # Vector indices + /// output[i, :, ..., :] = params[indices[i], :, ... :] + /// + /// # Higher rank indices + /// output[i, ..., j, :, ... :] = params[indices[i, ..., j], :, ..., :] + /// ``` + /// + /// + /// + /// + /// + /// + /// + /// + public static Tensor resource_gather(Tensor resource, Tensor indices, TF_DataType dtype, int batch_dims = 0, bool validate_indices = true, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "ResourceGather", name) { args = new object[] { resource, indices }, attrs = new Dictionary() { ["batch_dims"] = batch_dims, ["validate_indices"] = validate_indices, ["dtype"] = dtype } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return resource_gather_eager_fallback(resource, indices, batch_dims: batch_dims, validate_indices: validate_indices, dtype: dtype, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["resource"] = resource; + keywords["indices"] = indices; + keywords["batch_dims"] = batch_dims; + keywords["validate_indices"] = validate_indices; + keywords["dtype"] = dtype; + var _op = tf.OpDefLib._apply_op_helper("ResourceGather", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "batch_dims", _op._get_attr_int("batch_dims"), "validate_indices", _op._get_attr_bool("validate_indices"), "dtype", _op._get_attr_type("dtype"), "Tindices", _op._get_attr_type("Tindices") }; + _execute.record_gradient("ResourceGather", _op.inputs, _attrs, _result); } + return _result[0]; + } - public static Tensor var_is_initialized_op(Tensor resource, string name = null) + public static Tensor resource_gather_eager_fallback(Tensor resource, Tensor indices, int batch_dims, bool validate_indices, TF_DataType dtype, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { resource, indices }; + object[] _attrs = new object[] { "batch_dims", batch_dims, "validate_indices", validate_indices, "dtype", dtype, "Tindices", indices.dtype }; + var _result = _execute.execute("ResourceGather", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) { - if (tf.Context.executing_eagerly()) + _execute.record_gradient("ResourceGather", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// + /// + /// + /// + /// + /// + public static Tensor resource_gather_nd(Tensor resource, Tensor indices, TF_DataType dtype, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try { - var results = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo("VarIsInitializedOp", name, - resource)); + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "ResourceGatherNd", name) { args = new object[] { resource, indices }, attrs = new Dictionary() { ["dtype"] = dtype } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return resource_gather_nd_eager_fallback(resource, indices, dtype: dtype, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["resource"] = resource; + keywords["indices"] = indices; + keywords["dtype"] = dtype; + var _op = tf.OpDefLib._apply_op_helper("ResourceGatherNd", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "dtype", _op._get_attr_type("dtype"), "Tindices", _op._get_attr_type("Tindices") }; + _execute.record_gradient("ResourceGatherNd", _op.inputs, _attrs, _result); + } + return _result[0]; + } - return results[0]; + public static Tensor resource_gather_nd_eager_fallback(Tensor resource, Tensor indices, TF_DataType dtype, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { resource, indices }; + object[] _attrs = new object[] { "dtype", dtype, "Tindices", indices.dtype }; + var _result = _execute.execute("ResourceGatherNd", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("ResourceGatherNd", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Adds sparse updates to the variable referenced by `resource`. + /// + /// + /// + /// This operation computes + /// + /// # Scalar indices + /// ref[indices, ...] += updates[...] + /// + /// # Vector indices (for each i) + /// ref[indices[i], ...] += updates[i, ...] + /// + /// # High rank indices (for each i, ..., j) + /// ref[indices[i, ..., j], ...] += updates[i, ..., j, ...] + /// + /// Duplicate entries are handled correctly: if multiple `indices` reference + /// the same location, their contributions add. + /// + /// Requires `updates.shape = indices.shape + ref.shape[1:]` or `updates.shape = []`. + /// + ///
+ /// + ///
+ /// + ///
+ /// + /// + /// + /// + public static Operation resource_scatter_add(Tensor resource, Tensor indices, Tensor updates, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "ResourceScatterAdd", name) { args = new object[] { resource, indices, updates }, attrs = new Dictionary() { } }); + return null; + } + catch (NotOkStatusException ex) + { + throw ex; } + catch (Exception) + { + } + try + { + return resource_scatter_add_eager_fallback(resource, indices, updates, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["resource"] = resource; + keywords["indices"] = indices; + keywords["updates"] = updates; + var _op = tf.OpDefLib._apply_op_helper("ResourceScatterAdd", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "dtype", _op._get_attr_type("dtype"), "Tindices", _op._get_attr_type("Tindices") }; + _execute.record_gradient("ResourceScatterAdd", _op.inputs, _attrs, _result); + } + return _op; + } - var _op = tf.OpDefLib._apply_op_helper("VarIsInitializedOp", name, new { resource }); + public static Operation resource_scatter_add_eager_fallback(Tensor resource, Tensor indices, Tensor updates, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { resource, indices, updates }; + object[] _attrs = new object[] { "dtype", updates.dtype, "Tindices", indices.dtype }; + var _result = _execute.execute("ResourceScatterAdd", 0, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("ResourceScatterAdd", _inputs_flat, _attrs, _result); + } + return null; + } + /// + /// Divides sparse updates into the variable referenced by `resource`. + /// + /// + /// + /// This operation computes + /// + /// # Scalar indices + /// ref[indices, ...] /= updates[...] + /// + /// # Vector indices (for each i) + /// ref[indices[i], ...] /= updates[i, ...] + /// + /// # High rank indices (for each i, ..., j) + /// ref[indices[i, ..., j], ...] /= updates[i, ..., j, ...] + /// + /// Duplicate entries are handled correctly: if multiple `indices` reference + /// the same location, their contributions multiply. + /// + /// Requires `updates.shape = indices.shape + ref.shape[1:]` or `updates.shape = []`. + /// + ///
+ /// + ///
+ /// + ///
+ /// + /// + /// + /// + public static Operation resource_scatter_div(Tensor resource, Tensor indices, Tensor updates, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "ResourceScatterDiv", name) { args = new object[] { resource, indices, updates }, attrs = new Dictionary() { } }); + return null; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return resource_scatter_div_eager_fallback(resource, indices, updates, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["resource"] = resource; + keywords["indices"] = indices; + keywords["updates"] = updates; + var _op = tf.OpDefLib._apply_op_helper("ResourceScatterDiv", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "dtype", _op._get_attr_type("dtype"), "Tindices", _op._get_attr_type("Tindices") }; + _execute.record_gradient("ResourceScatterDiv", _op.inputs, _attrs, _result); + } + return _op; + } - return _op.output; + public static Operation resource_scatter_div_eager_fallback(Tensor resource, Tensor indices, Tensor updates, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { resource, indices, updates }; + object[] _attrs = new object[] { "dtype", updates.dtype, "Tindices", indices.dtype }; + var _result = _execute.execute("ResourceScatterDiv", 0, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("ResourceScatterDiv", _inputs_flat, _attrs, _result); } + return null; + } + /// + /// Reduces sparse updates into the variable referenced by `resource` using the `max` operation. + /// + /// + /// + /// This operation computes + /// + /// # Scalar indices + /// ref[indices, ...] = max(ref[indices, ...], updates[...]) + /// + /// # Vector indices (for each i) + /// ref[indices[i], ...] = max(ref[indices[i], ...], updates[i, ...]) + /// + /// # High rank indices (for each i, ..., j) + /// ref[indices[i, ..., j], ...] = max(ref[indices[i, ..., j], ...], updates[i, ..., j, ...]) + /// + /// Duplicate entries are handled correctly: if multiple `indices` reference + /// the same location, their contributions are combined. + /// + /// Requires `updates.shape = indices.shape + ref.shape[1:]` or `updates.shape = []`. + /// + ///
+ /// + ///
+ /// + ///
+ /// + /// + /// + /// + public static Operation resource_scatter_max(Tensor resource, Tensor indices, Tensor updates, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "ResourceScatterMax", name) { args = new object[] { resource, indices, updates }, attrs = new Dictionary() { } }); + return null; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return resource_scatter_max_eager_fallback(resource, indices, updates, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["resource"] = resource; + keywords["indices"] = indices; + keywords["updates"] = updates; + var _op = tf.OpDefLib._apply_op_helper("ResourceScatterMax", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "dtype", _op._get_attr_type("dtype"), "Tindices", _op._get_attr_type("Tindices") }; + _execute.record_gradient("ResourceScatterMax", _op.inputs, _attrs, _result); + } + return _op; + } - /// - /// Creates a handle to a Variable resource. - /// - /// - /// - /// - /// - /// - /// - public static Tensor var_handle_op(TF_DataType dtype, Shape shape, - string container = "", string shared_name = "", string name = null) + public static Operation resource_scatter_max_eager_fallback(Tensor resource, Tensor indices, Tensor updates, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { resource, indices, updates }; + object[] _attrs = new object[] { "dtype", updates.dtype, "Tindices", indices.dtype }; + var _result = _execute.execute("ResourceScatterMax", 0, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) { - if (tf.Context.executing_eagerly()) + _execute.record_gradient("ResourceScatterMax", _inputs_flat, _attrs, _result); + } + return null; + } + /// + /// Reduces sparse updates into the variable referenced by `resource` using the `min` operation. + /// + /// + /// + /// This operation computes + /// + /// # Scalar indices + /// ref[indices, ...] = min(ref[indices, ...], updates[...]) + /// + /// # Vector indices (for each i) + /// ref[indices[i], ...] = min(ref[indices[i], ...], updates[i, ...]) + /// + /// # High rank indices (for each i, ..., j) + /// ref[indices[i, ..., j], ...] = min(ref[indices[i, ..., j], ...], updates[i, ..., j, ...]) + /// + /// Duplicate entries are handled correctly: if multiple `indices` reference + /// the same location, their contributions are combined. + /// + /// Requires `updates.shape = indices.shape + ref.shape[1:]` or `updates.shape = []`. + /// + ///
+ /// + ///
+ /// + ///
+ /// + /// + /// + /// + public static Operation resource_scatter_min(Tensor resource, Tensor indices, Tensor updates, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "ResourceScatterMin", name) { args = new object[] { resource, indices, updates }, attrs = new Dictionary() { } }); + return null; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return resource_scatter_min_eager_fallback(resource, indices, updates, name: name, ctx: _ctx); + } + catch (Exception) { - var results = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo("VarHandleOp", name) - { - attrs = ConvertToDict(new - { - dtype, - shape = shape.dims, - container, - shared_name, - allowed_devices = new string[0] - }) - }); + } + } + Dictionary keywords = new(); + keywords["resource"] = resource; + keywords["indices"] = indices; + keywords["updates"] = updates; + var _op = tf.OpDefLib._apply_op_helper("ResourceScatterMin", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "dtype", _op._get_attr_type("dtype"), "Tindices", _op._get_attr_type("Tindices") }; + _execute.record_gradient("ResourceScatterMin", _op.inputs, _attrs, _result); + } + return _op; + } - return results[0]; + public static Operation resource_scatter_min_eager_fallback(Tensor resource, Tensor indices, Tensor updates, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { resource, indices, updates }; + object[] _attrs = new object[] { "dtype", updates.dtype, "Tindices", indices.dtype }; + var _result = _execute.execute("ResourceScatterMin", 0, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("ResourceScatterMin", _inputs_flat, _attrs, _result); + } + return null; + } + /// + /// Multiplies sparse updates into the variable referenced by `resource`. + /// + /// + /// + /// This operation computes + /// + /// # Scalar indices + /// ref[indices, ...] *= updates[...] + /// + /// # Vector indices (for each i) + /// ref[indices[i], ...] *= updates[i, ...] + /// + /// # High rank indices (for each i, ..., j) + /// ref[indices[i, ..., j], ...] *= updates[i, ..., j, ...] + /// + /// Duplicate entries are handled correctly: if multiple `indices` reference + /// the same location, their contributions multiply. + /// + /// Requires `updates.shape = indices.shape + ref.shape[1:]` or `updates.shape = []`. + /// + ///
+ /// + ///
+ /// + ///
+ /// + /// + /// + /// + public static Operation resource_scatter_mul(Tensor resource, Tensor indices, Tensor updates, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "ResourceScatterMul", name) { args = new object[] { resource, indices, updates }, attrs = new Dictionary() { } }); + return null; } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return resource_scatter_mul_eager_fallback(resource, indices, updates, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["resource"] = resource; + keywords["indices"] = indices; + keywords["updates"] = updates; + var _op = tf.OpDefLib._apply_op_helper("ResourceScatterMul", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "dtype", _op._get_attr_type("dtype"), "Tindices", _op._get_attr_type("Tindices") }; + _execute.record_gradient("ResourceScatterMul", _op.inputs, _attrs, _result); + } + return _op; + } - var _op = tf.OpDefLib._apply_op_helper("VarHandleOp", name, new + public static Operation resource_scatter_mul_eager_fallback(Tensor resource, Tensor indices, Tensor updates, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { resource, indices, updates }; + object[] _attrs = new object[] { "dtype", updates.dtype, "Tindices", indices.dtype }; + var _result = _execute.execute("ResourceScatterMul", 0, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("ResourceScatterMul", _inputs_flat, _attrs, _result); + } + return null; + } + /// + /// Subtracts sparse updates from the variable referenced by `resource`. + /// + /// + /// + /// This operation computes + /// + /// # Scalar indices + /// ref[indices, ...] -= updates[...] + /// + /// # Vector indices (for each i) + /// ref[indices[i], ...] -= updates[i, ...] + /// + /// # High rank indices (for each i, ..., j) + /// ref[indices[i, ..., j], ...] -= updates[i, ..., j, ...] + /// + /// Duplicate entries are handled correctly: if multiple `indices` reference + /// the same location, their contributions add. + /// + /// Requires `updates.shape = indices.shape + ref.shape[1:]` or `updates.shape = []`. + /// + ///
+ /// + ///
+ /// + ///
+ /// + /// + /// + /// + public static Operation resource_scatter_sub(Tensor resource, Tensor indices, Tensor updates, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "ResourceScatterSub", name) { args = new object[] { resource, indices, updates }, attrs = new Dictionary() { } }); + return null; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return resource_scatter_sub_eager_fallback(resource, indices, updates, name: name, ctx: _ctx); + } + catch (Exception) { - dtype, - shape, - container, - shared_name - }); + } + } + Dictionary keywords = new(); + keywords["resource"] = resource; + keywords["indices"] = indices; + keywords["updates"] = updates; + var _op = tf.OpDefLib._apply_op_helper("ResourceScatterSub", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "dtype", _op._get_attr_type("dtype"), "Tindices", _op._get_attr_type("Tindices") }; + _execute.record_gradient("ResourceScatterSub", _op.inputs, _attrs, _result); + } + return _op; + } - return _op.output; + public static Operation resource_scatter_sub_eager_fallback(Tensor resource, Tensor indices, Tensor updates, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { resource, indices, updates }; + object[] _attrs = new object[] { "dtype", updates.dtype, "Tindices", indices.dtype }; + var _result = _execute.execute("ResourceScatterSub", 0, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("ResourceScatterSub", _inputs_flat, _attrs, _result); } + return null; + } + /// + /// Assigns sparse updates to the variable referenced by `resource`. + /// + /// + /// + /// This operation computes + /// + /// # Scalar indices + /// ref[indices, ...] = updates[...] + /// + /// # Vector indices (for each i) + /// ref[indices[i], ...] = updates[i, ...] + /// + /// # High rank indices (for each i, ..., j) + /// ref[indices[i, ..., j], ...] = updates[i, ..., j, ...] + /// + /// + /// + /// + /// + /// + public static Operation resource_scatter_update(Tensor resource, Tensor indices, Tensor updates, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "ResourceScatterUpdate", name) { args = new object[] { resource, indices, updates }, attrs = new Dictionary() { } }); + return null; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return resource_scatter_update_eager_fallback(resource, indices, updates, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["resource"] = resource; + keywords["indices"] = indices; + keywords["updates"] = updates; + var _op = tf.OpDefLib._apply_op_helper("ResourceScatterUpdate", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "dtype", _op._get_attr_type("dtype"), "Tindices", _op._get_attr_type("Tindices") }; + _execute.record_gradient("ResourceScatterUpdate", _op.inputs, _attrs, _result); + } + return _op; + } - public static Tensor destroy_resource_op(Tensor resource, bool ignore_lookup_error = true, string name = null) - => tf.Context.ExecuteOp("DestroyResourceOp", name, - new ExecuteOpArgs(resource).SetAttributes(new { ignore_lookup_error })); + public static Operation resource_scatter_update_eager_fallback(Tensor resource, Tensor indices, Tensor updates, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { resource, indices, updates }; + object[] _attrs = new object[] { "dtype", updates.dtype, "Tindices", indices.dtype }; + var _result = _execute.execute("ResourceScatterUpdate", 0, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("ResourceScatterUpdate", _inputs_flat, _attrs, _result); + } + return null; + } + /// + /// Creates a handle to a Variable resource. + /// + /// + /// + /// the container this variable is placed in. + /// + /// + /// + /// + /// the name by which this variable is referred to. + /// + /// + /// + /// + /// the type of this variable. Must agree with the dtypes + /// of all ops using this variable. + /// + /// + /// + /// + /// The (possibly partially specified) shape of this variable. + /// + /// + /// + /// + /// DEPRECATED. The allowed devices containing the resource variable. Set when the + /// output ResourceHandle represents a per-replica/partitioned resource variable. + /// + /// + /// + public static Tensor var_handle_op(TF_DataType dtype, Shape shape, string container = "", string shared_name = "", string[] allowed_devices = null, string? name = null) + { + var _ctx = tf.Context; + if (allowed_devices is null) + { + allowed_devices = new string[] { }; + } + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "VarHandleOp", name) { args = new object[] { }, attrs = new Dictionary() { ["container"] = container, ["shared_name"] = shared_name, ["dtype"] = dtype, ["shape"] = shape, ["allowed_devices"] = allowed_devices } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return var_handle_op_eager_fallback(container: container, shared_name: shared_name, dtype: dtype, shape: shape, allowed_devices: allowed_devices, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + if (container is null) + { + container = ""; + } + if (shared_name is null) + { + shared_name = ""; + } + Dictionary keywords = new(); + keywords["container"] = container; + keywords["shared_name"] = shared_name; + keywords["dtype"] = dtype; + keywords["shape"] = shape; + keywords["allowed_devices"] = allowed_devices; + var _op = tf.OpDefLib._apply_op_helper("VarHandleOp", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "container", _op.get_attr("container"), "shared_name", _op.get_attr("shared_name"), "dtype", _op._get_attr_type("dtype"), "shape", _op.get_attr("shape"), "allowed_devices", _op.get_attr("allowed_devices") }; + _execute.record_gradient("VarHandleOp", _op.inputs, _attrs, _result); + } + return _result[0]; + } - /// - /// Reads the value of a variable. - /// - /// - /// - /// - /// - public static Tensor read_variable_op(Tensor resource, TF_DataType dtype, string name = null) - => tf.Context.ExecuteOp("ReadVariableOp", name, new ExecuteOpArgs(resource) - .SetAttributes(new { dtype })); + public static Tensor var_handle_op_eager_fallback(string container, string shared_name, TF_DataType dtype, Shape shape, string[] allowed_devices, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { }; + object[] _attrs = new object[] { "container", container, "shared_name", shared_name, "dtype", dtype, "shape", shape, "allowed_devices", allowed_devices }; + var _result = _execute.execute("VarHandleOp", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("VarHandleOp", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Checks whether a resource handle-based variable has been initialized. + /// + /// + /// + public static Tensor var_is_initialized_op(Tensor resource, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "VarIsInitializedOp", name) { args = new object[] { resource }, attrs = new Dictionary() { } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) + { + throw ex; + } + catch (Exception) + { + } + try + { + return var_is_initialized_op_eager_fallback(resource, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["resource"] = resource; + var _op = tf.OpDefLib._apply_op_helper("VarIsInitializedOp", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { }; + _execute.record_gradient("VarIsInitializedOp", _op.inputs, _attrs, _result); + } + return _result[0]; + } - public static Tensor resource_gather(Tensor resource, Tensor indices, TF_DataType dtype, - int batch_dims = 0, bool validate_indices = true, string name = null) + public static Tensor var_is_initialized_op_eager_fallback(Tensor resource, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { resource }; + object[] _attrs = new object[] { }; + var _result = _execute.execute("VarIsInitializedOp", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) { - var _op = tf.OpDefLib._apply_op_helper("ResourceGather", name, new + _execute.record_gradient("VarIsInitializedOp", _inputs_flat, _attrs, _result); + } + return _result[0]; + } + /// + /// Returns the shape of the variable pointed to by `resource`. + /// + /// + /// + /// This operation returns a 1-D integer tensor representing the shape of `input`. + /// + /// For example: + /// + /// ``` + /// # 't' is [[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]] + /// shape(t) ==> [2, 2, 3] + /// ``` + /// + /// + /// + /// + /// + public static Tensor variable_shape(Tensor input, TF_DataType out_type = TF_DataType.TF_INT32, string? name = null) + { + var _ctx = tf.Context; + if (_ctx.executing_eagerly()) + { + try + { + var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, "VariableShape", name) { args = new object[] { input }, attrs = new Dictionary() { ["out_type"] = out_type } }); + return _fast_path_result[0]; + } + catch (NotOkStatusException ex) { - resource, - indices, - dtype, - batch_dims, - validate_indices - }); + throw ex; + } + catch (Exception) + { + } + try + { + return variable_shape_eager_fallback(input, out_type: out_type, name: name, ctx: _ctx); + } + catch (Exception) + { + } + } + Dictionary keywords = new(); + keywords["input"] = input; + keywords["out_type"] = out_type; + var _op = tf.OpDefLib._apply_op_helper("VariableShape", name, keywords); + var _result = _op.outputs; + if (_execute.must_record_gradient()) + { + object[] _attrs = new object[] { "out_type", _op._get_attr_type("out_type") }; + _execute.record_gradient("VariableShape", _op.inputs, _attrs, _result); + } + return _result[0]; + } - return _op.output; + public static Tensor variable_shape_eager_fallback(Tensor input, TF_DataType out_type, string name, Context ctx) + { + Tensor[] _inputs_flat = new Tensor[] { input }; + object[] _attrs = new object[] { "out_type", out_type }; + var _result = _execute.execute("VariableShape", 1, inputs: _inputs_flat, attrs: _attrs, ctx: ctx, name: name); + if (_execute.must_record_gradient()) + { + _execute.record_gradient("VariableShape", _inputs_flat, _attrs, _result); } + return _result[0]; } } diff --git a/src/TensorFlowNET.Core/Operations/handle_data_util.cs b/src/TensorFlowNET.Core/Operations/handle_data_util.cs index a01efc520..363d3144e 100644 --- a/src/TensorFlowNET.Core/Operations/handle_data_util.cs +++ b/src/TensorFlowNET.Core/Operations/handle_data_util.cs @@ -51,7 +51,7 @@ public static void set_handle_data(Tensor target_t, HandleData handle_data) } Status status = new(); var proto = handle_data.ToByteArray(); - c_api.TFC_SetHandleShapeAndType(target_t.graph.c_graph, target_t._as_tf_output(), proto, proto.Length, status); + c_api.TF_SetHandleShapeAndType(target_t.graph.c_graph, target_t._as_tf_output(), proto, proto.Length, status); status.Check(true); } diff --git a/src/TensorFlowNET.Core/Operations/image_ops_impl.cs b/src/TensorFlowNET.Core/Operations/image_ops_impl.cs index e0bc037d2..f1aff28ee 100644 --- a/src/TensorFlowNET.Core/Operations/image_ops_impl.cs +++ b/src/TensorFlowNET.Core/Operations/image_ops_impl.cs @@ -102,25 +102,31 @@ internal static Operation[] _CheckAtLeast3DImage(Tensor image, bool require_stat { throw new ValueError("\'image\' must be fully defined."); } - for (int x = 1; x < 4; x++) + var dims = new Shape(new[] { + image_shape.dims[image_shape.dims.Length - 3], + image_shape.dims[image_shape.dims.Length - 2], + image_shape.dims[image_shape.dims.Length - 1]}); + foreach (var dim in dims.dims) { - if (image_shape.dims[x] == 0) + if (dim == 0) { - throw new ValueError(String.Format("inner 3 dims of \'image.shape\' must be > 0: {0}", image_shape)); + throw new ValueError("inner 3 dimensions of \'image\' must be > 0: " + image_shape); } } var image_shape_last_three_elements = new Shape(new[] { - image_shape.dims[image_shape.dims.Length - 1], + image_shape.dims[image_shape.dims.Length - 3], image_shape.dims[image_shape.dims.Length - 2], - image_shape.dims[image_shape.dims.Length - 3]}); + image_shape.dims[image_shape.dims.Length - 1]}); if (!image_shape_last_three_elements.IsFullyDefined) { Tensor image_shape_ = array_ops.shape(image); - var image_shape_return = tf.constant(new[] { - image_shape_.dims[image_shape.dims.Length - 1], - image_shape_.dims[image_shape.dims.Length - 2], - image_shape_.dims[image_shape.dims.Length - 3]}); + var image_shape_return = tf.slice(image_shape_, new[] { Math.Max(image_shape.dims.Length - 3, 0) }, new[] { 3 }); + + //var image_shape_return = tf.constant(new[] { + // image_shape_.dims[image_shape_.dims.Length - 3], + // image_shape_.dims[image_shape_.dims.Length - 2], + // image_shape_.dims[image_shape_.dims.Length - 1]}); return new Operation[] { check_ops.assert_positive( @@ -177,11 +183,11 @@ internal static Tensor _random_flip(Tensor image, int flip_index, int seed, stri if (shape.ndim == 3 || shape.ndim == Unknown) { Tensor uniform_random = random_ops.random_uniform(new int[] { }, 0f, 1.0f, seed: seed); - var mirror_cond = gen_math_ops.less(uniform_random, .5); + var mirror_cond = gen_math_ops.less(uniform_random, ops.convert_to_tensor(.5)); var result = control_flow_ops.cond( pred: mirror_cond, - true_fn: () => gen_array_ops.reverse(image, new { flip_index }), + true_fn: () => gen_array_ops.reverse(image, ops.convert_to_tensor(new int[] { flip_index })), false_fn: () => image, name: scope ); @@ -197,7 +203,7 @@ internal static Tensor _random_flip(Tensor image, int flip_index, int seed, stri var flips = math_ops.round( array_ops.reshape(uniform_random, shape: array_ops.constant(value: new object[] { batch_size[0], 1, 1, 1 }))); flips = math_ops.cast(flips, image.dtype); - var flipped_input = gen_array_ops.reverse(image, new int[] { flip_index + 1 }); + var flipped_input = gen_array_ops.reverse(image, ops.convert_to_tensor(new int[] { flip_index + 1 })); return flips * flipped_input + (1 - flips) * image; } else @@ -211,7 +217,7 @@ public static Tensor flip_left_right(Tensor image) => _flip(image, 1, "flip_left_right"); public static Tensor flip_up_down(Tensor image) - => _flip(image, 1, "flip_up_down"); + => _flip(image, 0, "flip_up_down"); internal static Tensor _flip(Tensor image, int flip_index, string scope_name) { @@ -222,11 +228,11 @@ internal static Tensor _flip(Tensor image, int flip_index, string scope_name) Shape shape = image.shape; if (shape.ndim == 3 || shape.ndim == Unknown) { - return fix_image_flip_shape(image, gen_array_ops.reverse(image, new { flip_index })); + return fix_image_flip_shape(image, gen_array_ops.reverse_v2(image, ops.convert_to_tensor(new int[] { flip_index }))); } else if (shape.ndim == 4) { - return gen_array_ops.reverse(image, new[] { flip_index + 1 }); + return gen_array_ops.reverse_v2(image, ops.convert_to_tensor(new[] { flip_index + 1 })); } else { @@ -268,15 +274,15 @@ internal static Tensor _rot90_3D(Tensor image, int k, string name_scope) { Tensor _rot90() { - return array_ops.transpose(gen_array_ops.reverse(image, new[] { 1, 0, 2 }), new int[] { 1 }); + return array_ops.transpose(gen_array_ops.reverse(image, ops.convert_to_tensor(new[] { 1, 0, 2 })), new int[] { 1 }); }; Tensor _rot180() { - return gen_array_ops.reverse(image, new[] { 0, 1 }); + return gen_array_ops.reverse(image, ops.convert_to_tensor(new[] { 0, 1 })); }; Tensor _rot270() { - return gen_array_ops.reverse(array_ops.transpose(image, new[] { 1, 0, 2 }), new[] { 1 }); + return gen_array_ops.reverse(array_ops.transpose(image, new[] { 1, 0, 2 }), ops.convert_to_tensor(new[] { 1 })); }; var cases = new[] {math_ops.equal(k, 1), _rot90(), @@ -965,9 +971,9 @@ public static Tensor per_image_standardization(Tensor image) if (Array.Exists(new[] { dtypes.float16, dtypes.float32 }, orig_dtype => orig_dtype == orig_dtype)) image = convert_image_dtype(image, dtypes.float32); - var num_pixels_ = array_ops.shape(image).dims; - num_pixels_ = num_pixels_.Skip(num_pixels_.Length - 3).Take(num_pixels_.Length - (num_pixels_.Length - 3)).ToArray(); - Tensor num_pixels = math_ops.reduce_prod(new Tensor(num_pixels_)); + var x = image.shape["-3:"]; + var num_pixels = math_ops.reduce_prod(x); + Tensor image_mean = math_ops.reduce_mean(image, axis: new(-1, -2, -3), keepdims: true); var stddev = math_ops.reduce_std(image, axis: new(-1, -2, -3), keepdims: true); @@ -1389,7 +1395,7 @@ internal static (Tensor, Tensor, Operation[]) _verify_compatible_image_shapes(Te Operation[] checks = new Operation[] { }; checks.append( control_flow_ops.Assert( - gen_math_ops.greater_equal(array_ops.size(shape1_tensor), 3), new[] { shape1, shape2 }, + gen_math_ops.greater_equal(array_ops.size(shape1_tensor), ops.convert_to_tensor(3)), new[] { shape1, shape2 }, summarize: 10)); checks.append( control_flow_ops.Assert( @@ -1762,8 +1768,8 @@ internal static (Tensor, Tensor, Tensor, Tensor) _cross_suppression(Tensor boxes { var batch_size = array_ops.shape(boxes)[0]; var new_slice = array_ops.slice( - boxes, new object[] { 0, inner_idx * tile_size, 0 }, - new object[] { batch_size, tile_size, 4 }); + boxes, new Tensor[] { ops.convert_to_tensor(0), ops.convert_to_tensor(inner_idx * tile_size), ops.convert_to_tensor(0) }, + new Tensor[] { ops.convert_to_tensor(batch_size), ops.convert_to_tensor(tile_size), ops.convert_to_tensor(4) }); var iou = _bbox_overlap(new_slice, box_slice); var box_slice_after_suppression = array_ops.expand_dims( math_ops.cast(math_ops.reduce_all(iou < iou_threshold, new(1)), @@ -1778,10 +1784,10 @@ internal static Tensor _bbox_overlap(Tensor boxes_a, Tensor boxes_b) { // a_y_min: [0], a_x_min: [1], a_y_max: [2], a_x_max[3] var a_xy_minmax = array_ops.split( - value: boxes_a, num_split: 4, axis: 2); + value: boxes_a, num_or_size_splits: 4, axis: ops.convert_to_tensor(2)); // b_y_min: [0], b_x_min: [1], b_y_max: [2], b_x_max[3] var b_xy_minmax = array_ops.split( - value: boxes_b, num_split: 4, axis: 2); + value: boxes_b, num_or_size_splits: 4, axis: ops.convert_to_tensor(2)); var i_xmin = math_ops.maximum( a_xy_minmax[1], array_ops.transpose(b_xy_minmax[1], new[] { 0, 2, 1 })); @@ -1816,8 +1822,8 @@ internal static (Tensor, float, Tensor, int) _suppression_loop_body(Tensor boxes (Tensor, Tensor, Tensor, Tensor) cross_suppression_func(Tensor boxes, Tensor box_slice, Tensor iou_threshold, Tensor inner_idx, int tile_size) => _cross_suppression(boxes, box_slice, iou_threshold, inner_idx, tile_size); - var box_slice = array_ops.slice(boxes, new[] { 0, idx * tile_size, 0 }, - new[] { batch_size, tile_size, 4 }); + var box_slice = array_ops.slice(boxes, new Tensor[]{ ops.convert_to_tensor(0), ops.convert_to_tensor(idx * tile_size), ops.convert_to_tensor(0) }, + new Tensor[] { ops.convert_to_tensor(batch_size), ops.convert_to_tensor(tile_size), ops.convert_to_tensor(4) }); var iou = _bbox_overlap(box_slice, box_slice); var mask = array_ops.expand_dims( @@ -1943,7 +1949,7 @@ public static (Tensor, Tensor) non_max_suppression_padded_v2(Tensor boxes, Tenso using (ops.name_scope("canonicalize_coordinates")) { // y_1 = [0], x_1 = [1], y_2 = [2], x_2 = [3] - var yx = array_ops.split(value: boxes, num_split: 4, axis: 2); + var yx = array_ops.split(value: boxes, num_or_size_splits: 4, axis: ops.convert_to_tensor(2)); var y_1_is_min = math_ops.reduce_all( gen_math_ops.less_equal(yx[0][0, 0, 0], yx[2][0, 0, 0])); var y_minmax = control_flow_ops.cond( @@ -2046,6 +2052,22 @@ internal static (Tensor, Tensor) non_max_suppression_padded_v1(Tensor boxes, Ten }); } + public static Tensor encode_jpeg(Tensor contents, string name = null) + { + return tf_with(ops.name_scope(name, "encode_jpeg"), scope => + { + return gen_ops.encode_jpeg(contents, name:name); + }); + } + + public static Tensor encode_png(Tensor contents, string name = null) + { + return tf_with(ops.name_scope(name, "encode_png"), scope => + { + return gen_ops.encode_png(contents, name: name); + }); + } + public static Tensor is_jpeg(Tensor contents, string name = null) { return tf_with(ops.name_scope(name, "is_jpeg"), scope => diff --git a/src/TensorFlowNET.Core/Operations/io_ops.cs b/src/TensorFlowNET.Core/Operations/io_ops.cs index 16e1bac47..0b77689d5 100644 --- a/src/TensorFlowNET.Core/Operations/io_ops.cs +++ b/src/TensorFlowNET.Core/Operations/io_ops.cs @@ -31,7 +31,7 @@ public Operation save_v2(Tensor prefix, string[] tensor_names, string[] shape_an try { var result = tf.Runner.TFE_FastPathExecute( - new FastPathOpExecInfo("SaveV2", name, new object[] { prefix, tensor_names, shape_and_slices, tensors })); + new FastPathOpExecInfo(tf.Context, "SaveV2", name, new object[] { prefix, tensor_names, shape_and_slices, tensors })); result = null; return null; } @@ -48,14 +48,14 @@ public Operation save_v2(Tensor prefix, string[] tensor_names, string[] shape_an public Operation save_v2_eager_fallback(Tensor prefix, string[] tensor_names, string[] shape_and_slices, Tensor[] tensors, string name, Context ctx) { DataType[] attr_dtypes; - (attr_dtypes, tensors) = execute.onvert_to_mixed_eager_tensors(tensors, ctx); + (attr_dtypes, tensors) = _execute.onvert_to_mixed_eager_tensors(tensors, ctx); prefix = ops.convert_to_tensor(prefix, TF_DataType.TF_STRING); var tensor_names_tensor = ops.convert_to_tensor(tensor_names, TF_DataType.TF_STRING); var shape_and_slices_tensor = ops.convert_to_tensor(shape_and_slices, TF_DataType.TF_STRING); var inputs_flat = tensors.Concat(new Tensor[] { prefix, tensor_names_tensor, shape_and_slices_tensor }).ToArray(); var attrs = new object[] { "dtypes", attr_dtypes }; - var result = execute.quick_execute("SaveV2", 0, inputs_flat, attrs, ctx, name); + var result = _execute.quick_execute("SaveV2", 0, inputs_flat, attrs, ctx, name); result = null; return null; } diff --git a/src/TensorFlowNET.Core/Operations/list_ops.cs b/src/TensorFlowNET.Core/Operations/list_ops.cs new file mode 100644 index 000000000..3791a2c19 --- /dev/null +++ b/src/TensorFlowNET.Core/Operations/list_ops.cs @@ -0,0 +1,111 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Tensorflow.Eager; + +namespace Tensorflow.Operations +{ + internal class list_ops + { + private static void _set_handle_data(Tensor list_handle, Shape element_shape, TF_DataType element_dtype) + { + if(list_handle is EagerTensor eagerTensor) + { + var handle_data = new CppShapeInferenceResult.Types.HandleData(); + handle_data.IsSet = true; + handle_data.ShapeAndType.Add(new CppShapeInferenceResult.Types.HandleShapeAndType() + { + Shape = element_shape.as_proto(), + Dtype = element_dtype.as_datatype_enum(), + Type = new FullTypeDef() { TypeId = FullTypeId.TftArray } + }); + list_handle.HandleData = handle_data; + } + } + + private static Tensor _build_element_shape(Shape? shape) + { + if(shape is null || shape.IsNull) + { + return ops.convert_to_tensor(-1); + } + else + { + return ops.convert_to_tensor(shape, dtype: dtypes.int32); + } + } + + public static Tensor tensor_list_reserve(Shape? shape, Tensor num_elements, TF_DataType element_dtype, string name = null) + { + var result = gen_list_ops.tensor_list_reserve(_build_element_shape(shape), num_elements, element_dtype, name); + _set_handle_data(result, shape, element_dtype); + return result; + } + + public static Tensor tensor_list_from_tensor(Tensor tensor, Shape element_shape, string? name = null) + { + var result = gen_list_ops.tensor_list_from_tensor(tensor, _build_element_shape(element_shape), name); + _set_handle_data(result, tensor.shape, tensor.dtype); + return result; + } + + public static Tensor tensor_list_get_item(Tensor input_handle, Tensor index, TF_DataType element_dtype, + Shape? element_shape = null, string? name = null) + { + return gen_list_ops.tensor_list_get_item(input_handle, index, _build_element_shape(element_shape), + element_dtype, name); + } + + public static Tensor tensor_list_set_item(Tensor input_handle, Tensor index, Tensor item, + bool resize_if_index_out_of_bounds = false, string? name = null) + { + if (resize_if_index_out_of_bounds) + { + var input_list_size = gen_list_ops.tensor_list_length(input_handle); + input_handle = control_flow_ops.cond(index >= input_list_size, + () => gen_list_ops.tensor_list_resize(input_handle, index + 1), + () => input_handle); + } + var output_handle = gen_list_ops.tensor_list_set_item(input_handle, index, item, name); + handle_data_util.copy_handle_data(input_handle, output_handle); + return output_handle; + } + + public static Tensor tensor_list_stack(Tensor input_handle, TF_DataType element_dtype, int num_elements = -1, + Shape? element_shape = null, string? name = null) + { + return gen_list_ops.tensor_list_stack(input_handle, _build_element_shape(element_shape), element_dtype, num_elements, name); + } + + public static Tensor tensor_list_gather(Tensor input_handle, Tensor indices, TF_DataType element_dtype, + Shape? element_shape = null, string? name = null) + { + return gen_list_ops.tensor_list_gather(input_handle, indices, _build_element_shape(element_shape), element_dtype, name); + } + + public static Tensor tensor_list_scatter(Tensor tensor, Tensor indices, Shape? element_shape = null, Tensor? input_handle = null, + string? name = null) + { + if(input_handle is not null) + { + var output_handle = gen_list_ops.tensor_list_scatter_into_existing_list(input_handle, tensor, indices, name); + handle_data_util.copy_handle_data(input_handle, output_handle); + return output_handle; + } + else + { + var output_handle = gen_list_ops.tensor_list_scatter_v2(tensor, indices, _build_element_shape(element_shape), + constant_op.constant(-1), name); + _set_handle_data(output_handle, element_shape, tensor.dtype); + return output_handle; + } + } + + public static Tensor empty_tensor_list(Shape? element_shape, TF_DataType element_dtype, int max_num_elements = -1, + string? name = null) + { + return gen_list_ops.empty_tensor_list(_build_element_shape(element_shape), element_dtype: element_dtype, + max_num_elements: ops.convert_to_tensor(max_num_elements, dtype: dtypes.int32), name: name); + } + } +} diff --git a/src/TensorFlowNET.Core/Operations/logging_ops.cs b/src/TensorFlowNET.Core/Operations/logging_ops.cs index e38e60b5b..3303cadc3 100644 --- a/src/TensorFlowNET.Core/Operations/logging_ops.cs +++ b/src/TensorFlowNET.Core/Operations/logging_ops.cs @@ -30,7 +30,7 @@ public Tensor print_v2(Tensor input, string output_stream = "stderr", string end name: name); return tf.Context.ExecuteOp("PrintV2", name, new ExecuteOpArgs(formatted_string) - .SetAttributes(new { output_stream, end })); + .SetAttributes(new { output_stream, end })).SingleOrNull; } } } diff --git a/src/TensorFlowNET.Core/Operations/math_ops.cs b/src/TensorFlowNET.Core/Operations/math_ops.cs index f7b428bb4..e77df702f 100644 --- a/src/TensorFlowNET.Core/Operations/math_ops.cs +++ b/src/TensorFlowNET.Core/Operations/math_ops.cs @@ -21,6 +21,7 @@ limitations under the License. using Tensorflow.Framework; using static Tensorflow.Binding; using Tensorflow.Operations; +using System.Runtime.CompilerServices; namespace Tensorflow { @@ -39,18 +40,18 @@ public static Tensor abs(Tensor x, string name = null) { return gen_ops.complex_abs(x, Tout: x.dtype.real_dtype(), name: name); } - return gen_math_ops._abs(x, name: name); + return gen_math_ops.abs(x, name: name); }); } public static Tensor add(Tx x, Ty y, string name = null) - => gen_math_ops.add(x, y, name); + => gen_math_ops.add(ops.convert_to_tensor(x), ops.convert_to_tensor(y), name); public static Tensor add_v2(Tensor x, Tensor y, string name = null) => tf.Context.ExecuteOp("AddV2", name, new ExecuteOpArgs(x, y)); public static Tensor add_v2(Tx x, Ty y, string name = null) - => gen_math_ops.add_v2(x, y, name); + => gen_math_ops.add_v2(ops.convert_to_tensor(x), ops.convert_to_tensor(y), name); /// /// Adds all input tensors element-wise. @@ -76,6 +77,9 @@ public static Tensor add_n(Tensor[] inputs, string name = null) public static Tensor argmax(Tensor input, Axis dimension, TF_DataType output_type = TF_DataType.TF_INT64, string name = null) => gen_math_ops.arg_max(input, dimension, output_type: output_type, name: name); + public static Tensor argmin(Tensor input, Axis dimension, TF_DataType output_type = TF_DataType.TF_INT64, string name = null) + => gen_math_ops.arg_min(input, dimension, output_type: output_type, name: name); + public static Tensor round(Tensor x, string name = null) { x = ops.convert_to_tensor(x, name: "x"); @@ -254,9 +258,9 @@ public static Tensor einsum(string equation, Tensors inputs, string name = null) } public static Tensor greater_equal(Tx x, Ty y, string name = null) - => gen_math_ops.greater_equal(x, y, name: name); + => gen_math_ops.greater_equal(ops.convert_to_tensor(x), ops.convert_to_tensor(y), name: name); public static Tensor equal(Tx x, Ty y, string name = null) - => gen_math_ops.equal(x, y, name: name); + => gen_math_ops.equal(ops.convert_to_tensor(x), ops.convert_to_tensor(y), name: name); /// /// Computes the Gauss error function of `x` element-wise. @@ -268,19 +272,19 @@ public static Tensor erf(Tensor x, string name = null) => tf.Context.ExecuteOp("Erf", name, new ExecuteOpArgs(x)); public static Tensor sqrt(Tensor x, string name = null) - => gen_math_ops.sqrt(x, name: name); + => tf.Context.ExecuteOp("Sqrt", name, new ExecuteOpArgs(x)); public static Tensor multiply(Tensor x, Tensor y, string name = null) => tf.Context.ExecuteOp("Mul", name, new ExecuteOpArgs(x, y)); public static Tensor multiply(Tx x, Ty y, string name = null) - => gen_math_ops.mul(x, y, name: name); + => gen_math_ops.mul(ops.convert_to_tensor(x), ops.convert_to_tensor(y), name: name); public static Tensor not_equal(Tx x, Ty y, string name = null) - => gen_math_ops.not_equal(x, y, name: name); + => gen_math_ops.not_equal(ops.convert_to_tensor(x), ops.convert_to_tensor(y), name: name); public static Tensor mul_no_nan(Tx x, Ty y, string name = null) - => gen_math_ops.mul_no_nan(x, y, name: name); + => gen_math_ops.mul_no_nan(ops.convert_to_tensor(x), ops.convert_to_tensor(y), name: name); public static Tensor scalar_mul(Tscale scale, Tx x, string name = null) => tf.Context.ExecuteOp("Mul", name, new ExecuteOpArgs(scale, x)); @@ -396,7 +400,7 @@ public static Tensor sigmoid(T x, string name = null) }); public static Tensor sign(T x, string name = null) - => gen_math_ops.sign(x, name: name); + => gen_math_ops.sign(ops.convert_to_tensor(x), name: name); public static Tensor sin(Tensor x, string name = null) => tf.Context.ExecuteOp("Sin", name, new ExecuteOpArgs(x)); @@ -421,7 +425,7 @@ public static Tensor square(Tensor x, string name = null) public static Tensor subtract(Tx x, Ty y, string name = null) { - return gen_math_ops.sub(x, y, name); + return gen_math_ops.sub(ops.convert_to_tensor(x), ops.convert_to_tensor(y), name); } public static Tensor log(Tensor x, string name = null) @@ -455,8 +459,8 @@ public static Tensor linspace(Tensor start, Tensor stop, int num = 50, string na var axis_tensor = array_ops.where_v2(constant_op.constant(axis >= 0), x: axis, y: ndims + axis); // The purpose is to avoid having negative values when repeating. - var num_fill = gen_math_ops.maximum(num_int_tensor - 2, 0); - var n_steps = gen_math_ops.maximum(num_int_tensor - 1, 1); + var num_fill = gen_math_ops.maximum(num_int_tensor - 2, ops.convert_to_tensor(0)); + var n_steps = gen_math_ops.maximum(num_int_tensor - 1, ops.convert_to_tensor(1)); var delta = (expanded_stop - expanded_start) / cast(n_steps, expanded_stop.dtype); var range_end = array_ops.where_v2(num_int_tensor >= 0, n_steps, -1); @@ -503,7 +507,7 @@ public static Tensor reduced_shape(Tensor input_shape, Tensor axes) var axes_shape = array_ops.shape(axes); var rng = math_ops.range(input_rank); var a1 = new Tensor[] { rng, axes }; - var fill = gen_array_ops.fill(axes_shape, 1); + var fill = gen_array_ops.fill(axes_shape, ops.convert_to_tensor(1)); var a2 = new Tensor[] { input_shape, fill }; return gen_data_flow_ops.dynamic_stitch(a1, a2); @@ -528,7 +532,7 @@ public static Tensor reciprocal(Tensor x, string name = null) /// public static Tensor reduce_all(Tensor input_tensor, Axis? axis = null, bool keepdims = false, string name = null) { - var all = gen_math_ops._all(input_tensor, + var all = gen_math_ops.all(input_tensor, _ReductionDims(input_tensor, axis), keepdims, name: name); @@ -581,23 +585,34 @@ public static Tensor reduce_logsumexp(Tensor input_tensor, Axis axis = null, boo public static Tensor reduce_any(Tensor input_tensor, Axis axis = null, bool keepdims = false, string name = null) { var r = _ReductionDims(input_tensor, axis); - var max = (axis != null) ? gen_math_ops._any(input_tensor, axis, keepdims, name) : - gen_math_ops._any(input_tensor, r, keepdims, name); + var max = (axis != null) ? gen_math_ops.any(input_tensor, axis, keepdims, name) : + gen_math_ops.any(input_tensor, r, keepdims, name); return _may_reduce_to_scalar(keepdims, axis, max); } + public static Tensor reduce_euclidean_norm(Tensor input_tensor, Axis axis = null, bool keepdims = false, string name = null) + { + var r = _ReductionDims(input_tensor, axis); + var distance = tf.Context.ExecuteOp("EuclideanNorm", name, + new ExecuteOpArgs(input_tensor, r).SetAttributes(new + { + keep_dims = keepdims + })); + return _may_reduce_to_scalar(keepdims, axis, distance); + } + public static Tensor reduce_max(Tensor input_tensor, Axis axis = null, bool keepdims = false, string name = null) { var r = _ReductionDims(input_tensor, axis); - var max = (axis != null) ? gen_math_ops._max(input_tensor, axis, keepdims, name) : - gen_math_ops._max(input_tensor, r, keepdims, name); + var max = (axis != null) ? gen_math_ops.max(input_tensor, axis, keepdims, name) : + gen_math_ops.max(input_tensor, r, keepdims, name); return _may_reduce_to_scalar(keepdims, axis, max); } public static Tensor reduce_min(Tensor input_tensor, Axis axis = null, bool keepdims = false, string name = null) { var r = _ReductionDims(input_tensor, axis); - var min = gen_math_ops._min(input_tensor, r, keepdims, name); + var min = gen_math_ops.min(input_tensor, r, keepdims, name); return _may_reduce_to_scalar(keepdims, axis, min); } @@ -643,7 +658,7 @@ public static Tensor __case__(Tensor x, TF_DataType dtype, string name = null) public static Tensor reduce_sum(Tensor input_tensor, Tensor axis = null, bool keepdims = false, string name = null) { var r = _ReductionDims(input_tensor, axis); - var m = gen_math_ops._sum(input_tensor, r, keep_dims: keepdims, name: name); + var m = gen_math_ops.sum(input_tensor, r, keep_dims: keepdims, name: name); return _may_reduce_to_scalar(keepdims, axis, m); } @@ -752,10 +767,10 @@ public static Tensor floordiv(Tensor x, Tensor y, string name = null) } public static Tensor minimum(Tx x, Ty y, string name = null) - => gen_math_ops.minimum(x, y, name: name); + => gen_math_ops.minimum(ops.convert_to_tensor(x), ops.convert_to_tensor(y), name: name); public static Tensor maximum(Tx x, Ty y, string name = null) - => gen_math_ops.maximum(x, y, name: name); + => gen_math_ops.maximum(ops.convert_to_tensor(x), ops.convert_to_tensor(y), name: name); /// /// Multiplies matrix `a` by matrix `b`, producing `a` * `b`. @@ -779,10 +794,7 @@ public static Tensor matmul(Tensor a, Tensor b, bool adjoint_a = false, bool adjoint_b = false, bool a_is_sparse = false, bool b_is_sparse = false, string name = null) - { - Tensor result = null; - - tf_with(ops.name_scope(name, "MatMul", new Tensor[] { a, b }), scope => + => tf_with(ops.name_scope(name, "MatMul", (a, b)), scope => { name = scope; @@ -803,12 +815,10 @@ public static Tensor matmul(Tensor a, Tensor b, transpose_b = true; } - result = gen_math_ops.mat_mul(a, b, transpose_a, transpose_b, name); + return tf.Context.ExecuteOp("MatMul", name, new ExecuteOpArgs(a, b) + .SetAttributes(new { transpose_a, transpose_b })); }); - return result; - } - public static Tensor batch_matmul(Tensor x, Tensor y, bool adj_x = false, bool adj_y = false, string name = null) diff --git a/src/TensorFlowNET.Core/Operations/nn_impl.py.cs b/src/TensorFlowNET.Core/Operations/nn_impl.py.cs index d24e81ef4..ca4b885f7 100644 --- a/src/TensorFlowNET.Core/Operations/nn_impl.py.cs +++ b/src/TensorFlowNET.Core/Operations/nn_impl.py.cs @@ -236,7 +236,7 @@ public static Tensor zero_fraction(Tensor value, string name = null) Tensor size = array_ops.size(value, out_type: dtypes.int64); Tensor zero_fraction_float32 = null; - size = gen_math_ops.less_equal(size, dtypes.int32.max()); + size = gen_math_ops.less_equal(size, ops.convert_to_tensor(dtypes.int32.max())); Tensor num_nonzero = control_flow_ops.cond( size, () => math_ops.cast(_count_nonzero(value, dtype: dtypes.int32), TF_DataType.TF_INT64), diff --git a/src/TensorFlowNET.Core/Operations/nn_ops.cs b/src/TensorFlowNET.Core/Operations/nn_ops.cs index b8d5103c4..00d7d316b 100644 --- a/src/TensorFlowNET.Core/Operations/nn_ops.cs +++ b/src/TensorFlowNET.Core/Operations/nn_ops.cs @@ -55,7 +55,7 @@ public static Tensor bias_add(Tensor value, return tf_with(ops.name_scope(name, "BiasAdd", new { value, bias }), scope => { name = scope; - return gen_nn_ops.bias_add(value, bias, data_format: data_format, name: name); + return gen_nn_ops.bias_add(value, ops.convert_to_tensor(bias), data_format: data_format, name: name); }); } @@ -117,7 +117,7 @@ public static Tensor in_top_k(Tensor predictions, Tensor targets, int k, string { return tf_with(ops.name_scope(name, "in_top_k"), delegate { - return gen_nn_ops.in_top_kv2(predictions, targets, k, name: name); + return gen_nn_ops.in_top_kv2(predictions, targets, ops.convert_to_tensor(k), name: name); }); } @@ -222,8 +222,8 @@ public static Tensor sparse_softmax_cross_entropy_with_logits(Tensor labels = nu // Check if no reshapes are required. if (logits.shape.ndim == 2) { - var (cost, _) = gen_nn_ops.sparse_softmax_cross_entropy_with_logits( - precise_logits, labels, name: name); + var cost = gen_nn_ops.sparse_softmax_cross_entropy_with_logits( + precise_logits, labels, name: name)[0]; if (logits.dtype == dtypes.float16) return math_ops.cast(cost, dtypes.float32); else @@ -261,7 +261,8 @@ public static Tensor softmax_cross_entropy_with_logits_v2_helper(Tensor labels, // The second output tensor contains the gradients. We use it in // _CrossEntropyGrad() in nn_grad but not here. - var (cost, unused_backprop) = gen_nn_ops.softmax_cross_entropy_with_logits(precise_logits, labels, name: name); + var entropy = gen_nn_ops.softmax_cross_entropy_with_logits(precise_logits, labels, name: name); + var (cost, unused_backprop) = (entropy[0], entropy[1]); // The output cost shape should be the input minus axis. var output_shape = array_ops.slice(input_shape, diff --git a/src/TensorFlowNET.Core/Operations/sort_ops.cs b/src/TensorFlowNET.Core/Operations/sort_ops.cs index 34b903230..db38a073b 100644 --- a/src/TensorFlowNET.Core/Operations/sort_ops.cs +++ b/src/TensorFlowNET.Core/Operations/sort_ops.cs @@ -44,7 +44,7 @@ public static Tensor argsort(Tensor values, Axis axis = null, string direction = { sorted = true })); - return indices; + return indices.Single; } public static Tensor sort(Tensor values, Axis axis, string direction = "ASCENDING", string? name = null) diff --git a/src/TensorFlowNET.Core/Operations/tensor_array_ops.cs b/src/TensorFlowNET.Core/Operations/tensor_array_ops.cs index 7d2da544c..6be0706c2 100644 --- a/src/TensorFlowNET.Core/Operations/tensor_array_ops.cs +++ b/src/TensorFlowNET.Core/Operations/tensor_array_ops.cs @@ -13,11 +13,23 @@ public class tensor_array_ops /// public static TensorArray build_ta_with_new_flow(TensorArray old_ta, Tensor flow) { - var new_ta = tf.TensorArray( - dtype: old_ta.dtype, - infer_shape: old_ta.infer_shape, + if (!tf.Context.executing_eagerly() && old_ta is not _GraphTensorArrayV2 && control_flow_util.EnableControlFlowV2(ops.get_default_graph())) + { + throw new NotImplementedException("Attempting to build a graph-mode TF2-style " + + "TensorArray from either an eager-mode " + + "TensorArray or a TF1-style TensorArray. " + + "This is not currently supported. You may be " + + "attempting to capture a TensorArray " + + "inside a tf.function or tf.data map function. " + + "Instead, construct a new TensorArray inside " + + "the function."); + } + var new_ta = TensorArray.Create(old_ta.dtype, handle: old_ta.handle, flow: flow, infer_shape: old_ta.infer_shape, colocate_with_first_write_call: old_ta.colocate_with_first_write_call); - + new_ta._dynamic_size = old_ta._dynamic_size; + new_ta._size = old_ta._size; + new_ta._colocate_with = old_ta._colocate_with; + new_ta._element_shape = old_ta._element_shape; return new_ta; } diff --git a/src/TensorFlowNET.Core/Operations/while_v2.cs b/src/TensorFlowNET.Core/Operations/while_v2.cs new file mode 100644 index 000000000..aae15b77d --- /dev/null +++ b/src/TensorFlowNET.Core/Operations/while_v2.cs @@ -0,0 +1,400 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Text; +using Tensorflow.Common.Extensions; +using Tensorflow.Common.Types; +using Tensorflow.Eager; +using Tensorflow.Framework; +using Tensorflow.Framework.Models; +using Tensorflow.Graphs; +using static Tensorflow.Binding; + +namespace Tensorflow.Operations +{ + class _OperationWithOutputs : Operation + { + public _OperationWithOutputs(IntPtr handle, Graph g = null) + { + _handle = handle; + _graph = g; + _outputs = null; + g._add_op(this); + } + } + internal class while_v2 + { + public static Tensor[] while_loop(Func cond, + Func body, + Tensors loop_vars, + int maximum_iterations = -1, + int parallel_iterations = 10, + string name = null, + bool back_prop = true, + bool return_same_structure = true) + { + var orig_loop_vars = loop_vars; + var flat_orig_loop_vars = orig_loop_vars.Flatten().ToArray(); + int len_orig_loop_vars = orig_loop_vars.Length; + + loop_vars = _tensor_array_to_flow(loop_vars); + loop_vars = Nest.MapStructure(x => _convert_to_tensor_or_indexed_slices(x), loop_vars).ToTensors(); + + var loop_vars_signature = Nest.MapStructure(x => new TensorSpec(x.shape, x.dtype), loop_vars); + + var flat_shape_invariants = Nest.Flatten(loop_vars_signature).Select(x => x.shape).ToArray(); + + if(string.IsNullOrEmpty(name)) + { + name = "while"; + } + + return tf_with(ops.name_scope(name), nameScopeWhile => + { + string scope = (nameScopeWhile as ops.NameScope).scope_name; + string cond_name = control_flow_util.unique_fn_name(scope, "cond"); + string body_name = control_flow_util.unique_fn_name(scope, "body"); + + var maximum_iterations_loop_var = _build_maximum_iterations_loop_var(maximum_iterations); + var loop_counter = constant_op.constant(0, maximum_iterations == -1 ? TF_DataType.DtInvalid : maximum_iterations_loop_var.dtype, + name: "loop_counter"); + loop_vars = new Tensor[] { loop_counter, maximum_iterations_loop_var }.Concat(loop_vars).ToArray(); + + var func_graph_signature = new TensorSpec[] {TensorSpec.FromTensor(loop_counter),TensorSpec.FromTensor(maximum_iterations_loop_var)} + .Concat(loop_vars_signature.Flatten()).ToArray(); + + // TODO(Rinne): possible wrong implemenation here. + var add_control_dependencies = false; + + object[] wrapped_cond(object[] inputs) + { + Tensor loop_counter = (Tensor)inputs[0]; + Tensor maximum_iterations_arg = (Tensor)inputs[1]; + Tensor[] args = inputs.Skip(2).Select(x => (Tensor)x).ToArray(); + var pred = cond(_pack_sequence_as(loop_vars_signature, flat_orig_loop_vars, args)); + if(pred.shape.IsNull || pred.shape.ndim > 0) + { + pred = array_ops.squeeze(pred); + } + if(maximum_iterations == -1) + { + return new object[] { pred }; + } + else + { + return new object[] { math_ops.logical_and(loop_counter < maximum_iterations_arg, pred) }; + } + } + + var cond_graph = FuncGraph.func_graph_from_func(cond_name, wrapped_cond, null, + null, signature: func_graph_signature, add_control_dependencies: add_control_dependencies); + + bool stateful_parallelism = false; + + object[] wrapped_body(object[] inputs) + { + Tensor loop_counter = (Tensor)inputs[0]; + Tensor maximum_iterations_arg = (Tensor)inputs[1]; + Tensor[] args = inputs.Skip(2).Select(x => (Tensor)x).ToArray(); + + _copy_handle_data(loop_vars.Flatten().Skip(2), args); + + foreach(var t in cond_graph.external_captures) + { + var graph = (FuncGraph)(ops.get_default_graph()); + graph.capture(t); + } + + var outputs = body(_pack_sequence_as(loop_vars_signature, flat_orig_loop_vars, args)); + outputs = _tensor_array_to_flow(outputs); + + return new object[] { loop_counter + 1, maximum_iterations_arg }.Concat(outputs).ToArray(); + } + + var body_graph = FuncGraph.func_graph_from_func(body_name, wrapped_body, null, null, func_graph_signature, + add_control_dependencies: add_control_dependencies, acd_record_initial_resource_uses: stateful_parallelism); + + // TODO(Rinne): possible wrong implementation here. + NestList loop_vars_list = new(new Tensors[] { loop_vars, body_graph.external_captures.ToTensors() }); + body_graph.Outputs.AddRange(body_graph.internal_captures); + + cond_graph.as_default(); + int num_cond_captures = cond_graph.external_captures.Length; + Debug.Assert(cond_graph.external_captures.SequenceEqual(body_graph.external_captures.Take(num_cond_captures).ToArray())); + _duplicate_body_captures_in_cond(cond_graph, body_graph.external_captures.Skip(num_cond_captures).ToArray()); + cond_graph.Exit(); + + int first_loop_var_index = 2; + + int num_flattened_oututs = orig_loop_vars.Length; + int num_original_outputs = body_graph.Outputs.Length; + if (back_prop && control_flow_util.output_all_intermediates()) + { + var intermediate_tensors = _get_intermediates(body_graph); + + foreach(var intermediate_tensor in intermediate_tensors) + { + var tensor_list = list_ops.empty_tensor_list(intermediate_tensor.shape, intermediate_tensor.dtype, maximum_iterations); + loop_vars_list.Values.Add(tensor_list); + + cond_graph.as_default(); + cond_graph.capture(tensor_list); + cond_graph.Exit(); + + body_graph.as_default(); + var appended_tensor_list = gen_ops.tensor_list_push_back(tensor_list, intermediate_tensor); + body_graph.Outputs.Add(appended_tensor_list); + body_graph.Exit(); + } + } + + List flattened_loop_vars = new(); + foreach(var item in loop_vars_list.Values) + { + flattened_loop_vars.AddRange(item.Flatten()); + } + // skip the check + + // TODO(Rinne): deal with control dependencies + var output_shapes = body_graph.Outputs.Select(t => t.shape).ToArray(); + var span = new Span(output_shapes).Slice(first_loop_var_index, num_flattened_oututs); + for(int i = 0; i < span.Length; i++) + { + span[i] = flat_shape_invariants[i]; + } + + Tensor[] outputs = _build_while_op(flattened_loop_vars.ToArray(), cond_graph, body_graph, output_shapes, parallel_iterations, + (nameScopeWhile as ops.NameScope).scope_name, num_original_outputs, stateful_parallelism); + + if (!ops.get_default_graph().building_function) + { + outputs = outputs.Select(t => array_ops.identity(t)).ToArray(); + } + + var output_loop_vars = outputs.Skip(first_loop_var_index).Take(num_flattened_oututs).ToArray(); + + if (!back_prop) + { + output_loop_vars = output_loop_vars.Select(t => array_ops.stop_gradient(t)).ToArray(); + } + outputs = _pack_sequence_as(loop_vars_signature, flat_orig_loop_vars, output_loop_vars); + + return outputs; + }); + } + + private static Tensors _tensor_array_to_flow(Tensors loop_vars) + { + if(loop_vars.NestType == NestType.Node) + { + if(loop_vars.NodeValue is FakeTensorByTensorArray fake) + { + return new Tensors(fake.TensorArray.flow); + } + else + { + return new Tensors(loop_vars.NodeValue!); + } + } + else if(loop_vars.NestType == NestType.List) + { + List> list = new(); + foreach(var item in loop_vars.ListValue!) + { + if(item.NestType == NestType.Node) + { + var nested = item.AsNest(); + if (nested.NodeValue is FakeTensorByTensorArray fake) + { + list.Add(new Nest(fake.TensorArray.flow)); + } + else + { + list.Add(new Nest(nested.NodeValue!)); + } + } + else + { + list.Add(new Nest(item.AsNest())); + } + } + return Tensors.FromNest(new Nest(list)); + } + else + { + throw new NotImplementedException(); + } + } + + private static Tensor[] _build_while_op(Tensor[] loop_vars, FuncGraph cond_graph, FuncGraph body_graph, + Shape[] output_shapes, int parallel_iterations, string name, int num_original_outputs, bool stateful_parallelism) + { + var cond_stateful_ops = cond_graph.get_operations().Select(x => x.op); + var body_stateful_ops = body_graph.get_operations().Select(x => x.op); + + bool is_stateful = cond_stateful_ops.Count() > 0 || body_stateful_ops.Count() > 0; + + Tensor[] _make_op(Tensor[] inputs) + { + Tensor[] outputs; + if (is_stateful) + { + outputs = gen_functional_ops._while( + inputs, + control_flow_util.create_new_tf_function(cond_graph), + control_flow_util.create_new_tf_function(body_graph), + output_shapes, + parallel_iterations, + name + ); + } + else + { + outputs = gen_functional_ops.stateless_while( + inputs, + control_flow_util.create_new_tf_function(cond_graph), + control_flow_util.create_new_tf_function(body_graph), + output_shapes, + parallel_iterations, + name + ); + } + var (while_op, tensors) = control_flow_util.get_op_and_outputs(outputs); + _copy_handle_data(body_graph.Outputs, tensors); + _set_read_only_resource_inputs_attr(while_op, new FuncGraph[]{cond_graph, body_graph}); + while_op._set_attr("_num_original_outputs", new AttrValue() { I = num_original_outputs }); + while_op._set_attr("_stateful_parallelism", new AttrValue() { B = stateful_parallelism }); + + cond_graph.outer_graph = ops.get_default_graph(); + body_graph.outer_graph = ops.get_default_graph(); + // TODO(Rinne): set the two graphs to while_op + return tensors; + } + + return control_flow_util.run_as_function_for_tape_gradients(_make_op, loop_vars); + } + + /// + /// Sets the list of resource inputs which are read-only. This is used by AutomaticControlDependencies. + /// + /// + /// + private static void _set_read_only_resource_inputs_attr(Operation op, FuncGraph[] branch_graphs) + { + List read_only_indices = Enumerable.Range(0, op.inputs.Length).ToList(); + foreach(var branch_graph in branch_graphs) + { + if (read_only_indices.Count == 0) + { + break; + } + var branch_read_only_indices = auto_control_deps_utils.get_read_only_resource_input_indices_graph(branch_graph); + read_only_indices = read_only_indices.Intersect(branch_read_only_indices).ToList(); + } + AttrValue.Types.ListValue listValue = new(); + listValue.I.AddRange(read_only_indices.OrderBy(x => x).Select(x => (long)x)); + op._set_attr(auto_control_deps_utils.READ_ONLY_RESOURCE_INPUTS_ATTR, new AttrValue() + { + List = listValue + }); + } + + private static Tensors _pack_sequence_as(INestStructure loop_vars_signature, Tensor[] flat_orig_loop_vars, Tensor[] loop_vars) + { + var flattened_loop_vars = zip(loop_vars, flat_orig_loop_vars).Select<(Tensor, Tensor), Tensor>(item => + { + var (flow, y) = item; + if (y is FakeTensorByTensorArray ta) + { + return new FakeTensorByTensorArray(tensor_array_ops.build_ta_with_new_flow(ta.TensorArray, flow)); + } + else + { + return flow; + } + }).ToArray(); + return Nest.PackSequenceAs(loop_vars_signature, flattened_loop_vars).ToTensors(); + } + + private static Tensor[] _get_intermediates(FuncGraph func_graph) + { + List intermediates = new(); + var reversed_captures = func_graph.captures.ToDictionary(x => x.Item2, x => x.Item1); + + foreach(var op in func_graph.get_operations()) + { + Debug.Assert(op is Operation); + var oper = (Operation)op; + if(oper.type == "Identity" || oper.type == "MutexLock") + { + continue; + } + foreach(var o in op.outputs) + { + if(o != func_graph.Inputs[0] && o.dtype != dtypes.resource && !reversed_captures.ContainsKey(o)) + { + intermediates.Add(o); + } + } + } + return intermediates.ToArray(); + } + + private static void _duplicate_body_captures_in_cond(FuncGraph cond_graph, Tensor[] body_graph_captures) + { + var types = body_graph_captures.Select(t => t.dtype).ToList(); + var c_graph = cond_graph.c_graph; + var placeholders = types.Select(x => CreatePlaceholder(c_graph, _build_cond_placeholders_name_prefix(cond_graph), x)).ToList(); + + var placeholder_ops = placeholders.Select(ph => new _OperationWithOutputs(ph.oper, cond_graph)).ToList(); + + List tensors = new(); + foreach(var (op, ph, dtype) in zip(placeholder_ops, placeholders, types)) + { + var tensor = Tensor._create_with_tf_output(op, 0, dtype, ph); + op._outputs = new Tensor[] { tensor }; + tensors.Add(tensor); + } + + var tuples = zip(body_graph_captures, tensors).ToList(); + var keys = body_graph_captures.Select(t => t.Id).ToList(); + cond_graph._captures.Update(zip(keys, tuples).ToDictionary(x => x.Item1, x => x.Item2)); + cond_graph.Inputs.AddRange(tensors); + } + + private static TF_Output CreatePlaceholder(SafeGraphHandle graph, string name, TF_DataType dtype) + { + var desc = c_api.TF_NewOperation(graph, "Placeholder", name); + c_api.TF_SetAttrType(desc, "dtype", dtype); + var op = c_api.TF_FinishOperation(desc, tf.Status); + tf.Status.Check(true); + var output = new TF_Output(); + output.oper = op; + output.index = 0; + return output; + } + + private static string _build_cond_placeholders_name_prefix(FuncGraph cond_graph) + { + return cond_graph.unique_name(cond_graph.Name + "___redundant_placeholder"); + } + + private static Tensor _convert_to_tensor_or_indexed_slices(Tensor value) + { + return ops.convert_to_tensor(value, as_ref: false); + } + + private static Tensor _build_maximum_iterations_loop_var(int maximum_iterations = -1) + { + return ops.convert_to_tensor(maximum_iterations, dtypes.int32, "maximum_iterations"); + } + + private static void _copy_handle_data(IEnumerable src_tensors, IEnumerable dst_tensors) + { + foreach(var (src_t, dst_t) in zip(src_tensors, dst_tensors)) + { + handle_data_util.copy_handle_data(src_t, dst_t); + } + } + } +} diff --git a/src/TensorFlowNET.Core/Sessions/BaseSession.cs b/src/TensorFlowNET.Core/Sessions/BaseSession.cs index 0a9cfc2eb..3dab4ec71 100644 --- a/src/TensorFlowNET.Core/Sessions/BaseSession.cs +++ b/src/TensorFlowNET.Core/Sessions/BaseSession.cs @@ -30,6 +30,7 @@ public BaseSession(SafeSessionHandle handle, Graph g) { _handle = handle; _graph = g ?? ops.get_default_graph(); + _status = tf.Status; } public BaseSession(string target = "", Graph g = null, ConfigProto config = null, Status status = null) diff --git a/src/TensorFlowNET.Core/Status/Status.cs b/src/TensorFlowNET.Core/Status/Status.cs index a890c2aef..12b6fba2b 100644 --- a/src/TensorFlowNET.Core/Status/Status.cs +++ b/src/TensorFlowNET.Core/Status/Status.cs @@ -17,6 +17,7 @@ limitations under the License. using System; using System.Diagnostics; using System.Runtime.CompilerServices; +using Tensorflow.Exceptions; using Tensorflow.Util; using static Tensorflow.c_api; @@ -88,7 +89,7 @@ public void Check(bool throwException = false) case TF_Code.TF_INVALID_ARGUMENT: throw new InvalidArgumentError(message); default: - throw new TensorflowException(message); + throw new NotOkStatusException(message); } } } diff --git a/src/TensorFlowNET.Core/Tensorflow.Binding.csproj b/src/TensorFlowNET.Core/Tensorflow.Binding.csproj index 09f5b0770..42c0399da 100644 --- a/src/TensorFlowNET.Core/Tensorflow.Binding.csproj +++ b/src/TensorFlowNET.Core/Tensorflow.Binding.csproj @@ -4,14 +4,14 @@ netstandard2.0;net6.0 Tensorflow.Binding Tensorflow - 2.10.0 - 0.100.5 + 2.15.0 + 0.150.0 10.0 enable - Haiping Chen, Meinrad Recheis, Eli Belash + Haiping Chen, Eli Belash, Yaohui Liu, Meinrad Recheis SciSharp STACK - true - Apache 2.0, Haiping Chen $([System.DateTime]::UtcNow.ToString(yyyy)) + False + Apache 2.0, Haiping Chen since 2018 https://github.com/SciSharp/TensorFlow.NET git http://scisharpstack.org @@ -20,8 +20,16 @@ Google's TensorFlow full binding in .NET Standard. Building, training and infering deep learning models. https://tensorflownet.readthedocs.io - 0.100.5.0 + 0.150.0.0 + tf.net 0.150.x and above are based on tensorflow native 2.15.0 + * Support BERT model. + + tf.net 0.110.x and above are based on tensorflow native 2.11.0 + * Support RNN, LSTM model. + * Support Transformer model. + * Added IMDB dataset. + tf.net 0.100.x and above are based on tensorflow native 2.10.0 * Eager Mode is added finally. @@ -37,13 +45,14 @@ https://tensorflownet.readthedocs.io tf.net 0.6x.x aligns with TensorFlow v2.6.x native library. tf.net 0.7x.x aligns with TensorFlow v2.7.x native library. tf.net 0.10x.x aligns with TensorFlow v2.10.x native library. + tf.net 0.11x.x aligns with TensorFlow v2.11.x native library. + tf.net 0.15x.x aligns with TensorFlow v2.15.x native library. - 0.100.5.0 + 0.150.0.0 LICENSE true packages true - Open.snk AnyCPU;x64 TensorFlow.NET Debug;Release;GPU @@ -84,6 +93,66 @@ https://tensorflownet.readthedocs.io + + 1 + $(NoWarn),1570,1573,1591,1712,8603,8604,8625,CS0612 + + + + 1 + $(NoWarn),1570,1573,1591,1712,8603,8604,8625,CS0612 + + + + 1 + $(NoWarn),1570,1573,1591,1712,8603,8604,8625,CS0612 + + + + 1 + $(NoWarn),1570,1573,1591,1712,8603,8604,8625,CS0612 + + + + 1 + $(NoWarn),1570,1573,1591,1712,8603,8604,8625,CS0612 + + + + 1 + $(NoWarn),1570,1573,1591,1712,8603,8604,8625,CS0612 + + + + 1 + $(NoWarn),1570,1573,1591,1712,8603,8604,8625,CS0612 + + + + 1 + $(NoWarn),1570,1573,1591,1712,8603,8604,8625,CS0612 + + + + 1 + $(NoWarn),1570,1573,1591,1712,8603,8604,8625,CS0612 + + + + 1 + $(NoWarn),1570,1573,1591,1712,8603,8604,8625,CS0612 + + + + 1 + $(NoWarn),1570,1573,1591,1712,8603,8604,8625,CS0612 + + + + 1 + $(NoWarn),1570,1573,1591,1712,8603,8604,8625,CS0612 + + @@ -108,10 +177,16 @@ https://tensorflownet.readthedocs.io - + - - + + + + + + + + diff --git a/src/TensorFlowNET.Core/Tensors/Ragged/RaggedTensor.cs b/src/TensorFlowNET.Core/Tensors/Ragged/RaggedTensor.cs index 4f85e1081..0f09d4128 100644 --- a/src/TensorFlowNET.Core/Tensors/Ragged/RaggedTensor.cs +++ b/src/TensorFlowNET.Core/Tensors/Ragged/RaggedTensor.cs @@ -163,5 +163,38 @@ public static implicit operator RaggedTensor(Tensor tensor) { return tensor.Tag as RaggedTensor; } + public Tensor nrows(TF_DataType out_type, string name = null) + { + tf_with(ops.name_scope(name, "RaggedNRows"), scope => + { + return math_ops.cast(this._row_partition.nrows(), dtype: out_type); + }); + return null; + } + public RaggedTensor row_lengths(int axis=-1, string name=null) + { + if (axis == 0) return this._row_partition.nrows(); + if (axis == 1) return this._row_partition.row_lengths(); + var values = (RaggedTensor)this._values; + axis = array_ops.get_positive_axis( + axis, this.shape.rank, ndims_name: "rank(this)"); + if (axis == 0) return this.nrows(this._row_partition.GetDataType()); + else if (axis == 1) + { + var splits = this._row_partition.row_splits; + return splits[new Slice(start: 1)] - splits[new Slice(stop: -1)]; + + } + else if (this._values is RaggedTensor) + { + return values.row_lengths(axis - 1); + } + else + { + var shape = array_ops.shape(values, out_type: this._row_partition.GetDataType()); + return array_ops.ones(shape[new Slice(stop:axis - 1)], this._row_partition.GetDataType()) * + shape[axis - 1]; + } + } } } diff --git a/src/TensorFlowNET.Core/Tensors/Ragged/RowPartition.cs b/src/TensorFlowNET.Core/Tensors/Ragged/RowPartition.cs index b1dbf5864..9e242ff38 100644 --- a/src/TensorFlowNET.Core/Tensors/Ragged/RowPartition.cs +++ b/src/TensorFlowNET.Core/Tensors/Ragged/RowPartition.cs @@ -14,10 +14,15 @@ You may obtain a copy of the License at limitations under the License. ******************************************************************************/ +using Serilog.Debugging; using System; +using System.Collections.Concurrent; using System.Collections.Generic; +//using System.ComponentModel.DataAnnotations; using System.Text; +using System.Xml.Linq; using Tensorflow.Framework; +using Tensorflow.NumPy; using static Tensorflow.Binding; namespace Tensorflow @@ -78,7 +83,7 @@ public static RowPartition from_value_rowids(Tensor value_rowids, minlength: nrows_int32, maxlength: nrows_int32, dtype: value_rowids.dtype); - var row_splits = array_ops.concat(new object[] + var row_splits = array_ops.concat(new Tensor[] { ops.convert_to_tensor(new long[] { 0 }), tf.cumsum(row_lengths) @@ -99,5 +104,55 @@ public static RowPartition from_row_splits(Tensor row_splits, return new RowPartition(row_splits); }); } + + public static RowPartition from_row_lengths(Tensor row_lengths, + bool validate=true, + TF_DataType dtype = TF_DataType.TF_INT32, + TF_DataType dtype_hint= TF_DataType.TF_INT32) + { + row_lengths = _convert_row_partition( + row_lengths, "row_lengths", dtype_hint: dtype_hint, dtype: dtype); + Tensor row_limits = math_ops.cumsum(row_lengths, tf.constant(-1)); + Tensor row_splits = array_ops.concat(new Tensor[] { tf.convert_to_tensor(np.array(new int[] { 0 }, TF_DataType.TF_INT64)), row_limits }, axis:0); + return new RowPartition(row_splits: row_splits, row_lengths: row_lengths); + } + + public static Tensor _convert_row_partition(Tensor partition, string name, TF_DataType dtype, + TF_DataType dtype_hint= TF_DataType.TF_INT64) + { + if (partition is NDArray && partition.GetDataType() == np.int32) partition = ops.convert_to_tensor(partition, name: name); + if (partition.GetDataType() != np.int32 && partition.GetDataType() != np.int64) throw new ValueError($"{name} must have dtype int32 or int64"); + return partition; + } + + public Tensor nrows() + { + /*Returns the number of rows created by this `RowPartition*/ + if (this._nrows != null) return this._nrows; + var nsplits = tensor_shape.dimension_at_index(this._row_splits.shape, 0); + if (nsplits == null) return array_ops.shape(this._row_splits, out_type: this.row_splits.dtype)[0] - 1; + else return constant_op.constant(nsplits.value - 1, dtype: this.row_splits.dtype); + } + + public Tensor row_lengths() + { + + if (this._row_splits != null) + { + int nrows_plus_one = tensor_shape.dimension_value(this._row_splits.shape[0]); + return tf.constant(nrows_plus_one - 1); + + } + if (this._row_lengths != null) + { + var nrows = tensor_shape.dimension_value(this._row_lengths.shape[0]); + return tf.constant(nrows); + } + if(this._nrows != null) + { + return tensor_util.constant_value(this._nrows); + } + return tf.constant(-1); + } } } diff --git a/src/TensorFlowNET.Core/Tensors/Tensor.Conversions.cs b/src/TensorFlowNET.Core/Tensors/Tensor.Conversions.cs index 18bdc1aaf..fdd62aeed 100644 --- a/src/TensorFlowNET.Core/Tensors/Tensor.Conversions.cs +++ b/src/TensorFlowNET.Core/Tensors/Tensor.Conversions.cs @@ -14,19 +14,10 @@ You may obtain a copy of the License at limitations under the License. ******************************************************************************/ -using Tensorflow.NumPy; -using System; -using System.Diagnostics.CodeAnalysis; -using System.Text; -using Tensorflow.Framework.Models; -using static Tensorflow.Binding; +namespace Tensorflow; -namespace Tensorflow +public partial class Tensor { - [SuppressMessage("ReSharper", "InvokeAsExtensionMethod")] - public partial class Tensor - { - public TensorSpec ToTensorSpec() - => new TensorSpec(shape, dtype, name); - } + public TensorSpec ToTensorSpec() + => new TensorSpec(shape, dtype, name); } \ No newline at end of file diff --git a/src/TensorFlowNET.Core/Tensors/Tensor.Creation.cs b/src/TensorFlowNET.Core/Tensors/Tensor.Creation.cs index 498ffda76..e7ff9f748 100644 --- a/src/TensorFlowNET.Core/Tensors/Tensor.Creation.cs +++ b/src/TensorFlowNET.Core/Tensors/Tensor.Creation.cs @@ -105,6 +105,13 @@ public Tensor(Operation op, int value_index, TF_DataType dtype) _id = ops.uid(); } + internal static Tensor _create_with_tf_output(Operation op, int value_index, TF_DataType dtype, TF_Output tf_output) + { + Tensor ret = new Tensor(op, value_index, dtype); + ret._tf_output = tf_output; + return ret; + } + protected unsafe void InitTensor(Shape shape, TF_DataType dtype) { _handle = TF_NewTensor(shape, dtype, null); diff --git a/src/TensorFlowNET.Core/Tensors/Tensor.Index.cs b/src/TensorFlowNET.Core/Tensors/Tensor.Index.cs index c8f47825c..51062cf3b 100644 --- a/src/TensorFlowNET.Core/Tensors/Tensor.Index.cs +++ b/src/TensorFlowNET.Core/Tensors/Tensor.Index.cs @@ -42,7 +42,7 @@ public Tensor this[params Slice[] slices] array_ops.stack(args.End), array_ops.stack(args.Strides)); - return gen_array_ops.strided_slice( + return array_ops.strided_slice( this, packed_begin, packed_end, @@ -180,8 +180,7 @@ public Tensor slice(int start) array_ops.stack(end.ToArray()), array_ops.stack(strides.ToArray())); - return gen_array_ops.strided_slice( - this, + return array_ops.strided_slice(this, packed_begin, packed_end, packed_strides, diff --git a/src/TensorFlowNET.Core/APIs/tf.exp.cs b/src/TensorFlowNET.Core/Tensors/Tensor.Keras.cs similarity index 75% rename from src/TensorFlowNET.Core/APIs/tf.exp.cs rename to src/TensorFlowNET.Core/Tensors/Tensor.Keras.cs index 56ea1898e..ca946ca48 100644 --- a/src/TensorFlowNET.Core/APIs/tf.exp.cs +++ b/src/TensorFlowNET.Core/Tensors/Tensor.Keras.cs @@ -1,6 +1,6 @@ /***************************************************************************** Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. - + Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at @@ -14,12 +14,14 @@ You may obtain a copy of the License at limitations under the License. ******************************************************************************/ -namespace Tensorflow +namespace Tensorflow; + +public partial class Tensor { - public partial class tensorflow - { - public Tensor exp(Tensor x, - string name = null) => gen_math_ops.exp(x, name); + public bool IsFromKerasTensor { get; set; } - } -} + /// + /// Keras History: (Layer, (node_index, tensor_index)) + /// + public KerasHistory KerasHistory { get; set; } +} \ No newline at end of file diff --git a/src/TensorFlowNET.Core/Tensors/Tensor.Operators.cs b/src/TensorFlowNET.Core/Tensors/Tensor.Operators.cs index ef71be2c0..c7a631d8b 100644 --- a/src/TensorFlowNET.Core/Tensors/Tensor.Operators.cs +++ b/src/TensorFlowNET.Core/Tensors/Tensor.Operators.cs @@ -154,103 +154,103 @@ public partial class Tensor public static Tensor operator >(Tensor lhs, Tensor rhs) => gen_math_ops.greater(lhs, rhs); public static Tensor operator >(Tensor lhs, NDArray rhs) => gen_math_ops.greater(lhs, rhs); public static Tensor operator >(NDArray lhs, Tensor rhs) => gen_math_ops.greater(lhs, rhs); - public static Tensor operator >(Tensor lhs, sbyte rhs) => gen_math_ops.greater(lhs, rhs); - public static Tensor operator >(sbyte lhs, Tensor rhs) => gen_math_ops.greater(lhs, rhs); - public static Tensor operator >(Tensor lhs, byte rhs) => gen_math_ops.greater(lhs, rhs); - public static Tensor operator >(byte lhs, Tensor rhs) => gen_math_ops.greater(lhs, rhs); - public static Tensor operator >(Tensor lhs, short rhs) => gen_math_ops.greater(lhs, rhs); - public static Tensor operator >(short lhs, Tensor rhs) => gen_math_ops.greater(lhs, rhs); - public static Tensor operator >(Tensor lhs, ushort rhs) => gen_math_ops.greater(lhs, rhs); - public static Tensor operator >(ushort lhs, Tensor rhs) => gen_math_ops.greater(lhs, rhs); - public static Tensor operator >(Tensor lhs, int rhs) => gen_math_ops.greater(lhs, rhs); - public static Tensor operator >(int lhs, Tensor rhs) => gen_math_ops.greater(lhs, rhs); - public static Tensor operator >(Tensor lhs, uint rhs) => gen_math_ops.greater(lhs, rhs); - public static Tensor operator >(uint lhs, Tensor rhs) => gen_math_ops.greater(lhs, rhs); - public static Tensor operator >(Tensor lhs, ulong rhs) => gen_math_ops.greater(lhs, rhs); - public static Tensor operator >(ulong lhs, Tensor rhs) => gen_math_ops.greater(lhs, rhs); - public static Tensor operator >(Tensor lhs, long rhs) => gen_math_ops.greater(lhs, rhs); - public static Tensor operator >(long lhs, Tensor rhs) => gen_math_ops.greater(lhs, rhs); - public static Tensor operator >(Tensor lhs, float rhs) => gen_math_ops.greater(lhs, rhs); - public static Tensor operator >(float lhs, Tensor rhs) => gen_math_ops.greater(lhs, rhs); - public static Tensor operator >(Tensor lhs, double rhs) => gen_math_ops.greater(lhs, rhs); - public static Tensor operator >(double lhs, Tensor rhs) => gen_math_ops.greater(lhs, rhs); - public static Tensor operator >(Tensor lhs, Complex rhs) => gen_math_ops.greater(lhs, rhs); - public static Tensor operator >(Complex lhs, Tensor rhs) => gen_math_ops.greater(lhs, rhs); + public static Tensor operator >(Tensor lhs, sbyte rhs) => gen_math_ops.greater(lhs, ops.convert_to_tensor(rhs)); + public static Tensor operator >(sbyte lhs, Tensor rhs) => gen_math_ops.greater(ops.convert_to_tensor(lhs), rhs); + public static Tensor operator >(Tensor lhs, byte rhs) => gen_math_ops.greater(lhs, ops.convert_to_tensor(rhs)); + public static Tensor operator >(byte lhs, Tensor rhs) => gen_math_ops.greater(ops.convert_to_tensor(lhs), rhs); + public static Tensor operator >(Tensor lhs, short rhs) => gen_math_ops.greater(lhs, ops.convert_to_tensor(rhs)); + public static Tensor operator >(short lhs, Tensor rhs) => gen_math_ops.greater(ops.convert_to_tensor(lhs), rhs); + public static Tensor operator >(Tensor lhs, ushort rhs) => gen_math_ops.greater(lhs, ops.convert_to_tensor(rhs)); + public static Tensor operator >(ushort lhs, Tensor rhs) => gen_math_ops.greater(ops.convert_to_tensor(lhs), rhs); + public static Tensor operator >(Tensor lhs, int rhs) => gen_math_ops.greater(lhs, ops.convert_to_tensor(rhs)); + public static Tensor operator >(int lhs, Tensor rhs) => gen_math_ops.greater(ops.convert_to_tensor(lhs), rhs); + public static Tensor operator >(Tensor lhs, uint rhs) => gen_math_ops.greater(lhs, ops.convert_to_tensor(rhs)); + public static Tensor operator >(uint lhs, Tensor rhs) => gen_math_ops.greater(ops.convert_to_tensor(lhs), rhs); + public static Tensor operator >(Tensor lhs, ulong rhs) => gen_math_ops.greater(lhs, ops.convert_to_tensor(rhs)); + public static Tensor operator >(ulong lhs, Tensor rhs) => gen_math_ops.greater(ops.convert_to_tensor(lhs), ops.convert_to_tensor(rhs)); + public static Tensor operator >(Tensor lhs, long rhs) => gen_math_ops.greater(lhs, ops.convert_to_tensor(rhs)); + public static Tensor operator >(long lhs, Tensor rhs) => gen_math_ops.greater(ops.convert_to_tensor(lhs), rhs); + public static Tensor operator >(Tensor lhs, float rhs) => gen_math_ops.greater(lhs, ops.convert_to_tensor(rhs)); + public static Tensor operator >(float lhs, Tensor rhs) => gen_math_ops.greater(ops.convert_to_tensor(lhs), rhs); + public static Tensor operator >(Tensor lhs, double rhs) => gen_math_ops.greater(lhs, ops.convert_to_tensor(rhs)); + public static Tensor operator >(double lhs, Tensor rhs) => gen_math_ops.greater(ops.convert_to_tensor(lhs), rhs); + public static Tensor operator >(Tensor lhs, Complex rhs) => gen_math_ops.greater(lhs, ops.convert_to_tensor(rhs)); + public static Tensor operator >(Complex lhs, Tensor rhs) => gen_math_ops.greater(ops.convert_to_tensor(lhs), rhs); public static Tensor operator <(Tensor lhs, Tensor rhs) => gen_math_ops.less(lhs, rhs); public static Tensor operator <(Tensor lhs, NDArray rhs) => gen_math_ops.less(lhs, rhs); public static Tensor operator <(NDArray lhs, Tensor rhs) => gen_math_ops.less(lhs, rhs); - public static Tensor operator <(Tensor lhs, sbyte rhs) => gen_math_ops.less(lhs, rhs); - public static Tensor operator <(sbyte lhs, Tensor rhs) => gen_math_ops.less(lhs, rhs); - public static Tensor operator <(Tensor lhs, byte rhs) => gen_math_ops.less(lhs, rhs); - public static Tensor operator <(byte lhs, Tensor rhs) => gen_math_ops.less(lhs, rhs); - public static Tensor operator <(Tensor lhs, short rhs) => gen_math_ops.less(lhs, rhs); - public static Tensor operator <(short lhs, Tensor rhs) => gen_math_ops.less(lhs, rhs); - public static Tensor operator <(Tensor lhs, ushort rhs) => gen_math_ops.less(lhs, rhs); - public static Tensor operator <(ushort lhs, Tensor rhs) => gen_math_ops.less(lhs, rhs); - public static Tensor operator <(Tensor lhs, int rhs) => gen_math_ops.less(lhs, rhs); - public static Tensor operator <(int lhs, Tensor rhs) => gen_math_ops.less(lhs, rhs); - public static Tensor operator <(Tensor lhs, uint rhs) => gen_math_ops.less(lhs, rhs); - public static Tensor operator <(uint lhs, Tensor rhs) => gen_math_ops.less(lhs, rhs); - public static Tensor operator <(Tensor lhs, ulong rhs) => gen_math_ops.less(lhs, rhs); - public static Tensor operator <(ulong lhs, Tensor rhs) => gen_math_ops.less(lhs, rhs); - public static Tensor operator <(Tensor lhs, long rhs) => gen_math_ops.less(lhs, rhs); - public static Tensor operator <(long lhs, Tensor rhs) => gen_math_ops.less(lhs, rhs); - public static Tensor operator <(Tensor lhs, float rhs) => gen_math_ops.less(lhs, rhs); - public static Tensor operator <(float lhs, Tensor rhs) => gen_math_ops.less(lhs, rhs); - public static Tensor operator <(Tensor lhs, double rhs) => gen_math_ops.less(lhs, rhs); - public static Tensor operator <(double lhs, Tensor rhs) => gen_math_ops.less(lhs, rhs); - public static Tensor operator <(Tensor lhs, Complex rhs) => gen_math_ops.less(lhs, rhs); - public static Tensor operator <(Complex lhs, Tensor rhs) => gen_math_ops.less(lhs, rhs); + public static Tensor operator <(Tensor lhs, sbyte rhs) => gen_math_ops.less(lhs, ops.convert_to_tensor(rhs)); + public static Tensor operator <(sbyte lhs, Tensor rhs) => gen_math_ops.less(ops.convert_to_tensor(lhs), rhs); + public static Tensor operator <(Tensor lhs, byte rhs) => gen_math_ops.less(lhs, ops.convert_to_tensor(rhs)); + public static Tensor operator <(byte lhs, Tensor rhs) => gen_math_ops.less(ops.convert_to_tensor(lhs), rhs); + public static Tensor operator <(Tensor lhs, short rhs) => gen_math_ops.less(lhs, ops.convert_to_tensor(rhs)); + public static Tensor operator <(short lhs, Tensor rhs) => gen_math_ops.less(ops.convert_to_tensor(lhs), rhs); + public static Tensor operator <(Tensor lhs, ushort rhs) => gen_math_ops.less(lhs, ops.convert_to_tensor(rhs)); + public static Tensor operator <(ushort lhs, Tensor rhs) => gen_math_ops.less(ops.convert_to_tensor(lhs), rhs); + public static Tensor operator <(Tensor lhs, int rhs) => gen_math_ops.less(lhs, ops.convert_to_tensor(rhs)); + public static Tensor operator <(int lhs, Tensor rhs) => gen_math_ops.less(ops.convert_to_tensor(lhs), rhs); + public static Tensor operator <(Tensor lhs, uint rhs) => gen_math_ops.less(lhs, ops.convert_to_tensor(rhs)); + public static Tensor operator <(uint lhs, Tensor rhs) => gen_math_ops.less(ops.convert_to_tensor(lhs), rhs); + public static Tensor operator <(Tensor lhs, ulong rhs) => gen_math_ops.less(lhs, ops.convert_to_tensor(rhs)); + public static Tensor operator <(ulong lhs, Tensor rhs) => gen_math_ops.less(ops.convert_to_tensor(lhs), rhs); + public static Tensor operator <(Tensor lhs, long rhs) => gen_math_ops.less(lhs, ops.convert_to_tensor(rhs)); + public static Tensor operator <(long lhs, Tensor rhs) => gen_math_ops.less(ops.convert_to_tensor(lhs), rhs); + public static Tensor operator <(Tensor lhs, float rhs) => gen_math_ops.less(lhs, ops.convert_to_tensor(rhs)); + public static Tensor operator <(float lhs, Tensor rhs) => gen_math_ops.less(ops.convert_to_tensor(lhs), rhs); + public static Tensor operator <(Tensor lhs, double rhs) => gen_math_ops.less(lhs, ops.convert_to_tensor(rhs)); + public static Tensor operator <(double lhs, Tensor rhs) => gen_math_ops.less(ops.convert_to_tensor(lhs), rhs); + public static Tensor operator <(Tensor lhs, Complex rhs) => gen_math_ops.less(lhs, ops.convert_to_tensor(rhs)); + public static Tensor operator <(Complex lhs, Tensor rhs) => gen_math_ops.less(ops.convert_to_tensor(lhs), rhs); public static Tensor operator >=(Tensor lhs, Tensor rhs) => gen_math_ops.greater_equal(lhs, rhs); public static Tensor operator >=(Tensor lhs, NDArray rhs) => gen_math_ops.greater_equal(lhs, rhs); public static Tensor operator >=(NDArray lhs, Tensor rhs) => gen_math_ops.greater_equal(lhs, rhs); - public static Tensor operator >=(Tensor lhs, sbyte rhs) => gen_math_ops.greater_equal(lhs, rhs); - public static Tensor operator >=(sbyte lhs, Tensor rhs) => gen_math_ops.greater_equal(lhs, rhs); - public static Tensor operator >=(Tensor lhs, byte rhs) => gen_math_ops.greater_equal(lhs, rhs); - public static Tensor operator >=(byte lhs, Tensor rhs) => gen_math_ops.greater_equal(lhs, rhs); - public static Tensor operator >=(Tensor lhs, short rhs) => gen_math_ops.greater_equal(lhs, rhs); - public static Tensor operator >=(short lhs, Tensor rhs) => gen_math_ops.greater_equal(lhs, rhs); - public static Tensor operator >=(Tensor lhs, ushort rhs) => gen_math_ops.greater_equal(lhs, rhs); - public static Tensor operator >=(ushort lhs, Tensor rhs) => gen_math_ops.greater_equal(lhs, rhs); - public static Tensor operator >=(Tensor lhs, int rhs) => gen_math_ops.greater_equal(lhs, rhs); - public static Tensor operator >=(int lhs, Tensor rhs) => gen_math_ops.greater_equal(lhs, rhs); - public static Tensor operator >=(Tensor lhs, uint rhs) => gen_math_ops.greater_equal(lhs, rhs); - public static Tensor operator >=(uint lhs, Tensor rhs) => gen_math_ops.greater_equal(lhs, rhs); - public static Tensor operator >=(Tensor lhs, ulong rhs) => gen_math_ops.greater_equal(lhs, rhs); - public static Tensor operator >=(ulong lhs, Tensor rhs) => gen_math_ops.greater_equal(lhs, rhs); - public static Tensor operator >=(Tensor lhs, long rhs) => gen_math_ops.greater_equal(lhs, rhs); - public static Tensor operator >=(long lhs, Tensor rhs) => gen_math_ops.greater_equal(lhs, rhs); - public static Tensor operator >=(Tensor lhs, float rhs) => gen_math_ops.greater_equal(lhs, rhs); - public static Tensor operator >=(float lhs, Tensor rhs) => gen_math_ops.greater_equal(lhs, rhs); - public static Tensor operator >=(Tensor lhs, double rhs) => gen_math_ops.greater_equal(lhs, rhs); - public static Tensor operator >=(double lhs, Tensor rhs) => gen_math_ops.greater_equal(lhs, rhs); - public static Tensor operator >=(Tensor lhs, Complex rhs) => gen_math_ops.greater_equal(lhs, rhs); - public static Tensor operator >=(Complex lhs, Tensor rhs) => gen_math_ops.greater_equal(lhs, rhs); + public static Tensor operator >=(Tensor lhs, sbyte rhs) => gen_math_ops.greater_equal(lhs, ops.convert_to_tensor(rhs)); + public static Tensor operator >=(sbyte lhs, Tensor rhs) => gen_math_ops.greater_equal(ops.convert_to_tensor(lhs), rhs); + public static Tensor operator >=(Tensor lhs, byte rhs) => gen_math_ops.greater_equal(lhs, ops.convert_to_tensor(rhs)); + public static Tensor operator >=(byte lhs, Tensor rhs) => gen_math_ops.greater_equal(ops.convert_to_tensor(lhs), rhs); + public static Tensor operator >=(Tensor lhs, short rhs) => gen_math_ops.greater_equal(lhs, ops.convert_to_tensor(rhs)); + public static Tensor operator >=(short lhs, Tensor rhs) => gen_math_ops.greater_equal(ops.convert_to_tensor(lhs), rhs); + public static Tensor operator >=(Tensor lhs, ushort rhs) => gen_math_ops.greater_equal(lhs, ops.convert_to_tensor(rhs)); + public static Tensor operator >=(ushort lhs, Tensor rhs) => gen_math_ops.greater_equal(ops.convert_to_tensor(lhs), rhs); + public static Tensor operator >=(Tensor lhs, int rhs) => gen_math_ops.greater_equal(lhs, ops.convert_to_tensor(rhs)); + public static Tensor operator >=(int lhs, Tensor rhs) => gen_math_ops.greater_equal(ops.convert_to_tensor(lhs), rhs); + public static Tensor operator >=(Tensor lhs, uint rhs) => gen_math_ops.greater_equal(lhs, ops.convert_to_tensor(rhs)); + public static Tensor operator >=(uint lhs, Tensor rhs) => gen_math_ops.greater_equal(ops.convert_to_tensor(lhs), rhs); + public static Tensor operator >=(Tensor lhs, ulong rhs) => gen_math_ops.greater_equal(lhs, ops.convert_to_tensor(rhs)); + public static Tensor operator >=(ulong lhs, Tensor rhs) => gen_math_ops.greater_equal(ops.convert_to_tensor(lhs), rhs); + public static Tensor operator >=(Tensor lhs, long rhs) => gen_math_ops.greater_equal(lhs, ops.convert_to_tensor(rhs)); + public static Tensor operator >=(long lhs, Tensor rhs) => gen_math_ops.greater_equal(ops.convert_to_tensor(lhs), rhs); + public static Tensor operator >=(Tensor lhs, float rhs) => gen_math_ops.greater_equal(lhs, ops.convert_to_tensor(rhs)); + public static Tensor operator >=(float lhs, Tensor rhs) => gen_math_ops.greater_equal(ops.convert_to_tensor(lhs), rhs); + public static Tensor operator >=(Tensor lhs, double rhs) => gen_math_ops.greater_equal(lhs, ops.convert_to_tensor(rhs)); + public static Tensor operator >=(double lhs, Tensor rhs) => gen_math_ops.greater_equal(ops.convert_to_tensor(lhs), rhs); + public static Tensor operator >=(Tensor lhs, Complex rhs) => gen_math_ops.greater_equal(lhs, ops.convert_to_tensor(rhs)); + public static Tensor operator >=(Complex lhs, Tensor rhs) => gen_math_ops.greater_equal(ops.convert_to_tensor(lhs), rhs); public static Tensor operator <=(Tensor lhs, Tensor rhs) => gen_math_ops.less_equal(lhs, rhs); public static Tensor operator <=(Tensor lhs, NDArray rhs) => gen_math_ops.less_equal(lhs, rhs); public static Tensor operator <=(NDArray lhs, Tensor rhs) => gen_math_ops.less_equal(lhs, rhs); - public static Tensor operator <=(Tensor lhs, sbyte rhs) => gen_math_ops.less_equal(lhs, rhs); - public static Tensor operator <=(sbyte lhs, Tensor rhs) => gen_math_ops.less_equal(lhs, rhs); - public static Tensor operator <=(Tensor lhs, byte rhs) => gen_math_ops.less_equal(lhs, rhs); - public static Tensor operator <=(byte lhs, Tensor rhs) => gen_math_ops.less_equal(lhs, rhs); - public static Tensor operator <=(Tensor lhs, short rhs) => gen_math_ops.less_equal(lhs, rhs); - public static Tensor operator <=(short lhs, Tensor rhs) => gen_math_ops.less_equal(lhs, rhs); - public static Tensor operator <=(Tensor lhs, ushort rhs) => gen_math_ops.less_equal(lhs, rhs); - public static Tensor operator <=(ushort lhs, Tensor rhs) => gen_math_ops.less_equal(lhs, rhs); - public static Tensor operator <=(Tensor lhs, int rhs) => gen_math_ops.less_equal(lhs, rhs); - public static Tensor operator <=(int lhs, Tensor rhs) => gen_math_ops.less_equal(lhs, rhs); - public static Tensor operator <=(Tensor lhs, uint rhs) => gen_math_ops.less_equal(lhs, rhs); - public static Tensor operator <=(uint lhs, Tensor rhs) => gen_math_ops.less_equal(lhs, rhs); - public static Tensor operator <=(Tensor lhs, ulong rhs) => gen_math_ops.less_equal(lhs, rhs); - public static Tensor operator <=(ulong lhs, Tensor rhs) => gen_math_ops.less_equal(lhs, rhs); - public static Tensor operator <=(Tensor lhs, long rhs) => gen_math_ops.less_equal(lhs, rhs); - public static Tensor operator <=(long lhs, Tensor rhs) => gen_math_ops.less_equal(lhs, rhs); - public static Tensor operator <=(Tensor lhs, float rhs) => gen_math_ops.less_equal(lhs, rhs); - public static Tensor operator <=(float lhs, Tensor rhs) => gen_math_ops.less_equal(lhs, rhs); - public static Tensor operator <=(Tensor lhs, double rhs) => gen_math_ops.less_equal(lhs, rhs); - public static Tensor operator <=(double lhs, Tensor rhs) => gen_math_ops.less_equal(lhs, rhs); - public static Tensor operator <=(Tensor lhs, Complex rhs) => gen_math_ops.less_equal(lhs, rhs); - public static Tensor operator <=(Complex lhs, Tensor rhs) => gen_math_ops.less_equal(lhs, rhs); + public static Tensor operator <=(Tensor lhs, sbyte rhs) => gen_math_ops.less_equal(lhs, ops.convert_to_tensor(rhs)); + public static Tensor operator <=(sbyte lhs, Tensor rhs) => gen_math_ops.less_equal(ops.convert_to_tensor(lhs), rhs); + public static Tensor operator <=(Tensor lhs, byte rhs) => gen_math_ops.less_equal(lhs, ops.convert_to_tensor(rhs)); + public static Tensor operator <=(byte lhs, Tensor rhs) => gen_math_ops.less_equal(ops.convert_to_tensor(lhs), rhs); + public static Tensor operator <=(Tensor lhs, short rhs) => gen_math_ops.less_equal(lhs, ops.convert_to_tensor(rhs)); + public static Tensor operator <=(short lhs, Tensor rhs) => gen_math_ops.less_equal(ops.convert_to_tensor(lhs), rhs); + public static Tensor operator <=(Tensor lhs, ushort rhs) => gen_math_ops.less_equal(lhs, ops.convert_to_tensor(rhs)); + public static Tensor operator <=(ushort lhs, Tensor rhs) => gen_math_ops.less_equal(ops.convert_to_tensor(lhs), rhs); + public static Tensor operator <=(Tensor lhs, int rhs) => gen_math_ops.less_equal(lhs, ops.convert_to_tensor(rhs)); + public static Tensor operator <=(int lhs, Tensor rhs) => gen_math_ops.less_equal(ops.convert_to_tensor(lhs), rhs); + public static Tensor operator <=(Tensor lhs, uint rhs) => gen_math_ops.less_equal(lhs, ops.convert_to_tensor(rhs)); + public static Tensor operator <=(uint lhs, Tensor rhs) => gen_math_ops.less_equal(ops.convert_to_tensor(lhs), rhs); + public static Tensor operator <=(Tensor lhs, ulong rhs) => gen_math_ops.less_equal(lhs, ops.convert_to_tensor(rhs)); + public static Tensor operator <=(ulong lhs, Tensor rhs) => gen_math_ops.less_equal(ops.convert_to_tensor(lhs), rhs); + public static Tensor operator <=(Tensor lhs, long rhs) => gen_math_ops.less_equal(lhs, ops.convert_to_tensor(rhs)); + public static Tensor operator <=(long lhs, Tensor rhs) => gen_math_ops.less_equal(ops.convert_to_tensor(lhs), rhs); + public static Tensor operator <=(Tensor lhs, float rhs) => gen_math_ops.less_equal(lhs, ops.convert_to_tensor(rhs)); + public static Tensor operator <=(float lhs, Tensor rhs) => gen_math_ops.less_equal(ops.convert_to_tensor(lhs), rhs); + public static Tensor operator <=(Tensor lhs, double rhs) => gen_math_ops.less_equal(lhs, ops.convert_to_tensor(rhs)); + public static Tensor operator <=(double lhs, Tensor rhs) => gen_math_ops.less_equal(ops.convert_to_tensor(lhs), rhs); + public static Tensor operator <=(Tensor lhs, Complex rhs) => gen_math_ops.less_equal(lhs, ops.convert_to_tensor(rhs)); + public static Tensor operator <=(Complex lhs, Tensor rhs) => gen_math_ops.less_equal(ops.convert_to_tensor(lhs), rhs); public static Tensor operator -(Tensor x) => gen_math_ops.neg(x); diff --git a/src/TensorFlowNET.Core/Tensors/Tensor.cs b/src/TensorFlowNET.Core/Tensors/Tensor.cs index c0e5d4357..65e1c8576 100644 --- a/src/TensorFlowNET.Core/Tensors/Tensor.cs +++ b/src/TensorFlowNET.Core/Tensors/Tensor.cs @@ -146,11 +146,6 @@ public int[] _shape_tuple() return rank < 0 ? null : shape.dims.Select(x => (int)x).ToArray(); } - /// - /// Keras History: (Layer, (node_index, tensor_index)) - /// - public KerasHistory KerasHistory { get; set; } - /// /// Updates the shape of this tensor. /// diff --git a/src/TensorFlowNET.Core/Tensors/TensorArray.cs b/src/TensorFlowNET.Core/Tensors/TensorArray.cs index fb59593ce..ff74956ac 100644 --- a/src/TensorFlowNET.Core/Tensors/TensorArray.cs +++ b/src/TensorFlowNET.Core/Tensors/TensorArray.cs @@ -14,7 +14,9 @@ You may obtain a copy of the License at limitations under the License. ******************************************************************************/ +using Tensorflow.Common.Types; using Tensorflow.Operations; +using static Tensorflow.Binding; namespace Tensorflow { @@ -44,5 +46,27 @@ public abstract class TensorArray : ITensorOrTensorArray public abstract Tensor stack(string name = null); public abstract Tensor gather(Tensor indices, string name = null); + + internal bool _dynamic_size; + internal Tensor _size; + internal List _colocate_with; + internal Shape _element_shape; + + public static TensorArray Create(TF_DataType dtype, Tensor size = null, bool dynamic_size = false, + bool clear_after_read = true, string tensor_array_name = null, Tensor handle = null, Tensor flow = null, + bool infer_shape = true, Shape? element_shape = null, + bool colocate_with_first_write_call = true, string name = null) + { + if (tf.Context.executing_eagerly() && (flow is null || flow.dtype != dtypes.variant)) + { + return new _EagerTensorArray(dtype, size, dynamic_size, clear_after_read, tensor_array_name, handle, flow, + infer_shape, element_shape, colocate_with_first_write_call, name); + } + else + { + return new _GraphTensorArrayV2(dtype, size, dynamic_size, clear_after_read, tensor_array_name, handle, flow, + infer_shape, element_shape, colocate_with_first_write_call, name); + } + } } } diff --git a/src/TensorFlowNET.Core/Tensors/Tensors.cs b/src/TensorFlowNET.Core/Tensors/Tensors.cs index b98495a32..2838b000d 100644 --- a/src/TensorFlowNET.Core/Tensors/Tensors.cs +++ b/src/TensorFlowNET.Core/Tensors/Tensors.cs @@ -3,6 +3,9 @@ using System.Collections; using System.Collections.Generic; using System.Linq; +using Tensorflow.Common.Types; +using Tensorflow.Operations; +using Tensorflow.Common.Extensions; namespace Tensorflow { @@ -13,154 +16,278 @@ namespace Tensorflow /// and Tensor[] from Tensors implicitily. /// It works for tuple and scalar as well. /// - public class Tensors : IEnumerable, IDisposable + public sealed class Tensors : Nest, IDisposable { - List items = new List(); - - public TF_DataType dtype => items.First().dtype; - public Shape shape => items.First().shape; - public int rank => items.First().rank; - public Graph graph => items.First().graph; + public TF_DataType dtype => this.First().dtype; + public Shape shape => this.First().shape; + public int rank => this.First().rank; + public Graph graph => this.First().graph; public bool IsList { get; set; } - public int Length => items.Count(); + public int Length => this.Count(); + /// + /// Return a Tensor if `Tensors` has only one tensor, otherwise throw an exception. + /// + public Tensor Single + { + get + { + if (Length != 1) + { + throw new ValueError("Tensors with more than one tensor cannot be " + + "implicitly converted to Tensor."); + } + return this.First(); + } + } - public Tensor this[int index] + /// + /// Return a Tensor if `Tensors` has only one tensor, and return null when `Tensors` is empty, + /// otherwise throw an exception. + /// + public Tensor? SingleOrNull { - get => items[index]; - set => items[index] = value; + get + { + if (Length > 1) + { + throw new ValueError($"Tensors with {Length} tensor cannot be " + + "implicitly converted to Tensor."); + } + return this.FirstOrDefault(); + } } public Tensor this[params string[] slices] - => items.First()[slices]; - public Tensors(params Tensor[] tensors) + => this.First()[slices]; + + internal Tensors(Nest nested) : base(nested) + { + + } + + public Tensors(params Tensor[] tensors): base(DealWithConstructorArrayInput(tensors)) { - items.AddRange(tensors); + } - public Tensors(IEnumerable tensors) + public Tensors(IList tensors) : base(tensors.Select(x => new Nest(x))) { - items.AddRange(tensors); + + } + + public Tensors(NDArray nd): base(ops.convert_to_tensor(nd)) + { + + } + + /// + /// Get the element in shallow level. For example, for ts = [1, [2, 3], 4], + /// common indexer has ts[1] = 2. Shallow indexer has ts[1] = [2, 3] + /// + /// + /// + public Tensors GetShallow(int index) + { + if(NestType == NestType.Node) + { + if(index > 0) + { + throw new IndexOutOfRangeException(); + } + return this; + } + else if(NestType == NestType.List) + { + return ListValue![index].AsNest().ToTensors(); + } + else + { + throw new NotImplementedException(); + } + } + + private static Nest DealWithConstructorArrayInput(Tensor[] tensors) + { + if (tensors.Length == 0) + { + return Nest.Empty; + } + else if(tensors.Length == 1) + { + return new Nest(tensors[0]); + } + else + { + return new Nest(tensors.Select(x => new Nest(x))); + } } - public Tensors(NDArray nd) + public bool IsSingle() { - items.Add(ops.convert_to_tensor(nd)); + return Length == 1; } - public IEnumerator GetEnumerator() + public new Tensors MergeWith(Nest? other) { - foreach (var tensor in items) - yield return tensor; + return FromNest(base.MergeWith(other)); } + [Obsolete("This method is not encouraged to be used. It may be removed in the future. If you do want to add " + + "a tensor to `Tensors`, creating a new instance with your newly added tensor is a better choice.")] public void Add(Tensor tensor) - => items.Add(tensor); + { + if(NestType == NestType.Dictionary) + { + throw new ValueError("Cannot add a tensor to dictionary type of nested tensors."); + } + else if(NestType == NestType.Node) + { + NestType = NestType.List; + ListValue = new() { new Nest(NodeValue), new Nest(tensor) }; + NodeValue = null; + } + else if(NestType == NestType.List) + { + ListValue!.Add(new Nest(tensor)); + } + else //Empty + { + NestType = NestType.Node; + NodeValue = tensor; + } + } + [Obsolete("This method is not encouraged to be used. It may be removed in the future. If you do want to add " + + "some tensors to `Tensors`, creating a new instance with your newly added tensors is a better choice.")] public void AddRange(IEnumerable tensors) - => items.AddRange(tensors); + { + if (NestType == NestType.Dictionary) + { + throw new ValueError("Cannot add a tensor to dictionary type of nested tensors."); + } + else if (NestType == NestType.Node) + { + NestType = NestType.List; + ListValue = new() { new Nest(NodeValue) }; + ListValue.AddRange(tensors.Select(x => new Nest(x))); + NodeValue = null; + } + else if(NestType == NestType.List) + { + ListValue!.AddRange(tensors.Select(x => new Nest(x))); + } + else // empty + { + NestType = NestType.List; + ListValue = tensors.Select(x => new Nest(x) as INestStructure).ToList(); + } + } + [Obsolete("This method is not encouraged to be used. It may be removed in the future. If you do want to insert " + + "a tensor to `Tensors`, creating a new instance with your newly added tensor is a better choice.")] public void Insert(int index, Tensor tensor) - => items.Insert(index, tensor); - - IEnumerator IEnumerable.GetEnumerator() - => GetEnumerator(); + { + if (NestType == NestType.List) + { + ListValue.Insert(index, new Nest(tensor)); + } + else if(NestType == NestType.Node) + { + NestType = NestType.List; + ListValue = new() { new Nest(NodeValue) }; + ListValue.Insert(index, new Nest(tensor)); + NodeValue = null; + } + else + { + throw new ValueError("Cannot add a tensor to dictionary type of nested tensors."); + } + } public string[] StringData() { - EnsureSingleTensor(this, "nnumpy"); - return this[0].StringData(); + return Single.StringData(); } public string StringData(int index) { - EnsureSingleTensor(this, "nnumpy"); - return this[0].StringData(index); + return Single.StringData(index); } public NDArray numpy() { - EnsureSingleTensor(this, "nnumpy"); - return this[0].numpy(); + return Single.numpy(); } + [Obsolete] public T[] ToArray() where T: unmanaged { - EnsureSingleTensor(this, $"ToArray<{typeof(T)}>"); - return this[0].ToArray(); + return Single.ToArray(); } #region Explicit Conversions - public unsafe static explicit operator bool(Tensors tensor) + public static explicit operator bool(Tensors tensor) { - EnsureSingleTensor(tensor, "explicit conversion to bool"); - return (bool)tensor[0]; + return (bool)tensor.Single; } - public unsafe static explicit operator sbyte(Tensors tensor) + public static explicit operator sbyte(Tensors tensor) { - EnsureSingleTensor(tensor, "explicit conversion to sbyte"); - return (sbyte)tensor[0]; + return (sbyte)tensor.Single; } - public unsafe static explicit operator byte(Tensors tensor) + public static explicit operator byte(Tensors tensor) { - EnsureSingleTensor(tensor, "explicit conversion to byte"); - return (byte)tensor[0]; + return (byte)tensor.Single; } - public unsafe static explicit operator ushort(Tensors tensor) + public static explicit operator ushort(Tensors tensor) { - EnsureSingleTensor(tensor, "explicit conversion to ushort"); - return (ushort)tensor[0]; + return (ushort)tensor.Single; } - public unsafe static explicit operator short(Tensors tensor) + public static explicit operator short(Tensors tensor) { - EnsureSingleTensor(tensor, "explicit conversion to short"); - return (short)tensor[0]; + return (short)tensor.Single; } - public unsafe static explicit operator int(Tensors tensor) + public static explicit operator int(Tensors tensor) { - EnsureSingleTensor(tensor, "explicit conversion to int"); - return (int)tensor[0]; + return (int)tensor.Single; } - public unsafe static explicit operator uint(Tensors tensor) + public static explicit operator uint(Tensors tensor) { - EnsureSingleTensor(tensor, "explicit conversion to uint"); - return (uint)tensor[0]; + return (uint)tensor.Single; } - public unsafe static explicit operator long(Tensors tensor) + public static explicit operator long(Tensors tensor) { - EnsureSingleTensor(tensor, "explicit conversion to long"); - return (long)tensor[0]; + return (long)tensor.Single; } - public unsafe static explicit operator ulong(Tensors tensor) + public static explicit operator ulong(Tensors tensor) { - EnsureSingleTensor(tensor, "explicit conversion to ulong"); - return (ulong)tensor[0]; + return (ulong)tensor.Single; } - public unsafe static explicit operator float(Tensors tensor) + public static explicit operator float(Tensors tensor) { - EnsureSingleTensor(tensor, "explicit conversion to byte"); - return (byte)tensor[0]; + return (byte)tensor.Single; } - public unsafe static explicit operator double(Tensors tensor) + public static explicit operator double(Tensors tensor) { - EnsureSingleTensor(tensor, "explicit conversion to double"); - return (double)tensor[0]; + return (double)tensor.Single; } - public unsafe static explicit operator string(Tensors tensor) + public static explicit operator string(Tensors tensor) { - EnsureSingleTensor(tensor, "explicit conversion to string"); - return (string)tensor[0]; + return (string)tensor.Single; } + + public static explicit operator object[](Tensors tensors) + => tensors.Flatten().ToArray(); #endregion #region Implicit Conversions @@ -180,56 +307,44 @@ public static implicit operator Tensors(Tensor[] tensors) public static implicit operator Tensors(List tensors) => new Tensors(tensors.ToArray()); - public static implicit operator Tensor(Tensors tensors) - => tensors.FirstOrDefault(); + public static implicit operator Tensor(Tensors? tensors) + => tensors?.SingleOrNull; public static implicit operator Tensor[](Tensors tensors) - => tensors.items.ToArray(); - + => tensors.Flatten().ToArray(); #endregion - public void Deconstruct(out Tensor a, out Tensor b) + public static Tensors? FromNest(Nest nested) { - a = items[0]; - b = items[1]; + if(nested == Nest.Empty) + { + return null; + } + return new Tensors(nested); } - private static void EnsureSingleTensor(Tensors tensors, string methodnName) + public void Deconstruct(out Tensor a, out Tensors? b) { - if(tensors.Length == 0) - { - throw new ValueError($"Method `{methodnName}` of `Tensors` cannot be used when `Tensors` contains no Tensor."); - } - else if(tensors.Length > 1) - { - throw new ValueError($"Method `{methodnName}` of `Tensors` cannot be used when `Tensors` contains more than one Tensor."); - } + a = this.First(); + b = Length == 1? null : new Tensors(this.Skip(1).ToArray()); } public override string ToString() { - if(items.Count == 1) + if(Length == 1) { - return items[0].ToString(); + return this.First().ToString(); } else { - StringBuilder sb = new StringBuilder(); - sb.Append($"Totally {items.Count} tensors, which are {string.Join(", ", items.Select(x => x.name))}\n[\n"); - for(int i = 0; i < items.Count; i++) - { - var tensor = items[i]; - sb.Append($"Tensor {i}({tensor.name}): {tensor.ToString()}\n"); - } - sb.Append("]\n"); - return sb.ToString(); + return $"Totally {Length} tensors: {base.ToString()}"; } } public void Dispose() { - foreach (var item in items) - item.Dispose(); + foreach (var tensor in this) + tensor.Dispose(); } } } diff --git a/src/TensorFlowNET.Core/Tensors/shape_utils.cs b/src/TensorFlowNET.Core/Tensors/shape_utils.cs index 254cdad89..a77dd34ce 100644 --- a/src/TensorFlowNET.Core/Tensors/shape_utils.cs +++ b/src/TensorFlowNET.Core/Tensors/shape_utils.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using Tensorflow.Eager; using static Tensorflow.Binding; namespace Tensorflow @@ -13,5 +14,31 @@ public static Tensor static_or_dynamic_map_fn(Func fn, Tensor el throw new NotImplementedException(""); } + + public static Shape from_object_array(object[] shape) + { + var dims = shape.Select(x => + { + if (x is KerasTensor kt && kt.inferred_value != null) + { + return kt.inferred_value.as_int_list()[0]; + } + else if (x is EagerTensor et && et.dtype == TF_DataType.TF_INT32) + { + return et.ToArray()[0]; + } + else if (x is int i) + { + return i; + } + else if (x is long l) + { + return l; + } + throw new NotImplementedException(); + }).ToArray(); + + return new Shape(dims); + } } } diff --git a/src/TensorFlowNET.Core/Tensors/tensor_util.cs b/src/TensorFlowNET.Core/Tensors/tensor_util.cs index e65c4850d..6e5024efd 100644 --- a/src/TensorFlowNET.Core/Tensors/tensor_util.cs +++ b/src/TensorFlowNET.Core/Tensors/tensor_util.cs @@ -1,4 +1,4 @@ -/***************************************************************************** +/***************************************************************************** Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); @@ -67,7 +67,7 @@ public static NDArray MakeNdarray(TensorProto tensor) T[] ExpandArrayToSize(IList src) { - if(src.Count == 0) + if (src.Count == 0) { return new T[0]; } @@ -77,7 +77,7 @@ T[] ExpandArrayToSize(IList src) var first_elem = src[0]; var last_elem = src[src.Count - 1]; T[] res = new T[num_elements]; - for(long i = 0; i < num_elements; i++) + for (long i = 0; i < num_elements; i++) { if (i < pre) res[i] = first_elem; else if (i >= num_elements - after) res[i] = last_elem; @@ -121,7 +121,7 @@ T[] ExpandArrayToSize(IList src) $"https://www.tensorflow.org/api_docs/python/tf/dtypes for supported TF dtypes."); } - if(values.size == 0) + if (values.size == 0) { return np.zeros(shape, tensor_dtype); } @@ -135,6 +135,47 @@ T[] ExpandArrayToSize(IList src) TF_DataType.TF_QINT32 }; + private static Array ConvertArray(Array inputArray, Func converter) + { + if (inputArray == null) + throw new ArgumentNullException(nameof(inputArray)); + + var elementType = typeof(TOut); + var lengths = new int[inputArray.Rank]; + for (var i = 0; i < inputArray.Rank; i++) + { + lengths[i] = inputArray.GetLength(i); + } + + var outputArray = Array.CreateInstance(elementType, lengths); + + FillArray(inputArray, outputArray, converter, new int[inputArray.Rank], 0); + + return outputArray; + } + + private static void FillArray(Array inputArray, Array outputArray, Func converter, int[] indices, int dimension) + { + if (dimension == inputArray.Rank - 1) + { + for (int i = 0; i < inputArray.GetLength(dimension); i++) + { + indices[dimension] = i; + var inputValue = (TIn)inputArray.GetValue(indices); + var convertedValue = converter(inputValue); + outputArray.SetValue(convertedValue, indices); + } + } + else + { + for (int i = 0; i < inputArray.GetLength(dimension); i++) + { + indices[dimension] = i; + FillArray(inputArray, outputArray, converter, indices, dimension + 1); + } + } + } + /// /// Create a TensorProto, invoked in graph mode /// @@ -154,22 +195,30 @@ public static TensorProto make_tensor_proto(object values, TF_DataType dtype = T var origin_dtype = values.GetDataType(); if (dtype == TF_DataType.DtInvalid) dtype = origin_dtype; - else if(origin_dtype != dtype) + else if (origin_dtype != dtype) { var new_system_dtype = dtype.as_system_dtype(); - if (values is long[] long_values) + + if (dtype != TF_DataType.TF_STRING && dtype != TF_DataType.TF_VARIANT && dtype != TF_DataType.TF_RESOURCE) { - if (dtype == TF_DataType.TF_INT32) - values = long_values.Select(x => (int)Convert.ChangeType(x, new_system_dtype)).ToArray(); - } - else if (values is double[] double_values) + if (values is Array arrayValues) + { + values = dtype switch + { + TF_DataType.TF_INT32 => ConvertArray(arrayValues, Convert.ToInt32), + TF_DataType.TF_FLOAT => ConvertArray(arrayValues, Convert.ToSingle), + TF_DataType.TF_DOUBLE => ConvertArray(arrayValues, Convert.ToDouble), + _ => values, + }; + } else + { + values = Convert.ChangeType(values, new_system_dtype); + } + + } else { - if (dtype == TF_DataType.TF_FLOAT) - values = double_values.Select(x => (float)Convert.ChangeType(x, new_system_dtype)).ToArray(); - } - else - values = Convert.ChangeType(values, new_system_dtype); + } dtype = values.GetDataType(); } @@ -249,6 +298,9 @@ public static TensorProto make_tensor_proto(object values, TF_DataType dtype = T case sbyte val: tensor_proto.IntVal.AddRange(new[] { (int)val }); break; + case byte val: + tensor_proto.IntVal.AddRange(new[] { (int)val }); + break; case int val: tensor_proto.IntVal.AddRange(new[] { val }); break; @@ -262,7 +314,7 @@ public static TensorProto make_tensor_proto(object values, TF_DataType dtype = T tensor_proto.DoubleVal.AddRange(new[] { val }); break; default: - throw new Exception("make_tensor_proto Not Implemented"); + throw new Exception($"make_tensor_proto Not Implemented {values.GetType().Name}"); } } @@ -284,7 +336,7 @@ bool hasattr(Graph property, string attr) if (tensor is EagerTensor eagerTensor) { - if(tensor.dtype == tf.int64) + if (tensor.dtype == tf.int64) return new Shape(tensor.ToArray()); else return new Shape(tensor.ToArray()); @@ -459,7 +511,7 @@ bool hasattr(Graph property, string attr) var d_ = new int[value.size]; foreach (var (index, d) in enumerate(value.ToArray())) d_[index] = d >= 0 ? d : -1; - + ret = ret.merge_with(new Shape(d_)); } return ret; diff --git a/src/TensorFlowNET.Core/Tensors/tf.constant.cs b/src/TensorFlowNET.Core/Tensors/tf.constant.cs index 6a62d34a5..ac26b3da3 100644 --- a/src/TensorFlowNET.Core/Tensors/tf.constant.cs +++ b/src/TensorFlowNET.Core/Tensors/tf.constant.cs @@ -46,6 +46,9 @@ public Tensor zeros(Tensor shape, TF_DataType dtype = TF_DataType.TF_FLOAT, stri public Tensor ones(Shape shape, TF_DataType dtype = TF_DataType.TF_FLOAT, string name = null) => array_ops.ones(shape, dtype, name); + public Tensor ones(Tensor shape, TF_DataType dtype = TF_DataType.TF_FLOAT, string name = null) + => array_ops.ones(shape, dtype, name); + public Tensor size(Tensor input, string name = null, TF_DataType out_type = TF_DataType.TF_INT32) => array_ops.size(input, diff --git a/src/TensorFlowNET.Core/Training/Saving/BaseSaverBuilder.cs b/src/TensorFlowNET.Core/Training/Saving/BaseSaverBuilder.cs index 10a85d9d9..e16f82c05 100644 --- a/src/TensorFlowNET.Core/Training/Saving/BaseSaverBuilder.cs +++ b/src/TensorFlowNET.Core/Training/Saving/BaseSaverBuilder.cs @@ -106,7 +106,7 @@ public virtual SaverDef _build_internal(IVariableV1[] names_to_saveables, name = scope; // Add a placeholder string tensor for the filename. - var filename_tensor = array_ops.placeholder_with_default(string.IsNullOrEmpty(filename) ? "model" : filename, shape: new int[0], name: "filename"); + var filename_tensor = array_ops.placeholder_with_default(tf.convert_to_tensor(string.IsNullOrEmpty(filename) ? "model" : filename), shape: new int[0], name: "filename"); // Keep the name "Const" for backwards compatibility. filename_tensor = gen_array_ops.placeholder_with_default(filename_tensor, shape: new int[0], name: "Const"); diff --git a/src/TensorFlowNET.Core/Training/Saving/SavedModel/AugmentedGraphView.cs b/src/TensorFlowNET.Core/Training/Saving/SavedModel/AugmentedGraphView.cs index a91933357..9d0b3f001 100644 --- a/src/TensorFlowNET.Core/Training/Saving/SavedModel/AugmentedGraphView.cs +++ b/src/TensorFlowNET.Core/Training/Saving/SavedModel/AugmentedGraphView.cs @@ -88,11 +88,11 @@ private ConcreteFunction maybe_uncache_variable_captures(ConcreteFunction concre public override (IList, IDictionary>) breadth_first_traversal() { - Trackable get_merged_trackable(Trackable x) + void merged_trackable(Trackable x) { // TODO: complete it with new definitions `Asset` and `TrackableConstant`. - return x; } + var trackable_objects = base.breadth_first_traversal(); foreach(var obj in _children_cache.Keys) @@ -100,7 +100,7 @@ Trackable get_merged_trackable(Trackable x) // skip the deletion of cache (maybe do it later). foreach(var pair in _children_cache[obj]) { - _children_cache[obj][pair.Key] = get_merged_trackable(pair.Value); + merged_trackable(pair.Value); } } @@ -109,15 +109,11 @@ Trackable get_merged_trackable(Trackable x) public List<(string, Trackable)> list_dependencies(Trackable obj) { - IDictionary children; - if (!_children_cache.ContainsKey(obj)) + if (!_children_cache.TryGetValue(obj, out var children)) { children= new Dictionary(); } - else - { - children= _children_cache[obj]; - } + List<(string, Trackable)> res = new(); foreach(var pair in obj.deserialization_dependencies(children)) { diff --git a/src/TensorFlowNET.Core/Training/Saving/SavedModel/save.cs b/src/TensorFlowNET.Core/Training/Saving/SavedModel/save.cs index 4313920f5..23e0a9295 100644 --- a/src/TensorFlowNET.Core/Training/Saving/SavedModel/save.cs +++ b/src/TensorFlowNET.Core/Training/Saving/SavedModel/save.cs @@ -88,7 +88,7 @@ private static (MetaGraphDef, Graph, TrackableSaver, AssetInfo, IList { if (ops.inside_function()) { - throw new AssertionError("`tf.saved_model.save` is not supported inside a traced @tf.function. " + + throw new AssertionError("`tf.saved_model.save` is not supported inside a traced [AutoGraph]. " + "Move the call to the outer eagerly-executed context."); } diff --git a/src/TensorFlowNET.Core/Training/Trackable.cs b/src/TensorFlowNET.Core/Training/Trackable.cs index 2b5bf2a72..3eff34875 100644 --- a/src/TensorFlowNET.Core/Training/Trackable.cs +++ b/src/TensorFlowNET.Core/Training/Trackable.cs @@ -179,8 +179,7 @@ protected virtual IVariableV1 _add_variable_with_custom_getter(VariableArgs args // handles slot variables. if (!args.Overwrite || new_variable is RefVariable || new_variable is Trackable) { - var temp = new_variable as Trackable; - var res = _track_trackable(temp, args.Name, args.Overwrite); + var res = _track_trackable(new_variable as Trackable, args.Name, args.Overwrite); Debug.Assert(res is IVariableV1); return res as IVariableV1; } diff --git a/src/TensorFlowNET.Core/Training/gen_training_ops.cs b/src/TensorFlowNET.Core/Training/gen_training_ops.cs index abe85a141..df7dd9e65 100644 --- a/src/TensorFlowNET.Core/Training/gen_training_ops.cs +++ b/src/TensorFlowNET.Core/Training/gen_training_ops.cs @@ -51,5 +51,9 @@ public static Tensor apply_gradient_descent(IVariableV1 var, Tensor alpha, Tenso public static Tensor resource_apply_gradient_descent(Tensor var, Tensor alpha, Tensor delta, bool use_locking = false, string name = null) => tf.Context.ExecuteOp("ResourceApplyGradientDescent", name, new ExecuteOpArgs(var, alpha, delta).SetAttributes(new { use_locking })); + + public static Tensor resource_apply_keras_momentum(Tensor var, Tensor accum, Tensor lr, Tensor grad, Tensor momentum, bool use_locking = false, bool use_nesterov = false, string name = null) + => tf.Context.ExecuteOp("ResourceApplyKerasMomentum", name, + new ExecuteOpArgs(var, accum, lr, grad, momentum).SetAttributes(new { use_locking, use_nesterov })); } } diff --git a/src/TensorFlowNET.Core/Util/Data.cs b/src/TensorFlowNET.Core/Util/Data.cs new file mode 100644 index 000000000..fe3466ed0 --- /dev/null +++ b/src/TensorFlowNET.Core/Util/Data.cs @@ -0,0 +1,78 @@ +using OneOf; +using Tensorflow.NumPy; + +namespace Tensorflow.Util +{ + /// + /// ValidationDataPack is used to pass validation data to fit method. + /// It can recive data which could be A tuple `(x_val, xy_val)` or `(x_val, y_val, sample_weight_val)` of Numpy arrays. + /// + public class ValidationDataPack + { + internal OneOf val_x; + internal NDArray val_y; + internal NDArray val_sample_weight = null; + public bool val_x_is_array = false; + public ValidationDataPack((NDArray, NDArray) validation_data) + { + this.val_x = validation_data.Item1; + this.val_y = validation_data.Item2; + } + + public ValidationDataPack((NDArray, NDArray, NDArray) validation_data) + { + this.val_x = validation_data.Item1; + this.val_y = validation_data.Item2; + this.val_sample_weight = validation_data.Item3; + } + + public ValidationDataPack((IEnumerable, NDArray) validation_data) + { + this.val_x = validation_data.Item1.ToArray(); + this.val_y = validation_data.Item2; + val_x_is_array = true; + } + + public ValidationDataPack((IEnumerable, NDArray, NDArray) validation_data) + { + this.val_x = validation_data.Item1.ToArray(); + this.val_y = validation_data.Item2; + this.val_sample_weight = validation_data.Item3; + val_x_is_array = true; + } + + public static implicit operator ValidationDataPack((NDArray, NDArray) validation_data) + => new ValidationDataPack(validation_data); + + public static implicit operator ValidationDataPack((NDArray, NDArray, NDArray) validation_data) + => new ValidationDataPack(validation_data); + + public static implicit operator ValidationDataPack((IEnumerable, NDArray) validation_data) + => new ValidationDataPack(validation_data); + + public static implicit operator ValidationDataPack((IEnumerable, NDArray, NDArray) validation_data) + => new ValidationDataPack(validation_data); + + public void Deconstruct(out NDArray val_x, out NDArray val_y) + { + val_x = this.val_x.AsT0; + val_y = this.val_y; + } + + public void Deconstruct(out NDArray val_x, out NDArray val_y, out NDArray val_sample_weight) + { + val_x = this.val_x.AsT0; + val_y = this.val_y; + val_sample_weight = this.val_sample_weight; + } + + // add a unuse parameter to make it different from Deconstruct(out NDArray val_x, out NDArray val_y, out NDArray val_sample_weight) + public void Deconstruct(out NDArray[] val_x_array, out NDArray val_y, out NDArray val_sample_weight, out NDArray unuse) + { + val_x_array = this.val_x.AsT1; + val_y = this.val_y; + val_sample_weight = this.val_sample_weight; + unuse = null; + } + } +} diff --git a/src/TensorFlowNET.Core/Util/nest.py.cs b/src/TensorFlowNET.Core/Util/nest.py.cs index eb94f4d05..3ba3ce78b 100644 --- a/src/TensorFlowNET.Core/Util/nest.py.cs +++ b/src/TensorFlowNET.Core/Util/nest.py.cs @@ -36,6 +36,7 @@ namespace Tensorflow.Util // (np.array([3, 4]), tf.constant([3, 4])))` // + [Obsolete] public static class nest { diff --git a/src/TensorFlowNET.Core/Variables/BaseResourceVariable.cs b/src/TensorFlowNET.Core/Variables/BaseResourceVariable.cs index b9a7022a2..a54283bd4 100644 --- a/src/TensorFlowNET.Core/Variables/BaseResourceVariable.cs +++ b/src/TensorFlowNET.Core/Variables/BaseResourceVariable.cs @@ -170,11 +170,28 @@ public IVariableV1 assign_lazy_load(Tensor value, string name = null) public Tensor value() => GraphElement ?? _read_variable_op(); - protected Tensor _read_variable_op() + protected Tensor _read_variable_op(bool no_copy = false) { variable_accessed(this); - var result = gen_resource_variable_ops.read_variable_op(handle, _dtype); - resource_variable_ops._maybe_set_handle_data(_dtype, handle, result); + + Tensor read_and_set_handle(bool no_copy) + { + if (no_copy) + { + gen_resource_variable_ops.disable_copy_on_read(handle); + } + var result = gen_resource_variable_ops.read_variable_op(handle, _dtype); + resource_variable_ops._maybe_set_handle_data(_dtype, handle, result); + return result; + } + + // TODO(Rinne): deal with caching device. + var result = read_and_set_handle(no_copy); + if (!tf.Context.executing_eagerly()) + { + tf.Runner.TFE_TapeSetRecordOperation("ReadVariableOp", new Tensor[] { result }, new Tensor[] { handle }, + backward_function: (x, _) => x); + } // have to set shape when converting to substituent placeholder if (result.shape.ndim == -1) diff --git a/src/TensorFlowNET.Core/Variables/variables.py.cs b/src/TensorFlowNET.Core/Variables/variables.py.cs index 0c07e0243..91f57e292 100644 --- a/src/TensorFlowNET.Core/Variables/variables.py.cs +++ b/src/TensorFlowNET.Core/Variables/variables.py.cs @@ -72,7 +72,9 @@ public static List global_variables(string scope = null) public static Operation variables_initializer(IVariableV1[] var_list, string name = "init") { if (var_list.Length > 0) + { return control_flow_ops.group(var_list.Select(x => x.Initializer).ToArray(), name); + } else return gen_control_flow_ops.no_op(name: name); } @@ -152,10 +154,5 @@ public static Operation _safe_initial_value_from_op(string name, Operation op, D return op; } - - public static Tensor global_variables_initializer() - { - throw new NotImplementedException(); - } } } diff --git a/src/TensorFlowNET.Core/ops.cs b/src/TensorFlowNET.Core/ops.cs index 6d1385ca4..6f51150a2 100644 --- a/src/TensorFlowNET.Core/ops.cs +++ b/src/TensorFlowNET.Core/ops.cs @@ -138,9 +138,22 @@ public static Tensor convert_to_tensor(object value, else { var graph = get_default_graph(); + if (graph is FuncGraph funcGraph) + { + return funcGraph.capture(eager_tensor, name: name); + } if (!graph.building_function) - throw new RuntimeError("Attempting to capture an EagerTensor without building a function."); - return (graph as FuncGraph).capture(eager_tensor, name: name); + { + // throw new RuntimeError("Attempting to capture an EagerTensor without building a function."); + return eager_tensor.AsPlaceholder(name: name); + } + } + } + else if (value is KerasTensor kt) + { + if (kt.inferred_value != null) + { + return convert_to_tensor(kt.inferred_value, dtype: kt.dtype, name: name); } } @@ -565,7 +578,9 @@ public static bool executing_eagerly_outside_functions() if (tf.Context.executing_eagerly()) return true; else - throw new NotImplementedException(""); + // TODO(Wanglongzhi2001), implement the false case + return true; + //throw new NotImplementedException(""); } public static bool inside_function() @@ -575,8 +590,15 @@ public static bool inside_function() public static HandleData get_resource_handle_data(Tensor graph_op) { - var handle_data = c_api.TFC_GetHandleShapeAndType(graph_op.graph.c_graph, graph_op._as_tf_output()); - return HandleData.Parser.ParseFrom(tf.compat.as_bytes(c_api.StringPiece(handle_data))); + var handle_data = c_api.TF_GetHandleShapeAndType(graph_op.graph.c_graph, graph_op._as_tf_output()); + try{ + var handle_str = c_api.ByteStringPiece(handle_data.DangerousGetHandle() == IntPtr.Zero ? null : new Buffer(handle_data)); + return HandleData.Parser.ParseFrom(handle_str); + } + catch(Exception){ + var handle_str = c_api.ByteStringPieceFromNativeString(handle_data.DangerousGetHandle()); + return HandleData.Parser.ParseFrom(handle_str); + } } public static void dismantle_graph(Graph graph) diff --git a/src/TensorFlowNET.Core/tensorflow.cs b/src/TensorFlowNET.Core/tensorflow.cs index 67530ddbd..e368b37cd 100644 --- a/src/TensorFlowNET.Core/tensorflow.cs +++ b/src/TensorFlowNET.Core/tensorflow.cs @@ -14,6 +14,7 @@ You may obtain a copy of the License at limitations under the License. ******************************************************************************/ +using Razorvine.Pickle; using Serilog; using Serilog.Core; using System.Reflection; @@ -22,6 +23,7 @@ limitations under the License. using Tensorflow.Eager; using Tensorflow.Gradients; using Tensorflow.Keras; +using Tensorflow.NumPy.Pickle; namespace Tensorflow { @@ -86,6 +88,22 @@ public tensorflow() OpDefLib = new OpDefLibrary(); InitGradientEnvironment(); + + try + { + var handle = c_api.TF_Version(); + } + catch (DllNotFoundException) + { + throw new RuntimeError("Tensorflow.NET cannot find a backend. Please install one of the following packages for your program: " + + "SciSharp.TensorFlow.Redist, SciSharp.TensorFlow.Redist-Linux-GPU, SciSharp.TensorFlow.Redist-Windows-GPU. For more details, " + + "please visit https://github.com/SciSharp/TensorFlow.NET. If it still not work after installing the backend, please submit an " + + "issue to https://github.com/SciSharp/TensorFlow.NET/issues"); + } + + // register numpy reconstructor for pickle + Unpickler.registerConstructor("numpy.core.multiarray", "_reconstruct", new MultiArrayConstructor()); + Unpickler.registerConstructor("numpy", "dtype", new DtypeConstructor()); } public string VERSION => c_api.StringPiece(c_api.TF_Version()); diff --git a/src/TensorFlowNET.Keras/Activations.cs b/src/TensorFlowNET.Keras/Activations.cs index 00de728f2..d3801902f 100644 --- a/src/TensorFlowNET.Keras/Activations.cs +++ b/src/TensorFlowNET.Keras/Activations.cs @@ -20,6 +20,11 @@ public class Activations: IActivationsApi Name = "relu", ActivationFunction = (features, name) => tf.Context.ExecuteOp("Relu", name, new ExecuteOpArgs(features)) }; + private static Activation _relu6 = new Activation() + { + Name = "relu6", + ActivationFunction = (features, name) => tf.Context.ExecuteOp("Relu6", name, new ExecuteOpArgs(features)) + }; private static Activation _sigmoid = new Activation() { Name = "sigmoid", @@ -44,7 +49,6 @@ public class Activations: IActivationsApi /// /// Register the name-activation mapping in this static class. /// - /// /// private static void RegisterActivation(Activation activation) { @@ -56,6 +60,7 @@ static Activations() _nameActivationMap = new Dictionary(); RegisterActivation(_relu); + RegisterActivation(_relu6); RegisterActivation(_linear); RegisterActivation(_sigmoid); RegisterActivation(_softmax); @@ -66,6 +71,7 @@ static Activations() public Activation Linear => _linear; public Activation Relu => _relu; + public Activation Relu6 => _relu6; public Activation Sigmoid => _sigmoid; @@ -77,6 +83,10 @@ static Activations() public Activation GetActivationFromName(string name) { + if (name == null) + { + return _linear; + } if (!_nameActivationMap.TryGetValue(name, out var res)) { throw new Exception($"Activation {name} not found"); diff --git a/src/TensorFlowNET.Keras/BackendImpl.cs b/src/TensorFlowNET.Keras/BackendImpl.cs index 80403ad6a..574cf5990 100644 --- a/src/TensorFlowNET.Keras/BackendImpl.cs +++ b/src/TensorFlowNET.Keras/BackendImpl.cs @@ -20,8 +20,12 @@ limitations under the License. using System.Collections.Generic; using Tensorflow.Functions; using Tensorflow.Graphs; +using Tensorflow.Common.Extensions; using static Tensorflow.Binding; using static Tensorflow.Graphs.SubGraphUtility; +using Tensorflow.Util; +using Tensorflow.Common.Types; +using System.Diagnostics; namespace Tensorflow.Keras { @@ -72,7 +76,7 @@ public void track_variable(IVariableV1 v) _GRAPH_VARIABLES[graph.graph_key] = v; } - public Tensor placeholder(Shape shape = null, + public KerasTensor placeholder(Shape shape = null, int ndim = -1, TF_DataType dtype = TF_DataType.DtInvalid, bool sparse = false, @@ -450,5 +454,552 @@ public Tensor conv2d_transpose(Tensor x, return x; } + + public (Tensors, Tensors, Tensors) rnn( + Func step_function, // args:inputs, states, return:output, new_states + Tensors inputs, // inputs is a tuple of tensors (one per input sequence) + Tensors initial_states, + bool go_backwards = false, + Tensor? mask = null, + Tensors? constants = null, + bool unroll = false, + Tensors? input_length = null, // An integer or a 1-D Tensor,depending on whether the time dimension is fixed-length or not + bool time_major = false, + bool zero_output_for_mask = false, + bool return_all_outputs = true) + { + + Tensor swap_batch_timestep(Tensor input_t) + { + var axes = Enumerable.Range(0, input_t.rank).ToArray(); + axes[0] = 1; + axes[1] = 0; + return tf.transpose(input_t, axes); + } + + if (!time_major) + { + inputs = Nest.MapStructure(swap_batch_timestep, inputs).ToTensors(); + } + + var flatted_inptus = Nest.Flatten(inputs).ToList(); + var first_flatted_input = flatted_inptus[0]; + var time_steps = first_flatted_input.shape[0]; + var batch = first_flatted_input.shape[1]; + var time_steps_t = tf.shape(first_flatted_input)[0]; + + foreach (var input_ in flatted_inptus) + { + input_.shape.with_rank_at_least(3); + } + + if (mask != null) + { + if (mask.dtype != TF_DataType.TF_BOOL) + { + mask = tf.cast(mask, TF_DataType.TF_BOOL); + } + + if (mask.rank == 2) + { + mask = tf.expand_dims(mask, -1); + } + + if (!time_major) + { + mask = swap_batch_timestep(mask); + } + + } + + // tf.where needs its condition tensor to be the same shape as its two + // result tensors, but in our case the condition (mask) tensor is + // (nsamples, 1), and inputs are (nsamples, ndimensions) or even more. + // So we need to broadcast the mask to match the shape of inputs. + // That's what the tile call does, it just repeats the mask along its + // second dimension n times. + + Tensors _expand_mask(Tensors mask_t, Tensors input_t, int fixed_dim = 1) + { + if (!mask_t.IsSingle()) + { + throw new ValueError($"mask_t is expected to be tensor, but got {mask_t}"); + } + + if (!input_t.IsSingle()) + { + throw new ValueError($"input_t is expected to be tensor, but got {input_t}"); + } + + var rank_diff = input_t.rank - mask_t.rank; + for (int i = 0; i < rank_diff; i++) + { + mask_t = tf.expand_dims(mask_t, -1); + } + var multiples = Enumerable.Repeat(1, fixed_dim).ToArray().concat(input_t.shape.as_int_list().Skip(fixed_dim).ToArray()); + return tf.tile(mask_t, multiples); + } + + Tensors outputs = new Tensors(); + Tensors output_time_zero = new Tensors(); + Tensors last_output = new Tensors(); + Tensors new_states = new Tensors(); + if (unroll) + { + if (time_steps == 0) + { + throw new ValueError("Unrolling requires a fixed number of timesteps."); + } + + // Process the input tensors. The input tensor need to be split on the + // time_step dim, and reverse if go_backwards is True. In the case of + // nested input, the input is flattened and then transformed + // individually. The result of this will be a tuple of lists, each of + // the item in tuple is list of the tensor with shape (batch, feature) + + + // TODO(Wanglongzhi2001),step_func接受的第二个参数为List,但是最后却用的tuple + //var states = Tuple.Create(initial_states); + var states = initial_states; + + var successive_states = new Tensors(); + var successive_outputs = new Tensors(); + + // Process the input tensors. The input tensor need to be split on the + // time_step dim, and reverse if go_backwards is True. In the case of + // nested input, the input is flattened and then transformed + // individually. The result of this will be a tuple of lists, each of + // the item in tuple is list of the tensor with shape (batch, feature) + + Tensors _process_single_input_t(Tensor input_t) + { + var unstaked_input_t = array_ops.unstack(input_t); // unstack for time_step dim + if (go_backwards) + { + unstaked_input_t = unstaked_input_t.Reverse().ToArray(); + } + return unstaked_input_t; + } + + // TODO(Wanglongzhi2001) + Tensors processed_input; + if (!inputs.IsSingle()) + { + processed_input = inputs.MapStructure(_process_single_input_t).ReduceTo().ToTensors(); + } + else + { + processed_input = _process_single_input_t(inputs); + } + + object _get_input_tensor(int time) + { + List inp = new List(); + foreach (var t_ in processed_input) + { + inp.Add(t_[time]); + } + return Nest.PackSequenceAs(inputs, inp); + } + + if (mask != null) + { + var mask_list = tf.unstack(mask); + if (go_backwards) + { + mask_list.Reverse().ToArray(); + } + + for (int i = 0; i < time_steps; i++) + { + // TODO(Wanglongzhi2001),deal with _get_input_tensor + var inp = _get_input_tensor(i); + var mask_t = mask_list[i]; + // TODO + var (output, newStates) = step_function((Tensors)inp, states.MergeWith(constants)); + + var tiled_mask_t = _expand_mask(mask_t, output); + + Tensors prev_output; + if (successive_outputs == null) + { + prev_output = tf.zeros_like(output); + } + else + { + prev_output = successive_outputs.Last(); + } + + // output could be a tensor + output = tf.where(tiled_mask_t, output, prev_output); + + var flat_states = Nest.Flatten(states).ToList(); + var flat_new_states = Nest.Flatten(newStates).ToList(); + + var tiledMaskT = flat_states + .Select(s => _expand_mask(mask_t, s)) + .ToArray(); + var tuple = Tuple.Create(tiledMaskT); + + List flat_final_states = new List(); + foreach (var (m, s, ps) in zip(tiled_mask_t.ToList(), flat_new_states, flat_states)) + { + flat_final_states.Add(tf.where(m, s, ps)); + } + + states = Nest.PackSequenceAs(states, flat_final_states).ToTensors(); + if (return_all_outputs) + { + successive_outputs = successive_outputs.MergeWith(output); + successive_outputs = successive_states.MergeWith(states); + } + else + { + successive_outputs = new Tensors(output); + successive_states = new Tensors(states); + } + + } + last_output = successive_outputs.Last(); + new_states = successive_states.Last(); + outputs = tf.stack(successive_outputs); + + if (zero_output_for_mask) + { + last_output = tf.where(_expand_mask(mask_list.Last(), last_output), last_output, tf.zeros_like(last_output)); + outputs = tf.where(_expand_mask(mask, outputs, fixed_dim: 2), outputs, tf.zeros_like(outputs)); + } + else // mask is null + { + for (int i = 0; i < time_steps; i++) + { + var inp = _get_input_tensor(i); + var (output, newStates) = step_function((Tensors)inp, states.MergeWith(constants)); + states = newStates; + + if (return_all_outputs) + { + successive_outputs.Add(output); + successive_states.Add(newStates); + } + else + { + successive_outputs = new Tensors { output }; + successive_states = new Tensors { newStates }; + } + } + last_output = successive_outputs.Last(); + new_states = successive_states.Last(); + outputs = tf.stack(successive_outputs); + } + } + } + else // unroll == false + { + var states = initial_states; + // Create input tensor array, if the inputs is nested tensors, then it + // will be flattened first, and tensor array will be created one per + // flattened tensor. + + + var input_ta = new List(); + for (int i = 0; i < flatted_inptus.Count; i++) + { + input_ta.Add(TensorArray.Create(dtype: flatted_inptus[i].dtype, size: time_steps_t)); + } + + foreach(var (ta, input_) in zip(input_ta, flatted_inptus)) + { + if (!go_backwards) + { + ta.unstack(input_); + } + else + { + ta.unstack(reverse(input_, 0)); + } + } + + + // Get the time(0) input and compute the output for that, the output will + // be used to determine the dtype of output tensor array. Don't read from + // input_ta due to TensorArray clear_after_read default to True. + var input_time_zero = Nest.PackSequenceAs(inputs, flatted_inptus.Select(x => x[0]).ToArray()).ToTensors(); + + // output_time_zero is used to determine the cell output shape and its + // dtype. the value is discarded. + (output_time_zero, _) = step_function(input_time_zero, + constants is null ? initial_states : initial_states.MergeWith(constants)); + + Tensor output_ta_size = return_all_outputs ? time_steps_t : constant_op.constant(1); + var output_ta = new List(); + foreach(var output in output_time_zero.Flatten()) + { + output_ta.Add(TensorArray.Create(dtype: output.dtype, size: output_ta_size, element_shape: output.shape)); + } + + var time = tf.constant(0, dtype: TF_DataType.TF_INT32, name: "time"); + + Func? masking_fn; + Func? compute_masked_output = null; + if (mask != null) + { + if (go_backwards) + { + mask = tf.reverse(mask, axis: new[] { 0 }); + } + var mask_ta = TensorArray.Create(dtype: TF_DataType.TF_BOOL, size: time_steps_t); + mask_ta = mask_ta.unstack(mask); + + masking_fn = (time) => + { + return mask_ta.read(time); + }; + + compute_masked_output = (mask_t, flat_out, flat_mask) => + { + var tiled_mask_t = new Tensors(); + foreach (var o in flat_out) + { + tiled_mask_t.Add(_expand_mask(mask_t, o, fixed_dim: mask_t.rank)); + } + + Tensors res = new Tensors(); + foreach (var (m, o, fm) in zip(tiled_mask_t.ToList(), flat_out.ToList(), flat_mask.ToList())) + { + res.Add(tf.where(m, o, fm)); + } + return res; + }; + } + // TODO(Wanglongzhi2001), what the input_length's type should be(an integer or a single tensor), it could be an integer or tensor + else if (input_length is Tensor) + { + if (go_backwards) + { + var max_len = tf.reduce_max(input_length, axis: 0); + var rev_input_length = tf.subtract(max_len - 1, input_length); + + masking_fn = (time) => + { + return tf.less(rev_input_length, time); + }; + } + else + { + masking_fn = (time) => + { + return tf.greater(input_length, time); + }; + } + + compute_masked_output = (mask_t, flat_out, flat_mask) => + { + var res = new List(); + foreach (var (o, zo) in zip(flat_out, flat_mask)) + { + res.Add(tf.where(mask_t, o, zo)); + } + return res; + }; + } + else + { + masking_fn = null; + } + + Func cond = (time) => (time[0] < time_steps_t); + int parallel_iterations = 32; + Tensors final_outputs; + if (masking_fn != null) + { + // Mask for the T output will be base on the output of T - 1. In the + // case T = 0, a zero filled tensor will be used. + var flat_zero_output = new Tensors(); + foreach (var o in Nest.Flatten(output_time_zero)) + { + flat_zero_output.Add(tf.zeros_like(o)); + } + + var prev_output = flat_zero_output; + var output_ta_t = output_ta; + Tensors _step(Tensors tensors) + { + /* + RNN step function. + Args: + time: Current timestep value. + output_ta_t: TensorArray. + prev_output: tuple of outputs from time - 1. + *states: List of states. + Returns: + Tuple(todo): `(time + 1, output_ta_t, output) + tuple(new_states)` + */ + + Tensor time = tensors[0]; + TensorArray output_ta_t = (tensors[1] as FakeTensorByTensorArray).TensorArray; + Tensors prev_output = tensors.GetShallow(2); + Tensors states = new Tensors(tensors.Skip(2 + prev_output.Length).ToArray()); + + var flat_current_input = input_ta.Select(x => x.read(time)).ToList(); + // maybe set shape + // TODO(Wanglongzhi2001),deal with nest.pack_sequence_as's return type + var current_input = Nest.PackSequenceAs(inputs, flat_current_input).ToTensors(); + var mask_t = masking_fn(time); + var (output, new_states) = step_function(current_input, states.MergeWith(constants)); + // mask output + var flat_output = Nest.Flatten(output).ToList(); + + var flat_mask_output = zero_output_for_mask ? flat_zero_output : prev_output.Flatten().ToList(); + + // TODO(Wanglongzhi2001),deal with compute_masked_output's third parameter's type + var flat_new_output = compute_masked_output(mask_t, flat_output, flat_mask_output); + + // mask states + var flat_state = states.Flatten().ToList(); + var flat_new_state = new_states.Flatten().ToList(); + + foreach (var (state, new_state) in zip(flat_state, flat_new_state)) + { + if (new_state is Tensor) + { + new_state.shape = state.shape; + } + } + + var flat_final_state = compute_masked_output(mask_t, flat_new_state, flat_state); + new_states = Nest.PackSequenceAs(new_states, flat_final_state.ToArray()).ToTensors(); + + var ta_index_to_write = return_all_outputs ? time : tf.constant(0); + Debug.Assert(flat_output.Count() == 1); + output_ta_t = output_ta_t.write(ta_index_to_write, flat_new_output.First()); + + return new Tensor[] { time + 1, new FakeTensorByTensorArray(output_ta_t) }.Concat(flat_new_output).Concat(new_states) + .ToArray().ToTensors(); + + } + var loop_vars = new Tensor[] { time + 1, new FakeTensorByTensorArray(output_ta[0]) } + .Concat(flat_zero_output.Flatten()).Concat(states).ToArray().ToTensors(); + final_outputs = control_flow_ops.while_loop(cond: cond, body: _step, loop_vars: loop_vars, parallel_iterations: parallel_iterations); + new_states = final_outputs.Skip(3).ToList(); + } + else + { + var output_ta_t = output_ta; + new_states = states; + Tensors _step(Tensors tensors) + { + Tensor time = tensors[0]; + TensorArray output_ta_t = (tensors[1] as FakeTensorByTensorArray).TensorArray; + Tensors states = new Tensors(tensors.Skip(2).ToArray()); + var flat_current_input = input_ta.Select(x => x.read(time)).ToList(); + // maybe set shape + // TODO(Wanglongzhi2001),deal with nest.pack_sequence_as's return type + var current_input = Nest.PackSequenceAs(inputs, flat_current_input).ToTensors(); + var (output, new_states) = step_function(current_input, states.MergeWith(constants)); + var flat_state = new_states.Flatten().ToList(); + var flat_new_state = new_states.Flatten().ToList(); + foreach (var (state, new_state) in zip(flat_state, flat_new_state)) + { + if (new_state is Tensor) + { + new_state.shape = state.shape; + } + } + var flat_output = Nest.Flatten(output); + var ta_index_to_write = return_all_outputs ? time : tf.constant(0); + Debug.Assert(flat_output.Count() == 1); + output_ta_t = output_ta_t.write(ta_index_to_write, flat_output.First()); + + new_states = Nest.PackSequenceAs(initial_states, flat_new_state).ToTensors(); + return new Tensor[] { time + 1, new FakeTensorByTensorArray(output_ta_t) }.Concat(new_states).ToArray().ToTensors(); + } + Debug.Assert(output_ta.Count == 1); + var loop_vars = new Tensor[] { time + 1, new FakeTensorByTensorArray(output_ta[0]) }.Concat(states).ToArray().ToTensors(); + final_outputs = control_flow_ops.while_loop(cond: cond, body: _step, loop_vars: loop_vars, parallel_iterations: parallel_iterations); + new_states = final_outputs.Skip(2).ToList(); + } + + output_ta = new List { (final_outputs[1] as FakeTensorByTensorArray).TensorArray }; + outputs = outputs.MergeWith(output_ta.Select(o => o.stack()).ToArray().ToTensors()); + last_output = last_output.MergeWith(outputs.Select(o => o[-1]).ToArray().ToTensors()); + outputs = Nest.PackSequenceAs(output_time_zero, (Tensor[])outputs).ToTensors(); + last_output = Nest.PackSequenceAs(output_time_zero, (Tensor[])last_output).ToTensors(); + } + + Func set_shape; + set_shape = (output_) => + { + if (output_ is Tensor) + { + var shape = output_.shape.as_int_list(); + if (return_all_outputs) + { + shape[0] = (int)time_steps; + } + else + { + shape[0] = 1; + } + shape[1] = (int)batch; + output_.shape = shape; + } + return output_; + }; + + outputs = Nest.MapStructure(set_shape, outputs).ToTensors(); + if (!time_major) + { + outputs = Nest.MapStructure(swap_batch_timestep, outputs).ToTensors(); + } + return (last_output, outputs, new_states); + + } + + /// + /// Repeats the elements of a tensor along an axis, like `np.repeat`. + /// + /// + /// + /// + /// + public Tensor repeat_elements(Tensor x, int rep, int axis) + { + var x_shape = x.shape.as_int_list(); + if (x_shape[axis] != -1) + { + var splits = tf.split(x, x_shape[axis], axis:axis); + var x_rep = splits.SelectMany(s => Enumerable.Repeat(s, rep)).ToArray(); + return concatenate(x_rep, axis); + } + //var auxiliary_axis = axis + 1; + //x_shape = x.shape; + //var x_rep = tf.expand_dims(x, auxiliary_axis); + //var reps = np.ones(x_shape.Length + 1); + //reps[auxiliary_axis] = rep; + //x_rep = tf.tile(x_rep, reps); + + throw new NotImplementedException(); + + } + public Tensor reverse(Tensor input, int axis) + { + return reverse(input, new int[] { axis }); + } + + public Tensor reverse(Tensor input, int[] axes) + { + return tf.reverse(input, axes); + } + + public Tensor maybe_convert_to_ragged(bool is_ragged_output, Tensor output, int nested_row_lengths, bool go_backwards = false) + { + if (!is_ragged_output) + { + return output; + } + + throw new NotImplementedException("Not implemented currently, please submit an issue to https://github.com/SciSharp/TensorFlow.NET/issues"); + } } } diff --git a/src/TensorFlowNET.Keras/Callbacks/CallbackList.cs b/src/TensorFlowNET.Keras/Callbacks/CallbackList.cs index 362f2280c..cb16aafa3 100644 --- a/src/TensorFlowNET.Keras/Callbacks/CallbackList.cs +++ b/src/TensorFlowNET.Keras/Callbacks/CallbackList.cs @@ -73,4 +73,9 @@ public void on_test_batch_end(long end_step, Dictionary logs) { callbacks.ForEach(x => x.on_test_batch_end(end_step, logs)); } + + public void on_test_end(Dictionary logs) + { + callbacks.ForEach(x => x.on_test_end(logs)); + } } diff --git a/src/TensorFlowNET.Keras/Callbacks/Earlystopping.cs b/src/TensorFlowNET.Keras/Callbacks/Earlystopping.cs index 73ccc87b0..a2a2ecfe2 100644 --- a/src/TensorFlowNET.Keras/Callbacks/Earlystopping.cs +++ b/src/TensorFlowNET.Keras/Callbacks/Earlystopping.cs @@ -5,13 +5,10 @@ namespace Tensorflow.Keras.Callbacks; /// /// Stop training when a monitored metric has stopped improving. /// -/// -/// - public class EarlyStopping: ICallback { int _paitence; - int _min_delta; + float _min_delta; int _verbose; int _stopped_epoch; int _wait; @@ -22,11 +19,13 @@ public class EarlyStopping: ICallback string _monitor; string _mode; bool _restore_best_weights; - List? _best_weights; + List? _best_weights; CallbackParams _parameters; + Func _monitor_op; + public Dictionary>? history { get; set; } // user need to pass a CallbackParams to EarlyStopping, CallbackParams at least need the model - public EarlyStopping(CallbackParams parameters,string monitor = "val_loss", int min_delta = 0, int patience = 0, + public EarlyStopping(CallbackParams parameters,string monitor = "val_loss", float min_delta = 0f, int patience = 0, int verbose = 1, string mode = "auto", float baseline = 0f, bool restore_best_weights = false, int start_from_epoch = 0) { @@ -41,17 +40,49 @@ public EarlyStopping(CallbackParams parameters,string monitor = "val_loss", int _min_delta = Math.Abs(min_delta); _restore_best_weights = restore_best_weights; _mode = mode; - if (mode != "auto" && mode != "min" && mode != "max") + + if (_mode != "auto" && _mode != "min" && _mode != "max") + { + Console.WriteLine($"EarlyStopping mode {_mode} is unknown, fallback to auto mode."); + _mode = "auto"; + } + + if (_mode == "min") + { + _monitor_op = np.less; + } + else if (_mode == "max") + { + _monitor_op = np.greater; + } + else + { + if (_monitor.EndsWith("acc") || _monitor.EndsWith("accuracy") || _monitor.EndsWith("auc")) + { + _monitor_op = np.greater; + } + else + { + _monitor_op = np.less; + } + } + + if (_monitor_op == np.greater) { - Console.WriteLine("EarlyStopping mode %s is unknown, fallback to auto mode.", mode); + _min_delta *= 1; + } + else + { + _min_delta *= -1; } } public void on_train_begin() { _wait = 0; _stopped_epoch = 0; + _best = _monitor_op == np.less ? (float)np.Inf : (float)-np.Inf; + _best_weights = null; _best_epoch = 0; - _best = (float)np.Inf; } public void on_epoch_begin(int epoch) @@ -77,7 +108,7 @@ public void on_epoch_end(int epoch, Dictionary epoch_logs) // Restore the weights after first epoch if no progress is ever made. if (_restore_best_weights && _best_weights == null) { - _best_weights = _parameters.Model.Weights; + _best_weights = _parameters.Model.get_weights(); } _wait += 1; @@ -86,7 +117,7 @@ public void on_epoch_end(int epoch, Dictionary epoch_logs) _best = current; _best_epoch = epoch; if (_restore_best_weights) - _best_weights = _parameters.Model.TrainableWeights; + _best_weights = _parameters.Model.get_weights(); // Only restart wait if we beat both the baseline and our previous best. if (_baseline == 0f || _is_improvement(current, _baseline)) _wait = 0; @@ -102,7 +133,7 @@ public void on_epoch_end(int epoch, Dictionary epoch_logs) { Console.WriteLine($"Restoring model weights from the end of the best epoch: {_best_epoch + 1}"); } - _parameters.Model.Weights = _best_weights; + _parameters.Model.set_weights(_best_weights); } } } @@ -134,20 +165,10 @@ float get_monitor_value(Dictionary logs) } public bool _is_improvement(float monitor_value, float reference_value) { - bool less_op = (monitor_value - _min_delta) < reference_value; - bool greater_op = (monitor_value - _min_delta) >= reference_value; - if (_mode == "min") - return less_op; - else if (_mode == "max") - return greater_op; - else - { - if (_monitor.EndsWith("acc") || _monitor.EndsWith("accuracy") || _monitor.EndsWith("auc")) - { - return greater_op; - } - else - return less_op; - } + return _monitor_op(monitor_value - _min_delta, reference_value); + } + + public void on_test_end(Dictionary logs) + { } } diff --git a/src/TensorFlowNET.Keras/Callbacks/History.cs b/src/TensorFlowNET.Keras/Callbacks/History.cs index c34f253d1..6d3ff6c38 100644 --- a/src/TensorFlowNET.Keras/Callbacks/History.cs +++ b/src/TensorFlowNET.Keras/Callbacks/History.cs @@ -81,4 +81,8 @@ public void on_test_batch_begin(long step) public void on_test_batch_end(long end_step, Dictionary logs) { } + + public void on_test_end(Dictionary logs) + { + } } diff --git a/src/TensorFlowNET.Keras/Callbacks/ProgbarLogger.cs b/src/TensorFlowNET.Keras/Callbacks/ProgbarLogger.cs index 9f2b1eb31..23b18cd47 100644 --- a/src/TensorFlowNET.Keras/Callbacks/ProgbarLogger.cs +++ b/src/TensorFlowNET.Keras/Callbacks/ProgbarLogger.cs @@ -118,5 +118,8 @@ public void on_test_batch_end(long end_step, Dictionary logs) } } + public void on_test_end(Dictionary logs) + { + } } } diff --git a/src/TensorFlowNET.Keras/Datasets/Imdb.cs b/src/TensorFlowNET.Keras/Datasets/Imdb.cs index 56b0d2a77..4d6df913b 100644 --- a/src/TensorFlowNET.Keras/Datasets/Imdb.cs +++ b/src/TensorFlowNET.Keras/Datasets/Imdb.cs @@ -3,8 +3,6 @@ using System.IO; using System.Text; using Tensorflow.Keras.Utils; -using Tensorflow.NumPy; -using System.Linq; namespace Tensorflow.Keras.Datasets { @@ -12,11 +10,57 @@ namespace Tensorflow.Keras.Datasets /// This is a dataset of 25,000 movies reviews from IMDB, labeled by sentiment /// (positive/negative). Reviews have been preprocessed, and each review is /// encoded as a list of word indexes(integers). + /// For convenience, words are indexed by overall frequency in the dataset, + /// so that for instance the integer "3" encodes the 3rd most frequent word in + /// the data.This allows for quick filtering operations such as: + /// "only consider the top 10,000 most + /// common words, but eliminate the top 20 most common words". + /// As a convention, "0" does not stand for a specific word, but instead is used + /// to encode the pad token. + /// Args: + /// path: where to cache the data (relative to %TEMP%/imdb/imdb.npz). + /// num_words: integer or None.Words are + /// ranked by how often they occur(in the training set) and only + /// the `num_words` most frequent words are kept.Any less frequent word + /// will appear as `oov_char` value in the sequence data.If None, + /// all words are kept.Defaults to `None`. + /// skip_top: skip the top N most frequently occurring words + /// (which may not be informative). These words will appear as + /// `oov_char` value in the dataset.When 0, no words are + /// skipped. Defaults to `0`. + /// maxlen: int or None.Maximum sequence length. + /// Any longer sequence will be truncated. None, means no truncation. + /// Defaults to `None`. + /// seed: int. Seed for reproducible data shuffling. + /// start_char: int. The start of a sequence will be marked with this + /// character. 0 is usually the padding character. Defaults to `1`. + /// oov_char: int. The out-of-vocabulary character. + /// Words that were cut out because of the `num_words` or + /// `skip_top` limits will be replaced with this character. + /// index_from: int. Index actual words with this index and higher. + /// Returns: + /// Tuple of Numpy arrays: `(x_train, labels_train), (x_test, labels_test)`. + /// + /// ** x_train, x_test**: lists of sequences, which are lists of indexes + /// (integers). If the num_words argument was specific, the maximum + /// possible index value is `num_words - 1`. If the `maxlen` argument was + /// specified, the largest possible sequence length is `maxlen`. + /// + /// ** labels_train, labels_test**: lists of integer labels(1 or 0). + /// + /// Raises: + /// ValueError: in case `maxlen` is so low + /// that no input sequence could be kept. + /// Note that the 'out of vocabulary' character is only used for + /// words that were present in the training set but are not included + /// because they're not making the `num_words` cut here. + /// Words that were not seen in the training set but are in the test set + /// have simply been skipped. /// + /// """Loads the [IMDB dataset](https://ai.stanford.edu/~amaas/data/sentiment/). public class Imdb { string origin_folder = "https://storage.googleapis.com/tensorflow/tf-keras-datasets/"; - string file_name = "imdb.npz"; string dest_folder = "imdb"; /// @@ -31,50 +75,163 @@ public class Imdb /// /// /// - public DatasetPass load_data(string path = "imdb.npz", - int num_words = -1, + public DatasetPass load_data( + string path = "imdb.npz", + int? num_words = null, int skip_top = 0, - int maxlen = -1, + int? maxlen = null, int seed = 113, - int start_char = 1, - int oov_char= 2, + int? start_char = 1, + int? oov_char = 2, int index_from = 3) { - var dst = Download(); + path = data_utils.get_file( + path, + origin: Path.Combine(origin_folder, "imdb.npz"), + file_hash: "69664113be75683a8fe16e3ed0ab59fda8886cb3cd7ada244f7d9544e4676b9f" + ); + path = Path.Combine(path, "imdb.npz"); + var fileBytes = File.ReadAllBytes(path); + var (x_train, x_test) = LoadX(fileBytes); + var (labels_train, labels_test) = LoadY(fileBytes); - var lines = File.ReadAllLines(Path.Combine(dst, "imdb_train.txt")); - var x_train_string = new string[lines.Length]; - var y_train = np.zeros(new int[] { lines.Length }, np.int64); - for (int i = 0; i < lines.Length; i++) + var indices = np.arange(len(x_train)); + np.random.shuffle(indices, seed); + x_train = x_train[indices]; + labels_train = labels_train[indices]; + + indices = np.arange(len(x_test)); + np.random.shuffle(indices, seed); + x_test = x_test[indices]; + labels_test = labels_test[indices]; + + var x_train_array = (int[,])x_train.ToMultiDimArray(); + var x_test_array = (int[,])x_test.ToMultiDimArray(); + var labels_train_array = (long[])labels_train.ToArray(); + var labels_test_array = (long[])labels_test.ToArray(); + + if (start_char != null) + { + var (d1, d2) = (x_train_array.GetLength(0), x_train_array.GetLength(1)); + int[,] new_x_train_array = new int[d1, d2 + 1]; + for (var i = 0; i < d1; i++) + { + new_x_train_array[i, 0] = (int)start_char; + Array.Copy(x_train_array, i * d2, new_x_train_array, i * (d2 + 1) + 1, d2); + } + (d1, d2) = (x_test_array.GetLength(0), x_test_array.GetLength(1)); + int[,] new_x_test_array = new int[d1, d2 + 1]; + for (var i = 0; i < d1; i++) + { + new_x_test_array[i, 0] = (int)start_char; + Array.Copy(x_test_array, i * d2, new_x_test_array, i * (d2 + 1) + 1, d2); + } + x_train_array = new_x_train_array; + x_test_array = new_x_test_array; + } + else if (index_from != 0) { - y_train[i] = long.Parse(lines[i].Substring(0, 1)); - x_train_string[i] = lines[i].Substring(2); + var (d1, d2) = (x_train_array.GetLength(0), x_train_array.GetLength(1)); + for (var i = 0; i < d1; i++) + { + for (var j = 0; j < d2; j++) + { + if (x_train_array[i, j] == 0) + break; + x_train_array[i, j] += index_from; + } + } + (d1, d2) = (x_test_array.GetLength(0), x_test_array.GetLength(1)); + for (var i = 0; i < d1; i++) + { + for (var j = 0; j < d2; j++) + { + if (x_test_array[i, j] == 0) + break; + x_test[i, j] += index_from; + } + } } - var x_train = np.array(x_train_string); + if (maxlen == null) + { + maxlen = max(x_train_array.GetLength(1), x_test_array.GetLength(1)); + } + (x_train_array, labels_train_array) = data_utils._remove_long_seq((int)maxlen, x_train_array, labels_train_array); + (x_test_array, labels_test_array) = data_utils._remove_long_seq((int)maxlen, x_test_array, labels_test_array); + if (x_train_array.Length == 0 || x_test_array.Length == 0) + throw new ValueError("After filtering for sequences shorter than maxlen=" + + $"{maxlen}, no sequence was kept. Increase maxlen."); + + int[,] xs_array = new int[x_train_array.GetLength(0) + x_test_array.GetLength(0), (int)maxlen]; + Array.Copy(x_train_array, xs_array, x_train_array.Length); + Array.Copy(x_test_array, 0, xs_array, x_train_array.Length, x_train_array.Length); + + long[] labels_array = new long[labels_train_array.Length + labels_test_array.Length]; + Array.Copy(labels_train_array, labels_array, labels_train_array.Length); + Array.Copy(labels_test_array, 0, labels_array, labels_train_array.Length, labels_test_array.Length); + + if (num_words == null) + { + var (d1, d2) = (xs_array.GetLength(0), xs_array.GetLength(1)); + num_words = 0; + for (var i = 0; i < d1; i++) + for (var j = 0; j < d2; j++) + num_words = max((int)num_words, (int)xs_array[i, j]); + } - File.ReadAllLines(Path.Combine(dst, "imdb_test.txt")); - var x_test_string = new string[lines.Length]; - var y_test = np.zeros(new int[] { lines.Length }, np.int64); - for (int i = 0; i < lines.Length; i++) + // by convention, use 2 as OOV word + // reserve 'index_from' (=3 by default) characters: + // 0 (padding), 1 (start), 2 (OOV) + if (oov_char != null) { - y_test[i] = long.Parse(lines[i].Substring(0, 1)); - x_test_string[i] = lines[i].Substring(2); + var (d1, d2) = (xs_array.GetLength(0), xs_array.GetLength(1)); + int[,] new_xs_array = new int[d1, d2]; + for (var i = 0; i < d1; i++) + { + for (var j = 0; j < d2; j++) + { + if (xs_array[i, j] == 0 || skip_top <= xs_array[i, j] && xs_array[i, j] < num_words) + new_xs_array[i, j] = xs_array[i, j]; + else + new_xs_array[i, j] = (int)oov_char; + } + } + xs_array = new_xs_array; } + else + { + var (d1, d2) = (xs_array.GetLength(0), xs_array.GetLength(1)); + int[,] new_xs_array = new int[d1, d2]; + for (var i = 0; i < d1; i++) + { + int k = 0; + for (var j = 0; j < d2; j++) + { + if (xs_array[i, j] == 0 || skip_top <= xs_array[i, j] && xs_array[i, j] < num_words) + new_xs_array[i, k++] = xs_array[i, j]; + } + } + xs_array = new_xs_array; + } + + Array.Copy(xs_array, x_train_array, x_train_array.Length); + Array.Copy(xs_array, x_train_array.Length, x_test_array, 0, x_train_array.Length); - var x_test = np.array(x_test_string); + Array.Copy(labels_array, labels_train_array, labels_train_array.Length); + Array.Copy(labels_array, labels_train_array.Length, labels_test_array, 0, labels_test_array.Length); return new DatasetPass { - Train = (x_train, y_train), - Test = (x_test, y_test) + Train = (x_train_array, labels_train_array), + Test = (x_test_array, labels_test_array) }; } (NDArray, NDArray) LoadX(byte[] bytes) { - var y = np.Load_Npz(bytes); - return (y["x_train.npy"], y["x_test.npy"]); + var x = np.Load_Npz(bytes); + return (x["x_train.npy"], x["x_test.npy"]); } (NDArray, NDArray) LoadY(byte[] bytes) @@ -82,16 +239,5 @@ public DatasetPass load_data(string path = "imdb.npz", var y = np.Load_Npz(bytes); return (y["y_train.npy"], y["y_test.npy"]); } - - string Download() - { - var dst = Path.Combine(Path.GetTempPath(), dest_folder); - Directory.CreateDirectory(dst); - - Web.Download(origin_folder + file_name, dst, file_name); - - return dst; - // return Path.Combine(dst, file_name); - } } } diff --git a/src/TensorFlowNET.Keras/Engine/DataAdapters/DataAdapter.cs b/src/TensorFlowNET.Keras/Engine/DataAdapters/DataAdapter.cs index 6c7d53b2f..590f30a78 100644 --- a/src/TensorFlowNET.Keras/Engine/DataAdapters/DataAdapter.cs +++ b/src/TensorFlowNET.Keras/Engine/DataAdapters/DataAdapter.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Text; using Tensorflow.Keras.ArgsDefinition; +using Tensorflow.Util; namespace Tensorflow.Keras.Engine.DataAdapters { @@ -34,9 +35,75 @@ public virtual (Tensors, Tensors) Expand1d(Tensors x, Tensors y) return (x, y); } + public virtual (Tensors, Tensors, Tensors) Expand1d(Tensors x, Tensors y, Tensors sample_weight) + { + for (int i = 0; i < x.Length; i++) + { + if (x[i].shape.ndim == 1) + x[i] = array_ops.expand_dims(x[i], axis: -1); + } + for (int i = 0; i < y.Length; i++) + { + if (y[i].shape.ndim == 1) + y[i] = array_ops.expand_dims(y[i], axis: -1); + } + for (int i = 0; i < sample_weight.Length; i++) + { + if (sample_weight[i].shape.ndim == 1) + sample_weight[i] = array_ops.expand_dims(sample_weight[i], axis: -1); + } + return (x, y, sample_weight); + } + public virtual bool ShouldRecreateIterator() { return true; } + + public static ((NDArray, NDArray, NDArray),ValidationDataPack) train_validation_split((NDArray, NDArray, NDArray) x_y_sample_weight, float validation_split) + { + var x = x_y_sample_weight.Item1; + var y = x_y_sample_weight.Item2; + var sample_weight = x_y_sample_weight.Item3; + int train_count = Convert.ToInt32(x.dims[0] * (1 - validation_split)); + var train_x = x[new Slice(0, train_count)]; + var train_y = y[new Slice(0, train_count)]; + ValidationDataPack validation_data; + if (sample_weight != null) + { + validation_data = (x[new Slice(train_count)], y[new Slice(train_count)], sample_weight[new Slice(train_count)]); + sample_weight = sample_weight[new Slice(0, train_count)]; + } + else + { + validation_data = (x[new Slice(train_count)], y[new Slice(train_count)]); + } + + return ((train_x, train_y, sample_weight), validation_data); + } + + public static ((IEnumerable, NDArray, NDArray), ValidationDataPack) train_validation_split((IEnumerable, NDArray, NDArray) x_y_sample_weight, float validation_split) + { + var x = x_y_sample_weight.Item1; + var y = x_y_sample_weight.Item2; + var sample_weight = x_y_sample_weight.Item3; + int train_count = Convert.ToInt32(y.dims[0] * (1 - validation_split)); + var train_x = x.Select(x => x[new Slice(0, train_count)] as NDArray); + var train_y = y[new Slice(0, train_count)]; + var val_x = x.Select(x => x[new Slice(train_count)] as NDArray); + var val_y = y[new Slice(train_count)]; + + ValidationDataPack validation_data; + if (sample_weight != null) + { + validation_data = (val_x, val_y, sample_weight[new Slice(train_count)]); + sample_weight = sample_weight[new Slice(0, train_count)]; + } + else + { + validation_data = (val_x, val_y); + } + return ((train_x, train_y, sample_weight), validation_data); + } } } diff --git a/src/TensorFlowNET.Keras/Engine/DataAdapters/DataHandler.cs b/src/TensorFlowNET.Keras/Engine/DataAdapters/DataHandler.cs index 4723222f2..a305e5033 100644 --- a/src/TensorFlowNET.Keras/Engine/DataAdapters/DataHandler.cs +++ b/src/TensorFlowNET.Keras/Engine/DataAdapters/DataHandler.cs @@ -2,6 +2,9 @@ using System.Collections.Generic; using Tensorflow.Keras.ArgsDefinition; using static Tensorflow.Binding; +using Tensorflow.Keras.Utils; +using Tensorflow.Util; +using Tensorflow.Framework; namespace Tensorflow.Keras.Engine.DataAdapters { @@ -23,11 +26,13 @@ public class DataHandler long _steps_per_execution_value; int _initial_epoch => args.InitialEpoch; int _epochs => args.Epochs; + NDArray _sample_weight => args.SampleWeight; IVariableV1 _steps_per_execution; public DataHandler(DataHandlerArgs args) { this.args = args; + if (args.StepsPerExecution == null) { _steps_per_execution = tf.Variable(1L); @@ -48,6 +53,7 @@ public DataHandler(DataHandlerArgs args) BatchSize = args.BatchSize, Steps = args.StepsPerEpoch, Epochs = args.Epochs - args.InitialEpoch, + SampleWeight = args.SampleWeight, Shuffle = args.Shuffle, MaxQueueSize = args.MaxQueueSize, Worker = args.Workers, @@ -72,10 +78,75 @@ public DataHandler(DataHandlerArgs args) } _dataset = _adapter.GetDataset(); - _inferred_steps = _infer_steps(args.StepsPerEpoch, _dataset); _current_step = 0; _step_increment = _steps_per_execution_value - 1; _insufficient_data = false; + _configure_dataset_and_inferred_steps(args.X, args.ClassWeight); + } + + void _configure_dataset_and_inferred_steps(Tensors x, Dictionary class_weight) + { + if (_dataset == null) + { + _dataset = _adapter.GetDataset(); + _inferred_steps = _infer_steps(args.StepsPerEpoch, _dataset); + } + + if (class_weight != null) + { + _dataset = _dataset.map(_make_class_weight_map_fn(class_weight)); + } + _inferred_steps = _infer_steps(args.StepsPerEpoch, _dataset); + } + + + Func _make_class_weight_map_fn(Dictionary class_weight) + { + var class_ids = class_weight.Keys.OrderBy(key => key).ToList(); + var expected_class_ids = range(class_ids[0], class_ids[class_ids.Count - 1] + 1); + if (!class_ids.SequenceEqual(expected_class_ids)) + { + throw new ValueError("Expected `class_weight` to be a dict with keys from 0 to one less "+ + $"than the number of classes, found {class_weight}"); + } + + var class_weight_list = new List(); + foreach (var class_id in class_ids) + { + class_weight_list.Add(class_weight[class_id]); + } + var class_weight_tensor = tf.convert_to_tensor(class_weight_list.ToArray()); + + Func _class_weight_map_fn = (Tensors data) => + { + var x = data[0]; + var y = data[1]; + var sw = _sample_weight == null ? null : ops.convert_to_tensor(_sample_weight); + + if (y.shape.rank > 2) + { + throw new ValueError("`class_weight` not supported for 3+ dimensional targets."); + } + + var y_classes = smart_module.smart_cond( + y.shape.rank == 2 && y.shape[1] > 1, + () => math_ops.argmax(y, dimension: 1), + () => math_ops.cast(tf.reshape(y, (-1)), TF_DataType.TF_INT64)); + + var cw = array_ops.gather(class_weight_tensor, y_classes); + if (sw != null) + { + cw = tf.cast(cw, sw.dtype); + cw *= sw; + } + else + { + sw = cw; + } + return new Tensors { x, y, sw }; + }; + + return _class_weight_map_fn; } long _infer_steps(int steps_per_epoch, IDatasetV2 dataset) diff --git a/src/TensorFlowNET.Keras/Engine/DataAdapters/IDataAdapter.cs b/src/TensorFlowNET.Keras/Engine/DataAdapters/IDataAdapter.cs index 4bdc49795..bb71b0a2d 100644 --- a/src/TensorFlowNET.Keras/Engine/DataAdapters/IDataAdapter.cs +++ b/src/TensorFlowNET.Keras/Engine/DataAdapters/IDataAdapter.cs @@ -17,6 +17,8 @@ public interface IDataAdapter IDatasetV2 GetDataset(); int GetSize(); (Tensors, Tensors) Expand1d(Tensors x, Tensors y); + (Tensors, Tensors, Tensors) Expand1d(Tensors x, Tensors y, Tensors sample_weight); + bool ShouldRecreateIterator(); } } diff --git a/src/TensorFlowNET.Keras/Engine/DataAdapters/TensorLikeDataAdapter.cs b/src/TensorFlowNET.Keras/Engine/DataAdapters/TensorLikeDataAdapter.cs index a7e1d7e34..978a3f51c 100644 --- a/src/TensorFlowNET.Keras/Engine/DataAdapters/TensorLikeDataAdapter.cs +++ b/src/TensorFlowNET.Keras/Engine/DataAdapters/TensorLikeDataAdapter.cs @@ -20,7 +20,7 @@ public class TensorLikeDataAdapter : DataAdapter, IDataAdapter public TensorLikeDataAdapter(DataAdapterArgs args) { this.args = args; - _process_tensorlike(); + Tensor sample_weight_tensor = args.SampleWeight != null ? _process_tensorlike(args.SampleWeight) : null; num_samples = (int)args.X.shape[0]; var batch_size = args.BatchSize == -1 ? 32 : args.BatchSize; _batch_size = batch_size; @@ -37,6 +37,8 @@ public TensorLikeDataAdapter(DataAdapterArgs args) inputs.AddRange(args.X); if (args.Y != null) inputs.AddRange(args.Y); + if (sample_weight_tensor != null) + inputs.Add(sample_weight_tensor); dataset = slice_inputs(indices_dataset, inputs); dataset.FirstInputTensorCount = args.X.Length; } @@ -52,12 +54,13 @@ Tensors permutation(Tensors tensor) /// /// Convert a Tensor of indices into a dataset of batched indices. /// - /// + /// /// IDatasetV2 slice_batch_indices(Tensor indices) { var num_in_full_batch = num_full_batches * _batch_size; - var first_k_indices = array_ops.slice(indices, new int[] { 0 }, new int[] { num_in_full_batch }); + var first_k_indices = array_ops.slice(indices, new Tensor[] { ops.convert_to_tensor(0) }, + new Tensor[] { ops.convert_to_tensor(num_in_full_batch) }); first_k_indices = array_ops.reshape(first_k_indices, new int[] { num_full_batches, _batch_size }); var flat_dataset = tf.data.Dataset.from_tensor_slices(first_k_indices); if (_partial_batch_size > 0) @@ -81,7 +84,7 @@ IDatasetV2 slice_inputs(IDatasetV2 indices_dataset, Tensors elements) { var indices = inputs[0]; var results = inputs.Skip(1) - .Select(x => gen_array_ops.gather_v2(x, indices, 0)) + .Select(x => array_ops.gather(x, indices, axis: 0)) .ToArray(); return new Tensors(results); }, -1); @@ -93,8 +96,9 @@ IDatasetV2 slice_inputs(IDatasetV2 indices_dataset, Tensors elements) public override bool ShouldRecreateIterator() => false; - void _process_tensorlike() + Tensor _process_tensorlike(NDArray sample_weights) { + return tf.convert_to_tensor(sample_weights); } } } diff --git a/src/TensorFlowNET.Keras/Engine/Functional.FromConfig.cs b/src/TensorFlowNET.Keras/Engine/Functional.FromConfig.cs index 7b826af8e..375fc9106 100644 --- a/src/TensorFlowNET.Keras/Engine/Functional.FromConfig.cs +++ b/src/TensorFlowNET.Keras/Engine/Functional.FromConfig.cs @@ -30,7 +30,7 @@ public static (Tensors, Tensors, Dictionary) reconstruct_from_co created_layers = created_layers ?? new Dictionary(); var node_index_map = new Dictionary<(string, int), int>(); var node_count_by_layer = new Dictionary(); - var unprocessed_nodes = new Dictionary(); + var unprocessed_nodes = new Dictionary>(); // First, we create all layers and enqueue nodes to be processed foreach (var layer_data in config.Layers) process_layer(created_layers, layer_data, unprocessed_nodes, node_count_by_layer); @@ -79,7 +79,7 @@ public static (Tensors, Tensors, Dictionary) reconstruct_from_co static void process_layer(Dictionary created_layers, LayerConfig layer_data, - Dictionary unprocessed_nodes, + Dictionary> unprocessed_nodes, Dictionary node_count_by_layer) { ILayer layer = null; @@ -92,32 +92,38 @@ static void process_layer(Dictionary created_layers, created_layers[layer_name] = layer; } - node_count_by_layer[layer] = _should_skip_first_node(layer) ? 1 : 0; + node_count_by_layer[layer] = layer_data.InboundNodes.Count - (_should_skip_first_node(layer) ? 1 : 0); var inbound_nodes_data = layer_data.InboundNodes; foreach (var node_data in inbound_nodes_data) { if (!unprocessed_nodes.ContainsKey(layer)) - unprocessed_nodes[layer] = node_data; + unprocessed_nodes[layer] = new List() { node_data }; else - unprocessed_nodes.Add(layer, node_data); + unprocessed_nodes[layer].Add(node_data); } } static void process_node(ILayer layer, - NodeConfig node_data, + List nodes_data, Dictionary created_layers, Dictionary node_count_by_layer, Dictionary<(string, int), int> node_index_map) { + var input_tensors = new List(); - var inbound_layer_name = node_data.Name; - var inbound_node_index = node_data.NodeIndex; - var inbound_tensor_index = node_data.TensorIndex; - var inbound_layer = created_layers[inbound_layer_name]; - var inbound_node = inbound_layer.InboundNodes[inbound_node_index]; - input_tensors.Add(inbound_node.Outputs[inbound_node_index]); + for (int i = 0; i < nodes_data.Count; i++) + { + var node_data = nodes_data[i]; + var inbound_layer_name = node_data.Name; + var inbound_node_index = node_data.NodeIndex; + var inbound_tensor_index = node_data.TensorIndex; + + var inbound_layer = created_layers[inbound_layer_name]; + var inbound_node = inbound_layer.InboundNodes[inbound_node_index]; + input_tensors.Add(inbound_node.Outputs[inbound_node_index]); + } var output_tensors = layer.Apply(input_tensors); diff --git a/src/TensorFlowNET.Keras/Engine/Functional.cs b/src/TensorFlowNET.Keras/Engine/Functional.cs index e768bd0bd..75854d82c 100644 --- a/src/TensorFlowNET.Keras/Engine/Functional.cs +++ b/src/TensorFlowNET.Keras/Engine/Functional.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using Tensorflow.Common.Types; using Tensorflow.Keras.ArgsDefinition; using Tensorflow.Keras.Saving.SavedModel; using Tensorflow.Keras.Utils; @@ -81,7 +82,7 @@ protected void _init_graph_network(Tensors inputs, Tensors outputs) } else { - _buildInputShape = new Saving.TensorShapeConfig(); + _buildInputShape = new TensorShapeConfig(); } if (outputs.Any(x => x.KerasHistory == null)) @@ -179,7 +180,7 @@ void ComputeTensorUsageCount() var (nodes_in_decreasing_depth, layer_indices) = BuildMap(outputs); var network_nodes = nodes_in_decreasing_depth .Select(node => MakeNodeKey(node.Layer.Name, node.Layer.InboundNodes.IndexOf(node))) - .ToArray(); + .ToList(); var nodes_depths = new Dictionary(); var layers_depths = new Dictionary(); @@ -220,7 +221,7 @@ void ComputeTensorUsageCount() layers_depths[input_layer] = 0; layer_indices[input_layer] = -1; nodes_depths[input_layer.InboundNodes[0]] = 0; - network_nodes.add(MakeNodeKey(input_layer.Name, 0)); + network_nodes.Add(MakeNodeKey(input_layer.Name, 0)); } } @@ -230,7 +231,7 @@ void ComputeTensorUsageCount() { if (!nodes_by_depth.ContainsKey(depth)) nodes_by_depth[depth] = new List(); - nodes_by_depth[depth].append(node); + nodes_by_depth[depth].Add(node); } var layers_by_depth = new Dictionary>(); @@ -238,7 +239,7 @@ void ComputeTensorUsageCount() { if (!layers_by_depth.ContainsKey(depth)) layers_by_depth[depth] = new List(); - layers_by_depth[depth].append(layer); + layers_by_depth[depth].Add(layer); } // Get sorted list of layer depths. @@ -259,7 +260,7 @@ void ComputeTensorUsageCount() // Get sorted list of node depths. depth_keys = nodes_by_depth.Keys.OrderBy(x => x).Reverse(); - return (network_nodes, nodes_by_depth, layers, layers_by_depth); + return (network_nodes.ToArray(), nodes_by_depth, layers, layers_by_depth); } string MakeNodeKey(string layer_name, int node_index) @@ -325,7 +326,7 @@ void BuildMapHelper(Tensor tensor, nodes_in_decreasing_depth.append(node); } - protected override Tensors Call(Tensors inputs, Tensor state = null, bool? training = null) + protected override Tensors Call(Tensors inputs, Tensors state = null, bool? training = null, IOptionalArgs? optional_args = null) { var tensor_dict = new Dictionary>(); // map input values diff --git a/src/TensorFlowNET.Keras/Engine/Layer.Apply.cs b/src/TensorFlowNET.Keras/Engine/Layer.Apply.cs index 7d3721f12..a3831bffa 100644 --- a/src/TensorFlowNET.Keras/Engine/Layer.Apply.cs +++ b/src/TensorFlowNET.Keras/Engine/Layer.Apply.cs @@ -1,4 +1,5 @@ using System.Threading; +using Tensorflow.Common.Types; using static Tensorflow.Binding; namespace Tensorflow.Keras.Engine @@ -8,11 +9,11 @@ public partial class Layer /// /// Wraps `call`, applying pre- and post-processing steps. /// - /// - /// + /// + /// /// /// - public Tensors Apply(Tensors inputs, Tensor state = null, bool training = false) + public virtual Tensors Apply(Tensors inputs, Tensors states = null, bool? training = false, IOptionalArgs? optional_args = null) { if (callContext.Value == null) callContext.Value = new CallContext(); @@ -30,16 +31,32 @@ public Tensors Apply(Tensors inputs, Tensor state = null, bool training = false) if (!built) MaybeBuild(inputs); - var outputs = Call(inputs, state: state, training: training); + var outputs = Call(inputs, state: states, training: training); // memory leak // _set_connectivity_metadata_(inputs, outputs); _handle_activity_regularization(inputs, outputs); _set_mask_metadata(inputs, outputs, null); + // TODO(Rinne): set save spec if null + scope.__exit__(); return outputs; } + + // TODO(Rinne): remove it and completely fix issue 1084 + [Obsolete] + private bool _enforce_layer_construction = false; + [Obsolete] + internal void enforce_layer_construction() + { + _enforce_layer_construction = true; + } + [Obsolete] + internal void unset_layer_construction() + { + _enforce_layer_construction = false; + } } } diff --git a/src/TensorFlowNET.Keras/Engine/Layer.FunctionalConstructionCall.cs b/src/TensorFlowNET.Keras/Engine/Layer.FunctionalConstructionCall.cs index 1d96e5811..e4023c3fd 100644 --- a/src/TensorFlowNET.Keras/Engine/Layer.FunctionalConstructionCall.cs +++ b/src/TensorFlowNET.Keras/Engine/Layer.FunctionalConstructionCall.cs @@ -1,7 +1,5 @@ using System; using Tensorflow.Keras.Utils; -using static Tensorflow.Binding; -using static Tensorflow.KerasApi; namespace Tensorflow.Keras.Engine { @@ -9,14 +7,6 @@ public partial class Layer { Tensors FunctionalConstructionCall(Tensors inputs) { - bool mask_arg_passed_by_framework = false; - bool training_arg_passed_by_framework = false; - Tensor training_value = null; - if (training_value == null) - { - training_arg_passed_by_framework = true; - } - if (base_layer_utils.needs_keras_history(inputs)) base_layer_utils.create_keras_history(inputs); diff --git a/src/TensorFlowNET.Keras/Engine/Layer.Serialize.cs b/src/TensorFlowNET.Keras/Engine/Layer.Serialize.cs index ed5c2de0a..49811417e 100644 --- a/src/TensorFlowNET.Keras/Engine/Layer.Serialize.cs +++ b/src/TensorFlowNET.Keras/Engine/Layer.Serialize.cs @@ -27,6 +27,6 @@ public override IDictionary _trackable_children(SaveType save children = new Dictionary(); } - return children.Concat(base._trackable_children(save_type, cache)).ToDictionary(x => x.Key, x => x.Value); + return children.Concat(base._trackable_children(save_type, cache)).GroupBy(x => x.Key).Select(g => g.First()).ToDictionary(x => x.Key, x => x.Value); } } \ No newline at end of file diff --git a/src/TensorFlowNET.Keras/Engine/Layer.cs b/src/TensorFlowNET.Keras/Engine/Layer.cs index 7462b1367..2f758a850 100644 --- a/src/TensorFlowNET.Keras/Engine/Layer.cs +++ b/src/TensorFlowNET.Keras/Engine/Layer.cs @@ -32,7 +32,7 @@ limitations under the License. using static Tensorflow.Binding; using Tensorflow.Framework; using Tensorflow.Sessions; - +using Tensorflow.Common.Types; namespace Tensorflow.Keras.Engine { @@ -291,7 +291,7 @@ internal virtual void Initialize(LayerArgs args) bool _in_functional_construction_mode(Tensors inputs) { return tf.Context.executing_eagerly() - && inputs.Count(x => x is not EagerTensor && x is not NDArray) == inputs.Count(); + && inputs.Count(x => x is not EagerTensor && x is not NDArray) == inputs.Count() || _enforce_layer_construction; } public void SetConnectivityMetadata(Tensors inputs, Tensors outputs) @@ -332,7 +332,7 @@ private Tensor compute_mask(Tensor inputs, Tensor mask = null) /// /// /// - protected virtual Tensors Call(Tensors inputs, Tensor state = null, bool? training = null) + protected virtual Tensors Call(Tensors inputs, Tensors state = null, bool? training = null, IOptionalArgs? optional_args = null) { if(ReplacedCall is not null) { diff --git a/src/TensorFlowNET.Keras/Engine/LossesContainer.cs b/src/TensorFlowNET.Keras/Engine/LossesContainer.cs index 6a91450de..c06fca593 100644 --- a/src/TensorFlowNET.Keras/Engine/LossesContainer.cs +++ b/src/TensorFlowNET.Keras/Engine/LossesContainer.cs @@ -26,11 +26,11 @@ public LossesContainer(ILossFunc losses, string[] output_names = null) /// /// /// - public Tensor Call(Tensor y_true, Tensor y_pred) + public Tensor Call(Tensor y_true, Tensor y_pred, Tensor sample_weight = null) { if (!_built) Build(y_pred); - var loss_value = _losses.Call(y_true, y_pred); + var loss_value = _losses.Call(y_true, y_pred, sample_weight:sample_weight); var loss_metric_value = loss_value; var batch_dim = array_ops.shape(y_true)[0]; diff --git a/src/TensorFlowNET.Keras/Engine/Model.Build.cs b/src/TensorFlowNET.Keras/Engine/Model.Build.cs index 69afdef90..233363832 100644 --- a/src/TensorFlowNET.Keras/Engine/Model.Build.cs +++ b/src/TensorFlowNET.Keras/Engine/Model.Build.cs @@ -23,7 +23,7 @@ public override void build(KerasShapesWrapper input_shape) var graph = tf.executing_eagerly() ? new FuncGraph("build_graph") : keras.backend.get_graph(); graph.as_default(); var shapes = input_shape.ToShapeArray(); - var x = new Tensors(shapes.Select(x => base_layer_utils.generate_placeholders_from_shape(x))); + var x = new Tensors(shapes.Select(x => base_layer_utils.generate_placeholders_from_shape(x)).ToArray()); try { Call(x, training: false); diff --git a/src/TensorFlowNET.Keras/Engine/Model.Evaluate.cs b/src/TensorFlowNET.Keras/Engine/Model.Evaluate.cs index 185de4f48..ec99d7ef9 100644 --- a/src/TensorFlowNET.Keras/Engine/Model.Evaluate.cs +++ b/src/TensorFlowNET.Keras/Engine/Model.Evaluate.cs @@ -1,21 +1,21 @@ -using Tensorflow.NumPy; using System; using System.Collections.Generic; using System.Linq; +using Tensorflow; using Tensorflow.Keras.ArgsDefinition; +using Tensorflow.Keras.Callbacks; using Tensorflow.Keras.Engine.DataAdapters; -using static Tensorflow.Binding; using Tensorflow.Keras.Layers; using Tensorflow.Keras.Utils; -using Tensorflow; -using Tensorflow.Keras.Callbacks; +using Tensorflow.NumPy; +using static Tensorflow.Binding; namespace Tensorflow.Keras.Engine { public partial class Model { /// - /// Returns the loss value & metrics values for the model in test mode. + /// Returns the loss value and metrics values for the model in test mode. /// /// /// @@ -30,6 +30,7 @@ public partial class Model public Dictionary evaluate(NDArray x, NDArray y, int batch_size = -1, int verbose = 1, + NDArray sample_weight = null, int steps = -1, int max_queue_size = 10, int workers = 1, @@ -51,6 +52,7 @@ public Dictionary evaluate(NDArray x, NDArray y, StepsPerEpoch = steps, InitialEpoch = 0, Epochs = 1, + SampleWeight = sample_weight, MaxQueueSize = max_queue_size, Workers = workers, UseMultiprocessing = use_multiprocessing, @@ -64,40 +66,23 @@ public Dictionary evaluate(NDArray x, NDArray y, Verbose = verbose, Steps = data_handler.Inferredsteps }); - callbacks.on_test_begin(); - - //Dictionary? logs = null; - var logs = new Dictionary(); - foreach (var (epoch, iterator) in data_handler.enumerate_epochs()) - { - reset_metrics(); - // data_handler.catch_stop_iteration(); - foreach (var step in data_handler.steps()) - { - callbacks.on_test_batch_begin(step); - logs = test_function(data_handler, iterator); - var end_step = step + data_handler.StepIncrement; - if (is_val == false) - callbacks.on_test_batch_end(end_step, logs); - } - } - - var results = new Dictionary(); - foreach (var log in logs) - { - results[log.Key] = log.Value; - } - return results; + return evaluate(data_handler, callbacks, is_val, test_function); } - public Dictionary evaluate(IEnumerable x, NDArray y, int verbose = 1, bool is_val = false) + public Dictionary evaluate( + IEnumerable x, + Tensor y, + int verbose = 1, + NDArray sample_weight = null, + bool is_val = false) { var data_handler = new DataHandler(new DataHandlerArgs { - X = new Tensors(x), + X = new Tensors(x.ToArray()), Y = y, Model = this, + SampleWeight = sample_weight, StepsPerExecution = _steps_per_execution }); @@ -107,34 +92,10 @@ public Dictionary evaluate(IEnumerable x, NDArray y, int Verbose = verbose, Steps = data_handler.Inferredsteps }); - callbacks.on_test_begin(); - Dictionary logs = null; - foreach (var (epoch, iterator) in data_handler.enumerate_epochs()) - { - reset_metrics(); - callbacks.on_epoch_begin(epoch); - // data_handler.catch_stop_iteration(); - - foreach (var step in data_handler.steps()) - { - callbacks.on_test_batch_begin(step); - logs = test_step_multi_inputs_function(data_handler, iterator); - var end_step = step + data_handler.StepIncrement; - if (is_val == false) - callbacks.on_test_batch_end(end_step, logs); - } - } - - var results = new Dictionary(); - foreach (var log in logs) - { - results[log.Key] = log.Value; - } - return results; + return evaluate(data_handler, callbacks, is_val, test_step_multi_inputs_function); } - public Dictionary evaluate(IDatasetV2 x, int verbose = 1, bool is_val = false) { var data_handler = new DataHandler(new DataHandlerArgs @@ -150,57 +111,96 @@ public Dictionary evaluate(IDatasetV2 x, int verbose = 1, bool is Verbose = verbose, Steps = data_handler.Inferredsteps }); + + Func> testFunction; + + if (data_handler.DataAdapter.GetDataset().structure.Length > 2 || + data_handler.DataAdapter.GetDataset().FirstInputTensorCount > 1) + { + testFunction = test_step_multi_inputs_function; + } + else + { + testFunction = test_function; + } + + return evaluate(data_handler, callbacks, is_val, testFunction); + } + + /// + /// Internal bare implementation of evaluate function. + /// + /// Interations handling objects + /// + /// The function to be called on each batch of data. + /// Whether it is validation or test. + /// + Dictionary evaluate(DataHandler data_handler, CallbackList callbacks, bool is_val, Func> test_func) + { callbacks.on_test_begin(); - Dictionary logs = null; + var logs = new Dictionary(); foreach (var (epoch, iterator) in data_handler.enumerate_epochs()) { reset_metrics(); - callbacks.on_epoch_begin(epoch); - // data_handler.catch_stop_iteration(); - foreach (var step in data_handler.steps()) { callbacks.on_test_batch_begin(step); - logs = test_function(data_handler, iterator); + logs = test_func(data_handler, iterator); var end_step = step + data_handler.StepIncrement; - if (is_val == false) + if (!is_val) callbacks.on_test_batch_end(end_step, logs); + GC.Collect(); } } - - var results = new Dictionary(); - foreach (var log in logs) - { - results[log.Key] = log.Value; - } + callbacks.on_test_end(logs); + var results = new Dictionary(logs); return results; } Dictionary test_function(DataHandler data_handler, OwnedIterator iterator) { var data = iterator.next(); - var outputs = test_step(data_handler, data[0], data[1]); + var outputs = data.Length == 2 ? test_step(data_handler, data[0], data[1]) : + test_step(data_handler, data[0], data[1], data[2]); tf_with(ops.control_dependencies(new object[0]), ctl => _test_counter.assign_add(1)); return outputs; } + Dictionary test_step_multi_inputs_function(DataHandler data_handler, OwnedIterator iterator) { var data = iterator.next(); var x_size = data_handler.DataAdapter.GetDataset().FirstInputTensorCount; - var outputs = train_step(data_handler, new Tensors(data.Take(x_size)), new Tensors(data.Skip(x_size))); - tf_with(ops.control_dependencies(new object[0]), ctl => _train_counter.assign_add(1)); + var outputs = data.Length == 2 ? + test_step(data_handler, new Tensors(data.Take(x_size).ToArray()), new Tensors(data.Skip(x_size).ToArray())) : + test_step( + data_handler, + new Tensors(data.Take(x_size).ToArray()), + new Tensors(data.Skip(x_size).Take(x_size).ToArray()), + new Tensors(data.Skip(2 * x_size).ToArray())); + tf_with(ops.control_dependencies(new object[0]), ctl => _test_counter.assign_add(1)); return outputs; } - Dictionary test_step(DataHandler data_handler, Tensor x, Tensor y) + + + Dictionary test_step(DataHandler data_handler, Tensors x, Tensors y) { - (x, y) = data_handler.DataAdapter.Expand1d(x, y); + (x,y) = data_handler.DataAdapter.Expand1d(x, y); + var y_pred = Apply(x, training: false); - var loss = compiled_loss.Call(y, y_pred); + var loss = compiled_loss.Call(y, y_pred); compiled_metrics.update_state(y, y_pred); + return metrics.Select(x => (x.Name, x.result())).ToDictionary(x => x.Item1, x => (float)x.Item2); + } - return metrics.Select(x => (x.Name, x.result())).ToDictionary(x=>x.Item1, x=>(float)x.Item2); + Dictionary test_step(DataHandler data_handler, Tensors x, Tensors y, Tensors sample_weight) + { + (x, y, sample_weight) = data_handler.DataAdapter.Expand1d(x, y, sample_weight); + var y_pred = Apply(x, training: false); + var loss = compiled_loss.Call(y, y_pred, sample_weight: sample_weight); + compiled_metrics.update_state(y, y_pred); + return metrics.Select(x => (x.Name, x.result())).ToDictionary(x => x.Item1, x => (float)x.Item2); } } } diff --git a/src/TensorFlowNET.Keras/Engine/Model.Fit.cs b/src/TensorFlowNET.Keras/Engine/Model.Fit.cs index bb8e18ccf..e1303513e 100644 --- a/src/TensorFlowNET.Keras/Engine/Model.Fit.cs +++ b/src/TensorFlowNET.Keras/Engine/Model.Fit.cs @@ -6,10 +6,13 @@ using Tensorflow.Keras.Engine.DataAdapters; using System.Diagnostics; using Tensorflow.Keras.Callbacks; -using System.Data; +using Tensorflow.Util; +using OneOf; namespace Tensorflow.Keras.Engine { + + public partial class Model { /// @@ -19,19 +22,30 @@ public partial class Model /// /// /// - /// /// + /// /// /// /// + /// + /// + /// + /// + /// + /// + /// + /// public ICallback fit(NDArray x, NDArray y, int batch_size = -1, int epochs = 1, int verbose = 1, List callbacks = null, float validation_split = 0f, - (NDArray val_x, NDArray val_y)? validation_data = null, + ValidationDataPack validation_data = null, + int validation_step = 10, bool shuffle = true, + Dictionary class_weight = null, + NDArray sample_weight = null, int initial_epoch = 0, int max_queue_size = 10, int workers = 1, @@ -43,25 +57,24 @@ public ICallback fit(NDArray x, NDArray y, $"The array x and y should have same value at dim 0, but got {x.dims[0]} and {y.dims[0]}"); } - var train_x = x; - var train_y = y; + // The default dtype in NDArray is double, so we need to cast sample_weight to float to mul with loss which's dtype is float. + sample_weight = sample_weight?.astype(TF_DataType.TF_FLOAT); if (validation_split != 0f && validation_data == null) { - int train_count = Convert.ToInt32(x.dims[0] * (1 - validation_split)); - train_x = x[new Slice(0, train_count)]; - train_y = y[new Slice(0, train_count)]; - validation_data = (val_x: x[new Slice(train_count)], val_y: y[new Slice(train_count)]); + ((x, y, sample_weight), validation_data) = DataAdapter.train_validation_split((x, y, sample_weight), validation_split); } var data_handler = new DataHandler(new DataHandlerArgs { - X = train_x, - Y = train_y, + X = x, + Y = y, + SampleWeight = sample_weight, BatchSize = batch_size, InitialEpoch = initial_epoch, Epochs = epochs, Shuffle = shuffle, + ClassWeight = class_weight, MaxQueueSize = max_queue_size, Workers = workers, UseMultiprocessing = use_multiprocessing, @@ -73,14 +86,17 @@ public ICallback fit(NDArray x, NDArray y, train_step_func: train_step_function); } + public ICallback fit(IEnumerable x, NDArray y, int batch_size = -1, int epochs = 1, int verbose = 1, List callbacks = null, float validation_split = 0f, - (IEnumerable val_x, NDArray val_y)? validation_data = null, + ValidationDataPack validation_data = null, bool shuffle = true, + Dictionary class_weight = null, + NDArray sample_weight = null, int initial_epoch = 0, int max_queue_size = 10, int workers = 1, @@ -95,27 +111,24 @@ public ICallback fit(IEnumerable x, NDArray y, } } - var train_x = x; - var train_y = y; + sample_weight = sample_weight?.astype(TF_DataType.TF_FLOAT); + if (validation_split != 0f && validation_data == null) { - int train_count = Convert.ToInt32(y.dims[0] * (1 - validation_split)); - train_x = x.Select(x => x[new Slice(0, train_count)] as NDArray); - train_y = y[new Slice(0, train_count)]; - var val_x = x.Select(x => x[new Slice(train_count)] as NDArray); - var val_y = y[new Slice(train_count)]; - validation_data = (val_x, val_y); + ((x, y, sample_weight), validation_data) = DataAdapter.train_validation_split((x, y, sample_weight), validation_split); } var data_handler = new DataHandler(new DataHandlerArgs { - X = new Tensors(train_x), - Y = train_y, + X = new Tensors(x.ToArray()), + Y = y, + SampleWeight = sample_weight, BatchSize = batch_size, InitialEpoch = initial_epoch, Epochs = epochs, Shuffle = shuffle, + ClassWeight = class_weight, MaxQueueSize = max_queue_size, Workers = workers, UseMultiprocessing = use_multiprocessing, @@ -136,13 +149,15 @@ public ICallback fit(IEnumerable x, NDArray y, } } - public History fit(IDatasetV2 dataset, + public ICallback fit(IDatasetV2 dataset, int batch_size = -1, int epochs = 1, int verbose = 1, List callbacks = null, IDatasetV2 validation_data = null, + int validation_step = 10, bool shuffle = true, + Dictionary class_weight = null, int initial_epoch = 0, int max_queue_size = 10, int workers = 1, @@ -156,6 +171,7 @@ public History fit(IDatasetV2 dataset, InitialEpoch = initial_epoch, Epochs = epochs, Shuffle = shuffle, + ClassWeight = class_weight, MaxQueueSize = max_queue_size, Workers = workers, UseMultiprocessing = use_multiprocessing, @@ -163,12 +179,23 @@ public History fit(IDatasetV2 dataset, StepsPerExecution = _steps_per_execution }); + Func> trainStepFunction; - return FitInternal(data_handler, epochs, verbose, callbacks, validation_data: validation_data, - train_step_func: train_step_function); + if (data_handler.DataAdapter.GetDataset().structure.Length > 2 || + data_handler.DataAdapter.GetDataset().FirstInputTensorCount > 1) + { + trainStepFunction = train_step_multi_inputs_function; + } + else + { + trainStepFunction = train_step_function; + } + + return FitInternal(data_handler, epochs, validation_step, verbose, callbacks, validation_data: validation_data, + train_step_func: trainStepFunction); } - History FitInternal(DataHandler data_handler, int epochs, int verbose, List callbackList, IDatasetV2 validation_data, + History FitInternal(DataHandler data_handler, int epochs, int validation_step, int verbose, List callbackList, IDatasetV2 validation_data, Func> train_step_func) { stop_training = false; @@ -203,10 +230,14 @@ History FitInternal(DataHandler data_handler, int epochs, int verbose, List 0 && epoch ==0 || (epoch) % validation_step != 0) + continue; + var val_logs = evaluate(validation_data); foreach(var log in val_logs) { @@ -215,17 +246,20 @@ History FitInternal(DataHandler data_handler, int epochs, int verbose, List callbackList, (NDArray, NDArray)? validation_data, + History FitInternal(DataHandler data_handler, int epochs, int verbose, List callbackList, ValidationDataPack validation_data, Func> train_step_func) { stop_training = false; @@ -260,13 +294,29 @@ History FitInternal(DataHandler data_handler, int epochs, int verbose, List val_logs; + if (!validation_data.val_x_is_array) + { + (val_x, val_y, val_sample_weight) = validation_data; + // Because evaluate calls call_test_batch_end, this interferes with our output on the screen + // so we need to pass a is_val parameter to stop on_test_batch_end + val_logs = evaluate(val_x, val_y, sample_weight: val_sample_weight, is_val: true); + + } + else + { + (val_x_array, val_y, val_sample_weight, _) = validation_data; + val_logs = evaluate(val_x_array, val_y, sample_weight: val_sample_weight, is_val: true); + } foreach (var log in val_logs) { logs["val_" + log.Key] = log.Value; @@ -278,66 +328,14 @@ History FitInternal(DataHandler data_handler, int epochs, int verbose, List callbackList, (IEnumerable, NDArray)? validation_data, - Func> train_step_func) - { - stop_training = false; - _train_counter.assign(0); - var callbacks = new CallbackList(new CallbackParams - { - Model = this, - Verbose = verbose, - Epochs = epochs, - Steps = data_handler.Inferredsteps - }); - - if (callbackList != null) - { - foreach (var callback in callbackList) - callbacks.callbacks.add(callback); - } - - callbacks.on_train_begin(); - - foreach (var (epoch, iterator) in data_handler.enumerate_epochs()) - { - reset_metrics(); - callbacks.on_epoch_begin(epoch); - // data_handler.catch_stop_iteration(); - var logs = new Dictionary(); - long End_step = 0; - foreach (var step in data_handler.steps()) + if (stop_training) { - callbacks.on_train_batch_begin(step); - logs = train_step_func(data_handler, iterator); - var end_step = step + data_handler.StepIncrement; - End_step = end_step; - callbacks.on_train_batch_end(end_step, logs); - } - - if (validation_data != null) - { - var val_logs = evaluate(validation_data.Value.Item1, validation_data.Value.Item2); - foreach (var log in val_logs) - { - logs["val_" + log.Key] = log.Value; - callbacks.on_train_batch_end(End_step, logs); - } + break; } - - callbacks.on_epoch_end(epoch, logs); - - GC.Collect(); - GC.WaitForPendingFinalizers(); } return callbacks.History; } + } } diff --git a/src/TensorFlowNET.Keras/Engine/Model.Predict.cs b/src/TensorFlowNET.Keras/Engine/Model.Predict.cs index 984bcb5dc..e3a5aba68 100644 --- a/src/TensorFlowNET.Keras/Engine/Model.Predict.cs +++ b/src/TensorFlowNET.Keras/Engine/Model.Predict.cs @@ -84,7 +84,7 @@ Tensors PredictInternal(DataHandler data_handler, int verbose) Steps = data_handler.Inferredsteps }); - Tensor batch_outputs = null; + Tensors batch_outputs = null; _predict_counter.assign(0); callbacks.on_predict_begin(); foreach (var (epoch, iterator) in data_handler.enumerate_epochs()) @@ -95,15 +95,16 @@ Tensors PredictInternal(DataHandler data_handler, int verbose) var tmp_batch_outputs = run_predict_step(iterator); if (batch_outputs == null) { - batch_outputs = tmp_batch_outputs[0]; + batch_outputs = tmp_batch_outputs; } else { - batch_outputs = tf.concat(new Tensor[] { batch_outputs, tmp_batch_outputs[0] }, axis: 0); + for (int i = 0; i < batch_outputs.Length; i++) + batch_outputs[i] = tf.concat(new Tensor[] { batch_outputs[i], tmp_batch_outputs[i] }, axis: 0); } - var end_step = step + data_handler.StepIncrement; callbacks.on_predict_batch_end(end_step, new Dictionary { { "outputs", batch_outputs } }); + GC.Collect(); } } @@ -116,7 +117,7 @@ Tensors run_predict_step(OwnedIterator iterator) { var data = iterator.next(); var outputs = predict_step(data); - tf_with(ops.control_dependencies(new object[0]), ctl => _predict_counter.assign_add(1)); + tf_with(ops.control_dependencies(Array.Empty()), ctl => _predict_counter.assign_add(1)); return outputs; } diff --git a/src/TensorFlowNET.Keras/Engine/Model.Train.cs b/src/TensorFlowNET.Keras/Engine/Model.Train.cs index 905ea453a..8f1ec808c 100644 --- a/src/TensorFlowNET.Keras/Engine/Model.Train.cs +++ b/src/TensorFlowNET.Keras/Engine/Model.Train.cs @@ -12,7 +12,9 @@ public partial class Model Dictionary train_step_function(DataHandler data_handler, OwnedIterator iterator) { var data = iterator.next(); - var outputs = train_step(data_handler, data[0], data[1]); + // whether have sample_weight + var outputs = data.Length == 2 ? train_step(data_handler, data[0], data[1]) : + train_step(data_handler, data[0], data[1], data[2]); tf_with(ops.control_dependencies(new object[0]), ctl => _train_counter.assign_add(1)); return outputs; } @@ -21,7 +23,13 @@ Dictionary train_step_multi_inputs_function(DataHandler data_hand { var data = iterator.next(); var x_size = data_handler.DataAdapter.GetDataset().FirstInputTensorCount; - var outputs = train_step(data_handler, new Tensors(data.Take(x_size)), new Tensors(data.Skip(x_size))); + var outputs = data.Length == 2 ? + train_step(data_handler, new Tensors(data.Take(x_size).ToArray()), new Tensors(data.Skip(x_size).ToArray())) : + train_step( + data_handler, + new Tensors(data.Take(x_size).ToArray()), + new Tensors(data.Skip(x_size).Take(x_size).ToArray()), + new Tensors(data.Skip(2 * x_size).ToArray())); tf_with(ops.control_dependencies(new object[0]), ctl => _train_counter.assign_add(1)); return outputs; } @@ -29,7 +37,9 @@ Dictionary train_step_multi_inputs_function(DataHandler data_hand /// /// The logic for one training step. /// - /// + /// + /// + /// /// Dictionary train_step(DataHandler data_handler, Tensors x, Tensors y) { @@ -59,6 +69,34 @@ Dictionary train_step(DataHandler data_handler, Tensors x, Tensor }); return dict; } + Dictionary train_step(DataHandler data_handler, Tensors x, Tensors y, Tensors sample_weight = null) + { + (x, y, sample_weight) = data_handler.DataAdapter.Expand1d(x, y, sample_weight); + using var tape = tf.GradientTape(); + var y_pred = Apply(x, training: true); + var loss = compiled_loss.Call(y, y_pred, sample_weight:sample_weight); + + // For custom training steps, users can just write: + // trainable_variables = self.trainable_variables + // gradients = tape.gradient(loss, trainable_variables) + // self.optimizer.apply_gradients(zip(gradients, trainable_variables)) + // The _minimize call does a few extra steps unnecessary in most cases, + // such as loss scaling and gradient clipping. + _minimize(tape, optimizer, loss, TrainableVariables); + compiled_metrics.update_state(y, y_pred); + + var dict = new Dictionary(); + metrics.ToList().ForEach(x => + { + var r = x.result(); + if (r.ndim > 0) + { + r = tf.reduce_mean(r); + } + dict[x.Name] = (float)r; + }); + return dict; + } void _minimize(GradientTape tape, IOptimizer optimizer, Tensor loss, List trainable_variables) { diff --git a/src/TensorFlowNET.Keras/Engine/Model.Training.cs b/src/TensorFlowNET.Keras/Engine/Model.Training.cs index 50d934d9d..457b3d694 100644 --- a/src/TensorFlowNET.Keras/Engine/Model.Training.cs +++ b/src/TensorFlowNET.Keras/Engine/Model.Training.cs @@ -10,8 +10,38 @@ namespace Tensorflow.Keras.Engine { public partial class Model { + static Dictionary> weightsCache + = new Dictionary>(); + public void load_weights(string filepath, bool by_name = false, bool skip_mismatch = false, object options = null) { + // Get from cache + if (weightsCache.ContainsKey(filepath)) + { + var filtered_layers = new List(); + foreach (var layer in Layers) + { + var weights = hdf5_format._legacy_weights(layer); + if (weights.Count > 0) + filtered_layers.append(layer); + } + + var weight_value_tuples = new List<(IVariableV1, NDArray)>(); + filtered_layers.Select((layer, i) => + { + var symbolic_weights = hdf5_format._legacy_weights(layer); + foreach(var weight in symbolic_weights) + { + var weight_value = weightsCache[filepath].First(x => x.Item1 == weight.Name).Item2; + weight_value_tuples.Add((weight, weight_value)); + } + return layer; + }).ToList(); + + keras.backend.batch_set_value(weight_value_tuples); + return; + } + long fileId = Hdf5.OpenFile(filepath, true); if(fileId < 0) { @@ -29,8 +59,11 @@ public void load_weights(string filepath, bool by_name = false, bool skip_mismat throw new NotImplementedException(""); else { - hdf5_format.load_weights_from_hdf5_group(fileId, Layers); + var weight_value_tuples = hdf5_format.load_weights_from_hdf5_group(fileId, Layers); Hdf5.CloseFile(fileId); + + weightsCache[filepath] = weight_value_tuples.Select(x => (x.Item1.Name, x.Item2)).ToList(); + keras.backend.batch_set_value(weight_value_tuples); } } diff --git a/src/TensorFlowNET.Keras/Engine/Model.cs b/src/TensorFlowNET.Keras/Engine/Model.cs index 83702b23a..7b35d5477 100644 --- a/src/TensorFlowNET.Keras/Engine/Model.cs +++ b/src/TensorFlowNET.Keras/Engine/Model.cs @@ -1,8 +1,8 @@ using System.Diagnostics; +using Tensorflow.Common.Types; using Tensorflow.Framework.Models; using Tensorflow.Keras.ArgsDefinition; using Tensorflow.Keras.Losses; -using Tensorflow.Keras.Saving; using Tensorflow.Keras.Saving.SavedModel; using Tensorflow.Keras.Utils; using Tensorflow.Train; diff --git a/src/TensorFlowNET.Keras/Engine/Sequential.cs b/src/TensorFlowNET.Keras/Engine/Sequential.cs index 90167a9d9..6a468ad27 100644 --- a/src/TensorFlowNET.Keras/Engine/Sequential.cs +++ b/src/TensorFlowNET.Keras/Engine/Sequential.cs @@ -21,6 +21,7 @@ limitations under the License. using Tensorflow.Keras.Layers; using Tensorflow.Keras.Utils; using static Tensorflow.KerasApi; +using Tensorflow.Common.Types; namespace Tensorflow.Keras.Engine { @@ -62,7 +63,17 @@ public void InitLayers(IEnumerable layers) { foreach(var layer in layers) { + // TODO(Rinne): remove it and completely fix issue 1084 + if(layer is Sequential s) + { + s.Layers.ForEach(x => ((Layer)x).enforce_layer_construction()); + } add(layer); + // TODO(Rinne): remove it and completely fix issue 1084 + if (layer is Sequential s2) + { + s2.Layers.ForEach(x => ((Layer)x).unset_layer_construction()); + } } } @@ -133,7 +144,7 @@ public void add(ILayer layer) } } - protected override Tensors Call(Tensors inputs, Tensor state = null, bool? training = null) + protected override Tensors Call(Tensors inputs, Tensors state = null, bool? training = null, IOptionalArgs? optional_args = null) { if (!_has_explicit_input_shape) { @@ -163,7 +174,7 @@ void _build_graph_network_for_inferred_shape(Shape input_shape, TF_DataType inpu Tensors layer_output = null; Tensors outputs = null; List created_nodes = new List(); - foreach (var layer in args.Layers) + foreach (var layer in Layers) { clear_previously_created_nodes(layer, _created_nodes); layer_output = layer.Apply(layer_input); diff --git a/src/TensorFlowNET.Keras/GlobalUsing.cs b/src/TensorFlowNET.Keras/GlobalUsing.cs index bc0798ede..85cd9194c 100644 --- a/src/TensorFlowNET.Keras/GlobalUsing.cs +++ b/src/TensorFlowNET.Keras/GlobalUsing.cs @@ -4,4 +4,5 @@ global using System.Linq; global using static Tensorflow.Binding; global using static Tensorflow.KerasApi; -global using Tensorflow.NumPy; \ No newline at end of file +global using Tensorflow.NumPy; +global using Tensorflow.Keras.Engine; \ No newline at end of file diff --git a/src/TensorFlowNET.Keras/IsExternalInit.cs b/src/TensorFlowNET.Keras/IsExternalInit.cs new file mode 100644 index 000000000..11f062fa8 --- /dev/null +++ b/src/TensorFlowNET.Keras/IsExternalInit.cs @@ -0,0 +1,4 @@ +namespace System.Runtime.CompilerServices +{ + internal static class IsExternalInit { } +} diff --git a/src/TensorFlowNET.Keras/KerasInterface.cs b/src/TensorFlowNET.Keras/KerasInterface.cs index 159564aac..6bc381095 100644 --- a/src/TensorFlowNET.Keras/KerasInterface.cs +++ b/src/TensorFlowNET.Keras/KerasInterface.cs @@ -72,8 +72,8 @@ public Sequential Sequential(params ILayer[] layers) /// /// `Model` groups layers into an object with training and inference features. /// - /// - /// + /// + /// /// public IModel Model(Tensors inputs, Tensors outputs, string name = null) => new Functional(inputs, outputs, name: name); diff --git a/src/TensorFlowNET.Keras/Layers/Activation/ELU.cs b/src/TensorFlowNET.Keras/Layers/Activation/ELU.cs index 739c0d56f..23f36c862 100644 --- a/src/TensorFlowNET.Keras/Layers/Activation/ELU.cs +++ b/src/TensorFlowNET.Keras/Layers/Activation/ELU.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Text; +using Tensorflow.Common.Types; using Tensorflow.Keras.ArgsDefinition; using Tensorflow.Keras.Engine; using Tensorflow.Keras.Saving; @@ -29,7 +30,7 @@ public override void build(KerasShapesWrapper input_shape) base.build(input_shape); } - protected override Tensors Call(Tensors inputs, Tensor state = null, bool? training = null) + protected override Tensors Call(Tensors inputs, Tensors state = null, bool? training = null, IOptionalArgs? optional_args = null) { Tensor output = inputs; output = tf.where(output > 0f, output, diff --git a/src/TensorFlowNET.Keras/Layers/Activation/Exponential.cs b/src/TensorFlowNET.Keras/Layers/Activation/Exponential.cs index 17636302f..81fefb314 100644 --- a/src/TensorFlowNET.Keras/Layers/Activation/Exponential.cs +++ b/src/TensorFlowNET.Keras/Layers/Activation/Exponential.cs @@ -4,7 +4,7 @@ using Tensorflow.Keras.ArgsDefinition; using Tensorflow.Keras.Engine; using Tensorflow.Keras.Saving; -using static Tensorflow.Binding; +using Tensorflow.Common.Types; namespace Tensorflow.Keras.Layers { public class Exponential : Layer @@ -17,7 +17,7 @@ public override void build(KerasShapesWrapper input_shape) { base.build(input_shape); } - protected override Tensors Call(Tensors inputs, Tensor state = null, bool? training = null) + protected override Tensors Call(Tensors inputs, Tensors state = null, bool? training = null, IOptionalArgs? optional_args = null) { Tensor output = inputs; return tf.exp(output); diff --git a/src/TensorFlowNET.Keras/Layers/Activation/HardSigmoid.cs b/src/TensorFlowNET.Keras/Layers/Activation/HardSigmoid.cs index b498d1b94..e0f91380b 100644 --- a/src/TensorFlowNET.Keras/Layers/Activation/HardSigmoid.cs +++ b/src/TensorFlowNET.Keras/Layers/Activation/HardSigmoid.cs @@ -3,6 +3,7 @@ using System.Text; using Tensorflow.Keras.ArgsDefinition; using Tensorflow.Keras.Engine; +using Tensorflow.Common.Types; using static Tensorflow.Binding; namespace Tensorflow.Keras.Layers { @@ -10,7 +11,7 @@ public class HardSigmoid : Layer { public HardSigmoid ( LayerArgs args ) : base(args) { // hard sigmoid has no arguments } - protected override Tensors Call ( Tensors inputs, Tensor state = null, bool? training = null ) { + protected override Tensors Call ( Tensors inputs, Tensors state = null, bool? training = null, IOptionalArgs? optional_args = null ) { Tensor x = inputs; return tf.clip_by_value( tf.add(tf.multiply(x, 0.2f), 0.5f), 0f, 1f); diff --git a/src/TensorFlowNET.Keras/Layers/Activation/LeakyReLu.cs b/src/TensorFlowNET.Keras/Layers/Activation/LeakyReLu.cs index 1fbbf4eaf..cfbd0186d 100644 --- a/src/TensorFlowNET.Keras/Layers/Activation/LeakyReLu.cs +++ b/src/TensorFlowNET.Keras/Layers/Activation/LeakyReLu.cs @@ -3,6 +3,7 @@ using System.Text; using Tensorflow.Keras.ArgsDefinition; using Tensorflow.Keras.Engine; +using Tensorflow.Common.Types; using static Tensorflow.Binding; namespace Tensorflow.Keras.Layers @@ -19,7 +20,7 @@ public LeakyReLu(LeakyReLuArgs args) : base(args) this.args = args; } - protected override Tensors Call(Tensors inputs, Tensor state = null, bool? training = null) + protected override Tensors Call(Tensors inputs, Tensors state = null, bool? training = null, IOptionalArgs? optional_args = null) { return tf.nn.leaky_relu(inputs, alpha: alpha); } diff --git a/src/TensorFlowNET.Keras/Layers/Activation/ReLu6.cs b/src/TensorFlowNET.Keras/Layers/Activation/ReLu6.cs new file mode 100644 index 000000000..5af3f7677 --- /dev/null +++ b/src/TensorFlowNET.Keras/Layers/Activation/ReLu6.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Tensorflow.Keras.ArgsDefinition; +using Tensorflow.Keras.Engine; +using Tensorflow.Common.Types; +using static Tensorflow.Binding; + +namespace Tensorflow.Keras.Layers +{ + /// + /// Leaky version of a Rectified Linear Unit. + /// + public class ReLu6 : Layer + { + public ReLu6() : base(new LayerArgs { }) + { + } + + protected override Tensors Call(Tensors inputs, Tensors state = null, bool? training = null, IOptionalArgs? optional_args = null) + { + return tf.nn.relu6(inputs); + } + } +} diff --git a/src/TensorFlowNET.Keras/Layers/Activation/SELU.cs b/src/TensorFlowNET.Keras/Layers/Activation/SELU.cs index 53101fbb4..2e943d5f7 100644 --- a/src/TensorFlowNET.Keras/Layers/Activation/SELU.cs +++ b/src/TensorFlowNET.Keras/Layers/Activation/SELU.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Text; +using Tensorflow.Common.Types; using Tensorflow.Keras.ArgsDefinition; using Tensorflow.Keras.Engine; using Tensorflow.Keras.Saving; @@ -22,7 +23,7 @@ public override void build(KerasShapesWrapper input_shape) { } base.build(input_shape); } - protected override Tensors Call ( Tensors inputs, Tensor state = null, bool? training = null ) { + protected override Tensors Call ( Tensors inputs, Tensors state = null, bool? training = null, IOptionalArgs? optional_args = null) { Tensor output = inputs; return tf.where(output > 0f, tf.multiply(scale, output), diff --git a/src/TensorFlowNET.Keras/Layers/Activation/Softmax.cs b/src/TensorFlowNET.Keras/Layers/Activation/Softmax.cs index 3ffae27f6..d018128d5 100644 --- a/src/TensorFlowNET.Keras/Layers/Activation/Softmax.cs +++ b/src/TensorFlowNET.Keras/Layers/Activation/Softmax.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Text; +using Tensorflow.Common.Types; using Tensorflow.Keras.ArgsDefinition; using Tensorflow.Keras.Engine; using static Tensorflow.Binding; @@ -11,8 +12,8 @@ public class Softmax : Layer { public Softmax ( SoftmaxArgs args ) : base(args) { axis = args.axis; } - protected override Tensors Call ( Tensors inputs, Tensor state = null, bool? training = null ) { - Tensor x = inputs.Length == 2 ? inputs + ((1.0 - tf.cast(inputs[1], inputs.dtype)) * 1e-9) + protected override Tensors Call ( Tensors inputs, Tensors state = null, bool? training = null, IOptionalArgs? optional_args = null) { + Tensor x = inputs.Length == 2 ? inputs[0] + ((1.0 - tf.cast(inputs[1], inputs.dtype)) * 1e-9) : inputs; Tensor e = tf.exp(tf.sub(x, tf.reduce_max(x, axis: this.axis, keepdims: true))); Tensor s = tf.reduce_sum(e, axis: this.axis, keepdims: true); diff --git a/src/TensorFlowNET.Keras/Layers/Activation/Softplus.cs b/src/TensorFlowNET.Keras/Layers/Activation/Softplus.cs index e82b01982..1e6c59b42 100644 --- a/src/TensorFlowNET.Keras/Layers/Activation/Softplus.cs +++ b/src/TensorFlowNET.Keras/Layers/Activation/Softplus.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Text; +using Tensorflow.Common.Types; using Tensorflow.Keras.ArgsDefinition; using Tensorflow.Keras.Engine; using static Tensorflow.Binding; @@ -10,7 +11,7 @@ public class Softplus : Layer { public Softplus ( LayerArgs args ) : base(args) { // Softplus has no arguments } - protected override Tensors Call ( Tensors inputs, Tensor state = null, bool? training = null ) { + protected override Tensors Call ( Tensors inputs, Tensors state = null, bool? training = null, IOptionalArgs? optional_args = null) { Tensor x = inputs; return tf.log( tf.add(tf.exp(x), 1f)); diff --git a/src/TensorFlowNET.Keras/Layers/Activation/Softsign.cs b/src/TensorFlowNET.Keras/Layers/Activation/Softsign.cs index 59329fd44..5ad33e99d 100644 --- a/src/TensorFlowNET.Keras/Layers/Activation/Softsign.cs +++ b/src/TensorFlowNET.Keras/Layers/Activation/Softsign.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Text; +using Tensorflow.Common.Types; using Tensorflow.Keras.ArgsDefinition; using Tensorflow.Keras.Engine; using static Tensorflow.Binding; @@ -10,7 +11,7 @@ public class Softsign : Layer { public Softsign ( LayerArgs args ) : base(args) { // Softsign has no arguments } - protected override Tensors Call ( Tensors inputs, Tensor state = null, bool? training = null ) { + protected override Tensors Call ( Tensors inputs, Tensors state = null, bool? training = null, IOptionalArgs? optional_args = null) { Tensor x = inputs; // x / (abs(x) + 1) return tf.div(x, tf.add(1f, tf.abs(x))); diff --git a/src/TensorFlowNET.Keras/Layers/Activation/Swish.cs b/src/TensorFlowNET.Keras/Layers/Activation/Swish.cs index 1dcb92b31..ed0d105a6 100644 --- a/src/TensorFlowNET.Keras/Layers/Activation/Swish.cs +++ b/src/TensorFlowNET.Keras/Layers/Activation/Swish.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Text; +using Tensorflow.Common.Types; using Tensorflow.Keras.ArgsDefinition; using Tensorflow.Keras.Engine; using static Tensorflow.Binding; @@ -10,7 +11,7 @@ public class Swish : Layer { public Swish ( LayerArgs args ) : base(args) { // Swish has no arguments } - protected override Tensors Call ( Tensors inputs, Tensor state = null, bool? training = null ) { + protected override Tensors Call ( Tensors inputs, Tensors state = null, bool? training = null, IOptionalArgs? optional_args = null) { Tensor x = inputs; // x / (1 + exp(-x)) diff --git a/src/TensorFlowNET.Keras/Layers/Activation/Tanh.cs b/src/TensorFlowNET.Keras/Layers/Activation/Tanh.cs index 99b803942..7e90cf9d8 100644 --- a/src/TensorFlowNET.Keras/Layers/Activation/Tanh.cs +++ b/src/TensorFlowNET.Keras/Layers/Activation/Tanh.cs @@ -3,6 +3,7 @@ using System.Text; using Tensorflow.Keras.ArgsDefinition; using Tensorflow.Keras.Engine; +using Tensorflow.Common.Types; using static Tensorflow.Binding; namespace Tensorflow.Keras.Layers @@ -13,7 +14,7 @@ public Tanh(LayerArgs args) : base(args) { // Tanh has no arguments } - protected override Tensors Call(Tensors inputs, Tensor state = null, bool? training = null) + protected override Tensors Call(Tensors inputs, Tensors state = null, bool? training = null, IOptionalArgs? optional_args = null) { Tensor x = inputs; diff --git a/src/TensorFlowNET.Keras/Layers/Attention/BaseDenseAttention.cs b/src/TensorFlowNET.Keras/Layers/Attention/BaseDenseAttention.cs index 1348e19cf..970a938d2 100644 --- a/src/TensorFlowNET.Keras/Layers/Attention/BaseDenseAttention.cs +++ b/src/TensorFlowNET.Keras/Layers/Attention/BaseDenseAttention.cs @@ -1,23 +1,18 @@ using Tensorflow.Keras.Engine; using Tensorflow.Keras.ArgsDefinition; -using static Tensorflow.Binding; -using static Tensorflow.KerasApi; using System; using System.Collections.Generic; using System.Linq; using Tensorflow.Keras.Saving; - -/// -/// Base class for attention layers that can be used in sequence DNN/CNN models. -///This file follows the terminology of https://arxiv.org/abs/1706.03762 Figure 2. -///Attention is formed by three tensors: Query, Key and Value. -/// +using Tensorflow.Common.Types; namespace Tensorflow.Keras.Layers { /// /// Base Attention class for Dense networks. + /// This file follows the terminology of https://arxiv.org/abs/1706.03762 Figure 2. + /// Attention is formed by three tensors: Query, Key and Value. /// This class is suitable for Dense or CNN networks, and not for RNN networks. /// Implementations of attention mechanisms should inherit from this class, and /// reuse the `apply_attention_scores()` method. @@ -114,7 +109,7 @@ public virtual Tensor _calculate_scores(Tensor query, Tensor key) => return (tf.linalg.einsum("bij,bjk->bik", (weights, value)), weights); } - protected override Tensors Call(Tensors inputs, Tensor state = null, bool? training = null) + protected override Tensors Call(Tensors inputs, Tensors state = null, bool? training = null, IOptionalArgs? optional_args = null) { Tensors _inp; Tensors _mask = null; diff --git a/src/TensorFlowNET.Keras/Layers/Attention/MultiHeadAttention.cs b/src/TensorFlowNET.Keras/Layers/Attention/MultiHeadAttention.cs index 701724d5b..75dd4a41a 100644 --- a/src/TensorFlowNET.Keras/Layers/Attention/MultiHeadAttention.cs +++ b/src/TensorFlowNET.Keras/Layers/Attention/MultiHeadAttention.cs @@ -6,6 +6,7 @@ using static Tensorflow.KerasApi; using System; using System.Linq; +using Tensorflow.Common.Types; namespace Tensorflow.Keras.Layers { @@ -252,7 +253,7 @@ public Tensors _compute_attention( return (attention_output, attention_scores); } - protected override Tensors Call(Tensors inputs, Tensor state = null, bool? training = null) + protected override Tensors Call(Tensors inputs, Tensors state = null, bool? training = null, IOptionalArgs? optional_args = null) { Tensors _inp; Tensor _mask = null; @@ -349,7 +350,7 @@ protected Tensors call(Tensors inputs, //} if (return_attention_scores) - return (attention_output, attention_scores); + return (attention_output, attention_scores.Single); return attention_output; } } diff --git a/src/TensorFlowNET.Keras/Layers/Convolution/Conv2DTranspose.cs b/src/TensorFlowNET.Keras/Layers/Convolution/Conv2DTranspose.cs index 13bea627e..94ad79141 100644 --- a/src/TensorFlowNET.Keras/Layers/Convolution/Conv2DTranspose.cs +++ b/src/TensorFlowNET.Keras/Layers/Convolution/Conv2DTranspose.cs @@ -20,6 +20,7 @@ limitations under the License. using Tensorflow.Keras.Utils; using static Tensorflow.KerasApi; using Tensorflow.Keras.Saving; +using Tensorflow.Common.Types; namespace Tensorflow.Keras.Layers { @@ -62,7 +63,7 @@ private static Conv2DArgs InitializeUndefinedArgs(Conv2DArgs args) public override void build(KerasShapesWrapper input_shape) { var single_shape = input_shape.ToSingleShape(); - if (len(input_shape) != 4) + if (len(single_shape) != 4) throw new ValueError($"Inputs should have rank 4. Received input shape: {input_shape}"); var channel_axis = _get_channel_axis(); @@ -83,7 +84,7 @@ public override void build(KerasShapesWrapper input_shape) _buildInputShape = input_shape; } - protected override Tensors Call(Tensors inputs, Tensor state = null, bool? training = null) + protected override Tensors Call(Tensors inputs, Tensors state = null, bool? training = null, IOptionalArgs? optional_args = null) { var inputs_shape = array_ops.shape(inputs); var batch_size = inputs_shape[0]; @@ -138,7 +139,10 @@ protected override Tensors Call(Tensors inputs, Tensor state = null, bool? train } if (use_bias) - throw new NotImplementedException(""); + tf.nn.bias_add( + outputs, + bias, + data_format: conv_utils.convert_data_format(data_format, ndim: 4)); if (activation != null) return activation.Apply(outputs); diff --git a/src/TensorFlowNET.Keras/Layers/Convolution/Convolutional.cs b/src/TensorFlowNET.Keras/Layers/Convolution/Convolutional.cs index c575362c0..d8e00d520 100644 --- a/src/TensorFlowNET.Keras/Layers/Convolution/Convolutional.cs +++ b/src/TensorFlowNET.Keras/Layers/Convolution/Convolutional.cs @@ -17,6 +17,7 @@ limitations under the License. using System; using System.Collections.Generic; using System.Linq; +using Tensorflow.Common.Types; using Tensorflow.Keras.ArgsDefinition; using Tensorflow.Keras.Engine; using Tensorflow.Keras.Saving; @@ -103,7 +104,7 @@ public override void build(KerasShapesWrapper input_shape) _buildInputShape = input_shape; } - protected override Tensors Call(Tensors inputs, Tensor state = null, bool? training = false) + protected override Tensors Call(Tensors inputs, Tensors state = null, bool? training = false, IOptionalArgs? optional_args = null) { var outputs = _convolution_op.Apply(inputs, kernel.AsTensor()); if (use_bias) diff --git a/src/TensorFlowNET.Keras/Layers/Convolution/DepthwiseConv2D.cs b/src/TensorFlowNET.Keras/Layers/Convolution/DepthwiseConv2D.cs new file mode 100644 index 000000000..dae4a4036 --- /dev/null +++ b/src/TensorFlowNET.Keras/Layers/Convolution/DepthwiseConv2D.cs @@ -0,0 +1,167 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System; +using Tensorflow.Keras.ArgsDefinition; +using Tensorflow.Keras.Saving; +using Tensorflow.Common.Types; +using Tensorflow.Keras.Utils; +using Tensorflow.Operations; +using Newtonsoft.Json; +using System.Security.Cryptography; + +namespace Tensorflow.Keras.Layers +{ + public class DepthwiseConv2DArgs: Conv2DArgs + { + /// + /// depth_multiplier: The number of depthwise convolution output channels for + /// each input channel.The total number of depthwise convolution output + /// channels will be equal to `filters_in* depth_multiplier`. + /// + [JsonProperty("depth_multiplier")] + public int DepthMultiplier { get; set; } = 1; + + [JsonProperty("depthwise_initializer")] + public IInitializer DepthwiseInitializer { get; set; } + } + + public class DepthwiseConv2D : Conv2D + { + /// + /// depth_multiplier: The number of depthwise convolution output channels for + /// each input channel.The total number of depthwise convolution output + /// channels will be equal to `filters_in* depth_multiplier`. + /// + int DepthMultiplier = 1; + + IInitializer DepthwiseInitializer; + + int[] strides; + + int[] dilation_rate; + + string getDataFormat() + { + return data_format == "channels_first" ? "NCHW" : "NHWC"; + } + + static int _id = 1; + + public DepthwiseConv2D(DepthwiseConv2DArgs args):base(args) + { + args.Padding = args.Padding.ToUpper(); + + if(string.IsNullOrEmpty(args.Name)) + name = "DepthwiseConv2D_" + _id; + + this.DepthMultiplier = args.DepthMultiplier; + this.DepthwiseInitializer = args.DepthwiseInitializer; + + } + + public override void build(KerasShapesWrapper input_shape) + { + //base.build(input_shape); + + var shape = input_shape.ToSingleShape(); + + int channel_axis = data_format == "channels_first" ? 1 : -1; + var input_channel = channel_axis < 0 ? + shape.dims[shape.ndim + channel_axis] : + shape.dims[channel_axis]; + + var arg = args as DepthwiseConv2DArgs; + + if (arg.Strides.ndim != shape.ndim) + { + if (arg.Strides.ndim == 2) + { + this.strides = new int[] { 1, (int)arg.Strides[0], (int)arg.Strides[1], 1 }; + } + else + { + this.strides = conv_utils.normalize_tuple(new int[] { (int)arg.Strides[0] }, shape.ndim, "strides"); + } + } + else + { + this.strides = arg.Strides.dims.Select(o=>(int)(o)).ToArray(); + } + + if (arg.DilationRate.ndim != shape.ndim) + { + this.dilation_rate = conv_utils.normalize_tuple(new int[] { (int)arg.DilationRate[0] }, shape.ndim, "dilation_rate"); + } + + long channel_data = data_format == "channels_first" ? shape[0] : shape[shape.Length - 1]; + + var depthwise_kernel_shape = this.kernel_size.dims.concat(new long[] { + channel_data, + this.DepthMultiplier + }); + + this.kernel = this.add_weight( + shape: depthwise_kernel_shape, + initializer: this.DepthwiseInitializer != null ? this.DepthwiseInitializer : this.kernel_initializer, + name: "depthwise_kernel", + trainable: true, + dtype: DType, + regularizer: this.kernel_regularizer + ); + + var axes = new Dictionary(); + axes.Add(-1, (int)input_channel); + inputSpec = new InputSpec(min_ndim: rank + 2, axes: axes); + + + if (use_bias) + { + bias = add_weight(name: "bias", + shape: ((int)channel_data), + initializer: bias_initializer, + trainable: true, + dtype: DType); + } + + built = true; + _buildInputShape = input_shape; + } + + protected override Tensors Call(Tensors inputs, Tensors state = null, + bool? training = false, IOptionalArgs? optional_args = null) + { + Tensor outputs = null; + + outputs = gen_nn_ops.depthwise_conv2d_native( + inputs, + filter: this.kernel.AsTensor(), + strides: this.strides, + padding: this.padding, + dilations: this.dilation_rate, + data_format: this.getDataFormat(), + name: name + ); + + if (use_bias) + { + if (data_format == "channels_first") + { + throw new NotImplementedException("call channels_first"); + } + else + { + outputs = gen_nn_ops.bias_add(outputs, ops.convert_to_tensor(bias), + data_format: this.getDataFormat(), name: name); + } + } + + if (activation != null) + outputs = activation.Apply(outputs); + + + return outputs; + } + + } +} \ No newline at end of file diff --git a/src/TensorFlowNET.Keras/Layers/Core/Dense.cs b/src/TensorFlowNET.Keras/Layers/Core/Dense.cs index b1cc2446c..db5d626ed 100644 --- a/src/TensorFlowNET.Keras/Layers/Core/Dense.cs +++ b/src/TensorFlowNET.Keras/Layers/Core/Dense.cs @@ -18,6 +18,7 @@ limitations under the License. using System.Collections.Generic; using System.Diagnostics; using System.Linq; +using Tensorflow.Common.Types; using Tensorflow.Keras.ArgsDefinition; using Tensorflow.Keras.Engine; using Tensorflow.Keras.Saving; @@ -69,7 +70,7 @@ public override void build(KerasShapesWrapper input_shape) built = true; } - protected override Tensors Call(Tensors inputs, Tensor state = null, bool? training = null) + protected override Tensors Call(Tensors inputs, Tensors state = null, bool? training = null, IOptionalArgs? optional_args = null) { Tensor outputs = null; var rank = inputs.rank; @@ -79,7 +80,7 @@ protected override Tensors Call(Tensors inputs, Tensor state = null, bool? train } else { - outputs = gen_math_ops.mat_mul(inputs, kernel.AsTensor()); + outputs = math_ops.matmul(inputs, kernel.AsTensor()); } if (args.UseBias) diff --git a/src/TensorFlowNET.Keras/Layers/Core/EinsumDense.cs b/src/TensorFlowNET.Keras/Layers/Core/EinsumDense.cs index fb604f77e..0cbd50846 100644 --- a/src/TensorFlowNET.Keras/Layers/Core/EinsumDense.cs +++ b/src/TensorFlowNET.Keras/Layers/Core/EinsumDense.cs @@ -7,6 +7,7 @@ using Tensorflow.Keras.Engine; using Tensorflow.Keras.ArgsDefinition.Core; using Tensorflow.Keras.Saving; +using Tensorflow.Common.Types; namespace Tensorflow.Keras.Layers { @@ -189,7 +190,7 @@ public override Shape ComputeOutputShape(Shape input_shape) // return new dict(base_config.items().ToList() + config.items().ToList()); //} - protected override Tensors Call(Tensors inputs, Tensor state = null, bool? training = null) + protected override Tensors Call(Tensors inputs, Tensors state = null, bool? training = null, IOptionalArgs? optional_args = null) { var ret = tf.linalg.einsum(this.equation, (inputs, this.kernel.AsTensor())); if (this.bias != null) diff --git a/src/TensorFlowNET.Keras/Layers/Core/Embedding.cs b/src/TensorFlowNET.Keras/Layers/Core/Embedding.cs index 9487a7d00..87b42bb7b 100644 --- a/src/TensorFlowNET.Keras/Layers/Core/Embedding.cs +++ b/src/TensorFlowNET.Keras/Layers/Core/Embedding.cs @@ -15,6 +15,7 @@ limitations under the License. ******************************************************************************/ using System.Linq; +using Tensorflow.Common.Types; using Tensorflow.Keras.ArgsDefinition; using Tensorflow.Keras.Engine; using Tensorflow.Keras.Saving; @@ -66,7 +67,7 @@ public override void build(KerasShapesWrapper input_shape) _buildInputShape = input_shape; } - protected override Tensors Call(Tensors inputs, Tensor state = null, bool? training = null) + protected override Tensors Call(Tensors inputs, Tensors state = null, bool? training = null, IOptionalArgs? optional_args = null) { var dtype = inputs.dtype; if (dtype != tf.int32 && dtype != tf.int64) diff --git a/src/TensorFlowNET.Keras/Layers/LayersApi.Activation.cs b/src/TensorFlowNET.Keras/Layers/LayersApi.Activation.cs index 280e91e2c..2c55f8fd5 100644 --- a/src/TensorFlowNET.Keras/Layers/LayersApi.Activation.cs +++ b/src/TensorFlowNET.Keras/Layers/LayersApi.Activation.cs @@ -10,14 +10,14 @@ public partial class LayersApi { public ILayer ELU ( float alpha = 0.1f ) => new ELU(new ELUArgs { Alpha = alpha }); public ILayer SELU () - => new SELU(new LayerArgs { }); + => new SELU(new SELUArgs { }); public ILayer Softmax(int axis = -1) => new Softmax(new SoftmaxArgs { axis = axis }); public ILayer Softmax ( Axis axis ) => new Softmax(new SoftmaxArgs { axis = axis }); - public ILayer Softplus () => new Softplus(new LayerArgs { }); - public ILayer HardSigmoid () => new HardSigmoid(new LayerArgs { }); - public ILayer Softsign () => new Softsign(new LayerArgs { }); - public ILayer Swish () => new Swish(new LayerArgs { }); - public ILayer Tanh () => new Tanh(new LayerArgs { }); - public ILayer Exponential () => new Exponential(new LayerArgs { }); + public ILayer Softplus () => new Softplus(new SoftplusArgs { }); + public ILayer HardSigmoid () => new HardSigmoid(new HardSigmoidArgs { }); + public ILayer Softsign () => new Softsign(new SoftsignArgs { }); + public ILayer Swish () => new Swish(new SwishArgs { }); + public ILayer Tanh () => new Tanh(new TanhArgs { }); + public ILayer Exponential () => new Exponential(new ExponentialArgs { }); } } diff --git a/src/TensorFlowNET.Keras/Layers/LayersApi.Merging.cs b/src/TensorFlowNET.Keras/Layers/LayersApi.Merging.cs index d94bfb4d8..bf06b1418 100644 --- a/src/TensorFlowNET.Keras/Layers/LayersApi.Merging.cs +++ b/src/TensorFlowNET.Keras/Layers/LayersApi.Merging.cs @@ -14,7 +14,7 @@ public partial class LayersApi /// Axis along which to concatenate. /// public ILayer Concatenate(int axis = -1) - => new Concatenate(new MergeArgs + => new Concatenate(new ConcatenateArgs { Axis = axis }); diff --git a/src/TensorFlowNET.Keras/Layers/LayersApi.Reshaping.cs b/src/TensorFlowNET.Keras/Layers/LayersApi.Reshaping.cs index d3db1d663..2ee99bc79 100644 --- a/src/TensorFlowNET.Keras/Layers/LayersApi.Reshaping.cs +++ b/src/TensorFlowNET.Keras/Layers/LayersApi.Reshaping.cs @@ -6,35 +6,48 @@ namespace Tensorflow.Keras.Layers { public partial class LayersApi { - /// - /// Zero-padding layer for 2D input (e.g. picture). - /// - /// - /// - public ILayer ZeroPadding2D ( NDArray padding ) + + /// + /// Upsampling layer for 1D inputs. Repeats each temporal step `size` times along the time axis. + /// + /// + /// + public ILayer UpSampling1D(int size) + => new UpSampling1D(new UpSampling1DArgs + { + Size = size + }); + + /// + /// Zero-padding layer for 2D input (e.g. picture). + /// + /// + /// + public ILayer ZeroPadding2D ( NDArray padding ) => new ZeroPadding2D(new ZeroPadding2DArgs { Padding = padding }); - /// - /// Upsampling layer for 2D inputs.
- /// Repeats the rows and columns of the data by size[0] and size[1] respectively. - ///
- /// - /// - /// - /// - public ILayer UpSampling2D ( Shape size = null, - string data_format = null, - string interpolation = "nearest" ) - => new UpSampling2D(new UpSampling2DArgs { - Size = size ?? (2, 2) - }); + /// + /// Upsampling layer for 2D inputs.
+ /// Repeats the rows and columns of the data by size[0] and size[1] respectively. + ///
+ /// + /// + /// + /// + public ILayer UpSampling2D(Shape size, string data_format, string interpolation) + => new UpSampling2D(new UpSampling2DArgs + { + Size = size, + DataFormat = data_format, + Interpolation = interpolation + }); - /// - /// Permutes the dimensions of the input according to a given pattern. - /// - public ILayer Permute ( int[] dims ) + /// + /// Permutes the dimensions of the input according to a given pattern. + /// + public ILayer Permute ( int[] dims ) => new Permute(new PermuteArgs { dims = dims }); diff --git a/src/TensorFlowNET.Keras/Layers/LayersApi.cs b/src/TensorFlowNET.Keras/Layers/LayersApi.cs index 3b095bc2a..a1e4c11b1 100644 --- a/src/TensorFlowNET.Keras/Layers/LayersApi.cs +++ b/src/TensorFlowNET.Keras/Layers/LayersApi.cs @@ -2,9 +2,8 @@ using Tensorflow.Framework.Models; using Tensorflow.Keras.ArgsDefinition; using Tensorflow.Keras.ArgsDefinition.Core; -using Tensorflow.Keras.ArgsDefinition.Rnn; using Tensorflow.Keras.Engine; -using Tensorflow.Keras.Layers.Rnn; +using Tensorflow.Keras.Layers; using Tensorflow.NumPy; using static Tensorflow.Binding; using static Tensorflow.KerasApi; @@ -113,7 +112,28 @@ public ILayer Conv1D(int filters, KernelInitializer = GetInitializerByName(kernel_initializer), BiasInitializer = GetInitializerByName(bias_initializer) }); - + public ILayer Conv2D(int filters, + Shape kernel_size = null, + Shape strides = null, + string padding = "valid") + => new Conv2D(new Conv2DArgs + { + Rank = 2, + Filters = filters, + KernelSize = (kernel_size == null) ? (5, 5) : kernel_size, + Strides = strides == null ? (1, 1) : strides, + Padding = padding, + DataFormat = null, + DilationRate = (1, 1), + Groups = 1, + UseBias = false, + KernelRegularizer = null, + KernelInitializer =tf.glorot_uniform_initializer, + BiasInitializer = tf.zeros_initializer, + BiasRegularizer = null, + ActivityRegularizer = null, + Activation = keras.activations.Linear, + }); /// /// 2D convolution layer (e.g. spatial convolution over images). /// This layer creates a convolution kernel that is convolved with the layer input to produce a tensor of outputs. @@ -183,9 +203,6 @@ public ILayer Conv2D(int filters, /// Boolean, whether the layer uses a bias vector. /// The name of the initializer for the kernel weights matrix (see keras.initializers). /// The name of the initializer for the bias vector (see keras.initializers). - /// The name of the regularizer function applied to the kernel weights matrix (see keras.regularizers). - /// The name of the regularizer function applied to the bias vector (see keras.regularizers). - /// The name of the regularizer function applied to the output of the layer (its "activation") (see keras.regularizers). /// A tensor of rank 4+ representing activation(conv2d(inputs, kernel) + bias). public ILayer Conv2D(int filters, Shape kernel_size = null, @@ -214,6 +231,38 @@ public ILayer Conv2D(int filters, Activation = keras.activations.GetActivationFromName(activation) }); + public ILayer DepthwiseConv2D(Shape kernel_size = null, + Shape strides = null, + string padding = "valid", + string data_format = null, + Shape dilation_rate = null, + int groups = 1, + int depth_multiplier = 1, + string activation = null, + bool use_bias = false, + string kernel_initializer = "glorot_uniform", + string bias_initializer = "zeros", + string depthwise_initializer = "glorot_uniform" + ) + => new DepthwiseConv2D(new DepthwiseConv2DArgs + { + Rank = 2, + Filters = 1, + KernelSize = (kernel_size == null) ? (5, 5) : kernel_size, + Strides = strides == null ? (1) : strides, + Padding = padding, + DepthMultiplier = depth_multiplier, + DataFormat = data_format, + DilationRate = dilation_rate == null ? (1) : dilation_rate, + Groups = groups, + UseBias = use_bias, + KernelInitializer = GetInitializerByName(kernel_initializer), + DepthwiseInitializer = GetInitializerByName(depthwise_initializer == null ? kernel_initializer : depthwise_initializer), + BiasInitializer = GetInitializerByName(bias_initializer), + Activation = keras.activations.GetActivationFromName(activation), + }); + + /// /// Transposed convolution layer (sometimes called Deconvolution). /// @@ -238,13 +287,13 @@ public ILayer Conv2DTranspose(int filters, string data_format = null, Shape dilation_rate = null, string activation = null, - bool use_bias = true, + bool use_bias = false, string kernel_initializer = null, string bias_initializer = null, string kernel_regularizer = null, string bias_regularizer = null, string activity_regularizer = null) - => new Conv2DTranspose(new Conv2DArgs + => new Conv2DTranspose(new Conv2DTransposeArgs { Rank = 2, Filters = filters, @@ -469,7 +518,7 @@ public ILayer Flatten(string data_format = null) /// In this case, values of 'None' in the 'shape' argument represent ragged dimensions. For more information about RaggedTensors, see this guide. /// /// A tensor. - public Tensors Input(Shape shape = null, + public KerasTensor Input(Shape shape = null, int batch_size = -1, string name = null, TF_DataType dtype = TF_DataType.DtInvalid, @@ -572,7 +621,7 @@ public ILayer MaxPooling1D(int? pool_size = null, int? strides = null, string padding = "valid", string data_format = null) - => new MaxPooling1D(new Pooling1DArgs + => new MaxPooling1D(new MaxPooling1DArgs { PoolSize = pool_size ?? 2, Strides = strides ?? (pool_size ?? 2), @@ -685,6 +734,41 @@ public ILayer LeakyReLU(float alpha = 0.3f) Alpha = alpha }); + + /// + /// Leaky version of a Rectified Linear Unit. + /// + /// Negative slope coefficient. + /// + public ILayer ReLU6() + => new ReLu6(); + + + public IRnnCell SimpleRNNCell( + int units, + string activation = "tanh", + bool use_bias = true, + string kernel_initializer = "glorot_uniform", + string recurrent_initializer = "orthogonal", + string bias_initializer = "zeros", + float dropout = 0f, + float recurrent_dropout = 0f) + => new SimpleRNNCell(new SimpleRNNCellArgs + { + Units = units, + Activation = keras.activations.GetActivationFromName(activation), + UseBias = use_bias, + KernelInitializer = GetInitializerByName(kernel_initializer), + RecurrentInitializer = GetInitializerByName(recurrent_initializer), + BiasInitializer = GetInitializerByName(bias_initializer), + Dropout = dropout, + RecurrentDropout = recurrent_dropout + }); + + public IRnnCell StackedRNNCells( + IEnumerable cells) + => new StackedRNNCells(cells.ToList(), new StackedRNNCellsArgs()); + /// /// /// @@ -709,6 +793,80 @@ public ILayer SimpleRNN(int units, ReturnState = return_state }); + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + public ILayer RNN( + IRnnCell cell, + bool return_sequences = false, + bool return_state = false, + bool go_backwards = false, + bool stateful = false, + bool unroll = false, + bool time_major = false) + => new RNN(cell, new RNNArgs + { + ReturnSequences = return_sequences, + ReturnState = return_state, + GoBackwards = go_backwards, + Stateful = stateful, + Unroll = unroll, + TimeMajor = time_major + }); + + public ILayer RNN( + IEnumerable cell, + bool return_sequences = false, + bool return_state = false, + bool go_backwards = false, + bool stateful = false, + bool unroll = false, + bool time_major = false) + => new RNN(cell, new RNNArgs + { + ReturnSequences = return_sequences, + ReturnState = return_state, + GoBackwards = go_backwards, + Stateful = stateful, + Unroll = unroll, + TimeMajor = time_major + }); + + + public IRnnCell LSTMCell(int uints, + string activation = "tanh", + string recurrent_activation = "sigmoid", + bool use_bias = true, + string kernel_initializer = "glorot_uniform", + string recurrent_initializer = "orthogonal", + string bias_initializer = "zeros", + bool unit_forget_bias = true, + float dropout = 0f, + float recurrent_dropout = 0f, + int implementation = 2) + => new LSTMCell(new LSTMCellArgs + { + Units = uints, + Activation = keras.activations.GetActivationFromName(activation), + RecurrentActivation = keras.activations.GetActivationFromName(recurrent_activation), + UseBias = use_bias, + KernelInitializer = GetInitializerByName(kernel_initializer), + RecurrentInitializer = GetInitializerByName(recurrent_initializer), + BiasInitializer = GetInitializerByName(bias_initializer), + UnitForgetBias = unit_forget_bias, + Dropout = dropout, + RecurrentDropout = recurrent_dropout, + Implementation = implementation + }); + /// /// Long Short-Term Memory layer - Hochreiter 1997. /// @@ -769,9 +927,122 @@ public ILayer LSTM(int units, GoBackwards = go_backwards, Stateful = stateful, TimeMajor = time_major, - Unroll = unroll + Unroll = unroll, + UnitForgetBias = unit_forget_bias }); + /// + /// Cell class for the GRU layer. + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + public IRnnCell GRUCell( + int units, + string activation = "tanh", + string recurrent_activation = "sigmoid", + bool use_bias = true, + string kernel_initializer = "glorot_uniform", + string recurrent_initializer = "orthogonal", + string bias_initializer = "zeros", + float dropout = 0f, + float recurrent_dropout = 0f, + bool reset_after = true) + => new GRUCell(new GRUCellArgs + { + Units = units, + Activation = keras.activations.GetActivationFromName(activation), + RecurrentActivation = keras.activations.GetActivationFromName(recurrent_activation), + KernelInitializer = GetInitializerByName(kernel_initializer), + RecurrentInitializer = GetInitializerByName(recurrent_initializer), + BiasInitializer = GetInitializerByName(bias_initializer), + UseBias = use_bias, + Dropout = dropout, + RecurrentDropout = recurrent_dropout, + ResetAfter = reset_after + }); + + /// + /// Gated Recurrent Unit - Cho et al. 2014. + /// + /// Positive integer, dimensionality of the output space. + /// Activation function to use. If you pass `None`, no activation is applied.(ie. "linear" activation: `a(x) = x`). + /// Activation function to use for the recurrent step. If you pass `None`, no activation is applied. (ie. "linear" activation: `a(x) = x`). + /// Boolean, (default `True`), whether the layer uses a bias vector. + /// Initializer for the `kernel` weights matrix, used for the linear transformation of the inputs. Default: `glorot_uniform`. + /// Initializer for the `recurrent_kernel` weights matrix, used for the linear transformation of the recurrent state. Default: `orthogonal`. + /// Initializer for the bias vector. Default: `zeros`. + /// Float between 0 and 1. Fraction of the units to drop for the linear transformation of the inputs. Default: 0. + /// Float between 0 and 1. Fraction of the units to drop for the linear transformation of the recurrent state. Default: 0. + /// + /// Boolean. Whether to return the last output in the output sequence, or the full sequence. Default: `False`. + /// Boolean. Whether to return the last state in addition to the output. Default: `False`. + /// Boolean (default `False`). If True, process the input sequence backwards and return the reversed sequence. + /// Boolean (default False). If True, the last state for each sample at index i in a batch will be used as initial state for the sample of index i in the following batch. + /// Boolean (default False). If True, the network will be unrolled, else a symbolic loop will be used. Unrolling can speed-up a RNN, + /// The shape format of the `inputs` and `outputs` tensors. + /// GRU convention (whether to apply reset gate after or before matrix multiplication). False = "before", True = "after" (default and cuDNN compatible). + /// + public ILayer GRU( + int units, + string activation = "tanh", + string recurrent_activation = "sigmoid", + bool use_bias = true, + string kernel_initializer = "glorot_uniform", + string recurrent_initializer = "orthogonal", + string bias_initializer = "zeros", + float dropout = 0f, + float recurrent_dropout = 0f, + bool return_sequences = false, + bool return_state = false, + bool go_backwards = false, + bool stateful = false, + bool unroll = false, + bool time_major = false, + bool reset_after = true + ) + => new GRU(new GRUArgs + { + Units = units, + Activation = keras.activations.GetActivationFromName(activation), + RecurrentActivation = keras.activations.GetActivationFromName(recurrent_activation), + KernelInitializer = GetInitializerByName(kernel_initializer), + RecurrentInitializer = GetInitializerByName(recurrent_initializer), + BiasInitializer = GetInitializerByName(bias_initializer), + UseBias = use_bias, + Dropout = dropout, + RecurrentDropout = recurrent_dropout, + ReturnSequences = return_sequences, + ReturnState = return_state, + GoBackwards = go_backwards, + Stateful = stateful, + TimeMajor = time_major, + Unroll = unroll, + ResetAfter = reset_after + }); + + public ILayer Bidirectional( + ILayer layer, + string merge_mode = "concat", + NDArray weights = null, + ILayer backward_layer = null) + => new Bidirectional(new BidirectionalArgs + { + Layer = layer, + MergeMode = merge_mode, + Weights = weights, + BackwardLayer = backward_layer + }); + + /// /// /// @@ -794,21 +1065,21 @@ public ILayer Rescaling(float scale, /// /// public ILayer Add() - => new Add(new MergeArgs { }); + => new Add(new AddArgs { }); /// /// /// /// public ILayer Subtract() - => new Subtract(new MergeArgs { }); + => new Subtract(new SubtractArgs { }); /// /// Global max pooling operation for spatial data. /// /// public ILayer GlobalAveragePooling2D() - => new GlobalAveragePooling2D(new Pooling2DArgs { }); + => new GlobalAveragePooling2D(new GlobalAveragePooling2DArgs { }); /// /// Global average pooling operation for temporal data. @@ -818,7 +1089,7 @@ public ILayer GlobalAveragePooling2D() /// /// public ILayer GlobalAveragePooling1D(string data_format = "channels_last") - => new GlobalAveragePooling1D(new Pooling1DArgs { DataFormat = data_format }); + => new GlobalAveragePooling1D(new GlobalAveragePooling1DArgs { DataFormat = data_format }); /// /// Global max pooling operation for spatial data. @@ -827,7 +1098,7 @@ public ILayer GlobalAveragePooling1D(string data_format = "channels_last") /// channels_last corresponds to inputs with shape (batch, height, width, channels) while channels_first corresponds to inputs with shape (batch, channels, height, width). /// public ILayer GlobalAveragePooling2D(string data_format = "channels_last") - => new GlobalAveragePooling2D(new Pooling2DArgs { DataFormat = data_format }); + => new GlobalAveragePooling2D(new GlobalAveragePooling2DArgs { DataFormat = data_format }); /// /// Global max pooling operation for 1D temporal data. @@ -838,7 +1109,7 @@ public ILayer GlobalAveragePooling2D(string data_format = "channels_last") /// /// public ILayer GlobalMaxPooling1D(string data_format = "channels_last") - => new GlobalMaxPooling1D(new Pooling1DArgs { DataFormat = data_format }); + => new GlobalMaxPooling1D(new GlobalMaxPooling1DArgs { DataFormat = data_format }); /// /// Global max pooling operation for spatial data. @@ -847,7 +1118,7 @@ public ILayer GlobalMaxPooling1D(string data_format = "channels_last") /// channels_last corresponds to inputs with shape (batch, height, width, channels) while channels_first corresponds to inputs with shape (batch, channels, height, width). /// public ILayer GlobalMaxPooling2D(string data_format = "channels_last") - => new GlobalMaxPooling2D(new Pooling2DArgs { DataFormat = data_format }); + => new GlobalMaxPooling2D(new GlobalMaxPooling2DArgs { DataFormat = data_format }); /// /// Get an weights initializer from its name. @@ -882,5 +1153,9 @@ public ILayer Normalization(Shape? input_shape = null, int? axis = -1, float? me Variance = variance, Invert = invert }); + + + + } } diff --git a/src/TensorFlowNET.Keras/Layers/Merging/Concatenate.cs b/src/TensorFlowNET.Keras/Layers/Merging/Concatenate.cs index a2a8286ba..fa82426ce 100644 --- a/src/TensorFlowNET.Keras/Layers/Merging/Concatenate.cs +++ b/src/TensorFlowNET.Keras/Layers/Merging/Concatenate.cs @@ -39,6 +39,7 @@ public override void build(KerasShapesWrapper input_shape) shape_set.Add(shape); }*/ _buildInputShape = input_shape; + built = true; } protected override Tensors _merge_function(Tensors inputs) diff --git a/src/TensorFlowNET.Keras/Layers/Merging/Merge.cs b/src/TensorFlowNET.Keras/Layers/Merging/Merge.cs index 7df654eeb..bcbb20d88 100644 --- a/src/TensorFlowNET.Keras/Layers/Merging/Merge.cs +++ b/src/TensorFlowNET.Keras/Layers/Merging/Merge.cs @@ -5,6 +5,7 @@ using Tensorflow.Keras.ArgsDefinition; using Tensorflow.Keras.Engine; using Tensorflow.Keras.Saving; +using Tensorflow.Common.Types; namespace Tensorflow.Keras.Layers { @@ -21,7 +22,7 @@ public override void build(KerasShapesWrapper input_shape) _buildInputShape = input_shape; } - protected override Tensors Call(Tensors inputs, Tensor state = null, bool? training = null) + protected override Tensors Call(Tensors inputs, Tensors state = null, bool? training = null, IOptionalArgs? optional_args = null) { return _merge_function(inputs); } diff --git a/src/TensorFlowNET.Keras/Layers/Normalization/BatchNormalization.cs b/src/TensorFlowNET.Keras/Layers/Normalization/BatchNormalization.cs index d02d2509c..655581576 100644 --- a/src/TensorFlowNET.Keras/Layers/Normalization/BatchNormalization.cs +++ b/src/TensorFlowNET.Keras/Layers/Normalization/BatchNormalization.cs @@ -17,6 +17,7 @@ limitations under the License. using System; using System.Collections.Generic; using System.Linq; +using Tensorflow.Common.Types; using Tensorflow.Keras.ArgsDefinition; using Tensorflow.Keras.Engine; using Tensorflow.Keras.Saving; @@ -146,7 +147,7 @@ bool _support_zero_size_input() return false; } - protected override Tensors Call(Tensors inputs, Tensor state = null, bool? training = null) + protected override Tensors Call(Tensors inputs, Tensors state = null, bool? training = null, IOptionalArgs? optional_args = null) { Tensor outputs = null; var training_tensor = training == null diff --git a/src/TensorFlowNET.Keras/Layers/Normalization/LayerNormalization.cs b/src/TensorFlowNET.Keras/Layers/Normalization/LayerNormalization.cs index e90c04029..69bdfbaa0 100644 --- a/src/TensorFlowNET.Keras/Layers/Normalization/LayerNormalization.cs +++ b/src/TensorFlowNET.Keras/Layers/Normalization/LayerNormalization.cs @@ -17,6 +17,7 @@ limitations under the License. using System; using System.Collections.Generic; using System.Linq; +using Tensorflow.Common.Types; using Tensorflow.Keras.ArgsDefinition; using Tensorflow.Keras.Engine; using Tensorflow.Keras.Saving; @@ -101,7 +102,7 @@ public override Shape ComputeOutputShape(Shape input_shape) return input_shape; } - protected override Tensors Call(Tensors inputs, Tensor state = null, bool? training = null) + protected override Tensors Call(Tensors inputs, Tensors state = null, bool? training = null, IOptionalArgs? optional_args = null) { Tensor outputs = null; var inputs_dtype = inputs.dtype.as_base_dtype(); @@ -152,9 +153,22 @@ protected override Tensors Call(Tensors inputs, Tensor state = null, bool? train } else { + var input_dtype = inputs.dtype; + if ((input_dtype == tf.float16) && DType == tf.float32) inputs = tf.cast(inputs, tf.float32); + (Tensor mean, Tensor variance) = tf.nn.moments(inputs, axis, keep_dims: true); - } + (Tensor scale, Tensor offset) = (_broadcast(gamma), _broadcast(beta)); + + outputs = tf.nn.batch_normalization( + inputs, + mean, + variance, + offset: offset, + scale: scale, + variance_epsilon: epsilon); + outputs = tf.cast(outputs, input_dtype); + } // If some components of the shape got lost due to adjustments, fix that. outputs.shape = input_shape; diff --git a/src/TensorFlowNET.Keras/Layers/Normalization/Normalization.cs b/src/TensorFlowNET.Keras/Layers/Normalization/Normalization.cs index a65154bf4..987b56bc4 100644 --- a/src/TensorFlowNET.Keras/Layers/Normalization/Normalization.cs +++ b/src/TensorFlowNET.Keras/Layers/Normalization/Normalization.cs @@ -14,6 +14,7 @@ You may obtain a copy of the License at limitations under the License. ******************************************************************************/ +using Tensorflow.Common.Types; using Tensorflow.Keras.ArgsDefinition; using Tensorflow.Keras.Saving; @@ -157,7 +158,7 @@ public override void adapt(Tensor data, int? batch_size = null, int? steps = nul base.adapt(data, batch_size: batch_size, steps: steps); } - protected override Tensors Call(Tensors inputs, Tensor state = null, bool? training = null) + protected override Tensors Call(Tensors inputs, Tensors state = null, bool? training = null, IOptionalArgs? optional_args = null) { if (_args.Invert) { diff --git a/src/TensorFlowNET.Keras/Layers/Pooling/GlobalAveragePooling1D.cs b/src/TensorFlowNET.Keras/Layers/Pooling/GlobalAveragePooling1D.cs index d62fb63a4..ffaabec97 100644 --- a/src/TensorFlowNET.Keras/Layers/Pooling/GlobalAveragePooling1D.cs +++ b/src/TensorFlowNET.Keras/Layers/Pooling/GlobalAveragePooling1D.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Text; using Tensorflow.Keras.ArgsDefinition; +using Tensorflow.Common.Types; namespace Tensorflow.Keras.Layers { @@ -12,7 +13,7 @@ public GlobalAveragePooling1D(Pooling1DArgs args) { } - protected override Tensors Call(Tensors inputs, Tensor state = null, bool? training = null) + protected override Tensors Call(Tensors inputs, Tensors state = null, bool? training = null, IOptionalArgs? optional_args = null) { if (data_format == "channels_last") return math_ops.reduce_mean(inputs, 1, false); diff --git a/src/TensorFlowNET.Keras/Layers/Pooling/GlobalAveragePooling2D.cs b/src/TensorFlowNET.Keras/Layers/Pooling/GlobalAveragePooling2D.cs index 000e4b8b9..e06665173 100644 --- a/src/TensorFlowNET.Keras/Layers/Pooling/GlobalAveragePooling2D.cs +++ b/src/TensorFlowNET.Keras/Layers/Pooling/GlobalAveragePooling2D.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Text; using Tensorflow.Keras.ArgsDefinition; +using Tensorflow.Common.Types; namespace Tensorflow.Keras.Layers { @@ -12,7 +13,7 @@ public GlobalAveragePooling2D(Pooling2DArgs args) { } - protected override Tensors Call(Tensors inputs, Tensor state = null, bool? training = null) + protected override Tensors Call(Tensors inputs, Tensors state = null, bool? training = null, IOptionalArgs? optional_args = null) { if (data_format == "channels_last") return math_ops.reduce_mean(inputs, (1, 2), false); diff --git a/src/TensorFlowNET.Keras/Layers/Pooling/GlobalMaxPooling1D.cs b/src/TensorFlowNET.Keras/Layers/Pooling/GlobalMaxPooling1D.cs index 2de4671ca..15695e8a7 100644 --- a/src/TensorFlowNET.Keras/Layers/Pooling/GlobalMaxPooling1D.cs +++ b/src/TensorFlowNET.Keras/Layers/Pooling/GlobalMaxPooling1D.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Text; using Tensorflow.Keras.ArgsDefinition; +using Tensorflow.Common.Types; namespace Tensorflow.Keras.Layers { @@ -12,7 +13,7 @@ public GlobalMaxPooling1D(Pooling1DArgs args) { } - protected override Tensors Call(Tensors inputs, Tensor state = null, bool? training = null) + protected override Tensors Call(Tensors inputs, Tensors state = null, bool? training = null, IOptionalArgs? optional_args = null) { if (data_format == "channels_last") return math_ops.reduce_max(inputs, 1, false); diff --git a/src/TensorFlowNET.Keras/Layers/Pooling/GlobalMaxPooling2D.cs b/src/TensorFlowNET.Keras/Layers/Pooling/GlobalMaxPooling2D.cs index b7e2c9452..76db858da 100644 --- a/src/TensorFlowNET.Keras/Layers/Pooling/GlobalMaxPooling2D.cs +++ b/src/TensorFlowNET.Keras/Layers/Pooling/GlobalMaxPooling2D.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Text; using Tensorflow.Keras.ArgsDefinition; +using Tensorflow.Common.Types; namespace Tensorflow.Keras.Layers { @@ -12,7 +13,7 @@ public GlobalMaxPooling2D(Pooling2DArgs args) { } - protected override Tensors Call(Tensors inputs, Tensor state = null, bool? training = null) + protected override Tensors Call(Tensors inputs, Tensors state = null, bool? training = null, IOptionalArgs? optional_args = null) { if (data_format == "channels_last") return math_ops.reduce_max(inputs, (1, 2), false); diff --git a/src/TensorFlowNET.Keras/Layers/Pooling/Pooling1D.cs b/src/TensorFlowNET.Keras/Layers/Pooling/Pooling1D.cs index a2f4c51b6..81a340199 100644 --- a/src/TensorFlowNET.Keras/Layers/Pooling/Pooling1D.cs +++ b/src/TensorFlowNET.Keras/Layers/Pooling/Pooling1D.cs @@ -18,6 +18,7 @@ limitations under the License. using Tensorflow.Keras.ArgsDefinition; using Tensorflow.Keras.Engine; using Tensorflow.Keras.Utils; +using Tensorflow.Common.Types; using static Tensorflow.Binding; namespace Tensorflow.Keras.Layers @@ -36,7 +37,7 @@ public Pooling1D(Pooling1DArgs args) input_spec = new InputSpec(ndim: 3); } - protected override Tensors Call(Tensors inputs, Tensor state = null, bool? training = null) + protected override Tensors Call(Tensors inputs, Tensors state = null, bool? training = null, IOptionalArgs? optional_args = null) { int pad_axis = args.DataFormat == "channels_first" ? 2 : 3; inputs = tf.expand_dims(inputs, pad_axis); diff --git a/src/TensorFlowNET.Keras/Layers/Pooling/Pooling2D.cs b/src/TensorFlowNET.Keras/Layers/Pooling/Pooling2D.cs index 270322559..f83f1e152 100644 --- a/src/TensorFlowNET.Keras/Layers/Pooling/Pooling2D.cs +++ b/src/TensorFlowNET.Keras/Layers/Pooling/Pooling2D.cs @@ -17,6 +17,7 @@ limitations under the License. using Tensorflow.Keras.ArgsDefinition; using Tensorflow.Keras.Engine; using Tensorflow.Keras.Utils; +using Tensorflow.Common.Types; namespace Tensorflow.Keras.Layers { @@ -36,7 +37,7 @@ public Pooling2D(Pooling2DArgs args) input_spec = new InputSpec(ndim: 4); } - protected override Tensors Call(Tensors inputs, Tensor state = null, bool? training = null) + protected override Tensors Call(Tensors inputs, Tensors state = null, bool? training = null, IOptionalArgs? optional_args = null) { int[] pool_shape; int[] strides; diff --git a/src/TensorFlowNET.Keras/Layers/Preprocessing/CategoryEncoding.cs b/src/TensorFlowNET.Keras/Layers/Preprocessing/CategoryEncoding.cs index 5620a916c..20d2a53d5 100644 --- a/src/TensorFlowNET.Keras/Layers/Preprocessing/CategoryEncoding.cs +++ b/src/TensorFlowNET.Keras/Layers/Preprocessing/CategoryEncoding.cs @@ -1,6 +1,6 @@ using Tensorflow.Keras.ArgsDefinition; using Tensorflow.Keras.Engine; - +using Tensorflow.Common.Types; namespace Tensorflow.Keras.Layers { /// @@ -15,7 +15,7 @@ public CategoryEncoding(CategoryEncodingArgs args) : base(args) this.args = args; } - protected override Tensors Call(Tensors inputs, Tensor state = null, bool? training = null) + protected override Tensors Call(Tensors inputs, Tensors state = null, bool? training = null, IOptionalArgs? optional_args = null) { var depth = args.NumTokens; var max_value = tf.reduce_max(inputs); diff --git a/src/TensorFlowNET.Keras/Layers/Preprocessing/Rescaling.cs b/src/TensorFlowNET.Keras/Layers/Preprocessing/Rescaling.cs index 5fc581af9..7fa367eea 100644 --- a/src/TensorFlowNET.Keras/Layers/Preprocessing/Rescaling.cs +++ b/src/TensorFlowNET.Keras/Layers/Preprocessing/Rescaling.cs @@ -1,5 +1,6 @@ using Tensorflow.Keras.ArgsDefinition; using Tensorflow.Keras.Engine; +using Tensorflow.Common.Types; namespace Tensorflow.Keras.Layers { @@ -17,7 +18,7 @@ public Rescaling(RescalingArgs args) : base(args) this.args = args; } - protected override Tensors Call(Tensors inputs, Tensor state = null, bool? training = null) + protected override Tensors Call(Tensors inputs, Tensors state = null, bool? training = null, IOptionalArgs? optional_args = null) { scale = constant_op.constant(args.Scale, args.DType); offset = constant_op.constant(args.Offset, args.DType); diff --git a/src/TensorFlowNET.Keras/Layers/Preprocessing/Resizing.cs b/src/TensorFlowNET.Keras/Layers/Preprocessing/Resizing.cs index 603e2b071..081966ad4 100644 --- a/src/TensorFlowNET.Keras/Layers/Preprocessing/Resizing.cs +++ b/src/TensorFlowNET.Keras/Layers/Preprocessing/Resizing.cs @@ -4,6 +4,7 @@ using System.Text; using Tensorflow.Keras.ArgsDefinition; using Tensorflow.Keras.Saving; +using Tensorflow.Common.Types; namespace Tensorflow.Keras.Layers { @@ -19,7 +20,7 @@ public Resizing(ResizingArgs args) : base(args) this.args = args; } - protected override Tensors Call(Tensors inputs, Tensor state = null, bool? training = null) + protected override Tensors Call(Tensors inputs, Tensors state = null, bool? training = null, IOptionalArgs? optional_args = null) { return image_ops_impl.resize_images_v2(inputs, new[] { args.Height, args.Width }, method: args.Interpolation); } diff --git a/src/TensorFlowNET.Keras/Layers/Regularization/Dropout.cs b/src/TensorFlowNET.Keras/Layers/Regularization/Dropout.cs index aa3a92a49..ada1851ce 100644 --- a/src/TensorFlowNET.Keras/Layers/Regularization/Dropout.cs +++ b/src/TensorFlowNET.Keras/Layers/Regularization/Dropout.cs @@ -1,4 +1,5 @@ -using Tensorflow.Keras.ArgsDefinition; +using Tensorflow.Common.Types; +using Tensorflow.Keras.ArgsDefinition; using Tensorflow.Keras.Engine; using Tensorflow.Keras.Utils; using static Tensorflow.Binding; @@ -15,7 +16,7 @@ public Dropout(DropoutArgs args) this.args = args; } - protected override Tensors Call(Tensors inputs, Tensor state = null, bool? training = null) + protected override Tensors Call(Tensors inputs, Tensors state = null, bool? training = null, IOptionalArgs? optional_args = null) { if (training == null) training = false; diff --git a/src/TensorFlowNET.Keras/Layers/Reshaping/Cropping1D.cs b/src/TensorFlowNET.Keras/Layers/Reshaping/Cropping1D.cs index 9ead15cb5..7d5385e6f 100644 --- a/src/TensorFlowNET.Keras/Layers/Reshaping/Cropping1D.cs +++ b/src/TensorFlowNET.Keras/Layers/Reshaping/Cropping1D.cs @@ -1,6 +1,7 @@ using Tensorflow.Keras.ArgsDefinition.Reshaping; using Tensorflow.Keras.Engine; using Tensorflow.Keras.Saving; +using Tensorflow.Common.Types; namespace Tensorflow.Keras.Layers.Reshaping { @@ -27,7 +28,7 @@ public override void build(KerasShapesWrapper input_shape) _buildInputShape = input_shape; } - protected override Tensors Call(Tensors inputs, Tensor state = null, bool? training = null) + protected override Tensors Call(Tensors inputs, Tensors state = null, bool? training = null, IOptionalArgs? optional_args = null) { Tensor output = inputs; if (output.rank != 3) diff --git a/src/TensorFlowNET.Keras/Layers/Reshaping/Cropping2D.cs b/src/TensorFlowNET.Keras/Layers/Reshaping/Cropping2D.cs index 087d59a14..4a5c6eabc 100644 --- a/src/TensorFlowNET.Keras/Layers/Reshaping/Cropping2D.cs +++ b/src/TensorFlowNET.Keras/Layers/Reshaping/Cropping2D.cs @@ -1,6 +1,7 @@ using Tensorflow.Keras.ArgsDefinition.Reshaping; using Tensorflow.Keras.Engine; using Tensorflow.Keras.Saving; +using Tensorflow.Common.Types; namespace Tensorflow.Keras.Layers.Reshaping { @@ -21,7 +22,7 @@ public override void build(KerasShapesWrapper input_shape) built = true; _buildInputShape = input_shape; } - protected override Tensors Call(Tensors inputs, Tensor state = null, bool? training = null) + protected override Tensors Call(Tensors inputs, Tensors state = null, bool? training = null, IOptionalArgs? optional_args = null) { Tensor output = inputs; if (output.rank != 4) diff --git a/src/TensorFlowNET.Keras/Layers/Reshaping/Cropping3D.cs b/src/TensorFlowNET.Keras/Layers/Reshaping/Cropping3D.cs index 04a1af600..83f86c6fc 100644 --- a/src/TensorFlowNET.Keras/Layers/Reshaping/Cropping3D.cs +++ b/src/TensorFlowNET.Keras/Layers/Reshaping/Cropping3D.cs @@ -1,6 +1,7 @@ using Tensorflow.Keras.ArgsDefinition.Reshaping; using Tensorflow.Keras.Engine; using Tensorflow.Keras.Saving; +using Tensorflow.Common.Types; namespace Tensorflow.Keras.Layers.Reshaping { @@ -21,7 +22,7 @@ public override void build(KerasShapesWrapper input_shape) _buildInputShape = input_shape; } - protected override Tensors Call(Tensors inputs, Tensor state = null, bool? training = null) + protected override Tensors Call(Tensors inputs, Tensors state = null, bool? training = null, IOptionalArgs? optional_args = null) { Tensor output = inputs; if (output.rank != 5) diff --git a/src/TensorFlowNET.Keras/Layers/Reshaping/Flatten.cs b/src/TensorFlowNET.Keras/Layers/Reshaping/Flatten.cs index 539b5f624..a6192849d 100644 --- a/src/TensorFlowNET.Keras/Layers/Reshaping/Flatten.cs +++ b/src/TensorFlowNET.Keras/Layers/Reshaping/Flatten.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using Tensorflow.Common.Types; using Tensorflow.Framework; using Tensorflow.Keras.ArgsDefinition; using Tensorflow.Keras.Engine; @@ -23,7 +24,7 @@ public Flatten(FlattenArgs args) _channels_first = args.DataFormat == "channels_first"; } - protected override Tensors Call(Tensors inputs, Tensor state = null, bool? training = null) + protected override Tensors Call(Tensors inputs, Tensors state = null, bool? training = null, IOptionalArgs? optional_args = null) { if (_channels_first) { diff --git a/src/TensorFlowNET.Keras/Layers/Reshaping/Permute.cs b/src/TensorFlowNET.Keras/Layers/Reshaping/Permute.cs index e391775c8..7fdb816bf 100644 --- a/src/TensorFlowNET.Keras/Layers/Reshaping/Permute.cs +++ b/src/TensorFlowNET.Keras/Layers/Reshaping/Permute.cs @@ -6,6 +6,7 @@ using static Tensorflow.Binding; using Tensorflow.Keras.ArgsDefinition; using Tensorflow.Keras.Saving; +using Tensorflow.Common.Types; namespace Tensorflow.Keras.Layers { public class Permute : Layer @@ -28,7 +29,7 @@ public override void build(KerasShapesWrapper input_shape) built = true; _buildInputShape = input_shape; } - protected override Tensors Call(Tensors inputs, Tensor state = null, bool? training = null) + protected override Tensors Call(Tensors inputs, Tensors state = null, bool? training = null, IOptionalArgs? optional_args = null) { Tensor outputs = inputs; return tf.transpose(outputs, new Axis(permute)); diff --git a/src/TensorFlowNET.Keras/Layers/Reshaping/Reshape.cs b/src/TensorFlowNET.Keras/Layers/Reshaping/Reshape.cs index 92a772f34..4b3d30e29 100644 --- a/src/TensorFlowNET.Keras/Layers/Reshaping/Reshape.cs +++ b/src/TensorFlowNET.Keras/Layers/Reshaping/Reshape.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System; using System.Linq; +using Tensorflow.Common.Types; namespace Tensorflow.Keras.Layers { @@ -19,7 +20,7 @@ public Reshape(ReshapeArgs args) this.args = args; } - protected override Tensors Call(Tensors inputs, Tensor state = null, bool? training = null) + protected override Tensors Call(Tensors inputs, Tensors state = null, bool? training = null, IOptionalArgs? optional_args = null) { var shapes = new List(); shapes.Add(array_ops.shape(inputs)[0]); diff --git a/src/TensorFlowNET.Keras/Layers/Reshaping/UpSampling1D.cs b/src/TensorFlowNET.Keras/Layers/Reshaping/UpSampling1D.cs new file mode 100644 index 000000000..3bc8d6c6b --- /dev/null +++ b/src/TensorFlowNET.Keras/Layers/Reshaping/UpSampling1D.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Tensorflow.Common.Types; +using Tensorflow.Keras.ArgsDefinition; +using Tensorflow.Keras.Engine; + + +namespace Tensorflow.Keras.Layers +{ + /// + /// Upsampling layer for 1D inputs. + /// + public class UpSampling1D : Layer + { + UpSampling1DArgs args; + int size; + + public UpSampling1D(UpSampling1DArgs args) : base(args) + { + this.args = args; + size = args.Size; + inputSpec = new InputSpec(ndim: 3); + } + + protected override Tensors Call(Tensors inputs, Tensors state = null, bool? training = null, IOptionalArgs? optional_args = null) + { + var output = keras.backend.repeat_elements(inputs, size, axis: 1); + return output; + } + } +} diff --git a/src/TensorFlowNET.Keras/Layers/Reshaping/UpSampling2D.cs b/src/TensorFlowNET.Keras/Layers/Reshaping/UpSampling2D.cs index 8314151f6..cb579d61e 100644 --- a/src/TensorFlowNET.Keras/Layers/Reshaping/UpSampling2D.cs +++ b/src/TensorFlowNET.Keras/Layers/Reshaping/UpSampling2D.cs @@ -6,9 +6,13 @@ using Tensorflow.Keras.Utils; using static Tensorflow.Binding; using static Tensorflow.KerasApi; +using Tensorflow.Common.Types; namespace Tensorflow.Keras.Layers { + /// + /// Upsampling layer for 2D inputs. + /// public class UpSampling2D : Layer { UpSampling2DArgs args; @@ -24,7 +28,7 @@ public UpSampling2D(UpSampling2DArgs args) : base(args) inputSpec = new InputSpec(ndim: 4); } - protected override Tensors Call(Tensors inputs, Tensor state = null, bool? training = null) + protected override Tensors Call(Tensors inputs, Tensors state = null, bool? training = null, IOptionalArgs? optional_args = null) { return keras.backend.resize_images(inputs, size[0], size[1], diff --git a/src/TensorFlowNET.Keras/Layers/Reshaping/ZeroPadding2D.cs b/src/TensorFlowNET.Keras/Layers/Reshaping/ZeroPadding2D.cs index 7c87100a2..3b37dac46 100644 --- a/src/TensorFlowNET.Keras/Layers/Reshaping/ZeroPadding2D.cs +++ b/src/TensorFlowNET.Keras/Layers/Reshaping/ZeroPadding2D.cs @@ -2,6 +2,7 @@ using Tensorflow.Keras.ArgsDefinition; using Tensorflow.Keras.Engine; using Tensorflow.Keras.Utils; +using Tensorflow.Common.Types; using static Tensorflow.KerasApi; namespace Tensorflow.Keras.Layers @@ -26,7 +27,7 @@ public ZeroPadding2D(ZeroPadding2DArgs args, string data_format = null) this.input_spec = new InputSpec(ndim: 4); } - protected override Tensors Call(Tensors inputs, Tensor state = null, bool? training = null) + protected override Tensors Call(Tensors inputs, Tensors state = null, bool? training = null, IOptionalArgs? optional_args = null) { return keras.backend.spatial_2d_padding(inputs, padding: padding, diff --git a/src/TensorFlowNET.Keras/Layers/Rnn/BaseWrapper.cs b/src/TensorFlowNET.Keras/Layers/Rnn/BaseWrapper.cs new file mode 100644 index 000000000..737f88cd4 --- /dev/null +++ b/src/TensorFlowNET.Keras/Layers/Rnn/BaseWrapper.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Text; +using Tensorflow.Keras.ArgsDefinition; +using Tensorflow.Keras.Saving; + +namespace Tensorflow.Keras.Layers +{ + /// + /// Abstract wrapper base class. Wrappers take another layer and augment it in various ways. + /// Do not use this class as a layer, it is only an abstract base class. + /// Two usable wrappers are the `TimeDistributed` and `Bidirectional` wrappers. + /// + public abstract class Wrapper: Layer + { + public ILayer _layer; + public Wrapper(WrapperArgs args):base(args) + { + _layer = args.Layer; + } + + public virtual void Build(KerasShapesWrapper input_shape) + { + if (!_layer.Built) + { + _layer.build(input_shape); + } + built = true; + } + + } +} diff --git a/src/TensorFlowNET.Keras/Layers/Rnn/Bidirectional.cs b/src/TensorFlowNET.Keras/Layers/Rnn/Bidirectional.cs new file mode 100644 index 000000000..0566b08ad --- /dev/null +++ b/src/TensorFlowNET.Keras/Layers/Rnn/Bidirectional.cs @@ -0,0 +1,285 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Tensorflow.Common.Types; +using Tensorflow.Keras.ArgsDefinition; +using Tensorflow.Keras.Saving; + +namespace Tensorflow.Keras.Layers +{ + /// + /// Bidirectional wrapper for RNNs. + /// + public class Bidirectional: Wrapper + { + int _num_constants = 0; + bool _support_masking = true; + bool _return_state; + bool _stateful; + bool _return_sequences; + BidirectionalArgs _args; + RNNArgs _layer_args_copy; + RNN _forward_layer; + RNN _backward_layer; + RNN _layer; + InputSpec _input_spec; + public Bidirectional(BidirectionalArgs args):base(args) + { + _args = args; + if (_args.Layer is not ILayer) + throw new ValueError( + "Please initialize `Bidirectional` layer with a " + + $"`tf.keras.layers.Layer` instance. Received: {_args.Layer}"); + + if (_args.BackwardLayer is not null && _args.BackwardLayer is not ILayer) + throw new ValueError( + "`backward_layer` need to be a `tf.keras.layers.Layer` " + + $"instance. Received: {_args.BackwardLayer}"); + if (!new List { "sum", "mul", "ave", "concat", null }.Contains(_args.MergeMode)) + { + throw new ValueError( + $"Invalid merge mode. Received: {_args.MergeMode}. " + + "Merge mode should be one of " + + "{\"sum\", \"mul\", \"ave\", \"concat\", null}" + ); + } + if (_args.Layer is RNN) + { + _layer = _args.Layer as RNN; + } + else + { + throw new ValueError( + "Bidirectional only support RNN instance such as LSTM or GRU"); + } + _return_state = _layer.Args.ReturnState; + _return_sequences = _layer.Args.ReturnSequences; + _stateful = _layer.Args.Stateful; + _layer_args_copy = _layer.Args.Clone(); + // We don't want to track `layer` since we're already tracking the two + // copies of it we actually run. + // TODO(Wanglongzhi2001), since the feature of setattr_tracking has not been implemented. + // _setattr_tracking = false; + // super().__init__(layer, **kwargs) + // _setattr_tracking = true; + + // Recreate the forward layer from the original layer config, so that it + // will not carry over any state from the layer. + if (_layer is LSTM) + { + var arg = _layer_args_copy as LSTMArgs; + _forward_layer = new LSTM(arg); + } + else if(_layer is SimpleRNN) + { + var arg = _layer_args_copy as SimpleRNNArgs; + _forward_layer = new SimpleRNN(arg); + } + // TODO(Wanglongzhi2001), add GRU if case. + else + { + _forward_layer = new RNN(_layer.Cell, _layer_args_copy); + } + //_forward_layer = _recreate_layer_from_config(_layer); + if (_args.BackwardLayer is null) + { + _backward_layer = _recreate_layer_from_config(_layer, go_backwards:true); + } + else + { + _backward_layer = _args.BackwardLayer as RNN; + } + _forward_layer.Name = "forward_" + _forward_layer.Name; + _backward_layer.Name = "backward_" + _backward_layer.Name; + _verify_layer_config(); + + void force_zero_output_for_mask(RNN layer) + { + layer.Args.ZeroOutputForMask = layer.Args.ReturnSequences; + } + + force_zero_output_for_mask(_forward_layer); + force_zero_output_for_mask(_backward_layer); + + if (_args.Weights is not null) + { + var nw = len(_args.Weights); + _forward_layer.set_weights(_args.Weights[$":,{nw / 2}"]); + _backward_layer.set_weights(_args.Weights[$"{nw / 2},:"]); + } + + _input_spec = _layer.InputSpec; + } + + private void _verify_layer_config() + { + if (_forward_layer.Args.GoBackwards == _backward_layer.Args.GoBackwards) + { + throw new ValueError( + "Forward layer and backward layer should have different " + + "`go_backwards` value." + + "forward_layer.go_backwards = " + + $"{_forward_layer.Args.GoBackwards}," + + "backward_layer.go_backwards = " + + $"{_backward_layer.Args.GoBackwards}"); + } + if (_forward_layer.Args.Stateful != _backward_layer.Args.Stateful) + { + throw new ValueError( + "Forward layer and backward layer are expected to have "+ + $"the same value for attribute stateful, got "+ + $"{_forward_layer.Args.Stateful} for forward layer and "+ + $"{_backward_layer.Args.Stateful} for backward layer"); + } + if (_forward_layer.Args.ReturnState != _backward_layer.Args.ReturnState) + { + throw new ValueError( + "Forward layer and backward layer are expected to have " + + $"the same value for attribute return_state, got " + + $"{_forward_layer.Args.ReturnState} for forward layer and " + + $"{_backward_layer.Args.ReturnState} for backward layer"); + } + if (_forward_layer.Args.ReturnSequences != _backward_layer.Args.ReturnSequences) + { + throw new ValueError( + "Forward layer and backward layer are expected to have " + + $"the same value for attribute return_sequences, got " + + $"{_forward_layer.Args.ReturnSequences} for forward layer and " + + $"{_backward_layer.Args.ReturnSequences} for backward layer"); + } + } + + private RNN _recreate_layer_from_config(RNN layer, bool go_backwards = false) + { + var config = layer.get_config() as RNNArgs; + var cell = layer.Cell; + if (go_backwards) + { + config.GoBackwards = !config.GoBackwards; + } + + if (layer is LSTM) + { + var arg = config as LSTMArgs; + return new LSTM(arg); + } + else if(layer is SimpleRNN) + { + var arg = config as SimpleRNNArgs; + return new SimpleRNN(arg); + } + // TODO(Wanglongzhi2001), add GRU if case. + else + { + return new RNN(cell, config); + } + } + + public override void build(KerasShapesWrapper input_shape) + { + _buildInputShape = input_shape; + tf_with(ops.name_scope(_forward_layer.Name), scope=> + { + _forward_layer.build(input_shape); + }); + tf_with(ops.name_scope(_backward_layer.Name), scope => + { + _backward_layer.build(input_shape); + }); + built = true; + } + + protected override Tensors Call(Tensors inputs, Tensors state = null, bool? training = null, IOptionalArgs? optional_args = null) + { + // `Bidirectional.call` implements the same API as the wrapped `RNN`. + Tensors forward_inputs; + Tensors backward_inputs; + Tensors forward_state; + Tensors backward_state; + // if isinstance(inputs, list) and len(inputs) > 1: + if (inputs.Length > 1) + { + // initial_states are keras tensors, which means they are passed + // in together with inputs as list. The initial_states need to be + // split into forward and backward section, and be feed to layers + // accordingly. + forward_inputs = new Tensors { inputs[0] }; + backward_inputs = new Tensors { inputs[0] }; + var pivot = (len(inputs) - _num_constants) / 2 + 1; + // add forward initial state + forward_inputs.Concat(new Tensors { inputs[$"1:{pivot}"] }); + if (_num_constants != 0) + // add backward initial state + backward_inputs.Concat(new Tensors { inputs[$"{pivot}:"] }); + else + { + // add backward initial state + backward_inputs.Concat(new Tensors { inputs[$"{pivot}:{-_num_constants}"] }); + // add constants for forward and backward layers + forward_inputs.Concat(new Tensors { inputs[$"{-_num_constants}:"] }); + backward_inputs.Concat(new Tensors { inputs[$"{-_num_constants}:"] }); + } + forward_state = null; + backward_state = null; + } + else if (state is not null) + { + // initial_states are not keras tensors, eg eager tensor from np + // array. They are only passed in from kwarg initial_state, and + // should be passed to forward/backward layer via kwarg + // initial_state as well. + forward_inputs = inputs; + backward_inputs = inputs; + var half = len(state) / 2; + forward_state = state[$":{half}"]; + backward_state = state[$"{half}:"]; + } + else + { + forward_inputs = inputs; + backward_inputs = inputs; + forward_state = null; + backward_state = null; + } + var y = _forward_layer.Apply(forward_inputs, forward_state); + var y_rev = _backward_layer.Apply(backward_inputs, backward_state); + + Tensors states = new(); + if (_return_state) + { + states = y["1:"] + y_rev["1:"]; + y = y[0]; + y_rev = y_rev[0]; + } + + if (_return_sequences) + { + int time_dim = _forward_layer.Args.TimeMajor ? 0 : 1; + y_rev = keras.backend.reverse(y_rev, time_dim); + } + Tensors output; + if (_args.MergeMode == "concat") + output = keras.backend.concatenate(new Tensors { y.Single(), y_rev.Single() }); + else if (_args.MergeMode == "sum") + output = y.Single() + y_rev.Single(); + else if (_args.MergeMode == "ave") + output = (y.Single() + y_rev.Single()) / 2; + else if (_args.MergeMode == "mul") + output = y.Single() * y_rev.Single(); + else if (_args.MergeMode is null) + output = new Tensors { y.Single(), y_rev.Single() }; + else + throw new ValueError( + "Unrecognized value for `merge_mode`. " + + $"Received: {_args.MergeMode}" + + "Expected values are [\"concat\", \"sum\", \"ave\", \"mul\"]"); + if (_return_state) + { + if (_args.MergeMode is not null) + return new Tensors { output.Single(), states.Single()}; + } + return output; + } + } +} diff --git a/src/TensorFlowNET.Keras/Layers/Rnn/DropoutRNNCellMixin.cs b/src/TensorFlowNET.Keras/Layers/Rnn/DropoutRNNCellMixin.cs new file mode 100644 index 000000000..27c13f349 --- /dev/null +++ b/src/TensorFlowNET.Keras/Layers/Rnn/DropoutRNNCellMixin.cs @@ -0,0 +1,109 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Tensorflow.Common.Types; +using Tensorflow.Keras.ArgsDefinition; +using Tensorflow.Keras.Engine; +using Tensorflow.Keras.Utils; + +namespace Tensorflow.Keras.Layers +{ + public abstract class DropoutRNNCellMixin: Layer, IRnnCell + { + public float dropout; + public float recurrent_dropout; + // TODO(Rinne): deal with cache. + public DropoutRNNCellMixin(LayerArgs args): base(args) + { + + } + + public abstract INestStructure StateSize { get; } + public abstract INestStructure OutputSize { get; } + public abstract bool SupportOptionalArgs { get; } + public virtual Tensors GetInitialState(Tensors inputs, Tensor batch_size, TF_DataType dtype) + { + return RnnUtils.generate_zero_filled_state_for_cell(this, inputs, batch_size, dtype); + } + + protected void _create_non_trackable_mask_cache() + { + + } + + public void reset_dropout_mask() + { + + } + + public void reset_recurrent_dropout_mask() + { + + } + + public Tensors? get_dropout_mask_for_cell(Tensors input, bool training, int count = 1) + { + if (dropout == 0f) + return null; + return _generate_dropout_mask( + tf.ones_like(input), + dropout, + training, + count); + } + + // Get the recurrent dropout mask for RNN cell. + public Tensors? get_recurrent_dropout_mask_for_cell(Tensors input, bool training, int count = 1) + { + if (dropout == 0f) + return null; + return _generate_dropout_mask( + tf.ones_like(input), + recurrent_dropout, + training, + count); + } + + public Tensors _create_dropout_mask(Tensors input, bool training, int count = 1) + { + return _generate_dropout_mask( + tf.ones_like(input), + dropout, + training, + count); + } + + public Tensors _create_recurrent_dropout_mask(Tensors input, bool training, int count = 1) + { + return _generate_dropout_mask( + tf.ones_like(input), + recurrent_dropout, + training, + count); + } + + public Tensors _generate_dropout_mask(Tensor ones, float rate, bool training, int count = 1) + { + Tensors dropped_inputs() + { + DropoutArgs args = new DropoutArgs(); + args.Rate = rate; + var DropoutLayer = new Dropout(args); + var mask = DropoutLayer.Apply(ones, training: training); + return mask; + } + + if (count > 1) + { + Tensors results = new Tensors(); + for (int i = 0; i < count; i++) + { + results.Add(dropped_inputs()); + } + return results; + } + + return dropped_inputs(); + } + } +} diff --git a/src/TensorFlowNET.Keras/Layers/Rnn/GRU.cs b/src/TensorFlowNET.Keras/Layers/Rnn/GRU.cs new file mode 100644 index 000000000..0919883d2 --- /dev/null +++ b/src/TensorFlowNET.Keras/Layers/Rnn/GRU.cs @@ -0,0 +1,168 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Tensorflow.Keras.ArgsDefinition; +using Tensorflow.Common.Extensions; +using Tensorflow.Common.Types; +using Tensorflow.Keras.Saving; + + +namespace Tensorflow.Keras.Layers +{ + public class GRU : RNN + { + GRUArgs _args; + private static GRUCell _cell; + + bool _return_runtime; + public GRUCell Cell { get => _cell; } + public int units { get => _args.Units; } + public Activation activation { get => _args.Activation; } + public Activation recurrent_activation { get => _args.RecurrentActivation; } + public bool use_bias { get => _args.UseBias; } + public float dropout { get => _args.Dropout; } + public float recurrent_dropout { get => _args.RecurrentDropout; } + public IInitializer kernel_initializer { get => _args.KernelInitializer; } + public IInitializer recurrent_initializer { get => _args.RecurrentInitializer; } + public IInitializer bias_initializer { get => _args.BiasInitializer; } + public int implementation { get => _args.Implementation; } + public bool reset_after { get => _args.ResetAfter; } + + public GRU(GRUArgs args) : base(CreateCell(args), PreConstruct(args)) + { + _args = args; + + if (_args.Implementation == 0) + { + // Use the red output to act as a warning message that can also be used under the release version + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine("Warning: `implementation=0` has been deprecated, "+ + "and now defaults to `implementation=2`."+ + "Please update your layer call."); + Console.ResetColor(); + } + + GRUCell cell = new GRUCell(new GRUCellArgs + { + Units = _args.Units, + Activation = _args.Activation, + RecurrentActivation = _args.RecurrentActivation, + UseBias = _args.UseBias, + Dropout = _args.Dropout, + RecurrentDropout = _args.RecurrentDropout, + KernelInitializer = _args.KernelInitializer, + RecurrentInitializer = _args.RecurrentInitializer, + BiasInitializer = _args.BiasInitializer, + ResetAfter = _args.ResetAfter, + Implementation = _args.Implementation + }); + _cell = cell; + } + + protected override Tensors Call(Tensors inputs, Tensors initial_state = null, bool? training = null, IOptionalArgs? optional_args = null) + { + GRUOptionalArgs? gru_optional_args = optional_args as GRUOptionalArgs; + if (optional_args is not null && gru_optional_args is null) + { + throw new ArgumentException("The type of optional args should be `GRUOptionalArgs`."); + } + Tensors? mask = gru_optional_args?.Mask; + + // Not support ragger input temporarily; + int row_length = 0; + bool is_ragged_input = false; + + _validate_args_if_ragged(is_ragged_input, mask); + + // GRU does not support constants.Ignore it during process. + (inputs, initial_state, _) = this._process_inputs(inputs, initial_state, null); + + if (mask.Length > 1) + { + mask = mask[0]; + } + + var input_shape = inputs.shape; + var timesteps = _args.TimeMajor ? input_shape[0] : input_shape[1]; + + + // TODO(Wanglongzhi2001), finish _could_use_gpu_kernel part + Func step = (cell_inputs, cell_states) => + { + var res = Cell.Apply(cell_inputs, cell_states, training is null ? true : training.Value); + var (output, state) = res; + return (output, state); + }; + + var (last_output, outputs, states) = keras.backend.rnn( + step, + inputs, + initial_state, + constants: null, + go_backwards: _args.GoBackwards, + mask: mask, + unroll: _args.Unroll, + input_length: ops.convert_to_tensor(timesteps), + time_major: _args.TimeMajor, + zero_output_for_mask: base.Args.ZeroOutputForMask, + return_all_outputs: _args.ReturnSequences + ); + + Tensors output; + if (_args.ReturnSequences) + { + output = outputs; + } + else + { + output = last_output; + } + + if (_args.ReturnState) + { + output = new Tensors { output, states }; + } + return output; + } + + private static IRnnCell CreateCell(GRUArgs gruArgs) + { + return new GRUCell(new GRUCellArgs + { + Units = gruArgs.Units, + Activation = gruArgs.Activation, + RecurrentActivation = gruArgs.RecurrentActivation, + UseBias = gruArgs.UseBias, + Dropout = gruArgs.Dropout, + RecurrentDropout = gruArgs.RecurrentDropout, + KernelInitializer = gruArgs.KernelInitializer, + RecurrentInitializer = gruArgs.RecurrentInitializer, + BiasInitializer = gruArgs.BiasInitializer, + ResetAfter = gruArgs.ResetAfter, + Implementation = gruArgs.Implementation + }); + } + + private static RNNArgs PreConstruct(GRUArgs args) + { + return new RNNArgs + { + ReturnSequences = args.ReturnSequences, + ReturnState = args.ReturnState, + GoBackwards = args.GoBackwards, + Stateful = args.Stateful, + Unroll = args.Unroll, + TimeMajor = args.TimeMajor, + Units = args.Units, + Activation = args.Activation, + RecurrentActivation = args.RecurrentActivation, + UseBias = args.UseBias, + Dropout = args.Dropout, + RecurrentDropout = args.RecurrentDropout, + KernelInitializer = args.KernelInitializer, + RecurrentInitializer = args.RecurrentInitializer, + BiasInitializer = args.BiasInitializer + }; + } + } +} diff --git a/src/TensorFlowNET.Keras/Layers/Rnn/GRUCell.cs b/src/TensorFlowNET.Keras/Layers/Rnn/GRUCell.cs new file mode 100644 index 000000000..2b9c01e31 --- /dev/null +++ b/src/TensorFlowNET.Keras/Layers/Rnn/GRUCell.cs @@ -0,0 +1,281 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Text; +using Tensorflow.Keras.ArgsDefinition; +using Tensorflow.Common.Extensions; +using Tensorflow.Common.Types; +using Tensorflow.Keras.Saving; + +namespace Tensorflow.Keras.Layers +{ + /// + /// Cell class for the GRU layer. + /// + public class GRUCell : DropoutRNNCellMixin + { + GRUCellArgs _args; + IVariableV1 _kernel; + IVariableV1 _recurrent_kernel; + IInitializer _bias_initializer; + IVariableV1 _bias; + INestStructure _state_size; + INestStructure _output_size; + int Units; + public override INestStructure StateSize => _state_size; + + public override INestStructure OutputSize => _output_size; + + public override bool SupportOptionalArgs => false; + public GRUCell(GRUCellArgs args) : base(args) + { + _args = args; + if (_args.Units <= 0) + { + throw new ValueError( + $"units must be a positive integer, got {args.Units}"); + } + _args.Dropout = Math.Min(1f, Math.Max(0f, _args.Dropout)); + _args.RecurrentDropout = Math.Min(1f, Math.Max(0f, this._args.RecurrentDropout)); + if (_args.RecurrentDropout != 0f && _args.Implementation != 1) + { + Debug.WriteLine("RNN `implementation=2` is not supported when `recurrent_dropout` is set." + + "Using `implementation=1`."); + _args.Implementation = 1; + } + Units = _args.Units; + _state_size = new NestList(Units); + _output_size = new NestNode(Units); + } + + public override void build(KerasShapesWrapper input_shape) + { + //base.build(input_shape); + + var single_shape = input_shape.ToSingleShape(); + var input_dim = single_shape[-1]; + + _kernel = add_weight("kernel", (input_dim, _args.Units * 3), + initializer: _args.KernelInitializer + ); + + _recurrent_kernel = add_weight("recurrent_kernel", (Units, Units * 3), + initializer: _args.RecurrentInitializer + ); + if (_args.UseBias) + { + Shape bias_shape; + if (!_args.ResetAfter) + { + bias_shape = new Shape(3 * Units); + } + else + { + bias_shape = (2, 3 * Units); + } + _bias = add_weight("bias", bias_shape, + initializer: _bias_initializer + ); + } + built = true; + } + + protected override Tensors Call(Tensors inputs, Tensors states = null, bool? training = null, IOptionalArgs? optional_args = null) + { + var h_tm1 = states.IsNested() ? states[0] : states.Single(); + var dp_mask = get_dropout_mask_for_cell(inputs, training.Value, count: 3); + var rec_dp_mask = get_recurrent_dropout_mask_for_cell(h_tm1, training.Value, count: 3); + + IVariableV1 input_bias = _bias; + IVariableV1 recurrent_bias = _bias; + if (_args.UseBias) + { + if (!_args.ResetAfter) + { + input_bias = _bias; + recurrent_bias = null; + } + else + { + input_bias = tf.Variable(tf.unstack(_bias.AsTensor())[0]); + recurrent_bias = tf.Variable(tf.unstack(_bias.AsTensor())[1]); + } + } + + + Tensor hh; + Tensor z; + if ( _args.Implementation == 1) + { + Tensor inputs_z; + Tensor inputs_r; + Tensor inputs_h; + if (0f < _args.Dropout && _args.Dropout < 1f) + { + inputs_z = inputs * dp_mask[0]; + inputs_r = inputs * dp_mask[1]; + inputs_h = inputs * dp_mask[2]; + } + else + { + inputs_z = inputs.Single(); + inputs_r = inputs.Single(); + inputs_h = inputs.Single(); + } + + + int startIndex = (int)_kernel.AsTensor().shape[0]; + var _kernel_slice = tf.slice(_kernel.AsTensor(), + new[] { 0, 0 }, new[] { startIndex, Units }); + var x_z = math_ops.matmul(inputs_z, _kernel_slice); + _kernel_slice = tf.slice(_kernel.AsTensor(), + new[] { 0, Units }, new[] { Units, Units}); + var x_r = math_ops.matmul( + inputs_r, _kernel_slice); + int endIndex = (int)_kernel.AsTensor().shape[1]; + _kernel_slice = tf.slice(_kernel.AsTensor(), + new[] { 0, Units * 2 }, new[] { startIndex, endIndex - Units * 2 }); + var x_h = math_ops.matmul(inputs_h, _kernel_slice); + + if(_args.UseBias) + { + x_z = tf.nn.bias_add( + x_z, tf.Variable(input_bias.AsTensor()[$":{Units}"])); + x_r = tf.nn.bias_add( + x_r, tf.Variable(input_bias.AsTensor()[$"{Units}:{Units * 2}"])); + x_h = tf.nn.bias_add( + x_h, tf.Variable(input_bias.AsTensor()[$"{Units * 2}:"])); + } + + Tensor h_tm1_z; + Tensor h_tm1_r; + Tensor h_tm1_h; + if (0f < _args.RecurrentDropout && _args.RecurrentDropout < 1f) + { + h_tm1_z = h_tm1 * rec_dp_mask[0]; + h_tm1_r = h_tm1 * rec_dp_mask[1]; + h_tm1_h = h_tm1 * rec_dp_mask[2]; + } + else + { + h_tm1_z = h_tm1; + h_tm1_r = h_tm1; + h_tm1_h = h_tm1; + } + + startIndex = (int)_recurrent_kernel.AsTensor().shape[0]; + var _recurrent_kernel_slice = tf.slice(_recurrent_kernel.AsTensor(), + new[] { 0, 0 }, new[] { startIndex, Units }); + var recurrent_z = math_ops.matmul( + h_tm1_z, _recurrent_kernel_slice); + _recurrent_kernel_slice = tf.slice(_recurrent_kernel.AsTensor(), + new[] { 0, Units }, new[] { startIndex, Units}); + var recurrent_r = math_ops.matmul( + h_tm1_r, _recurrent_kernel_slice); + if(_args.ResetAfter && _args.UseBias) + { + recurrent_z = tf.nn.bias_add( + recurrent_z, tf.Variable(recurrent_bias.AsTensor()[$":{Units}"])); + recurrent_r = tf.nn.bias_add( + recurrent_r, tf.Variable(recurrent_bias.AsTensor()[$"{Units}: {Units * 2}"])); + } + z = _args.RecurrentActivation.Apply(x_z + recurrent_z); + var r = _args.RecurrentActivation.Apply(x_r + recurrent_r); + + Tensor recurrent_h; + if (_args.ResetAfter) + { + endIndex = (int)_recurrent_kernel.AsTensor().shape[1]; + _recurrent_kernel_slice = tf.slice(_recurrent_kernel.AsTensor(), + new[] { 0, Units * 2 }, new[] { startIndex, endIndex - Units * 2 }); + recurrent_h = math_ops.matmul( + h_tm1_h, _recurrent_kernel_slice); + if(_args.UseBias) + { + recurrent_h = tf.nn.bias_add( + recurrent_h, tf.Variable(recurrent_bias.AsTensor()[$"{Units * 2}:"])); + } + recurrent_h *= r; + } + else + { + _recurrent_kernel_slice = tf.slice(_recurrent_kernel.AsTensor(), + new[] { 0, Units * 2 }, new[] { startIndex, endIndex - Units * 2 }); + recurrent_h = math_ops.matmul( + r * h_tm1_h, _recurrent_kernel_slice); + } + hh = _args.Activation.Apply(x_h + recurrent_h); + } + else + { + if (0f < _args.Dropout && _args.Dropout < 1f) + { + inputs = inputs * dp_mask[0]; + } + + var matrix_x = math_ops.matmul(inputs, _kernel.AsTensor()); + if(_args.UseBias) + { + matrix_x = tf.nn.bias_add(matrix_x, input_bias); + } + var matrix_x_spilted = tf.split(matrix_x, 3, axis: -1); + var x_z = matrix_x_spilted[0]; + var x_r = matrix_x_spilted[1]; + var x_h = matrix_x_spilted[2]; + + Tensor matrix_inner; + if (_args.ResetAfter) + { + matrix_inner = math_ops.matmul(h_tm1, _recurrent_kernel.AsTensor()); + if ( _args.UseBias) + { + matrix_inner = tf.nn.bias_add( + matrix_inner, recurrent_bias); + } + } + else + { + var startIndex = (int)_recurrent_kernel.AsTensor().shape[0]; + var _recurrent_kernel_slice = tf.slice(_recurrent_kernel.AsTensor(), + new[] { 0, 0 }, new[] { startIndex, Units * 2 }); + matrix_inner = math_ops.matmul( + h_tm1, _recurrent_kernel_slice); + } + + var matrix_inner_splitted = tf.split(matrix_inner, new int[] {Units, Units, -1}, axis:-1); + var recurrent_z = matrix_inner_splitted[0]; + var recurrent_r = matrix_inner_splitted[0]; + var recurrent_h = matrix_inner_splitted[0]; + + z = _args.RecurrentActivation.Apply(x_z + recurrent_z); + var r = _args.RecurrentActivation.Apply(x_r + recurrent_r); + + if(_args.ResetAfter) + { + recurrent_h = r * recurrent_h; + } + else + { + var startIndex = (int)_recurrent_kernel.AsTensor().shape[0]; + var endIndex = (int)_recurrent_kernel.AsTensor().shape[1]; + var _recurrent_kernel_slice = tf.slice(_recurrent_kernel.AsTensor(), + new[] { 0, 2*Units }, new[] { startIndex, endIndex - 2 * Units }); + recurrent_h = math_ops.matmul( + r * h_tm1, _recurrent_kernel_slice); + } + hh = _args.Activation.Apply(x_h + recurrent_h); + } + var h = z * h_tm1 + (1 - z) * hh; + if (states.IsNested()) + { + var new_state = new NestList(h); + return new Nest(new INestStructure[] { new NestNode(h), new_state }).ToTensors(); + } + else + { + return new Nest(new INestStructure[] { new NestNode(h), new NestNode(h)}).ToTensors(); + } + + } + } +} diff --git a/src/TensorFlowNET.Keras/Layers/Rnn/LSTM.cs b/src/TensorFlowNET.Keras/Layers/Rnn/LSTM.cs index 59555e62b..c766e8d69 100644 --- a/src/TensorFlowNET.Keras/Layers/Rnn/LSTM.cs +++ b/src/TensorFlowNET.Keras/Layers/Rnn/LSTM.cs @@ -1,8 +1,11 @@ using System.Linq; -using Tensorflow.Keras.ArgsDefinition.Rnn; +using Tensorflow.Keras.ArgsDefinition; using Tensorflow.Keras.Engine; +using Tensorflow.Common.Types; +using Tensorflow.Common.Extensions; +using Tensorflow.Keras.Saving; -namespace Tensorflow.Keras.Layers.Rnn +namespace Tensorflow.Keras.Layers { /// /// Long Short-Term Memory layer - Hochreiter 1997. @@ -12,23 +15,112 @@ namespace Tensorflow.Keras.Layers.Rnn /// public class LSTM : RNN { - LSTMArgs args; - InputSpec[] state_spec; + LSTMArgs _args; + InputSpec[] _state_spec; + InputSpec _input_spec; + bool _could_use_gpu_kernel; + public LSTMArgs Args { get => _args; } + public LSTM(LSTMArgs args) : + base(CreateCell(args), args) + { + _args = args; + _input_spec = new InputSpec(ndim: 3); + _state_spec = new[] { args.Units, args.Units }.Select(dim => new InputSpec(shape: (-1, dim))).ToArray(); + _could_use_gpu_kernel = args.Activation == keras.activations.Tanh + && args.RecurrentActivation == keras.activations.Sigmoid + && args.RecurrentDropout == 0 && !args.Unroll && args.UseBias + && ops.executing_eagerly_outside_functions(); + } - int units => args.Units; + private static IRnnCell CreateCell(LSTMArgs lstmArgs) + { + return new LSTMCell(new LSTMCellArgs() + { + Units = lstmArgs.Units, + Activation = lstmArgs.Activation, + RecurrentActivation = lstmArgs.RecurrentActivation, + UseBias = lstmArgs.UseBias, + KernelInitializer = lstmArgs.KernelInitializer, + RecurrentInitializer = lstmArgs.RecurrentInitializer, + UnitForgetBias = lstmArgs.UnitForgetBias, + BiasInitializer = lstmArgs.BiasInitializer, + // TODO(Rinne): kernel_regularizer + // TODO(Rinne): recurrent_regularizer + // TODO(Rinne): bias_regularizer + // TODO(Rinne): kernel_constriant + // TODO(Rinne): recurrent_constriant + // TODO(Rinne): bias_constriant + Dropout = lstmArgs.Dropout, + RecurrentDropout = lstmArgs.RecurrentDropout, + Implementation = lstmArgs.Implementation, + DType = lstmArgs.DType, + Trainable = lstmArgs.Trainable + }); + } - public LSTM(LSTMArgs args) : - base(args) + protected override Tensors Call(Tensors inputs, Tensors initial_state = null, bool? training = null, IOptionalArgs? optional_args = null) { - this.args = args; - state_spec = new[] { units, units } - .Select(dim => new InputSpec(shape: (-1, dim))) - .ToArray(); + // skip the condition of ragged input + + (inputs, initial_state, _) = _process_inputs(inputs, initial_state, null); + + Tensor mask = null; + if(optional_args is RnnOptionalArgs rnnArgs) + { + mask = rnnArgs.Mask; + } + + var single_input = inputs.Single; + var input_shape = single_input.shape; + var timesteps = _args.TimeMajor ? input_shape[0] : input_shape[1]; + + _maybe_reset_cell_dropout_mask(Cell); + + Func step = (inputs, states) => + { + var res = Cell.Apply(inputs, states, training is null ? true : training.Value); + var (output, state) = res; + return (output, state); + }; + + var (last_output, outputs, states) = keras.backend.rnn( + step, + inputs, + initial_state, + constants: null, + go_backwards: _args.GoBackwards, + mask: mask, + unroll: _args.Unroll, + input_length: ops.convert_to_tensor(timesteps), + time_major: _args.TimeMajor, + zero_output_for_mask: _args.ZeroOutputForMask, + return_all_outputs: _args.ReturnSequences + ); + + Tensor output; + if (_args.ReturnSequences) + { + output = keras.backend.maybe_convert_to_ragged(false, outputs, (int)timesteps, _args.GoBackwards); + } + else + { + output = last_output; + } + + if (_args.ReturnState) + { + return new Tensor[] { output }.Concat(states).ToArray().ToTensors(); + } + else + { + return output; + } } - protected override Tensors Call(Tensors inputs, Tensor state = null, bool? training = null) + public override IKerasConfig get_config() { - return base.Call(inputs, state: state, training: training); + return _args; } + } } diff --git a/src/TensorFlowNET.Keras/Layers/Rnn/LSTMCell.cs b/src/TensorFlowNET.Keras/Layers/Rnn/LSTMCell.cs index a622c91a9..e4fc6dd22 100644 --- a/src/TensorFlowNET.Keras/Layers/Rnn/LSTMCell.cs +++ b/src/TensorFlowNET.Keras/Layers/Rnn/LSTMCell.cs @@ -1,16 +1,233 @@ -using Tensorflow.Keras.ArgsDefinition.Rnn; +using Newtonsoft.Json; +using Serilog.Core; +using System.Diagnostics; +using Tensorflow.Common.Extensions; +using Tensorflow.Common.Types; +using Tensorflow.Keras.ArgsDefinition; using Tensorflow.Keras.Engine; +using Tensorflow.Keras.Saving; +using Tensorflow.Keras.Utils; -namespace Tensorflow.Keras.Layers.Rnn +namespace Tensorflow.Keras.Layers { - public class LSTMCell : Layer + /// + /// Cell class for the LSTM layer. + /// See [the Keras RNN API guide](https://www.tensorflow.org/guide/keras/rnn) + /// for details about the usage of RNN API. + /// This class processes one step within the whole time sequence input, whereas + /// `tf.keras.layer.LSTM` processes the whole sequence. + /// + public class LSTMCell : DropoutRNNCellMixin { - LSTMCellArgs args; + LSTMCellArgs _args; + IVariableV1 _kernel; + IVariableV1 _recurrent_kernel; + IInitializer _bias_initializer; + IVariableV1 _bias; + INestStructure _state_size; + INestStructure _output_size; + public override INestStructure StateSize => _state_size; + public override INestStructure OutputSize => _output_size; + + public override bool SupportOptionalArgs => false; public LSTMCell(LSTMCellArgs args) : base(args) { - this.args = args; + _args = args; + if (args.Units <= 0) + { + throw new ValueError( + $"units must be a positive integer, got {args.Units}"); + } + _args.Dropout = Math.Min(1f, Math.Max(0f, this._args.Dropout)); + _args.RecurrentDropout = Math.Min(1f, Math.Max(0f, this._args.RecurrentDropout)); + if (_args.RecurrentDropout != 0f && _args.Implementation != 1) + { + Debug.WriteLine("RNN `implementation=2` is not supported when `recurrent_dropout` is set." + + "Using `implementation=1`."); + _args.Implementation = 1; + } + + _state_size = new NestList(_args.Units, _args.Units); + _output_size = new NestNode(_args.Units); + } + + public override void build(KerasShapesWrapper input_shape) + { + base.build(input_shape); + var single_shape = input_shape.ToSingleShape(); + var input_dim = single_shape[-1]; + _kernel = add_weight("kernel", (input_dim, _args.Units * 4), + initializer: _args.KernelInitializer + ); + + _recurrent_kernel = add_weight("recurrent_kernel", (_args.Units, _args.Units * 4), + initializer: _args.RecurrentInitializer + ); + + if (_args.UseBias) + { + if (_args.UnitForgetBias) + { + Tensor bias_initializer() + { + return keras.backend.concatenate( + new Tensors( + _args.BiasInitializer.Apply(new InitializerArgs(shape: (_args.Units))), + tf.ones_initializer.Apply(new InitializerArgs(shape: (_args.Units))), + _args.BiasInitializer.Apply(new InitializerArgs(shape: (_args.Units)))), axis: 0); + } + } + else + { + _bias_initializer = _args.BiasInitializer; + } + _bias = add_weight("bias", (_args.Units * 4), + initializer: _bias_initializer + ); + } + built = true; + } + protected override Tensors Call(Tensors inputs, Tensors states = null, bool? training = null, IOptionalArgs? optional_args = null) + { + var h_tm1 = states[0]; // previous memory state + var c_tm1 = states[1]; // previous carry state + + var dp_mask = get_dropout_mask_for_cell(inputs, training.Value, count: 4); + var rec_dp_mask = get_recurrent_dropout_mask_for_cell( + h_tm1, training.Value, count: 4); + + Tensor c; + Tensor o; + if (_args.Implementation == 1) + { + Tensor inputs_i; + Tensor inputs_f; + Tensor inputs_c; + Tensor inputs_o; + if (0f < _args.Dropout && _args.Dropout < 1f) + { + inputs_i = inputs * dp_mask[0]; + inputs_f = inputs * dp_mask[1]; + inputs_c = inputs * dp_mask[2]; + inputs_o = inputs * dp_mask[3]; + } + else + { + inputs_i = inputs; + inputs_f = inputs; + inputs_c = inputs; + inputs_o = inputs; + } + var k = tf.split(_kernel.AsTensor(), num_split: 4, axis: 1); + Tensor k_i = k[0], k_f = k[1], k_c = k[2], k_o = k[3]; + var x_i = math_ops.matmul(inputs_i, k_i); + var x_f = math_ops.matmul(inputs_f, k_f); + var x_c = math_ops.matmul(inputs_c, k_c); + var x_o = math_ops.matmul(inputs_o, k_o); + if (_args.UseBias) + { + var b = tf.split(_bias.AsTensor(), num_split: 4, axis: 0); + Tensor b_i = b[0], b_f = b[1], b_c = b[2], b_o = b[3]; + x_i = gen_nn_ops.bias_add(x_i, b_i); + x_f = gen_nn_ops.bias_add(x_f, b_f); + x_c = gen_nn_ops.bias_add(x_c, b_c); + x_o = gen_nn_ops.bias_add(x_o, b_o); + } + + Tensor h_tm1_i; + Tensor h_tm1_f; + Tensor h_tm1_c; + Tensor h_tm1_o; + if (0f < _args.RecurrentDropout && _args.RecurrentDropout < 1f) + { + h_tm1_i = h_tm1 * rec_dp_mask[0]; + h_tm1_f = h_tm1 * rec_dp_mask[1]; + h_tm1_c = h_tm1 * rec_dp_mask[2]; + h_tm1_o = h_tm1 * rec_dp_mask[3]; + } + else + { + h_tm1_i = h_tm1; + h_tm1_f = h_tm1; + h_tm1_c = h_tm1; + h_tm1_o = h_tm1; + } + var x = new Tensor[] { x_i, x_f, x_c, x_o }; + var h_tm1_array = new Tensor[] { h_tm1_i, h_tm1_f, h_tm1_c, h_tm1_o }; + (c, o) = _compute_carry_and_output(x, h_tm1_array, c_tm1); + } + else + { + if (0f < _args.Dropout && _args.Dropout < 1f) + inputs = inputs * dp_mask[0]; + var z = math_ops.matmul(inputs, _kernel.AsTensor()); + z += math_ops.matmul(h_tm1, _recurrent_kernel.AsTensor()); + if (_args.UseBias) + { + z = tf.nn.bias_add(z, _bias); + } + var z_array = tf.split(z, num_split: 4, axis: 1); + (c, o) = _compute_carry_and_output_fused(z_array, c_tm1); + } + var h = o * _args.Activation.Apply(c); + // 这里是因为 Tensors 类初始化的时候会把第一个元素之后的元素打包成一个数组 + return new Nest(new INestStructure[] { new NestNode(h), new NestList(h, c) }).ToTensors(); + } + + /// + /// Computes carry and output using split kernels. + /// + /// + /// + /// + /// + /// + public Tensors _compute_carry_and_output(Tensor[] x, Tensor[] h_tm1, Tensor c_tm1) + { + Tensor x_i = x[0], x_f = x[1], x_c = x[2], x_o = x[3]; + Tensor h_tm1_i = h_tm1[0], h_tm1_f = h_tm1[1], h_tm1_c = h_tm1[2], + h_tm1_o = h_tm1[3]; + + var _recurrent_kernel_tensor = _recurrent_kernel.AsTensor(); + int startIndex = (int)_recurrent_kernel_tensor.shape[0]; + var _recurrent_kernel_slice = tf.slice(_recurrent_kernel_tensor, + new[] { 0, 0 }, new[] { startIndex, _args.Units }); + var i = _args.RecurrentActivation.Apply( + x_i + math_ops.matmul(h_tm1_i, _recurrent_kernel_slice)); + _recurrent_kernel_slice = tf.slice(_recurrent_kernel_tensor, + new[] { 0, _args.Units }, new[] { startIndex, _args.Units}); + var f = _args.RecurrentActivation.Apply( + x_f + math_ops.matmul(h_tm1_f, _recurrent_kernel_slice)); + _recurrent_kernel_slice = tf.slice(_recurrent_kernel_tensor, + new[] { 0, _args.Units * 2 }, new[] { startIndex, _args.Units }); + var c = f * c_tm1 + i * _args.Activation.Apply( + x_c + math_ops.matmul(h_tm1_c, _recurrent_kernel_slice)); + _recurrent_kernel_slice = tf.slice(_recurrent_kernel_tensor, + new[] { 0, _args.Units * 3 }, new[] { startIndex, _args.Units }); + var o = _args.Activation.Apply( + x_o + math_ops.matmul(h_tm1_o, _recurrent_kernel_slice)); + + return new Tensors(c, o); + } + + /// + /// Computes carry and output using fused kernels. + /// + /// + /// + /// + public Tensors _compute_carry_and_output_fused(Tensor[] z, Tensor c_tm1) + { + Tensor z0 = z[0], z1 = z[1], z2 = z[2], z3 = z[3]; + var i = _args.RecurrentActivation.Apply(z0); + var f = _args.RecurrentActivation.Apply(z1); + var c = f * c_tm1 + i * _args.Activation.Apply(z2); + var o = _args.RecurrentActivation.Apply(z3); + return new Tensors(c, o); } } + + } diff --git a/src/TensorFlowNET.Keras/Layers/Rnn/RNN.cs b/src/TensorFlowNET.Keras/Layers/Rnn/RNN.cs index 310e80574..fec75559c 100644 --- a/src/TensorFlowNET.Keras/Layers/Rnn/RNN.cs +++ b/src/TensorFlowNET.Keras/Layers/Rnn/RNN.cs @@ -1,136 +1,546 @@ -using System; +using OneOf; +using System; using System.Collections.Generic; +using System.Reflection; using Tensorflow.Keras.ArgsDefinition; -using Tensorflow.Keras.ArgsDefinition.Rnn; using Tensorflow.Keras.Engine; using Tensorflow.Keras.Saving; +using Tensorflow.Util; +using Tensorflow.Common.Extensions; +using System.Linq.Expressions; +using Tensorflow.Keras.Utils; +using Tensorflow.Common.Types; +using System.Runtime.CompilerServices; // from tensorflow.python.distribute import distribution_strategy_context as ds_context; -namespace Tensorflow.Keras.Layers.Rnn +namespace Tensorflow.Keras.Layers { - public class RNN : Layer + /// + /// Base class for recurrent layers. + /// See [the Keras RNN API guide](https://www.tensorflow.org/guide/keras/rnn) + /// for details about the usage of RNN API. + /// + public class RNN : RnnBase { - private RNNArgs args; - private object input_spec = null; // or NoneValue?? - private object state_spec = null; - private object _states = null; - private object constants_spec = null; - private int _num_constants = 0; - protected IVariableV1 kernel; - protected IVariableV1 bias; - protected ILayer cell; - public RNN(RNNArgs args) : base(PreConstruct(args)) - { - this.args = args; + private RNNArgs _args; + private object _input_spec = null; // or NoneValue?? + private object _state_spec = null; + private object _constants_spec = null; + private Tensors _states = null; + private int _num_constants; + protected IVariableV1 _kernel; + protected IVariableV1 _bias; + private IRnnCell _cell; + + public RNNArgs Args { get => _args; } + public IRnnCell Cell + { + get + { + return _cell; + } + init + { + _cell = value; + _self_tracked_trackables.Add(_cell); + } + } + + public RNN(IRnnCell cell, RNNArgs args) : base(PreConstruct(args)) + { + _args = args; + SupportsMasking = true; + + Cell = cell; + + // get input_shape + _args = PreConstruct(args); + + _num_constants = 0; + } + + public RNN(IEnumerable cells, RNNArgs args) : base(PreConstruct(args)) + { + _args = args; SupportsMasking = true; - // The input shape is unknown yet, it could have nested tensor inputs, and - // the input spec will be the list of specs for nested inputs, the structure - // of the input_spec will be the same as the input. + Cell = new StackedRNNCells(cells, new StackedRNNCellsArgs()); + + // get input_shape + _args = PreConstruct(args); - //if(stateful) - //{ - // if (ds_context.has_strategy()) // ds_context???? - // { - // throw new Exception("RNNs with stateful=True not yet supported with tf.distribute.Strategy"); - // } - //} + _num_constants = 0; + } + + // States is a tuple consist of cell states_size, like (cell1.state_size, cell2.state_size,...) + // state_size can be a single integer, can also be a list/tuple of integers, can also be TensorShape or a list/tuple of TensorShape + public Tensors States + { + get + { + if (_states == null) + { + // CHECK(Rinne): check if this is correct. + var nested = Cell.StateSize.MapStructure(x => null); + _states = nested.AsNest().ToTensors(); + } + return _states; + } + set { _states = value; } + } + + private INestStructure compute_output_shape(Shape input_shape) + { + var batch = input_shape[0]; + var time_step = input_shape[1]; + if (_args.TimeMajor) + { + (batch, time_step) = (time_step, batch); + } + + // state_size is a array of ints or a positive integer + var state_size = Cell.StateSize; + if(state_size?.TotalNestedCount == 1) + { + state_size = new NestList(state_size.Flatten().First()); + } + + Func _get_output_shape = (flat_output_size) => + { + var output_dim = new Shape(flat_output_size).as_int_list(); + Shape output_shape; + if (_args.ReturnSequences) + { + if (_args.TimeMajor) + { + output_shape = new Shape(new int[] { (int)time_step, (int)batch }.concat(output_dim)); + } + else + { + output_shape = new Shape(new int[] { (int)batch, (int)time_step }.concat(output_dim)); + + } + } + else + { + output_shape = new Shape(new int[] { (int)batch }.concat(output_dim)); + } + return output_shape; + }; + + Type type = Cell.GetType(); + PropertyInfo output_size_info = type.GetProperty("output_size"); + INestStructure output_shape; + if (output_size_info != null) + { + output_shape = Nest.MapStructure(_get_output_shape, Cell.OutputSize); + } + else + { + output_shape = new NestNode(_get_output_shape(state_size.Flatten().First())); + } + + if (_args.ReturnState) + { + Func _get_state_shape = (flat_state) => + { + var state_shape = new int[] { (int)batch }.concat(new Shape(flat_state).as_int_list()); + return new Shape(state_shape); + }; + + + var state_shape = Nest.MapStructure(_get_state_shape, state_size); + + return new Nest(new[] { output_shape, state_shape } ); + } + else + { + return output_shape; + } + + } + + private Tensors compute_mask(Tensors inputs, Tensors mask) + { + // Time step masks must be the same for each input. + // This is because the mask for an RNN is of size [batch, time_steps, 1], + // and specifies which time steps should be skipped, and a time step + // must be skipped for all inputs. + + mask = nest.flatten(mask)[0]; + var output_mask = _args.ReturnSequences ? mask : null; + if (_args.ReturnState) + { + var state_mask = new List(); + for (int i = 0; i < len(States); i++) + { + state_mask.Add(null); + } + return new List { output_mask }.concat(state_mask); + } + else + { + return output_mask; + } } public override void build(KerasShapesWrapper input_shape) { - if (!cell.Built) + _buildInputShape = input_shape; + input_shape = new KerasShapesWrapper(input_shape.Shapes[0]); + + InputSpec get_input_spec(Shape shape) + { + var input_spec_shape = shape.as_int_list(); + + var (batch_index, time_step_index) = _args.TimeMajor ? (1, 0) : (0, 1); + if (!_args.Stateful) + { + input_spec_shape[batch_index] = -1; + } + input_spec_shape[time_step_index] = -1; + return new InputSpec(shape: input_spec_shape); + } + + Shape get_step_input_shape(Shape shape) + { + + // return shape[1:] if self.time_major else (shape[0],) + shape[2:] + if (_args.TimeMajor) + { + return shape.as_int_list().ToList().GetRange(1, shape.Length - 1).ToArray(); + } + else + { + return new int[] { shape.as_int_list()[0] }.concat(shape.as_int_list().ToList().GetRange(2, shape.Length - 2).ToArray()); + } + + + } + + object get_state_spec(Shape shape) + { + var state_spec_shape = shape.as_int_list(); + // append bacth dim + state_spec_shape = new int[] { -1 }.concat(state_spec_shape); + return new InputSpec(shape: state_spec_shape); + } + + // Check whether the input shape contains any nested shapes. It could be + // (tensor_shape(1, 2), tensor_shape(3, 4)) or (1, 2, 3) which is from + // numpy inputs. + + + if (Cell is Layer layer && !layer.Built) + { + layer.build(input_shape); + layer.Built = true; + } + + this.built = true; + } + + /// + /// + /// + /// + /// List of initial state tensors to be passed to the first call of the cell + /// + /// + /// + /// + /// + protected override Tensors Call(Tensors inputs, Tensors initial_state = null, bool? training = null, IOptionalArgs? optional_args = null) + { + RnnOptionalArgs? rnn_optional_args = optional_args as RnnOptionalArgs; + if(optional_args is not null && rnn_optional_args is null) { - cell.build(input_shape); + throw new ArgumentException("The optional args shhould be of type `RnnOptionalArgs`"); + } + Tensors? constants = rnn_optional_args?.Constants; + Tensors? mask = rnn_optional_args?.Mask; + //var (inputs_padded, row_length) = BackendImpl.convert_inputs_if_ragged(inputs); + // 暂时先不接受ragged tensor + int row_length = 0; // TODO(Rinne): support this param. + bool is_ragged_input = false; + _validate_args_if_ragged(is_ragged_input, mask); + + (inputs, initial_state, constants) = _process_inputs(inputs, initial_state, constants); + + _maybe_reset_cell_dropout_mask(Cell); + if (Cell is StackedRNNCells) + { + var stack_cell = Cell as StackedRNNCells; + foreach (IRnnCell cell in stack_cell.Cells) + { + _maybe_reset_cell_dropout_mask(cell); + } + } + + if (mask != null) + { + // Time step masks must be the same for each input. + mask = mask.Flatten().First(); + } + + Shape input_shape; + if (!inputs.IsNested()) + { + // In the case of nested input, use the first element for shape check + // input_shape = nest.flatten(inputs)[0].shape; + // TODO(Wanglongzhi2001) + input_shape = inputs.Flatten().First().shape; + } + else + { + input_shape = inputs.shape; + } + + var timesteps = _args.TimeMajor ? input_shape[0] : input_shape[1]; + + if (_args.Unroll && timesteps == null) + { + throw new ValueError( + "Cannot unroll a RNN if the " + + "time dimension is undefined. \n" + + "- If using a Sequential model, " + + "specify the time dimension by passing " + + "an `input_shape` or `batch_input_shape` " + + "argument to your first layer. If your " + + "first layer is an Embedding, you can " + + "also use the `input_length` argument.\n" + + "- If using the functional API, specify " + + "the time dimension by passing a `shape` " + + "or `batch_shape` argument to your Input layer." + ); + } + + // cell_call_fn = (self.cell.__call__ if callable(self.cell) else self.cell.call) + Func step; + bool is_tf_rnn_cell = false; + if (constants is not null) + { + if (!Cell.SupportOptionalArgs) + { + throw new ValueError( + $"RNN cell {Cell} does not support constants." + + $"Received: constants={constants}"); + } + + step = (inputs, states) => + { + constants = new Tensors(states.TakeLast(_num_constants).ToArray()); + states = new Tensors(states.SkipLast(_num_constants).ToArray()); + states = len(states) == 1 && is_tf_rnn_cell ? new Tensors(states[0]) : states; + var (output, new_states) = Cell.Apply(inputs, states, optional_args: new RnnOptionalArgs() { Constants = constants }); + return (output, new_states); + }; + } + else + { + step = (inputs, states) => + { + states = len(states) == 1 && is_tf_rnn_cell ? new Tensors(states.First()) : states; + var (output, new_states) = Cell.Apply(inputs, states); + return (output, new_states); + }; + } + + var (last_output, outputs, states) = keras.backend.rnn( + step, + inputs, + initial_state, + constants: constants, + go_backwards: _args.GoBackwards, + mask: mask, + unroll: _args.Unroll, + input_length: row_length != null ? new Tensor(row_length) : new Tensor(timesteps), + time_major: _args.TimeMajor, + zero_output_for_mask: _args.ZeroOutputForMask, + return_all_outputs: _args.ReturnSequences); + + if (_args.Stateful) + { + throw new NotImplementedException("this argument havn't been developed."); + } + + Tensors output = new Tensors(); + if (_args.ReturnSequences) + { + // TODO(Rinne): add go_backwards parameter and revise the `row_length` param + output = keras.backend.maybe_convert_to_ragged(is_ragged_input, outputs, row_length, false); + } + else + { + output = last_output; + } + + if (_args.ReturnState) + { + foreach (var state in states) + { + output.Add(state); + } + return output; + } + else + { + //var tapeSet = tf.GetTapeSet(); + //foreach(var tape in tapeSet) + //{ + // tape.Watch(output); + //} + return output; } } - protected override Tensors Call(Tensors inputs, Tensor state = null, bool? training = null) + public override Tensors Apply(Tensors inputs, Tensors initial_states = null, bool? training = false, IOptionalArgs? optional_args = null) { - return base.Call(inputs, state, training); + RnnOptionalArgs? rnn_optional_args = optional_args as RnnOptionalArgs; + if (optional_args is not null && rnn_optional_args is null) + { + throw new ArgumentException("The type of optional args should be `RnnOptionalArgs`."); + } + Tensors? constants = rnn_optional_args?.Constants; + (inputs, initial_states, constants) = RnnUtils.standardize_args(inputs, initial_states, constants, _num_constants); + + if(initial_states is null && constants is null) + { + return base.Apply(inputs); + } + + // TODO(Rinne): implement it. + throw new NotImplementedException(); } - private static RNNArgs PreConstruct(RNNArgs args) + protected (Tensors inputs, Tensors initial_state, Tensors constants) _process_inputs(Tensors inputs, Tensors initial_state, Tensors constants) + { + if (inputs.Length > 1) + { + if (_num_constants != 0) + { + initial_state = new Tensors(inputs.Skip(1).ToArray()); + } + else + { + initial_state = new Tensors(inputs.Skip(1).SkipLast(_num_constants).ToArray()); + constants = new Tensors(inputs.TakeLast(_num_constants).ToArray()); + } + if (len(initial_state) == 0) + initial_state = null; + inputs = inputs[0]; + } + + + if (_args.Stateful) + { + if (initial_state != null) + { + var tmp = new Tensor[] { }; + foreach (var s in nest.flatten(States)) + { + tmp.add(tf.math.count_nonzero(s.Single())); + } + var non_zero_count = tf.add_n(tmp); + initial_state = tf.cond(non_zero_count > 0, States, initial_state); + if ((int)non_zero_count.numpy() > 0) + { + initial_state = States; + } + } + else + { + initial_state = States; + } + //initial_state = Nest.MapStructure(v => tf.cast(v, this.), initial_state); + } + else if (initial_state is null) + { + initial_state = get_initial_state(inputs); + } + + if (initial_state.Length != States.Length) + { + throw new ValueError($"Layer {this} expects {States.Length} state(s), " + + $"but it received {initial_state.Length} " + + $"initial state(s). Input received: {inputs}"); + } + + return (inputs, initial_state, constants); + } + + protected void _validate_args_if_ragged(bool is_ragged_input, Tensors mask) { - if (args.Kwargs == null) + if (!is_ragged_input) { - args.Kwargs = new Dictionary(); + return; } + if (_args.Unroll) + { + throw new ValueError("The input received contains RaggedTensors and does " + + "not support unrolling. Disable unrolling by passing " + + "`unroll=False` in the RNN Layer constructor."); + } + if (mask != null) + { + throw new ValueError($"The mask that was passed in was {mask}, which " + + "cannot be applied to RaggedTensor inputs. Please " + + "make sure that there is no mask injected by upstream " + + "layers."); + } + + } + + protected void _maybe_reset_cell_dropout_mask(ILayer cell) + { + if (cell is DropoutRNNCellMixin CellDRCMixin) + { + CellDRCMixin.reset_dropout_mask(); + CellDRCMixin.reset_recurrent_dropout_mask(); + } + } + + private static RNNArgs PreConstruct(RNNArgs args) + { // If true, the output for masked timestep will be zeros, whereas in the // false case, output from previous timestep is returned for masked timestep. - var zeroOutputForMask = (bool)args.Kwargs.Get("zero_output_for_mask", false); + var zeroOutputForMask = args.ZeroOutputForMask; Shape input_shape; - var propIS = (Shape)args.Kwargs.Get("input_shape", null); - var propID = (int?)args.Kwargs.Get("input_dim", null); - var propIL = (int?)args.Kwargs.Get("input_length", null); + var propIS = args.InputShape; + var propID = args.InputDim; + var propIL = args.InputLength; if (propIS == null && (propID != null || propIL != null)) { input_shape = new Shape( propIL ?? -1, propID ?? -1); - args.Kwargs["input_shape"] = input_shape; + args.InputShape = input_shape; } return args; } - public RNN New(LayerRnnCell cell, - bool return_sequences = false, - bool return_state = false, - bool go_backwards = false, - bool stateful = false, - bool unroll = false, - bool time_major = false) - => new RNN(new RNNArgs - { - Cell = cell, - ReturnSequences = return_sequences, - ReturnState = return_state, - GoBackwards = go_backwards, - Stateful = stateful, - Unroll = unroll, - TimeMajor = time_major - }); - - public RNN New(IList cell, - bool return_sequences = false, - bool return_state = false, - bool go_backwards = false, - bool stateful = false, - bool unroll = false, - bool time_major = false) - => new RNN(new RNNArgs - { - Cell = new StackedRNNCells(new StackedRNNCellsArgs { Cells = cell }), - ReturnSequences = return_sequences, - ReturnState = return_state, - GoBackwards = go_backwards, - Stateful = stateful, - Unroll = unroll, - TimeMajor = time_major - }); - - - protected Tensor get_initial_state(Tensor inputs) + public Tensors __call__(Tensors inputs, Tensor state = null, Tensor training = null) { - return _generate_zero_filled_state_for_cell(null, null); + throw new NotImplementedException(); } - Tensor _generate_zero_filled_state_for_cell(LSTMCell cell, Tensor batch_size) + protected Tensors get_initial_state(Tensors inputs) { - throw new NotImplementedException(""); + var input = inputs[0]; + var input_shape = array_ops.shape(inputs); + var batch_size = _args.TimeMajor ? input_shape[1] : input_shape[0]; + var dtype = input.dtype; + Tensors init_state = Cell.GetInitialState(null, batch_size, dtype); + return init_state; } - // Check whether the state_size contains multiple states. - public static bool _is_multiple_state(object state_size) + public override IKerasConfig get_config() { - var myIndexerProperty = state_size.GetType().GetProperty("Item"); - return myIndexerProperty != null - && myIndexerProperty.GetIndexParameters().Length == 1 - && !(state_size.GetType() == typeof(Shape)); + return _args; } } } diff --git a/src/TensorFlowNET.Keras/Layers/Rnn/RnnBase.cs b/src/TensorFlowNET.Keras/Layers/Rnn/RnnBase.cs new file mode 100644 index 000000000..1419da4b2 --- /dev/null +++ b/src/TensorFlowNET.Keras/Layers/Rnn/RnnBase.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Tensorflow.Keras.ArgsDefinition; +using Tensorflow.Keras.Engine; + +namespace Tensorflow.Keras.Layers +{ + public abstract class RnnBase: Layer + { + public RnnBase(LayerArgs args): base(args) { } + } +} diff --git a/src/TensorFlowNET.Keras/Layers/Rnn/SimpleRNN.cs b/src/TensorFlowNET.Keras/Layers/Rnn/SimpleRNN.cs index 2d7aab70e..9c199eb43 100644 --- a/src/TensorFlowNET.Keras/Layers/Rnn/SimpleRNN.cs +++ b/src/TensorFlowNET.Keras/Layers/Rnn/SimpleRNN.cs @@ -1,32 +1,35 @@ using System.Data; -using Tensorflow.Keras.ArgsDefinition.Rnn; +using Tensorflow.Keras.ArgsDefinition; using Tensorflow.Keras.Saving; using Tensorflow.Operations.Activation; using static HDF.PInvoke.H5Z; using static Tensorflow.ApiDef.Types; -namespace Tensorflow.Keras.Layers.Rnn +namespace Tensorflow.Keras.Layers { public class SimpleRNN : RNN { SimpleRNNArgs args; - public SimpleRNN(SimpleRNNArgs args) : base(args) + public SimpleRNN(SimpleRNNArgs args) : base(CreateCellForArgs(args), args) { this.args = args; } - public override void build(KerasShapesWrapper input_shape) + private static SimpleRNNCell CreateCellForArgs(SimpleRNNArgs args) { - var single_shape = input_shape.ToSingleShape(); - var input_dim = single_shape[-1]; - _buildInputShape = input_shape; - - kernel = add_weight("kernel", (single_shape[-1], args.Units), - initializer: args.KernelInitializer - //regularizer = self.kernel_regularizer, - //constraint = self.kernel_constraint, - //caching_device = default_caching_device, - ); + return new SimpleRNNCell(new SimpleRNNCellArgs() + { + Units = args.Units, + Activation = args.Activation, + UseBias = args.UseBias, + KernelInitializer = args.KernelInitializer, + RecurrentInitializer = args.RecurrentInitializer, + BiasInitializer = args.BiasInitializer, + Dropout = args.Dropout, + RecurrentDropout = args.RecurrentDropout, + DType = args.DType, + Trainable = args.Trainable, + }); } } } \ No newline at end of file diff --git a/src/TensorFlowNET.Keras/Layers/Rnn/SimpleRNNCell.cs b/src/TensorFlowNET.Keras/Layers/Rnn/SimpleRNNCell.cs index 46061b211..e74b56925 100644 --- a/src/TensorFlowNET.Keras/Layers/Rnn/SimpleRNNCell.cs +++ b/src/TensorFlowNET.Keras/Layers/Rnn/SimpleRNNCell.cs @@ -1,50 +1,119 @@ using System; using System.Collections.Generic; using System.Text; -using Tensorflow.Keras.ArgsDefinition.Rnn; +using Tensorflow.Keras.ArgsDefinition; using Tensorflow.Keras.Engine; using Tensorflow.Keras.Saving; +using Tensorflow.Common.Types; +using Tensorflow.Common.Extensions; +using Tensorflow.Keras.Utils; +using Tensorflow.Graphs; -namespace Tensorflow.Keras.Layers.Rnn +namespace Tensorflow.Keras.Layers { - public class SimpleRNNCell : Layer + /// + /// Cell class for SimpleRNN. + /// See [the Keras RNN API guide](https://www.tensorflow.org/guide/keras/rnn) + /// for details about the usage of RNN API. + /// This class processes one step within the whole time sequence input, whereas + /// `tf.keras.layer.SimpleRNN` processes the whole sequence. + /// + public class SimpleRNNCell : DropoutRNNCellMixin { - SimpleRNNArgs args; - IVariableV1 kernel; - IVariableV1 recurrent_kernel; - IVariableV1 bias; + SimpleRNNCellArgs _args; + IVariableV1 _kernel; + IVariableV1 _recurrent_kernel; + IVariableV1 _bias; + INestStructure _state_size; + INestStructure _output_size; - public SimpleRNNCell(SimpleRNNArgs args) : base(args) + public override INestStructure StateSize => _state_size; + public override INestStructure OutputSize => _output_size; + public override bool SupportOptionalArgs => false; + + public SimpleRNNCell(SimpleRNNCellArgs args) : base(args) { - this.args = args; + this._args = args; + if (args.Units <= 0) + { + throw new ValueError( + $"units must be a positive integer, got {args.Units}"); + } + this._args.Dropout = Math.Min(1f, Math.Max(0f, this._args.Dropout)); + this._args.RecurrentDropout = Math.Min(1f, Math.Max(0f, this._args.RecurrentDropout)); + _state_size = new NestNode(args.Units); + _output_size = new NestNode(args.Units); } public override void build(KerasShapesWrapper input_shape) { + // TODO(Rinne): add the cache. var single_shape = input_shape.ToSingleShape(); var input_dim = single_shape[-1]; - kernel = add_weight("kernel", (single_shape[-1], args.Units), - initializer: args.KernelInitializer + _kernel = add_weight("kernel", (single_shape[-1], _args.Units), + initializer: _args.KernelInitializer ); - recurrent_kernel = add_weight("recurrent_kernel", (args.Units, args.Units), - initializer: args.RecurrentInitializer + _recurrent_kernel = add_weight("recurrent_kernel", (_args.Units, _args.Units), + initializer: _args.RecurrentInitializer ); - if (args.UseBias) + if (_args.UseBias) { - bias = add_weight("bias", (args.Units), - initializer: args.BiasInitializer + _bias = add_weight("bias", (_args.Units), + initializer: _args.BiasInitializer ); } built = true; } - protected override Tensors Call(Tensors inputs, Tensor state = null, bool? training = null) + // TODO(Rinne): revise the trining param (with refactoring of the framework) + protected override Tensors Call(Tensors inputs, Tensors states = null, bool? training = null, IOptionalArgs? optional_args = null) { - return base.Call(inputs, state, training); + // TODO(Rinne): check if it will have multiple tensors when not nested. + Tensors prev_output = Nest.IsNested(states) ? new Tensors(states[0]) : states; + var dp_mask = get_dropout_mask_for_cell(inputs, training.Value); + var rec_dp_mask = get_recurrent_dropout_mask_for_cell(prev_output, training.Value); + + Tensor h; + var ranks = inputs.rank; + if (dp_mask != null) + { + + h = math_ops.matmul(math_ops.multiply(inputs.Single, dp_mask.Single), _kernel.AsTensor()); + } + else + { + h = math_ops.matmul(inputs, _kernel.AsTensor()); + } + + if (_bias != null) + { + h = tf.nn.bias_add(h, _bias); + } + + if (rec_dp_mask != null) + { + prev_output = math_ops.multiply(prev_output, rec_dp_mask); + } + Tensor output = h + math_ops.matmul(prev_output, _recurrent_kernel.AsTensor()); + + if (_args.Activation != null) + { + output = _args.Activation.Apply(output); + } + if (Nest.IsNested(states)) + { + return new Nest(new List> { + new Nest(new List> { new Nest(output) }), new Nest(output) }) + .ToTensors(); + } + else + { + return new Tensors(output, output); + } } } } diff --git a/src/TensorFlowNET.Keras/Layers/Rnn/StackedRNNCells.cs b/src/TensorFlowNET.Keras/Layers/Rnn/StackedRNNCells.cs index 20962df1f..ece2bc5bf 100644 --- a/src/TensorFlowNET.Keras/Layers/Rnn/StackedRNNCells.cs +++ b/src/TensorFlowNET.Keras/Layers/Rnn/StackedRNNCells.cs @@ -1,29 +1,27 @@ using System; -using System.Collections.Generic; using System.ComponentModel; +using System.Linq; +using Tensorflow.Common.Extensions; +using Tensorflow.Common.Types; using Tensorflow.Keras.ArgsDefinition; -using Tensorflow.Keras.ArgsDefinition.Rnn; using Tensorflow.Keras.Engine; using Tensorflow.Keras.Saving; +using Tensorflow.Keras.Utils; -namespace Tensorflow.Keras.Layers.Rnn +namespace Tensorflow.Keras.Layers { - public class StackedRNNCells : Layer, RNNArgs.IRnnArgCell + public class StackedRNNCells : Layer, IRnnCell { - public IList Cells { get; set; } - public bool reverse_state_order; + public IList Cells { get; set; } + public bool _reverse_state_order; - public StackedRNNCells(StackedRNNCellsArgs args) : base(args) + public StackedRNNCells(IEnumerable cells, StackedRNNCellsArgs args) : base(args) { - if (args.Kwargs == null) - { - args.Kwargs = new Dictionary(); - } + Cells = cells.ToList(); - Cells = args.Cells; - reverse_state_order = (bool)args.Kwargs.Get("reverse_state_order", false); + _reverse_state_order = args.ReverseStateOrder; - if (reverse_state_order) + if (_reverse_state_order) { throw new WarningException("reverse_state_order=True in StackedRNNCells will soon " + "be deprecated. Please update the code to work with the " + @@ -32,109 +30,104 @@ public StackedRNNCells(StackedRNNCellsArgs args) : base(args) } } - public object state_size + public bool SupportOptionalArgs => false; + + public INestStructure StateSize { - get => throw new NotImplementedException(); - //@property - //def state_size(self) : - // return tuple(c.state_size for c in - // (self.cells[::- 1] if self.reverse_state_order else self.cells)) + get + { + if (_reverse_state_order) + { + var state_sizes = Cells.Reverse().Select(cell => cell.StateSize); + return new Nest(state_sizes); + } + else + { + var state_sizes = Cells.Select(cell => cell.StateSize); + return new Nest(state_sizes); + } + } } - public object output_size + public INestStructure OutputSize { get { - var lastCell = Cells[Cells.Count - 1]; - - if (lastCell.output_size != -1) + var lastCell = Cells.Last(); + if(lastCell.OutputSize is not null) { - return lastCell.output_size; + return lastCell.OutputSize; } - else if (RNN._is_multiple_state(lastCell.state_size)) + else if (RnnUtils.is_multiple_state(lastCell.StateSize)) { - // return ((dynamic)Cells[-1].state_size)[0]; - throw new NotImplementedException(""); + return new NestNode(lastCell.StateSize.Flatten().First()); } else { - return Cells[-1].state_size; + return lastCell.StateSize; } } } - public object get_initial_state() + public Tensors GetInitialState(Tensors inputs = null, Tensor batch_size = null, TF_DataType dtype = TF_DataType.DtInvalid) { - throw new NotImplementedException(); - // def get_initial_state(self, inputs= None, batch_size= None, dtype= None) : - // initial_states = [] - // for cell in self.cells[::- 1] if self.reverse_state_order else self.cells: - // get_initial_state_fn = getattr(cell, 'get_initial_state', None) - // if get_initial_state_fn: - // initial_states.append(get_initial_state_fn( - // inputs=inputs, batch_size=batch_size, dtype=dtype)) - // else: - // initial_states.append(_generate_zero_filled_state_for_cell( - // cell, inputs, batch_size, dtype)) - - // return tuple(initial_states) + var cells = _reverse_state_order ? Cells.Reverse() : Cells; + List initial_states = new List(); + foreach (var cell in cells) + { + initial_states.Add(cell.GetInitialState(inputs, batch_size, dtype)); + } + return new Tensors(initial_states); } - public object call() + protected override Tensors Call(Tensors inputs, Tensors states = null, bool? training = null, IOptionalArgs? optional_args = null) { - throw new NotImplementedException(); - // def call(self, inputs, states, constants= None, training= None, ** kwargs): - // # Recover per-cell states. - // state_size = (self.state_size[::- 1] - // if self.reverse_state_order else self.state_size) - // nested_states = nest.pack_sequence_as(state_size, nest.flatten(states)) - - // # Call the cells in order and store the returned states. - // new_nested_states = [] - // for cell, states in zip(self.cells, nested_states) : - // states = states if nest.is_nested(states) else [states] - //# TF cell does not wrap the state into list when there is only one state. - // is_tf_rnn_cell = getattr(cell, '_is_tf_rnn_cell', None) is not None - // states = states[0] if len(states) == 1 and is_tf_rnn_cell else states - // if generic_utils.has_arg(cell.call, 'training'): - // kwargs['training'] = training - // else: - // kwargs.pop('training', None) - // # Use the __call__ function for callable objects, eg layers, so that it - // # will have the proper name scopes for the ops, etc. - // cell_call_fn = cell.__call__ if callable(cell) else cell.call - // if generic_utils.has_arg(cell.call, 'constants'): - // inputs, states = cell_call_fn(inputs, states, - // constants= constants, ** kwargs) - // else: - // inputs, states = cell_call_fn(inputs, states, ** kwargs) - // new_nested_states.append(states) - - // return inputs, nest.pack_sequence_as(state_size, - // nest.flatten(new_nested_states)) + // Recover per-cell states. + var state_size = _reverse_state_order ? new NestList(StateSize.Flatten().Reverse()) : StateSize; + var nested_states = Nest.PackSequenceAs(state_size, Nest.Flatten(states).ToArray()); + + var new_nest_states = Nest.Empty; + // Call the cells in order and store the returned states. + foreach (var (cell, internal_states) in zip(Cells, nested_states)) + { + RnnOptionalArgs? rnn_optional_args = optional_args as RnnOptionalArgs; + Tensors? constants = rnn_optional_args?.Constants; + + Tensors new_states; + (inputs, new_states) = cell.Apply(inputs, internal_states, optional_args: new RnnOptionalArgs() { Constants = constants }); + + new_nest_states = new_nest_states.MergeWith(new_states); + } + return Tensors.FromNest((inputs, Nest.PackSequenceAs(state_size, Nest.Flatten(new_nest_states).ToArray()))); } - public void build() + public override void build(KerasShapesWrapper input_shape) { - throw new NotImplementedException(); - // @tf_utils.shape_type_conversion - // def build(self, input_shape) : - // if isinstance(input_shape, list) : - // input_shape = input_shape[0] - // for cell in self.cells: - // if isinstance(cell, Layer) and not cell.built: - // with K.name_scope(cell.name): - // cell.build(input_shape) - // cell.built = True - // if getattr(cell, 'output_size', None) is not None: - // output_dim = cell.output_size - // elif _is_multiple_state(cell.state_size) : - // output_dim = cell.state_size[0] - // else: - // output_dim = cell.state_size - // input_shape = tuple([input_shape[0]] + - // tensor_shape.TensorShape(output_dim).as_list()) - // self.built = True + var shape = input_shape.ToSingleShape(); + foreach(var cell in Cells) + { + if(cell is Layer layer && !layer.Built) + { + // ignored the name scope. + layer.build(shape); + layer.Built = true; + } + INestStructure output_dim; + if(cell.OutputSize is not null) + { + output_dim = cell.OutputSize; + } + else if (RnnUtils.is_multiple_state(cell.StateSize)) + { + output_dim = new NestNode(cell.StateSize.Flatten().First()); + } + else + { + output_dim = cell.StateSize; + } + shape = new Shape(new long[] { shape.dims[0] }.Concat(output_dim.Flatten()).ToArray()); + } + this.Built = true; } public override IKerasConfig get_config() diff --git a/src/TensorFlowNET.Keras/Layers/TensorFlowOpLayer.cs b/src/TensorFlowNET.Keras/Layers/TensorFlowOpLayer.cs index 1ac4a277c..6dfec3196 100644 --- a/src/TensorFlowNET.Keras/Layers/TensorFlowOpLayer.cs +++ b/src/TensorFlowNET.Keras/Layers/TensorFlowOpLayer.cs @@ -10,6 +10,7 @@ using static Tensorflow.Binding; using Tensorflow.Functions; using System.Threading; +using Tensorflow.Common.Types; namespace Tensorflow.Keras.Layers { @@ -34,7 +35,7 @@ public TensorFlowOpLayer(TensorFlowOpLayerArgs args) built = true; } - protected override Tensors Call(Tensors inputs, Tensor state = null, bool? training = null) + protected override Tensors Call(Tensors inputs, Tensors state = null, bool? training = null, IOptionalArgs? optional_args = null) { if (tf.Context.executing_eagerly()) return DeFunCall(inputs); diff --git a/src/TensorFlowNET.Keras/Losses/BinaryCrossentropy.cs b/src/TensorFlowNET.Keras/Losses/BinaryCrossentropy.cs index ff7bb6b70..0de50a7ec 100644 --- a/src/TensorFlowNET.Keras/Losses/BinaryCrossentropy.cs +++ b/src/TensorFlowNET.Keras/Losses/BinaryCrossentropy.cs @@ -1,8 +1,9 @@ namespace Tensorflow.Keras.Losses; -public class BinaryCrossentropy : LossFunctionWrapper, ILossFunc +public class BinaryCrossentropy : LossFunctionWrapper { float label_smoothing; + public BinaryCrossentropy( bool from_logits = false, float label_smoothing = 0, @@ -15,7 +16,6 @@ public BinaryCrossentropy( this.label_smoothing = label_smoothing; } - public override Tensor Apply(Tensor y_true, Tensor y_pred, bool from_logits = false, int axis = -1) { var sum = keras.backend.binary_crossentropy(y_true, y_pred, from_logits: from_logits); diff --git a/src/TensorFlowNET.Keras/Losses/CategoricalCrossentropy.cs b/src/TensorFlowNET.Keras/Losses/CategoricalCrossentropy.cs index feb052244..1af57b552 100644 --- a/src/TensorFlowNET.Keras/Losses/CategoricalCrossentropy.cs +++ b/src/TensorFlowNET.Keras/Losses/CategoricalCrossentropy.cs @@ -1,8 +1,9 @@ namespace Tensorflow.Keras.Losses; -public class CategoricalCrossentropy : LossFunctionWrapper, ILossFunc +public class CategoricalCrossentropy : LossFunctionWrapper { float label_smoothing; + public CategoricalCrossentropy( bool from_logits = false, float label_smoothing = 0, @@ -15,7 +16,6 @@ public CategoricalCrossentropy( this.label_smoothing = label_smoothing; } - public override Tensor Apply(Tensor y_true, Tensor y_pred, bool from_logits = false, int axis = -1) { // Try to adjust the shape so that rank of labels = rank of logits - 1. diff --git a/src/TensorFlowNET.Keras/Losses/CosineSimilarity.cs b/src/TensorFlowNET.Keras/Losses/CosineSimilarity.cs index 16ab4b799..cf9df8d0d 100644 --- a/src/TensorFlowNET.Keras/Losses/CosineSimilarity.cs +++ b/src/TensorFlowNET.Keras/Losses/CosineSimilarity.cs @@ -1,28 +1,22 @@ -using System; -using System.Collections.Generic; -using System.Text; -using static Tensorflow.Binding; -using static Tensorflow.KerasApi; +namespace Tensorflow.Keras.Losses; -namespace Tensorflow.Keras.Losses +public class CosineSimilarity : LossFunctionWrapper { - public class CosineSimilarity : LossFunctionWrapper, ILossFunc + protected int axis = -1; + + public CosineSimilarity( + string reduction = null, + int axis = -1, + string name = null) : + base(reduction: reduction, name: name == null ? "cosine_similarity" : name) { - protected int axis=-1; - public CosineSimilarity( - string reduction = null, - int axis=-1, - string name = null) : - base(reduction: reduction, name: name == null ? "cosine_similarity" : name) - { - this.axis = axis; - } + this.axis = axis; + } - public override Tensor Apply(Tensor y_true = null, Tensor y_pred =null, bool from_logits = false, int axis = -1) - { - Tensor y_true_normalize = nn_impl.l2_normalize(y_true, axis : this.axis); - Tensor y_pred_normalize = nn_impl.l2_normalize(y_pred, axis: this.axis); - return -math_ops.reduce_sum(y_true_normalize * y_pred_normalize, axis : constant_op.constant(this.axis)); - } + public override Tensor Apply(Tensor y_true = null, Tensor y_pred = null, bool from_logits = false, int axis = -1) + { + Tensor y_true_normalize = nn_impl.l2_normalize(y_true, axis: this.axis); + Tensor y_pred_normalize = nn_impl.l2_normalize(y_pred, axis: this.axis); + return -math_ops.reduce_sum(y_true_normalize * y_pred_normalize, axis: constant_op.constant(this.axis)); } -} +} \ No newline at end of file diff --git a/src/TensorFlowNET.Keras/Losses/Huber.cs b/src/TensorFlowNET.Keras/Losses/Huber.cs index a256786f1..61f006d2b 100644 --- a/src/TensorFlowNET.Keras/Losses/Huber.cs +++ b/src/TensorFlowNET.Keras/Losses/Huber.cs @@ -1,36 +1,29 @@ -using System; -using System.Collections.Generic; -using System.Text; -using static Tensorflow.Binding; -using static Tensorflow.KerasApi; +namespace Tensorflow.Keras.Losses; -namespace Tensorflow.Keras.Losses +public class Huber : LossFunctionWrapper { - public class Huber : LossFunctionWrapper, ILossFunc + protected Tensor delta = tf.Variable(1.0); + + public Huber( + string reduction = null, + Tensor delta = null, + string name = null) : + base(reduction: reduction, name: name == null ? "huber" : name) { - protected Tensor delta = tf.Variable(1.0) ; - public Huber ( - string reduction = null, - Tensor delta = null, - string name = null) : - base(reduction: reduction, name: name == null ? "huber" : name) - { - this.delta = delta==null? this.delta: delta; - - } + this.delta = delta == null ? this.delta : delta; + } - public override Tensor Apply(Tensor y_true = null, Tensor y_pred =null, bool from_logits = false, int axis = -1) - { - Tensor y_pred_cast = math_ops.cast(y_pred, dtype: TF_DataType.TF_FLOAT); - Tensor y_true_cast = math_ops.cast(y_true, dtype: TF_DataType.TF_FLOAT); - Tensor delta = math_ops.cast(this.delta, dtype: TF_DataType.TF_FLOAT); - Tensor error = math_ops.subtract(y_pred_cast, y_true_cast); - Tensor abs_error = math_ops.abs(error); - Tensor half = ops.convert_to_tensor(0.5, dtype: abs_error.dtype); - return gen_math_ops.mean(array_ops.where_v2(abs_error <= delta, - half * math_ops.pow(error, 2), - half * math_ops.pow(delta, 2) + delta * (abs_error - delta)), - axis: -1); - } + public override Tensor Apply(Tensor y_true = null, Tensor y_pred = null, bool from_logits = false, int axis = -1) + { + Tensor y_pred_cast = math_ops.cast(y_pred, dtype: TF_DataType.TF_FLOAT); + Tensor y_true_cast = math_ops.cast(y_true, dtype: TF_DataType.TF_FLOAT); + Tensor delta = math_ops.cast(this.delta, dtype: TF_DataType.TF_FLOAT); + Tensor error = math_ops.subtract(y_pred_cast, y_true_cast); + Tensor abs_error = math_ops.abs(error); + Tensor half = ops.convert_to_tensor(0.5, dtype: abs_error.dtype); + return gen_math_ops.mean(array_ops.where_v2(abs_error <= delta, + half * math_ops.pow(error, 2), + half * math_ops.pow(delta, 2) + delta * (abs_error - delta)), + ops.convert_to_tensor(-1)); } } diff --git a/src/TensorFlowNET.Keras/Losses/LogCosh.cs b/src/TensorFlowNET.Keras/Losses/LogCosh.cs index 8acbbe9d2..0c7a9b6e2 100644 --- a/src/TensorFlowNET.Keras/Losses/LogCosh.cs +++ b/src/TensorFlowNET.Keras/Losses/LogCosh.cs @@ -1,26 +1,20 @@ -using System; -using System.Collections.Generic; -using System.Text; -using Tensorflow.Operations; -using static Tensorflow.Binding; -using static Tensorflow.KerasApi; +namespace Tensorflow.Keras.Losses; -namespace Tensorflow.Keras.Losses +public class LogCosh : LossFunctionWrapper { - public class LogCosh : LossFunctionWrapper, ILossFunc - { - public LogCosh( - string reduction = null, - string name = null) : - base(reduction: reduction, name: name == null ? "log_cosh" : name){ } + public LogCosh( + string reduction = null, + string name = null) : + base(reduction: reduction, name: name == null ? "log_cosh" : name) + { } - public override Tensor Apply(Tensor y_true = null, Tensor y_pred =null, bool from_logits = false, int axis = -1) - { - Tensor y_pred_dispatch = ops.convert_to_tensor(y_pred); - Tensor y_true_cast = gen_math_ops.cast(y_true, y_pred_dispatch.dtype); - Tensor x = y_pred_dispatch - y_true_cast; + public override Tensor Apply(Tensor y_true = null, Tensor y_pred = null, bool from_logits = false, int axis = -1) + { + Tensor y_pred_dispatch = ops.convert_to_tensor(y_pred); + Tensor y_true_cast = gen_math_ops.cast(y_true, y_pred_dispatch.dtype); + Tensor x = y_pred_dispatch - y_true_cast; - return gen_math_ops.mean(x + gen_math_ops.softplus(-2.0 * x) - math_ops.cast(math_ops.log(tf.Variable(2.0)), x.dtype), axis: -1); - } + return gen_math_ops.mean(x + gen_nn_ops.softplus(-2.0 * x) - math_ops.cast(math_ops.log(tf.Variable(2.0)), x.dtype), + ops.convert_to_tensor(-1)); } -} +} \ No newline at end of file diff --git a/src/TensorFlowNET.Keras/Losses/Loss.cs b/src/TensorFlowNET.Keras/Losses/Loss.cs index 77bf7e1dc..ce77f6d63 100644 --- a/src/TensorFlowNET.Keras/Losses/Loss.cs +++ b/src/TensorFlowNET.Keras/Losses/Loss.cs @@ -1,55 +1,51 @@ -using System; -using Tensorflow.Keras.Utils; +using Tensorflow.Keras.Utils; -namespace Tensorflow.Keras.Losses +namespace Tensorflow.Keras.Losses; + +/// +/// Loss base class. +/// +public abstract class Loss : ILossFunc { - /// - /// Loss base class. - /// - public abstract class Loss + protected string reduction; + protected string name; + bool _allow_sum_over_batch_size; + protected bool from_logits = false; + string _name_scope; + + public string Reduction => reduction; + public string Name => name; + + public Loss(string reduction = ReductionV2.AUTO, + string name = null, + bool from_logits = false) { - protected string reduction; - protected string name; - bool _allow_sum_over_batch_size; - protected bool from_logits = false; - string _name_scope; - - public string Reduction => reduction; - public string Name => name; - public Loss(string reduction = ReductionV2.AUTO, - string name = null, - bool from_logits = false) - { - this.reduction = reduction == null ? ReductionV2.SUM_OVER_BATCH_SIZE : reduction; - this.name = name; - this.from_logits = from_logits; - _allow_sum_over_batch_size = false; - } + this.reduction = reduction == null ? ReductionV2.SUM_OVER_BATCH_SIZE : reduction; + this.name = name; + this.from_logits = from_logits; + _allow_sum_over_batch_size = false; + } - public virtual Tensor Apply(Tensor y_true, Tensor y_pred, bool from_logits = false, int axis = -1) - { - throw new NotImplementedException(""); - } + public abstract Tensor Apply(Tensor y_true, Tensor y_pred, bool from_logits = false, int axis = -1); - public Tensor Call(Tensor y_true, Tensor y_pred, Tensor sample_weight = null) - { - var losses = Apply(y_true, y_pred, from_logits: from_logits); - var reduction = GetReduction(); - return losses_utils.compute_weighted_loss(losses, reduction: reduction, sample_weight: sample_weight); - } + public Tensor Call(Tensor y_true, Tensor y_pred, Tensor sample_weight = null) + { + var losses = Apply(y_true, y_pred, from_logits: from_logits); + var reduction = GetReduction(); + return losses_utils.compute_weighted_loss(losses, reduction: reduction, sample_weight: sample_weight); + } - string GetReduction() - { - return reduction switch - { - ReductionV2.AUTO => ReductionV2.SUM_OVER_BATCH_SIZE, - _ => reduction - }; - } - - void _set_name_scope() + string GetReduction() + { + return reduction switch { - _name_scope = name; - } + ReductionV2.AUTO => ReductionV2.SUM_OVER_BATCH_SIZE, + _ => reduction + }; + } + + void _set_name_scope() + { + _name_scope = name; } -} +} \ No newline at end of file diff --git a/src/TensorFlowNET.Keras/Losses/LossFunctionWrapper.cs b/src/TensorFlowNET.Keras/Losses/LossFunctionWrapper.cs index 758b46f4b..f4ee2b346 100644 --- a/src/TensorFlowNET.Keras/Losses/LossFunctionWrapper.cs +++ b/src/TensorFlowNET.Keras/Losses/LossFunctionWrapper.cs @@ -1,16 +1,14 @@ using Tensorflow.Keras.Utils; -namespace Tensorflow.Keras.Losses +namespace Tensorflow.Keras.Losses; + +public abstract class LossFunctionWrapper : Loss { - public class LossFunctionWrapper : Loss - { - public LossFunctionWrapper(string reduction = ReductionV2.AUTO, - string name = null, - bool from_logits = false) - : base(reduction: reduction, - name: name, - from_logits: from_logits) - { - } - } + public LossFunctionWrapper(string reduction = ReductionV2.AUTO, + string name = null, + bool from_logits = false) + : base(reduction: reduction, + name: name, + from_logits: from_logits) + { } } diff --git a/src/TensorFlowNET.Keras/Losses/MeanAbsoluteError.cs b/src/TensorFlowNET.Keras/Losses/MeanAbsoluteError.cs index 5d0f83d43..19476a68a 100644 --- a/src/TensorFlowNET.Keras/Losses/MeanAbsoluteError.cs +++ b/src/TensorFlowNET.Keras/Losses/MeanAbsoluteError.cs @@ -1,23 +1,16 @@ -using System; -using System.Collections.Generic; -using System.Text; -using static Tensorflow.Binding; -using static Tensorflow.KerasApi; +namespace Tensorflow.Keras.Losses; -namespace Tensorflow.Keras.Losses +public class MeanAbsoluteError : LossFunctionWrapper { - public class MeanAbsoluteError : LossFunctionWrapper, ILossFunc - { - public MeanAbsoluteError( - string reduction = null, - string name = null) : - base(reduction: reduction, name: name == null ? "mean_absolute_error" : name){ } + public MeanAbsoluteError( + string reduction = null, + string name = null) : + base(reduction: reduction, name: name == null ? "mean_absolute_error" : name){ } - public override Tensor Apply(Tensor y_true = null, Tensor y_pred =null, bool from_logits = false, int axis = -1) - { - Tensor y_pred_dispatch = ops.convert_to_tensor(y_pred); - Tensor y_true_cast = gen_math_ops.cast(y_true, y_pred_dispatch.dtype); - return gen_math_ops.mean(math_ops.abs(y_pred_dispatch - y_true_cast), axis: -1); - } + public override Tensor Apply(Tensor y_true = null, Tensor y_pred =null, bool from_logits = false, int axis = -1) + { + Tensor y_pred_dispatch = ops.convert_to_tensor(y_pred); + Tensor y_true_cast = gen_math_ops.cast(y_true, y_pred_dispatch.dtype); + return gen_math_ops.mean(math_ops.abs(y_pred_dispatch - y_true_cast), ops.convert_to_tensor(-1)); } } diff --git a/src/TensorFlowNET.Keras/Losses/MeanAbsolutePercentageError.cs b/src/TensorFlowNET.Keras/Losses/MeanAbsolutePercentageError.cs index 3295b12b1..226c4237a 100644 --- a/src/TensorFlowNET.Keras/Losses/MeanAbsolutePercentageError.cs +++ b/src/TensorFlowNET.Keras/Losses/MeanAbsolutePercentageError.cs @@ -1,24 +1,17 @@ -using System; -using System.Collections.Generic; -using System.Text; -using static Tensorflow.Binding; -using static Tensorflow.KerasApi; +namespace Tensorflow.Keras.Losses; -namespace Tensorflow.Keras.Losses +public class MeanAbsolutePercentageError : LossFunctionWrapper { - public class MeanAbsolutePercentageError : LossFunctionWrapper, ILossFunc - { - public MeanAbsolutePercentageError( - string reduction = null, - string name = null) : - base(reduction: reduction, name: name == null ? "mean_absolute_percentage_error" : name){ } + public MeanAbsolutePercentageError( + string reduction = null, + string name = null) : + base(reduction: reduction, name: name == null ? "mean_absolute_percentage_error" : name){ } - public override Tensor Apply(Tensor y_true = null, Tensor y_pred =null, bool from_logits = false, int axis = -1) - { - Tensor y_pred_dispatch = ops.convert_to_tensor(y_pred); - Tensor y_true_cast = gen_math_ops.cast(y_true, y_pred_dispatch.dtype); - Tensor diff = math_ops.abs(y_true_cast - y_pred_dispatch) / gen_math_ops.maximum(math_ops.abs(y_true_cast), gen_math_ops.cast(tf.constant(1e-7), y_pred_dispatch.dtype)); - return gen_math_ops.cast(tf.constant(100), y_pred_dispatch.dtype) * gen_math_ops.mean(diff, axis: -1); - } + public override Tensor Apply(Tensor y_true = null, Tensor y_pred =null, bool from_logits = false, int axis = -1) + { + Tensor y_pred_dispatch = ops.convert_to_tensor(y_pred); + Tensor y_true_cast = gen_math_ops.cast(y_true, y_pred_dispatch.dtype); + Tensor diff = math_ops.abs(y_true_cast - y_pred_dispatch) / gen_math_ops.maximum(math_ops.abs(y_true_cast), gen_math_ops.cast(tf.constant(1e-7), y_pred_dispatch.dtype)); + return gen_math_ops.cast(tf.constant(100), y_pred_dispatch.dtype) * gen_math_ops.mean(diff, ops.convert_to_tensor(-1)); } } diff --git a/src/TensorFlowNET.Keras/Losses/MeanSquaredError.cs b/src/TensorFlowNET.Keras/Losses/MeanSquaredError.cs index 6ae7d86d4..a937c1963 100644 --- a/src/TensorFlowNET.Keras/Losses/MeanSquaredError.cs +++ b/src/TensorFlowNET.Keras/Losses/MeanSquaredError.cs @@ -1,23 +1,16 @@ -using System; -using System.Collections.Generic; -using System.Text; -using static Tensorflow.Binding; -using static Tensorflow.KerasApi; +namespace Tensorflow.Keras.Losses; -namespace Tensorflow.Keras.Losses +public class MeanSquaredError : LossFunctionWrapper { - public class MeanSquaredError : LossFunctionWrapper, ILossFunc - { - public MeanSquaredError( - string reduction = null, - string name = null) : - base(reduction: reduction, name: name==null? "mean_squared_error" : name){ } + public MeanSquaredError( + string reduction = null, + string name = null) : + base(reduction: reduction, name: name==null? "mean_squared_error" : name){ } - public override Tensor Apply(Tensor y_true = null, Tensor y_pred =null, bool from_logits = false, int axis = -1) - { - Tensor y_pred_dispatch = ops.convert_to_tensor(y_pred); - Tensor y_true_cast = gen_math_ops.cast(y_true, y_pred_dispatch.dtype); - return gen_math_ops.mean(gen_math_ops.squared_difference(y_pred_dispatch, y_true_cast), axis: -1); - } + public override Tensor Apply(Tensor y_true = null, Tensor y_pred =null, bool from_logits = false, int axis = -1) + { + Tensor y_pred_dispatch = ops.convert_to_tensor(y_pred); + Tensor y_true_cast = gen_math_ops.cast(y_true, y_pred_dispatch.dtype); + return gen_math_ops.mean(gen_math_ops.squared_difference(y_pred_dispatch, y_true_cast), ops.convert_to_tensor(-1)); } } diff --git a/src/TensorFlowNET.Keras/Losses/MeanSquaredLogarithmicError.cs b/src/TensorFlowNET.Keras/Losses/MeanSquaredLogarithmicError.cs index 22b5a6ff9..0a4e7d3c5 100644 --- a/src/TensorFlowNET.Keras/Losses/MeanSquaredLogarithmicError.cs +++ b/src/TensorFlowNET.Keras/Losses/MeanSquaredLogarithmicError.cs @@ -1,33 +1,28 @@ -using System; -using System.Collections.Generic; -using System.Text; -using static Tensorflow.Binding; -using static Tensorflow.KerasApi; +namespace Tensorflow.Keras.Losses; -namespace Tensorflow.Keras.Losses +public class MeanSquaredLogarithmicError : LossFunctionWrapper { - public class MeanSquaredLogarithmicError : LossFunctionWrapper, ILossFunc - { - public MeanSquaredLogarithmicError( - string reduction = null, - string name = null) : - base(reduction: reduction, name: name == null ? "mean_squared_logarithmic_error" : name){ } - + public MeanSquaredLogarithmicError( + string reduction = null, + string name = null) : + base(reduction: reduction, name: name == null ? "mean_squared_logarithmic_error" : name) + { } - public override Tensor Apply(Tensor y_true = null, Tensor y_pred =null, bool from_logits = false, int axis = -1) + public override Tensor Apply(Tensor y_true = null, Tensor y_pred = null, bool from_logits = false, int axis = -1) + { + Tensor y_pred_dispatch = ops.convert_to_tensor(y_pred); + Tensor y_true_cast = gen_math_ops.cast(y_true, y_pred_dispatch.dtype); + Tensor first_log = null, second_log = null; + if (y_pred_dispatch.dtype == TF_DataType.TF_DOUBLE) + { + first_log = math_ops.log(math_ops.maximum(y_pred_dispatch, 1e-7) + 1.0); + second_log = math_ops.log(math_ops.maximum(y_true_cast, 1e-7) + 1.0); + } + else { - Tensor y_pred_dispatch = ops.convert_to_tensor(y_pred); - Tensor y_true_cast = gen_math_ops.cast(y_true, y_pred_dispatch.dtype); - Tensor first_log=null, second_log=null; - if (y_pred_dispatch.dtype == TF_DataType.TF_DOUBLE) { - first_log = math_ops.log(gen_math_ops.maximum(y_pred_dispatch, 1e-7) + 1.0); - second_log = math_ops.log(gen_math_ops.maximum(y_true_cast, 1e-7) + 1.0); - } - else { - first_log = math_ops.log(gen_math_ops.maximum(y_pred_dispatch, 1e-7f) + 1.0f); - second_log = math_ops.log(gen_math_ops.maximum(y_true_cast, 1e-7f) + 1.0f); - } - return gen_math_ops.mean(gen_math_ops.squared_difference(first_log, second_log), axis: -1); + first_log = math_ops.log(math_ops.maximum(y_pred_dispatch, 1e-7f) + 1.0f); + second_log = math_ops.log(math_ops.maximum(y_true_cast, 1e-7f) + 1.0f); } + return gen_math_ops.mean(gen_math_ops.squared_difference(first_log, second_log), ops.convert_to_tensor(-1)); } -} +} \ No newline at end of file diff --git a/src/TensorFlowNET.Keras/Losses/SigmoidFocalCrossEntropy.cs b/src/TensorFlowNET.Keras/Losses/SigmoidFocalCrossEntropy.cs index 7ac3fa0bb..ec6dcedf8 100644 --- a/src/TensorFlowNET.Keras/Losses/SigmoidFocalCrossEntropy.cs +++ b/src/TensorFlowNET.Keras/Losses/SigmoidFocalCrossEntropy.cs @@ -2,7 +2,7 @@ namespace Tensorflow.Keras.Losses; -public class SigmoidFocalCrossEntropy : LossFunctionWrapper, ILossFunc +public class SigmoidFocalCrossEntropy : LossFunctionWrapper { float _alpha; float _gamma; @@ -20,7 +20,6 @@ public SigmoidFocalCrossEntropy(bool from_logits = false, _gamma = gamma; } - public override Tensor Apply(Tensor y_true, Tensor y_pred, bool from_logits = false, int axis = -1) { y_true = tf.cast(y_true, dtype: y_pred.dtype); diff --git a/src/TensorFlowNET.Keras/Losses/SparseCategoricalCrossentropy.cs b/src/TensorFlowNET.Keras/Losses/SparseCategoricalCrossentropy.cs index 4e2790ab1..17ce2d30b 100644 --- a/src/TensorFlowNET.Keras/Losses/SparseCategoricalCrossentropy.cs +++ b/src/TensorFlowNET.Keras/Losses/SparseCategoricalCrossentropy.cs @@ -1,41 +1,41 @@ using static Tensorflow.Binding; -namespace Tensorflow.Keras.Losses +namespace Tensorflow.Keras.Losses; + +public class SparseCategoricalCrossentropy : LossFunctionWrapper { - public class SparseCategoricalCrossentropy : LossFunctionWrapper, ILossFunc + private bool _from_logits = false; + + public SparseCategoricalCrossentropy( + bool from_logits = false, + string reduction = null, + string name = null) : + base(reduction: reduction, name: name == null ? "sparse_categorical_crossentropy" : name) + { + _from_logits = from_logits; + } + + public override Tensor Apply(Tensor target, Tensor output, bool from_logits = false, int axis = -1) { - private bool _from_logits = false; - public SparseCategoricalCrossentropy( - bool from_logits = false, - string reduction = null, - string name = null) : - base(reduction: reduction, name: name == null ? "sparse_categorical_crossentropy" : name) + target = tf.cast(target, dtype: TF_DataType.TF_INT64); + + if (!_from_logits) { - _from_logits = from_logits; + var epsilon = tf.constant(KerasApi.keras.backend.epsilon(), output.dtype); + output = tf.clip_by_value(output, epsilon, 1 - epsilon); + output = tf.log(output); } - public override Tensor Apply(Tensor target, Tensor output, bool from_logits = false, int axis = -1) + // Try to adjust the shape so that rank of labels = rank of logits - 1. + var output_shape = array_ops.shape_v2(output); + var output_rank = output.shape.ndim; + var target_rank = target.shape.ndim; + var update_shape = target_rank != output_rank - 1; + if (update_shape) { - target = tf.cast(target, dtype: TF_DataType.TF_INT64); - - if (!_from_logits) - { - var epsilon = tf.constant(KerasApi.keras.backend.epsilon(), output.dtype); - output = tf.clip_by_value(output, epsilon, 1 - epsilon); - output = tf.log(output); - } - - // Try to adjust the shape so that rank of labels = rank of logits - 1. - var output_shape = array_ops.shape_v2(output); - var output_rank = output.shape.ndim; - var target_rank = target.shape.ndim; - var update_shape = target_rank != output_rank - 1; - if (update_shape) - { - target = array_ops.reshape(target, new int[] { -1 }); - output = array_ops.reshape(output, new int[] { -1, output_shape[-1].numpy() }); - } - return tf.nn.sparse_softmax_cross_entropy_with_logits(target, output); + target = array_ops.reshape(target, new int[] { -1 }); + output = array_ops.reshape(output, new int[] { -1, output_shape[-1].numpy() }); } + return tf.nn.sparse_softmax_cross_entropy_with_logits(target, output); } -} +} \ No newline at end of file diff --git a/src/TensorFlowNET.Keras/Metrics/metrics_utils.cs b/src/TensorFlowNET.Keras/Metrics/metrics_utils.cs index be6a49ec5..3c2f8a7be 100644 --- a/src/TensorFlowNET.Keras/Metrics/metrics_utils.cs +++ b/src/TensorFlowNET.Keras/Metrics/metrics_utils.cs @@ -304,7 +304,7 @@ private static Tensor _filter_top_k(Tensor x, int k) var NEG_INF = -1e10; var (_, top_k_idx) = tf.math.top_k(x, k, sorted: false); var top_k_mask = tf.reduce_sum( - tf.one_hot(top_k_idx, (int)x.shape[-1], axis: -1), axis: -2); + tf.one_hot(top_k_idx.Single, (int)x.shape[-1], axis: -1), axis: -2); return x * top_k_mask + NEG_INF * (1 - top_k_mask); } } diff --git a/src/TensorFlowNET.Keras/Models/ModelsApi.cs b/src/TensorFlowNET.Keras/Models/ModelsApi.cs index 44dca58d0..2605c41e3 100644 --- a/src/TensorFlowNET.Keras/Models/ModelsApi.cs +++ b/src/TensorFlowNET.Keras/Models/ModelsApi.cs @@ -1,22 +1,15 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Text; -using Tensorflow.Keras.Engine; -using Tensorflow.Keras.Saving; +using Tensorflow.Keras.Saving; using Tensorflow.Keras.Saving.SavedModel; -using ThirdParty.Tensorflow.Python.Keras.Protobuf; -namespace Tensorflow.Keras.Models +namespace Tensorflow.Keras.Models; + +public class ModelsApi: IModelsApi { - public class ModelsApi: IModelsApi - { - public Functional from_config(FunctionalConfig config) - => Functional.from_config(config); + public Functional from_config(FunctionalConfig config) + => Functional.from_config(config); - public IModel load_model(string filepath, bool compile = true, LoadOptions? options = null) - { - return KerasLoadModelUtils.load_model(filepath, compile: compile, options: options) as Model; - } + public IModel load_model(string filepath, bool compile = true, LoadOptions? options = null) + { + return KerasLoadModelUtils.load_model(filepath, compile: compile, options: options) as Model; } } diff --git a/src/TensorFlowNET.Keras/Optimizers/AdamW.cs b/src/TensorFlowNET.Keras/Optimizers/AdamW.cs new file mode 100644 index 000000000..d111b5d3a --- /dev/null +++ b/src/TensorFlowNET.Keras/Optimizers/AdamW.cs @@ -0,0 +1,64 @@ +namespace Tensorflow.Keras.Optimizers +{ + public class AdamW : Adam + { + string name; + float weight_decay; + DeviceDType deType; + List no_decay_params = null; + public AdamW(float learning_rate= 0.001f, + float weight_decay= 0.004f, + float beta_1= 0.9f, + float beta_2= 0.999f, + float epsilon= 1e-7f, + bool amsgrad = false, + List no_decay_params = null, + string name= "AdamW") : base(learning_rate, beta_1, beta_2, epsilon, amsgrad) + { + this.name = name; + this.weight_decay = weight_decay; + this.no_decay_params = no_decay_params; + } + + protected Operation _decay_weights_op(IVariableV1 var, float learning_rate, Dictionary> apply_state) + { + bool do_decay = _do_use_weight_decay(var.Name); + if (do_decay) return var.assign_add( + -learning_rate * var.AsTensor() * apply_state[deType]["weight_decay"]); + return tf.no_op(); + } + + + protected bool _do_use_weight_decay(string param_name) + { + // Whether to use L2 weight decay for `param_name`. + if (this.weight_decay == 0) + return false; + + if (this.no_decay_params != null) + { + foreach (var name in no_decay_params) + { + if (param_name.Contains(name)) return false; + } + + } + return true; + } + + protected override Operation _resource_apply_dense(IVariableV1 var, Tensor grad, Dictionary> apply_state) + { + var decay = _decay_weights_op(var, _hyper["learning_rate"], apply_state); + tf.control_dependencies(new[] { decay }); + return base._resource_apply_dense(var, grad, apply_state); + } + + protected override void _prepare_local(DeviceDType device_dtype, Dictionary> apply_state) + { + this.deType = device_dtype; + base._prepare_local(device_dtype, apply_state); + apply_state[device_dtype]["weight_decay"] = tf.constant( + weight_decay, name: "adam_weight_decay_rate"); + } + } +} diff --git a/src/TensorFlowNET.Keras/Optimizers/OptimizerApi.cs b/src/TensorFlowNET.Keras/Optimizers/OptimizerApi.cs index 31eb88be7..a237499f9 100644 --- a/src/TensorFlowNET.Keras/Optimizers/OptimizerApi.cs +++ b/src/TensorFlowNET.Keras/Optimizers/OptimizerApi.cs @@ -29,6 +29,22 @@ public IOptimizer Adam(float learning_rate = 0.001f, amsgrad: amsgrad, name: name); + public IOptimizer AdamW(float learning_rate = 0.001f, + float weight_decay = 0.004f, + float beta_1 = 0.9f, + float beta_2 = 0.999f, + float epsilon = 1e-7f, + bool amsgrad = false, + List no_decay_params = null, + string name = "AdamW") => new AdamW(learning_rate: learning_rate, + beta_1: beta_1, + beta_2: beta_2, + epsilon: epsilon, + amsgrad: amsgrad, + name: name, + weight_decay: weight_decay, + no_decay_params: no_decay_params); + /// /// Construct a new RMSprop optimizer. /// @@ -55,7 +71,7 @@ public IOptimizer RMSprop(float learning_rate = 0.001f, Name = name }); - public IOptimizer SGD(float learning_rate) - => new SGD(learning_rate); + public IOptimizer SGD(float learning_rate = 0.01f, float momentum = 0f) + => new SGD(learning_rate, momentum); } } diff --git a/src/TensorFlowNET.Keras/Optimizers/SGD.cs b/src/TensorFlowNET.Keras/Optimizers/SGD.cs index f97f4b15f..1d9ceb810 100644 --- a/src/TensorFlowNET.Keras/Optimizers/SGD.cs +++ b/src/TensorFlowNET.Keras/Optimizers/SGD.cs @@ -22,6 +22,8 @@ public SGD(float learning_rate, _set_hyper("decay", decay); _momentum = momentum > 0; + if (momentum < 0 || momentum > 1) + throw new ValueError($"momentum must be a number between 0 and 1, got {momentum}."); _set_hyper("momentum", momentum); @@ -30,6 +32,13 @@ public SGD(float learning_rate, #pragma warning restore CS1717 // Assignment made to same variable } + protected override void _create_slots(IVariableV1[] var_list) + { + if (_momentum) + foreach (var var in var_list) + add_slot(var, "momentum"); + } + protected override void _prepare_local(DeviceDType device_dtype, Dictionary> _apply_state) { @@ -43,7 +52,15 @@ protected override Operation _resource_apply_dense(IVariableV1 var, Tensor grad, { if (_momentum) { - throw new NotImplementedException("_resource_apply_dense"); + var momentum_var = get_slot(var, "momentum"); + return gen_training_ops.resource_apply_keras_momentum( + var.Handle, + momentum_var.Handle, + _get_hyper("learning_rate", var.dtype), + grad, + _get_hyper("momentum", var.dtype), + use_locking: _use_locking, + use_nesterov: nesterov); } var device_dtype = _apply_state.Keys.FirstOrDefault(x => x.Device == var.Device && x.DType == var.dtype.as_base_dtype()); diff --git a/src/TensorFlowNET.Keras/Preprocessings/DatasetUtils.get_training_or_validation_split.cs b/src/TensorFlowNET.Keras/Preprocessings/DatasetUtils.get_training_or_validation_split.cs index 2f3d8f527..18ca404ef 100644 --- a/src/TensorFlowNET.Keras/Preprocessings/DatasetUtils.get_training_or_validation_split.cs +++ b/src/TensorFlowNET.Keras/Preprocessings/DatasetUtils.get_training_or_validation_split.cs @@ -6,7 +6,7 @@ namespace Tensorflow.Keras.Preprocessings public partial class DatasetUtils { /// - /// Potentially restict samples & labels to a training or validation split. + /// Potentially restict samples and labels to a training or validation split. /// /// /// diff --git a/src/TensorFlowNET.Keras/Preprocessings/Preprocessing.image_dataset_from_directory.cs b/src/TensorFlowNET.Keras/Preprocessings/Preprocessing.image_dataset_from_directory.cs index fa19987b1..377ac4de7 100644 --- a/src/TensorFlowNET.Keras/Preprocessings/Preprocessing.image_dataset_from_directory.cs +++ b/src/TensorFlowNET.Keras/Preprocessings/Preprocessing.image_dataset_from_directory.cs @@ -8,6 +8,37 @@ public partial class Preprocessing { public static string[] WHITELIST_FORMATS = new[] { ".bmp", ".gif", ".jpeg", ".jpg", ".png" }; + /// + /// Function that calculates the classification statistics for a given array of classified data. + /// The function takes an array of classified data as input and returns a dictionary containing the count and percentage of each class in the input array. + /// This function can be used to analyze the distribution of classes in a dataset or to evaluate the performance of a classification model. + /// + /// + /// code from copilot + /// + /// + /// + Dictionary get_classification_statistics(int[] label_ids, string[] label_class_names) + { + var countDict = label_ids.GroupBy(x => x) + .ToDictionary(g => g.Key, g => g.Count()); + var totalCount = label_ids.Length; + var ratioDict = label_class_names.ToDictionary(name => name, + name => + (double)(countDict.ContainsKey(Array.IndexOf(label_class_names, name)) + ? countDict[Array.IndexOf(label_class_names, name)] : 0) + / totalCount); + + print("Classification statistics:"); + foreach (string labelName in label_class_names) + { + double ratio = ratioDict[labelName]; + print($"{labelName}: {ratio * 100:F2}%"); + } + + return ratioDict; + } + /// /// Generates a `tf.data.Dataset` from image files in a directory. /// https://www.tensorflow.org/api_docs/python/tf/keras/preprocessing/image_dataset_from_directory @@ -53,11 +84,13 @@ public IDatasetV2 image_dataset_from_directory(string directory, follow_links: follow_links); (image_paths, label_list) = keras.preprocessing.dataset_utils.get_training_or_validation_split(image_paths, label_list, validation_split, subset); + get_classification_statistics(label_list, class_name_list); var dataset = paths_and_labels_to_dataset(image_paths, image_size, num_channels, label_list, label_mode, class_name_list.Length, interpolation); if (shuffle) dataset = dataset.shuffle(batch_size * 8, seed: seed); dataset = dataset.batch(batch_size); + dataset.class_names = class_name_list; return dataset; } @@ -129,7 +162,7 @@ public IDatasetV2 timeseries_dataset_from_array(Tensor data, int sequence_length var indices = z.map(m => { var (i, positions) = m; - return tf.range(positions[i], positions[i] + sequence_length_tensor * sampling_rate_tensor, sampling_rate_tensor); + return tf.range(positions.Single[i], positions.Single[i] + sequence_length_tensor * sampling_rate_tensor, sampling_rate_tensor); }, num_parallel_calls: -1); var dataset = sequences_from_indices(data, indices, start_index, end_index); diff --git a/src/TensorFlowNET.Keras/Preprocessings/Preprocessing.paths_and_labels_to_dataset.cs b/src/TensorFlowNET.Keras/Preprocessings/Preprocessing.paths_and_labels_to_dataset.cs index b4d583878..232f81eb5 100644 --- a/src/TensorFlowNET.Keras/Preprocessings/Preprocessing.paths_and_labels_to_dataset.cs +++ b/src/TensorFlowNET.Keras/Preprocessings/Preprocessing.paths_and_labels_to_dataset.cs @@ -6,6 +6,32 @@ namespace Tensorflow.Keras { public partial class Preprocessing { + + /// + /// 图片路径转为数据处理用的dataset + /// 通常用于预测时读取图片 + /// + /// + /// + /// + /// + /// 用于调整大小的插值方法。支持`bilinear`、`nearest`、`bicubic`、`area`、`lanczos3`、`lanczos5`、`gaussian`、`mitchellcubic`。 + /// 默认为`'bilinear'`。 + /// + /// + public IDatasetV2 paths_to_dataset(string[] image_paths, + Shape image_size, + int num_channels = 3, + int num_classes = 6, + string interpolation = "bilinear") + { + var path_ds = tf.data.Dataset.from_tensor_slices(image_paths); + var img_ds = path_ds.map(x => path_to_image(x, image_size, num_channels, interpolation)); + var label_ds = dataset_utils.labels_to_dataset(new int[num_classes] , "", num_classes); + + return img_ds; + } + public IDatasetV2 paths_and_labels_to_dataset(string[] image_paths, Shape image_size, int num_channels, diff --git a/src/TensorFlowNET.Keras/Regularizers.cs b/src/TensorFlowNET.Keras/Regularizers.cs index 98da27a7f..73b72a051 100644 --- a/src/TensorFlowNET.Keras/Regularizers.cs +++ b/src/TensorFlowNET.Keras/Regularizers.cs @@ -1,8 +1,51 @@ -namespace Tensorflow.Keras +using Tensorflow.Operations.Regularizers; + +namespace Tensorflow.Keras { - public class Regularizers + public class Regularizers: IRegularizerApi + { + private static Dictionary _nameActivationMap; + + public IRegularizer l1(float l1 = 0.01f) + => new L1(l1); + public IRegularizer l2(float l2 = 0.01f) + => new L2(l2); + + //From TF source + //# The default value for l1 and l2 are different from the value in l1_l2 + //# for backward compatibility reason. Eg, L1L2(l2=0.1) will only have l2 + //# and no l1 penalty. + public IRegularizer l1l2(float l1 = 0.00f, float l2 = 0.00f) + => new L1L2(l1, l2); + + static Regularizers() { - public IRegularizer l2(float l2 = 0.01f) - => new L2(l2); + _nameActivationMap = new Dictionary(); + _nameActivationMap["L1"] = new L1(); + _nameActivationMap["L1"] = new L2(); + _nameActivationMap["L1"] = new L1L2(); } + + public IRegularizer L1 => l1(); + + public IRegularizer L2 => l2(); + + public IRegularizer L1L2 => l1l2(); + + public IRegularizer GetRegularizerFromName(string name) + { + if (name == null) + { + throw new Exception($"Regularizer name cannot be null"); + } + if (!_nameActivationMap.TryGetValue(name, out var res)) + { + throw new Exception($"Regularizer {name} not found"); + } + else + { + return res; + } + } + } } diff --git a/src/TensorFlowNET.Keras/Regularizers/L1.cs b/src/TensorFlowNET.Keras/Regularizers/L1.cs deleted file mode 100644 index 0f904b6f9..000000000 --- a/src/TensorFlowNET.Keras/Regularizers/L1.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System; - -namespace Tensorflow.Keras -{ - public class L1 : IRegularizer - { - float l1; - - public L1(float l1 = 0.01f) - { - this.l1 = l1; - } - - public Tensor Apply(RegularizerArgs args) - { - return l1 * math_ops.reduce_sum(math_ops.abs(args.X)); - } - } -} diff --git a/src/TensorFlowNET.Keras/Regularizers/L1L2.cs b/src/TensorFlowNET.Keras/Regularizers/L1L2.cs deleted file mode 100644 index f619f1582..000000000 --- a/src/TensorFlowNET.Keras/Regularizers/L1L2.cs +++ /dev/null @@ -1,24 +0,0 @@ -using System; -using static Tensorflow.Binding; -namespace Tensorflow.Keras -{ - public class L1L2 : IRegularizer - { - float l1; - float l2; - - public L1L2(float l1 = 0.0f, float l2 = 0.0f) - { - this.l1 = l1; - this.l2 = l2; - - } - public Tensor Apply(RegularizerArgs args) - { - Tensor regularization = tf.constant(0.0, args.X.dtype); - regularization += l1 * math_ops.reduce_sum(math_ops.abs(args.X)); - regularization += l2 * math_ops.reduce_sum(math_ops.square(args.X)); - return regularization; - } - } -} diff --git a/src/TensorFlowNET.Keras/Regularizers/L2.cs b/src/TensorFlowNET.Keras/Regularizers/L2.cs deleted file mode 100644 index 034bbd236..000000000 --- a/src/TensorFlowNET.Keras/Regularizers/L2.cs +++ /dev/null @@ -1,17 +0,0 @@ -namespace Tensorflow.Keras -{ - public class L2 : IRegularizer - { - float l2; - - public L2(float l2 = 0.01f) - { - this.l2 = l2; - } - - public Tensor Apply(RegularizerArgs args) - { - return l2 * math_ops.reduce_sum(math_ops.square(args.X)); - } - } -} diff --git a/src/TensorFlowNET.Keras/Saving/KerasObjectLoader.cs b/src/TensorFlowNET.Keras/Saving/KerasObjectLoader.cs index a26879e0c..0bd816ccb 100644 --- a/src/TensorFlowNET.Keras/Saving/KerasObjectLoader.cs +++ b/src/TensorFlowNET.Keras/Saving/KerasObjectLoader.cs @@ -8,12 +8,11 @@ using System.Linq; using System.Reflection; using System.Text.RegularExpressions; -using Tensorflow.Extensions; +using Tensorflow.Common.Extensions; using Tensorflow.Framework.Models; using Tensorflow.Keras.ArgsDefinition; using Tensorflow.Keras.Engine; using Tensorflow.Keras.Layers; -using Tensorflow.Keras.Layers.Rnn; using Tensorflow.Keras.Losses; using Tensorflow.Keras.Metrics; using Tensorflow.Keras.Saving.SavedModel; @@ -174,7 +173,7 @@ public void del_tracking() } if(node is Functional functional) { - foreach(var name in functional.UnconditionalDependencyNames.Keys) + foreach(var name in functional.UnconditionalDependencyNames.Keys.ToArray()) { if(Regex.Match(name, @"^layer(_with_weights)?-[\d+]").Success) { @@ -693,7 +692,6 @@ private bool _try_build_layer(Layer obj, int node_id, KerasShapesWrapper build_i /// Infers input shape of layer from SavedModel functions. /// /// - /// /// private TensorSpec _infer_inputs(int layer_node_id) { diff --git a/src/TensorFlowNET.Keras/Saving/SavedModel/load.cs b/src/TensorFlowNET.Keras/Saving/SavedModel/load.cs index aa763fc2e..091dbb810 100644 --- a/src/TensorFlowNET.Keras/Saving/SavedModel/load.cs +++ b/src/TensorFlowNET.Keras/Saving/SavedModel/load.cs @@ -1,97 +1,89 @@ -using Google.Protobuf; -using System; -using System.Collections.Generic; -using System.IO; -using System.Text; -using Tensorflow.Keras.Engine; +using System.IO; using Tensorflow.Train; using ThirdParty.Tensorflow.Python.Keras.Protobuf; -using static Tensorflow.Binding; -using static Tensorflow.KerasApi; -namespace Tensorflow.Keras.Saving.SavedModel +namespace Tensorflow.Keras.Saving.SavedModel; + +public class KerasLoadModelUtils { - public class KerasLoadModelUtils + /// + /// Corresponding to keras/saving/save.py/load_model + /// + /// + /// + /// + /// + /// + public static Trackable load_model(string filepath, IDictionary? custom_objects = null, + bool compile = true, LoadOptions? options = null) { - /// - /// Corresponding to keras/saving/save.py/load_model - /// - /// - /// - /// - /// - /// - public static Trackable load_model(string filepath, IDictionary? custom_objects = null, - bool compile = true, LoadOptions? options = null) + using var savingScope = SharedObjectSavingScope.Enter(); + + using var ctx = LoadContext.load_context(options); + + if (!File.Exists(filepath) && !Directory.Exists(filepath)) { - using (SharedObjectSavingScope.Enter()) - { - using (LoadContext.load_context(options)) - { - if (!File.Exists(filepath) && !Directory.Exists(filepath)) - { - throw new IOException($"No file or directory found at {filepath}."); - } - if (Directory.Exists(filepath)) - { - return load(filepath, compile, options); - } - else - { - throw new NotImplementedException("Model load of h5 format has not been supported. Please submit an issue to https://github.com/SciSharp/TensorFlow.NET/issues if it's needed."); - } - } - } + throw new IOException($"No file or directory found at {filepath}."); } - private static Trackable load(string path, bool compile = true, LoadOptions? options = null) + if (Directory.Exists(filepath)) + { + return load(filepath, compile, options); + } + else { - SavedMetadata metadata = new SavedMetadata(); - var meta_graph_def = Loader.parse_saved_model(path).MetaGraphs[0]; - var object_graph_def = meta_graph_def.ObjectGraphDef; - string path_to_metadata_pb = Path.Combine(path, Constants.SAVED_METADATA_PATH); - if (File.Exists(path_to_metadata_pb)) - { - metadata.MergeFrom(new FileStream(path_to_metadata_pb, FileMode.Open, FileAccess.Read)); - } - else - { - throw new NotImplementedException("SavedModel saved prior to TF 2.5 detected when loading Keras model, please" + - " use higher version or submit an issue to https://github.com/SciSharp/TensorFlow.NET/issues. to let us know you need it."); - } + throw new NotImplementedException("Model load of h5 format has not been supported. Please submit an issue to https://github.com/SciSharp/TensorFlow.NET/issues if it's needed."); + } + } - if (metadata.Nodes is null || metadata.Nodes.Count == 0) - { - return Loader.load(path, options: options) as Model; - } + private static Trackable load(string path, bool compile = true, LoadOptions? options = null) + { + SavedMetadata metadata; + var meta_graph_def = Loader.parse_saved_model(path).MetaGraphs[0]; + var object_graph_def = meta_graph_def.ObjectGraphDef; + string path_to_metadata_pb = Path.Combine(path, Constants.SAVED_METADATA_PATH); + if (File.Exists(path_to_metadata_pb)) + { + using var stream = new FileStream(path_to_metadata_pb, FileMode.Open, FileAccess.Read); + metadata = SavedMetadata.Parser.ParseFrom(stream); + } + else + { + throw new NotImplementedException("SavedModel saved prior to TF 2.5 detected when loading Keras model, please" + + " use higher version or submit an issue to https://github.com/SciSharp/TensorFlow.NET/issues. to let us know you need it."); + } - var keras_loader = new KerasObjectLoader(metadata, object_graph_def); - keras_loader.load_layers(compile: compile); + if (metadata.Nodes is null || metadata.Nodes.Count == 0) + { + return Loader.load(path, options: options) as Model; + } - Dictionary)> nodes_to_load = new(); - nodes_to_load["root"] = (null, null); - foreach(var item in keras_loader.LoadedNodes) - { - nodes_to_load[keras_loader.get_path(item.Key)] = item.Value; - } - var loaded = Loader.load_partial(path, nodes_to_load, options); + var keras_loader = new KerasObjectLoader(metadata, object_graph_def); + keras_loader.load_layers(compile: compile); - keras_loader.finalize_objects(); - keras_loader.del_tracking(); + Dictionary)> nodes_to_load = new(); + nodes_to_load["root"] = (null, null); + foreach(var item in keras_loader.LoadedNodes) + { + nodes_to_load[keras_loader.get_path(item.Key)] = item.Value; + } + var loaded = Loader.load_partial(path, nodes_to_load, options); - var model = loaded["root"]; + keras_loader.finalize_objects(); + keras_loader.del_tracking(); - if(model is Model && compile) - { - // TODO(Rinne): implement it. - } + var model = loaded["root"]; - if (!tf.Context.executing_eagerly()) - { - // TODO(Rinne): implement it. - } + if (model is Model && compile) + { + // TODO(Rinne): implement it. + } - return model; + if (!tf.Context.executing_eagerly()) + { + // TODO(Rinne): implement it. } + + return model; } } diff --git a/src/TensorFlowNET.Keras/Saving/SavedModel/serialized_attributes.cs b/src/TensorFlowNET.Keras/Saving/SavedModel/serialized_attributes.cs index 0ec5d1a8c..325d3327a 100644 --- a/src/TensorFlowNET.Keras/Saving/SavedModel/serialized_attributes.cs +++ b/src/TensorFlowNET.Keras/Saving/SavedModel/serialized_attributes.cs @@ -3,7 +3,7 @@ using System.Linq; using System.Text; using Tensorflow.Keras.Engine; -using Tensorflow.Keras.Layers.Rnn; +using Tensorflow.Keras.Layers; using Tensorflow.Keras.Metrics; using Tensorflow.Train; diff --git a/src/TensorFlowNET.Keras/Saving/hdf5_format.cs b/src/TensorFlowNET.Keras/Saving/hdf5_format.cs index b04391be9..68b73953d 100644 --- a/src/TensorFlowNET.Keras/Saving/hdf5_format.cs +++ b/src/TensorFlowNET.Keras/Saving/hdf5_format.cs @@ -7,6 +7,8 @@ using static Tensorflow.Binding; using static Tensorflow.KerasApi; using System.Linq; +using System.Text.RegularExpressions; + namespace Tensorflow.Keras.Saving { public class hdf5_format @@ -80,7 +82,7 @@ public static void load_optimizer_weights_from_hdf5_group(long filepath = -1, Di } - public static void load_weights_from_hdf5_group(long f, List layers) + public static List<(IVariableV1, NDArray)> load_weights_from_hdf5_group(long f, List layers) { string original_keras_version = "2.5.0"; string original_backend = null; @@ -131,7 +133,7 @@ public static void load_weights_from_hdf5_group(long f, List layers) long g = H5G.open(f, name); var weight_names = load_attributes_from_hdf5_group(g, "weight_names"); foreach (var i_ in weight_names) - { + { (success, Array result) = Hdf5.ReadDataset(g, i_); if (success) weight_values.Add(np.array(result)); @@ -150,7 +152,7 @@ public static void load_weights_from_hdf5_group(long f, List layers) weight_value_tuples.AddRange(zip(symbolic_weights, weight_values)); } - keras.backend.batch_set_value(weight_value_tuples); + return weight_value_tuples; } public static void toarrayf4(long filepath = -1, Dictionary custom_objects = null, bool compile = false) @@ -192,8 +194,13 @@ public static void save_weights_to_hdf5_group(long f, List layers) var tensor = val.AsTensor(); if (name.IndexOf("/") > 1) { - var crDataGroup = Hdf5.CreateOrOpenGroup(g, Hdf5Utils.NormalizedName(name.Split('/')[0])); - WriteDataset(crDataGroup, name.Split('/')[1], tensor); + var crDataGroup = g; + string[] name_split = name.Split('/'); + for(int i = 0; i < name_split.Length - 1; i++) + { + crDataGroup = Hdf5.CreateOrOpenGroup(crDataGroup, Hdf5Utils.NormalizedName(name_split[i])); + } + WriteDataset(crDataGroup, name_split[name_split.Length - 1], tensor); Hdf5.CloseGroup(crDataGroup); } else diff --git a/src/TensorFlowNET.Keras/Tensorflow.Keras.csproj b/src/TensorFlowNET.Keras/Tensorflow.Keras.csproj index 8b3c92655..eb8ebf93c 100644 --- a/src/TensorFlowNET.Keras/Tensorflow.Keras.csproj +++ b/src/TensorFlowNET.Keras/Tensorflow.Keras.csproj @@ -7,39 +7,44 @@ enable Tensorflow.Keras AnyCPU;x64 - 0.10.5 + 0.15.0 Haiping Chen Keras for .NET - Apache 2.0, Haiping Chen 2023 + Apache 2.0, Haiping Chen since 2018 TensorFlow.Keras https://github.com/SciSharp/TensorFlow.NET https://avatars3.githubusercontent.com/u/44989469?s=200&v=4 https://github.com/SciSharp/TensorFlow.NET - Keras for .NET is a C# version of Keras ported from the python version. - -* Support CIFAR-10 dataset in keras.datasets. -* Support Conv2D functional API. -* Support BatchNormalization layer. -* Building keras model in subclass, functional and sequential api -* Implemented backward_function. -* Support model.load_weights. -* Add Subtract layer -* Text preprocessing -* Preprocessing.timeseries_dataset_from_array -* Fixed memory leak for YOLOv3 model. + + Keras for .NET is a C# version of Keras ported from the python version. + + * Support CIFAR-10 dataset in keras.datasets. + * Support Conv2D functional API. + * Support BatchNormalization layer. + * Building keras model in subclass, functional and sequential api + * Implemented backward_function. + * Support model.load_weights. + * Add Subtract layer + * Text preprocessing + * Preprocessing.timeseries_dataset_from_array + * Fixed memory leak for YOLOv3 model. + * Support RNN and LSTM models + * Support Transformer model + * Support BERT model + Keras for .NET Keras is an API designed for human beings, not machines. Keras follows best practices for reducing cognitive load: it offers consistent & simple APIs, it minimizes the number of user actions required for common use cases, and it provides clear & actionable error messages. SciSharp STACK - true + False tensorflow, keras, deep learning, machine learning true packages Git - true + False Open.snk - 0.10.5.0 - 0.10.5.0 + 0.15.0.0 + 0.15.0.0 LICENSE Debug;Release;GPU @@ -70,9 +75,77 @@ Keras is an API designed for human beings, not machines. Keras follows best prac + + True + 1 + $(NoWarn),1573,1591,1712,8602,8603,8625,CS0612 + + + + True + 1 + $(NoWarn),1573,1591,1712,8602,8603,8625,CS0612 + + + + True + 1 + $(NoWarn),1573,1591,1712,8602,8603,8625,CS0612 + + + + True + 1 + $(NoWarn),1573,1591,1712,8602,8603,8625,CS0612 + + + + False + 1 + $(NoWarn),1573,1591,1712,8602,8603,8625,CS0612 + + + + False + 1 + $(NoWarn),1573,1591,1712,8602,8603,8625,CS0612 + + + + False + 1 + $(NoWarn),1573,1591,1712,8602,8603,8625,CS0612 + + + + False + 1 + $(NoWarn),1573,1591,1712,8602,8603,8625,CS0612 + + + + 1 + $(NoWarn),1573,1591,1712,8602,8603,8625,CS0612 + + + + 1 + $(NoWarn),1573,1591,1712,8602,8603,8625,CS0612 + + + + 1 + $(NoWarn),1573,1591,1712,8602,8603,8625,CS0612 + + + + 1 + $(NoWarn),1573,1591,1712,8602,8603,8625,CS0612 + + - - + + diff --git a/src/TensorFlowNET.Keras/Utils/RnnUtils.cs b/src/TensorFlowNET.Keras/Utils/RnnUtils.cs new file mode 100644 index 000000000..1e9f6d845 --- /dev/null +++ b/src/TensorFlowNET.Keras/Utils/RnnUtils.cs @@ -0,0 +1,103 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Text; +using Tensorflow.Common.Types; +using Tensorflow.Keras.Layers; +using Tensorflow.Common.Extensions; + +namespace Tensorflow.Keras.Utils +{ + internal static class RnnUtils + { + internal static Tensors generate_zero_filled_state(Tensor batch_size_tensor, INestStructure state_size, TF_DataType dtype) + { + Func create_zeros = (unnested_state_size) => + { + var flat_dims = new Shape(unnested_state_size).dims; + var init_state_size = new Tensor[] { batch_size_tensor }. + Concat(flat_dims.Select(x => tf.constant(x, dtypes.int32))).ToArray(); + return array_ops.zeros(init_state_size, dtype: dtype); + }; + + // TODO(Rinne): map structure with nested tensors. + if(state_size.TotalNestedCount > 1) + { + return new Tensors(state_size.Flatten().Select(s => create_zeros(s)).ToArray()); + } + else + { + return create_zeros(state_size.Flatten().First()); + } + + } + + internal static Tensors generate_zero_filled_state_for_cell(IRnnCell cell, Tensors inputs, Tensor batch_size, TF_DataType dtype) + { + if (inputs is not null) + { + batch_size = array_ops.shape(inputs)[0]; + dtype = inputs.dtype; + } + return generate_zero_filled_state(batch_size, cell.StateSize, dtype); + } + + /// + /// Standardizes `__call__` to a single list of tensor inputs. + /// + /// When running a model loaded from a file, the input tensors + /// `initial_state` and `constants` can be passed to `RNN.__call__()` as part + /// of `inputs` instead of by the dedicated keyword arguments.This method + /// makes sure the arguments are separated and that `initial_state` and + /// `constants` are lists of tensors(or None). + /// + /// Tensor or list/tuple of tensors. which may include constants + /// and initial states.In that case `num_constant` must be specified. + /// Tensor or list of tensors or None, initial states. + /// Tensor or list of tensors or None, constant tensors. + /// Expected number of constants (if constants are passed as + /// part of the `inputs` list. + /// + internal static (Tensors, Tensors, Tensors) standardize_args(Tensors inputs, Tensors initial_state, Tensors constants, int num_constants) + { + if(inputs.Length > 1) + { + // There are several situations here: + // In the graph mode, __call__ will be only called once. The initial_state + // and constants could be in inputs (from file loading). + // In the eager mode, __call__ will be called twice, once during + // rnn_layer(inputs=input_t, constants=c_t, ...), and second time will be + // model.fit/train_on_batch/predict with real np data. In the second case, + // the inputs will contain initial_state and constants as eager tensor. + // + // For either case, the real input is the first item in the list, which + // could be a nested structure itself. Then followed by initial_states, which + // could be a list of items, or list of list if the initial_state is complex + // structure, and finally followed by constants which is a flat list. + Debug.Assert(initial_state is null && constants is null); + if(num_constants > 0) + { + constants = inputs.TakeLast(num_constants).ToArray().ToTensors(); + inputs = inputs.SkipLast(num_constants).ToArray().ToTensors(); + } + if(inputs.Length > 1) + { + initial_state = inputs.Skip(1).ToArray().ToTensors(); + inputs = inputs.Take(1).ToArray().ToTensors(); + } + } + + return (inputs, initial_state, constants); + } + + /// + /// Check whether the state_size contains multiple states. + /// + /// + /// + public static bool is_multiple_state(INestStructure state_size) + { + return state_size.TotalNestedCount > 1; + } + } +} diff --git a/src/TensorFlowNET.Keras/Utils/data_utils.cs b/src/TensorFlowNET.Keras/Utils/data_utils.cs index 5b84c601f..b0bc15540 100644 --- a/src/TensorFlowNET.Keras/Utils/data_utils.cs +++ b/src/TensorFlowNET.Keras/Utils/data_utils.cs @@ -39,5 +39,54 @@ public static string get_file(string fname, string origin, return datadir; } + + public static (int[,], long[]) _remove_long_seq(int maxlen, int[,] seq, long[] label) + { + /*Removes sequences that exceed the maximum length. + + Args: + maxlen: Int, maximum length of the output sequences. + seq: List of lists, where each sublist is a sequence. + label: List where each element is an integer. + + Returns: + new_seq, new_label: shortened lists for `seq` and `label`. + + */ + var nRow = seq.GetLength(0); + var nCol = seq.GetLength(1); + List new_seq = new List(); + List new_label = new List(); + + for (var i = 0; i < nRow; i++) + { + if (maxlen < nCol && seq[i, maxlen] != 0) + continue; + int[] sentence = new int[maxlen]; + for (var j = 0; j < maxlen && j < nCol; j++) + { + sentence[j] = seq[i, j]; + } + new_seq.Add(sentence); + new_label.Add(label[i]); + } + + int[,] new_seq_array = new int[new_seq.Count, maxlen]; + long[] new_label_array = new long[new_label.Count]; + + for (var i = 0; i < new_seq.Count; i++) + { + for (var j = 0; j < maxlen; j++) + { + new_seq_array[i, j] = new_seq[i][j]; + } + } + + for (var i = 0; i < new_label.Count; i++) + { + new_label_array[i] = new_label[i]; + } + return (new_seq_array, new_label_array); + } } } diff --git a/src/TensorFlowNET.Keras/Utils/generic_utils.cs b/src/TensorFlowNET.Keras/Utils/generic_utils.cs index 6a59fb880..20937e2e5 100644 --- a/src/TensorFlowNET.Keras/Utils/generic_utils.cs +++ b/src/TensorFlowNET.Keras/Utils/generic_utils.cs @@ -29,6 +29,7 @@ limitations under the License. using Tensorflow.Keras.Layers; using Tensorflow.Keras.Saving; using Tensorflow.Train; +using System.Text.RegularExpressions; namespace Tensorflow.Keras.Utils { @@ -111,12 +112,23 @@ public static FunctionalConfig deserialize_model_config(JToken json) foreach (var token in layersToken) { var args = deserialize_layer_args(token["class_name"].ToObject(), token["config"]); + + List nodeConfig = null; //python tensorflow sometimes exports inbound nodes in an extra nested array + if (token["inbound_nodes"].Count() > 0 && token["inbound_nodes"][0].Count() > 0 && token["inbound_nodes"][0][0].Count() > 0) + { + nodeConfig = token["inbound_nodes"].ToObject>>().FirstOrDefault() ?? new List(); + } + else + { + nodeConfig = token["inbound_nodes"].ToObject>(); + } + config.Layers.Add(new LayerConfig() { Config = args, Name = token["name"].ToObject(), ClassName = token["class_name"].ToObject(), - InboundNodes = token["inbound_nodes"].ToObject>() + InboundNodes = nodeConfig, }); } config.InputLayers = json["input_layers"].ToObject>(); @@ -126,12 +138,15 @@ public static FunctionalConfig deserialize_model_config(JToken json) public static string to_snake_case(string name) { - return string.Concat(name.Select((x, i) => + string intermediate = Regex.Replace(name, "(.)([A-Z][a-z0-9]+)", "$1_$2"); + string insecure = Regex.Replace(intermediate, "([a-z])([A-Z])", "$1_$2").ToLower(); + + if (insecure[0] != '_') { - return i > 0 && char.IsUpper(x) && !Char.IsDigit(name[i - 1]) ? - "_" + x.ToString() : - x.ToString(); - })).ToLower(); + return insecure; + } + + return "private" + insecure; } /// diff --git a/src/TensorflowNET.Hub/KerasLayer.cs b/src/TensorflowNET.Hub/KerasLayer.cs index b9ca949bc..20d9851b1 100644 --- a/src/TensorflowNET.Hub/KerasLayer.cs +++ b/src/TensorflowNET.Hub/KerasLayer.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using Tensorflow.Common.Types; using Tensorflow.Keras.Engine; using Tensorflow.Train; using Tensorflow.Training; @@ -89,7 +90,7 @@ private void _setup_layer(bool trainable = false) } } - protected override Tensors Call(Tensors inputs, Tensor state = null, bool? training = null) + protected override Tensors Call(Tensors inputs, Tensors state = null, bool? training = null, IOptionalArgs? optionalArgs = null) { _check_trainability(); diff --git a/src/TensorflowNET.Hub/Tensorflow.Hub.csproj b/src/TensorflowNET.Hub/Tensorflow.Hub.csproj index 3c09f808e..efa37598d 100644 --- a/src/TensorflowNET.Hub/Tensorflow.Hub.csproj +++ b/src/TensorflowNET.Hub/Tensorflow.Hub.csproj @@ -26,7 +26,7 @@ - + diff --git a/test/TensorFlow.Kernel.UnitTest/TensorFlow.Kernel.UnitTest.csproj b/test/TensorFlow.Kernel.UnitTest/TensorFlow.Kernel.UnitTest.csproj new file mode 100644 index 000000000..461993408 --- /dev/null +++ b/test/TensorFlow.Kernel.UnitTest/TensorFlow.Kernel.UnitTest.csproj @@ -0,0 +1,24 @@ + + + + net6.0 + enable + enable + + false + true + + + + + + + + + + + + + + + diff --git a/test/TensorFlow.Kernel.UnitTest/array_ops/concat_op_test.cs b/test/TensorFlow.Kernel.UnitTest/array_ops/concat_op_test.cs new file mode 100644 index 000000000..67d0aa602 --- /dev/null +++ b/test/TensorFlow.Kernel.UnitTest/array_ops/concat_op_test.cs @@ -0,0 +1,63 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Tensorflow; +using Tensorflow.NumPy; +using static Tensorflow.Binding; + +namespace TensorFlow.Kernel.UnitTest +{ + [TestClass] + public class concat_op_test + { + [TestMethod] + public void testConcatEmpty() + { + var t1 = tf.constant(new int[] { }); + var t2 = tf.constant(new int[] { }); + var c = array_ops.concat(new[] { t1, t2 }, 0); + var expected = np.array(new int[] { }); + Assert.IsTrue(Enumerable.SequenceEqual(expected.ToArray(), c.numpy().ToArray())); + } + + [TestMethod] + public void testConcatNegativeAxis() + { + var t1 = tf.constant(new int[,] { { 1, 2, 3 }, { 4, 5, 6 } }); + var t2 = tf.constant(new int[,] { { 7, 8, 9 }, { 10, 11, 12 } }); + var c = array_ops.concat(new[] { t1, t2 }, -2); + var expected = np.array(new int[,,] { { { 1, 2, 3 }, { 4, 5, 6 } }, { { 7, 8, 9 }, { 10, 11, 12 } } }); + Assert.IsTrue(Enumerable.SequenceEqual(expected.ToArray(), c.numpy().ToArray())); + + c = array_ops.concat(new[] { t1, t2 }, -1); + expected = np.array(new int[,] { { 1, 2, 3, 7, 8, 9 }, { 4, 5, 6, 10, 11, 12 } }); + Assert.IsTrue(Enumerable.SequenceEqual(expected.ToArray(), c.numpy().ToArray())); + } + + [TestMethod] + [DataRow(TF_DataType.TF_INT32)] + [DataRow(TF_DataType.TF_INT64)] + [DataRow(TF_DataType.TF_UINT32)] + [DataRow(TF_DataType.TF_UINT64)] + public void testConcatDtype(TF_DataType dtype) + { + var t1 = tf.constant(new int[,] { { 1, 2, 3 }, { 4, 5, 6 } }, dtype: dtype); + var t2 = tf.constant(new int[,] { { 7, 8, 9 }, { 10, 11, 12 } }, dtype: dtype); + var c = array_ops.concat(new[] { t1, t2 }, 1); + var expected = np.array(new int[,] { { 1, 2, 3, 7, 8, 9 }, { 4, 5, 6, 10, 11, 12 } }); + Assert.IsTrue(Enumerable.SequenceEqual(expected.ToArray(), tf.cast(c, TF_DataType.TF_INT32).numpy().ToArray())); + + } + + [TestMethod] + [DataRow(TF_DataType.TF_INT32)] + [DataRow(TF_DataType.TF_INT64)] + public void testConcatAxisType(TF_DataType dtype) + { + var t1 = tf.constant(new int[,] { { 1, 2, 3 }, { 4, 5, 6 } }); + var t2 = tf.constant(new int[,] { { 7, 8, 9 }, { 10, 11, 12 } }); + var c = array_ops.concat(new[] { t1, t2 }, tf.constant(1, dtype: dtype)); + var expected = np.array(new int[,] { { 1, 2, 3, 7, 8, 9 }, { 4, 5, 6, 10, 11, 12 } }); + Assert.IsTrue(Enumerable.SequenceEqual(expected.ToArray(), tf.cast(c, TF_DataType.TF_INT32).numpy().ToArray())); + } + + } +} \ No newline at end of file diff --git a/test/TensorFlowNET.Graph.UnitTest/Basics/TensorTest.cs b/test/TensorFlowNET.Graph.UnitTest/Basics/TensorTest.cs index 90de78743..8093c1f23 100644 --- a/test/TensorFlowNET.Graph.UnitTest/Basics/TensorTest.cs +++ b/test/TensorFlowNET.Graph.UnitTest/Basics/TensorTest.cs @@ -3,6 +3,7 @@ using System; using System.Linq; using static Tensorflow.Binding; +using Tensorflow; namespace TensorFlowNET.UnitTest.Basics { @@ -60,14 +61,14 @@ public void batch_to_space_nd() Assert.IsTrue(Enumerable.SequenceEqual(new int[] { 15, 21, 16, 22, 17, 23 }, result[0, 3].ToArray())); } - [TestMethod, Ignore] + [TestMethod] public void boolean_mask() { + if (!tf.executing_eagerly()) + tf.enable_eager_execution(); var tensor = new[] { 0, 1, 2, 3 }; var mask = np.array(new[] { true, false, true, false }); var masked = tf.boolean_mask(tensor, mask); - var sess = tf.Session(); - var result = sess.run(masked); Assert.IsTrue(Enumerable.SequenceEqual(new int[] { 0, 2 }, masked.ToArray())); } } diff --git a/test/TensorFlowNET.Graph.UnitTest/ControlFlowTest/WhileContextTestCase.cs b/test/TensorFlowNET.Graph.UnitTest/ControlFlowTest/WhileContextTestCase.cs index a31dea7d2..e93324f3e 100644 --- a/test/TensorFlowNET.Graph.UnitTest/ControlFlowTest/WhileContextTestCase.cs +++ b/test/TensorFlowNET.Graph.UnitTest/ControlFlowTest/WhileContextTestCase.cs @@ -1,5 +1,6 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using System; +using System.Linq; using Tensorflow; using static Tensorflow.Binding; @@ -23,10 +24,10 @@ public void SimpleWhileLoop() private void _testWhileContextHelper(int maximum_iterations) { // TODO: implement missing code dependencies - var sess = this.cached_session(); + using var sess = this.cached_session(); var i = constant_op.constant(0, name: "i"); - var c = new Func(x => gen_math_ops.less(x, 10, name: "c")); - var b = new Func(x => gen_math_ops.add(x, 1, name: "c")); + var c = new Func(x => gen_math_ops.less(x, ops.convert_to_tensor(10), name: "c")); + var b = new Func(x => math_ops.add(x, 1, name: "c")); //control_flow_ops.while_loop( // c, b, i , maximum_iterations: tf.constant(maximum_iterations)); foreach (Operation op in sess.graph.get_operations()) diff --git a/test/TensorFlowNET.Graph.UnitTest/GradientTest/GradientTest.cs b/test/TensorFlowNET.Graph.UnitTest/GradientTest/GradientTest.cs index 92afd6a3f..cea6de172 100644 --- a/test/TensorFlowNET.Graph.UnitTest/GradientTest/GradientTest.cs +++ b/test/TensorFlowNET.Graph.UnitTest/GradientTest/GradientTest.cs @@ -5,6 +5,7 @@ using System.Linq; using Tensorflow; using static Tensorflow.Binding; +using Tensorflow.Framework; namespace TensorFlowNET.UnitTest.Gradient { @@ -260,7 +261,7 @@ public void testConcatGrad() public void testStopGradientFunction() { var ap = tf.constant(1f); - var b = tf.tanh(ap) + gen_array_ops.stop_gradient(ap); + var b = tf.tanh(ap) + array_ops.stop_gradient(ap); var g = tf.gradients(b, ap); var sess = tf.Session(); var result = sess.run(g); @@ -388,81 +389,77 @@ public void testBoundaryStop() } - [Ignore("TODO")] [TestMethod] public void testBoundaryContinue() { - //@test_util.run_v1_only("b/120545219") - //def testBoundaryContinue(self): - // # Test that we differentiate both 'x' and 'y' correctly when x is a - // # predecessor of y. - // with self.cached_session(): - // x = constant(1.0) - // y = x * 2.0 - // z = y * 3.0 - // grads = gradients.gradients(z, [x, y]) - // self.assertTrue(all(x is not None for x in grads)) - // self.assertEqual(6.0, grads[0].eval()) + // Test that we differentiate both 'x' and 'y' correctly when x is a + // predecessor of y. + + //TODO: @test_util.run_v1_only("b/120545219") + using (self.cached_session()) + { + var x = tf.constant(1.0); + var y = x * 2.0; + var z = y * 3.0; + var grads = tf.gradients(z, new[] { x, y }); + self.assertTrue(all(grads.Select(x => x != null))); + self.assertEqual(6.0, grads[0].eval()); + } } - [Ignore("TODO")] [TestMethod] public void testAggregationMethodAccumulateN() { + //TODO: @test_util.run_v1_only("b/120545219") - //@test_util.run_v1_only("b/120545219") - //def testAggregationMethodAccumulateN(self): - // with self.cached_session(): - // x = constant(1.0) - // y = x * 2.0 - // z = y + y + y + y + y + y + y + y + y + y - // grads = gradients.gradients( - // z, [x, y], - // aggregation_method=gradients.AggregationMethod. - // EXPERIMENTAL_ACCUMULATE_N) - // self.assertTrue(all(x is not None for x in grads)) - // self.assertEqual(20.0, grads[0].eval()) - // self.assertEqual(10.0, grads[1].eval()) - + using (self.cached_session()) + { + var x = tf.constant(1.0); + var y = x * 2.0; + var z = y + y + y + y + y + y + y + y + y + y; + var grads = tf.gradients(z, new[] { x, y }, + aggregation_method: AggregationMethod.EXPERIMENTAL_ACCUMULATE_N); + self.assertTrue(all(grads.Select(x => x != null))); + self.assertEqual(20.0, grads[0].eval()); + self.assertEqual(10.0, grads[1].eval()); + } } - [Ignore("TODO")] [TestMethod] public void testAggregationMethodAddN() { - //@test_util.run_v1_only("b/120545219") - //def testAggregationMethodAddN(self): - // with self.cached_session(): - // x = constant(1.0) - // y = x * 2.0 - // z = y + y + y + y + y + y + y + y + y + y - // grads = gradients.gradients( - // z, [x, y], aggregation_method=gradients.AggregationMethod.ADD_N) - // self.assertTrue(all(x is not None for x in grads)) - // self.assertEqual(20.0, grads[0].eval()) - // self.assertEqual(10.0, grads[1].eval()) - + //TODO: @test_util.run_v1_only("b/120545219") + using (self.cached_session()) + { + var x = tf.constant(1.0); + var y = x * 2.0; + var z = y + y + y + y + y + y + y + y + y + y; + var grads = tf.gradients(z, new[] { x, y }, + aggregation_method: AggregationMethod.ADD_N); + self.assertTrue(grads.All(x => x != null)); + self.assertEqual(20.0, grads[0].eval()); + self.assertEqual(10.0, grads[1].eval()); + } } - [Ignore("TODO")] [TestMethod] public void testAggregationMethodTree() { - //@test_util.run_v1_only("b/120545219") - //def testAggregationMethodTree(self): - // with self.cached_session(): - // x = constant(1.0) - // y = x * 2.0 - // z = y + y + y + y + y + y + y + y + y + y - // grads = gradients.gradients( - // z, [x, y], - // aggregation_method=gradients.AggregationMethod.EXPERIMENTAL_TREE) - // self.assertTrue(all(x is not None for x in grads)) - // self.assertEqual(20.0, grads[0].eval()) - // self.assertEqual(10.0, grads[1].eval()) + //TODO: @test_util.run_v1_only("b/120545219") + using (self.cached_session()) + { + var x = tf.constant(1.0); + var y = x * 2.0; + var z = y + y + y + y + y + y + y + y + y + y; + var grads = tf.gradients(z, new[] { x, y }, + aggregation_method: AggregationMethod.EXPERIMENTAL_TREE); + self.assertTrue(grads.All(x => x != null)); + self.assertEqual(20.0, grads[0].eval()); + self.assertEqual(10.0, grads[1].eval()); + } } [Ignore("TODO")] @@ -491,24 +488,37 @@ public void testNoGradientForStringOutputs() // self.assertTrue(isinstance(grads[0], ops.Tensor)) } - [Ignore("TODO")] + [Ignore("TODO: CompositeTensors are not supported yet.")] [TestMethod] public void testSingletonIndexedSlices() { + tf.Graph().as_default(); + + // TODO: uncomment when CompositeTensors are supported. + /* + var x = tf.placeholder(TF_DataType.TF_FLOAT); + var y = tf.identity(x); + var dy_indices = tf.placeholder(TF_DataType.TF_INT32); + var dy_values = tf.placeholder(TF_DataType.TF_FLOAT); + var dy = new IndexedSlices(dy_values, dy_indices); + + var dx = tf.gradients(new[] { y }, new[] { x }, grad_ys: new[] { dy })[0]; + // The IndexedSlices gradient of tf.identity is the identity map. + using (var sess = self.cached_session()) + { + var feed_dict = new FeedItem[] + { + ( x, new Tensor(new float[] { 1.0f }) ), + (dy_indices, new Tensor(new int[] { 0 })), + (dy_values, new Tensor(new float[] { 2.0f })) + }; + var result = sess.run(new[] { dx, dy }, feed_dict); + var vdx = result[0]; + var vdy = result[1]; + self.assertEqual(vdx, vdy); + } + */ - //def testSingletonIndexedSlices(self): - // with ops.Graph().as_default(): - // x = array_ops.placeholder(dtypes.float32) - // y = array_ops.identity(x) - // dy = ops.IndexedSlices( - // array_ops.placeholder(dtypes.float32), - // array_ops.placeholder(dtypes.int32)) - // dx, = gradients.gradients(y, x, grad_ys=dy) - // # The IndexedSlices gradient of tf.identity is the identity map. - // with self.cached_session() as sess: - // vdx, vdy = sess.run( - // [dx, dy], feed_dict={x: [1.0], dy.indices: [0], dy.values: [2.0]}) - // self.assertEqual(vdx, vdy) } [Ignore("TODO")] @@ -576,26 +586,25 @@ public void testVariableRefGradient() // self.assertIsNotNone(gradient) } - [Ignore("TODO")] [TestMethod] public void testDependentYs() { - //@test_util.run_v1_only("b/120545219") - //def testDependentYs(self): - // with self.cached_session(): - // x = constant_op.constant(3.0) - // y = math_ops.square(x) - // y1 = math_ops.square(y) - // y2 = math_ops.square(y1) - // g = gradients.gradients([y, y2], x) - // self.assertAllClose(17502.0, g[0].eval()) - // g = gradients.gradients(y + y2, x) - // self.assertAllClose(17502.0, g[0].eval()) - // z = array_ops.identity(y) - // z2 = array_ops.identity(y2) - // g = gradients.gradients([z, z2], x) - // self.assertAllClose(17502.0, g[0].eval()) - + //TODO: @test_util.run_v1_only("b/120545219") + using (self.cached_session()) + { + var x = constant_op.constant(3.0); + var y = math_ops.square(x); + var y1 = math_ops.square(y); + var y2 = math_ops.square(y1); + var g = tf.gradients(new[] { y, y2 }, new[] { x }); + self.assertAllClose(17502.0, g[0].eval()); + g = tf.gradients(y + y2, x); + self.assertAllClose(17502.0, g[0].eval()); + var z = array_ops.identity(y); + var z2 = array_ops.identity(y2); + g = tf.gradients(new[] { z, z2 }, new[] { x }); + self.assertAllClose(17502.0, g[0].eval()); + } } [Ignore("TODO")] @@ -603,75 +612,132 @@ public void testDependentYs() public void testPartialDerivatives() { - //@test_util.run_v1_only("b/120545219") - //def testPartialDerivatives(self): - // with self.cached_session(): - // x = constant_op.constant(1.) - // y = 2 * x - // z = x + y - // totalg = gradients.gradients(z, [x, y]) - // self.assertEqual([3.0, 1.0], [g.eval() for g in totalg]) - // partialg = gradients.gradients(z, [x, y], stop_gradients=[x, y]) - // self.assertEqual([1.0, 1.0], [g.eval() for g in partialg]) + //TODO: @test_util.run_v1_only("b/120545219") + using (self.cached_session()) + { + var x = tf.constant(1.0); + var y = 2 * x; + var z = x + y; + var totalg = tf.gradients(z, new[] { x, y }); + self.assertEqual(new[] { 3.0, 1.0 }, totalg.Select(g => g.eval())); + var partialg = tf.gradients(z, new[] { x, y }, stop_gradients: new[] { x, y }); + self.assertEqual(new[] { 1.0, 1.0 }, partialg.Select(g => g.eval())); + } } - [Ignore("TODO")] + private struct Case + { + public Tensor[] grad1; + public Tensor[] grad2; + public string constants; + public string variables; + } + + [Ignore("FIXME")] [TestMethod] public void testStopGradients() { + + //TODO: @test_util.run_v1_only("b/120545219") + Dictionary makeGraph(RandomizedImpl rng, string stop_gradients) + { + Tensor functionOf(Tensor[] xs, int k) + { + var shape = new Shape(k, k); + // TODO: replace by DefaultIfEmpty() before Aggregate(). + if (!xs.Any()) + { + return rng.random(shape).astype(np.float32); + } + return xs.Select(x => gen_math_ops.mat_mul(rng.random(shape).astype(np.float32), x)) + .Aggregate((t1, t2) => t1 + t2) + + rng.random(shape).astype(np.float32); + } + var a = functionOf(Array.Empty(), 3); + if (stop_gradients.Contains('a')) a = array_ops.stop_gradient(a); + var b = functionOf(new Tensor[] { a }, 3); + if (stop_gradients.Contains('b')) b = array_ops.stop_gradient(b); + var c = functionOf(new Tensor[] { a, b }, 3); + if (stop_gradients.Contains('c')) c = array_ops.stop_gradient(c); + var d = functionOf(new Tensor[] { b, c }, 3); + if (stop_gradients.Contains('d')) d = array_ops.stop_gradient(d); - //@test_util.run_v1_only("b/120545219") - //def testStopGradients(self): - // def _MakeGraph(rng, stop_gradients=()): - // def _FunctionOf(xs, k=3): - // return ops.convert_to_tensor( - // sum(math_ops.matmul(rng.rand(k, k), x) for x in xs) - // + rng.rand(k, k)) - - // a = _FunctionOf([]) - // if "a" in stop_gradients: a = array_ops.stop_gradient(a) - // b = _FunctionOf([a]) - // if "b" in stop_gradients: b = array_ops.stop_gradient(b) - // c = _FunctionOf([a, b]) - // if "c" in stop_gradients: c = array_ops.stop_gradient(c) - // d = _FunctionOf([b, c]) - // if "d" in stop_gradients: d = array_ops.stop_gradient(d) - // return dict(a=a, b=b, c=c, d=d) - - // def _Gradients(ys, xs, **kwargs): - // dydxs = gradients.gradients(ys, xs, **kwargs) - // dydxs = [0. * x if dydx is None else dydx - // for x, dydx in zip(xs, dydxs)] - // return dydxs - // seed = np.random.randint(1000) - // cases = [] - // subsets = [""] + "a b c d ab ac ad bc bd cd abc abd acd bcd abcd".split() - // graph = _MakeGraph(np.random.RandomState(seed)) - // for constants in subsets: - // graph_with_stops = _MakeGraph(np.random.RandomState(seed), constants) - // for variables_ in subsets: - // # compute the gradient when stopped using tf.stop_gradients - // grad1 = _Gradients([graph_with_stops["d"]], - // [graph_with_stops[v] for v in variables_]) - // # compute the gradient when stopped using the stop_gradients kwarg - // grad2 = _Gradients([graph["d"]], - // [graph[v] for v in variables_], - // stop_gradients=[graph[v] for v in constants]) - // cases.append(dict(grad1=grad1, grad2=grad2, - // constants=constants, variables=variables_)) - - // # evaluate all tensors in one call to session.run for speed - // with self.cached_session() as sess: - // results = sess.run([(case["grad1"], case["grad2"]) for case in cases]) - - // for (npgrad1, npgrad2), case in zip(results, cases): - // for a, b in zip(npgrad1, npgrad2): - // np.testing.assert_allclose(a, b) + return new Dictionary + { + { 'a', a }, + { 'b', b }, + { 'c', c }, + { 'd', d } + }; + } + Tensor[] gradients(Tensor[] ys, Tensor[] xs, Tensor[] stop_gradients = null) + { + var dydxs = tf.gradients(ys, xs, stop_gradients); + dydxs = dydxs.Select((dydx, i) => dydx == null ? xs[i] * 0 : dydx).ToArray(); + return dydxs; + } + + var seed = np.random.randint(1000); + // TODO: remove next line when np.random.RandomState implemented. + tf.set_random_seed(seed); + var cases = new List(); + // TODO: add "" case. + var subsets = new List { "" }.Concat("a b c d ab ac ad bc bd cd abc abd acd bcd abcd".Split()); + // TODO: pass np.random.RandomState(seed) instead of np.random + var graph = makeGraph(np.random, string.Empty); + foreach (var constants in subsets) + { + var graphWithStops = makeGraph(np.random, constants); + foreach (var variables_ in subsets) + { + // compute the gradient when stopped using tf.stop_gradients + var grad1 = gradients( + new[] { graphWithStops['d'] }, + variables_.ToCharArray().Select(v => graphWithStops[v]).ToArray() + ); + // compute the gradient when stopped using the stop_gradients from args + var grad2 = gradients( + new[] { graph['d'] }, + variables_.ToCharArray().Select(v => graph[v]).ToArray(), + constants.ToCharArray().Select(c => graph[c]).DefaultIfEmpty(null)?.ToArray() + ); + cases.Add(new Case + { + grad1 = grad1, + grad2 = grad2, + variables = variables_, + constants = constants, + }) ; + } + } + + // evaluate all tensors in one call to session.run for speed + using (var sess = self.cached_session()) + { + var results = sess.run( + cases.Select(case_ => ( + case_.grad1, + case_.grad2 + )).ToArray() + ); + + foreach (var (result, case_) in results.Zip(cases)) + { + var npgrad1 = result[0]; + var npgrad2 = result[1]; + foreach (var (a, b) in npgrad1.Zip(npgrad2)) + { + self.assertAllClose(a, b); + } + } + } } - [Ignore("TODO")] + + + [Ignore("TODO: Unconnected gradients are not implemented")] [TestMethod] public void testUnconnectedGradientsNoneUnconnectedGradients() { @@ -686,12 +752,10 @@ public void testUnconnectedGradientsNoneUnconnectedGradients() // self.assertIsNone(grad[0]) } - [Ignore("TODO")] + [Ignore("TODO: Unconnected gradients are not implemented")] [TestMethod] public void testUnconnectedGradientsZerosUnconnectedGradients() { - - //def testUnconnectedGradientsZerosUnconnectedGradients(self): // with ops.Graph().as_default(): // x = constant(1.0, shape=[2, 2]) @@ -700,15 +764,21 @@ public void testUnconnectedGradientsZerosUnconnectedGradients() // [y], [x], unconnected_gradients="zero") // with self.cached_session() as sess: // self.assertAllEqual([[0.0, 0.0], [0.0, 0.0]], self.evaluate(grads)[0]) + + // tf.Graph().as_default(); + // var x = tf.constant(1.0, shape: new long[] { 2, 2 }); + // var y = tf.constant(3.0, shape: new long[] { 3, 1 }); + // var grads = tf.gradients(new[] { y }, new[] { x }, unconnected_gradients: "zero"); + // using (self.cached_session()) + // { + // self.assertAllEqual(new[,] { { 0.0, 0.0 }, { 0.0, 0.0 } }, self.evaluate(grads)[0]); + // } } - [Ignore("TODO")] + [Ignore("TODO: Unconnected gradients are not implemented")] [TestMethod] public void testUnconnectedGradientsZeroConnectedGradients() { - - - //def testUnconnectedGradientsZeroConnectedGradients(self): // with ops.Graph().as_default(): // x = constant(1.0) @@ -717,9 +787,19 @@ public void testUnconnectedGradientsZeroConnectedGradients() // [y], [x], unconnected_gradients="zero") // with self.cached_session() as sess: // self.assertEquals(3.0, self.evaluate(grad)[0]) + + // tf.Graph().as_default(); + + // var x = tf.constant(1.0f); + // var y = x * 3.0f; + // var grad = tf.gradients(new [] { y }, new [] { x }, unconnected_gradients: "zero"); + // using (var sess = tf.Session()) + // { + // self.assertEquals(3.0, self.evaluate(grad)[0]); + // } } - [Ignore("TODO")] + [Ignore("TODO: Unconnected gradients are not implemented")] [TestMethod] public void testUnknownUnconnectedGradientsValueGiven() { @@ -730,15 +810,6 @@ public void testUnknownUnconnectedGradientsValueGiven() // with self.assertRaisesRegexp( // ValueError, "Unknown value for unconnected_gradients: 'nonsense'"): // gradients.gradients([y], [x], unconnected_gradients="nonsense") - } - - - - /* - - - - */ } } diff --git a/test/TensorFlowNET.Graph.UnitTest/ImageTest.cs b/test/TensorFlowNET.Graph.UnitTest/ImageTest.cs index c42445cf1..127b65bf6 100644 --- a/test/TensorFlowNET.Graph.UnitTest/ImageTest.cs +++ b/test/TensorFlowNET.Graph.UnitTest/ImageTest.cs @@ -3,6 +3,8 @@ using System.Linq; using Tensorflow; using static Tensorflow.Binding; +using System; +using System.IO; namespace TensorFlowNET.UnitTest { @@ -22,13 +24,86 @@ public void Initialize() contents = tf.io.read_file(imgPath); } + [TestMethod] + public void adjust_contrast() + { + var input = np.array(0f, 1f, 2f, 3f, 4f, 5f, 6f, 7f, 8f); + var image = tf.reshape(input, new int[] { 3, 3, 1 }); + + var init = tf.global_variables_initializer(); + var sess = tf.Session(); + sess.run(init); + var adjust_contrast = tf.image.adjust_contrast(image, 2.0f); + var result = sess.run(adjust_contrast); + var res = np.array(-4f, -2f, 0f, 2f, 4f, 6f, 8f, 10f, 12f).reshape((3,3,1)); + Assert.AreEqual(result.numpy(), res); + } + + [Ignore] + [TestMethod] + public void adjust_hue() + { + var image = tf.constant(new int[] {1,2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,16,17,18}); + image = tf.reshape(image, new int[] { 3, 2, 3 }); + var adjusted_image = tf.image.adjust_hue(image, 0.2f); + var res = tf.constant(new int[] {2,1,3, 4, 5, 6,8,7,9,11,10,12,14,13,15,17,16,18}); + res = tf.reshape(res,(3,2,3)); + Assert.AreEqual(adjusted_image, res); + } + + [TestMethod] + public void combined_non_max_suppression() + { + var boxesX = tf.constant(new float[,] { { 200, 100, 150, 100 }, { 220, 120, 150, 100 }, { 190, 110, 150, 100 }, { 210, 112, 150, 100 } }); + var boxes1 = tf.reshape(boxesX, (1, 4, 1, 4)); + var scoresX = tf.constant(new float[,] { { 0.2f, 0.7f, 0.1f }, { 0.1f, 0.8f, 0.1f }, { 0.3f, 0.6f, 0.1f }, { 0.05f, 0.9f, 0.05f } }); + var scores1 = tf.reshape(scoresX, (1, 4, 3)); + + var init = tf.global_variables_initializer(); + var sess = tf.Session(); + sess.run(init); + + var (boxes, scores, classes, valid_detections) = tf.image.combined_non_max_suppression(boxes1, scores1, 10, 10, 0.5f, 0.2f, clip_boxes: false); + var result = sess.run((boxes, scores, classes, valid_detections)); + + var boxes_gt = tf.constant(new float[,] { { 210f, 112f, 150f, 100f }, { 200f, 100f, 150f, 100f }, { 190f, 110f, 150f, 100f }, + { 0f, 0f, 0f, 0f},{ 0f, 0f, 0f, 0f},{ 0f, 0f, 0f, 0f},{ 0f, 0f, 0f , 0f},{ 0f, 0f, 0f, 0f},{ 0f , 0f, 0f, 0f},{ 0f, 0f, 0f, 0f} }); + boxes_gt = tf.reshape(boxes_gt, (1, 10, 4)); + Assert.AreEqual(result.Item1.numpy(), boxes_gt.numpy()); + var scores_gt = tf.constant(new float[,] { { 0.9f, 0.7f, 0.3f, 0f, 0f, 0f, 0f, 0f, 0f, 0f } }); + scores_gt = tf.reshape(scores_gt, (1, 10)); + Assert.AreEqual(result.Item2.numpy(), scores_gt.numpy()); + var classes_gt = tf.constant(new float[,] { { 1f, 1f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f } }); + classes_gt = tf.reshape(classes_gt, (1, 10)); + Assert.AreEqual(result.Item3.numpy(), classes_gt.numpy()); + var valid_detections_gt = tf.constant(new int[,] { { 3 } }); + valid_detections_gt = tf.reshape(valid_detections_gt, (1)); + Assert.AreEqual(result.Item4.numpy(), valid_detections_gt.numpy()); + } + + [TestMethod] + public void crop_and_resize() + { + int BATCH_SIZE = 1; + int NUM_BOXES = 5; + int IMAGE_HEIGHT = 256; + int IMAGE_WIDTH = 256; + int CHANNELS = 3; + var crop_size = tf.constant(new int[] { 24, 24 }); + var image = tf.random.uniform((BATCH_SIZE, IMAGE_HEIGHT, IMAGE_WIDTH, CHANNELS)); + var boxes = tf.random.uniform((NUM_BOXES, 4)); + var box_ind = tf.random.uniform((NUM_BOXES), minval: 0, maxval: BATCH_SIZE, dtype: TF_DataType.TF_INT32); + var output = tf.image.crop_and_resize(image, boxes, box_ind, crop_size); + Assert.AreEqual((5,24,24,3), output.shape); + } + [TestMethod] public void decode_image() { var img = tf.image.decode_image(contents); Assert.AreEqual(img.name, "decode_image/DecodeImage:0"); } - + [TestMethod] public void resize_image() { @@ -90,5 +165,94 @@ public void TestCropAndResize() Assert.AreEqual(result.size, 16ul); Assert.AreEqual(result[0, 0, 0, 0], 12f); } + + [TestMethod] + public void ImageSaveTest() + { + var imgPath = TestHelper.GetFullPathFromDataDir("img001.bmp"); + var jpegImgPath = TestHelper.GetFullPathFromDataDir("img001.jpeg"); + var pngImgPath = TestHelper.GetFullPathFromDataDir("img001.png"); + + File.Delete(jpegImgPath); + File.Delete(pngImgPath); + + var contents = tf.io.read_file(imgPath); + var bmp = tf.image.decode_image(contents); + Assert.AreEqual(bmp.name, "decode_image/DecodeImage:0"); + + var jpeg = tf.image.encode_jpeg(bmp); + var op1 = tf.io.write_file(jpegImgPath, jpeg); + + var png = tf.image.encode_png(bmp); + var op2 = tf.io.write_file(pngImgPath, png); + + this.session().run(op1); + this.session().run(op2); + + Assert.IsTrue(File.Exists(jpegImgPath), "not find file:" + jpegImgPath); + Assert.IsTrue(File.Exists(pngImgPath), "not find file:" + pngImgPath); + + // 如果要测试图片正确性,需要注释下面两行代码 + File.Delete(jpegImgPath); + File.Delete(pngImgPath); + } + + [TestMethod] + public void ImageFlipTest() + { + var imgPath = TestHelper.GetFullPathFromDataDir("img001.bmp"); + + var contents = tf.io.read_file(imgPath); + var bmp = tf.image.decode_image(contents); + + // 左右翻转 + var lrImgPath = TestHelper.GetFullPathFromDataDir("img001_lr.png"); + File.Delete(lrImgPath); + + var lr = tf.image.flip_left_right(bmp); + var png = tf.image.encode_png(lr); + var op = tf.io.write_file(lrImgPath, png); + this.session().run(op); + + Assert.IsTrue(File.Exists(lrImgPath), "not find file:" + lrImgPath); + + // 上下翻转 + var updownImgPath = TestHelper.GetFullPathFromDataDir("img001_updown.png"); + File.Delete(updownImgPath); + + var updown = tf.image.flip_up_down(bmp); + var pngupdown = tf.image.encode_png(updown); + var op2 = tf.io.write_file(updownImgPath, pngupdown); + this.session().run(op2); + Assert.IsTrue(File.Exists(updownImgPath)); + + + // 暂时先人工观测图片是否翻转,观测时需要删除下面这两行代码 + File.Delete(lrImgPath); + File.Delete(updownImgPath); + + // 多图翻转 + // 目前直接通过 bmp 拿到 shape ,这里先用默认定义图片大小来构建了 + var mImg = tf.stack(new[] { bmp, lr }, axis:0); + print(mImg.shape); + + var up2 = tf.image.flip_up_down(mImg); + + var updownImgPath_m1 = TestHelper.GetFullPathFromDataDir("img001_m_ud.png"); // 直接上下翻转 + File.Delete(updownImgPath_m1); + + var img001_updown_m2 = TestHelper.GetFullPathFromDataDir("img001_m_lr_ud.png"); // 先左右再上下 + File.Delete(img001_updown_m2); + + var png2 = tf.image.encode_png(up2[0]); + tf.io.write_file(updownImgPath_m1, png2); + + png2 = tf.image.encode_png(up2[1]); + tf.io.write_file(img001_updown_m2, png2); + + // 如果要测试图片正确性,需要注释下面两行代码 + File.Delete(updownImgPath_m1); + File.Delete(img001_updown_m2); + } } } diff --git a/test/TensorFlowNET.Graph.UnitTest/PythonTest.cs b/test/TensorFlowNET.Graph.UnitTest/PythonTest.cs deleted file mode 100644 index 513791933..000000000 --- a/test/TensorFlowNET.Graph.UnitTest/PythonTest.cs +++ /dev/null @@ -1,308 +0,0 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; -using Newtonsoft.Json.Linq; -using Tensorflow.NumPy; -using System; -using System.Collections; -using System.Linq; -using Tensorflow; -using static Tensorflow.Binding; - -namespace TensorFlowNET.UnitTest -{ - /// - /// Use as base class for test classes to get additional assertions - /// - public class PythonTest - { - #region python compatibility layer - protected PythonTest self { get => this; } - protected int None => -1; - #endregion - - #region pytest assertions - - public void assertItemsEqual(ICollection given, ICollection expected) - { - if (given is Hashtable && expected is Hashtable) - { - Assert.AreEqual(JObject.FromObject(expected).ToString(), JObject.FromObject(given).ToString()); - return; - } - Assert.IsNotNull(expected); - Assert.IsNotNull(given); - var e = expected.OfType().ToArray(); - var g = given.OfType().ToArray(); - Assert.AreEqual(e.Length, g.Length, $"The collections differ in length expected {e.Length} but got {g.Length}"); - for (int i = 0; i < e.Length; i++) - { - /*if (g[i] is NDArray && e[i] is NDArray) - assertItemsEqual((g[i] as NDArray).GetData(), (e[i] as NDArray).GetData()); - else*/ - if (e[i] is ICollection && g[i] is ICollection) - assertEqual(g[i], e[i]); - else - Assert.AreEqual(e[i], g[i], $"Items differ at index {i}, expected {e[i]} but got {g[i]}"); - } - } - - public void assertAllEqual(ICollection given, ICollection expected) - { - assertItemsEqual(given, expected); - } - - public void assertFloat32Equal(float expected, float actual, string msg) - { - float eps = 1e-6f; - Assert.IsTrue(Math.Abs(expected - actual) < eps * Math.Max(1.0f, Math.Abs(expected)), $"{msg}: expected {expected} vs actual {actual}"); - } - - public void assertFloat64Equal(double expected, double actual, string msg) - { - double eps = 1e-16f; - Assert.IsTrue(Math.Abs(expected - actual) < eps * Math.Max(1.0f, Math.Abs(expected)), $"{msg}: expected {expected} vs actual {actual}"); - } - - public void assertEqual(object given, object expected) - { - /*if (given is NDArray && expected is NDArray) - { - assertItemsEqual((given as NDArray).GetData(), (expected as NDArray).GetData()); - return; - }*/ - if (given is Hashtable && expected is Hashtable) - { - Assert.AreEqual(JObject.FromObject(expected).ToString(), JObject.FromObject(given).ToString()); - return; - } - if (given is ICollection && expected is ICollection) - { - assertItemsEqual(given as ICollection, expected as ICollection); - return; - } - if (given is float && expected is float) - { - assertFloat32Equal((float)expected, (float)given, ""); - return; - } - if (given is double && expected is double) - { - assertFloat64Equal((double)expected, (double)given, ""); - return; - } - Assert.AreEqual(expected, given); - } - - public void assertEquals(object given, object expected) - { - assertEqual(given, expected); - } - - public void assert(object given) - { - if (given is bool) - Assert.IsTrue((bool)given); - Assert.IsNotNull(given); - } - - public void assertIsNotNone(object given) - { - Assert.IsNotNull(given); - } - - public void assertFalse(bool cond) - { - Assert.IsFalse(cond); - } - - public void assertTrue(bool cond) - { - Assert.IsTrue(cond); - } - - public void assertAllClose(NDArray array1, NDArray array2, double eps = 1e-5) - { - Assert.IsTrue(np.allclose(array1, array2, rtol: eps)); - } - - public void assertAllClose(double value, NDArray array2, double eps = 1e-5) - { - var array1 = np.ones_like(array2) * value; - // Assert.IsTrue(np.allclose(array1, array2, rtol: eps)); - } - - public void assertProtoEquals(object toProto, object o) - { - throw new NotImplementedException(); - } - - #endregion - - #region tensor evaluation and test session - - //protected object _eval_helper(Tensor[] tensors) - //{ - // if (tensors == null) - // return null; - // return nest.map_structure(self._eval_tensor, tensors); - //} - - protected object _eval_tensor(object tensor) - { - if (tensor == null) - return None; - //else if (callable(tensor)) - // return self._eval_helper(tensor()) - else - { - try - { - //TODO: - // if sparse_tensor.is_sparse(tensor): - // return sparse_tensor.SparseTensorValue(tensor.indices, tensor.values, - // tensor.dense_shape) - //return (tensor as Tensor).numpy(); - } - catch (Exception) - { - throw new ValueError("Unsupported type: " + tensor.GetType()); - } - return null; - } - } - - /// - /// This function is used in many original tensorflow unit tests to evaluate tensors - /// in a test session with special settings (for instance constant folding off) - /// - /// - public T evaluate(Tensor tensor) - { - object result = null; - // if context.executing_eagerly(): - // return self._eval_helper(tensors) - // else: - { - var sess = tf.Session(); - var ndarray = tensor.eval(sess); - if (typeof(T) == typeof(double)) - { - double x = ndarray; - result = x; - } - else if (typeof(T) == typeof(int)) - { - int x = ndarray; - result = x; - } - else - { - result = ndarray; - } - - return (T)result; - } - } - - - public Session cached_session() - { - throw new NotImplementedException(); - } - - //Returns a TensorFlow Session for use in executing tests. - public Session session(Graph graph = null, object config = null, bool use_gpu = false, bool force_gpu = false) - { - //Note that this will set this session and the graph as global defaults. - - //Use the `use_gpu` and `force_gpu` options to control where ops are run.If - //`force_gpu` is True, all ops are pinned to `/device:GPU:0`. Otherwise, if - //`use_gpu` is True, TensorFlow tries to run as many ops on the GPU as - //possible.If both `force_gpu and `use_gpu` are False, all ops are pinned to - //the CPU. - - //Example: - //```python - //class MyOperatorTest(test_util.TensorFlowTestCase): - // def testMyOperator(self): - // with self.session(use_gpu= True): - // valid_input = [1.0, 2.0, 3.0, 4.0, 5.0] - // result = MyOperator(valid_input).eval() - // self.assertEqual(result, [1.0, 2.0, 3.0, 5.0, 8.0] - // invalid_input = [-1.0, 2.0, 7.0] - // with self.assertRaisesOpError("negative input not supported"): - // MyOperator(invalid_input).eval() - //``` - - //Args: - // graph: Optional graph to use during the returned session. - // config: An optional config_pb2.ConfigProto to use to configure the - // session. - // use_gpu: If True, attempt to run as many ops as possible on GPU. - // force_gpu: If True, pin all ops to `/device:GPU:0`. - - //Yields: - // A Session object that should be used as a context manager to surround - // the graph building and execution code in a test case. - - Session s = null; - //if (context.executing_eagerly()) - // yield None - //else - //{ - s = self._create_session(graph, config, force_gpu); - //} - return s.as_default(); - } - - // See session() for details. - private Session _create_session(Graph graph, object cfg, bool forceGpu) - { - var prepare_config = new Func((config) => - { - // """Returns a config for sessions. - // Args: - // config: An optional config_pb2.ConfigProto to use to configure the - // session. - // Returns: - // A config_pb2.ConfigProto object. - - //TODO: config - - // # use_gpu=False. Currently many tests rely on the fact that any device - // # will be used even when a specific device is supposed to be used. - // allow_soft_placement = not force_gpu - // if config is None: - // config = config_pb2.ConfigProto() - // config.allow_soft_placement = allow_soft_placement - // config.gpu_options.per_process_gpu_memory_fraction = 0.3 - // elif not allow_soft_placement and config.allow_soft_placement: - // config_copy = config_pb2.ConfigProto() - // config_copy.CopyFrom(config) - // config = config_copy - // config.allow_soft_placement = False - // # Don't perform optimizations for tests so we don't inadvertently run - // # gpu ops on cpu - // config.graph_options.optimizer_options.opt_level = -1 - // # Disable Grappler constant folding since some tests & benchmarks - // # use constant input and become meaningless after constant folding. - // # DO NOT DISABLE GRAPPLER OPTIMIZERS WITHOUT CONSULTING WITH THE - // # GRAPPLER TEAM. - // config.graph_options.rewrite_options.constant_folding = ( - // rewriter_config_pb2.RewriterConfig.OFF) - // config.graph_options.rewrite_options.pin_to_host_optimization = ( - // rewriter_config_pb2.RewriterConfig.OFF) - return config; - }); - //TODO: use this instead of normal session - //return new ErrorLoggingSession(graph = graph, config = prepare_config(config)) - return new Session(graph);//, config = prepare_config(config)) - } - - #endregion - - public void AssetSequenceEqual(T[] a, T[] b) - { - Assert.IsTrue(Enumerable.SequenceEqual(a, b)); - } - } -} diff --git a/test/TensorFlowNET.Graph.UnitTest/TensorFlowNET.Graph.UnitTest.csproj b/test/TensorFlowNET.Graph.UnitTest/TensorFlowNET.Graph.UnitTest.csproj index 1385f8611..40dd53f74 100644 --- a/test/TensorFlowNET.Graph.UnitTest/TensorFlowNET.Graph.UnitTest.csproj +++ b/test/TensorFlowNET.Graph.UnitTest/TensorFlowNET.Graph.UnitTest.csproj @@ -24,9 +24,9 @@ - - - + + + all runtime; build; native; contentfiles; analyzers; buildtransitive @@ -34,8 +34,9 @@ - + + diff --git a/test/TensorFlowNET.Keras.UnitTest/Assets/lstm_from_sequential/fingerprint.pb b/test/TensorFlowNET.Keras.UnitTest/Assets/lstm_from_sequential/fingerprint.pb new file mode 100644 index 000000000..c37cc37bd --- /dev/null +++ b/test/TensorFlowNET.Keras.UnitTest/Assets/lstm_from_sequential/fingerprint.pb @@ -0,0 +1 @@ +̟땐͉ Σ(ռ2 \ No newline at end of file diff --git a/test/TensorFlowNET.Keras.UnitTest/Assets/lstm_from_sequential/keras_metadata.pb b/test/TensorFlowNET.Keras.UnitTest/Assets/lstm_from_sequential/keras_metadata.pb new file mode 100644 index 000000000..5fe8f1a65 --- /dev/null +++ b/test/TensorFlowNET.Keras.UnitTest/Assets/lstm_from_sequential/keras_metadata.pb @@ -0,0 +1,7 @@ + +&root"_tf_keras_sequential*&{"name": "sequential", "trainable": true, "expects_training_arg": true, "dtype": "float32", "batch_input_shape": null, "must_restore_from_config": false, "preserve_input_structure_in_config": false, "autocast": false, "class_name": "Sequential", "config": {"name": "sequential", "layers": [{"class_name": "InputLayer", "config": {"batch_input_shape": {"class_name": "__tuple__", "items": [null, 5, 3]}, "dtype": "float32", "sparse": false, "ragged": false, "name": "input_1"}}, {"class_name": "LSTM", "config": {"name": "lstm", "trainable": true, "dtype": "float32", "return_sequences": false, "return_state": false, "go_backwards": false, "stateful": false, "unroll": false, "time_major": false, "units": 32, "activation": "tanh", "recurrent_activation": "sigmoid", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}, "shared_object_id": 1}, "recurrent_initializer": {"class_name": "Orthogonal", "config": {"gain": 1.0, "seed": null}, "shared_object_id": 2}, "bias_initializer": {"class_name": "Zeros", "config": {}, "shared_object_id": 3}, "unit_forget_bias": true, "kernel_regularizer": null, "recurrent_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "recurrent_constraint": null, "bias_constraint": null, "dropout": 0.0, "recurrent_dropout": 0.0, "implementation": 2}}, {"class_name": "Dense", "config": {"name": "dense", "trainable": true, "dtype": "float32", "units": 1, "activation": "sigmoid", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}}]}, "shared_object_id": 9, "input_spec": [{"class_name": "InputSpec", "config": {"dtype": null, "shape": {"class_name": "__tuple__", "items": [null, 5, 3]}, "ndim": 3, "max_ndim": null, "min_ndim": null, "axes": {}}}], "build_input_shape": {"class_name": "TensorShape", "items": [null, 5, 3]}, "is_graph_network": true, "full_save_spec": {"class_name": "__tuple__", "items": [[{"class_name": "TypeSpec", "type_spec": "tf.TensorSpec", "serialized": [{"class_name": "TensorShape", "items": [null, 5, 3]}, "float32", "input_1"]}], {}]}, "save_spec": {"class_name": "TypeSpec", "type_spec": "tf.TensorSpec", "serialized": [{"class_name": "TensorShape", "items": [null, 5, 3]}, "float32", "input_1"]}, "keras_version": "2.12.0", "backend": "tensorflow", "model_config": {"class_name": "Sequential", "config": {"name": "sequential", "layers": [{"class_name": "InputLayer", "config": {"batch_input_shape": {"class_name": "__tuple__", "items": [null, 5, 3]}, "dtype": "float32", "sparse": false, "ragged": false, "name": "input_1"}, "shared_object_id": 0}, {"class_name": "LSTM", "config": {"name": "lstm", "trainable": true, "dtype": "float32", "return_sequences": false, "return_state": false, "go_backwards": false, "stateful": false, "unroll": false, "time_major": false, "units": 32, "activation": "tanh", "recurrent_activation": "sigmoid", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}, "shared_object_id": 1}, "recurrent_initializer": {"class_name": "Orthogonal", "config": {"gain": 1.0, "seed": null}, "shared_object_id": 2}, "bias_initializer": {"class_name": "Zeros", "config": {}, "shared_object_id": 3}, "unit_forget_bias": true, "kernel_regularizer": null, "recurrent_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "recurrent_constraint": null, "bias_constraint": null, "dropout": 0.0, "recurrent_dropout": 0.0, "implementation": 2}, "shared_object_id": 5}, {"class_name": "Dense", "config": {"name": "dense", "trainable": true, "dtype": "float32", "units": 1, "activation": "sigmoid", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}, "shared_object_id": 6}, "bias_initializer": {"class_name": "Zeros", "config": {}, "shared_object_id": 7}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "shared_object_id": 8}]}}, "training_config": {"loss": "binary_crossentropy", "metrics": [[{"class_name": "MeanMetricWrapper", "config": {"name": "accuracy", "dtype": "float32", "fn": "binary_accuracy"}, "shared_object_id": 11}]], "weighted_metrics": null, "loss_weights": null, "optimizer_config": {"class_name": "Custom>Adam", "config": {"name": "Adam", "weight_decay": null, "clipnorm": null, "global_clipnorm": null, "clipvalue": null, "use_ema": false, "ema_momentum": 0.99, "ema_overwrite_frequency": null, "jit_compile": false, "is_legacy_optimizer": false, "learning_rate": 0.0010000000474974513, "beta_1": 0.9, "beta_2": 0.999, "epsilon": 1e-07, "amsgrad": false}}}}2 + root.layer_with_weights-0"_tf_keras_rnn_layer* {"name": "lstm", "trainable": true, "expects_training_arg": true, "dtype": "float32", "batch_input_shape": null, "stateful": false, "must_restore_from_config": false, "preserve_input_structure_in_config": false, "autocast": true, "class_name": "LSTM", "config": {"name": "lstm", "trainable": true, "dtype": "float32", "return_sequences": false, "return_state": false, "go_backwards": false, "stateful": false, "unroll": false, "time_major": false, "units": 32, "activation": "tanh", "recurrent_activation": "sigmoid", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}, "shared_object_id": 1}, "recurrent_initializer": {"class_name": "Orthogonal", "config": {"gain": 1.0, "seed": null}, "shared_object_id": 2}, "bias_initializer": {"class_name": "Zeros", "config": {}, "shared_object_id": 3}, "unit_forget_bias": true, "kernel_regularizer": null, "recurrent_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "recurrent_constraint": null, "bias_constraint": null, "dropout": 0.0, "recurrent_dropout": 0.0, "implementation": 2}, "shared_object_id": 5, "input_spec": [{"class_name": "InputSpec", "config": {"dtype": null, "shape": {"class_name": "__tuple__", "items": [null, null, 3]}, "ndim": 3, "max_ndim": null, "min_ndim": null, "axes": {}}, "shared_object_id": 12}], "build_input_shape": {"class_name": "TensorShape", "items": [null, 5, 3]}}2 +root.layer_with_weights-1"_tf_keras_layer*{"name": "dense", "trainable": true, "expects_training_arg": false, "dtype": "float32", "batch_input_shape": null, "stateful": false, "must_restore_from_config": false, "preserve_input_structure_in_config": false, "autocast": true, "class_name": "Dense", "config": {"name": "dense", "trainable": true, "dtype": "float32", "units": 1, "activation": "sigmoid", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}, "shared_object_id": 6}, "bias_initializer": {"class_name": "Zeros", "config": {}, "shared_object_id": 7}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "shared_object_id": 8, "input_spec": {"class_name": "InputSpec", "config": {"dtype": null, "shape": null, "ndim": null, "max_ndim": null, "min_ndim": 2, "axes": {"-1": 32}}, "shared_object_id": 13}, "build_input_shape": {"class_name": "TensorShape", "items": [null, 32]}}2 +root.layer_with_weights-0.cell"_tf_keras_layer*{"name": "lstm_cell", "trainable": true, "expects_training_arg": true, "dtype": "float32", "batch_input_shape": null, "stateful": false, "must_restore_from_config": false, "preserve_input_structure_in_config": false, "autocast": true, "class_name": "LSTMCell", "config": {"name": "lstm_cell", "trainable": true, "dtype": "float32", "units": 32, "activation": "tanh", "recurrent_activation": "sigmoid", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}, "shared_object_id": 1}, "recurrent_initializer": {"class_name": "Orthogonal", "config": {"gain": 1.0, "seed": null}, "shared_object_id": 2}, "bias_initializer": {"class_name": "Zeros", "config": {}, "shared_object_id": 3}, "unit_forget_bias": true, "kernel_regularizer": null, "recurrent_regularizer": null, "bias_regularizer": null, "kernel_constraint": null, "recurrent_constraint": null, "bias_constraint": null, "dropout": 0.0, "recurrent_dropout": 0.0, "implementation": 2}, "shared_object_id": 4, "build_input_shape": {"class_name": "__tuple__", "items": [null, 3]}}2 +Rroot.keras_api.metrics.0"_tf_keras_metric*{"class_name": "Mean", "name": "loss", "dtype": "float32", "config": {"name": "loss", "dtype": "float32"}, "shared_object_id": 14}2 +Sroot.keras_api.metrics.1"_tf_keras_metric*{"class_name": "MeanMetricWrapper", "name": "accuracy", "dtype": "float32", "config": {"name": "accuracy", "dtype": "float32", "fn": "binary_accuracy"}, "shared_object_id": 11}2 \ No newline at end of file diff --git a/test/TensorFlowNET.Keras.UnitTest/Assets/lstm_from_sequential/saved_model.pb b/test/TensorFlowNET.Keras.UnitTest/Assets/lstm_from_sequential/saved_model.pb new file mode 100644 index 000000000..618c800eb Binary files /dev/null and b/test/TensorFlowNET.Keras.UnitTest/Assets/lstm_from_sequential/saved_model.pb differ diff --git a/test/TensorFlowNET.Keras.UnitTest/Assets/lstm_from_sequential/variables/variables.data-00000-of-00001 b/test/TensorFlowNET.Keras.UnitTest/Assets/lstm_from_sequential/variables/variables.data-00000-of-00001 new file mode 100644 index 000000000..ea67db4f4 Binary files /dev/null and b/test/TensorFlowNET.Keras.UnitTest/Assets/lstm_from_sequential/variables/variables.data-00000-of-00001 differ diff --git a/test/TensorFlowNET.Keras.UnitTest/Assets/lstm_from_sequential/variables/variables.index b/test/TensorFlowNET.Keras.UnitTest/Assets/lstm_from_sequential/variables/variables.index new file mode 100644 index 000000000..11f13d165 Binary files /dev/null and b/test/TensorFlowNET.Keras.UnitTest/Assets/lstm_from_sequential/variables/variables.index differ diff --git a/test/TensorFlowNET.Keras.UnitTest/Callbacks/EarlystoppingTest.cs b/test/TensorFlowNET.Keras.UnitTest/Callbacks/EarlystoppingTest.cs index ac5ba15ed..29648790f 100644 --- a/test/TensorFlowNET.Keras.UnitTest/Callbacks/EarlystoppingTest.cs +++ b/test/TensorFlowNET.Keras.UnitTest/Callbacks/EarlystoppingTest.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using Tensorflow.Keras.Callbacks; using Tensorflow.Keras.Engine; +using Tensorflow.NumPy; using static Tensorflow.KerasApi; @@ -18,7 +19,7 @@ public void Earlystopping() var layers = keras.layers; var model = keras.Sequential(new List { - layers.Rescaling(1.0f / 255, input_shape: (32, 32, 3)), + layers.Rescaling(1.0f / 255, input_shape: (28, 28, 1)), layers.Conv2D(32, 3, padding: "same", activation: keras.activations.Relu), layers.MaxPooling2D(), layers.Flatten(), @@ -36,8 +37,20 @@ public void Earlystopping() var num_epochs = 3; var batch_size = 8; - var ((x_train, y_train), (x_test, y_test)) = keras.datasets.cifar10.load_data(); - x_train = x_train / 255.0f; + var data_loader = new MnistModelLoader(); + + var dataset = data_loader.LoadAsync(new ModelLoadSetting + { + TrainDir = "mnist", + OneHot = false, + ValidationSize = 59900, + }).Result; + + NDArray x1 = np.reshape(dataset.Train.Data, (dataset.Train.Data.shape[0], 28, 28, 1)); + NDArray x2 = x1; + + var x = new NDArray[] { x1, x2 }; + // define a CallbackParams first, the parameters you pass al least contain Model and Epochs. CallbackParams callback_parameters = new CallbackParams { @@ -47,10 +60,8 @@ public void Earlystopping() // define your earlystop ICallback earlystop = new EarlyStopping(callback_parameters, "accuracy"); // define a callbcaklist, then add the earlystopping to it. - var callbacks = new List(); - callbacks.add(earlystop); - - model.fit(x_train[new Slice(0, 2000)], y_train[new Slice(0, 2000)], batch_size, num_epochs, callbacks: callbacks); + var callbacks = new List{ earlystop}; + model.fit(x, dataset.Train.Labels, batch_size, num_epochs, callbacks: callbacks); } } diff --git a/test/TensorFlowNET.Keras.UnitTest/EagerModeTestBase.cs b/test/TensorFlowNET.Keras.UnitTest/EagerModeTestBase.cs index c7eab364c..635f13a54 100644 --- a/test/TensorFlowNET.Keras.UnitTest/EagerModeTestBase.cs +++ b/test/TensorFlowNET.Keras.UnitTest/EagerModeTestBase.cs @@ -33,6 +33,40 @@ public bool Equal(float[] f1, float[] f2) return ret; } + + public void AssertArray(int[] f1, int[] f2) + { + bool ret = false; + for (var i = 0; i < f1.Length; i++) + { + ret = f1[i] == f2[i]; + if (!ret) + break; + } + + if (!ret) + { + Assert.Fail($"Array not Equal:[{string.Join(",", f1)}] [{string.Join(",", f2)}]"); + } + } + + public void AssertArray(float[] f1, float[] f2) + { + bool ret = false; + var tolerance = .00001f; + for (var i = 0; i < f1.Length; i++) + { + ret = Math.Abs(f1[i] - f2[i]) <= tolerance; + if (!ret) + break; + } + + if (!ret) + { + Assert.Fail($"Array float not Equal:[{string.Join(",", f1)}] [{string.Join(",", f2)}]"); + } + } + public bool Equal(double[] d1, double[] d2) { bool ret = false; diff --git a/test/TensorFlowNET.Keras.UnitTest/InitLayerNameTest.cs b/test/TensorFlowNET.Keras.UnitTest/InitLayerNameTest.cs new file mode 100644 index 000000000..256eb69c1 --- /dev/null +++ b/test/TensorFlowNET.Keras.UnitTest/InitLayerNameTest.cs @@ -0,0 +1,33 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Tensorflow.Keras.Layers; +using static Tensorflow.Binding; +using static Tensorflow.KerasApi; + +namespace Tensorflow.Keras.UnitTest +{ + [TestClass] + public class InitLayerNameTest + { + [TestMethod] + public void RNNLayerNameTest() + { + var simpleRnnCell = keras.layers.SimpleRNNCell(1); + Assert.AreEqual("simple_rnn_cell", simpleRnnCell.Name); + var simpleRnn = keras.layers.SimpleRNN(2); + Assert.AreEqual("simple_rnn", simpleRnn.Name); + var lstmCell = keras.layers.LSTMCell(2); + Assert.AreEqual("lstm_cell", lstmCell.Name); + var lstm = keras.layers.LSTM(3); + Assert.AreEqual("lstm", lstm.Name); + } + + [TestMethod] + public void ConvLayerNameTest() + { + var conv2d = keras.layers.Conv2D(8, activation: "linear"); + Assert.AreEqual("conv2d", conv2d.Name); + var conv2dTranspose = keras.layers.Conv2DTranspose(8); + Assert.AreEqual("conv2d_transpose", conv2dTranspose.Name); + } + } +} diff --git a/test/TensorFlowNET.Keras.UnitTest/Layers/Layers.Convolution.Test.cs b/test/TensorFlowNET.Keras.UnitTest/Layers/Layers.Convolution.Test.cs index 997dcb4f6..15c6e80fe 100644 --- a/test/TensorFlowNET.Keras.UnitTest/Layers/Layers.Convolution.Test.cs +++ b/test/TensorFlowNET.Keras.UnitTest/Layers/Layers.Convolution.Test.cs @@ -1,6 +1,8 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; +using System.Linq; using Tensorflow.NumPy; using static Tensorflow.KerasApi; +using static Tensorflow.Binding; namespace Tensorflow.Keras.UnitTest.Layers { @@ -193,5 +195,128 @@ public void BasicConv2D_ksize_dilation_same() Assert.AreEqual(x.dims[2], y.shape[2]); Assert.AreEqual(filters, y.shape[3]); } + + + [TestMethod] + public void BasicDepthwiseConv2D() + { + var conv = keras.layers.DepthwiseConv2D(kernel_size:3, strides:1, activation: null, + padding:"same", depthwise_initializer: "ones"); + + var x = np.arange(2 * 9* 9* 3).reshape((2, 9, 9, 3)); + var x2 = ops.convert_to_tensor(x, TF_DataType.TF_FLOAT); + + var y = conv.Apply(x2); + + print($"input:{x2.shape} DepthwiseConv2D.out: {y.shape}"); + + + Assert.AreEqual(4, y.shape.ndim); + var arr = y.numpy().reshape((2, 9, 9, 3)); + + AssertArray(x[new int[] { 1, 1, 1 }].ToArray(), new int[] { 273, 274, 275 }); + AssertArray(arr[new int[] { 1, 1, 1 }].ToArray(), new float[] { 2457f, 2466f, 2475f }); + + var bn = keras.layers.BatchNormalization(); + var y2 = bn.Apply(y); + arr = y2.numpy().ToArray(); + + double delta = 0.0001; // 误差范围 + + Assert.AreEqual(arr[0], 59.97002f, delta); + Assert.AreEqual(arr[1], 63.96802f, delta); + } + + + [TestMethod] + public void BasicDepthwiseConv2D_strides_2() + { + var conv = keras.layers.DepthwiseConv2D(kernel_size: 3, strides: (1, 2, 2, 1), activation: null, + padding: "same", depthwise_initializer: "ones"); + + var x = np.arange(2 * 9 * 9 * 3).reshape((2, 9, 9, 3)); + var x2 = ops.convert_to_tensor(x, TF_DataType.TF_FLOAT); + + var y = conv.Apply(x2); + + print($"input:{x2.shape} DepthwiseConv2D.out: {y.shape}"); + + Assert.AreEqual(4, y.shape.ndim); + var arr = y.numpy().reshape((2, 5, 5, 3)); + + AssertArray(x[new int[] { 1, 1, 1 }].ToArray(), new int[] { 273, 274, 275 }); + AssertArray(arr[new int[] { 1, 1, 1 }].ToArray(), new float[] { 2727f, 2736f, 2745f }); + + var bn = keras.layers.BatchNormalization(); + var y2 = bn.Apply(y); + arr = y2.numpy().ToArray(); + + double delta = 0.0001; // 误差范围 + + Assert.AreEqual(arr[0], 59.97002f, delta); + Assert.AreEqual(arr[1], 63.96802f, delta); + } + + + + [TestMethod] + public void BasicDepthwiseConv2D_strides_3() + { + var conv = keras.layers.DepthwiseConv2D(kernel_size: 3, strides: 3, activation: null, + padding: "same", depthwise_initializer: "ones"); + + var x = np.arange(2 * 9 * 9 * 3).reshape((2, 9, 9, 3)); + var x2 = ops.convert_to_tensor(x, TF_DataType.TF_FLOAT); + + var y = conv.Apply(x2); + + print($"input:{x2.shape} DepthwiseConv2D.out: {y.shape}"); + + Assert.AreEqual(4, y.shape.ndim); + var arr = y.numpy().reshape((2, 3, 3, 3)); + + AssertArray(x[new int[] { 1, 1, 1 }].ToArray(), new int[] { 273, 274, 275 }); + AssertArray(arr[new int[] { 1, 1, 1 }].ToArray(), new float[] { 3267f, 3276f, 3285f }); + + var bn = keras.layers.BatchNormalization(); + var y2 = bn.Apply(y); + arr = y2.numpy().ToArray(); + + double delta = 0.0001; // 误差范围 + + Assert.AreEqual(arr[0], 269.86508f, delta); + Assert.AreEqual(arr[1], 278.8606f, delta); + + } + [TestMethod] + public void BasicDepthwiseConv2D_UseBias() + { + var conv = keras.layers.DepthwiseConv2D(kernel_size: 3, strides: 1, activation: null, + use_bias: true, padding: "same", + depthwise_initializer: "ones", + bias_initializer:"ones" + ); + + var weight = conv.get_weights(); + + var x = np.arange(9 * 9 * 3).reshape((1, 9, 9, 3)); + var x2 = ops.convert_to_tensor(x, TF_DataType.TF_FLOAT); + var y = conv.Apply(x2); + + Assert.AreEqual(4, y.shape.ndim); + var arr = y.numpy().ToArray(); + + Assert.AreEqual(arr[0], 61f); + Assert.AreEqual(arr[1], 65f); + + var bn = keras.layers.BatchNormalization(); + var y2 = bn.Apply(y); + arr = y2.numpy().ToArray(); + + double delta = 0.0001; // 误差范围 + + Assert.AreEqual(arr[0], 60.96952f, delta); + Assert.AreEqual(arr[1], 64.96752f, delta); + } } } diff --git a/test/TensorFlowNET.Keras.UnitTest/Layers/Layers.Merging.Test.cs b/test/TensorFlowNET.Keras.UnitTest/Layers/Layers.Merging.Test.cs index 36e44e482..9bc2fa767 100644 --- a/test/TensorFlowNET.Keras.UnitTest/Layers/Layers.Merging.Test.cs +++ b/test/TensorFlowNET.Keras.UnitTest/Layers/Layers.Merging.Test.cs @@ -1,4 +1,5 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; +using System.Collections.Generic; using Tensorflow.NumPy; using static Tensorflow.KerasApi; @@ -8,12 +9,16 @@ namespace Tensorflow.Keras.UnitTest.Layers public class LayersMergingTest : EagerModeTestBase { [TestMethod] - public void Concatenate() + [DataRow(1, 4, 1, 5)] + [DataRow(2, 2, 2, 5)] + [DataRow(3, 2, 1, 10)] + public void Concatenate(int axis, int shapeA, int shapeB, int shapeC) { - var x = np.arange(20).reshape((2, 2, 5)); - var y = np.arange(20, 30).reshape((2, 1, 5)); - var z = keras.layers.Concatenate(axis: 1).Apply(new Tensors(x, y)); - Assert.AreEqual((2, 3, 5), z.shape); + var x = np.arange(10).reshape((1, 2, 1, 5)); + var y = np.arange(10, 20).reshape((1, 2, 1, 5)); + var z = keras.layers.Concatenate(axis: axis).Apply(new Tensors(x, y)); + Assert.AreEqual((1, shapeA, shapeB, shapeC), z.shape); } + } } diff --git a/test/TensorFlowNET.Keras.UnitTest/Layers/Layers.Reshaping.Test.cs b/test/TensorFlowNET.Keras.UnitTest/Layers/Layers.Reshaping.Test.cs index 748544cb0..5b16cc908 100644 --- a/test/TensorFlowNET.Keras.UnitTest/Layers/Layers.Reshaping.Test.cs +++ b/test/TensorFlowNET.Keras.UnitTest/Layers/Layers.Reshaping.Test.cs @@ -1,4 +1,5 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; using Tensorflow.NumPy; using static Tensorflow.Binding; using static Tensorflow.KerasApi; @@ -18,6 +19,15 @@ public void ZeroPadding2D() Assert.AreEqual((1, 2, 3, 2), y.shape); } + [TestMethod] + public void UpSampling1D() + { + Shape input_shape = (2, 2, 3); + var x = np.arange(input_shape.size).reshape(input_shape); + var y = tf.keras.layers.UpSampling1D(size: 2).Apply(x); + Assert.AreEqual((2, 4, 3), y.shape); + } + [TestMethod] public void UpSampling2D() { diff --git a/test/TensorFlowNET.Keras.UnitTest/Layers/LayersTest.cs b/test/TensorFlowNET.Keras.UnitTest/Layers/LayersTest.cs index 3de337469..7ebb53db3 100644 --- a/test/TensorFlowNET.Keras.UnitTest/Layers/LayersTest.cs +++ b/test/TensorFlowNET.Keras.UnitTest/Layers/LayersTest.cs @@ -1,5 +1,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; using System.Collections.Generic; +using System.Linq; using Tensorflow.NumPy; using static Tensorflow.Binding; using static Tensorflow.KerasApi; @@ -108,6 +110,17 @@ public void Embedding() var output_array = model.predict(input_array); Assert.AreEqual((32, 10, 64), output_array.shape); } + [TestMethod] + public void EmbeddingGrad() + { + var inputs = keras.layers.Input(shape: new[] { 32, 10 }); + var outputs = keras.layers.Embedding(1000, 64, input_length: 10).Apply(inputs); + var model = keras.Model(inputs: inputs, outputs: outputs); + var input_array = np.random.randint(1000, size: (1, 32, 10)); + var output_array = np.random.random(size: (1, 32, 10, 64)); + model.compile("rmsprop", "mse", new[] { "accuracy" }); + model.fit(input_array, output_array); + } /// /// https://www.tensorflow.org/api_docs/python/tf/keras/layers/Dense @@ -144,17 +157,6 @@ public void EinsumDense() Assert.AreEqual(expected_output, actual_output); } - [TestMethod, Ignore("WIP")] - public void SimpleRNN() - { - var inputs = np.arange(6 * 10 * 8).reshape((6, 10, 8)).astype(np.float32); - /*var simple_rnn = keras.layers.SimpleRNN(4); - var output = simple_rnn.Apply(inputs); - Assert.AreEqual((32, 4), output.shape);*/ - var simple_rnn = tf.keras.layers.SimpleRNN(4, return_sequences: true, return_state: true); - var (whole_sequence_output, final_state) = simple_rnn.Apply(inputs); - } - [TestMethod] public void Resizing() { @@ -172,6 +174,26 @@ public void LayerNormalization() Tensor output = layer.Apply(inputs); Assert.AreEqual((5, 2), output.shape); Assert.IsTrue(output[0].numpy().Equals(new[] { -0.99998f, 0.99998f })); + + // test_layernorm_weights + Assert.AreEqual(len(layer.TrainableWeights), 2); + Assert.AreEqual(len(layer.Weights), 2); + + var beta = layer.Weights.Where(x => x.Name.StartsWith("beta")).Single(); + var gamma = layer.Weights.Where(x => x.Name.StartsWith("gamma")).Single(); + + // correctness_test + layer = keras.layers.LayerNormalization(axis: -1, epsilon: (float) 1e-12); + var x = np.random.normal(loc: 5.0f, scale: 10.0f, size: (1000, 2, 2, 2)).astype(tf.float32); + + output = layer.Apply(x); + + var y = (output - beta.numpy()) / gamma.numpy(); + + var y_mean = np.mean(y.numpy()); + var y_std = np.sqrt(np.sum(np.power(y.numpy() - np.mean(y.numpy()), 2)) / 8000); + Assert.IsTrue(tf.greater(np.array(0.1f), tf.abs(y_std - 1.0)).ToArray()[0]); + Assert.IsTrue(tf.greater(np.array(0.1f), tf.abs(y_mean)).ToArray()[0]); } /// diff --git a/test/TensorFlowNET.Keras.UnitTest/Layers/Rnn.Test.cs b/test/TensorFlowNET.Keras.UnitTest/Layers/Rnn.Test.cs new file mode 100644 index 000000000..67e2b0464 --- /dev/null +++ b/test/TensorFlowNET.Keras.UnitTest/Layers/Rnn.Test.cs @@ -0,0 +1,167 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Tensorflow.Common.Types; +using Tensorflow.Keras.ArgsDefinition; +using Tensorflow.Keras.Engine; +using Tensorflow.Keras.Layers; +using Tensorflow.Keras.Saving; +using Tensorflow.NumPy; +using Tensorflow.Train; +using static Tensorflow.Binding; +using static Tensorflow.KerasApi; + +namespace Tensorflow.Keras.UnitTest.Layers +{ + [TestClass] + public class Rnn + { + [TestMethod] + public void SimpleRNNCell() + { + var cell = tf.keras.layers.SimpleRNNCell(64, dropout: 0.5f, recurrent_dropout: 0.5f); + var h0 = new Tensors { tf.zeros(new Shape(4, 64)) }; + var x = tf.random.normal((4, 100)); + var (y, h1) = cell.Apply(inputs: x, states: h0); + var h2 = h1; + Assert.AreEqual((4, 64), y.shape); + Assert.AreEqual((4, 64), h2[0].shape); + } + + [TestMethod] + public void StackedRNNCell() + { + var inputs = tf.ones((32, 10)); + var states = new Tensors { tf.zeros((32, 4)), tf.zeros((32, 5)) }; + var cells = new IRnnCell[] { tf.keras.layers.SimpleRNNCell(4), tf.keras.layers.SimpleRNNCell(5) }; + var stackedRNNCell = tf.keras.layers.StackedRNNCells(cells); + var (output, state) = stackedRNNCell.Apply(inputs, states); + Assert.AreEqual((32, 5), output.shape); + Assert.AreEqual((32, 4), state[0].shape); + } + + [TestMethod] + public void LSTMCell() + { + var inputs = tf.ones((2, 100)); + var states = new Tensors { tf.zeros((2, 4)), tf.zeros((2, 4)) }; + var rnn = tf.keras.layers.LSTMCell(4); + var (output, new_states) = rnn.Apply(inputs, states); + Assert.AreEqual((2, 4), output.shape); + Assert.AreEqual((2, 4), new_states[0].shape); + } + + [TestMethod] + public void TrainLSTMWithMnist() + { + var input = keras.Input((784)); + var x = keras.layers.Reshape((28, 28)).Apply(input); + x = keras.layers.LSTM(50, return_sequences: true).Apply(x); + x = keras.layers.LSTM(100).Apply(x); + var output = keras.layers.Dense(10, activation: "softmax").Apply(x); + + var model = keras.Model(input, output); + model.summary(); + model.compile(keras.optimizers.Adam(), keras.losses.CategoricalCrossentropy(), new string[] { "accuracy" }); + + var data_loader = new MnistModelLoader(); + var dataset = data_loader.LoadAsync(new ModelLoadSetting + { + TrainDir = "mnist", + OneHot = true, + ValidationSize = 55000, + }).Result; + var sample_weight = np.ones(((int)dataset.Train.Data.shape[0])); + model.fit(dataset.Train.Data, dataset.Train.Labels, batch_size: 16, epochs: 1, sample_weight:sample_weight); + } + + [TestMethod] + public void SimpleRNN() + { + var input = keras.Input((784)); + var x = keras.layers.Reshape((28, 28)).Apply(input); + x = keras.layers.SimpleRNN(10).Apply(x); + var output = keras.layers.Dense(10, activation: "softmax").Apply(x); + + var model = keras.Model(input, output); + model.summary(); + model.compile(keras.optimizers.Adam(), keras.losses.CategoricalCrossentropy(), new string[] { "accuracy" }); + + var data_loader = new MnistModelLoader(); + var dataset = data_loader.LoadAsync(new ModelLoadSetting + { + TrainDir = "mnist", + OneHot = false, + ValidationSize = 58000, + }).Result; + + model.fit(dataset.Train.Data, dataset.Train.Labels, batch_size: 16, epochs: 2); + } + + [TestMethod] + public void RNNForSimpleRNNCell() + { + var inputs = tf.random.normal((32, 10, 8)); + var cell = tf.keras.layers.SimpleRNNCell(10, dropout: 0.5f, recurrent_dropout: 0.5f); + var rnn = tf.keras.layers.RNN(cell: cell); + var cgf = rnn.get_config(); + var output = rnn.Apply(inputs); + Assert.AreEqual((32, 10), output.shape); + + } + [TestMethod] + public void RNNForStackedRNNCell() + { + var inputs = tf.random.normal((32, 10, 8)); + var cells = new IRnnCell[] { tf.keras.layers.SimpleRNNCell(4), tf.keras.layers.SimpleRNNCell(5) }; + var stackedRNNCell = tf.keras.layers.StackedRNNCells(cells); + var rnn = tf.keras.layers.RNN(cell: stackedRNNCell); + var output = rnn.Apply(inputs); + Assert.AreEqual((32, 5), output.shape); + } + + [TestMethod] + public void RNNForLSTMCell() + { + var inputs = tf.ones((5, 10, 8)); + var rnn = tf.keras.layers.RNN(tf.keras.layers.LSTMCell(4)); + var output = rnn.Apply(inputs); + Console.WriteLine($"output: {output}"); + Assert.AreEqual((5, 4), output.shape); + } + + [TestMethod] + public void GRUCell() + { + var inputs = tf.random.normal((32, 10, 8)); + var rnn = tf.keras.layers.RNN(tf.keras.layers.GRUCell(4)); + var output = rnn.Apply(inputs); + Assert.AreEqual((32, 4), output.shape); + rnn = tf.keras.layers.RNN(tf.keras.layers.GRUCell(4, reset_after:false, use_bias:false)); + output = rnn.Apply(inputs); + Assert.AreEqual((32, 4), output.shape); + + } + + [TestMethod] + public void GRU() + { + var inputs = tf.ones((32, 10, 8)); + var gru = tf.keras.layers.GRU(4); + var output = gru.Apply(inputs); + Assert.AreEqual((32, 4), output.shape); + } + + [TestMethod] + public void Bidirectional() + { + var bi = tf.keras.layers.Bidirectional(keras.layers.LSTM(10, return_sequences:true)); + var inputs = tf.random.normal((32, 10, 8)); + var outputs = bi.Apply(inputs); + Assert.AreEqual((32, 10, 20), outputs.shape); + } + } +} diff --git a/test/TensorFlowNET.Keras.UnitTest/Model/ModelBuildTest.cs b/test/TensorFlowNET.Keras.UnitTest/Model/ModelBuildTest.cs index e1fe9ff4f..d4b11a9b2 100644 --- a/test/TensorFlowNET.Keras.UnitTest/Model/ModelBuildTest.cs +++ b/test/TensorFlowNET.Keras.UnitTest/Model/ModelBuildTest.cs @@ -1,5 +1,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; using static Tensorflow.Binding; +using static Tensorflow.KerasApi; namespace Tensorflow.Keras.UnitTest.Model { @@ -14,24 +16,47 @@ public void DenseBuild() var dense = tf.keras.layers.Dense(64); var output = dense.Apply(input); var model = tf.keras.Model(input, output); + model.compile(tf.keras.optimizers.Adam(), tf.keras.losses.CategoricalCrossentropy()); // one dimensions input with unknown batchsize var input_2 = tf.keras.layers.Input((60)); var dense_2 = tf.keras.layers.Dense(64); - var output_2 = dense.Apply(input_2); + var output_2 = dense_2.Apply(input_2); var model_2 = tf.keras.Model(input_2, output_2); + model_2.compile(tf.keras.optimizers.Adam(), tf.keras.losses.CategoricalCrossentropy()); // two dimensions input with specified batchsize var input_3 = tf.keras.layers.Input((17, 60), 8); var dense_3 = tf.keras.layers.Dense(64); - var output_3 = dense.Apply(input_3); + var output_3 = dense_3.Apply(input_3); var model_3 = tf.keras.Model(input_3, output_3); + model_3.compile(tf.keras.optimizers.Adam(), tf.keras.losses.CategoricalCrossentropy()); // one dimensions input with specified batchsize var input_4 = tf.keras.layers.Input((60), 8); var dense_4 = tf.keras.layers.Dense(64); - var output_4 = dense.Apply(input_4); + var output_4 = dense_4.Apply(input_4); var model_4 = tf.keras.Model(input_4, output_4); + model_4.compile(tf.keras.optimizers.Adam(), tf.keras.losses.CategoricalCrossentropy()); + } + + [TestMethod] + public void NestedSequential() + { + var block1 = keras.Sequential(new[] { + keras.layers.InputLayer((3, 3)), + keras.Sequential(new [] + { + keras.layers.Flatten(), + keras.layers.Dense(5) + } + ) + }); + block1.compile(tf.keras.optimizers.Adam(), tf.keras.losses.CategoricalCrossentropy()); + + var x = tf.ones((1, 3, 3)); + var y = block1.predict(x); + Console.WriteLine(y); } } } diff --git a/test/TensorFlowNET.Keras.UnitTest/Model/ModelLoadTest.cs b/test/TensorFlowNET.Keras.UnitTest/Model/ModelLoadTest.cs index 10db2bd11..c733537e7 100644 --- a/test/TensorFlowNET.Keras.UnitTest/Model/ModelLoadTest.cs +++ b/test/TensorFlowNET.Keras.UnitTest/Model/ModelLoadTest.cs @@ -1,8 +1,14 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; +using Microsoft.VisualStudio.TestPlatform.Utilities; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Newtonsoft.Json.Linq; +using System.Collections.Generic; using System.Linq; +using System.Xml.Linq; +using Tensorflow.Keras.Engine; using Tensorflow.Keras.Optimizers; using Tensorflow.Keras.UnitTest.Helpers; using Tensorflow.NumPy; +using static HDF.PInvoke.H5Z; using static Tensorflow.Binding; using static Tensorflow.KerasApi; @@ -79,6 +85,18 @@ public void ModelWithSelfDefinedModule() model.fit(dataset.Train.Data, dataset.Train.Labels, batch_size, num_epochs); } + [Ignore] + [TestMethod] + public void LSTMLoad() + { + var model = tf.keras.models.load_model(@"Assets/lstm_from_sequential"); + model.summary(); + model.compile(tf.keras.optimizers.Adam(), tf.keras.losses.MeanSquaredError(), new string[] { "accuracy" }); + var inputs = tf.random.normal(shape: (10, 5, 3)); + var outputs = tf.random.normal(shape: (10, 1)); + model.fit(inputs.numpy(), outputs.numpy(), batch_size: 10, epochs: 5, workers: 16, use_multiprocessing: true); + } + [Ignore] [TestMethod] public void VGG19() @@ -110,4 +128,91 @@ public void TestModelBeforeTF2_5() var model = tf.saved_model.load(@"D:\development\temp\saved_model") as Tensorflow.Keras.Engine.Model; model.summary(); } + + + [TestMethod] + public void BiasRegularizerSaveAndLoad() + { + var savemodel = keras.Sequential(new List() + { + tf.keras.layers.InputLayer((227, 227, 3)), + tf.keras.layers.Conv2D(96, (11, 11), (4, 4), activation:"relu", padding:"valid"), + tf.keras.layers.BatchNormalization(), + tf.keras.layers.MaxPooling2D((3, 3), strides:(2, 2)), + + tf.keras.layers.Conv2D(256, (5, 5), (1, 1), "same", activation: keras.activations.Relu, bias_regularizer:keras.regularizers.L1L2), + tf.keras.layers.BatchNormalization(), + + tf.keras.layers.Conv2D(256, (5, 5), (1, 1), "same", activation: keras.activations.Relu, bias_regularizer:keras.regularizers.L2), + tf.keras.layers.BatchNormalization(), + + tf.keras.layers.Conv2D(256, (5, 5), (1, 1), "same", activation: keras.activations.Relu, bias_regularizer:keras.regularizers.L1), + tf.keras.layers.BatchNormalization(), + tf.keras.layers.MaxPooling2D((3, 3), (2, 2)), + + tf.keras.layers.Flatten(), + + tf.keras.layers.Dense(1000, activation: "linear"), + tf.keras.layers.Softmax(1) + }); + + savemodel.compile(tf.keras.optimizers.Adam(), tf.keras.losses.SparseCategoricalCrossentropy(from_logits: true), new string[] { "accuracy" }); + + var num_epochs = 1; + var batch_size = 8; + + var trainDataset = new RandomDataSet(new Shape(227, 227, 3), 16); + + savemodel.fit(trainDataset.Data, trainDataset.Labels, batch_size, num_epochs); + + savemodel.save(@"./bias_regularizer_save_and_load", save_format: "tf"); + + var loadModel = tf.keras.models.load_model(@"./bias_regularizer_save_and_load"); + loadModel.summary(); + + loadModel.compile(tf.keras.optimizers.Adam(), tf.keras.losses.SparseCategoricalCrossentropy(from_logits: true), new string[] { "accuracy" }); + + var fitDataset = new RandomDataSet(new Shape(227, 227, 3), 16); + + loadModel.fit(fitDataset.Data, fitDataset.Labels, batch_size, num_epochs); + } + + + [TestMethod] + public void CreateConcatenateModelSaveAndLoad() + { + // a small demo model that is just here to see if the axis value for the concatenate method is saved and loaded. + var input_layer = tf.keras.layers.Input((8, 8, 5)); + + var conv1 = tf.keras.layers.Conv2D(2, kernel_size: 3, activation: "relu", padding: "same"/*, data_format: "_conv_1"*/).Apply(input_layer); + conv1.Name = "conv1"; + + var conv2 = tf.keras.layers.Conv2D(2, kernel_size: 3, activation: "relu", padding: "same"/*, data_format: "_conv_2"*/).Apply(input_layer); + conv2.Name = "conv2"; + + var concat1 = tf.keras.layers.Concatenate(axis: 3).Apply((conv1, conv2)); + concat1.Name = "concat1"; + + var model = tf.keras.Model(input_layer, concat1); + model.compile(tf.keras.optimizers.Adam(), tf.keras.losses.CategoricalCrossentropy()); + + model.save(@"Assets/concat_axis3_model"); + + + var tensorInput = np.arange(320).reshape((1, 8, 8, 5)).astype(TF_DataType.TF_FLOAT); + + var tensors1 = model.predict(tensorInput); + + Assert.AreEqual((1, 8, 8, 4), tensors1.shape); + + model = null; + keras.backend.clear_session(); + + var model2 = tf.keras.models.load_model(@"Assets/concat_axis3_model"); + + var tensors2 = model2.predict(tensorInput); + + Assert.AreEqual(tensors1.shape, tensors2.shape); + } + } diff --git a/test/TensorFlowNET.Keras.UnitTest/MultiInputModelTest.cs b/test/TensorFlowNET.Keras.UnitTest/MultiInputModelTest.cs index dd8ef8f91..54b76d41a 100644 --- a/test/TensorFlowNET.Keras.UnitTest/MultiInputModelTest.cs +++ b/test/TensorFlowNET.Keras.UnitTest/MultiInputModelTest.cs @@ -2,6 +2,7 @@ using System; using Tensorflow.Keras.Optimizers; using Tensorflow.NumPy; +using static Tensorflow.Binding; using static Tensorflow.KerasApi; namespace Tensorflow.Keras.UnitTest @@ -54,10 +55,91 @@ public void LeNetModel() var x = new NDArray[] { x1, x2 }; model.fit(x, dataset.Train.Labels, batch_size: 8, epochs: 3); + x1 = x1["0:8"]; + x2 = x1; + + x = new NDArray[] { x1, x2 }; + var y = dataset.Train.Labels["0:8"]; + (model as Engine.Model).evaluate(x, y); + x1 = np.ones((1, 28, 28, 1), TF_DataType.TF_FLOAT); x2 = np.zeros((1, 28, 28, 1), TF_DataType.TF_FLOAT); var pred = model.predict((x1, x2)); Console.WriteLine(pred); } + + [TestMethod] + public void LeNetModelDataset() + { + var inputs = keras.Input((28, 28, 1)); + var conv1 = keras.layers.Conv2D(16, (3, 3), activation: "relu", padding: "same").Apply(inputs); + var pool1 = keras.layers.MaxPooling2D((2, 2), 2).Apply(conv1); + var conv2 = keras.layers.Conv2D(32, (3, 3), activation: "relu", padding: "same").Apply(pool1); + var pool2 = keras.layers.MaxPooling2D((2, 2), 2).Apply(conv2); + var flat1 = keras.layers.Flatten().Apply(pool2); + + var inputs_2 = keras.Input((28, 28, 1)); + var conv1_2 = keras.layers.Conv2D(16, (3, 3), activation: "relu", padding: "same").Apply(inputs_2); + var pool1_2 = keras.layers.MaxPooling2D((4, 4), 4).Apply(conv1_2); + var conv2_2 = keras.layers.Conv2D(32, (1, 1), activation: "relu", padding: "same").Apply(pool1_2); + var pool2_2 = keras.layers.MaxPooling2D((2, 2), 2).Apply(conv2_2); + var flat1_2 = keras.layers.Flatten().Apply(pool2_2); + + var concat = keras.layers.Concatenate().Apply((flat1, flat1_2)); + var dense1 = keras.layers.Dense(512, activation: "relu").Apply(concat); + var dense2 = keras.layers.Dense(128, activation: "relu").Apply(dense1); + var dense3 = keras.layers.Dense(10, activation: "relu").Apply(dense2); + var output = keras.layers.Softmax(-1).Apply(dense3); + + var model = keras.Model((inputs, inputs_2), output); + model.summary(); + + var data_loader = new MnistModelLoader(); + + var dataset = data_loader.LoadAsync(new ModelLoadSetting + { + TrainDir = "mnist", + OneHot = false, + ValidationSize = 59900, + }).Result; + + var loss = keras.losses.SparseCategoricalCrossentropy(); + var optimizer = new Adam(0.001f); + model.compile(optimizer, loss, new string[] { "accuracy" }); + + NDArray x1 = np.reshape(dataset.Train.Data, (dataset.Train.Data.shape[0], 28, 28, 1)); + + var multiInputDataset = tf.data.Dataset.zip( + tf.data.Dataset.from_tensor_slices(x1), + tf.data.Dataset.from_tensor_slices(x1), + tf.data.Dataset.from_tensor_slices(dataset.Train.Labels) + ).batch(8); + multiInputDataset.FirstInputTensorCount = 2; + + model.fit(multiInputDataset, epochs: 3); + + x1 = x1["0:8"]; + + multiInputDataset = tf.data.Dataset.zip( + tf.data.Dataset.from_tensor_slices(x1), + tf.data.Dataset.from_tensor_slices(x1), + tf.data.Dataset.from_tensor_slices(dataset.Train.Labels["0:8"]) + ).batch(8); + multiInputDataset.FirstInputTensorCount = 2; + + (model as Engine.Model).evaluate(multiInputDataset); + + x1 = np.ones((1, 28, 28, 1), TF_DataType.TF_FLOAT); + var x2 = np.zeros((1, 28, 28, 1), TF_DataType.TF_FLOAT); + + multiInputDataset = tf.data.Dataset.zip( + tf.data.Dataset.from_tensor_slices(x1), + tf.data.Dataset.from_tensor_slices(x2) + ).batch(8); + multiInputDataset.FirstInputTensorCount = 2; + + var pred = model.predict(multiInputDataset); + Console.WriteLine(pred); + } } } diff --git a/test/TensorFlowNET.Keras.UnitTest/Tensorflow.Keras.UnitTest.csproj b/test/TensorFlowNET.Keras.UnitTest/Tensorflow.Keras.UnitTest.csproj index b964d1178..edac1c2ff 100644 --- a/test/TensorFlowNET.Keras.UnitTest/Tensorflow.Keras.UnitTest.csproj +++ b/test/TensorFlowNET.Keras.UnitTest/Tensorflow.Keras.UnitTest.csproj @@ -13,9 +13,9 @@ - - - + + + all runtime; build; native; contentfiles; analyzers; buildtransitive @@ -23,8 +23,9 @@ - + + @@ -65,6 +66,22 @@ PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + diff --git a/test/TensorFlowNET.Native.UnitTest/Lite/TfLiteTest.cs b/test/TensorFlowNET.Native.UnitTest/Lite/TfLiteTest.cs index e16655575..4d0d6d8c9 100644 --- a/test/TensorFlowNET.Native.UnitTest/Lite/TfLiteTest.cs +++ b/test/TensorFlowNET.Native.UnitTest/Lite/TfLiteTest.cs @@ -13,6 +13,7 @@ namespace Tensorflow.Native.UnitTest public class TfLiteTest { [TestMethod] + [Ignore] public void TfLiteVersion() { var ver = c_api_lite.StringPiece(c_api_lite.TfLiteVersion()); @@ -20,6 +21,7 @@ public void TfLiteVersion() } [TestMethod] + [Ignore] public unsafe void SmokeTest() { var model = c_api_lite.TfLiteModelCreateFromFile("Lite/testdata/add.bin"); @@ -85,6 +87,7 @@ public unsafe void SmokeTest() } [TestMethod] + [Ignore] public unsafe void QuantizationParamsTest() { var model = c_api_lite.TfLiteModelCreateFromFile("Lite/testdata/add_quantized.bin"); diff --git a/test/TensorFlowNET.Native.UnitTest/Tensorflow.Native.UnitTest.csproj b/test/TensorFlowNET.Native.UnitTest/Tensorflow.Native.UnitTest.csproj index 61373d2dc..c054a8707 100644 --- a/test/TensorFlowNET.Native.UnitTest/Tensorflow.Native.UnitTest.csproj +++ b/test/TensorFlowNET.Native.UnitTest/Tensorflow.Native.UnitTest.csproj @@ -44,9 +44,9 @@ - - - + + + all runtime; build; native; contentfiles; analyzers; buildtransitive @@ -54,8 +54,8 @@ - + diff --git a/test/TensorFlowNET.UnitTest/Basics/ThreadSafeTest.cs b/test/TensorFlowNET.UnitTest/Basics/ThreadSafeTest.cs new file mode 100644 index 000000000..6a633448c --- /dev/null +++ b/test/TensorFlowNET.UnitTest/Basics/ThreadSafeTest.cs @@ -0,0 +1,41 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using Tensorflow; +using static Tensorflow.Binding; + +namespace TensorFlowNET.UnitTest.Basics +{ + [TestClass] + public class ThreadSafeTest + { + [TestMethod] + public void GraphWithMultiThreads() + { + List threads = new List(); + + const int THREADS_COUNT = 5; + + for (int t = 0; t < THREADS_COUNT; t++) + { + Thread thread = new Thread(() => + { + Graph g = new Graph(); + Session session = new Session(g); + session.as_default(); + var input = tf.placeholder(tf.int32, shape: new Shape(6)); + var op = tf.reshape(input, new int[] { 2, 3 }); + }); + thread.Start(); + threads.Add(thread); + } + + threads.ForEach(t => t.Join()); + } + } +} diff --git a/test/TensorFlowNET.UnitTest/Dataset/DatasetTest.cs b/test/TensorFlowNET.UnitTest/Dataset/DatasetTest.cs index 8317346ea..183544ab6 100644 --- a/test/TensorFlowNET.UnitTest/Dataset/DatasetTest.cs +++ b/test/TensorFlowNET.UnitTest/Dataset/DatasetTest.cs @@ -1,7 +1,10 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using System; +using System.Collections.Generic; using System.Linq; +using Tensorflow.NumPy; using static Tensorflow.Binding; +using static Tensorflow.KerasApi; namespace TensorFlowNET.UnitTest.Dataset { @@ -195,5 +198,40 @@ public void Shuffle() Assert.IsFalse(allEqual); } + [Ignore] + [TestMethod] + public void GetData() + { + var vocab_size = 20000; // Only consider the top 20k words + var maxlen = 200; // Only consider the first 200 words of each movie review + var dataset = keras.datasets.imdb.load_data(num_words: vocab_size, maxlen: maxlen); + var x_train = dataset.Train.Item1; + var y_train = dataset.Train.Item2; + var x_val = dataset.Test.Item1; + var y_val = dataset.Test.Item2; + + x_train = keras.preprocessing.sequence.pad_sequences(RemoveZeros(x_train), maxlen: maxlen); + x_val = keras.preprocessing.sequence.pad_sequences(RemoveZeros(x_val), maxlen: maxlen); + print(len(x_train) + " Training sequences"); + print(len(x_val) + " Validation sequences"); + } + IEnumerable RemoveZeros(NDArray data) + { + var data_array = (int[,])data.ToMultiDimArray(); + List new_data = new List(); + for (var i = 0; i < data_array.GetLength(0); i++) + { + List new_array = new List(); + for (var j = 0; j < data_array.GetLength(1); j++) + { + if (data_array[i, j] == 0) + break; + else + new_array.Add(data_array[i, j]); + } + new_data.Add(new_array.ToArray()); + } + return new_data; + } } } diff --git a/test/TensorFlowNET.UnitTest/EagerModeTestBase.cs b/test/TensorFlowNET.UnitTest/EagerModeTestBase.cs index d08f4e505..b7b9ae128 100644 --- a/test/TensorFlowNET.UnitTest/EagerModeTestBase.cs +++ b/test/TensorFlowNET.UnitTest/EagerModeTestBase.cs @@ -20,6 +20,20 @@ public bool Equal(float f1, float f2) return Math.Abs(f1 - f2) <= tolerance; } + public bool Equal(long[] l1, long[] l2) + { + if (l1.Length != l2.Length) + return false; + + for (var i = 0; i < l1.Length; i++) + { + if (l1[i] != l2[i]) + return false; + } + + return true; + } + public bool Equal(float[] f1, float[] f2) { bool ret = false; diff --git a/test/TensorFlowNET.UnitTest/GradientTest/GradientEagerTest.cs b/test/TensorFlowNET.UnitTest/GradientTest/GradientEagerTest.cs index e41e1d617..1cfceb3e3 100644 --- a/test/TensorFlowNET.UnitTest/GradientTest/GradientEagerTest.cs +++ b/test/TensorFlowNET.UnitTest/GradientTest/GradientEagerTest.cs @@ -62,7 +62,7 @@ public void SquaredDifference_1D() // Calcute the gradient of (x1-x2)^2 // by Automatic Differentiation in Eager mode // Expected is 2*(abs(x1-x2)) - Tensor x1 = new NDArray( new float[] { 1, 3, 5, 21, 19, 17 }); + Tensor x1 = new NDArray(new float[] { 1, 3, 5, 21, 19, 17 }); Tensor x2 = new NDArray(new float[] { 29, 27, 23, 7, 11, 13 }); float[] expected = new float[] { @@ -173,5 +173,34 @@ public void ConditionalMultiply() var result = grad(x, 4); Assert.AreEqual((float)result, 4.0f); } + + [TestMethod] + public void Tile() + { + var a = tf.constant(new int[] { 1 }, TF_DataType.TF_FLOAT); + var b = tf.constant(new int[] { 2 }); + using (var tape = tf.GradientTape()) + { + tape.watch(a); + var y = tf.tile(a, b); + var grad = tape.gradient(y, a); + Assert.AreEqual((float)grad.numpy(), 2.0f); + } + } + + [TestMethod] + public void GatherNdTest() + { + var x = tf.constant(new float[,] { { 1.0f, 2.0f, 3.0f }, { 1.0f, 2.0f, 3.0f }, { 1.0f, 2.0f, 3.0f } }, dtype: TF_DataType.TF_FLOAT); + var indices = tf.constant(new int[,] { { 0, 1 }, { 1, 1 }, { 2, 1 } }, dtype: TF_DataType.TF_INT32); + using (var tape = tf.GradientTape()) + { + tape.watch(x); + var res = tf.gather_nd(x, indices); + var grad = tape.gradient(res, x); + var expected = np.array(new float[,] { { 0f, 1f, 0f }, { 0f, 1f, 0f }, { 0f, 1f, 0f } }); + Assert.IsTrue(Enumerable.SequenceEqual(grad.ToArray(), expected.ToArray())); + } + } } } diff --git a/test/TensorFlowNET.UnitTest/ManagedAPI/ArrayOpsTest.cs b/test/TensorFlowNET.UnitTest/ManagedAPI/ArrayOpsTest.cs index 6a12ed20b..e25c9779d 100644 --- a/test/TensorFlowNET.UnitTest/ManagedAPI/ArrayOpsTest.cs +++ b/test/TensorFlowNET.UnitTest/ManagedAPI/ArrayOpsTest.cs @@ -2,6 +2,8 @@ using Tensorflow.NumPy; using Tensorflow; using static Tensorflow.Binding; +using System.Linq; +using Tensorflow.Operations; namespace TensorFlowNET.UnitTest.ManagedAPI { @@ -18,7 +20,7 @@ public void Slice() var input_array = tf.constant(np.array(new int[] { 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6 }).reshape((3,2,3))); var indices = tf.constant(np.array(new int[] { 0, 2 })); - var r1 = array_ops.slice(input_array, new int[] { 1, 0, 0 }, new int[] { 1, 1, 3 }); + var r1 = array_ops.slice(input_array, ops.convert_n_to_tensor(new object[] { 1, 0, 0 }), ops.convert_n_to_tensor(new object[] { 1, 1, 3 })); Assert.AreEqual(new Shape(1,1,3), r1.shape); var r1np = r1.numpy(); Assert.AreEqual(r1np[0, 0, 0], 3); @@ -26,7 +28,7 @@ public void Slice() Assert.AreEqual(r1np[0, 0, 2], 3); - var r2 = array_ops.slice(input_array, new int[] { 1, 0, 0 }, new int[] { 1, 2, 3 }); + var r2 = array_ops.slice(input_array, ops.convert_n_to_tensor(new object[] { 1, 0, 0 }), ops.convert_n_to_tensor(new object[] { 1, 2, 3 })); Assert.AreEqual(new Shape(1, 2, 3), r2.shape); var r2np = r2.numpy(); Assert.AreEqual(r2np[0, 0, 0], 3); @@ -36,7 +38,7 @@ public void Slice() Assert.AreEqual(r2np[0, 1, 1], 4); Assert.AreEqual(r2np[0, 1, 2], 4); - var r3 = array_ops.slice(input_array, new int[] { 1, 0, 0 }, new int[] { 2, 1, 3 }); + var r3 = array_ops.slice(input_array, ops.convert_n_to_tensor(new object[] { 1, 0, 0 }), ops.convert_n_to_tensor(new object[] { 2, 1, 3 })); Assert.AreEqual(new Shape(2, 1, 3), r3.shape); var r3np = r3.numpy(); Assert.AreEqual(r3np[0, 0, 0], 3); @@ -92,5 +94,333 @@ public void TensorArray() Assert.AreEqual(ta.read(1).numpy(), 20f); Assert.AreEqual(ta.read(2).numpy(), 30f); } + + /// + /// https://www.tensorflow.org/api_docs/python/tf/reverse + /// + [TestMethod] + public void ReverseArray() + { + var a = tf.random.normal((2, 3)); + var b = tf.reverse(a, -1); + Assert.IsTrue(Equal(a[0].ToArray().Reverse().ToArray(), b[0].ToArray())); + Assert.IsTrue(Equal(a[1].ToArray().Reverse().ToArray(), b[1].ToArray())); + } + + [TestMethod] + public void ReverseImgArray3D() + { + // 创建 sourceImg 数组 + var sourceImgArray = new float[,,] { + { + { 237, 28, 36 }, + { 255, 255, 255 }, + { 255, 255, 255 } + }, + { + { 255, 255, 255 }, + { 255, 255, 255 }, + { 255, 255, 255 } + } + }; + var sourceImg = ops.convert_to_tensor(sourceImgArray); + + // 创建 lrImg 数组 + var lrImgArray = new float[,,] { + { + { 255, 255, 255 }, + { 255, 255, 255 }, + { 237, 28, 36 } + }, + { + { 255, 255, 255 }, + { 255, 255, 255 }, + { 255, 255, 255 } + } + }; + var lrImg = ops.convert_to_tensor(lrImgArray); + + var lr = tf.image.flip_left_right(sourceImg); + Assert.IsTrue(Equal(lrImg.numpy().ToArray(), lr.numpy().ToArray()), "tf.image.flip_left_right fail."); + + var lr2 = tf.reverse(sourceImg, 1); + Assert.IsTrue(Equal(lrImg.numpy().ToArray(), lr2.numpy().ToArray()), "tf.reverse (axis=1) fail."); + + var lr3 = gen_array_ops.reverse_v2(sourceImg, ops.convert_to_tensor(new[] { 1 })); + Assert.IsTrue(Equal(lrImg.numpy().ToArray(), lr3.numpy().ToArray()), "gen_array_ops.reverse_v2 axis=1 fail."); + + // 创建 udImg 数组 + var udImgArray = new float[,,] { + { + { 255, 255, 255 }, + { 255, 255, 255 }, + { 255, 255, 255 } + }, + { + { 237, 28, 36 }, + { 255, 255, 255 }, + { 255, 255, 255 } + } + }; + var udImg = ops.convert_to_tensor(udImgArray); + + var ud = tf.image.flip_up_down(sourceImg); + Assert.IsTrue(Equal(udImg.numpy().ToArray(), ud.numpy().ToArray()), "tf.image.flip_up_down fail."); + + var ud2 = tf.reverse(sourceImg, new Axis(0)); + Assert.IsTrue(Equal(udImg.numpy().ToArray(), ud2.numpy().ToArray()), "tf.reverse (axis=0) fail."); + + var ud3 = gen_array_ops.reverse_v2(sourceImg, ops.convert_to_tensor(new[] { 0 })); + Assert.IsTrue(Equal(udImg.numpy().ToArray(), ud3.numpy().ToArray()), "gen_array_ops.reverse_v2 axis=0 fail."); + } + + [TestMethod] + public void ReverseImgArray4D() + { + // 原图左上角,加一张左右翻转后的图片 + var m = new float[,,,] { + { + { + { 237, 28, 36 }, + { 255, 255, 255 }, + { 255, 255, 255 } + }, + { + { 255, 255, 255 }, + { 255, 255, 255 }, + { 255, 255, 255 } + } + }, + { + { + { 255, 255, 255 }, + { 255, 255, 255 }, + { 237, 28, 36 } + }, + { + { 255, 255, 255 }, + { 255, 255, 255 }, + { 255, 255, 255 } + } + } + }; + var sourceImg = ops.convert_to_tensor(m); + + var lrArray = new float[,,,] { + { + { + { 255, 255, 255 }, + { 255, 255, 255 }, + { 237, 28, 36 }, + }, + { + { 255, 255, 255 }, + { 255, 255, 255 }, + { 255, 255, 255 } + } + }, + { + { + { 237, 28, 36 }, + { 255, 255, 255 }, + { 255, 255, 255 }, + }, + { + { 255, 255, 255 }, + { 255, 255, 255 }, + { 255, 255, 255 } + } + } + }; + var lrImg = ops.convert_to_tensor(lrArray); + + // 创建 ud 数组 + var udArray = new float[,,,] { + { + { + { 255, 255, 255 }, + { 255, 255, 255 }, + { 255, 255, 255 } + }, + { + { 237, 28, 36 }, + { 255, 255, 255 }, + { 255, 255, 255 } + } + }, + { + { + { 255, 255, 255 }, + { 255, 255, 255 }, + { 255, 255, 255 } + }, + { + { 255, 255, 255 }, + { 255, 255, 255 }, + { 237, 28, 36 } + } + } + }; + var udImg = ops.convert_to_tensor(udArray); + + var ud3 = gen_array_ops.reverse_v2(sourceImg, ops.convert_to_tensor(new[] { 1 })); + Assert.IsTrue(Equal(udImg.numpy().ToArray(), ud3.numpy().ToArray()), "gen_array_ops.reverse_v2 axis=1 fail."); + + var ud2 = tf.reverse(sourceImg, new Axis(1)); + Assert.IsTrue(Equal(udImg.numpy().ToArray(), ud2.numpy().ToArray()), "tf.reverse (axis=1) fail."); + + var ud = tf.image.flip_up_down(sourceImg); + Assert.IsTrue(Equal(udImg.numpy().ToArray(), ud.numpy().ToArray()), "tf.image.flip_up_down fail."); + + // 左右翻转 + var lr = tf.image.flip_left_right(sourceImg); + Assert.IsTrue(Equal(lrImg.numpy().ToArray(), lr.numpy().ToArray()), "tf.image.flip_left_right fail."); + + var lr2 = tf.reverse(sourceImg, 0); + Assert.IsTrue(Equal(lrImg.numpy().ToArray(), lr2.numpy().ToArray()), "tf.reverse (axis=1) fail."); + + var lr3 = gen_array_ops.reverse_v2(sourceImg, ops.convert_to_tensor(new[] { 0 })); + Assert.IsTrue(Equal(lrImg.numpy().ToArray(), lr3.numpy().ToArray()), "gen_array_ops.reverse_v2 axis=1 fail."); + + } + + [TestMethod] + public void ReverseImgArray4D_3x3() + { + // 原图左上角,加一张左右翻转后的图片 + var m = new float[,,,] { + { + { + { 237, 28, 36 }, + { 255, 255, 255 }, + { 255, 255, 255 } + }, + { + { 255, 255, 255 }, + { 255, 255, 255 }, + { 255, 255, 255 } + }, + { + { 255, 255, 255 }, + { 255, 255, 255 }, + { 255, 255, 255 } + } + }, + { + { + { 255, 255, 255 }, + { 255, 255, 255 }, + { 237, 28, 36 } + }, + { + { 255, 255, 255 }, + { 255, 255, 255 }, + { 255, 255, 255 } + }, + { + { 255, 255, 255 }, + { 255, 255, 255 }, + { 255, 255, 255 } + } + } + }; + var sourceImg = ops.convert_to_tensor(m); + + var lrArray = new float[,,,] { + { + { + { 255, 255, 255 }, + { 255, 255, 255 }, + { 237, 28, 36 }, + }, + { + { 255, 255, 255 }, + { 255, 255, 255 }, + { 255, 255, 255 } + }, + { + { 255, 255, 255 }, + { 255, 255, 255 }, + { 255, 255, 255 } + } + }, + { + { + { 237, 28, 36 }, + { 255, 255, 255 }, + { 255, 255, 255 }, + }, + { + { 255, 255, 255 }, + { 255, 255, 255 }, + { 255, 255, 255 } + }, + { + { 255, 255, 255 }, + { 255, 255, 255 }, + { 255, 255, 255 } + } + } + }; + var lrImg = ops.convert_to_tensor(lrArray); + + // 创建 ud 数组 + var udArray = new float[,,,] { + { + { + { 255, 255, 255 }, + { 255, 255, 255 }, + { 255, 255, 255 } + }, + { + { 255, 255, 255 }, + { 255, 255, 255 }, + { 255, 255, 255 } + }, + { + { 237, 28, 36 }, + { 255, 255, 255 }, + { 255, 255, 255 } + } + }, + { { + { 255, 255, 255 }, + { 255, 255, 255 }, + { 255, 255, 255 } + }, + { + { 255, 255, 255 }, + { 255, 255, 255 }, + { 255, 255, 255 } + }, + { + { 255, 255, 255 }, + { 255, 255, 255 }, + { 237, 28, 36 } + } + } + }; + var udImg = ops.convert_to_tensor(udArray); + + var ud3 = gen_array_ops.reverse_v2(sourceImg, ops.convert_to_tensor(new[] { 1 })); + Assert.IsTrue(Equal(udImg.numpy().ToArray(), ud3.numpy().ToArray()), "gen_array_ops.reverse_v2 axis=1 fail."); + + var ud2 = tf.reverse(sourceImg, new Axis(1)); + Assert.IsTrue(Equal(udImg.numpy().ToArray(), ud2.numpy().ToArray()), "tf.reverse (axis=1) fail."); + + var ud = tf.image.flip_up_down(sourceImg); + Assert.IsTrue(Equal(udImg.numpy().ToArray(), ud.numpy().ToArray()), "tf.image.flip_up_down fail."); + + // 左右翻转 + var lr = tf.image.flip_left_right(sourceImg); + Assert.IsTrue(Equal(lrImg.numpy().ToArray(), lr.numpy().ToArray()), "tf.image.flip_left_right fail."); + + var lr2 = tf.reverse(sourceImg, 0); + Assert.IsTrue(Equal(lrImg.numpy().ToArray(), lr2.numpy().ToArray()), "tf.reverse (axis=1) fail."); + + var lr3 = gen_array_ops.reverse_v2(sourceImg, ops.convert_to_tensor(new[] { 0 })); + Assert.IsTrue(Equal(lrImg.numpy().ToArray(), lr3.numpy().ToArray()), "gen_array_ops.reverse_v2 axis=1 fail."); + + } } } diff --git a/test/TensorFlowNET.UnitTest/ManagedAPI/ControlFlowApiTest.cs b/test/TensorFlowNET.UnitTest/ManagedAPI/ControlFlowApiTest.cs index 6d7182e09..23dc1d44d 100644 --- a/test/TensorFlowNET.UnitTest/ManagedAPI/ControlFlowApiTest.cs +++ b/test/TensorFlowNET.UnitTest/ManagedAPI/ControlFlowApiTest.cs @@ -28,8 +28,8 @@ public void WhileLoopTwoInputsEagerMode() var i = tf.constant(2); var j = tf.constant(3); - Func c = (x) => tf.less(x[0] + x[1], 10); - Func b = (x) => new[] { tf.add(x[0], 1), tf.add(x[1], 1) }; + Func c = (x) => tf.less(x[0] + x[1], 10); + Func b = (x) => new[] { tf.add(x[0], 1), tf.add(x[1], 1) }; var r = tf.while_loop(c, b, new[] { i, j }); Assert.AreEqual(5, (int)r[0]); Assert.AreEqual(6, (int)r[1]); diff --git a/test/TensorFlowNET.UnitTest/ManagedAPI/MathApiTest.cs b/test/TensorFlowNET.UnitTest/ManagedAPI/MathApiTest.cs index 42ac641b1..411deb18f 100644 --- a/test/TensorFlowNET.UnitTest/ManagedAPI/MathApiTest.cs +++ b/test/TensorFlowNET.UnitTest/ManagedAPI/MathApiTest.cs @@ -1,6 +1,8 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; using System.Linq; using Tensorflow; +using Tensorflow.NumPy; using static Tensorflow.Binding; namespace TensorFlowNET.UnitTest.ManagedAPI @@ -57,5 +59,26 @@ public void Erf() var actual = erf.ToArray(); Assert.IsTrue(Equal(expected, actual)); } + + [TestMethod] + public void ReduceEuclideanNorm() + { + var x = tf.constant(new[,] { { 1, 2, 3 }, { 1, 1, 1 } }); + Assert.AreEqual(tf.math.reduce_euclidean_norm(x).numpy(), 4); + + var y = tf.constant(new[,] { { 1, 2, 3 }, { 1, 1, 1 } }, dtype: tf.float32); + Assert.IsTrue(Equal(tf.math.reduce_euclidean_norm(y).numpy(), 4.1231055f)); + + Assert.IsTrue(Equal(tf.math.reduce_euclidean_norm(y, 0).ToArray(), + new float[] { np.sqrt(2f), np.sqrt(5f), np.sqrt(10f) })); + + Assert.IsTrue(Equal(tf.math.reduce_euclidean_norm(y, 1).ToArray(), + new float[] { np.sqrt(14f), np.sqrt(3f) })); + + Assert.IsTrue(Equal(tf.math.reduce_euclidean_norm(y, 1, keepdims: true).ToArray(), + new float[] { np.sqrt(14f), np.sqrt(3f) })); + + Assert.AreEqual(tf.math.reduce_euclidean_norm(y, (0, 1)).numpy(), np.sqrt(17f)); + } } } diff --git a/test/TensorFlowNET.UnitTest/ManagedAPI/RaggedTensorTest.cs b/test/TensorFlowNET.UnitTest/ManagedAPI/RaggedTensorTest.cs new file mode 100644 index 000000000..7a3de882e --- /dev/null +++ b/test/TensorFlowNET.UnitTest/ManagedAPI/RaggedTensorTest.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Tensorflow; +using Tensorflow.NumPy; +using static Tensorflow.Binding; + +namespace TensorFlowNET.UnitTest.ManagedAPI +{ + public class RaggedTensorTest :EagerModeTestBase + { + [TestMethod] + public void Test_from_row_lengths() + { + var row_lengths = tf.convert_to_tensor(np.array(new int[] { 2, 0, 3, 1, 1 }, TF_DataType.TF_INT64)); + var rp = RowPartition.from_row_lengths(row_lengths, validate: false); + var rp_row_lengths = rp.row_lengths(); + var rp_nrows = rp.nrows(); + Assert.IsTrue(rp_nrows.ToArray()[0] == rp.nrows().ToArray()[0]); + + } + } +} diff --git a/test/TensorFlowNET.UnitTest/NumPy/ShapeTest.cs b/test/TensorFlowNET.UnitTest/NumPy/ShapeTest.cs new file mode 100644 index 000000000..f5a8685be --- /dev/null +++ b/test/TensorFlowNET.UnitTest/NumPy/ShapeTest.cs @@ -0,0 +1,44 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Tensorflow.NumPy; +using System; +using System.Linq; +using static Tensorflow.Binding; +using Tensorflow; + +namespace TensorFlowNET.UnitTest.NumPy +{ + [TestClass] + public class ShapeTest : EagerModeTestBase + { + [Ignore] + [TestMethod] + public unsafe void ShapeGetLastElements() + { + // test code from function _CheckAtLeast3DImage + // 之前的 _CheckAtLeast3DImage 有bug,现在通过测试,下面的代码是正确的 + // todo: shape["-3:"] 的写法,目前有bug,需要修复,单元测试等修复后再放开,暂时先忽略测试 + + var image_shape = new Shape(new[] { 32, 64, 3 }); + var image_shape_4d = new Shape(new[] { 4, 64, 32, 3 }); + + var image_shape_last_three_elements = new Shape(new[] { + image_shape.dims[image_shape.dims.Length - 3], + image_shape.dims[image_shape.dims.Length - 2], + image_shape.dims[image_shape.dims.Length - 1]}); + + var image_shape_last_three_elements2 = image_shape["-3:"]; + + Assert.IsTrue(Equal(image_shape_last_three_elements.dims, image_shape_last_three_elements2.dims), "3dims get fail."); + + var image_shape_last_three_elements_4d = new Shape(new[] { + image_shape_4d.dims[image_shape_4d.dims.Length - 3], + image_shape_4d.dims[image_shape_4d.dims.Length - 2], + image_shape_4d.dims[image_shape_4d.dims.Length - 1]}); + + var image_shape_last_three_elements2_4d = image_shape_4d["-3:"]; + + Assert.IsTrue(Equals(image_shape_last_three_elements_4d.dims, image_shape_last_three_elements2_4d.dims), "4dims get fail."); + } + + } +} \ No newline at end of file diff --git a/test/TensorFlowNET.UnitTest/Numpy/Math.Test.cs b/test/TensorFlowNET.UnitTest/Numpy/Math.Test.cs index a0e6fa4ec..65cdaedd9 100644 --- a/test/TensorFlowNET.UnitTest/Numpy/Math.Test.cs +++ b/test/TensorFlowNET.UnitTest/Numpy/Math.Test.cs @@ -65,5 +65,47 @@ public void power() var y = np.power(x, 3); Assert.AreEqual(y, new[] { 0, 1, 8, 27, 64, 125 }); } + [TestMethod] + public void square() + { + var x = np.arange(6); + var y = np.square(x); + Assert.AreEqual(y, new[] { 0, 1, 4, 9, 16, 25 }); + } + [TestMethod] + public void dotproduct() + { + var x1 = new NDArray(new[] { 1, 2, 3 }); + var x2 = new NDArray(new[] { 4, 5, 6 }); + double result1 = np.dot(x1, x2); + NDArray y1 = new float[,] { + { 1.0f, 2.0f, 3.0f }, + { 4.0f, 5.1f,6.0f }, + { 4.0f, 5.1f,6.0f } + }; + NDArray y2 = new float[,] { + { 3.0f, 2.0f, 1.0f }, + { 6.0f, 5.1f, 4.0f }, + { 6.0f, 5.1f, 4.0f } + }; + double result2 = np.dot(y1, y2); + Assert.AreEqual(result1, 32); + Assert.AreEqual(Math.Round(result2, 2), 158.02); + } + [TestMethod] + public void maximum() + { + var x1 = new NDArray(new[,] { { 1, 2, 3 }, { 4, 5.1, 6 } }); + var x2 = new NDArray(new[,] { { 3, 2, 1 }, { 6, 5.1, 4 } }); + var y0 = np.maximum(x1,x2); + var y1 = np.maximum(x1, x2, axis: 0); + var y2 = np.maximum(x1, x2, axis: 1); + var y3 = new NDArray(new[,] { { 3, 2, 3 }, { 6, 5.1, 6 } }); + var y4 = new NDArray(new[] { 6, 5.1, 6 }); + var y5 = new NDArray(new[] { 3.0, 6 }); + Assert.AreEqual(y0, y3); + Assert.AreEqual(y1, y4); + Assert.AreEqual(y2, y5); + } } } diff --git a/test/TensorFlowNET.UnitTest/Tensorflow.Binding.UnitTest.csproj b/test/TensorFlowNET.UnitTest/Tensorflow.Binding.UnitTest.csproj index 3a5562e2c..5264cb104 100644 --- a/test/TensorFlowNET.UnitTest/Tensorflow.Binding.UnitTest.csproj +++ b/test/TensorFlowNET.UnitTest/Tensorflow.Binding.UnitTest.csproj @@ -41,16 +41,17 @@ - - - - + + + + - + + diff --git a/test/TensorFlowNET.UnitTest/Training/GradientDescentOptimizerTests.cs b/test/TensorFlowNET.UnitTest/Training/GradientDescentOptimizerTests.cs new file mode 100644 index 000000000..3b53ff9cd --- /dev/null +++ b/test/TensorFlowNET.UnitTest/Training/GradientDescentOptimizerTests.cs @@ -0,0 +1,232 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Linq; +using Tensorflow; +using Tensorflow.NumPy; +using static Tensorflow.Binding; + +namespace TensorFlowNET.UnitTest.Training +{ + [TestClass] + public class GradientDescentOptimizerTest : PythonTest + { + private static TF_DataType GetTypeForNumericType() where T : struct + { + return Type.GetTypeCode(typeof(T)) switch + { + TypeCode.Single => np.float32, + TypeCode.Double => np.float64, + _ => throw new NotImplementedException(), + }; + } + + private void TestBasic() where T : struct + { + var dtype = GetTypeForNumericType(); + + // train.GradientDescentOptimizer is V1 only API. + tf.Graph().as_default(); + using (var sess = self.cached_session()) + { + var var0 = tf.Variable(new[] { 1.0, 2.0 }, dtype: dtype); + var var1 = tf.Variable(new[] { 3.0, 4.0 }, dtype: dtype); + var grads0 = tf.constant(new[] { 0.1, 0.1 }, dtype: dtype); + var grads1 = tf.constant(new[] { 0.01, 0.01 }, dtype: dtype); + var optimizer = tf.train.GradientDescentOptimizer(3.0f); + var grads_and_vars = new[] { + Tuple.Create(grads0, var0 as IVariableV1), + Tuple.Create(grads1, var1 as IVariableV1) + }; + var sgd_op = optimizer.apply_gradients(grads_and_vars); + + var global_variables = tf.global_variables_initializer(); + sess.run(global_variables); + + var initialVar0 = sess.run(var0); + var initialVar1 = sess.run(var1); + // Fetch params to validate initial values + self.assertAllCloseAccordingToType(new[] { 1.0, 2.0 }, self.evaluate(var0)); + self.assertAllCloseAccordingToType(new[] { 3.0, 4.0 }, self.evaluate(var1)); + // Run 1 step of sgd + sgd_op.run(); + // Validate updated params + self.assertAllCloseAccordingToType( + new[] { 1.0 - 3.0 * 0.1, 2.0 - 3.0 * 0.1 }, + self.evaluate(var0)); + self.assertAllCloseAccordingToType( + new[] { 3.0 - 3.0 * 0.01, 4.0 - 3.0 * 0.01 }, + self.evaluate(var1)); + // TODO: self.assertEqual(0, len(optimizer.variables())); + } + } + + [TestMethod] + public void TestBasic() + { + //TODO: add np.half + TestBasic(); + TestBasic(); + } + + private void TestMinimizeResourceVariable() where T : struct + { + var dtype = GetTypeForNumericType(); + + // train.GradientDescentOptimizer is V1 only API. + tf.Graph().as_default(); + using (var sess = self.cached_session()) + { + var var0 = tf.Variable(new[,] { { 1.0f, 2.0f } }, dtype: dtype); + var var1 = tf.Variable(new[] { 3.0 }, dtype: dtype); + var x = tf.constant(new[,] { { 4.0f }, { 5.0f } }, dtype: dtype); + + var pred = math_ops.matmul(var0, x) + var1; + var loss = pred * pred; + var sgd_op = tf.train.GradientDescentOptimizer(1.0f).minimize(loss); + + var global_variables = tf.global_variables_initializer(); + sess.run(global_variables); + + sess.run(new[] { var0, var1 }); + // Fetch params to validate initial values + self.assertAllCloseAccordingToType(new[,] { { 1.0, 2.0 } }, self.evaluate(var0)); + self.assertAllCloseAccordingToType(new[] { 3.0 }, self.evaluate(var1)); + // Run 1 step of sgd + sgd_op.run(); + // Validate updated params + var np_pred = 1.0 * 4.0 + 2.0 * 5.0 + 3.0; + var np_grad = 2 * np_pred; + self.assertAllCloseAccordingToType( + new[,] { { 1.0 - np_grad * 4.0, 2.0 - np_grad * 5.0 } }, + self.evaluate(var0)); + self.assertAllCloseAccordingToType( + new[] { 3.0 - np_grad }, + self.evaluate(var1)); + } + } + + [TestMethod] + public void TestMinimizeResourceVariable() + { + //TODO: add np.half + TestMinimizeResourceVariable(); + TestMinimizeResourceVariable(); + } + + private void TestTensorLearningRate() where T : struct + { + var dtype = GetTypeForNumericType(); + + // train.GradientDescentOptimizer is V1 only API. + tf.Graph().as_default(); + using (var sess = self.cached_session()) + { + var var0 = tf.Variable(new[] { 1.0, 2.0 }, dtype: dtype); + var var1 = tf.Variable(new[] { 3.0, 4.0 }, dtype: dtype); + var grads0 = tf.constant(new[] { 0.1, 0.1 }, dtype: dtype); + var grads1 = tf.constant(new[] { 0.01, 0.01 }, dtype: dtype); + var lrate = constant_op.constant(3.0); + var grads_and_vars = new[] { + Tuple.Create(grads0, var0 as IVariableV1), + Tuple.Create(grads1, var1 as IVariableV1) + }; + var sgd_op = tf.train.GradientDescentOptimizer(lrate) + .apply_gradients(grads_and_vars); + + var global_variables = tf.global_variables_initializer(); + sess.run(global_variables); + + var initialVar0 = sess.run(var0); + var initialVar1 = sess.run(var1); + // Fetch params to validate initial values + self.assertAllCloseAccordingToType(new[] { 1.0, 2.0 }, self.evaluate(var0)); + self.assertAllCloseAccordingToType(new[] { 3.0, 4.0 }, self.evaluate(var1)); + // Run 1 step of sgd + sgd_op.run(); + // Validate updated params + self.assertAllCloseAccordingToType( + new[] { 1.0 - 3.0 * 0.1, 2.0 - 3.0 * 0.1 }, + self.evaluate(var0)); + self.assertAllCloseAccordingToType( + new[] { 3.0 - 3.0 * 0.01, 4.0 - 3.0 * 0.01 }, + self.evaluate(var1)); + // TODO: self.assertEqual(0, len(optimizer.variables())); + } + } + + [TestMethod] + public void TestTensorLearningRate() + { + //TODO: add np.half + TestTensorLearningRate(); + TestTensorLearningRate(); + } + + public void TestGradWrtRef() where T : struct + { + var dtype = GetTypeForNumericType(); + + var graph = tf.Graph().as_default(); + using (var sess = self.cached_session()) + { + var opt = tf.train.GradientDescentOptimizer(3.0f); + var values = new[] { 1.0, 3.0 }; + var vars_ = values.Select( + v => tf.Variable(new[] { v }, dtype: dtype) as IVariableV1 + ).ToList(); + var grads_and_vars = opt.compute_gradients(tf.add(vars_[0], vars_[1]), vars_); + sess.run(tf.global_variables_initializer()); + foreach (var (grad, _) in grads_and_vars) + self.assertAllCloseAccordingToType(new[] { 1.0 }, self.evaluate(grad)); + + } + } + + [TestMethod] + public void TestGradWrtRef() + { + TestGradWrtRef(); + TestGradWrtRef(); + } + + public void TestWithGlobalStep() where T : struct + { + var dtype = GetTypeForNumericType(); + + tf.Graph().as_default(); + using (var sess = self.cached_session()) + { + var global_step = tf.Variable(0, trainable: false); + var var0 = tf.Variable(new[] { 1.0, 2.0 }, dtype: dtype); + var var1 = tf.Variable(new[] { 3.0, 4.0 }, dtype: dtype); + var grads0 = tf.constant(new[] { 0.1, 0.1 }, dtype: dtype); + var grads1 = tf.constant(new[] { 0.01, 0.01 }, dtype: dtype); + var grads_and_vars = new[] { + Tuple.Create(grads0, var0 as IVariableV1), + Tuple.Create(grads1, var1 as IVariableV1) + }; + var sgd_op = tf.train.GradientDescentOptimizer(3.0f) + .apply_gradients(grads_and_vars, global_step: global_step); + + sess.run(tf.global_variables_initializer()); + // Fetch params to validate initial values + self.assertAllCloseAccordingToType(new[] { 1.0, 2.0 }, self.evaluate(var0)); + self.assertAllCloseAccordingToType(new[] { 3.0, 4.0 }, self.evaluate(var1)); + // Run 1 step of sgd + sgd_op.run(); + // Validate updated params and global_step + self.assertAllCloseAccordingToType(new[] { 1.0 - 3.0 * 0.1, 2.0 - 3.0 * 0.1 }, self.evaluate(var0)); + self.assertAllCloseAccordingToType(new[] { 3.0 - 3.0 * 0.01, 4.0 - 3.0 * 0.01 }, self.evaluate(var1)); + Assert.AreEqual(1, self.evaluate(global_step)); + } + + } + + [TestMethod] + public void TestWithGlobalStep() + { + TestWithGlobalStep(); + TestWithGlobalStep(); + } + } +} diff --git a/test/TensorFlowNET.UnitTest/PythonTest.cs b/test/Tensorflow.UnitTest/PythonTest.cs similarity index 51% rename from test/TensorFlowNET.UnitTest/PythonTest.cs rename to test/Tensorflow.UnitTest/PythonTest.cs index 50cc2b328..1ccd39f02 100644 --- a/test/TensorFlowNET.UnitTest/PythonTest.cs +++ b/test/Tensorflow.UnitTest/PythonTest.cs @@ -1,9 +1,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using Newtonsoft.Json.Linq; using Tensorflow.NumPy; -using System; using System.Collections; -using System.Linq; using Tensorflow; using static Tensorflow.Binding; @@ -88,9 +86,9 @@ public void assertEqual(object given, object expected) Assert.AreEqual(JObject.FromObject(expected).ToString(), JObject.FromObject(given).ToString()); return; } - if (given is ICollection && expected is ICollection) + if (given is ICollection collectionGiven && expected is ICollection collectionExpected) { - assertItemsEqual(given as ICollection, expected as ICollection); + assertItemsEqual(collectionGiven, collectionExpected); return; } if (given is float && expected is float) @@ -135,13 +133,83 @@ public void assertTrue(bool cond) public void assertAllClose(NDArray array1, NDArray array2, double eps = 1e-5) { - Assert.IsTrue(np.allclose(array1, array2, rtol: eps)); + CollectionAssert.AreEqual(array1.ToArray(), array2.ToArray(), new CollectionComparer(eps)); + + //TODO: Assert.IsTrue(np.allclose(array1, array2, rtol: eps)); } public void assertAllClose(double value, NDArray array2, double eps = 1e-5) { + if (array2.shape.IsScalar) + { + double value2 = array2; + Assert.AreEqual(value, value2, eps); + return; + } var array1 = np.ones_like(array2) * value; - Assert.IsTrue(np.allclose(array1, array2, rtol: eps)); + CollectionAssert.AreEqual(array1.ToArray(), array2.ToArray(), new CollectionComparer(eps)); + + //TODO: Assert.IsTrue(np.allclose(array1, array2, rtol: eps)); + } + + private class CollectionComparer : IComparer + { + private readonly double _epsilon; + + public CollectionComparer(double eps = 1e-06) + { + _epsilon = eps; + } + public int Compare(object? x, object? y) + { + if (x == null && y == null) + { + return 0; + } + else if (x == null) + { + return -1; + } + else if (y == null) + { + return 1; + } + + var a = Convert.ToDouble(x); + var b = Convert.ToDouble(y); + + double delta = Math.Abs(a - b); + if (delta < _epsilon) + { + return 0; + } + return a.CompareTo(b); + } + } + + public void assertAllCloseAccordingToType( + double[,] expected, + T[,] given, + double eps = 1e-6, + float float_eps = 1e-6f) + { + Assert.AreEqual(expected.GetLength(0), given.GetLength(0)); + Assert.AreEqual(expected.GetLength(1), given.GetLength(1)); + + var flattenGiven = given.Cast().ToArray(); + assertAllCloseAccordingToType(expected, flattenGiven, eps, float_eps); + } + + public void assertAllCloseAccordingToType( + ICollection expected, + ICollection given, + double eps = 1e-6, + float float_eps = 1e-6f) + { + // TODO: check if any of arguments is not double and change toletance + // remove givenAsDouble and cast expected instead + var givenAsDouble = given.Select(x => Convert.ToDouble(x)).ToArray(); + CollectionAssert.AreEqual(expected, givenAsDouble, new CollectionComparer(eps)); } public void assertProtoEquals(object toProto, object o) @@ -153,6 +221,20 @@ public void assertProtoEquals(object toProto, object o) #region tensor evaluation and test session + private Session? _cached_session = null; + private Graph? _cached_graph = null; + private object? _cached_config = null; + private bool _cached_force_gpu = false; + + private void _ClearCachedSession() + { + if (self._cached_session != null) + { + self._cached_session.Dispose(); + self._cached_session = null; + } + } + //protected object _eval_helper(Tensor[] tensors) //{ // if (tensors == null) @@ -160,7 +242,7 @@ public void assertProtoEquals(object toProto, object o) // return nest.map_structure(self._eval_tensor, tensors); //} - protected object _eval_tensor(object tensor) + protected object? _eval_tensor(object tensor) { if (tensor == null) return None; @@ -191,22 +273,44 @@ protected object _eval_tensor(object tensor) /// public T evaluate(Tensor tensor) { - object result = null; + object? result = null; // if context.executing_eagerly(): // return self._eval_helper(tensors) // else: { - var sess = tf.Session(); + var sess = tf.get_default_session(); var ndarray = tensor.eval(sess); - if (typeof(T) == typeof(double)) + + if (typeof(T) == typeof(int)) + { + int i = ndarray; + result = i; + } + else if (typeof(T) == typeof(float)) { - double x = ndarray; - result = x; + float f = ndarray; + result = f; } - else if (typeof(T) == typeof(int)) + else if (typeof(T) == typeof(double)) { - int x = ndarray; - result = x; + double d = ndarray; + result = d; + } + else if ( + typeof(T) == typeof(double[]) + || typeof(T) == typeof(double[,])) + { + result = ndarray.ToMultiDimArray(); + } + else if (typeof(T) == typeof(float[]) + || typeof(T) == typeof(float[,])) + { + result = ndarray.ToMultiDimArray(); + } + else if (typeof(T) == typeof(int[]) + || typeof(T) == typeof(int[,])) + { + result = ndarray.ToMultiDimArray(); } else { @@ -218,13 +322,60 @@ public T evaluate(Tensor tensor) } - public Session cached_session() + ///Returns a TensorFlow Session for use in executing tests. + public Session? cached_session( + Graph? graph = null, object? config = null, bool use_gpu = false, bool force_gpu = false) { - throw new NotImplementedException(); + // This method behaves differently than self.session(): for performance reasons + // `cached_session` will by default reuse the same session within the same + // test.The session returned by this function will only be closed at the end + // of the test(in the TearDown function). + + // Use the `use_gpu` and `force_gpu` options to control where ops are run.If + // `force_gpu` is True, all ops are pinned to `/ device:GPU:0`. Otherwise, if + // `use_gpu` is True, TensorFlow tries to run as many ops on the GPU as + // possible.If both `force_gpu and `use_gpu` are False, all ops are pinned to + // the CPU. + + // Example: + // python + // class MyOperatorTest(test_util.TensorFlowTestCase) : + // def testMyOperator(self): + // with self.cached_session() as sess: + // valid_input = [1.0, 2.0, 3.0, 4.0, 5.0] + // result = MyOperator(valid_input).eval() + // self.assertEqual(result, [1.0, 2.0, 3.0, 5.0, 8.0] + // invalid_input = [-1.0, 2.0, 7.0] + // with self.assertRaisesOpError("negative input not supported"): + // MyOperator(invalid_input).eval() + + + // Args: + // graph: Optional graph to use during the returned session. + // config: An optional config_pb2.ConfigProto to use to configure the + // session. + // use_gpu: If True, attempt to run as many ops as possible on GPU. + // force_gpu: If True, pin all ops to `/device:GPU:0`. + + // Yields: + // A Session object that should be used as a context manager to surround + // the graph building and execution code in a test case. + + + // TODO: + // if context.executing_eagerly(): + // return self._eval_helper(tensors) + // else: + { + var sess = self._get_cached_session( + graph, config, force_gpu, crash_if_inconsistent_args: true); + using var cached = self._constrain_devices_and_set_default(sess, use_gpu, force_gpu); + return cached; + } } //Returns a TensorFlow Session for use in executing tests. - public Session session(Graph graph = null, object config = null, bool use_gpu = false, bool force_gpu = false) + public Session session(Graph? graph = null, object? config = null, bool use_gpu = false, bool force_gpu = false) { //Note that this will set this session and the graph as global defaults. @@ -258,7 +409,7 @@ public Session session(Graph graph = null, object config = null, bool use_gpu = // A Session object that should be used as a context manager to surround // the graph building and execution code in a test case. - Session s = null; + Session? s = null; //if (context.executing_eagerly()) // yield None //else @@ -268,8 +419,42 @@ public Session session(Graph graph = null, object config = null, bool use_gpu = return s.as_default(); } + private Session? _constrain_devices_and_set_default(Session sess, bool use_gpu, bool force_gpu) + { + // Set the session and its graph to global default and constrain devices.""" + if (tf.executing_eagerly()) + return null; + else + { + sess.graph.as_default(); + sess.as_default(); + { + if (force_gpu) + { + // TODO: + + // Use the name of an actual device if one is detected, or + // '/device:GPU:0' otherwise + /* var gpu_name = gpu_device_name(); + if (!gpu_name) + gpu_name = "/device:GPU:0" + using (sess.graph.device(gpu_name)) { + yield return sess; + }*/ + return sess; + } + else if (use_gpu) + return sess; + else + using (sess.graph.device("/device:CPU:0")) + return sess; + } + + } + } + // See session() for details. - private Session _create_session(Graph graph, object cfg, bool forceGpu) + private Session _create_session(Graph? graph, object? cfg, bool forceGpu) { var prepare_config = new Func((config) => { @@ -312,6 +497,54 @@ private Session _create_session(Graph graph, object cfg, bool forceGpu) return new Session(graph);//, config = prepare_config(config)) } + private Session _get_cached_session( + Graph? graph = null, + object? config = null, + bool force_gpu = false, + bool crash_if_inconsistent_args = true) + { + // See cached_session() for documentation. + if (self._cached_session == null) + { + var sess = self._create_session(graph, config, force_gpu); + self._cached_session = sess; + self._cached_graph = graph; + self._cached_config = config; + self._cached_force_gpu = force_gpu; + return sess; + } + else + { + + if (crash_if_inconsistent_args && self._cached_graph != null && !self._cached_graph.Equals(graph)) + throw new ValueError(@"The graph used to get the cached session is + different than the one that was used to create the + session. Maybe create a new session with + self.session()"); + if (crash_if_inconsistent_args && self._cached_config != null && !self._cached_config.Equals(config)) + { + throw new ValueError(@"The config used to get the cached session is + different than the one that was used to create the + session. Maybe create a new session with + self.session()"); + } + if (crash_if_inconsistent_args && !self._cached_force_gpu.Equals(force_gpu)) + { + throw new ValueError(@"The force_gpu value used to get the cached session is + different than the one that was used to create the + session. Maybe create a new session with + self.session()"); + } + return self._cached_session; + } + } + + [TestCleanup] + public void Cleanup() + { + _ClearCachedSession(); + } + #endregion public void AssetSequenceEqual(T[] a, T[] b) diff --git a/test/Tensorflow.UnitTest/Tensorflow.UnitTest.csproj b/test/Tensorflow.UnitTest/Tensorflow.UnitTest.csproj new file mode 100644 index 000000000..9ad6bc7a5 --- /dev/null +++ b/test/Tensorflow.UnitTest/Tensorflow.UnitTest.csproj @@ -0,0 +1,24 @@ + + + + net6.0 + enable + enable + + false + true + + + + + + + + + + + + + + + diff --git a/test/TensorflowNET.Hub.Unittest/KerasLayerTest.cs b/test/TensorflowNET.Hub.Unittest/KerasLayerTest.cs index 4ee4d54c4..b9a8ed804 100644 --- a/test/TensorflowNET.Hub.Unittest/KerasLayerTest.cs +++ b/test/TensorflowNET.Hub.Unittest/KerasLayerTest.cs @@ -6,6 +6,7 @@ namespace Tensorflow.Hub.Unittest [TestClass] public class KerasLayerTest { + [Ignore] [TestMethod] public void SmallBert() { diff --git a/test/TensorflowNET.Hub.Unittest/Tensorflow.Hub.Unittest.csproj b/test/TensorflowNET.Hub.Unittest/Tensorflow.Hub.Unittest.csproj index 35cb9f16d..c93b89256 100644 --- a/test/TensorflowNET.Hub.Unittest/Tensorflow.Hub.Unittest.csproj +++ b/test/TensorflowNET.Hub.Unittest/Tensorflow.Hub.Unittest.csproj @@ -9,15 +9,15 @@ - + - + diff --git a/src/TensorFlowNet.Benchmarks/Crash/RepeatDataSetCrash.cs b/tools/TensorFlowNET.Benchmarks/Crash/RepeatDataSetCrash.cs similarity index 100% rename from src/TensorFlowNet.Benchmarks/Crash/RepeatDataSetCrash.cs rename to tools/TensorFlowNET.Benchmarks/Crash/RepeatDataSetCrash.cs diff --git a/src/TensorFlowNet.Benchmarks/Leak/GpuLeakByCNN.cs b/tools/TensorFlowNET.Benchmarks/Leak/GpuLeakByCNN.cs similarity index 100% rename from src/TensorFlowNet.Benchmarks/Leak/GpuLeakByCNN.cs rename to tools/TensorFlowNET.Benchmarks/Leak/GpuLeakByCNN.cs diff --git a/src/TensorFlowNet.Benchmarks/Leak/SavedModelCleanup.cs b/tools/TensorFlowNET.Benchmarks/Leak/SavedModelCleanup.cs similarity index 100% rename from src/TensorFlowNet.Benchmarks/Leak/SavedModelCleanup.cs rename to tools/TensorFlowNET.Benchmarks/Leak/SavedModelCleanup.cs diff --git a/src/TensorFlowNet.Benchmarks/Leak/TestModel/saved_model/saved_model.pb b/tools/TensorFlowNET.Benchmarks/Leak/TestModel/saved_model/saved_model.pb similarity index 100% rename from src/TensorFlowNet.Benchmarks/Leak/TestModel/saved_model/saved_model.pb rename to tools/TensorFlowNET.Benchmarks/Leak/TestModel/saved_model/saved_model.pb diff --git a/src/TensorFlowNet.Benchmarks/Leak/TestModel/saved_model/variables/variables.data-00000-of-00001 b/tools/TensorFlowNET.Benchmarks/Leak/TestModel/saved_model/variables/variables.data-00000-of-00001 similarity index 100% rename from src/TensorFlowNet.Benchmarks/Leak/TestModel/saved_model/variables/variables.data-00000-of-00001 rename to tools/TensorFlowNET.Benchmarks/Leak/TestModel/saved_model/variables/variables.data-00000-of-00001 diff --git a/src/TensorFlowNet.Benchmarks/Leak/TestModel/saved_model/variables/variables.index b/tools/TensorFlowNET.Benchmarks/Leak/TestModel/saved_model/variables/variables.index similarity index 100% rename from src/TensorFlowNet.Benchmarks/Leak/TestModel/saved_model/variables/variables.index rename to tools/TensorFlowNET.Benchmarks/Leak/TestModel/saved_model/variables/variables.index diff --git a/src/TensorFlowNet.Benchmarks/Program.cs b/tools/TensorFlowNET.Benchmarks/Program.cs similarity index 100% rename from src/TensorFlowNet.Benchmarks/Program.cs rename to tools/TensorFlowNET.Benchmarks/Program.cs diff --git a/src/TensorFlowNet.Benchmarks/README.md b/tools/TensorFlowNET.Benchmarks/README.md similarity index 100% rename from src/TensorFlowNet.Benchmarks/README.md rename to tools/TensorFlowNET.Benchmarks/README.md diff --git a/src/TensorFlowNet.Benchmarks/TensorBenchmark.cs b/tools/TensorFlowNET.Benchmarks/TensorBenchmark.cs similarity index 100% rename from src/TensorFlowNet.Benchmarks/TensorBenchmark.cs rename to tools/TensorFlowNET.Benchmarks/TensorBenchmark.cs diff --git a/src/TensorFlowNet.Benchmarks/Tensorflow.Benchmark.csproj b/tools/TensorFlowNET.Benchmarks/Tensorflow.Benchmark.csproj similarity index 94% rename from src/TensorFlowNet.Benchmarks/Tensorflow.Benchmark.csproj rename to tools/TensorFlowNET.Benchmarks/Tensorflow.Benchmark.csproj index 53261f805..dd6f9538b 100644 --- a/src/TensorFlowNet.Benchmarks/Tensorflow.Benchmark.csproj +++ b/tools/TensorFlowNET.Benchmarks/Tensorflow.Benchmark.csproj @@ -37,11 +37,11 @@ - + - + diff --git a/src/TensorFlowNet.Benchmarks/Unmanaged/StructCastBenchmark.cs b/tools/TensorFlowNET.Benchmarks/Unmanaged/StructCastBenchmark.cs similarity index 100% rename from src/TensorFlowNet.Benchmarks/Unmanaged/StructCastBenchmark.cs rename to tools/TensorFlowNET.Benchmarks/Unmanaged/StructCastBenchmark.cs diff --git a/src/TensorFlowNET.Console/Diagnostician.cs b/tools/TensorFlowNET.Console/Diagnostician.cs similarity index 100% rename from src/TensorFlowNET.Console/Diagnostician.cs rename to tools/TensorFlowNET.Console/Diagnostician.cs diff --git a/src/TensorFlowNET.Console/Exploring.cs b/tools/TensorFlowNET.Console/Exploring.cs similarity index 100% rename from src/TensorFlowNET.Console/Exploring.cs rename to tools/TensorFlowNET.Console/Exploring.cs diff --git a/src/TensorFlowNET.Console/MemoryBasicTest.cs b/tools/TensorFlowNET.Console/MemoryBasicTest.cs similarity index 97% rename from src/TensorFlowNET.Console/MemoryBasicTest.cs rename to tools/TensorFlowNET.Console/MemoryBasicTest.cs index 3b0deeabb..2bb11a02d 100644 --- a/src/TensorFlowNET.Console/MemoryBasicTest.cs +++ b/tools/TensorFlowNET.Console/MemoryBasicTest.cs @@ -112,7 +112,7 @@ public Action Conv2DWithTensor var strides = new[] { 1, 1, 1, 1 }; var dilations = new[] { 1, 1, 1, 1 }; - var results = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo("Conv2D", null, input, filter) + var results = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(tf.Context, "Conv2D", null, input, filter) { attrs = ConvertToDict(new { @@ -134,7 +134,7 @@ public Action Conv2DWithVariable var strides = new[] { 1, 1, 1, 1 }; var dilations = new[] { 1, 1, 1, 1 }; - var results = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo("Conv2D", null, input, filter) + var results = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(tf.Context, "Conv2D", null, input, filter) { attrs = ConvertToDict(new { diff --git a/src/TensorFlowNET.Console/MemoryFuncGraphTest.cs b/tools/TensorFlowNET.Console/MemoryFuncGraphTest.cs similarity index 100% rename from src/TensorFlowNET.Console/MemoryFuncGraphTest.cs rename to tools/TensorFlowNET.Console/MemoryFuncGraphTest.cs diff --git a/src/TensorFlowNET.Console/MemoryKerasTest.cs b/tools/TensorFlowNET.Console/MemoryKerasTest.cs similarity index 100% rename from src/TensorFlowNET.Console/MemoryKerasTest.cs rename to tools/TensorFlowNET.Console/MemoryKerasTest.cs diff --git a/src/TensorFlowNET.Console/MemoryMonitor.cs b/tools/TensorFlowNET.Console/MemoryMonitor.cs similarity index 100% rename from src/TensorFlowNET.Console/MemoryMonitor.cs rename to tools/TensorFlowNET.Console/MemoryMonitor.cs diff --git a/src/TensorFlowNET.Console/Program.cs b/tools/TensorFlowNET.Console/Program.cs similarity index 100% rename from src/TensorFlowNET.Console/Program.cs rename to tools/TensorFlowNET.Console/Program.cs diff --git a/src/TensorFlowNET.Console/SimpleRnnTest.cs b/tools/TensorFlowNET.Console/SimpleRnnTest.cs similarity index 90% rename from src/TensorFlowNET.Console/SimpleRnnTest.cs rename to tools/TensorFlowNET.Console/SimpleRnnTest.cs index 9769eb655..ae6ebb8a8 100644 --- a/src/TensorFlowNET.Console/SimpleRnnTest.cs +++ b/tools/TensorFlowNET.Console/SimpleRnnTest.cs @@ -20,7 +20,7 @@ public void Run() // whole_sequence_output has shape `[32, 10, 4]`. // final_state has shape `[32, 4]`. - var (whole_sequence_output, final_state) = simple_rnn.Apply(inputs); + var (whole_sequence_output, final_states) = simple_rnn.Apply(inputs); } } } diff --git a/src/TensorFlowNET.Console/Tensorflow.Console.csproj b/tools/TensorFlowNET.Console/Tensorflow.Console.csproj similarity index 71% rename from src/TensorFlowNET.Console/Tensorflow.Console.csproj rename to tools/TensorFlowNET.Console/Tensorflow.Console.csproj index 1b84bb145..bb60b6b63 100644 --- a/src/TensorFlowNET.Console/Tensorflow.Console.csproj +++ b/tools/TensorFlowNET.Console/Tensorflow.Console.csproj @@ -20,12 +20,9 @@ - - - - - - + + + diff --git a/tools/Tensorflow.CodeGen/DescriptionGenerator.cs b/tools/Tensorflow.CodeGen/DescriptionGenerator.cs new file mode 100644 index 000000000..0437370a1 --- /dev/null +++ b/tools/Tensorflow.CodeGen/DescriptionGenerator.cs @@ -0,0 +1,263 @@ +using Microsoft.CodeAnalysis.CSharp; +using Protobuf.Text; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection.Metadata.Ecma335; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading.Tasks; + +namespace Tensorflow.CodeGen +{ + public class DescriptionGenerator + { + private static readonly string replaceStrInner = "~~%~~"; + private static readonly string replaceStrInnerQuotationMarks = "^%^"; + Dictionary> _opDescriptions = new Dictionary>(); + Dictionary _opDescriptionDefs = new Dictionary(); + public DescriptionGenerator(string apiDefDirectory) + { + DirectoryInfo directory = new DirectoryInfo(apiDefDirectory); + + int errors = 0; + foreach (FileInfo file in directory.GetFiles()) + { + string target = file.Name.Split('.')[0].Split('_').Last(); + OpDef op = null; + try + { + op = ReadOpDefs(file.FullName).Op[0]; + } + catch + { + errors++; + continue; + } + _opDescriptionDefs[target] = op; + _opDescriptions[target] = new Dictionary(); + foreach (var arg in op.InputArg) + { + string argName = arg.Name; + var token = SyntaxFactory.ParseToken(argName); + if (token.IsKeyword()) + { + argName = $"{argName}_"; + } + _opDescriptions[target][argName] = arg.Description ?? ""; + } + foreach (var arg in op.Attr) + { + var token = SyntaxFactory.ParseToken(arg.Name); + string realKey = arg.Name; + if (token.IsKeyword()) + { + realKey += "_"; + } + _opDescriptions[target][realKey] = arg.Description ?? ""; + } + _opDescriptions[target]["SUMMARY"] = op.Summary ?? ""; + _opDescriptions[target]["DESC"] = op.Description ?? ""; + } + Console.WriteLine($"Warning: {errors} description files cannot be analyzed! Please revise it if " + + $"the failed files number is large, or ignore it."); + } + + /// + /// + /// + /// + /// + public void AppendDescription(OpDef fullOp, StringBuilder sb) + { + var opName = fullOp.Name; + if(_opDescriptions.TryGetValue(opName, out var op)) + { + var def = _opDescriptionDefs[opName]; + sb.AppendLine("/// "); + sb.AppendLine($"/// {op["SUMMARY"]}"); + sb.AppendLine("/// "); + + string totalDesc = op["DESC"]; + if (!string.IsNullOrEmpty(totalDesc)) + { + totalDesc = totalDesc.Replace(replaceStrInnerQuotationMarks, "\""); + sb.AppendLine("/// "); + string[] lines = totalDesc.Split(replaceStrInner); + foreach (var line in lines) + { + sb.AppendLine($"/// {line}"); + } + sb.AppendLine("/// "); + } + + var argNames = GetInputArgNames(fullOp); + foreach (var argName in argNames) + { + if(op.TryGetValue(argName, out var desc)) + { + desc = desc.Replace(replaceStrInnerQuotationMarks, "\""); + string[] lines = desc.Split(replaceStrInner); + sb.AppendLine($"/// "); + foreach (var line in lines) + { + sb.AppendLine($"/// {line}"); + } + sb.AppendLine("/// "); + } + else + { + sb.AppendLine($"/// "); + } + } + + List returnValueDescs = new(); + foreach (var arg in def.OutputArg) + { + if (!string.IsNullOrEmpty(arg.Description)) + { + returnValueDescs.Add($"{arg.Name}: {arg.Description}"); + } + } + string returnValueDesc = ""; + if (returnValueDescs.Count > 0) + { + returnValueDesc = string.Join(" && ", returnValueDescs); + } + sb.AppendLine($"/// {returnValueDesc}"); + } + else + { + sb.AppendLine("/// "); + sb.AppendLine($"///"); + sb.AppendLine("/// "); + + var argNames = GetInputArgNames(fullOp); + foreach (var argName in argNames) + { + sb.AppendLine($"/// "); + } + + sb.AppendLine($"/// "); + } + } + + /// + /// + /// + /// + /// + /// + /// + public List GetInputArgNames(OpDef op) + { + List names = new(); + foreach (var arg in op.InputArg) + { + string argName = arg.Name; + var token = SyntaxFactory.ParseToken(argName); + if (token.IsKeyword()) + { + argName = $"{argName}_"; + } + names.Add(argName); + } + var attrValueDic = Utils.GetAttrsDefaultValue(op, out var dynamicDefaultValues); + foreach (var (key, typeStr, value) in attrValueDic) + { + var token = SyntaxFactory.ParseToken(key); + string realKey = key; + if (token.IsKeyword()) + { + realKey += "_"; + } + names.Add(realKey); + } + return names; + } + + private static OpList ReadOpDefs(string path) + { + var text = File.ReadAllText(path); + text = RemoveLintTags(text); + text = PreProcessText(text); + + string pattern = @"< { + string matchedText = match.Value; + string innerText = match.Groups[1].Value; + innerText = innerText.Replace("\"", replaceStrInnerQuotationMarks) + .Replace("\r\n", replaceStrInner).Replace("\n", replaceStrInner); // 替换内部换行符 + return replaceStrPrefix + innerText + replaceStrSuffix; // 替换首尾 + }, RegexOptions.Multiline); + + var opDefs = new TextParser(TextParser.Settings.Default.WithIgnoreUnknownFields(true)).Parse(replacedText); + return opDefs; + } + + static string PreProcessText(string input) + { + int depth = 0; + int endBlockDepth = -1; + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < input.Length; i++) + { + char c = input[i]; + if (c == '{') + { + depth++; + sb.Append(c); + } + else if (c == '}') + { + if (depth == endBlockDepth) + { + sb.Append("END\n"); + endBlockDepth = -1; + } + sb.Append(c); + depth--; + } + else if (c == '<' && i + 5 < input.Length && input.Substring(i, 5) == "< x.IsRef, null); + sb.AppendLine($"throw new RuntimeError(\"{funcName} op does not support eager execution. Arg {possibleRefArg.Name} is a ref.\");"); + } + else + { + sb.Append("try\n{\n"); + + AppendFastPathExecute(op, sb); + if (outputArgsCount == 0) + { + sb.AppendLine("return null;"); + } + else if (outputArgsCount == 1 && string.IsNullOrEmpty(op.OutputArg[0].NumberAttr) + && string.IsNullOrEmpty(op.OutputArg[0].TypeListAttr)) + { + sb.AppendLine("return _fast_path_result[0];"); + } + else + { + sb.AppendLine("return _fast_path_result;"); + } + + sb.AppendLine("}"); // try + + sb.Append("catch(NotOkStatusException ex1)\n{\n"); + sb.AppendLine("throw ex1;"); + sb.AppendLine("}"); // catch + + sb.Append("catch(InvalidArgumentError ex2)\n{\n"); + sb.AppendLine("throw ex2;"); + sb.AppendLine("}"); // catch + + sb.Append("catch(Exception)\n{\n"); + sb.AppendLine("}"); // catch + + sb.Append("try\n{\n"); + AppendEagerFallbackCall(op, sb); + sb.AppendLine("}"); // try + + sb.Append("catch(Exception)\n{\n"); + sb.AppendLine("}"); // catch + } + + sb.AppendLine("}"); // if + + foreach(var (name, type, value) in attrValueDic.Where(x => x.Item2 == "string")) + { + if(value != "NOVALUE") + { + sb.AppendLine($"if({name} is null)"); + sb.AppendLine("{"); + sb.AppendLine($"{name} = {value};"); + sb.AppendLine("}"); + } + } + + // begin to use op helper. + AppendOpHelperCall(op, sb); + sb.AppendLine("var _result = _op.outputs;"); + + // check if it needs to record gradient. + sb.Append("if(_execute.must_record_gradient())\n{\n"); + sb.Append("object[] _attrs = new object[]{"); + foreach (var attr in op.Attr) + { + string attrRealName = attr.Name; + if (SyntaxFactory.ParseToken(attrRealName).IsKeyword()) + { + attrRealName += "_"; + } + if (attr.Type == "type") + { + sb.Append($"\"{attr.Name}\", _op._get_attr_type(\"{attrRealName}\"), "); + } + else if (attr.Type == "int") + { + sb.Append($"\"{attr.Name}\", _op._get_attr_int(\"{attrRealName}\"), "); + } + else if (attr.Type == "bool") + { + sb.Append($"\"{attr.Name}\", _op._get_attr_bool(\"{attrRealName}\"), "); + } + else + { + sb.Append($"\"{attr.Name}\", _op.get_attr(\"{attr.Name}\"), "); + } + } + if (sb[sb.Length - 1] == ' ' && sb[sb.Length - 2] == ',') + { + sb.Remove(sb.Length - 2, 2); + } + sb.Append("};\n"); + sb.AppendLine($"_execute.record_gradient(\"{op.Name}\", _op.inputs, _attrs, _result);"); + + sb.AppendLine("}"); // if + + if (outputArgsCount == 0) + { + sb.AppendLine("return _op;"); + } + else if (outputArgsCount == 1 && string.IsNullOrEmpty(op.OutputArg[0].NumberAttr) + && string.IsNullOrEmpty(op.OutputArg[0].TypeListAttr)) + { + sb.AppendLine("return _result[0];"); + } + else + { + sb.AppendLine("return _result;"); + } + sb.AppendLine("}"); // body + + sb.AppendLine(); + + AppendEagerFallbackDefinition(op, sb); + } + + public void AppendArgs(OpDef op, StringBuilder sb) + { + foreach (var arg in op.InputArg) + { + string argName = arg.Name; + var token = SyntaxFactory.ParseToken(argName); + if (token.IsKeyword()) + { + argName = $"{argName}_"; + } + if (!string.IsNullOrEmpty(arg.NumberAttr) || !string.IsNullOrEmpty(arg.TypeListAttr)) + { + sb.Append($"Tensors {argName}, "); + } + else + { + sb.Append($"Tensor {argName}, "); + } + } + var attrValueDic = Utils.GetAttrsDefaultValue(op, out var dynamicDefaultValues); + foreach (var (key, typeStr, value) in attrValueDic.Where(x => x.Item3 == "NOVALUE")) + { + var token = SyntaxFactory.ParseToken(key); + string realKey = key; + if (token.IsKeyword()) + { + realKey += "_"; + } + sb.Append($"{typeStr} {realKey}, "); + } + foreach (var (key, typeStr, value) in attrValueDic.Where(x => x.Item3 != "NOVALUE")) + { + var token = SyntaxFactory.ParseToken(key); + string realKey = key; + if (token.IsKeyword()) + { + realKey += "_"; + } + sb.Append($"{typeStr} {realKey} = {value}, "); + } + sb.Append($"string? name = null"); + } + + public void AppendFastPathExecute(OpDef op, StringBuilder sb) + { + sb.Append($"var _fast_path_result = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(_ctx, \"{op.Name}\", name)"); + sb.Append("{ args = new object[]{ "); + foreach (var arg in op.InputArg) + { + string attrArgName = arg.Name; + if (SyntaxFactory.ParseToken(attrArgName).IsKeyword()) + { + attrArgName += "_"; + } + sb.Append($"{attrArgName}, "); + } + if (sb[sb.Length - 1] == ' ' && sb[sb.Length - 2] == ',') + { + sb.Remove(sb.Length - 2, 2); + } + + sb.Append("}, attrs = new Dictionary(){ "); + var attrValueDic = Utils.GetAttrsDefaultValue(op, out var _); + foreach (var (key, _, _) in attrValueDic) + { + sb.Append($"[\"{key}\"] = {key}, "); + } + + if (sb[sb.Length - 1] == ' ' && sb[sb.Length - 2] == ',') + { + sb.Remove(sb.Length - 2, 2); + } + sb.Append("}});\n"); + } + + public void AppendEagerFallbackCall(OpDef op, StringBuilder sb) + { + string funcName = $"{Utils.ConvertToUnderscore(op.Name)}_eager_fallback"; + sb.Append($"return {funcName}("); + foreach (var arg in op.InputArg) + { + string inputArgRealName = arg.Name; + if (SyntaxFactory.ParseToken(inputArgRealName).IsKeyword()) + { + inputArgRealName += "_"; + } + sb.Append($"{inputArgRealName}, "); + } + var attrValueDic = Utils.GetAttrsDefaultValue(op, out var _); + foreach (var (key, _, _) in attrValueDic) + { + string keyRealName = key; + if (SyntaxFactory.ParseToken(keyRealName).IsKeyword()) + { + keyRealName += "_"; + } + sb.Append($"{key}: {keyRealName}, "); + } + sb.Append("name: name, ctx: _ctx);\n"); + } + + public void AppendEagerFallbackDefinition(OpDef op, StringBuilder sb) + { + sb.Append("public static "); + int outputArgsCount = op.OutputArg.Count; + if (outputArgsCount == 0) + { + sb.Append("Operation "); + } + else if (outputArgsCount == 1 && string.IsNullOrEmpty(op.OutputArg[0].NumberAttr) + && string.IsNullOrEmpty(op.OutputArg[0].TypeListAttr)) + { + sb.Append("Tensor "); + } + else + { + sb.Append("Tensor[] "); + } + string opName = op.Name; + string funcName = Utils.ConvertToUnderscore(op.Name); + sb.Append($" {funcName}_eager_fallback("); + AppendFallBackFunctionArgs(op, sb); + sb.Append(")\n{\n"); + + var possibleRefArg = op.InputArg.FirstOrDefault(x => x.IsRef, null); + if (possibleRefArg is not null) + { + sb.AppendLine($"throw new RuntimeError($\"{funcName} op does not support eager execution." + + $" Arg '{possibleRefArg.Name}' is a ref.\");"); + sb.AppendLine("}"); // body + return; + } + + if(op.InputArg.Any(x => !string.IsNullOrEmpty(x.NumberAttr))) + { + sb.AppendLine("List _inputs_flat_list = new();"); + foreach (var arg in op.InputArg) + { + string realArgName = arg.Name; + if (SyntaxFactory.ParseToken(realArgName).IsKeyword()) + { + realArgName = $"{realArgName}_"; + } + if (string.IsNullOrEmpty(arg.NumberAttr)) + { + sb.AppendLine($"_inputs_flat_list.Add({realArgName});"); + } + else + { + sb.AppendLine($"_inputs_flat_list.AddRange({realArgName});"); + } + } + sb.AppendLine($"var _inputs_flat = _inputs_flat_list.ToArray();"); + } + else + { + sb.Append("Tensor[] _inputs_flat = new Tensor[]{"); + foreach (var arg in op.InputArg) + { + string realArgName = arg.Name; + if (SyntaxFactory.ParseToken(realArgName).IsKeyword()) + { + realArgName = $"{realArgName}_"; + } + sb.Append($"{realArgName}, "); + } + if (sb[sb.Length - 1] == ' ' && sb[sb.Length - 2] == ',') + { + sb.Remove(sb.Length - 2, 2); + } + sb.Append("};\n"); + } + + sb.Append("object[] _attrs = new object[]{"); + foreach (var attr in op.Attr) + { + if (attr.Type == "type") + { + bool found = false; + foreach (var arg in op.InputArg) + { + string realArgName = arg.Name; + if (SyntaxFactory.ParseToken(realArgName).IsKeyword()) + { + realArgName = $"{realArgName}_"; + } + if (arg.TypeAttr == attr.Name) + { + sb.Append($"\"{attr.Name}\", {realArgName}.dtype, "); + found = true; + break; + } + } + if (!found) + { + string attrRealName = attr.Name; + if (SyntaxFactory.ParseToken(attrRealName).IsKeyword()) + { + attrRealName = $"{attrRealName}_"; + } + sb.Append($"\"{attr.Name}\", {attrRealName}, "); + } + } + else if(attr.Type == "list(type)") + { + if (op.InputArg.Any(x => x.TypeListAttr == attr.Name)) + { + continue; + } + } + else if(attr.Type == "int" && op.InputArg.Any(x => x.NumberAttr == attr.Name)) + { + bool found = false; + foreach (var arg in op.InputArg) + { + string realArgName = arg.Name; + if (SyntaxFactory.ParseToken(realArgName).IsKeyword()) + { + realArgName = $"{realArgName}_"; + } + if (arg.NumberAttr == attr.Name) + { + sb.Append($"\"{attr.Name}\", {realArgName}.Length, "); + found = true; + break; + } + } + } + else + { + sb.Append($"\"{attr.Name}\", {attr.Name}, "); + } + } + if (sb[sb.Length - 1] == ' ' && sb[sb.Length - 2] == ',') + { + sb.Remove(sb.Length - 2, 2); + } + sb.Append("};\n"); + + sb.AppendLine($"var _result = _execute.execute(\"{op.Name}\", {outputArgsCount}, inputs: _inputs_flat, " + + $"attrs: _attrs, ctx: ctx, name: name);"); + + sb.Append("if(_execute.must_record_gradient())\n{\n"); + + sb.AppendLine($"_execute.record_gradient(\"{op.Name}\", _inputs_flat, _attrs, _result);"); + + sb.AppendLine("}"); // if + + if (outputArgsCount == 0) + { + sb.AppendLine("return null;"); + } + else if (outputArgsCount == 1 && string.IsNullOrEmpty(op.OutputArg[0].NumberAttr) + && string.IsNullOrEmpty(op.OutputArg[0].TypeListAttr)) + { + sb.AppendLine("return _result[0];"); + } + else + { + sb.AppendLine("return _result;"); + } + + sb.AppendLine("}"); // body + } + + public void AppendFallBackFunctionArgs(OpDef op, StringBuilder sb) + { + foreach (var arg in op.InputArg) + { + string argName = arg.Name; + var token = SyntaxFactory.ParseToken(argName); + if (token.IsKeyword()) + { + argName = $"{argName}_"; + } + if (!string.IsNullOrEmpty(arg.NumberAttr)) + { + sb.Append($"Tensors {argName}, "); + } + else + { + sb.Append($"Tensor {argName}, "); + } + } + var attrValueDic = Utils.GetAttrsDefaultValue(op, out var _); + foreach (var (key, typeStr, _) in attrValueDic) + { + var token = SyntaxFactory.ParseToken(key); + string realKey = key; + if (token.IsKeyword()) + { + realKey += "_"; + } + sb.Append($"{typeStr} {realKey}, "); + } + sb.Append($"string name, Context ctx"); + } + + public void AppendOpHelperCall(OpDef op, StringBuilder sb) + { + sb.AppendLine("Dictionary keywords = new();"); + foreach (var arg in op.InputArg) + { + string realArgName = arg.Name; + if (SyntaxFactory.ParseToken(realArgName).IsKeyword()) + { + realArgName += "_"; + } + sb.AppendLine($"keywords[\"{arg.Name}\"] = {realArgName};"); + } + var attrValueDic = Utils.GetAttrsDefaultValue(op, out var _); + foreach (var (key, _, _) in attrValueDic) + { + sb.AppendLine($"keywords[\"{key}\"] = {key};"); + } + sb.AppendLine($"var _op = tf.OpDefLib._apply_op_helper(\"{op.Name}\", name, keywords);"); + } + + private static bool HasRefArgs(OpDef op) + { + return op.InputArg.Any(x => x.IsRef); + } + } +} diff --git a/tools/Tensorflow.CodeGen/GenOpsWriter.cs b/tools/Tensorflow.CodeGen/GenOpsWriter.cs new file mode 100644 index 000000000..9eefca07e --- /dev/null +++ b/tools/Tensorflow.CodeGen/GenOpsWriter.cs @@ -0,0 +1,81 @@ +using Protobuf.Text; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Tensorflow.CodeGen +{ + public class GenOpsWriter + { + private string _basePath; + private Dictionary _opMap; + private OpClassifier _opClassifier; + private FunctionGenerator _fg = new(); + private DescriptionGenerator _dg; + + public GenOpsWriter(string basePath, string pythonFilesDirectory, string apiDefFilesDirectory, string opDefFilename) + { + _basePath = basePath; + + var opDefs = Utils.ReadAllOpDefs(opDefFilename); + _opMap = opDefs.Op.ToDictionary( + x => Utils.ConvertToUnderscore(x.Name), x => x); + _opClassifier = new OpClassifier(pythonFilesDirectory, opDefs.Op.Select(x => Utils.ConvertToUnderscore(x.Name))); + _dg = new DescriptionGenerator(apiDefFilesDirectory); + } + + public void WriteAll() + { + foreach(var (target, set) in _opClassifier.OpSet) + { + StringBuilder sb = new StringBuilder(); + + // Write file header. + sb.AppendLine("/*Wrappers around TensorFlow ops. This file is MACHINE GENERATED! Do not edit.*/"); + sb.AppendLine(); + + // Add commonly used namespaces. + sb.AppendLine("using Tensorflow.Eager;"); + sb.AppendLine("using Tensorflow.Contexts;"); + sb.AppendLine("using Tensorflow.Exceptions;"); + sb.AppendLine("using static Tensorflow.Binding;"); + sb.AppendLine(); + + // Specify the namespace + sb.AppendLine("namespace Tensorflow;"); + sb.AppendLine(); + + // Write class name + sb.AppendLine($"public static class {target}"); + sb.AppendLine("{"); + + foreach(var funcName in set) + { + if(_opMap.ContainsKey(funcName)) + { + var opDef = _opMap[funcName]; + + // write the descriptions. + _dg.AppendDescription(opDef, sb); + + // write the function body. + _fg.AppendFunction(opDef, sb); + } + else if (funcName.StartsWith("_")) + { + var opDef = _opMap[funcName.Substring(1)]; + _fg.AppendFunction(opDef, sb); + } + } + + // Close class scope. + sb.AppendLine("}"); + + string fullFilePath = Path.Combine(_basePath, $"{target}.cs"); + File.WriteAllText(fullFilePath, sb.ToString()); + } + } + } +} diff --git a/tools/Tensorflow.CodeGen/OpClassifier.cs b/tools/Tensorflow.CodeGen/OpClassifier.cs new file mode 100644 index 000000000..2d22c5d22 --- /dev/null +++ b/tools/Tensorflow.CodeGen/OpClassifier.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Text.RegularExpressions; + +namespace Tensorflow.CodeGen +{ + public class OpClassifier + { + private static readonly string _filenamePattern = @"^gen_[a-z_]*_ops.py$"; + private static readonly string _pythonFunctionPattern = @"def\s+(\w+\d*\w*)\((?:\s*\w+\s*(?:=\s*[\S]*)*,\s*)*\s*name=None\):"; + private Dictionary> _opSet = new(); + public Dictionary> OpSet => _opSet; + public OpClassifier(string pythonFileFolder, IEnumerable funcNames) + { + DirectoryInfo directory = new DirectoryInfo(pythonFileFolder); + + Dictionary fileContentMap = new(); + foreach (FileInfo file in directory.GetFiles()) + { + if (Regex.IsMatch(file.Name, _filenamePattern)) + { + Console.WriteLine(file.Name); + string filenamePrefix = file.Name.Split('.')[0]; + string content = File.ReadAllText(file.FullName); + fileContentMap[filenamePrefix] = content; + } + } + + foreach(var funcName in funcNames) + { + Console.WriteLine(funcName); + string funcPattern = @$"^def\s+{funcName}\("; + string fallbackFuncPattern = @$"^def\s+{funcName}_eager_fallback\("; + foreach (var (target, content) in fileContentMap) + { + if(content.Contains($"def {funcName}") && content.Contains($"def {funcName}_eager_fallback")) + { + _opSet.SetDefault(target, new HashSet()).Add(funcName); + } + else if (content.Contains($"def _{funcName}") && content.Contains($"def _{funcName}_eager_fallback")) + { + _opSet.SetDefault(target, new HashSet()).Add(funcName); + } + } + } + } + } +} diff --git a/tools/Tensorflow.CodeGen/Program.cs b/tools/Tensorflow.CodeGen/Program.cs new file mode 100644 index 000000000..cea52e0b4 --- /dev/null +++ b/tools/Tensorflow.CodeGen/Program.cs @@ -0,0 +1,13 @@ +using OneOf.Types; +using Protobuf.Text; +using System.Diagnostics; +using System.Text; +using System.Xml.Linq; +using Tensorflow.CodeGen; + +GenOpsWriter writer = new(@"D:\development\tf.net\gen_ops_v2", + @"D:\Apps\miniconda3\envs\tf2.11\Lib\site-packages\tensorflow\python\ops", + @"D:\development\tf.net\tensorflow-2.11.0\tensorflow\core\api_def\base_api", + @"D:\development\tf.net\tensorflow-2.11.0\tensorflow\core\ops\ops.pbtxt"); + +writer.WriteAll(); diff --git a/tools/Tensorflow.CodeGen/Tensorflow.CodeGen.csproj b/tools/Tensorflow.CodeGen/Tensorflow.CodeGen.csproj new file mode 100644 index 000000000..2afc68a3c --- /dev/null +++ b/tools/Tensorflow.CodeGen/Tensorflow.CodeGen.csproj @@ -0,0 +1,18 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + + + + + diff --git a/tools/Tensorflow.CodeGen/Utils.cs b/tools/Tensorflow.CodeGen/Utils.cs new file mode 100644 index 000000000..6c69b7f95 --- /dev/null +++ b/tools/Tensorflow.CodeGen/Utils.cs @@ -0,0 +1,271 @@ +using Protobuf.Text; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection.Metadata.Ecma335; +using System.Text; +using System.Threading.Tasks; + +namespace Tensorflow.CodeGen +{ + public static class Utils + { + public static string ConvertToUnderscore(string input) + { + if (string.IsNullOrEmpty(input)) + { + return input; + } + + StringBuilder result = new StringBuilder(); + + int state = 1; // the previous char was not lowered. + for (int i = 0; i < input.Length; i++) + { + char current = input[i]; + + // 首字母不需要添加下划线 + if (char.IsUpper(current)) + { + if(i > 0) + { + char pre = input[i - 1]; + if (char.IsDigit(pre)) + { + result.Append(char.ToLower(current)); + continue; + } + } + if (state == 0) + { + result.Append("_"); + state = 1; + } + result.Append(char.ToLower(current)); + } + else + { + result.Append(char.ToLower(current)); + state = 0; + } + } + + return result.ToString(); + } + + public static OpList ReadAllOpDefs(string path) + { + var text = File.ReadAllText(path); + var opDefs = OpList.Parser.ParseText(text); + return opDefs; + } + + // name, type string, default value + public static List<(string, string, string)> GetAttrsDefaultValue(OpDef op, out Dictionary dynamicDefaultValues) + { + dynamicDefaultValues = new(); + List<(string, string, string)> res = new(); + foreach (var attr in op.Attr) + { + if (attr.Type == "type") + { + bool found = op.InputArg.Any(x => x.TypeAttr == attr.Name); + if (!found) + { + if (attr.DefaultValue is not null && attr.DefaultValue.ValueCase == AttrValue.ValueOneofCase.Type) + { + string name = Enum.GetName(typeof(TF_DataType), attr.DefaultValue.Type.as_tf_dtype()); + string enumPath = typeof(TF_DataType).Name + "." + name; + res.Add((attr.Name, "TF_DataType", enumPath)); + } + else + { + res.Add((attr.Name, "TF_DataType", "NOVALUE")); + } + } + } + else if (attr.Type == "int") + { + if (op.InputArg.Any(x => x.NumberAttr == attr.Name)) + { + continue; + } + if (attr.DefaultValue is not null && attr.DefaultValue.ValueCase == AttrValue.ValueOneofCase.I) + { + res.Add((attr.Name, "int", attr.DefaultValue.I.ToString())); + } + else + { + res.Add((attr.Name, "int", "0")); + } + } + else if (attr.Type == "float") + { + if (attr.DefaultValue is not null && attr.DefaultValue.ValueCase == AttrValue.ValueOneofCase.F) + { + res.Add((attr.Name, "float", attr.DefaultValue.F.ToString() + "f")); + } + else + { + res.Add((attr.Name, "float", "NOVALUE")); + } + } + else if (attr.Type == "string") + { + if (attr.DefaultValue is not null && attr.DefaultValue.ValueCase == AttrValue.ValueOneofCase.S) + { + res.Add((attr.Name, "string", $"\"{attr.DefaultValue.S.ToStringUtf8()}\"")); + } + else + { + res.Add((attr.Name, "string", "NOVALUE")); + } + } + else if (attr.Type == "bool") + { + if (attr.DefaultValue is not null && attr.DefaultValue.ValueCase == AttrValue.ValueOneofCase.B) + { + res.Add((attr.Name, "bool", attr.DefaultValue.B.ToString().ToLower())); + } + else + { + res.Add((attr.Name, "bool", "NOVALUE")); + } + } + else if (attr.Type == "shape") + { + if (attr.DefaultValue is not null && attr.DefaultValue.ValueCase == AttrValue.ValueOneofCase.Shape) + { + if (attr.DefaultValue.Shape.UnknownRank) + { + res.Add((attr.Name, "Shape", $"null")); + } + else + { + Shape shape = new Shape(attr.DefaultValue.Shape); + string expression = $"new Shape({string.Join(", ", shape.dims)})"; + dynamicDefaultValues[attr.Name] = expression; + res.Add((attr.Name, "Shape", $"null")); + } + } + else + { + res.Add((attr.Name, "Shape", "NOVALUE")); + } + } + else if (attr.Type == "list(type)") + { + if(op.InputArg.Any(x => x.TypeListAttr == attr.Name)) + { + continue; + } + if (attr.DefaultValue is not null && attr.DefaultValue.ValueCase == AttrValue.ValueOneofCase.Type) + { + List values = new(); + foreach (var value in attr.DefaultValue.List.Type) + { + values.Add(value.as_tf_dtype()); + } + string expression = "new TF_DataType[]{" + $"{string.Join(", ", values)}" + "}"; + dynamicDefaultValues[attr.Name] = expression; + res.Add((attr.Name, "TF_DataType[]", $"null")); + } + else + { + res.Add((attr.Name, "TF_DataType[]", "NOVALUE")); + } + } + else if (attr.Type == "list(shape)") + { + res.Add((attr.Name, "Shape[]", "NOVALUE")); + if (attr.DefaultValue is not null && attr.DefaultValue.ValueCase == AttrValue.ValueOneofCase.List) + { + List exps = new(); + foreach (var value in attr.DefaultValue.List.Shape) + { + exps.Add($"new Shape({string.Join(", ", value.Dim.Select(x => x.Size))})"); + } + string expression = "new Shape[]{" + $"{string.Join(", ", exps)}" + "}"; + dynamicDefaultValues[attr.Name] = expression; + res.Add((attr.Name, "string[]", $"null")); + } + else + { + res.Add((attr.Name, "string[]", "NOVALUE")); + } + } + else if (attr.Type == "list(string)") + { + if (attr.DefaultValue is not null && attr.DefaultValue.ValueCase == AttrValue.ValueOneofCase.List) + { + List values = new(); + foreach (var value in attr.DefaultValue.List.S) + { + values.Add(value.ToStringUtf8()); + } + string expression = "new string[]{" + $"{string.Join(", ", values)}" + "}"; + dynamicDefaultValues[attr.Name] = expression; + res.Add((attr.Name, "string[]", $"null")); + } + else + { + res.Add((attr.Name, "string[]", "NOVALUE")); + } + } + else if (attr.Type == "list(int)") + { + if (attr.DefaultValue is not null && attr.DefaultValue.ValueCase == AttrValue.ValueOneofCase.List) + { + List values = new(); + foreach (var value in attr.DefaultValue.List.I) + { + values.Add((int)value); + } + string expression = "new int[]{" + $"{string.Join(", ", values)}" + "}"; + dynamicDefaultValues[attr.Name] = expression; + res.Add((attr.Name, "int[]", $"null")); + } + else + { + res.Add((attr.Name, "int[]", "NOVALUE")); + } + } + else if (attr.Type == "list(float)") + { + if (attr.DefaultValue is not null && attr.DefaultValue.ValueCase == AttrValue.ValueOneofCase.List) + { + List values = new(); + foreach (var value in attr.DefaultValue.List.F) + { + values.Add(value); + } + string expression = "new float[]{" + $"{string.Join(", ", values)}" + "}"; + dynamicDefaultValues[attr.Name] = expression; + res.Add((attr.Name, "float[]", $"null")); + } + else + { + res.Add((attr.Name, "float[]", "NOVALUE")); + } + } + else if (attr.Type == "func") + { + res.Add((attr.Name, "object", "NOVALUE")); + } + else if (attr.Type == "list(func)") + { + res.Add((attr.Name, "object[]", "NOVALUE")); + } + else if (attr.Type == "tensor") + { + res.Add((attr.Name, "TensorProto", "NOVALUE")); + } + else + { + throw new NotImplementedException(); + } + } + return res; + } + } +} diff --git a/tools/Tensorflow.Redist.NativeLibrarySplitter/Program.cs b/tools/Tensorflow.Redist.NativeLibrarySplitter/Program.cs new file mode 100644 index 000000000..cdc011ea9 --- /dev/null +++ b/tools/Tensorflow.Redist.NativeLibrarySplitter/Program.cs @@ -0,0 +1,212 @@ + +// =================================================================== // +// This is a tool to split the native .so file of linux gpu library // +// =================================================================== // + +using System.Security.Cryptography; + +string filename = "libtensorflow.so"; +int count = 5; +SplitFile(filename, count); + +static void SplitFile(string filename, int count) +{ + // 打开读取二进制文件的文件流 + using (FileStream input = new FileStream(filename, FileMode.Open, FileAccess.Read)) + { + long filesize = new FileInfo(filename).Length; // 获取文件大小 + long fragmentSize = (long)(filesize / count + 1); // 计算每个分片的大小 + + byte[] buffer = new byte[fragmentSize]; // 设置缓冲区大小 + int bytesRead; // 存储读取长度 + int fragmentIndex = 1; // 分片计数器 + + // 使用循环遍历分片并写入相应的文件 + while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0) + { + string outputFileName = $"{filename}.fragment{fragmentIndex++}"; + using (FileStream output = new FileStream(outputFileName, FileMode.Create, FileAccess.Write)) + { + output.Write(buffer, 0, bytesRead); + } + } + + // 计算整个文件的 SHA-256 哈希值并写入 .sha 文件 + using (SHA256 sha256Hash = SHA256.Create()) + { + input.Seek(0, SeekOrigin.Begin); + byte[] hashValue = sha256Hash.ComputeHash(input); + + string shaFileName = $"{filename}.sha"; + using (StreamWriter writer = new StreamWriter(shaFileName, false)) + { + writer.Write(BitConverter.ToString(hashValue).Replace("-", "")); + } + } + } +} + +// Resume the file from fregments. Thanks for the code in TorchSharp! +static void Restitch(string RestitcherPackage) +{ + // !!!!!!!------------------------------NOTE------------------------------------!!!!!! + // !!!!!!! This code is manually copied into pkg\common\RestitchPackage.targets !!!!!! + // !!!!!!!------------------------------NOTE------------------------------------!!!!!! + // + // vvvvvvvvvvvvvvvvvvvvvvvvvvvvv START HERE vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv + try + { + if (Directory.Exists(RestitcherPackage)) + { + using (var writer = File.CreateText("obj/tensorflow_redist_build_log.txt")) + { + foreach (var p in Directory.EnumerateFiles(RestitcherPackage, "*", SearchOption.AllDirectories)) + { + + var primaryFile = Path.GetFullPath(p); + writer.WriteLine("Found primary file at {0}", primaryFile); + + // See if there are fragments in the parallel nuget packages. If the primary is + // some-package-primary\runtimes\....\a.so + // some-package-primary\runtimes\....\a.so.sha + // then the expected fragments are + // some-package-fragment1\fragments\....\a.so + // some-package-fragment2\fragments\....\a.so + // some-package-fragment3\fragments\....\a.so + // some-package-fragment4\fragments\....\a.so + // some-package-fragment5\fragments\....\a.so + // some-package-fragment6\fragments\....\a.so + // some-package-fragment7\fragments\....\a.so + // some-package-fragment8\fragments\....\a.so + // some-package-fragment9\fragments\....\a.so + // some-package-fragment10\fragments\....\a.so + var shaFile = primaryFile + ".sha"; + var fragmentFile1 = primaryFile.Replace("-primary", "-fragment1").Replace("runtimes", "fragments") + ".fragment1"; + var fragmentFile2 = primaryFile.Replace("-primary", "-fragment2").Replace("runtimes", "fragments") + ".fragment2"; + var fragmentFile3 = primaryFile.Replace("-primary", "-fragment3").Replace("runtimes", "fragments") + ".fragment3"; + var fragmentFile4 = primaryFile.Replace("-primary", "-fragment4").Replace("runtimes", "fragments") + ".fragment4"; + var fragmentFile5 = primaryFile.Replace("-primary", "-fragment5").Replace("runtimes", "fragments") + ".fragment5"; + + + if (File.Exists(fragmentFile1)) writer.WriteLine("Found fragment file at {0}", fragmentFile1); + if (File.Exists(fragmentFile2)) writer.WriteLine("Found fragment file at {0}", fragmentFile2); + if (File.Exists(fragmentFile3)) writer.WriteLine("Found fragment file at {0}", fragmentFile3); + if (File.Exists(fragmentFile4)) writer.WriteLine("Found fragment file at {0}", fragmentFile4); + if (File.Exists(fragmentFile5)) writer.WriteLine("Found fragment file at {0}", fragmentFile5); + + if (File.Exists(fragmentFile1)) + { + var tmpFile = Path.GetTempFileName(); + + { + writer.WriteLine("Writing restored primary file at {0}", tmpFile); + using (var os = File.OpenWrite(tmpFile)) + { + + //writer.WriteLine("Writing bytes from {0} to {1}", primaryFile, tmpFile); + //var primaryBytes = File.ReadAllBytes(primaryFile); + + //os.Write(primaryBytes, 0, primaryBytes.Length); + if (File.Exists(fragmentFile1)) + { + writer.WriteLine("Writing fragment bytes from {0} to {1}", fragmentFile1, tmpFile); + var fragmentBytes1 = File.ReadAllBytes(fragmentFile1); + os.Write(fragmentBytes1, 0, fragmentBytes1.Length); + } + if (File.Exists(fragmentFile2)) + { + writer.WriteLine("Writing fragment bytes from {0} to {1}", fragmentFile2, tmpFile); + var fragmentBytes2 = File.ReadAllBytes(fragmentFile2); + os.Write(fragmentBytes2, 0, fragmentBytes2.Length); + } + if (File.Exists(fragmentFile3)) + { + writer.WriteLine("Writing fragment bytes from {0} to {1}", fragmentFile3, tmpFile); + var fragmentBytes3 = File.ReadAllBytes(fragmentFile3); + os.Write(fragmentBytes3, 0, fragmentBytes3.Length); + } + if (File.Exists(fragmentFile4)) + { + writer.WriteLine("Writing fragment bytes from {0} to {1}", fragmentFile4, tmpFile); + var fragmentBytes4 = File.ReadAllBytes(fragmentFile4); + os.Write(fragmentBytes4, 0, fragmentBytes4.Length); + } + if (File.Exists(fragmentFile5)) + { + writer.WriteLine("Writing fragment bytes from {0} to {1}", fragmentFile5, tmpFile); + var fragmentBytes5 = File.ReadAllBytes(fragmentFile5); + os.Write(fragmentBytes5, 0, fragmentBytes5.Length); + } + } + } + + var shaExpected = File.Exists(shaFile) ? File.ReadAllText(shaFile).ToUpper() : ""; + writer.WriteLine($"real sha: {shaExpected}"); + + using (var sha256Hash = System.Security.Cryptography.SHA256.Create()) + { + using (var os2 = File.OpenRead(tmpFile)) + { + + byte[] bytes = sha256Hash.ComputeHash(os2); + var builder = new System.Text.StringBuilder(); + for (int i = 0; i < bytes.Length; i++) + { + builder.Append(bytes[i].ToString("x2")); + } + var shaReconstituted = builder.ToString().ToUpper(); + if (shaExpected != shaReconstituted) + { + string msg = + $"Error downloading and reviving packages. Reconsituted file contents have incorrect SHA\n\tExpected SHA: ${shaExpected}\n\tActual SHA: ${shaReconstituted}\n\tFile was reconstituted from:" + + $"\n\t{primaryFile} (length ${new FileInfo(primaryFile).Length})" + + (File.Exists(fragmentFile1) ? $"\n\t{fragmentFile1} (length ${new FileInfo(fragmentFile1).Length})" : "") + + (File.Exists(fragmentFile2) ? $"\n\t{fragmentFile2} (length ${new FileInfo(fragmentFile2).Length})" : "") + + (File.Exists(fragmentFile3) ? $"\n\t{fragmentFile3} (length ${new FileInfo(fragmentFile3).Length})" : "") + + (File.Exists(fragmentFile4) ? $"\n\t{fragmentFile4} (length ${new FileInfo(fragmentFile4).Length})" : "") + + (File.Exists(fragmentFile5) ? $"\n\t{fragmentFile5} (length ${new FileInfo(fragmentFile5).Length})" : ""); + writer.WriteLine(msg); + throw new Exception(msg); + } + } + + } + + writer.WriteLine("Deleting {0}", primaryFile); + File.Delete(primaryFile); + if (File.Exists(primaryFile)) + throw new Exception("wtf?"); + + writer.WriteLine("Moving {0} --> {1}", tmpFile, primaryFile); + File.Move(tmpFile, primaryFile); + + writer.WriteLine("Deleting {0}", fragmentFile1); + File.Delete(fragmentFile1); // free up space and prevent us doing this again + + writer.WriteLine("Deleting {0}", fragmentFile2); + if (File.Exists(fragmentFile2)) + File.Delete(fragmentFile2); // free up space and prevent us doing this again + + writer.WriteLine("Deleting {0}", fragmentFile3); + if (File.Exists(fragmentFile3)) + File.Delete(fragmentFile3); // free up space and prevent us doing this again + + writer.WriteLine("Deleting {0}", fragmentFile4); + if (File.Exists(fragmentFile4)) + File.Delete(fragmentFile4); // free up space and prevent us doing this again + + writer.WriteLine("Deleting {0}", fragmentFile5); + if (File.Exists(fragmentFile5)) + File.Delete(fragmentFile5); // free up space and prevent us doing this again + } + } + } + } + } + catch (Exception ex) + { + Console.Error.WriteLine(ex.ToString()); + Console.Error.WriteLine(ex.StackTrace); + } + // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ END HERE^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +} \ No newline at end of file diff --git a/tools/Tensorflow.Redist.NativeLibrarySplitter/Tensorflow.Redist.NativeLibrarySplitter.csproj b/tools/Tensorflow.Redist.NativeLibrarySplitter/Tensorflow.Redist.NativeLibrarySplitter.csproj new file mode 100644 index 000000000..74abf5c97 --- /dev/null +++ b/tools/Tensorflow.Redist.NativeLibrarySplitter/Tensorflow.Redist.NativeLibrarySplitter.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/helpers/Tensorflow.UnitTest.RedistHolder/EmptyClass.cs b/tools/Tensorflow.UnitTest.RedistHolder/EmptyClass.cs similarity index 100% rename from helpers/Tensorflow.UnitTest.RedistHolder/EmptyClass.cs rename to tools/Tensorflow.UnitTest.RedistHolder/EmptyClass.cs diff --git a/helpers/Tensorflow.UnitTest.RedistHolder/Tensorflow.UnitTest.RedistHolder.csproj b/tools/Tensorflow.UnitTest.RedistHolder/Tensorflow.UnitTest.RedistHolder.csproj similarity index 95% rename from helpers/Tensorflow.UnitTest.RedistHolder/Tensorflow.UnitTest.RedistHolder.csproj rename to tools/Tensorflow.UnitTest.RedistHolder/Tensorflow.UnitTest.RedistHolder.csproj index 878077582..0d1018cab 100644 --- a/helpers/Tensorflow.UnitTest.RedistHolder/Tensorflow.UnitTest.RedistHolder.csproj +++ b/tools/Tensorflow.UnitTest.RedistHolder/Tensorflow.UnitTest.RedistHolder.csproj @@ -5,7 +5,7 @@ - + diff --git a/scripts/Copy-NativeTensorFlowLibs.ps1 b/tools/scripts/Copy-NativeTensorFlowLibs.ps1 similarity index 100% rename from scripts/Copy-NativeTensorFlowLibs.ps1 rename to tools/scripts/Copy-NativeTensorFlowLibs.ps1 diff --git a/tensorflowlib/README.md b/tools/tensorflowlib/README.md similarity index 100% rename from tensorflowlib/README.md rename to tools/tensorflowlib/README.md