Beispiel #1
0
from dffml import Features, Feature, SLRModel
from dffml.noasync import train, accuracy, predict

model = SLRModel(
    features=Features(Feature("f1", float, 1)),
    predict=Feature("ans", int, 1),
    directory="tempdir",
)

# 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)
Beispiel #2
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),
    location="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(
Beispiel #3
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,
)

# 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)
Beispiel #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),
                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)])

            await score(
                model,
                MeanSquaredErrorAccuracy(),
                Feature("ans", int, 1),
                *[{
                    "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-location",
                        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)
Beispiel #5
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})
Beispiel #6
0
from dffml import 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),
)

# Train the model
train(
    model,
    {
        "Years": 0,
        "Expertise": 1,
        "Trust": 0.1,
        "Salary": 10
    },
    {
        "Years": 1,
        "Expertise": 3,
        "Trust": 0.2,
        "Salary": 20
    },
    {
        "Years": 2,
        "Expertise": 5,
Beispiel #7
0
from dffml import Features, Feature, SLRModel
from dffml.noasync import score, train
from dffml.accuracy import MeanSquaredErrorAccuracy

model = SLRModel(
    features=Features(Feature("f1", float, 1)),
    predict=Feature("ans", int, 1),
    location="tempdir",
)

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

# Choose the accuracy plugin
mse_accuracy = MeanSquaredErrorAccuracy()

# Assess accuracy (alternate way of specifying data source)
print(
    "Accuracy:",
    score(model, mse_accuracy, Feature("ans", int, 1), "dataset.csv"),
)
Beispiel #8
0
from dffml import CSVSource, Feature
from dffml.noasync import train, accuracy, predict
from dffml_model_transformers.ner.ner_model import NERModel

model = NERModel(
    sid=Feature("SentenceId", int, 1),
    words=Feature("Words", str, 1),
    predict=Feature("Tag", str, 1),
    model_name_or_path="distilbert-base-cased",
    epochs=1,
    no_cuda=True,
)

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

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

# Make prediction
for i, features, prediction in predict(
        model,
    {
        "SentenceID": 1,
        "Words": "DFFML models can do NER"
    },
    {
        "SentenceID": 2,
        "Words": "DFFML models can do regression"
    },
):
Beispiel #9
0
from dffml import CSVSource, Features, Feature
from dffml.noasync import train, accuracy, predict
from dffml_model_tensorflow.dnnr import DNNRegressionModel

model = DNNRegressionModel(
    features=Features(Feature("Feature1", float, 1),
                      Feature("Feature2", float, 1)),
    predict=Feature("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)
Beispiel #10
0
from dffml import CSVSource, Features, Feature
from dffml.noasync import train, score, predict
from dffml_model_daal4py.daal4pylr import DAAL4PyLRModel
from dffml.accuracy import MeanSquaredErrorAccuracy

model = DAAL4PyLRModel(
    features=Features(Feature("f1", float, 1)),
    predict=Feature("ans", int, 1),
    location="tempdir",
)

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

# Assess accuracy (alternate way of specifying data source)
scorer = MeanSquaredErrorAccuracy()
print(
    "Accuracy:",
    score(model, scorer, Feature("ans", int, 1),
          CSVSource(filename="test.csv")),
)

# Make prediction
for i, features, prediction in predict(model, {"f1": 0.8, "ans": 0}):
    features["ans"] = prediction["ans"]["value"]
    print(features)
Beispiel #11
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),
        directory="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
    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)
Beispiel #12
0
from dffml import Features, Feature, SLRModel
from dffml.noasync import train, score, predict
from dffml.accuracy import MeanSquaredErrorAccuracy

model = SLRModel(
    features=Features(Feature("f1", float, 1)),
    predict=Feature("ans", int, 1),
    location="tempdir",
)

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

# Assess accuracy (alternate way of specifying data source)
scorer = MeanSquaredErrorAccuracy()
print("Accuracy:", score(model, scorer, Feature("ans", int, 1), "dataset.csv"))

# Make prediction
for i, features, prediction in predict(model, {"f1": 0.8, "ans": 0}):
    features["ans"] = prediction["ans"]["value"]
    print(features)