Generate Chinese Poetry with Pipcook’s Text‑Creation Pipeline
This tutorial walks you through using Pipcook’s text‑creation pipeline to train a TensorFlow.js LSTM model that generates Chinese poems from a given seed, covering plugin configuration, GPU training, model evaluation, and Node.js inference.
Preface
To help everyone learn Pipcook and its pipeline, we prepared a practical series that distinguishes front‑end component migration, image‑style migration, and AI poem generation, showing concrete examples of how to use Pipcook 1.0 in daily development.
Background
Pipcook now includes a Text Creation model that can generate poetry based on input conditions. For example, feeding the string "一二三四" produces a poem line continuation.
console.log(await predict('一二三四'));</code><code>// 一二三四過,橈泛澄違篁。。。The pipeline is defined with the following plugins:
{
"plugins": {
"dataCollect": {
"package": "@pipcook/plugins-chinese-poem-data-collect",
"params": {
"url": "https://raw.githubusercontent.com/DavidCai1993/chinese-poem-generator.js/master/test/data/poet.song.91000.json"
}
},
"dataAccess": {
"package": "@pipcook/plugins-textline-data-access"
},
"modelDefine": {
"package": "@pipcook/plugins-tfjs-text-lstm-model-define"
},
"modelTrain": {
"package": "@pipcook/plugins-text-creation-tfjs-model-train",
"params": { "epochs": 200 }
},
"modelEvaluate": {
"package": "@pipcook/plugins-text-creation-tfjs-model-evaluate"
}
}
}The plugins serve the following purposes:
@pipcook/plugins-chinese-poem-data-collect downloads a Chinese poem dataset.
@pipcook/plugins-textline-data-access converts the dataset into a line‑by‑line text format required for text creation.
@pipcook/plugins-tfjs-text-lstm-model-define defines the LSTM model, based on an English poetry generation implementation.
@pipcook/plugins-text-creation-tfjs-model-train provides training support for the text‑creation task.
@pipcook/plugins-text-creation-tfjs-model-evaluate offers model evaluation for the task.
Training
Because the model is large, run the pipeline on a GPU server: $ pipcook run text-creation-lstm.json --verbose Training converges slowly, so we set epochs to 200, which takes about four hours. Sample training output (truncated):
_____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____
Layer (type) Output shape Param #
=================================================================
embedding_Embedding1 (Embedding) [null,11,100] 478400
_____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____
bidirectional_Bidirectional1 [null,11,300] 301200
_____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____
dropout_Dropout1 (Dropout) [null,11,300] 0
_____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____
lstm_LSTM2 (LSTM) [null,100] 160400
_____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____
dense_Dense1 (Dense) [null,2392] 241592
_____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____
dense_Dense2 (Dense) [null,4784] 11448112
=================================================================
Total params: 12629704
Trainable params: 12629704
Non-trainable params: 0
_____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____
Epoch 1 / 200 - acc=0.0892 loss=7.14
... (omitted intermediate epochs) ...
Epoch 199 / 200 - acc=0.868 loss=1.04
Epoch 200 / 200 - acc=0.875 loss=1.04Why set epochs to 200? Each epoch prints accuracy and loss; the model converges slowly, and around 200 epochs the accuracy approaches 0.9, which is why we chose this number.
After training, an output directory is created as a new npm package. Install its dependencies: $ cd output && npm install Then use the package to generate poems:
const predict = require('./output');
(async () => {
const v1 = await predict('一二三四');
console.log(v1); // 一二三四過,橈泛澄違篁。。。
const v2 = await predict('天涯');
console.log(v2); // 天涯酒祠輿,悽慟歡夫寛。。。
const v3 = await predict('中');
console.log(v3); // 中慟前弧日,當余弱冠年。。。
const v4 = await predict('乐乐');
console.log(v4); // 乐乐池方志,衰病各華顛。。。
const v5 = await predict('月明');
console.log(v5); // 月明三象服,凄凉對夕暉。。。
const v6 = await predict('妙静');
console.log(v6); // 妙静尋世夜,里感仗履遊。。。
})();Running the script prints a poem line that starts with the provided seed.
Conclusion
By now you should understand how to use Pipcook to create AI‑generated poetry. The pipeline can be extended to other text styles such as naming, prose, or rap by feeding appropriately formatted datasets.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
