简体   繁体   English

需要帮助理解 GPT-2s 源代码中的 Python 函数

[英]Need Help understanding a Python Function in GPT-2s Source Code

I'm walking through GPT-2s source code in Github.我正在 Github 中浏览 GPT-2 的源代码。 I'm trying to understand how it all works.我试图了解这一切是如何运作的。 I'm getting stumped on a function and I'm hoping somebody can explain to me what's happening.我被一个函数难住了,我希望有人能向我解释发生了什么。

https://github.com/nshepperd/gpt-2/blob/finetuning/src/model.py https://github.com/nshepperd/gpt-2/blob/finetuning/src/model.py

The code can be found in model.py, in the link above.代码可以在上面链接中的 model.py 中找到。 Here it is specifically:这里具体是:

def shape_list(x):
   """Deal with dynamic shape in tensorflow cleanly."""
   static = x.shape.as_list()
   dynamic = tf.shape(x)
   return [dynamic[i] if s is None else s for i, s in enumerate(static)]

I did some research on what Tensorflow.Shape() returns and on the differences between a static and dynamic shape here: How to understand static shape and dynamic shape in TensorFlow?我对 Tensorflow.Shape() 返回的内容以及静态和动态形状之间的差异进行了一些研究: 如何理解 TensorFlow 中的静态形状和动态形状?

I also read through this series of articles: https://medium.com/analytics-vidhya/understanding-the-gpt-2-source-code-part-3-9796a5a5cc7c我也通读了这一系列文章: https : //medium.com/analytics-vidhya/understanding-the-gpt-2-source-code-part-3-9796a5a5cc7c

Despite all that reading, I'm not entirely sure what's going.尽管读了这么多,我还是不完全确定发生了什么。 What isn't clear to me is the last statement:我不清楚的是最后一句话:

return [dynamic[i] if s is None else s for i, s in enumerate(static)]

What exactly is it saying here?它到底在说什么? My guess is that the functions purpose is to determine if the value of X has been defined yet.我的猜测是函数的目的是确定 X 的值是否已经定义。 If it hasn't then it will return the static shape, if it has then it will return the dynamic shape.如果没有,则返回静态形状,如果有,则返回动态形状。

Am I way off here?我离这儿很远吗?

Your problem is not with anything Tensorflow, but with list comprehensions in python, which are a more pythonic way to define lists based on other iterables.您的问题不在于 Tensorflow,而在于 Python 中的列表推导式,这是一种基于其他可迭代对象定义列表的更 Python 化的方式。

The last statement is (almost*) equivalent to:最后一条语句(几乎*)相当于:

ret = []
for i, s in enumerate(static):
  if s is None:
    ret.append(dynamic[i])
  else:
    ret.append(s)
return ret

* : About the "almost" above, the comprehension is actually more efficient, because internally it pre-allocates the memory for the whole result, while the loop appends s on every iteration, thus causing multiple allocations when extending the list, which is slower. * : 关于上面的“几乎”,其实推导的效率更高,因为内部是为整个结果预先分配内存,而循环每次迭代都会appends s,从而导致扩展列表时多次分配,比较慢.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM