2022-12-10 01:14:44 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
public class Onehot
|
|
|
|
{
|
|
|
|
private List<string> tags = new List<string>();
|
|
|
|
public List<List<float>> onehot = new List<List<float>>();
|
|
|
|
private float totalNum;
|
2023-06-30 09:30:12 +00:00
|
|
|
|
|
|
|
public void Initialize(List<string> inputTags)
|
2022-12-10 01:14:44 +00:00
|
|
|
{
|
|
|
|
tags = inputTags;
|
|
|
|
totalNum = tags.Count;
|
|
|
|
for (int i = 0; i < totalNum; i++)
|
|
|
|
{
|
2023-08-22 17:58:50 +00:00
|
|
|
List<float> oneHot = new List<float>();
|
|
|
|
for (int j = 0; j < totalNum; j++) oneHot.Add(0f);
|
|
|
|
oneHot[i] = 1f;
|
|
|
|
onehot.Add(oneHot);
|
2022-12-10 01:14:44 +00:00
|
|
|
}
|
|
|
|
}
|
2023-06-30 09:30:12 +00:00
|
|
|
|
|
|
|
public List<float> Encoder(string name = null)
|
2022-12-10 01:14:44 +00:00
|
|
|
{
|
|
|
|
if (name == null)
|
|
|
|
{
|
|
|
|
List<float> allZeroOnehot = new List<float>();
|
|
|
|
for (int j = 0; j < totalNum; j++) allZeroOnehot.Add(0);
|
|
|
|
return allZeroOnehot;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return onehot[tags.IndexOf(name)];
|
2023-06-30 09:30:12 +00:00
|
|
|
}
|
|
|
|
catch (ArgumentOutOfRangeException)
|
2022-12-10 01:14:44 +00:00
|
|
|
{
|
|
|
|
List<float> allZeroOnehot = new List<float>();
|
|
|
|
for (int j = 0; j < totalNum; j++) allZeroOnehot.Add(0);
|
|
|
|
return allZeroOnehot;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-22 17:58:50 +00:00
|
|
|
public string Decoder(List<float> oneHot)
|
2022-12-10 01:14:44 +00:00
|
|
|
{
|
2023-08-22 17:58:50 +00:00
|
|
|
return tags[onehot.IndexOf(oneHot)];
|
2022-12-10 01:14:44 +00:00
|
|
|
}
|
2023-06-30 09:30:12 +00:00
|
|
|
}
|