forked from SciSharp/TensorFlow.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnp_utils.cs
More file actions
31 lines (29 loc) · 980 Bytes
/
np_utils.cs
File metadata and controls
31 lines (29 loc) · 980 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
using Tensorflow.NumPy;
using System;
using System.Collections.Generic;
using System.Text;
namespace Tensorflow.Keras.Utils
{
public class np_utils
{
/// <summary>
/// Converts a class vector (integers) to binary class matrix.
/// </summary>
/// <param name="y"></param>
/// <param name="num_classes"></param>
/// <param name="dtype"></param>
/// <returns></returns>
public static NDArray to_categorical(NDArray y, int num_classes = -1, TF_DataType dtype = TF_DataType.TF_FLOAT)
{
var y1 = y.astype(np.int32).ToArray<int>();
// var input_shape = y.shape[..^1];
var categorical = np.zeros(((int)y.size, num_classes), dtype: dtype);
// categorical[np.arange(y.size), y] = 1;
for (var i = 0; i < (int)y.size; i++)
{
categorical[i, y1[i]] = 1.0f;
}
return categorical;
}
}
}