這是我的模型:
def build_classifier_model():
text_input = tf.keras.layers.Input(shape=(), dtype=tf.string, name='features')
preprocessing_layer = hub.KerasLayer(tfhub_handle_preprocess, name='preprocessing')
encoder_inputs = preprocessing_layer(text_input)
encoder = hub.KerasLayer(tfhub_handle_encoder, trainable=True, name='BERT_encoder')
outputs = encoder(encoder_inputs)
net = outputs['pooled_output']
net = tf.keras.layers.Dropout(0.1)(net)
net = tf.keras.layers.Dense(3, activation="softmax", name='classifier')(net)
return tf.keras.Model(text_input, net)
在預處理層,我使用了來自 TF-Hub 的 BERT 前處理器。
我已經將資料分成corpus_train, corpus_test, labels_train, labels_test. 語料庫是帶有將用作特征的文本的熊貓資料框,標簽是 NumPy 陣列。
corpus=df_speech_EN_merged["contents"]
corpus.shape
(1768,)
labels=np.asarray(df_speech_EN_merged["Classes"].astype("int"))
labels.shape
(1768,)
為了創建訓練和測驗資料集,我使用了以下內容:
train_dataset = (
tf.data.Dataset.from_tensor_slices(
{
"features":tf.cast(corpus_train.values, tf.string),
"labels":tf.cast(labels_train, tf.int32) #labels is already an array, no need for .values
}
)
test_dataset = tf.data.Dataset.from_tensor_slices(
{"features":tf.cast(corpus_test.values, tf.string),
"labels":tf.cast(labels_test, tf.int32)
} #labels is already an array, no need for .values
)
)
在沒有任何錯誤訊息的情況下構建和編譯模型后,當我將模型擬合為:
classifier_model.fit(x=train_dataset,
validation_data=test_dataset,
epochs=2)
我收到以下錯誤:
ValueError: Could not find matching function to call loaded from the SavedModel. Got:
Positional arguments (3 total):
* Tensor("inputs:0", shape=(), dtype=string)
* False
* None
Keyword arguments: {}
Expected these arguments to match one of the following 4 option(s):
Option 1:
Positional arguments (3 total):
* TensorSpec(shape=(None,), dtype=tf.string, name='sentences')
* False
* None
Keyword arguments: {}
Option 2:
Positional arguments (3 total):
* TensorSpec(shape=(None,), dtype=tf.string, name='sentences')
* True
* None
Keyword arguments: {}
Option 3:
Positional arguments (3 total):
* TensorSpec(shape=(None,), dtype=tf.string, name='inputs')
* False
* None
Keyword arguments: {}
Option 4:
Positional arguments (3 total):
* TensorSpec(shape=(None,), dtype=tf.string, name='inputs')
* True
* None
Keyword arguments: {}
我認為發生此錯誤是因為我構建的 train_dataset/test_dataset 錯誤或因為 text_input 層期望的資料型別錯誤。任何幫助,將不勝感激。
uj5u.com熱心網友回復:
使用時tf.data.Dataset.from_tensor_slices,請嘗試提供 batch_size,因為Bert預處理層需要非常特定的形狀。這是一個基于本教程中使用的 Bert 模型和您的具體細節的簡化作業示例:
def build_classifier_model():
text_input = tf.keras.layers.Input(shape=(), dtype=tf.string, name='features')
preprocessing_layer = hub.KerasLayer(tfhub_handle_preprocess, name='preprocessing')
encoder_inputs = preprocessing_layer(text_input)
encoder = hub.KerasLayer(tfhub_handle_encoder, trainable=True, name='BERT_encoder')
outputs = encoder(encoder_inputs)
net = outputs['pooled_output']
net = tf.keras.layers.Dropout(0.1)(net)
net = tf.keras.layers.Dense(3, activation="softmax", name='classifier')(net)
return tf.keras.Model(text_input, net)
sentences = tf.constant([
"Improve the physical fitness of your goldfish by getting him a bicycle",
"You are unsure whether or not to trust him but very thankful that you wore a turtle neck",
"Not all people who wander are lost",
"There is a reason that roses have thorns",
"Charles ate the french fries knowing they would be his last meal",
"He hated that he loved what she hated about hate",
])
labels = tf.random.uniform((6, ), minval=0, maxval=2, dtype=tf.dtypes.int32)
classifier_model = build_classifier_model()
classifier_model.compile(optimizer=tf.keras.optimizers.Adam(),
loss=tf.keras.losses.SparseCategoricalCrossentropy(),
metrics=tf.keras.metrics.SparseCategoricalAccuracy())
BATCH_SIZE = 1
train_dataset = tf.data.Dataset.from_tensor_slices(
(sentences, labels)).shuffle(
sentences.shape[0]).batch(
BATCH_SIZE)
classifier_model.fit(x=train_dataset, epochs=2)
Epoch 1/2
6/6 [==============================] - 7s 446ms/step - loss: 2.4348 - sparse_categorical_accuracy: 0.5000
Epoch 2/2
6/6 [==============================] - 3s 447ms/step - loss: 1.3977 - sparse_categorical_accuracy: 0.5000
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/330645.html
上一篇:使用MPS進行細粒度內核調度
