谷歌 colab_如何在Google Colab中使用gpt 2

谷歌 colab

For those who do not have access to the GPT-3 API it is important to remember we can still experiment with and use GPT-2!

对于那些无法访问GPT-3 API的人,请务必记住我们仍然可以尝试并使用GPT-2!

For everyone who is not familiar with what GPT-2 and GPT-3 are here is a short explanation from OpenAI, “We’ve trained a large-scale unsupervised language model which generates coherent paragraphs of text, achieves state-of-the-art performance on many language modeling benchmarks, and performs rudimentary reading comprehension, machine translation, question answering, and summarization — all without task-specific training.” Pretty awesome right?

对于所有不熟悉GPT-2和GPT-3的人,这都是OpenAI的简短说明:“我们已经训练了一种大规模的无监督语言模型,该模型可以生成文本的连贯段落,并可以达到在许多语言建模基准上表现出色,并执行基本的阅读理解,机器翻译,问题解答和总结-所有这些都无需特定任务的培训。” 太棒了吧?

So let’s get started with this awesome technology!

因此,让我们开始使用这项超赞的技术!

获取Google Colab设置 (Getting Google Colab Setup)

If you have used Google Colab before you can skip these next steps. If you have never used Google Colab before here are the necessary steps to get started.

如果您曾经使用过Google Colab,则可以跳过以下后续步骤。 如果您以前从未使用过Google Colab,请按照以下必要步骤开始。

  1. You need to have a Google account.

    您需要有一个Google帐户。

  2. After you have the Google account created go to Google Colab.

    创建完Google帐户后,转到Google Colab 。

  3. I would highly recommend upgrading to the paid version which is Google Colab Pro. It is only $9.99/month and you get access to more powerful GPUs, longer run times, and more memory which means more data!

    我强烈建议您升级到付费版本,即Google Colab Pro 。 每月仅$ 9.99,您就可以访问功能更强大的GPU,更长的运行时间和更多的内存,这意味着更多的数据!

实施GPT-2 (Implementing GPT-2)

TL;DR this is the link to the complete notebook: Here

TL; DR这是完整笔记本的链接:这里

Now that you have your Google account all setup with Google Colab we can move into implementing GPT-2.

现在,您已经使用Google Colab完成了所有的Google帐户设置,我们可以开始实施GPT-2。

1.Create a new notebook within Google Colab. If you are unfamiliar with how to do that click the + New in Google Drive like you would to create a Google Doc. You might have to click more and you will see a dropdown that says “Google Colaboratory.” Click that and it will open a blank notebook.

1.在Google Colab中创建一个新笔记本。 如果您不熟悉该操作,则可以在Google云端硬盘中单击+新建,就像创建Google文档一样。 您可能需要单击更多,然后看到一个下拉菜单,显示“ Google Colaboratory”。 单击该按钮,它将打开一个空白笔记本。

2. Of course name your notebook.

2.当然要给笔记本起个名字。

3. If you have Colab Pro make sure to change your run time type to Hardware Accelerator: GPU and Runtime Shape: High-RAM. This will give you the best performance.

3.如果您拥有Colab Pro,请确保将运行时类型更改为Hardware Accelerator:GPU和Runtime Shape:High-RAM。 这将为您提供最佳性能。

4. Next step is mounting your Google Drive which is basically connecting your Google Drive to the Google Colab notebook. Here is the code to do that.

4.下一步是安装您的Google云端硬盘,这实际上是将您的Google云端硬盘连接到Google Colab笔记本。 这是执行此操作的代码。

from google.colab import drivedrive.mount('/content/drive') 

When you run this cell it will tell you “Go to this URL in a browser:” and click that link. This will open a new tab and make sure to click the proper Google account. Click allow at the bottom. Copy that authorization code and paste it back into where it asks for it in Google Colab.

当您运行此单元格时,它将告诉您“在浏览器中转到此URL:”,然后单击该链接。 这将打开一个新标签,并确保单击正确的Google帐户。 点击底部的允许。 复制该授权码,然后将其粘贴回它在Google Colab中要求的位置。

5. Next we need to create a folder for GPT-2 and clone their repo into there.

5.接下来,我们需要为GPT-2创建一个文件夹,并将其仓库克隆到该文件夹​​中。

