Skip to content
This repository has been archived by the owner on Mar 12, 2020. It is now read-only.

Commit

Permalink
Working on Examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Deepak Kumar committed Apr 9, 2019
1 parent 56b143e commit 197fab0
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 38 deletions.
4 changes: 2 additions & 2 deletions Backends/SiaNet.Backend.ArrayFire/Backend/SiaNetBackend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -606,9 +606,9 @@ public Tensor Tile(Tensor x, int n, int axis = 0)
for (int i = 0; i < dims.Length; i++)
{
if (i == axis)
dims[i] = (uint)n;
else
dims[i] = 1;
else
dims[i] = (uint)n;
}

return Out(Data.Tile(In(x), dims));
Expand Down
32 changes: 17 additions & 15 deletions Backends/SiaNet.Backend.CNTK/SiaNetBackend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,16 @@ public Tensor CreateVariable(float[] data, long[] shape, string name = "")
return Out(v);
}

private Tensor CreateVariable(int[] data, long[] shape, string name = "")
{
shape = BackendUtil.Row2ColMajor(shape);

var arr = new CNTK.NDArrayView(BackendUtil.CastShapeInt(shape), Array.ConvertAll(data, x => (float)x), DeviceManager.Current);
var v = new CNTK.Variable(BackendUtil.CastShapeInt(shape), VariableKind.Input, CNTK.DataType.Float, arr, false, new AxisVector(), false, name, name);

return Out(v);
}

public Tensor Diag(Tensor x)
{
throw new NotImplementedException();
Expand Down Expand Up @@ -418,7 +428,6 @@ public Tensor Min(Tensor x, params int[] dims)
x = Min(x, CorrDim(x.DimCount, item));
}


return x;
}

Expand Down Expand Up @@ -459,7 +468,8 @@ public Tensor Pow(Tensor x, float value)

public void Print(Tensor x, string title = "")
{
BackendUtil.Print(x.Shape, x.ToArray());
Console.WriteLine(In(x).GetValue().ToString());
//BackendUtil.Print(x.Shape, x.ToArray());
}

public Tensor RandomBernoulli(long[] shape, float p)
Expand Down Expand Up @@ -530,15 +540,12 @@ public Tensor Sinh(Tensor x)

public Tensor SliceCols(Tensor x, long start, long end)
{
end = end + 1;
var t = C.Transpose(In(x));
return Out(C.Slice(In(x), AxisVector.Repeat(new Axis(0), 1), IntVector.Repeat((int)start, 1), IntVector.Repeat((int)end, 1)));
}

public Tensor SliceRows(Tensor x, long start, long end)
{
end = end + 1;
return Out(C.Slice(In(x), AxisVector.Repeat(new Axis(x.DimCount - 1), 1), IntVector.Repeat((int)start, 1), IntVector.Repeat((int)end, 1)));
return Out(C.Slice(In(x), new AxisVector(new Axis[] {new Axis(0)}), new IntVector(new int[] { 1, 1, 1, 1, 1, 1 }), new IntVector(new int[] { 3, 3, 3, 3, 3, 3 })));
}

public Tensor Softmax(Tensor x, int axis = -1)
Expand Down Expand Up @@ -613,15 +620,10 @@ public Tensor Tanh(Tensor x)
public Tensor Tile(Tensor x, int n, int axis = 0)
{
axis = CorrDim(x.DimCount, axis);

uint[] dims = new uint[x.DimCount];
for (int i = 0; i < dims.Length; i++)
{
if (i == axis)
dims[i] = (uint)n;
else
dims[i] = 1;
}
if (axis == 0)
axis = 1;
else
axis = 1;

return Out(C.Splice(VariableVector.Repeat(In(x), n), new Axis(axis)));
}
Expand Down
12 changes: 5 additions & 7 deletions Examples/BackendTester/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,20 @@
using SiaNet.Initializers;
using System;
using System.Linq;
using System.Numerics.Tensors;

