Exemple #1
0
def wrap_high_level_accuracy(state):
    model = SLRModel(
        features=Features(Feature("Years", int, 1), ),
        predict=Feature("Salary", int, 1),
        location="tempdir",
    )

    train(
        model,
        {
            "Years": 0,
            "Salary": 10
        },
        {
            "Years": 1,
            "Salary": 20
        },
        {
            "Years": 2,
            "Salary": 30
        },
        {
            "Years": 3,
            "Salary": 40
        },
    )

    yield
Exemple #2
0
def wrap_noasync_accuracy(state):
    model = SLRModel(
        features=Features(Feature("Years", int, 1), ),
        predict=Feature("Salary", int, 1),
        directory="tempdir",
    )

    train(
        model,
        {
            "Years": 0,
            "Salary": 10
        },
        {
            "Years": 1,
            "Salary": 20
        },
        {
            "Years": 2,
            "Salary": 30
        },
        {
            "Years": 3,
            "Salary": 40
        },
    )

    yield
Exemple #3
0
async def main():
    # Load the model using the entrypoint listed on the model plugins page
    SLRModel = Model.load("slr")

    # Configure the model
    model = SLRModel(
        features=Features(Feature("Years", int, 1)),
        predict=Feature("Salary", int, 1),
        # You can set this as any archive format like zip, tgz, etc.
        location="slr-model.tar.gz",
    )

    # Train the model
    await train(
        model,
        {
            "Years": 0,
            "Expertise": 1,
            "Trust": 0.1,
            "Salary": 10
        },
        {
            "Years": 1,
            "Expertise": 3,
            "Trust": 0.2,
            "Salary": 20
        },
    )
Exemple #4
0
    async def test_models(self):
        with tempfile.TemporaryDirectory() as tempdir:
            # Model the HTTP API will pre-load
            model = SLRModel(
                features=Features(Feature("f1", float, 1)),
                predict=Feature("ans", int, 1),
                directory=tempdir,
            )

            # y = m * x + b for equation SLR is solving for
            m = 5
            b = 3

            # Train the model
            await train(model, *[{
                "f1": x,
                "ans": m * x + b
            } for x in range(0, 10)])

            await accuracy(
                model, *[{
                    "f1": x,
                    "ans": m * x + b
                } for x in range(10, 20)])

            async with ServerRunner.patch(HTTPService.server) as tserver:
                cli = await tserver.start(
                    HTTPService.server.cli(
                        "-insecure",
                        "-port",
                        "0",
                        "-models",
                        "mymodel=slr",
                        "-model-mymodel-directory",
                        tempdir,
                        "-model-mymodel-features",
                        "f1:float:1",
                        "-model-mymodel-predict",
                        "ans:int:1",
                    ))
                async with self.post(
                        cli,
                        f"/model/mymodel/predict/0",
                        json={
                            f"record_{x}": {
                                "features": {
                                    "f1": x
                                }
                            }
                            for x in range(20, 30)
                        },
                ) as response:
                    response = await response.json()
                    records = response["records"]
                    self.assertEqual(len(records), 10)
                    for record in records.values():
                        should_be = m * record["features"]["f1"] + b
                        prediction = record["prediction"]["ans"]["value"]
                        percent_error = abs(should_be - prediction) / should_be
                        self.assertLess(percent_error, 0.2)
Exemple #5
0
async def main():
    # Load the model using the entrypoint listed on the model plugins page
    SLRModel = Model.load("slr")

    # Configure the model
    model = SLRModel(
        features=Features(Feature("Years", int, 1)),
        predict=Feature("Salary", int, 1),
        directory="slr-model",
    )

    # Train the model
    await train(
        model,
        {
            "Years": 0,
            "Expertise": 1,
            "Trust": 0.1,
            "Salary": 10
        },
        {
            "Years": 1,
            "Expertise": 3,
            "Trust": 0.2,
            "Salary": 20
        },
    )
Exemple #6
0
 def setUpClass(cls):
     # Create a temporary directory to store the trained model
     cls.model_dir = tempfile.TemporaryDirectory()
     # Create an instance of the model
     cls.model = MySLRModel(
         features=Features(Feature("X", float, 1)),
         predict=Feature("Y", float, 1),
         directory=cls.model_dir.name,
     )
Exemple #7
0
 def setUpClass(cls):
     # Create a temporary directory to store the trained model
     cls.model_dir = tempfile.TemporaryDirectory()
     # Create an instance of the model
     cls.model = MySLRModel(
         features=Features(Feature("X", float, 1)),
         predict=Feature("Y", float, 1),
         location=cls.model_dir.name,
     )
     cls.scorer = MeanSquaredErrorAccuracy()
Exemple #8
0
 async def _add_fake_model(self):
     with tempfile.TemporaryDirectory() as tempdir:
         async with FakeModel(
                 FakeModelConfig(
                     location=tempdir,
                     features=Features(Feature("by_ten")),
                     predict=Feature("by_ten"),
                 )) as model:
             self.model = self.cli.app["models"][self.mlabel] = model
             async with model() as mctx:
                 self.mctx = self.cli.app["model_contexts"][
                     self.mlabel] = mctx
                 yield
Exemple #9
0
 def setUpClass(cls):
     cls.model_dir = tempfile.TemporaryDirectory()
     cls.model = PyTorchNeuralNetwork(
         classifications=["rock", "paper", "scissors"],
         features=Features(Feature("image", int, 300 * 300)),
         predict=Feature("label", int, 1),
         directory=cls.model_dir.name,
         network=RockPaperScissorsModel,
         epochs=1,
         batch_size=32,
         imageSize=150,
         validation_split=0.2,
         loss=Loss,
         optimizer="Adam",
         enableGPU=True,
     )
Exemple #10
0
 def setUpClass(cls):
     # Create a temporary directory to store the trained model
     cls.model_dir = tempfile.TemporaryDirectory()
     # Create the training data
     cls.train_data = []
     for x, y in TRAIN_DATA:
         cls.train_data.append({"X": x, "Y": y})
     # Create the test data
     cls.test_data = []
     for x, y in TEST_DATA:
         cls.test_data.append({"X": x, "Y": y})
     # Create an instance of the model
     cls.model = LogisticRegression(
         directory=cls.model_dir.name,
         predict=DefFeature("Y", float, 1),
         features=Features(DefFeature("X", float, 1)),
     )
Exemple #11
0
async def main():
    # Configure the model
    model = MySLRModel(
        features=Features(Feature("Years", int, 1)),
        predict=Feature("Salary", int, 1),
        directory="model",
    )

    # Train the model
    await train(model, "train.csv")

    # Assess accuracy
    print("Accuracy:", await accuracy(model, "test.csv"))

    # Make predictions
    async for i, features, prediction in predict(model, "predict.csv"):
        features["Salary"] = prediction["Salary"]["value"]
        print(features)
 def setUpClass(cls):
     cls.model_dir = tempfile.TemporaryDirectory()
     cls.model = PyTorchNeuralNetwork(
         classifications=["rock", "paper", "scissors"],
         features=Features(Feature("image", int, 300 * 300)),
         predict=Feature("label", int, 1),
         directory=cls.model_dir.name,
         network=RockPaperScissorsModel,
         epochs=1,
         batch_size=32,
         imageSize=150,
         validation_split=0.2,
         loss=Loss,
         optimizer="Adam",
         enableGPU=True,
     )
     cls.traindir = asyncio.run(
         cached_download_unpack_archive(
             "https://storage.googleapis.com/laurencemoroney-blog.appspot.com/rps.zip",
             "rps.zip",
             "traindir",
             "c6a9119b0c6a0907b782bd99e04ce09a0924c0895df6a26bc6fb06baca4526f55e51f7156cceb4791cc65632d66085e8",
         )
     )
     cls.testdir = asyncio.run(
         cached_download_unpack_archive(
             "https://storage.googleapis.com/laurencemoroney-blog.appspot.com/rps-test-set.zip",
             "rps-test-set.zip",
             "testdir",
             "fc45a0ebe58b9aafc3cd5a60020fa042d3a19c26b0f820aee630b9602c8f53dd52fd40f35d44432dd031dea8f30a5f66",
         )
     )
     cls.predictdir = asyncio.run(
         cached_download_unpack_archive(
             "https://storage.googleapis.com/laurencemoroney-blog.appspot.com/rps-validation.zip",
             "rps-validation.zip",
             "predictdir",
             "375457bb95771ffeace2beedab877292d232f31e76502618d25e0d92a3e029d386429f52c771b05ae1c7229d2f5ecc29",
         )
     )
Exemple #13
0
async def main():
    # Configure the model
    model = MySLRModel(
        features=Features(Feature("Years", int, 1)),
        predict=Feature("Salary", int, 1),
        location="model",
    )

    # Train the model
    await train(model, "train.csv")

    # Assess accuracy
    scorer = MeanSquaredErrorAccuracy()
    print(
        "Accuracy:",
        await score(model, scorer, Feature("Salary", int, 1), "test.csv"),
    )

    # Make predictions
    async for i, features, prediction in predict(model, "predict.csv"):
        features["Salary"] = prediction["Salary"]["value"]
        print(features)
Exemple #14
0
async def main():
    model = LinearRegressionModel(
        features=Features(
            DefFeature("Years", int, 1),
            DefFeature("Expertise", int, 1),
            DefFeature("Trust", float, 1),
        ),
        predict=DefFeature("Salary", int, 1),
    )

    # Train the model
    await train(
        model,
        {"Years": 0, "Expertise": 1, "Trust": 0.1, "Salary": 10},
        {"Years": 1, "Expertise": 3, "Trust": 0.2, "Salary": 20},
        {"Years": 2, "Expertise": 5, "Trust": 0.3, "Salary": 30},
        {"Years": 3, "Expertise": 7, "Trust": 0.4, "Salary": 40},
    )

    # Assess accuracy
    print(
        "Accuracy:",
        await accuracy(
            model,
            {"Years": 4, "Expertise": 9, "Trust": 0.5, "Salary": 50},
            {"Years": 5, "Expertise": 11, "Trust": 0.6, "Salary": 60},
        ),
    )

    # Make prediction
    async for i, features, prediction in predict(
        model,
        {"Years": 6, "Expertise": 13, "Trust": 0.7},
        {"Years": 7, "Expertise": 15, "Trust": 0.8},
    ):
        features["Salary"] = prediction["Salary"]["value"]
        print(features)
Exemple #15
0
from dffml import CSVSource, Features, Feature
from dffml.noasync import train, accuracy, predict
from dffml_model_scikit import LinearRegressionModel

model = LinearRegressionModel(
    features=Features(
        Feature("Years", int, 1),
        Feature("Expertise", int, 1),
        Feature("Trust", float, 1),
    ),
    predict=Feature("Salary", int, 1),
    directory="tempdir",
)

# Train the model
train(model, "train.csv")

# Assess accuracy (alternate way of specifying data source)
print("Accuracy:", accuracy(model, CSVSource(filename="test.csv")))

# Make prediction
for i, features, prediction in predict(
        model,
    {
        "Years": 6,
        "Expertise": 13,
        "Trust": 0.7
    },
    {
        "Years": 7,
        "Expertise": 15,
Exemple #16
0
from dffml import Features, DefFeature
from dffml.noasync import train, accuracy, predict
from dffml.model.slr import SLRModel

model = SLRModel(
    features=Features(DefFeature("f1", float, 1)),
    predict=DefFeature("ans", int, 1),
)

# Train the model
train(model, "dataset.csv")

# Assess accuracy (alternate way of specifying data source)
print("Accuracy:", accuracy(model, "dataset.csv"))

# Make prediction
for i, features, prediction in predict(model, {"f1": 0.8, "ans": 0}):
    features["ans"] = prediction["ans"]["value"]
    print(features)
Exemple #17
0
async def main():
    model = LinearRegressionModel(
        features=Features(
            Feature("Years", int, 1),
            Feature("Expertise", int, 1),
            Feature("Trust", float, 1),
        ),
        predict=Feature("Salary", int, 1),
        location="tempdir",
    )

    # Train the model
    await train(
        model,
        {
            "Years": 0,
            "Expertise": 1,
            "Trust": 0.1,
            "Salary": 10
        },
        {
            "Years": 1,
            "Expertise": 3,
            "Trust": 0.2,
            "Salary": 20
        },
        {
            "Years": 2,
            "Expertise": 5,
            "Trust": 0.3,
            "Salary": 30
        },
        {
            "Years": 3,
            "Expertise": 7,
            "Trust": 0.4,
            "Salary": 40
        },
    )

    # Assess accuracy
    scorer = MeanSquaredErrorAccuracy()
    print(
        "Accuracy:",
        await score(
            model,
            scorer,
            Feature("Salary", int, 1),
            {
                "Years": 4,
                "Expertise": 9,
                "Trust": 0.5,
                "Salary": 50
            },
            {
                "Years": 5,
                "Expertise": 11,
                "Trust": 0.6,
                "Salary": 60
            },
        ),
    )

    # Make prediction
    async for i, features, prediction in predict(
        model,
        {
            "Years": 6,
            "Expertise": 13,
            "Trust": 0.7
        },
        {
            "Years": 7,
            "Expertise": 15,
            "Trust": 0.8
        },
    ):
        features["Salary"] = prediction["Salary"]["value"]
        print(features)
Exemple #18
0
from dffml import Feature, Features
from dffml.noasync import score, train

from dffml_model_scratch.anomalydetection import AnomalyModel
from dffml_model_scratch.anomaly_detection_scorer import (
    AnomalyDetectionAccuracy,
)

# Configure the model

model = AnomalyModel(
    features=Features(Feature("A", int, 2),),
    predict=Feature("Y", int, 1),
    location="model",
)


# Train the model
train(model, "trainex.csv")

# Assess accuracy for test set
scorer = AnomalyDetectionAccuracy()
print(
    "Test set F1 score :",
    score(model, scorer, Feature("Y", int, 1), "testex.csv"),
)

# Assess accuracy for training set
print(
    "Training set F1 score :",
    score(model, scorer, Feature("Y", int, 1), "trainex.csv"),
Exemple #19
0
from dffml import CSVSource, Features, Feature
from dffml.noasync import train, accuracy, predict
from dffml_model_tensorflow_hub.text_classifier import TextClassificationModel

model = TextClassificationModel(
    features=Features(Feature("sentence", str, 1)),
    predict=Feature("sentiment", int, 1),
    classifications=[0, 1, 2],
    clstype=int,
    directory="tempdir",
)

# Train the model
train(model, "train.csv")

# Assess accuracy (alternate way of specifying data source)
print("Accuracy:", accuracy(model, CSVSource(filename="test.csv")))

# Make prediction
for i, features, prediction in predict(
        model,
    {"sentence": "This track is horrible"},
):
    features["sentiment"] = prediction["sentiment"]["value"]
    print(features)
Exemple #20
0
    XGBClassifierModelConfig,
)

iris = load_iris()
y = iris["target"]
X = iris["data"]
trainX, testX, trainy, testy = train_test_split(X,
                                                y,
                                                test_size=0.1,
                                                random_state=123)

# Configure the model
model = XGBClassifierModel(
    XGBClassifierModelConfig(
        features=Features(Feature(
            "data",
            float,
        )),
        predict=Feature("target", float, 1),
        directory="model",
        max_depth=3,
        learning_rate=0.01,
        n_estimators=200,
        reg_lambda=1,
        reg_alpha=0,
        gamma=0,
        colsample_bytree=0,
        subsample=1,
    ))

# Train the model
train(model, *[{"data": x, "target": y} for x, y in zip(trainX, trainy)])
Exemple #21
0
        # block 4
        x = self.pooling(self.relu(self.conv3(x)))

        # fully connected layer
        x = self.linear(x.view(-1, 16 * 9 * 9))
        return x


RockPaperScissorsModel = ConvNet()
Loss = CrossEntropyLossFunction()

# Define the dffml model config
model = PyTorchNeuralNetwork(
    classifications=["rock", "paper", "scissors"],
    features=Features(Feature("image", int, 300 * 300)),
    predict=Feature("label", int, 1),
    directory="rps_model",
    network=RockPaperScissorsModel,
    epochs=10,
    batch_size=32,
    imageSize=150,
    validation_split=0.2,
    loss=Loss,
    optimizer="Adam",
    enableGPU=True,
    patience=2,
)

# Define source for training image dataset
train_source = DirectorySource(
Exemple #22
0
from dffml import CSVSource, Features, DefFeature
from dffml.noasync import train, accuracy, predict
from dffml_model_tensorflow.dnnr import DNNRegressionModel

model = DNNRegressionModel(
    features=Features(DefFeature("Feature1", float, 1),
                      DefFeature("Feature2", float, 1)),
    predict=DefFeature("TARGET", float, 1),
    epochs=300,
    steps=2000,
    hidden=[8, 16, 8],
)

# Train the model
train(model, "train.csv")

# Assess accuracy (alternate way of specifying data source)
print("Accuracy:", accuracy(model, CSVSource(filename="test.csv")))

# Make prediction
for i, features, prediction in predict(model, {
        "Feature1": 0.21,
        "Feature2": 0.18,
        "TARGET": 0.84
}):
    features["TARGET"] = prediction["TARGET"]["value"]
    print(features)
Exemple #23
0
import asyncio

from dffml import train, Features, DefFeature
from dffml.operation.output import GetSingle
from dffml.df.memory import MemoryOrchestrator
from dffml.operation.mapping import create_mapping
from dffml.operation.preprocess import literal_eval
from dffml.df.types import DataFlow, Input, InputFlow
from dffml.operation.io import AcceptUserInput, print_output
from dffml.operation.model import model_predict, ModelPredictConfig
from dffml.model.slr import SLRModel

slr_model = SLRModel(
    features=Features(DefFeature("Years", int, 1), ),
    predict=DefFeature("Salary", int, 1),
)

# This Dataflow takes input from stdio using `AcceptUserInput`
# operation. The string input which corresponds to feature `Years`
# is converted to `int`/`float` by
# `literal_eval` operation.
# `create_mapping` operation creates a mapping using the numeric output
# of `literal_eval` eg. {"Years":34}.
# The mapping is then fed to `model_predict` operation which
# uses the `slr` model trained above to make prediction. The prediction is then printed to
# stdout using `print_output` operation.
dataflow = DataFlow(
    operations={
        "get_user_input": AcceptUserInput,
        "literal_eval_input": literal_eval,
        "create_feature_map": create_mapping,
Exemple #24
0
from dffml import CSVSource, Features, DefFeature
from dffml.noasync import train, accuracy, predict
from dffml_model_tensorflow.dnnc import DNNClassifierModel

model = DNNClassifierModel(
    features=Features(
        DefFeature("SepalLength", float, 1),
        DefFeature("SepalWidth", float, 1),
        DefFeature("PetalLength", float, 1),
        DefFeature("PetalWidth", float, 1),
    ),
    predict=DefFeature("classification", int, 1),
    epochs=3000,
    steps=20000,
    classifications=[0, 1, 2],
    clstype=int,
)

# Train the model
train(model, "iris_training.csv")

# Assess accuracy (alternate way of specifying data source)
print("Accuracy:", accuracy(model, CSVSource(filename="iris_test.csv")))

# Make prediction
for i, features, prediction in predict(
        model,
    {
        "PetalLength": 4.2,
        "PetalWidth": 1.5,
        "SepalLength": 5.9,
Exemple #25
0
from dffml import CSVSource, Features, Feature
from dffml.noasync import train, accuracy, predict
from dffml_model_scratch.logisticregression import LogisticRegression

model = LogisticRegression(
    features=Features(Feature("f1", float, 1)),
    predict=Feature("ans", int, 1),
)

# Train the model
train(model, "dataset.csv")

# Assess accuracy (alternate way of specifying data source)
print("Accuracy:", accuracy(model, CSVSource(filename="dataset.csv")))

# Make prediction
for i, features, prediction in predict(model, {"f1": 0.8, "ans": 0}):
    features["ans"] = prediction["ans"]["value"]
    print(features)
Exemple #26
0
    async def test_scorer(self):
        with tempfile.TemporaryDirectory() as tempdir:
            model = SLRModel(
                features=Features(Feature("f1", float, 1)),
                predict=Feature("ans", int, 1),
                location=tempdir,
            )
            # y = m * x + b for equation SLR is solving for
            m = 5
            b = 3

            # Train the model
            await train(model, *[{
                "f1": x,
                "ans": m * x + b
            } for x in range(0, 10)])

            source = JSONSource(
                filename=pathlib.Path(tempdir, "source.json"),
                allowempty=True,
                readwrite=True,
            )

            # Record the source will have in it
            await save(
                source,
                *[
                    Record(
                        str(i),
                        data={"features": {
                            "f1": x,
                            "ans": (m * x) + b
                        }},
                    ) for i, x in enumerate(range(10, 20))
                ],
            )

            async with ServerRunner.patch(HTTPService.server) as tserver:
                cli = await tserver.start(
                    HTTPService.server.cli(
                        "-insecure",
                        "-port",
                        "0",
                        "-models",
                        "mymodel=slr",
                        "-model-mymodel-location",
                        tempdir,
                        "-model-mymodel-features",
                        "f1:float:1",
                        "-model-mymodel-predict",
                        "ans:int:1",
                        "-features",
                        "ans:int:1",
                        "-sources",
                        "mysource=json",
                        "-source-mysource-filename",
                        str(source.config.filename),
                        "-scorers",
                        "myscorer=mse",
                    ))
                async with self.post(cli,
                                     "/scorer/myscorer/mymodel/score",
                                     json=["mysource"]) as r:
                    self.assertEqual(await r.json(), {"accuracy": 0.0})
Exemple #27
0
from dffml import Feature, Features
from dffml.noasync import train, accuracy, predict

from REPLACE_IMPORT_PACKAGE_NAME.myslr import MySLRModel

model = MySLRModel(
    features=Features(Feature("x", float, 1)),
    predict=Feature("y", int, 1),
    directory="tempdir",
)

# Train the model
train(model, "train.csv")

# Assess accuracy (alternate way of specifying data source)
print("Accuracy:", accuracy(model, "test.csv"))

# Make prediction
for i, features, prediction in predict(model, "predict.csv"):
    features["y"] = prediction["y"]["value"]
    print(features)
Exemple #28
0
from dffml import Features, Feature
from dffml.noasync import train, accuracy, predict
from dffml_model_autosklearn import AutoSklearnRegressorModel

model = AutoSklearnRegressorModel(
    features=Features(
        Feature("Feature1", float, 1), Feature("Feature2", float, 1),
    ),
    predict=Feature("TARGET", float, 1),
    directory="tempdir-python",
    time_left_for_this_task=120,
)


def main():
    # Train the model
    train(model, "train.csv")

    # Assess accuracy
    print("Accuracy:", accuracy(model, "test.csv"))

    # Make prediction
    for i, features, prediction in predict(model, "predict.csv"):
        features["TARGET"] = prediction["TARGET"]["value"]
        print(features)


if __name__ == "__main__":
    main()