Skip to content

Commit 16af26b

Browse files
update docs
1 parent a673a0b commit 16af26b

33 files changed

+1875
-305
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,7 @@ learning_orchestra_client/transform/__pycache__
66
learning_orchestra_client/main.py
77
learning_orchestra_client/explore/__pycache__
88
learning_orchestra_client/builder/__pycache__
9-
docs
9+
docs
10+
sentiment_analysis_output.py
11+
mnist_output.py
12+
mnist_treatment.py

examples/mnist.py

Lines changed: 274 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,274 @@
1+
from learning_orchestra_client.dataset.generic import DatasetGeneric
2+
from learning_orchestra_client.function.python import FunctionPython
3+
from learning_orchestra_client.model.tensorflow import ModelTensorflow
4+
from learning_orchestra_client.train.tensorflow import TrainTensorflow
5+
from learning_orchestra_client.predict.tensorflow import PredictTensorflow
6+
from learning_orchestra_client.evaluate.tensorflow import EvaluateTensorflow
7+
8+
CLUSTER_IP = "http://35.224.50.116"
9+
10+
dataset_generic = DatasetGeneric(CLUSTER_IP)
11+
dataset_generic.insert_dataset_async(
12+
dataset_name="mnist_train_images",
13+
url="https://drive.google.com/u/0/uc?"
14+
"id=1ec6hVwvq4UPyQ7DmJVxzofE2TcKUTHNY&export=download",
15+
)
16+
17+
dataset_generic.insert_dataset_async(
18+
dataset_name="mnist_train_labels",
19+
url="https://drive.google.com/u/0/uc?"
20+
"id=187ID_LfQCTJOYieC-yo94jxnWiFJZ7uX&export=download",
21+
)
22+
23+
dataset_generic.insert_dataset_async(
24+
dataset_name="mnist_test_images",
25+
url="https://drive.google.com/u/0/uc?"
26+
"id=1ZNuiRJLKSFzmegRgIHXUl4t-UdjEPYVf&export=download",
27+
)
28+
29+
dataset_generic.insert_dataset_async(
30+
dataset_name="mnist_test_labels",
31+
url="https://drive.google.com/u/0/uc?"
32+
"id=1v0PRmUL8nOg3mHakjfTxOjNA9bi6XkkA&export=download",
33+
)
34+
35+
dataset_generic.wait("mnist_train_images")
36+
dataset_generic.wait("mnist_train_labels")
37+
dataset_generic.wait("mnist_test_images")
38+
dataset_generic.wait("mnist_test_labels")
39+
40+
function_python = FunctionPython(CLUSTER_IP)
41+
mnist_datasets_treatment = '''
42+
import numpy as np
43+
import struct as st
44+
import math
45+
46+
training_filenames = {'images': mnist_train_images,
47+
'labels': mnist_train_labels}
48+
test_filenames = {'images': mnist_test_images,
49+
'labels': mnist_test_labels}
50+
51+
data_types = {
52+
0x08: ('ubyte', 'B', 1),
53+
0x09: ('byte', 'b', 1),
54+
0x0B: ('>i2', 'h', 2),
55+
0x0C: ('>i4', 'i', 4),
56+
0x0D: ('>f4', 'f', 4),
57+
0x0E: ('>f8', 'd', 8)}
58+
59+
60+
def treat_dataset(dataset: dict) -> tuple:
61+
global np, st, math, data_types
62+
63+
for name in dataset.keys():
64+
if name == 'images':
65+
images_file = dataset[name]
66+
if name == 'labels':
67+
labels_file = dataset[name]
68+
69+
images_file.seek(0)
70+
magic = st.unpack('>4B', images_file.read(4))
71+
data_format = data_types[magic[2]][1]
72+
data_size = data_types[magic[2]][2]
73+
74+
images_file.seek(4)
75+
76+
content_amount = st.unpack('>I', images_file.read(4))[0]
77+
rows_amount = st.unpack('>I', images_file.read(4))[0]
78+
columns_amount = st.unpack('>I', images_file.read(4))[0]
79+
80+
labels_file.seek(8)
81+
82+
labels_array = np.asarray(
83+
st.unpack(
84+
'>' + data_format * content_amount,
85+
labels_file.read(content_amount * data_size))).reshape(
86+
(content_amount, 1))
87+
88+
n_batch = 10000
89+
n_iter = int(math.ceil(content_amount / n_batch))
90+
n_bytes = n_batch * rows_amount * columns_amount * data_size
91+
images_array = np.array([])
92+
93+
for i in range(0, n_iter):
94+
temp_images_array = np.asarray(
95+
st.unpack('>' + data_format * n_bytes,
96+
images_file.read(n_bytes))).reshape(
97+
(n_batch, rows_amount, columns_amount))
98+
99+
if images_array.size == 0:
100+
images_array = temp_images_array
101+
else:
102+
images_array = np.vstack((images_array, temp_images_array))
103+
104+
temp_images_array = np.array([])
105+
106+
return images_array, labels_array
107+
108+
109+
train_images, train_labels = treat_dataset(training_filenames)
110+
test_images, test_labels = treat_dataset(test_filenames)
111+
112+
response = {
113+
"train_images": train_images,
114+
"train_labels": train_labels,
115+
"test_images": test_images,
116+
"test_labels": test_labels,
117+
}
118+
'''
119+
120+
function_python.run_function_async(
121+
name="mnist_datasets_treated",
122+
parameters={
123+
"mnist_train_images": "$mnist_train_images",
124+
"mnist_train_labels": "$mnist_train_labels",
125+
"mnist_test_images": "$mnist_test_images",
126+
"mnist_test_labels": "$mnist_test_labels"
127+
},
128+
code=mnist_datasets_treatment)
129+
function_python.wait("mnist_datasets_treated")
130+
131+
mnist_datasets_normalization = '''
132+
test_images = test_images / 255
133+
train_images = train_images / 255
134+
135+
print(train_images.shape)
136+
print(train_labels.shape)
137+
138+
print(test_images.shape)
139+
print(test_labels.shape)
140+
141+
response = {
142+
"test_images": test_images,
143+
"test_labels": test_labels,
144+
"train_images": train_images,
145+
"train_labels": train_labels
146+
}
147+
'''
148+
149+
function_python.run_function_async(
150+
name="mnist_datasets_normalized",
151+
parameters={
152+
"train_images": "$mnist_datasets_treated.train_images",
153+
"train_labels": "$mnist_datasets_treated.train_labels",
154+
"test_images": "$mnist_datasets_treated.test_images",
155+
"test_labels": "$mnist_datasets_treated.test_labels"
156+
},
157+
code=mnist_datasets_normalization)
158+
function_python.wait("mnist_datasets_normalized")
159+
160+
model_tensorflow = ModelTensorflow(CLUSTER_IP)
161+
model_tensorflow.create_model_async(
162+
name="mnist_model",
163+
module_path="tensorflow.keras.models",
164+
class_name="Sequential",
165+
class_parameters={
166+
"layers":
167+
[
168+
"#tensorflow.keras.layers.Flatten(input_shape=(28, 28))",
169+
"#tensorflow.keras.layers.Dense(128, activation='relu')",
170+
"#tensorflow.keras.layers.Dense(10, activation='softmax')",
171+
]}
172+
)
173+
model_tensorflow.wait("mnist_model")
174+
175+
model_compilation = '''
176+
import tensorflow as tf
177+
178+
model.compile(
179+
optimizer=tf.keras.optimizers.Adam(0.001),
180+
loss=tf.keras.losses.SparseCategoricalCrossentropy(),
181+
metrics=[tf.keras.metrics.SparseCategoricalAccuracy()],
182+
)
183+
184+
response = model
185+
'''
186+
187+
function_python.run_function_async(
188+
name="mnist_model_compiled",
189+
parameters={
190+
"model": "$mnist_model"
191+
},
192+
code=model_compilation)
193+
function_python.wait("mnist_model_compiled")
194+
195+
train_tensorflow = TrainTensorflow(CLUSTER_IP)
196+
train_tensorflow.create_training_async(
197+
name="mnist_model_trained",
198+
model_name="mnist_model",
199+
parent_name="mnist_model_compiled",
200+
method_name="fit",
201+
parameters={
202+
"x": "$mnist_datasets_normalized.train_images",
203+
"y": "$mnist_datasets_normalized.train_labels",
204+
"validation_split": 0.1,
205+
"epochs": 6,
206+
}
207+
)
208+
train_tensorflow.wait("mnist_model_trained")
209+
210+
predict_tensorflow = PredictTensorflow(CLUSTER_IP)
211+
predict_tensorflow.create_prediction_async(
212+
name="mnist_model_predicted",
213+
model_name="mnist_model",
214+
parent_name="mnist_model_trained",
215+
method_name="predict",
216+
parameters={
217+
"x": "$mnist_datasets_normalized.test_images"
218+
}
219+
)
220+
221+
evaluate_tensorflow = EvaluateTensorflow(CLUSTER_IP)
222+
evaluate_tensorflow.create_evaluate_async(
223+
name="mnist_model_evaluated",
224+
model_name="mnist_model",
225+
parent_name="mnist_model_trained",
226+
method_name="evaluate",
227+
parameters={
228+
"x": "$mnist_datasets_normalized.test_images",
229+
"y": "$mnist_datasets_normalized.test_labels"
230+
}
231+
)
232+
233+
predict_tensorflow.wait("mnist_model_predicted")
234+
evaluate_tensorflow.wait("mnist_model_evaluated")
235+
236+
show_mnist_predict = '''
237+
print(mnist_predicted)
238+
response = None
239+
'''
240+
function_python.run_function_async(
241+
name="mnist_model_predicted_print",
242+
parameters={
243+
"mnist_predicted": "$mnist_model_predicted"
244+
},
245+
code=show_mnist_predict
246+
)
247+
248+
249+
show_mnist_evaluate = '''
250+
print(mnist_evaluated)
251+
response = None
252+
'''
253+
function_python.run_function_async(
254+
name="mnist_model_evaluated_print",
255+
parameters={
256+
"mnist_evaluated": "$mnist_model_evaluated"
257+
},
258+
code=show_mnist_evaluate
259+
)
260+
261+
function_python.wait("mnist_model_evaluated_print")
262+
function_python.wait("mnist_model_predicted_print")
263+
264+
print(function_python.search_execution_content(
265+
name="mnist_model_predicted_print",
266+
limit=1,
267+
skip=1,
268+
pretty_response=True))
269+
270+
print(function_python.search_execution_content(
271+
name="mnist_model_evaluated_print",
272+
limit=1,
273+
skip=1,
274+
pretty_response=True))

0 commit comments

Comments
 (0)