I am trying to create an NLP neural-network using the following code:
imports:
import zipfile
import pandas as pd
import numpy as np
import tensorflow as tf
from tensorflow.keras.layers import TextVectorization,Embedding,Input,GlobalAveragePooling1D,Dense
from sklearn.model_selection import train_test_split
Download and unzip dataset:
# Download data (same as from Kaggle)
!wget "https://storage.googleapis.com/ztm_tf_course/nlp_getting_started.zip"
# Unzip data
zip_ref = zipfile.ZipFile("nlp_getting_started.zip", "r")
zip_ref.extractall()
zip_ref.close()
Split data into train and test datasets:
train_df = pd.read_csv("train.csv")
train_sentences, val_sentences, train_labels, val_labels = train_test_split(train_df['text'], train_df['target'],train_size=.8)
average_output_sequence_length = 15
Create the neural-network:
input = Input(shape=(1,),dtype='string')
x = TextVectorization(max_tokens=10000,
ngrams=5,
standardize='lower_and_strip_punctuation',
output_mode='int',
output_sequence_length = average_output_sequence_length)(input)
x = Embedding(input_dim=22,embeddings_initializer='uniform',output_dim=128, name= 'embeding_layer')(x)
x = GlobalAveragePooling1D()(x)
output = Dense(1,activation='sigmoid')(x)
model = tf.keras.Model(input,output)
model.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'] )
model.fit(x=train_sentences,y=train_labels,epochs=6,validation_data=(val_sentences,val_labels))
Unfortunately, when i run the code, i face with the following error:
Epoch 1/6
---------------------------------------------------------------------------
FailedPreconditionError Traceback (most recent call last)
<ipython-input-52-991547d73612> in <module>()
13 model.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'] )
14
---> 15 model.fit(x=train_sentences,y=train_labels,epochs=6,validation_data=(val_sentences,val_labels))
1 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
57 ctx.ensure_initialized()
58 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
---> 59 inputs, attrs, num_outputs)
60 except core._NotOkStatusException as e:
61 if name is not None:
FailedPreconditionError: Table not initialized.
[[node model_6/text_vectorization_7/string_lookup_7/None_Lookup/LookupTableFindV2
(defined at /usr/local/lib/python3.7/dist-packages/keras/layers/preprocessing/index_lookup.py:669)
]] [Op:__inference_train_function_5066]
Errors may have originated from an input operation.
UPDATE: I got it to work when i change the way that i use the functional code:
Create TextVectorization function:
text_vectorization_layer = TextVectorization(max_tokens=10000,
ngrams=5,
standardize='lower_and_strip_punctuation',
output_mode='int',
output_sequence_length = average_output_sequence_length
)
And then finally create the neural-network:
input = Input(shape=(1,),dtype='string')
x = text_vectorization_layer(input)
x = Embedding(input_dim=22,embeddings_initializer='uniform',output_dim=128, name= 'embeding_layer')(x)
x = GlobalAveragePooling1D()(x)
output = Dense(1,activation='sigmoid')(x)
model = tf.keras.Model(input,output)
model.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'] )
model.fit(x=train_sentences,y=train_labels,epochs=6,validation_data=(val_sentences,val_labels))
QUESTION:
But, i still do not understand why these change fix the issue ?
in other word, why the:
x = TextVectorization(input)
and the
x = TextVectorization(max_tokens=10000,
ngrams=5,
standardize='lower_and_strip_punctuation',
output_mode='int',
output_sequence_length = average_output_sequence_length)(input)
are not equal ?
CodePudding user response:
The TextVectorization
layer is a preprocessing layer that needs to be instantiated before being called. Also as the docs explain:
The vocabulary for the layer must be either supplied on construction or learned via adapt().
Another important information can be found here:
Crucially, these layers are non-trainable. Their state is not set during training; it must be set before training, either by initializing them from a precomputed constant, or by "adapting" them on data
Furthermore, it is important to note, that the TextVectorization
layer uses an underlying StringLookup
layer that also needs to be initialized beforehand. Otherwise, you will get the FailedPreconditionError: Table not initialized
as you posted.