How to load use pretrained bert model (trained using ktrain)?

I trained the bert_model and saved it it was woking just fine. After saving the predictor it creates bert_model folder which contains tf_model.h5 and tf_model.preproc but loading the predictor and saving it gives lots of errors.

y_train = [encoding[x] for x in y_train]
y_test = [encoding[x] for x in y_test]
(x_train,  y_train), (x_test, y_test), preproc = text.texts_from_array(x_train=X_train, y_train=y_train,
                                                                       x_test=X_test, y_test=y_test,
                                                                       class_names=class_names,
                                                                       preprocess_mode='bert',
                                                                       maxlen=350,
                                                                       max_features=35000)

model = text.text_classifier(
    'bert', train_data=(x_train, y_train), preproc=preproc)
learner = ktrain.get_learner(model, train_data=(x_train, y_train),
                             val_data=(x_test, y_test),
                             batch_size=6)
learner.fit_onecycle(2e-5, 3)
learner.validate(val_data=(x_test, y_test), class_names=class_names)
predictor = ktrain.get_predictor(learner.model, preproc)
predictor.get_classes()

import time
message = 'she left me while i was waiting for her and she isnt herself '

start_time = time.time()
prediction = predictor.predict(message)

print('predicted: {} ({:.2f})'.format(prediction, (time.time() - start_time)))
predictor.save("models/bert_model")

Now after saving it it created bert_model folder containing two files tf_model.h5 and tf_model.preproc
to load it and use i am using this:


from keras.models import load_model
import ktrain
from ktrain import text
import time

message = 'she left me while i was waiting for her and she isnt herself '
model_path = "/home/priyanshi/Desktop/folder/models/bert_model/"

predictor = ktrain.load_predictor(model_path)

start_time = time.time()
prediction = predictor.predict(message)

print('predicted: {} ({:.2f} seconds)'.format(
    prediction, (time.time() - start_time)))

But it is giving this error:—

python -u “/home/priyanshi/Desktop/Array/email obsulation/test2.py”
2024-06-02 00:59:18.984734: I external/local_tsl/tsl/cuda/cudart_stub.cc:32] Could not find cuda drivers on your machine, GPU will not be used.
2024-06-02 00:59:18.987632: I external/local_tsl/tsl/cuda/cudart_stub.cc:32] Could not find cuda drivers on your machine, GPU will not be used.
2024-06-02 00:59:19.031606: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
2024-06-02 00:59:19.734612: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT
Call to keras.models.load_model failed. Try manually invoking this function to investigate error and report issue if necessary.
Traceback (most recent call last):
File “/home/priyanshi/.local/lib/python3.10/site-packages/ktrain/core.py”, line 2048, in _load_model
model = keras.models.load_model(
File “/home/priyanshi/.local/lib/python3.10/site-packages/tf_keras/src/saving/saving_api.py”, line 262, in load_model
return legacy_sm_saving_lib.load_model(
File “/home/priyanshi/.local/lib/python3.10/site-packages/tf_keras/src/utils/traceback_utils.py”, line 70, in error_handler
raise e.with_traceback(filtered_tb) from None
File “/home/priyanshi/.local/lib/python3.10/site-packages/tensorflow/python/saved_model/loader_impl.py”, line 119, in parse_saved_model
raise IOError(
OSError: SavedModel file does not exist at: /home/priyanshi/Desktop/Array/email obsulation/bert_model//{saved_model.pbtxt|saved_model.pb}

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “/home/priyanshi/Desktop/Array/email obsulation/test2.py”, line 13, in
predictor = ktrain.load_predictor(model_path)
File “/home/priyanshi/.local/lib/python3.10/site-packages/ktrain/core.py”, line 1848, in load_predictor
model = _load_model(fpath, preproc=preproc, custom_objects=custom_objects)
File “/home/priyanshi/.local/lib/python3.10/site-packages/ktrain/core.py”, line 2055, in _load_model
raise Exception(“Error detected: %s” % (e))
Exception: Error detected: SavedModel file does not exist at: /home/priyanshi/Desktop/Array/email obsulation/bert_model//{saved_model.pbtxt|saved_model.pb}

I tried many other ways to use it but its not working and giving errors

New contributor

Priyanshi Soni is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật