Skip to content

Add pad preprocessing for imdb dataset #1126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/TensorFlowNET.Keras/Datasets/Imdb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public DatasetPass load_data(string path = "imdb.npz",
int oov_char= 2,
int index_from = 3)
{
if (maxlen == -1) throw new InvalidArgumentError("maxlen must be assigned.");

var dst = Download();

var lines = File.ReadAllLines(Path.Combine(dst, "imdb_train.txt"));
Expand All @@ -51,7 +53,7 @@ public DatasetPass load_data(string path = "imdb.npz",
x_train_string[i] = lines[i].Substring(2);
}

var x_train = np.array(x_train_string);
var x_train = keras.preprocessing.sequence.pad_sequences(PraseData(x_train_string), maxlen: maxlen);

File.ReadAllLines(Path.Combine(dst, "imdb_test.txt"));
var x_test_string = new string[lines.Length];
Expand All @@ -62,7 +64,7 @@ public DatasetPass load_data(string path = "imdb.npz",
x_test_string[i] = lines[i].Substring(2);
}

var x_test = np.array(x_test_string);
var x_test = keras.preprocessing.sequence.pad_sequences(PraseData(x_test_string), maxlen: maxlen);

return new DatasetPass
{
Expand Down Expand Up @@ -93,5 +95,23 @@ string Download()
return dst;
// return Path.Combine(dst, file_name);
}

protected IEnumerable<int[]> PraseData(string[] x)
{
var data_list = new List<int[]>();
for (int i = 0; i < len(x); i++)
{
var list_string = x[i];
var cleaned_list_string = list_string.Replace("[", "").Replace("]", "").Replace(" ", "");
string[] number_strings = cleaned_list_string.Split(',');
int[] numbers = new int[number_strings.Length];
for (int j = 0; j < number_strings.Length; j++)
{
numbers[j] = int.Parse(number_strings[j]);
}
data_list.Add(numbers);
}
return data_list;
}
}
}