Skip to content

Commit f26c1d2

Browse files
committed
fix(csharp/Tensor.NET): fix bugs of mean, slice, tensorlayout.
1 parent 8a1d845 commit f26c1d2

File tree

5 files changed

+32
-24
lines changed

5 files changed

+32
-24
lines changed

csharp/Tensor.NET/Statistics/Mean.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,19 @@ private unsafe static void MeanInternal<T>(Tensor<T> src, Tensor<double> dst, bo
4848
}
4949
}
5050
private static TensorLayout DeduceLayout(TensorLayout src, int[] axes){
51-
var res = new TensorLayout(src.Shape, DType.Float64);
51+
var res = new TensorLayout(src as TensorShape, DType.Float64);
5252
foreach(var dim in axes){
5353
res.Shape[dim] = 1;
5454
}
5555
return res;
5656
}
5757
private static TensorLayout DeduceLayout(TensorLayout src, int axis){
58-
var res = new TensorLayout(src.Shape, DType.Float64);
58+
var res = new TensorLayout(src as TensorShape, DType.Float64);
5959
res.Shape[axis] = 1;
6060
return res;
6161
}
6262
private static TensorLayout DeduceLayout(TensorLayout src){
63-
var res = new TensorLayout(src.Shape, DType.Float64);
63+
var res = new TensorLayout(src as TensorShape, DType.Float64);
6464
res.Shape.AsSpan().Fill(1);
6565
return res;
6666
}

csharp/Tensor.NET/Tensor.NET.csproj

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,22 @@
66
<Nullable>enable</Nullable>
77
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
88
<PackageId>Tensor.NET</PackageId>
9-
<Version>0.0.8</Version>
9+
<Version>0.1.0</Version>
1010
<Authors>AsakusaRinne</Authors>
1111
<Company>Null</Company>
1212
<Description>
13-
This is a test version, please do not download and use.
13+
A lightweight and high-performance tensor library which provides numpy-like operations but .NET style interfaces. It supports generic tensor, Linq, C# native slices and so on. It is young so that it may may lack some features or have some BUGs. Please tell us on github or via email, thank you!
14+
15+
Github repository: https://github.com/AsakusaRinne/Tensor.NET
16+
17+
Email: AsakusaRinne@gmail.com
1418
</Description>
19+
<PackageLicenseExpression>Apache2.0</PackageLicenseExpression>
1520
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
1621
</PropertyGroup>
1722

1823
<ItemGroup>
19-
<!-- <Content Include="./CppLibrary/libnumnet.dll">
20-
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
21-
<Pack>true</Pack>
22-
<PackagePath>lib\$(TargetFramework)</PackagePath>
23-
</Content>
24-
<Content Include="./CppLibrary/libnumnet_core_base.dll">
25-
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
26-
<Pack>true</Pack>
27-
<PackagePath>lib\$(TargetFramework)</PackagePath>
28-
</Content>
29-
<Content Include="./CppLibrary/libnumnet_core_op.dll">
24+
<Content Include="./CppLibrary/libnumnet.dll">
3025
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
3126
<Pack>true</Pack>
3227
<PackagePath>lib\$(TargetFramework)</PackagePath>
@@ -40,7 +35,7 @@
4035
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
4136
<Pack>true</Pack>
4237
<PackagePath>lib\$(TargetFramework)</PackagePath>
43-
</Content> -->
38+
</Content>
4439
<Content Include="libnumnet.so">
4540
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
4641
<PackageCopyToOutput>true</PackageCopyToOutput>

csharp/Tensor.NET/Tensor/Common/OnElemOperation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
namespace Tensornet.Common{
3-
public static class OnElemOperation{
3+
internal static class OnElemOperation{
44
public static Tensor<TResult> Execute<TInput, TResult>(Tensor<TInput> inp, Func<TInput, TResult> operation)
55
where TInput : struct, IEquatable<TInput>, IConvertible
66
where TResult : struct, IEquatable<TResult>, IConvertible{

csharp/Tensor.NET/Tensor/Common/Slice.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace Tensornet{
44
public struct Slice{
5-
public int Begin{ get; set; }
6-
public int End{ get; set; }
7-
public int Step{ get; set; }
5+
public int Begin{ get; internal set; }
6+
public int End{ get; internal set; }
7+
public int Step{ get; internal set; }
88
public Slice(int dim){
99
Begin = dim;
1010
End = dim;
@@ -16,8 +16,17 @@ public Slice(int begin, int end, int step = 1){
1616
Step = step;
1717
}
1818
public Slice(Range range){
19-
Begin = range.Start.IsFromEnd ? -range.Start.Value : range.Start.Value;
19+
Begin = range.Start.IsFromEnd? -range.Start.Value : range.Start.Value;
2020
End = range.End.IsFromEnd ? -range.End.Value : range.End.Value;
21+
if(Begin == 0 && range.Start.IsFromEnd){
22+
Step = 1;
23+
return;
24+
}
25+
if(End == 0 && range.End.IsFromEnd){
26+
End = Begin - 1;
27+
Step = -1;
28+
return;
29+
}
2130
Step = 1;
2231
}
2332
public Slice(Index index){
@@ -46,6 +55,10 @@ internal TensorLayout ApplySlice(Slice s, int axis){
4655
if(axis >= res.NDim){
4756
throw new InvalidArgumentException($"Axis to slice exceeds the limit. The axis is {axis}, the limit is {res.NDim - 1}.");
4857
}
58+
if(s.End + 1 == s.Begin && s.Step == -1){
59+
s.End = Shape[axis];
60+
s.Step = 1;
61+
}
4962
int axisDim = res.Shape[axis];
5063
int begin = s.Begin;
5164
int end = s.End;

csharp/Tensor.NET/Tensor/Tensor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ internal Tensor(T value){
4040
internal int IndicesToPosition(params int[] indices){
4141
if(indices.Length != TLayout.NDim){
4242
throw new InvalidArgumentException($"Index does not have same dims with tensor, " +
43-
"the index is {indices.Length} dims but the tensor is {TLayout.NDim}.");
43+
$"the index is {indices.Length} dims but the tensor is {TLayout.NDim}.");
4444
}
4545
int res = 0;
4646
for (int i = 0; i < TLayout.NDim; i++) {
@@ -65,7 +65,7 @@ public T this[params int[] indices]{
6565
}
6666

6767
public Tensor<T> ToContiguousTensor(){
68-
Tensor<T> res = new Tensor<T>(new TensorLayout(TLayout.Shape, TLayout.DType));
68+
Tensor<T> res = new Tensor<T>(new TensorLayout(TLayout as TensorShape, TLayout.DType));
6969
this.CopyTo(res);
7070
return res;
7171
}

0 commit comments

Comments
 (0)