Pradeep Kumar commited on
Commit
10384c3
·
verified ·
1 Parent(s): a173d3b

Upload ISCO_Prediction.py

Browse files
Files changed (1) hide show
  1. ISCO_Prediction.py +88 -0
ISCO_Prediction.py ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ Created on Mon Aug 12 11:34:42 2024
4
+
5
+ @author: Pradeep Kumar
6
+
7
+ """
8
+ import numpy as np
9
+ import tensorflow as tf
10
+ import tensorflow_hub as hub
11
+ import tf_keras as keras
12
+ import pandas as pd
13
+ from tensorflow.keras.models import load_model
14
+ from official.nlp.data import classifier_data_lib
15
+ from official.nlp.tools import tokenization
16
+ import joblib
17
+
18
+ model = load_model('best_model.h5', custom_objects={'KerasLayer': hub.KerasLayer})
19
+
20
+
21
+ vocab_file = model.resolved_object.vocab_file.asset_path.numpy()
22
+ do_lower_case = model.resolved_object.do_lower_case.numpy()
23
+ tokenizer = tokenization.FullTokenizer(vocab_file,do_lower_case)
24
+
25
+ # Parameters
26
+ max_seq_length = 128
27
+ label_list = 424
28
+ dummy_label = 100
29
+
30
+
31
+ # Define a function to preprocess the new data
32
+ def get_feature_new(text, max_seq_length, tokenizer, dummy_label):
33
+ example = classifier_data_lib.InputExample(guid=None,
34
+ text_a=text.numpy().decode('utf-8'),
35
+ text_b=None,
36
+ label=dummy_label) # Use a valid dummy label
37
+ feature = classifier_data_lib.convert_single_example(0, example, label_list, max_seq_length, tokenizer)
38
+ return feature.input_ids, feature.input_mask, feature.segment_ids
39
+
40
+ def get_feature_map_new(text):
41
+ input_ids, input_mask, segment_ids = tf.py_function(
42
+ lambda text: get_feature_new(text, max_seq_length, tokenizer, dummy_label),
43
+ inp=[text],
44
+ Tout=[tf.int32, tf.int32, tf.int32]
45
+ )
46
+ input_ids.set_shape([max_seq_length])
47
+ input_mask.set_shape([max_seq_length])
48
+ segment_ids.set_shape([max_seq_length])
49
+
50
+ x = {'input_word_ids': input_ids,
51
+ 'input_mask': input_mask,
52
+ 'input_type_ids': segment_ids}
53
+
54
+ return x
55
+
56
+ def preprocess_new_data(texts):
57
+ dataset = tf.data.Dataset.from_tensor_slices((texts,))
58
+ dataset = dataset.map(get_feature_map_new,
59
+ num_parallel_calls=tf.data.experimental.AUTOTUNE)
60
+ dataset = dataset.batch(32, drop_remainder=False)
61
+ dataset = dataset.prefetch(tf.data.experimental.AUTOTUNE)
62
+
63
+ return dataset
64
+
65
+ data = pd.read_csv('data.csv')
66
+
67
+
68
+ label_encoder = joblib.load('label_encoder.joblib')
69
+
70
+
71
+ # Preprocess the new data
72
+ sample_example = data['text'].to_list()
73
+ new_data_dataset = preprocess_new_data(sample_example)
74
+ # Make predictions on the new data
75
+ predictions = model.predict(new_data_dataset)
76
+
77
+ # Decode the predictions
78
+ predicted_classes = [label_list[np.argmax(pred)] for pred in predictions]
79
+
80
+ print(predicted_classes)
81
+ highest_probabilities = [max(instance) for instance in predictions]
82
+ decoded_labels = label_encoder.inverse_transform(predicted_classes)
83
+
84
+ data['prob'] = highest_probabilities
85
+ data['predicted_isco'] = predicted_classes
86
+
87
+ data['target_isco'] =label_encoder.inverse_transform(data.target)
88
+ data['predicted_isco_decoded'] =label_encoder.inverse_transform(data.predicted_isco)