okeowo1014 commited on
Commit
257a51b
1 Parent(s): 63d582a

Update tr.py

Browse files
Files changed (1) hide show
  1. tr.py +36 -20
tr.py CHANGED
@@ -1,33 +1,49 @@
1
- import tensorflow as tf
2
- from tensorflow.keras.layers import Dense, Embedding, GlobalAveragePooling1D
3
  from tensorflow.keras.models import Sequential
4
- from transformers import AutoTokenizer, TFAutoModelForSequenceClassification, pipeline
 
 
 
5
 
6
  # Sample data for sentiment analysis
7
  texts = ["I love deep learning!", "I hate Mondays.", "This movie is fantastic.", "The weather is terrible."]
 
8
 
9
- labels = [1, 0, 1, 0] # 1 for positive sentiment, 0 for negative sentiment
 
 
 
 
10
 
11
- # Load the tokenizer and model
12
- tokenizer = AutoTokenizer.from_pretrained('bert-base-uncased')
13
- model = TFAutoModelForSequenceClassification.from_pretrained('bert-base-uncased', num_labels=2)
14
 
15
- # Tokenize the texts
16
- inputs = tokenizer(texts, padding=True, truncation=True, return_tensors='tf')
 
 
 
 
 
17
 
18
- # Compile the model
19
- model.compile(optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), metrics=['accuracy'])
20
 
21
  # Train the model
22
- model.fit(inputs, labels, epochs=3, batch_size=2)
 
 
 
 
23
 
24
- # Save the model to Hugging Face Model Hub
25
- model.save_pretrained("./my-text-classifier")
26
 
27
- # Load the saved model from disk
28
- loaded_model = TFAutoModelForSequenceClassification.from_pretrained("./my-text-classifier")
29
 
30
- # Use the loaded model for prediction
31
- classifier = pipeline('text-classification', model=loaded_model, tokenizer=tokenizer)
32
- result = classifier("I'm feeling great!")
33
- print(result)
 
 
 
1
+ import numpy as np
 
2
  from tensorflow.keras.models import Sequential
3
+ from tensorflow.keras.layers import Dense, Embedding, GlobalAveragePooling1D
4
+ from tensorflow.keras.preprocessing.text import Tokenizer
5
+ from tensorflow.keras.preprocessing.sequence import pad_sequences
6
+ from sklearn.model_selection import train_test_split
7
 
8
  # Sample data for sentiment analysis
9
  texts = ["I love deep learning!", "I hate Mondays.", "This movie is fantastic.", "The weather is terrible."]
10
+ labels = np.array([1, 0, 1, 0]) # 1 for positive sentiment, 0 for negative sentiment
11
 
12
+ # Tokenize the texts
13
+ tokenizer = Tokenizer(num_words=1000, oov_token='<OOV>')
14
+ tokenizer.fit_on_texts(texts)
15
+ sequences = tokenizer.texts_to_sequences(texts)
16
+ padded_sequences = pad_sequences(sequences, maxlen=10, padding='post', truncating='post')
17
 
18
+ # Split data into training and testing sets
19
+ X_train, X_test, y_train, y_test = train_test_split(padded_sequences, labels, test_size=0.2, random_state=42)
 
20
 
21
+ # Build the model
22
+ model = Sequential([
23
+ Embedding(input_dim=1000, output_dim=16, input_length=10),
24
+ GlobalAveragePooling1D(),
25
+ Dense(16, activation='relu'),
26
+ Dense(1, activation='sigmoid')
27
+ ])
28
 
29
+ model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
 
30
 
31
  # Train the model
32
+ model.fit(X_train, y_train, epochs=5, batch_size=2)
33
+
34
+ # Evaluate the model
35
+ loss, accuracy = model.evaluate(X_test, y_test)
36
+ print(f'Accuracy: {accuracy * 100:.2f}%')
37
 
38
+ # Save the model
39
+ model.save('my_custom_text_classifier')
40
 
41
+ # Later, load the model and make predictions
42
+ loaded_model = tf.keras.models.load_model('my_custom_text_classifier')
43
 
44
+ # Example prediction
45
+ new_texts = ["I'm feeling great!", "This book is boring."]
46
+ sequences = tokenizer.texts_to_sequences(new_texts)
47
+ padded_sequences = pad_sequences(sequences, maxlen=10, padding='post', truncating='post')
48
+ predictions = loaded_model.predict(padded_sequences)
49
+ print(predictions)