-
Notifications
You must be signed in to change notification settings - Fork 1
/
tst_240306.py
153 lines (115 loc) · 4.77 KB
/
tst_240306.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# tst_240306: wayne: torch CPU vs MTL, TF on CPU only
import socket
HOST = socket.gethostname()
import numpy as np
import string
import re
import sys
import os
os.environ["KERAS_BACKEND"] = "torch" # "torch" # "tensorflow"
import keras
from keras import layers
import tensorflow as tf
import torch
import torchtext
import keras_nlp
import tensorflow_text
# Model constants.
max_features = 20000
embedding_dim = 128
sequence_length = 500
def custom_standardization(input_data):
lowercase = tf.strings.lower(input_data)
stripped_html = tf.strings.regex_replace(lowercase, "<br />", " ")
return tf.strings.regex_replace(
stripped_html, f"[{re.escape(string.punctuation)}]", ""
)
vectorize_layer = keras.layers.TextVectorization(
standardize=custom_standardization,
max_tokens=max_features,
output_mode="int",
output_sequence_length=sequence_length,
)
def vectorize_text(text, label):
text = tf.expand_dims(text, -1)
return vectorize_layer(text), label
if os.environ["KERAS_BACKEND"] == 'torch':
# DEVICE = torch.device("mps")
# print(f'device torch mps fixed: {DEVICE == torch.device("mps")}')
DEVICE = torch.device("cpu")
print(f'device torch cpu fixed: {DEVICE == torch.device("cpu")}')
elif os.environ["KERAS_BACKEND"] == 'tensorflow':
# DEVICE = tf.config.list_physical_devices("GPU")[0]
# print(f'device fixed: {DEVICE == tf.config.list_physical_devices("GPU")[0]}')
DEVICE = tf.config.list_physical_devices("CPU")[0]
print(f'device TF CPU fixed: {DEVICE == tf.config.list_physical_devices("CPU")[0]}')
if os.environ["KERAS_BACKEND"] == 'torch' and DEVICE == torch.device("mps"):
print(f"{HOST} torch MPS {str(DEVICE)} avail={torch.backends.mps.is_available()} built={torch.backends.mps.is_built()}")
# elif os.environ["KERAS_BACKEND"] == 'tensorflow' and DEVICE == tf.config.list_physical_devices('GPU')[0]:
# tf.debugging.set_log_device_placement(True)
# print(f"{HOST} TF MPS NGPUs Available: {len(tf.config.list_physical_devices('GPU'))}")
elif os.environ["KERAS_BACKEND"] == 'tensorflow' and DEVICE != tf.config.list_physical_devices("CPU")[0]:
print('keras-tensorflow doesnt work on MTL!?')
sys.exit(-1)
print(f'''\ttorch={torch.__version__}
torchtext={torchtext.__version__}
tensorflow={tf.__version__}
tensorflow_text={tensorflow_text.__version__}
keras={keras.__version__}
keras_nlp={keras_nlp.__version__}
''')
max_features = 20000
embedding_dim = 128
sequence_length = 500
with torch.device("cpu"):
# with tf.device("CPU"):
kerasNLPDir = '/Users/rik/data/corpora/keras-nlp/'
batch_size = 32
raw_train_ds = keras.utils.text_dataset_from_directory(
kerasNLPDir+"aclImdb/train",
batch_size=batch_size,
validation_split=0.2,
subset="training",
seed=1337,
)
raw_val_ds = keras.utils.text_dataset_from_directory(
kerasNLPDir+"aclImdb/train",
batch_size=batch_size,
validation_split=0.2,
subset="validation",
seed=1337,
)
raw_test_ds = keras.utils.text_dataset_from_directory(
kerasNLPDir+"aclImdb/test",
batch_size=batch_size,
)
text_ds = raw_train_ds.map(lambda x, y: x)
vectorize_layer.adapt(text_ds)
# Vectorize the data.
train_ds = raw_train_ds.map(vectorize_text)
val_ds = raw_val_ds.map(vectorize_text)
test_ds = raw_test_ds.map(vectorize_text)
# A integer input for vocab indices.
inputs = keras.Input(shape=(None,), dtype="int64")
# Next, we add a layer to map those vocab indices into a space of dimensionality
# 'embedding_dim'.
x = layers.Embedding(max_features, embedding_dim,name='embed')(inputs)
x = layers.Dropout(0.5)(x)
# Conv1D + global max pooling
x = layers.Conv1D(128, 7, padding="valid", activation="relu", strides=3,name='conv1')(x)
x = layers.Conv1D(128, 7, padding="valid", activation="relu", strides=3,name='conv2')(x)
x = layers.GlobalMaxPooling1D()(x)
# We add a vanilla hidden layer:
x = layers.Dense(128, activation="relu",name='dense')(x)
x = layers.Dropout(0.5,name='drop')(x)
# We project onto a single unit output layer, and squash it with a sigmoid:
predictions = layers.Dense(1, activation="sigmoid", name="predictions")(x)
model = keras.Model(inputs, predictions)
# Compile the model with binary crossentropy loss and an adam optimizer.
model.compile(loss="binary_crossentropy", optimizer="adam", metrics=["accuracy"])
epochs = 2
# Fit the model using the train and test datasets.
model.fit(train_ds, validation_data=val_ds, epochs=epochs)
model.evaluate(test_ds)
import pdb; pdb.set_trace()
print('done')