Browse Source

create constant by NumSharp NDArray.

tags/v0.1.0-Tensor
Oceania2018 7 years ago
parent
commit
78685bd5cb
9 changed files with 101 additions and 59 deletions
  1. +5
    -5
      TensorFlow.NET.sln
  2. +2
    -6
      src/TensorFlowNET.Core/Operations/ops.cs
  3. +17
    -0
      src/TensorFlowNET.Core/TensorFlowNET.Core.csproj
  4. +5
    -4
      src/TensorFlowNET.Core/Tensors/constant_op.cs
  5. +46
    -41
      src/TensorFlowNET.Core/Tensors/tensor_util.cs
  6. +4
    -3
      src/TensorFlowNET.Core/Tensors/tf.constant.cs
  7. +4
    -0
      test/TensorFlowNET.Examples/TensorFlowNET.Examples.csproj
  8. +13
    -0
      test/TensorFlowNET.UnitTest/ConstantTest.cs
  9. +5
    -0
      test/TensorFlowNET.UnitTest/TensorFlowNET.UnitTest.csproj

+ 5
- 5
TensorFlow.NET.sln View File

@@ -9,7 +9,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TensorFlowNET.Core", "src\T
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TensorFlowNET.Examples", "test\TensorFlowNET.Examples\TensorFlowNET.Examples.csproj", "{1FE60088-157C-4140-91AB-E96B915E4BAE}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TensorFlowNET.Examples", "test\TensorFlowNET.Examples\TensorFlowNET.Examples.csproj", "{1FE60088-157C-4140-91AB-E96B915E4BAE}"
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NumSharp.Core", "..\NumSharp\src\NumSharp.Core\NumSharp.Core.csproj", "{6ACED8FF-F08E-40E6-A75D-D01BAAA41072}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NumSharp.Core", "..\NumSharp\src\NumSharp.Core\NumSharp.Core.csproj", "{DA680126-DA60-4CE3-9094-72C355C081D3}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -29,10 +29,10 @@ Global
{1FE60088-157C-4140-91AB-E96B915E4BAE}.Debug|Any CPU.Build.0 = Debug|Any CPU {1FE60088-157C-4140-91AB-E96B915E4BAE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1FE60088-157C-4140-91AB-E96B915E4BAE}.Release|Any CPU.ActiveCfg = Release|Any CPU {1FE60088-157C-4140-91AB-E96B915E4BAE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1FE60088-157C-4140-91AB-E96B915E4BAE}.Release|Any CPU.Build.0 = Release|Any CPU {1FE60088-157C-4140-91AB-E96B915E4BAE}.Release|Any CPU.Build.0 = Release|Any CPU
{6ACED8FF-F08E-40E6-A75D-D01BAAA41072}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6ACED8FF-F08E-40E6-A75D-D01BAAA41072}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6ACED8FF-F08E-40E6-A75D-D01BAAA41072}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6ACED8FF-F08E-40E6-A75D-D01BAAA41072}.Release|Any CPU.Build.0 = Release|Any CPU
{DA680126-DA60-4CE3-9094-72C355C081D3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DA680126-DA60-4CE3-9094-72C355C081D3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DA680126-DA60-4CE3-9094-72C355C081D3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DA680126-DA60-4CE3-9094-72C355C081D3}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE


+ 2
- 6
src/TensorFlowNET.Core/Operations/ops.cs View File

@@ -18,12 +18,8 @@ namespace Tensorflow


public static Tensor convert_to_tensor(object value, string name = "") public static Tensor convert_to_tensor(object value, string name = "")
{ {
return internal_convert_to_tensor(value, name);
}

private static Tensor internal_convert_to_tensor(object value, string name = "")
{
return tf.constant(value);
var nd = tensor_util.convert_to_numpy_ndarray(value);
return tf.constant(nd, name);
} }


public static unsafe IntPtr _create_c_op(Graph graph, NodeDef node_def, List<Tensor> inputs) public static unsafe IntPtr _create_c_op(Graph graph, NodeDef node_def, List<Tensor> inputs)


+ 17
- 0
src/TensorFlowNET.Core/TensorFlowNET.Core.csproj View File

@@ -4,6 +4,18 @@
<TargetFramework>netstandard2.0</TargetFramework> <TargetFramework>netstandard2.0</TargetFramework>
<AssemblyName>TensorFlow.NET</AssemblyName> <AssemblyName>TensorFlow.NET</AssemblyName>
<RootNamespace>Tensorflow</RootNamespace> <RootNamespace>Tensorflow</RootNamespace>
<Version>0.0.2</Version>
<Authors>Haiping Chen</Authors>
<Company>SciSharp.org</Company>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Copyright>Apache 2.0</Copyright>
<RepositoryUrl>https://github.com/SciSharp/TensorFlow.NET</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageProjectUrl>https://github.com/SciSharp</PackageProjectUrl>
<PackageIconUrl>https://avatars3.githubusercontent.com/u/44989469?s=200&amp;v=4</PackageIconUrl>
<PackageTags>TensorFlow, NumSharp, SciSharp, MachineLearning, TensorFlow.NET</PackageTags>
<Description>TensorFlow binding for .NET Standard.</Description>
<AssemblyVersion>0.0.2.0</AssemblyVersion>
</PropertyGroup> </PropertyGroup>


