|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Text; |
| 5 | +using Tensorflow.Keras.Utils; |
| 6 | +using NumSharp; |
| 7 | +using System.Linq; |
| 8 | + |
| 9 | +namespace Tensorflow.Keras.Datasets |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// This is a dataset of 25,000 movies reviews from IMDB, labeled by sentiment |
| 13 | + /// (positive/negative). Reviews have been preprocessed, and each review is |
| 14 | + /// encoded as a list of word indexes(integers). |
| 15 | + /// </summary> |
| 16 | + public class Imdb |
| 17 | + { |
| 18 | + string origin_folder = "https://storage.googleapis.com/tensorflow/tf-keras-datasets/"; |
| 19 | + string file_name = "imdb.npz"; |
| 20 | + string dest_folder = "imdb"; |
| 21 | + |
| 22 | + /// <summary> |
| 23 | + /// Loads the [IMDB dataset](https://ai.stanford.edu/~amaas/data/sentiment/). |
| 24 | + /// </summary> |
| 25 | + /// <param name="path"></param> |
| 26 | + /// <param name="num_words"></param> |
| 27 | + /// <param name="skip_top"></param> |
| 28 | + /// <param name="maxlen"></param> |
| 29 | + /// <param name="seed"></param> |
| 30 | + /// <param name="start_char"></param> |
| 31 | + /// <param name="oov_char"></param> |
| 32 | + /// <param name="index_from"></param> |
| 33 | + /// <returns></returns> |
| 34 | + public DatasetPass load_data(string path = "imdb.npz", |
| 35 | + int num_words = -1, |
| 36 | + int skip_top = 0, |
| 37 | + int maxlen = -1, |
| 38 | + int seed = 113, |
| 39 | + int start_char = 1, |
| 40 | + int oov_char= 2, |
| 41 | + int index_from = 3) |
| 42 | + { |
| 43 | + var dst = Download(); |
| 44 | + |
| 45 | + var lines = File.ReadAllLines(Path.Combine(dst, "imdb_train.txt")); |
| 46 | + var x_train_string = new string[lines.Length]; |
| 47 | + var y_train = np.zeros(new int[] { lines.Length }, NPTypeCode.Int64); |
| 48 | + for (int i = 0; i < lines.Length; i++) |
| 49 | + { |
| 50 | + y_train[i] = long.Parse(lines[i].Substring(0, 1)); |
| 51 | + x_train_string[i] = lines[i].Substring(2); |
| 52 | + } |
| 53 | + |
| 54 | + var x_train = np.array(x_train_string); |
| 55 | + |
| 56 | + File.ReadAllLines(Path.Combine(dst, "imdb_test.txt")); |
| 57 | + var x_test_string = new string[lines.Length]; |
| 58 | + var y_test = np.zeros(new int[] { lines.Length }, NPTypeCode.Int64); |
| 59 | + for (int i = 0; i < lines.Length; i++) |
| 60 | + { |
| 61 | + y_test[i] = long.Parse(lines[i].Substring(0, 1)); |
| 62 | + x_test_string[i] = lines[i].Substring(2); |
| 63 | + } |
| 64 | + |
| 65 | + var x_test = np.array(x_test_string); |
| 66 | + |
| 67 | + return new DatasetPass |
| 68 | + { |
| 69 | + Train = (x_train, y_train), |
| 70 | + Test = (x_test, y_test) |
| 71 | + }; |
| 72 | + } |
| 73 | + |
| 74 | + (NDArray, NDArray) LoadX(byte[] bytes) |
| 75 | + { |
| 76 | + var y = np.Load_Npz<byte[]>(bytes); |
| 77 | + return (y["x_train.npy"], y["x_test.npy"]); |
| 78 | + } |
| 79 | + |
| 80 | + (NDArray, NDArray) LoadY(byte[] bytes) |
| 81 | + { |
| 82 | + var y = np.Load_Npz<long[]>(bytes); |
| 83 | + return (y["y_train.npy"], y["y_test.npy"]); |
| 84 | + } |
| 85 | + |
| 86 | + string Download() |
| 87 | + { |
| 88 | + var dst = Path.Combine(Path.GetTempPath(), dest_folder); |
| 89 | + Directory.CreateDirectory(dst); |
| 90 | + |
| 91 | + Web.Download(origin_folder + file_name, dst, file_name); |
| 92 | + |
| 93 | + return dst; |
| 94 | + // return Path.Combine(dst, file_name); |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments