-
Notifications
You must be signed in to change notification settings - Fork 136
Expand file tree
/
Copy pathtest_dl.py
More file actions
80 lines (67 loc) · 3.17 KB
/
test_dl.py
File metadata and controls
80 lines (67 loc) · 3.17 KB
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
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import pytest
from typing import List
from pywy.dataquanta import WayangContext
from pywy.platforms.java import JavaPlugin
from pywy.platforms.spark import SparkPlugin
from pywy.platforms.tensorflow import TensorflowPlugin
from pywy.basic.model.ops import Mean, Cast, Eq, ArgMax, Input, Op, CrossEntropyLoss, Linear, Sigmoid
from pywy.basic.model.optimizer import GradientDescent
from pywy.basic.model.option import Option
from pywy.basic.model.models import DLModel
from importlib import resources
from pywy.tests import resources as resources_folder
def test_dl_tensorflow():
with resources.path(resources_folder, "sample_data.md") as resource_path, \
resources.path(resources_folder, "wordcount_out_python.txt") as output_path:
l1 = Linear(4, 64, True)
s1 = Sigmoid()
l2 = Linear(64, 3, True)
s1.with_ops(l1.with_ops(Input(Input.Type.FEATURES)))
l2.with_ops(s1)
model = DLModel(l2)
criterion = CrossEntropyLoss(3)
criterion.with_ops(
Input(Input.Type.PREDICTED),
Input(Input.Type.LABEL, Op.DType.INT32)
)
acc = Mean(0)
acc.with_ops(
Cast(Op.DType.FLOAT32).with_ops(
Eq().with_ops(
ArgMax(1).with_ops(
Input(Input.Type.PREDICTED)
),
Input(Input.Type.LABEL, Op.DType.INT32)
)
)
)
optimizer = GradientDescent(0.02)
option = Option(criterion, optimizer, 6, 100)
floats: List[List[float]] = [[5.1, 3.5, 1.4, 0.2]]
ints: List[List[int]] = [[0, 0, 1, 1, 2, 2]]
ctx = WayangContext() \
.register({JavaPlugin, SparkPlugin, TensorflowPlugin})
trainXSource = ctx.textfile(f"file://{resource_path}").map(lambda x: floats, str, List[List[float]])
trainYSource = ctx.textfile(f"file://{resource_path}").map(lambda x: floats, str, List[List[float]])
testXSource = ctx.textfile(f"file://{resource_path}").map(lambda x: floats, str, List[List[float]])
data_quanta = trainXSource.dlTraining(model, option, trainYSource, List[List[float]], List[List[float]]) \
.predict(testXSource, List[List[float]], List[List[float]]) \
.map(lambda x: "Test", List[List[float]], str) \
.store_textfile(f"file://{output_path}", str)
assert data_quanta is not None