<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
@@ -11,12 +23,17 @@
<DefineConstants>DEBUG;TRACE</DefineConstants> <DefineConstants>DEBUG;TRACE</DefineConstants>
</PropertyGroup> </PropertyGroup>


<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup> <ItemGroup>
<None Remove="Protobuf\README.md" /> <None Remove="Protobuf\README.md" />
</ItemGroup> </ItemGroup>


<ItemGroup> <ItemGroup>
<PackageReference Include="Google.Protobuf" Version="3.6.1" /> <PackageReference Include="Google.Protobuf" Version="3.6.1" />
<PackageReference Include="NumSharp" Version="0.6.2" />
</ItemGroup> </ItemGroup>


<ItemGroup> <ItemGroup>


+ 5
- 4
src/TensorFlowNET.Core/Tensors/constant_op.cs View File

@@ -1,4 +1,5 @@
using System;
using NumSharp.Core;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;


@@ -18,11 +19,11 @@ namespace Tensorflow
/// <param name="name">Optional name for the tensor.</param> /// <param name="name">Optional name for the tensor.</param>
/// <param name="verify_shape">Boolean that enables verification of a shape of values.</param> /// <param name="verify_shape">Boolean that enables verification of a shape of values.</param>
/// <returns></returns> /// <returns></returns>
public static Tensor Create(object value, TF_DataType dtype = TF_DataType.DtInvalid, TensorShape shape = null, string name = "Const", bool verify_shape = false)
public static Tensor Create(NDArray nd, string name = "Const", bool verify_shape = false)
{ {
Graph g = ops.get_default_graph(); Graph g = ops.get_default_graph();
var tensor_value = new AttrValue(); var tensor_value = new AttrValue();
var tensor_pb = tensor_util.make_tensor_proto(value, dtype, shape, verify_shape);
var tensor_pb = tensor_util.make_tensor_proto(nd, verify_shape);
tensor_value.Tensor = tensor_pb; tensor_value.Tensor = tensor_pb;
var dtype_value = new AttrValue var dtype_value = new AttrValue
{ {
@@ -33,7 +34,7 @@ namespace Tensorflow
attrs["dtype"] = dtype_value; attrs["dtype"] = dtype_value;
attrs["value"] = tensor_value; attrs["value"] = tensor_value;
var const_tensor = g.create_op("Const", null, new TF_DataType[] { (TF_DataType)dtype_value.Type }, attrs: attrs).outputs[0]; var const_tensor = g.create_op("Const", null, new TF_DataType[] { (TF_DataType)dtype_value.Type }, attrs: attrs).outputs[0];
const_tensor.value = value;
const_tensor.value = nd.Data();


return const_tensor; return const_tensor;
} }


+ 46
- 41
src/TensorFlowNET.Core/Tensors/tensor_util.cs View File

@@ -1,4 +1,5 @@
using NumSharp.Core; using NumSharp.Core;
using NumSharp.Core.Interfaces;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;
@@ -8,66 +9,70 @@ namespace Tensorflow
{ {
public static class tensor_util public static class tensor_util
{ {
public static TensorProto make_tensor_proto(object values, TF_DataType dtype = TF_DataType.DtInvalid, Shape shape = null, bool verify_shape = false)
public static TensorProto make_tensor_proto(NDArray nd, bool verify_shape = false)
{ {
NDArray nparray;
TensorProto tensor_proto = null;
TF_DataType numpy_dtype;
if(shape is null)
var shape = nd.Storage.Shape;

var numpy_dtype = dtypes.as_dtype(nd.dtype);
var tensor_proto = new tensor_pb2.TensorProto
{
Dtype = numpy_dtype.as_datatype_enum(),
TensorShape = shape.as_shape(nd.shape).as_proto()
};

switch (nd.dtype.Name)
{ {
shape = new Shape();
case "Int32":
tensor_proto.IntVal.AddRange(nd.Data<int>());
break;
case "Single":
tensor_proto.FloatVal.AddRange(nd.Data<float>());
break;
case "Double":
tensor_proto.DoubleVal.AddRange(nd.Data<double>());
break;
case "String":
tensor_proto.StringVal.Add(Google.Protobuf.ByteString.CopyFrom(nd.Data<string>()[0], Encoding.UTF8));
break;
default:
throw new Exception("Not Implemented");
} }


return tensor_proto;
}

public static NDArray convert_to_numpy_ndarray(object values)
{
NDArray nd;

switch (values) switch (values)
{ {
case NDArray val:
nd = val;
break;
case int val: case int val:
nparray = np.asarray(val);
numpy_dtype = dtypes.as_dtype(nparray.dtype);
tensor_proto = new tensor_pb2.TensorProto
{
Dtype = numpy_dtype.as_datatype_enum(),
TensorShape = shape.as_shape(nparray.shape).as_proto()
};
tensor_proto.IntVal.Add(val);
nd = np.asarray(val);
break;
case int[] val:
nd = np.array(val);
break; break;
case float val: case float val:
nparray = np.asarray(val);
numpy_dtype = dtypes.as_dtype(nparray.dtype);
tensor_proto = new tensor_pb2.TensorProto
{
Dtype = numpy_dtype.as_datatype_enum(),
TensorShape = shape.as_shape(nparray.shape).as_proto()
};
tensor_proto.FloatVal.Add(val);
nd = np.asarray(val);
break; break;
case double val: case double val:
nparray = np.asarray(val);
numpy_dtype = dtypes.as_dtype(nparray.dtype);
tensor_proto = new tensor_pb2.TensorProto
{
Dtype = numpy_dtype.as_datatype_enum(),
TensorShape = shape.as_shape(nparray.shape).as_proto()
};
tensor_proto.DoubleVal.Add(val);
nd = np.asarray(val);
break; break;
case string val: case string val:
nparray = np.asarray(val);
numpy_dtype = dtypes.as_dtype(nparray.dtype);
tensor_proto = new tensor_pb2.TensorProto
{
Dtype = numpy_dtype.as_datatype_enum(),
TensorShape = shape.as_shape(nparray.shape).as_proto()
};
tensor_proto.StringVal.Add(Google.Protobuf.ByteString.CopyFrom(val, Encoding.UTF8));
nd = np.asarray(val);
break; break;
default: default:
throw new Exception("Not Implemented"); throw new Exception("Not Implemented");
} }


return tensor_proto;
return nd;
} }


public static TensorShape as_shape(this Shape shape, int[] dims)
public static TensorShape as_shape(this IShape shape, int[] dims)
{ {
return new TensorShape(dims); return new TensorShape(dims);
} }
@@ -80,7 +85,7 @@ namespace Tensorflow
{ {
var dim = new TensorShapeProto.Types.Dim(); var dim = new TensorShapeProto.Types.Dim();
dim.Size = tshape.Dimensions[i]; dim.Size = tshape.Dimensions[i];
dim.Name = $"{dim}_1";
dim.Name = $"dim_{i}";


shape.Dim.Add(dim); shape.Dim.Add(dim);
} }


+ 4
- 3
src/TensorFlowNET.Core/Tensors/tf.constant.cs View File

@@ -1,4 +1,5 @@
using System;
using NumSharp.Core;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;


@@ -6,9 +7,9 @@ namespace Tensorflow
{ {
public static partial class tf public static partial class tf
{ {
public static Tensor constant(object value, TF_DataType dtype = TF_DataType.DtInvalid, TensorShape shape = null, string name = "Const", bool verify_shape = false)
public static Tensor constant(NDArray value, string name = "Const", bool verify_shape = false)
{ {
return constant_op.Create(value, dtype, shape, name, verify_shape);
return constant_op.Create(value, name, verify_shape);
} }
} }
} }

+ 4
- 0
test/TensorFlowNET.Examples/TensorFlowNET.Examples.csproj View File

@@ -5,6 +5,10 @@
<TargetFramework>netcoreapp2.1</TargetFramework> <TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup> </PropertyGroup>


<ItemGroup>
<PackageReference Include="NumSharp" Version="0.6.2" />
</ItemGroup>

<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\src\TensorFlowNET.Core\TensorFlowNET.Core.csproj" /> <ProjectReference Include="..\..\src\TensorFlowNET.Core\TensorFlowNET.Core.csproj" />
</ItemGroup> </ItemGroup>


+ 13
- 0
test/TensorFlowNET.UnitTest/ConstantTest.cs View File

@@ -1,4 +1,5 @@
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
using NumSharp.Core;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;
@@ -24,5 +25,17 @@ namespace TensorFlowNET.UnitTest
{ {
tensor = tf.constant("Elephant"); tensor = tf.constant("Elephant");
} }

[TestMethod]
public void NDimConst()
{
var nd = np.array(new int[][]
{
new int[]{ 1, 2, 3 },
new int[]{ 4, 5, 6 }
});

tensor = tf.constant(nd);
}
} }
} }

+ 5
- 0
test/TensorFlowNET.UnitTest/TensorFlowNET.UnitTest.csproj View File

@@ -11,10 +11,15 @@
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup> </PropertyGroup>


<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="MSTest.TestAdapter" Version="1.4.0" /> <PackageReference Include="MSTest.TestAdapter" Version="1.4.0" />
<PackageReference Include="MSTest.TestFramework" Version="1.4.0" /> <PackageReference Include="MSTest.TestFramework" Version="1.4.0" />
<PackageReference Include="NumSharp" Version="0.6.2" />
</ItemGroup> </ItemGroup>


<ItemGroup> <ItemGroup>


Loading…
Cancel
Save