Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 8381fe8

Browse filesBrowse files
committed
Text2text generation
1 parent f8891c2 commit 8381fe8
Copy full SHA for 8381fe8

File tree

Expand file treeCollapse file tree

2 files changed

+97
-1
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+97
-1
lines changed

‎README.md

Copy file name to clipboardExpand all lines: README.md
+97-1Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
- [Summarization](#summarization)
4444
- [Question Answering](#question-answering)
4545
- [Text Generation](#text-generation)
46+
- [Text-to-Text Generation](#text-to-text-generation)
4647
- [Fill-Mask](#fill-mask)
4748
- [Sentence Similarity](#sentence-similarity)
4849
- [Regression](#regression)
@@ -633,7 +634,102 @@ SELECT pgml.transform(
633634
]
634635
]
635636
```
636-
### Text2Text Generation
637+
Text generation typically utilizes a greedy search algorithm that selects the word with the highest probability as the next word in the sequence. However, an alternative method called beam search can be used, which aims to minimize the possibility of overlooking hidden high probability word combinations. Beam search achieves this by retaining the num_beams most likely hypotheses at each step and ultimately selecting the hypothesis with the highest overall probability. We set `num_beams > 1` and `early_stopping=True` so that generation is finished when all beam hypotheses reached the EOS token.
638+
639+
```sql
640+
SELECT pgml.transform(
641+
task => '{
642+
"task" : "text-generation",
643+
"model" : "gpt2-medium"
644+
}'::JSONB,
645+
inputs => ARRAY[
646+
'Three Rings for the Elven-kings under the sky, Seven for the Dwarf-lords in their halls of stone'
647+
],
648+
args => '{
649+
"num_beams" : 5,
650+
"early_stopping" : true
651+
}'::JSONB
652+
) AS answer;
653+
```
654+
655+
*Result*
656+
```json
657+
[[
658+
{"generated_text": "Three Rings for the Elven-kings under the sky, Seven for the Dwarf-lords in their halls of stone, Nine for the Dwarves in their caverns of ice, Ten for the Elves in their caverns of fire, Eleven for the"}
659+
]]
660+
```
661+
Sampling methods involve selecting the next word or sequence of words at random from the set of possible candidates, weighted by their probabilities according to the language model. This can result in more diverse and creative text, as well as avoiding repetitive patterns. In its most basic form, sampling means randomly picking the next word $w_t$ according to its conditional probability distribution:
662+
$$ w_t \approx P(w_t|w_{1:t-1})$$
663+
664+
665+
However, the randomness of the sampling method can also result in less coherent or inconsistent text, depending on the quality of the model and the chosen sampling parameters such as temperature, top-k, or top-p. Therefore, choosing an appropriate sampling method and parameters is crucial for achieving the desired balance between creativity and coherence in generated text.
666+
667+
You can pass `do_sample = True` in the arguments to use sampling methods. It is recommended to alter `temperature` or `top_p` but not both.
668+
669+
*Temperature*
670+
```sql
671+
SELECT pgml.transform(
672+
task => '{
673+
"task" : "text-generation",
674+
"model" : "gpt2-medium"
675+
}'::JSONB,
676+
inputs => ARRAY[
677+
'Three Rings for the Elven-kings under the sky, Seven for the Dwarf-lords in their halls of stone'
678+
],
679+
args => '{
680+
"do_sample" : true,
681+
"temperature" : 0.9
682+
}'::JSONB
683+
) AS answer;
684+
```
685+
*Result*
686+
```json
687+
[[{"generated_text": "Three Rings for the Elven-kings under the sky, Seven for the Dwarf-lords in their halls of stone, and Thirteen for the Giants and Men of S.A.\n\nThe First Seven-Year Time-Traveling Trilogy is"}]]
688+
```
689+
*Top p*
690+
691+
```sql
692+
SELECT pgml.transform(
693+
task => '{
694+
"task" : "text-generation",
695+
"model" : "gpt2-medium"
696+
}'::JSONB,
697+
inputs => ARRAY[
698+
'Three Rings for the Elven-kings under the sky, Seven for the Dwarf-lords in their halls of stone'
699+
],
700+
args => '{
701+
"do_sample" : true,
702+
"top_p" : 0.8
703+
}'::JSONB
704+
) AS answer;
705+
```
706+
*Result*
707+
```json
708+
[[{"generated_text": "Three Rings for the Elven-kings under the sky, Seven for the Dwarf-lords in their halls of stone, Four for the Elves of the forests and fields, and Three for the Dwarfs and their warriors.\" ―Lord Rohan [src"}]]
709+
```
710+
## Text-to-Text Generation
711+
Text-to-text generation methods, such as T5, are neural network architectures designed to perform various natural language processing tasks, including summarization, translation, and question answering. T5 is a transformer-based architecture pre-trained on a large corpus of text data using denoising autoencoding. This pre-training process enables the model to learn general language patterns and relationships between different tasks, which can be fine-tuned for specific downstream tasks. During fine-tuning, the T5 model is trained on a task-specific dataset to learn how to perform the specific task.
712+
![text-to-text](pgml-docs/docs/images/text-to-text-generation.png)
713+
714+
*Translation*
715+
```sql
716+
SELECT pgml.transform(
717+
task => '{
718+
"task" : "text2text-generation"
719+
}'::JSONB,
720+
inputs => ARRAY[
721+
'translate from English to French: I''m very happy'
722+
]
723+
) AS answer;
724+
```
725+
726+
*Result*
727+
```json
728+
[
729+
{"generated_text": "Je suis très heureux"}
730+
]
731+
```
732+
637733
## Fill-Mask
638734
![fill mask](pgml-docs/docs/images/fill-mask.png)
639735

Loading

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.