Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement RNN text Generation Model with Code

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

In this issue, the editor will bring you about how to use code to achieve RNN text generation model. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.

Text generation (generating text) seems like one of the interesting projects for machine learning and NLP beginners, but it is also a very difficult project. Fortunately, there are a variety of excellent resources on the Internet that can be used to understand how RNN is used for text generation, from theory to in-depth specific technology, there are some very good resources. All of these resources share one thing in particular: at some point in the text generation process, you have to build a RNN model and call parameters to do the job.

Although text generation is a valuable task, especially in the process of learning, what if the task is highly abstract? What if you are a data scientist and need a module in the form of an RNN text generator to populate the project? Or as a newcomer, you just want to try or improve yourself. In both cases, take a look at the textgenrnn project, which can easily train arbitrarily sized and complex text generation neural networks on any text dataset with a few lines of code. The textgenrnn project was developed by data scientist Max Woolf.

Textgenrnn is built on top of Keras and TensorFlow and can be used to generate character and text-level text. The network architecture uses attention weighting to speed up the training process and improve quality, and allows a large number of hyperparameters such as RNN model size, RNN layer, and bi-directional RNN to be adjusted. Readers can read more about textgenrnn and its functions and architecture on Github or similar introductory blog articles.

Because of "Hello,World!" As easy for text generation as Trump generates tweets, textgenrnn's default pre-training model can be easily trained on new text, and you can also use textgenrnn to train the new model (just add new_model = True to any training function). Get data

This article crawls Trump's tweets from January 1, 2014 to June 11, 2018, including tweets from before and after the inauguration of the President of the United States (from Trump Twitter Archive). Select only tweets within the date range to get the text, save it to a text file, and name the text trump-tweets.txt.

Training model

Let's take a look at a simple way to generate text using textgenrnn. The following four lines are the libraries we need to import, create a text generation object, train the model's 10 epoch in the trump-tweets.txt file, and then generate some sample tweets.

From textgenrnn import textgenrnntextgen = textgenrnn () textgen.train_from_file ('trump-tweets.txt', num_epochs=10) textgen.generate (5)

After about 30 minutes, the following results will be produced (training time is related to computer performance, at the 10th epoch):

My @ FoxNews will be self finally complaining about me that so he is a great day and companies and is starting to report the president in safety and more than any mention of the bail of the underaches to the construction and freedom and efforts the politicians and expensive meetings should have beeThe world will be interviewed on @ foxandfriends at 7:30pm. Enjoybush. JebBush and Fake News Media is a major place in the White House in the service and sense where the people of the debate and his show of many people who is a great press considering the GREAT job on the way to the. A the best and people in the biggest! Thank you!New Hampshire Trump Int'l Hotel Leadership Barrier Lou Clinton is a forever person politically record supporters have really beginning in the media on the heart of the bad and women who have been succeeded and before you can also work the people are there a time strong and send out the world with Join me in Maryland at 7:00 A.M. And happened to the WALL and be true the longer of the same sign into the Fake News Media will be a great honor to serve that the Republican Party will be a great legal rate the media with the Best Republican Party and the American people that will be the bill by a...

Politics aside, considering that we only train with about 12, 000 tweets in 10 epoch, these generated tweets are not so bad. Textgenrnn is set to 0.5 by default. If you want to get more creative tweets, you can increase this value. Let's try to see how it works when you increase this value:

Textgen.generate (5, temperature=0.9) "Via-can see this Democrats were the opening at GREAT ENSUS CALLLSTONG GovSeptorald Taster is got to that the subcent Vote waiting them. @ CalkersMajor President Obama will listen for the disasterners grateful and South Carolina so his real ability and much better-- or big crisis on many signingworthy it is absolutely dumbers for well tonight. Love us in the great inherition of fast. With bill of badly to forget the greatest puppet at my wedds. No Turnberry is" bigger. "- All

The above results are not very convincing, so what about lowering the textgenrnn value? The results show that the model is more stable.

Textgen.generate (5 Temperature=0.1) The Fake News Media is a great people of the president was a great people of the many people who would be a great people of the president was a big crowd of the statement of the media is a great people of the people of the statement of the people of the people of the world with the statement of thThank you @ TrumpTowerNY # Trump2016 https://t.co/25551R58350Thank you for your support! # Trump2016 https://t.co/7eN53P55cThe people of the room.has been a great people of the presidential country is a great time and the best Thing that the people of the statement of the media is the people of the state of the best thing that the people of the statement of the statement of the problem in the problem and success and tThank you @ TheBrodyFile tonight at 8:00 A.M. Enjoy!

According to the comparison of the two examples, we can have a clearer understanding of the project.

The above is the editor for you to share how to use the code to achieve the RNN text generation model, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report