namespace BackendTester
{
class Program
{
static void Main(string[] args)
{
Global.UseEngine(SiaNet.Backend.ArrayFire.SiaNetBackend.Instance, DeviceType.CPU);
Global.UseEngine(SiaNet.Backend.CNTKLib.SiaNetBackend.Instance, DeviceType.CPU);
var K = Global.CurrentBackend;

var data = new float[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
var arr = np.array(data);
arr = arr.reshape(6, 3);

var a = K.CreateVariable(new float[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, new long[] { 3, 3 });
var a = K.CreateVariable(new float[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, new long[] { 6, 3 });
a.Print();

var sliced = K.SliceRows(a, 1, 2);

//var d = K.CreateVariable(new float[] { 1 }, new long[] { 1, 1 });
//c = c + d;
Expand Down
4 changes: 2 additions & 2 deletions Examples/BasicClassificationWithTitanicDataset/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Program
static void Main(string[] args)
{
//Setup Engine
Global.UseEngine(SiaNet.Backend.MxNetLib.SiaNetBackend.Instance, DeviceType.Default);
Global.UseEngine(SiaNet.Backend.TensorSharp.SiaNetBackend.Instance, DeviceType.Default);

var train = LoadTrain(); //Load train data
var test = LoadTest(); //Load test data
Expand All @@ -31,7 +31,7 @@ static void Main(string[] args)
model.Compile(OptimizerType.Adam, LossType.BinaryCrossEntropy, MetricType.BinaryAccurary);

// Perform training with train and val dataset
model.Train(train, epochs: 100, batchSize: 128);
model.Train(train, epochs: 100, batchSize: 32);

//var prediction = model.Predict(test);
//TOps.Round(prediction).Print();
Expand Down
2 changes: 1 addition & 1 deletion Examples/GettingStarted/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ static void Main(string[] args)
{
//Setup Engine

Global.UseEngine(SiaNet.Backend.TensorSharp.SiaNetBackend.Instance, DeviceType.CPU);
Global.UseEngine(SiaNet.Backend.MxNetLib.SiaNetBackend.Instance, DeviceType.CPU);

//Prep Data
var (x, y) = PrepDataset();
Expand Down
1 change: 0 additions & 1 deletion SiaNet.Engine/SiaNet.Engine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
<ItemGroup>
<PackageReference Include="System.Drawing.Common" Version="4.5.1" />
<PackageReference Include="System.Drawing.Primitives" Version="4.3.0" />
<PackageReference Include="System.Numerics.Tensors" Version="0.2.0-preview3.19128.7" />
</ItemGroup>

</Project>
4 changes: 2 additions & 2 deletions SiaNet.Engine/Tensor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,12 +250,12 @@ private static ValueTuple<Tensor, Tensor> BroadcastTensor(Tensor lhs, Tensor rhs
{
if (lhs.Shape[0] == 1)
{
lhs = lhs.RepeatTensor(rhs.Shape[0], 0);
lhs = lhs.RepeatTensor(rhs.Shape[0], 1);
}

if (rhs.Shape[0] == 1)
{
rhs = rhs.RepeatTensor(lhs.Shape[0], 0);
rhs = rhs.RepeatTensor(lhs.Shape[0], 1);
}
}

Expand Down
22 changes: 16 additions & 6 deletions SiaNet/Data/DataFrame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public class DataFrame

internal NDArray UnderlayingVariable;

private Tensor UnderlayingTensor;

public DataFrame()
{

Expand Down Expand Up @@ -58,7 +60,12 @@ public virtual void Load(params float[] data)
/// <returns></returns>
public Tensor GetTensor()
{
return K.CreateVariable(UnderlayingVariable.Data<float>(), Shape);
if (UnderlayingTensor == null)
{
UnderlayingTensor = K.CreateVariable(UnderlayingVariable.Data<float>(), Shape);
}

return UnderlayingTensor;
}

public Array DataArray
Expand Down Expand Up @@ -128,15 +135,18 @@ public Tensor GetBatch(int start, int size, int axis = 0)

if (start + size <= Shape[0])
{
data = UnderlayingVariable[new NDArray(Enumerable.Range(start, size).ToArray())];
return K.SliceRows(GetTensor(), start, start + size - 1);
//data = UnderlayingVariable[new NDArray(Enumerable.Range(start, size).ToArray())];
}
else
{
int count = (int)Shape[0] - start;
data = UnderlayingVariable[new NDArray(Enumerable.Range(start, count).ToArray())];
//int count = (int)Shape[0] - start;
//data = UnderlayingVariable[new NDArray(Enumerable.Range(start, count).ToArray())];

return K.SliceRows(GetTensor(), start, Shape[0] - 1);
}

return K.CreateVariable(data.Data<float>(), BackendUtil.Int2Long(data.shape));
//return K.CreateVariable(data.Data<float>(), BackendUtil.Int2Long(data.shape));
}

/// <summary>
Expand All @@ -151,7 +161,7 @@ public void Head(int count = 5, string title = "")
count = UnderlayingVariable.shape[0];
}

Console.WriteLine(UnderlayingVariable[new NDArray(new int[] { 0, count })].ToString());
Console.WriteLine(UnderlayingVariable[new NDArray(Enumerable.Range(0, count - 1).ToArray())].ToString());
}
}
}
3 changes: 1 addition & 2 deletions SiaNet/SiaNet.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
Expand Down Expand Up @@ -53,7 +53,6 @@ Code well structured, easy to extend if you would like to extend with new layer,
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
<PackageReference Include="NumSharp" Version="0.8.3" />
<PackageReference Include="OpenCvSharp3-AnyCPU" Version="4.0.0.20181129" />
<PackageReference Include="System.Numerics.Tensors" Version="0.2.0-preview3.19128.7" />
</ItemGroup>

</Project>

0 comments on commit 197fab0

Please sign in to comment.