以下代碼摘自以下鏈接:
https://www.tensorflow.org/text/guide/word_embeddings
import tensorflow as tf
# Embed a 1,000 word vocabulary into 5 dimensions.
embedding_layer = tf.keras.layers.Embedding(1000, 5)
print("embedding_layer: {}".format(embedding_layer))
result = embedding_layer(tf.constant([1, 2, 3]))
print("result: {}".format(result.numpy()))
embedding_layer: <keras.layers.embeddings.Embedding object at 0x7ffb180b17f0>
result: [[-0.04678862 -0.03500976 -0.04254207 -0.0452533 0.04933525]
[-0.0366199 -0.01814463 0.04166402 0.02388224 0.03472105]
[ 0.02966919 0.04294082 0.00715581 0.0376732 0.00529655]]
執行“ embedding_layer(tf.constant([1, 2, 3]))”時,會從 tf.keras.layers.Embedding 類中呼叫哪個方法?
是呼叫__init__方法嗎?
以下代碼將引發以下錯誤:
embedding_layer = tf.keras.layers.Embedding(tf.constant([1, 2, 3]))
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/tmp/ipykernel_4537/3254652836.py in <cell line: 10>()
8 print("result: {}".format(result.numpy()))
9
---> 10 embedding_layer = tf.keras.layers.Embedding(tf.constant([1, 2, 3]))
TypeError: __init__() missing 1 required positional argument: 'output_dim'
這是嵌入的源代碼:
class Embedding(Layer):
def __init__(self,
input_dim,
output_dim,
embeddings_initializer='uniform',
embeddings_regularizer=None,
activity_regularizer=None,
embeddings_constraint=None,
mask_zero=False,
input_length=None,
**kwargs):
if 'input_shape' not in kwargs:
if input_length:
kwargs['input_shape'] = (input_length,)
else:
kwargs['input_shape'] = (None,)
if input_dim <= 0 or output_dim <= 0:
raise ValueError('Both `input_dim` and `output_dim` should be positive, '
'found input_dim {} and output_dim {}'.format(
input_dim, output_dim))
if (not base_layer_utils.v2_dtype_behavior_enabled() and
'dtype' not in kwargs):
# In TF1, the dtype defaults to the input dtype which is typically int32,
# so explicitly set it to floatx
kwargs['dtype'] = backend.floatx()
# We set autocast to False, as we do not want to cast floating- point inputs
# to self.dtype. In call(), we cast to int32, and casting to self.dtype
# before casting to int32 might cause the int32 values to be different due
# to a loss of precision.
kwargs['autocast'] = False
super(Embedding, self).__init__(**kwargs)
self.input_dim = input_dim
self.output_dim = output_dim
self.embeddings_initializer = initializers.get(embeddings_initializer)
self.embeddings_regularizer = regularizers.get(embeddings_regularizer)
self.activity_regularizer = regularizers.get(activity_regularizer)
self.embeddings_constraint = constraints.get(embeddings_constraint)
self.mask_zero = mask_zero
self.supports_masking = mask_zero
self.input_length = input_length
uj5u.com熱心網友回復:
運行時呼叫call方法:
result = embedding_layer(tf.constant([1, 2, 3]))
需要注意的是,Embedding層在使用之前首先需要初始化。在內部,在 期間__init__,該Embedding層根據您定義的詞匯表的大小和嵌入維度(在您的情況下為 1000 和 5)創建一個查找表。除非另有說明,否則每個 5 維向量均來自均勻分布。我建議檢查嵌入是如何在構建方法中創建的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/437201.html