# ONLY RUN ONCE%cd /content/drive/My\ Drive/!mkdir gpt-2%cd gpt-2/!git clone https://github.com/openai/gpt-2.git%cd cd gpt-2

It is important to remember to only run this cell block once since you do not want to clone the GPT-2 repo multiple times.

重要的是要记住只运行一次此单元块,因为您不想多次克隆GPT-2存储库。

6. After cloning their repo we need to go one folder deeper and here is the code to do that!

6.克隆他们的仓库后,我们需要更深入一个文件夹,这是执行此操作的代码!

%cd /content/drive/My\ Drive/gpt-2/gpt-2

If you are typing this out make sure you add a “\” after “My”. If the “\” is not there it will throw an error.

如果要输入,请确保在“我的”之后添加“ \”。 如果“ \”不存在,将引发错误。

7. GPT-2 uses a certain version of Tensorflow so we need to update that in Colab as well. Run the code below to change the Tensorflow version in Colab

7. GPT-2使用特定版本的Tensorflow,因此我们也需要在Colab中进行更新。 运行以下代码以更改Colab中的Tensorflow版本

%tensorflow_version 1.x

8. Now we need to install the necessary requirements provided by GPT-2.

8.现在,我们需要安装GPT-2提供的必要要求。

!pip3 install -r requirements.txt

If you are typing this out make sure the “!” is there or it will throw an error. If you run this and get an output that has “WARNING: The following packages were previously imported in this runtime: [idna,requests] You must restart the runtime in order to use newly installed versions.” in it you do not have to restart the runtime you can continue onto the next step.

如果要输入,请确保“!” 在那里,否则将引发错误。 如果运行此程序并获得带有“警告:以前在此运行时中导入了以下软件包的输出:[idna,requests]您必须重新启动运行时才能使用新安装的版本。” 在其中,您不必重新启动运行时,可以继续进行下一步。

9. After that is done installing we can now download their model data.

9.完成安装后,我们现在可以下载其模型数据。

!python3 download_model.py 124M!python3 download_model.py 355M!python3 download_model.py 774M!python3 download_model.py 1558M

These will take some time to download since these models are fairly large.

由于这些模型相当大,因此下载需要一些时间。

10. After these models are downloading we can now play around with and experiment with the awesomeness of GPT-2!

10.下载完这些模型后,我们现在可以尝试并尝试GPT-2的强大功能!

This next line will now allow us to input text and GPT-2 will generate text based off of what we input!

现在,下一行将允许我们输入文本,而GPT-2将根据我们输入的内容生成文本!

!python3 src/interactive_conditional_samples.py --top_k 40

A small box will pop up. You can input any text you’d like in there and see what it generates. If nothing generates the first time when you run it just add the same text in the next small box that says Model prompt >>>.

一个小盒子会弹出。 您可以在其中输入任何文本,然后查看其生成内容。 如果第一次运行时什么都没有生成,则只需在下一个显示模型提示>>>的小框中添加相同的文本即可。

11. Google Colab will eventually time out so you will have to rerun all of these steps EXCEPT the cloning their repo step. Also, when you go to rerun these steps if you have Colab Pro make sure to change the run time type again.

11. Google Colab最终将超时,因此除了克隆其回购步骤外,您将必须重新运行所有这些步骤。 另外,如果您有Colab Pro,则在重新运行这些步骤时,请确保再次更改运行时类型。

Good luck I hope you find some interesting insights into what can be generated!

祝您好运,我希望您能从中找到有趣的见解!

Here is a link to the complete code: Here

这里是完整代码的链接:这里

GPT-2产生的谢谢(A GPT-2 Generated Thank You)

“I think a lot of you guys would’ve like to know about this, so please, follow along to this post. Thanks for reading, and stay tuned. I hope you enjoyed this article. Happy business.” - GPT-2

“我想你们中的很多人都想知道这一点,所以请继续阅读本文。 感谢您的阅读,敬请期待。 希望您喜欢这篇文章。 愉快的生意。” -GPT-2

Good enough! Thank you to everyone who made it this far and if you have any questions feel free to reach out to me on LinkedIn and I will respond as soon as possible.

够好了! 谢谢所有到目前为止取得成就的人,如果您有任何疑问,请随时通过LinkedIn与我联系,我将尽快答复。

翻译自: https://medium.com/swlh/how-to-use-gpt-2-in-google-colab-de44f59199c1

谷歌 colab

你可能感兴趣的:(python)