Пример #1
0
        static DataSet Load(Gpt2Encoder encoder, IEnumerable <string> texts)
        {
            dynamic numpy            = Py.Import("numpy");
            var     result           = new DataSet();
            string  encodedEndOfText = encoder.EncodedEndOfText;
            var     chunk            = new List <string>();
            int     chunkSize        = 0;

            void AddChunk()
            {
                PyObject tokens = numpy.stack(chunk);

                chunk.Clear();
                chunkSize = 0;
                result.Add(ndarray.Wrap(tokens));
            }

            foreach (string text in texts)
            {
                if (string.IsNullOrWhiteSpace(text))
                {
                    continue;
                }

                if (chunkSize + text.Length + encodedEndOfText.Length >= TrimAfter)
                {
                    AddChunk();
                }
                else
                {
                    chunkSize += text.Length + encodedEndOfText.Length;
                    var encoded = encoder.Encode(text);
                    chunk.AddRange(encoded);
                    chunk.Add(encodedEndOfText);
                }
            }
            if (chunk.Count > 0)
            {
                AddChunk();
            }
            return(result);
        }
Пример #2
0
        internal static List <ndarray> LoadDataset(Gpt2Encoder encoder, List <string> fileNames)
        {
            if (encoder == null)
            {
                throw new ArgumentNullException(nameof(encoder));
            }

            var tokenChunks = new List <ndarray>();

            foreach (string file in fileNames)
            {
                Debug.WriteLine($"Reading {file}");
                if (Path.GetExtension(file) == ".npz")
                {
                    // pre-encoded
                    dynamic npzObject = np.load(file);
                    var     npz       = npzObject.__enter__();
                    foreach (var item in npz.files)
                    {
                        tokenChunks.Add(npz[item]);
                    }
                    npzObject.__exit__();
                }
                else
                {
                    string rawText = File.ReadAllText(file);
                    if (String.IsNullOrWhiteSpace(rawText))
                    {
                        continue;
                    }
                    dynamic  numpy  = Py.Import("numpy");
                    PyObject tokens = numpy.stack(encoder.Encode(rawText));
                    tokenChunks.Add(ndarray.Wrap(tokens));
                }
            }

            return(tokenChunks);
        }