Poetry always belongs to the romance of literati, but with the development of technology, science students become literary, and there is no such thing as liberal arts (just kidding). Through this article, the author will introduce the mainstream AI poetry creation platform and how to build it yourself for your reference.
The advancement of technology is to better serve the original creation, not to replace it. After all, AI poetry has no soul.
1. Introduction of mainstream AI poetry writing platforms
1. Poetry 300 AI Poetry Writing Platform
The platform supports various literary forms such as quatrains, verses, words, and couplets, with a relatively complete range of types.
2. Jiuge·Artificial Intelligence Poetry Writing System
The platform is similar to Shisanbai, developed by the Natural Language Processing and Social Humanities Computing Laboratory of Tsinghua University.
3. Huawei Yuefu·Artificial Intelligence Poetry Applet
Huawei Yuefu AI is the first poetry writing system based on GPT, and it is closely related to BERT proposed by Google. The quality of poems written by Huawei Yuefu Province is relatively high, but there is a fee.
4. Microsoft Xiaobing AI Modern Poetry Creation System
The platform focuses on modern poetry creation, and it is worth noting its statement: Xiaobing announced that she has given up the copyright of her poems, so you can publish the final works at will without even mentioning that she participated in your creation , that is, the creation works are completely owned by you. The platform can be trained to create poems through pictures and prompt words, and it is also very good to turn the wonderful moments left in the camera into poems.
2. Independent construction
The project address used here is https://github.com/lucasjinreal/tensorflow_poems , an AI poetry project based on tensorflow. Now the author will introduce how to build your own AI poetry program independently.
1. Install and configure Anaconda
Go to the official website to download the installation package and install it, then configure Anaconda to the environment variable.
After cloning the project, create a new file environment.yml
and an empty file in the root directory of the project requirements.txt
, and environment.yml
fill in the following content:
name: poemai
channels:
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
- defaults
dependencies:
- pip==21.2.4
- python==3.10.6
Open the command line, and use the following commands to create a virtual environment for running Python.
conda env create
E:\workspace\02_Python\002_AI作诗>conda info --envs
# conda environments:
#
base * D:\anaconda3
novelai D:\anaconda3\envs\novelai
poemai D:\anaconda3\envs\poemai
E:\workspace\02_Python\002_AI作诗>conda activate poemai
tensorflow
But it should be noted that version 2 is already installed at this time , and the project is based on tensorflow1.10 api
development and needs to be modified:
import tensorflow as tf =>
import tensorflow.compat.v1 as tf
Otherwise, the following error will appear:
(poemai) E:\workspace\02_Python\002_AI作诗>python train.py
Traceback (most recent call last):
File "E:\workspace\02_Python\002_AI作诗\train.py", line 24, in <module>
tf.app.flags.DEFINE_integer('batch_size', 64, 'batch size.')
AttributeError: module 'tensorflow' has no attribute 'app'
2. Model training and operation
After the environment is set up, execute python train.py
the command to train the model first.
After the model training is complete, the execution python compose_poem.py
starts poetry creation:
It looks decent, but it can't be compared with the mainstream platforms mentioned above. Needless to say, the meaning of poetry, and sometimes it will let yourself go.
3. Possible problems
3.1 tf.placeholder() is not compatible with eager execution
The specific error content is as follows:
(poemai) E:\workspace\02_Python\002_AI作诗>python train.py
Traceback (most recent call last):
File "E:\workspace\02_Python\002_AI作诗\train.py", line 87, in <module>
tf.app.run()
File "D:\anaconda3\envs\poemai\lib\site-packages\tensorflow\python\platform\app.py", line 36, in run
_run(main=main, argv=argv, flags_parser=_parse_flags_tolerate_undef)
File "D:\anaconda3\envs\poemai\lib\site-packages\absl\app.py", line 308, in run
_run_main(main, args)
File "D:\anaconda3\envs\poemai\lib\site-packages\absl\app.py", line 254, in _run_main
sys.exit(main(argv))
File "E:\workspace\02_Python\002_AI作诗\train.py", line 83, in main
run_training()
File "E:\workspace\02_Python\002_AI作诗\train.py", line 42, in run_training
input_data = tf.placeholder(tf.int32, [FLAGS.batch_size, None])
File "D:\anaconda3\envs\poemai\lib\site-packages\tensorflow\python\ops\array_ops.py", line 3340, in placeholder
raise RuntimeError("tf.placeholder() is not compatible with "
RuntimeError: tf.placeholder() is not compatible with eager execution.
Reason analysis: ThisTensorflow 2.0
function will be used by default , the specific code is as follows. Eager Execution
This code will directly cause tf.placeholder()
a conflict with this code.
tf.compat.v1.enable_eager_execution()
Solution: tf.placeholder()
Enter the following code in front of to disable the function first.
tf.compat.v1.disable_eager_execution()
3.2 module ‘tensorflow’ has no attribute ‘contrib’
The specific error content is as follows:
(poemai) E:\workspace\02_Python\002_AI作诗>python train.py
Traceback (most recent call last):
File "E:\workspace\02_Python\002_AI作诗\train.py", line 88, in <module>
tf.app.run()
File "D:\anaconda3\envs\poemai\lib\site-packages\tensorflow\python\platform\app.py", line 36, in run
_run(main=main, argv=argv, flags_parser=_parse_flags_tolerate_undef)
File "D:\anaconda3\envs\poemai\lib\site-packages\absl\app.py", line 308, in run
_run_main(main, args)
File "D:\anaconda3\envs\poemai\lib\site-packages\absl\app.py", line 254, in _run_main
sys.exit(main(argv))
File "E:\workspace\02_Python\002_AI作诗\train.py", line 84, in main
run_training()
File "E:\workspace\02_Python\002_AI作诗\train.py", line 46, in run_training
end_points = rnn_model(model='lstm', input_data=input_data, output_data=output_targets, vocab_size=len(
File "E:\workspace\02_Python\002_AI作诗\poems\model.py", line 44, in rnn_model
cell_fun = tf.contrib.rnn.BasicLSTMCell
AttributeError: module 'tensorflow' has no attribute 'contrib'
Reason analysis: Due to tensorflow2.x
the abandonment of many tensorflow1.x API
interfaces, such problems often occur when using tensorflow2.x
the version to call the code of the function. Such as the author's mistake, because the version has no library, you can try the following methods to solve it.tensorflow1.x
module ‘tensorflow’ has no attribute ‘contrib’
tensorflow2.x
contrib
The first placeholder
method to use is to modify import tensorflow as tf
it to
import tensorflow.compat.v1 as tf
tf.compat.v1.disable_eager_execution()
BasicLSTMCell
Processing method, DropoutWrapper
and MultiRNNCell
in the same way:
cell = tf.contrib.rnn.BasicLSTMCell(num_units=units, forget_bias=0.9)
修改为:
cell = tf.nn.rnn_cell.BasicLSTMCell(num_units=units,forget_bias=0.9)
Change contrib.rnn
to nn.rnn_cell
, if you use static_rnn
a similar one, just contrib.rnn
change it to nn
.
outputs, _ = tf.contrib.rnn.static_rnn(stacked_lstm_cells, inputs, dtype=tf.float32)
修改为:
outputs, _ = tf.nn.static_rnn(stacked_lstm_cells, inputs, dtype=tf.float32)