Ejemplo n.º 1
0
def test_from_csv(tmpdir):
    train_csv = Path(tmpdir) / "train.csv"
    val_csv = test_csv = Path(tmpdir) / "valid.csv"
    TEST_DF_1.to_csv(train_csv)
    TEST_DF_2.to_csv(val_csv)
    TEST_DF_2.to_csv(test_csv)

    dm = TabularData.from_csv(categorical_fields=["category"],
                              numerical_fields=["scalar_a", "scalar_b"],
                              target_fields="label",
                              train_file=str(train_csv),
                              val_file=str(val_csv),
                              test_file=str(test_csv),
                              num_workers=0,
                              batch_size=1)
    for dl in [
            dm.train_dataloader(),
            dm.val_dataloader(),
            dm.test_dataloader()
    ]:
        data = next(iter(dl))
        (cat, num) = data[DefaultDataKeys.INPUT]
        target = data[DefaultDataKeys.TARGET]
        assert cat.shape == (1, 1)
        assert num.shape == (1, 2)
        assert target.shape == (1, )
Ejemplo n.º 2
0
def test_from_csv(tmpdir):
    train_csv = Path(tmpdir) / "train.csv"
    val_csv = test_csv = Path(tmpdir) / "valid.csv"
    TEST_DF_1.to_csv(train_csv)
    TEST_DF_2.to_csv(val_csv)
    TEST_DF_2.to_csv(test_csv)

    dm = TabularData.from_csv(train_csv=train_csv,
                              categorical_cols=["category"],
                              numerical_cols=["scalar_b", "scalar_b"],
                              target_col="label",
                              val_csv=val_csv,
                              test_csv=test_csv,
                              num_workers=0,
                              batch_size=1)
    for dl in [
            dm.train_dataloader(),
            dm.val_dataloader(),
            dm.test_dataloader()
    ]:
        (cat, num), target = next(iter(dl))
        assert cat.shape == (1, 1)
        assert num.shape == (1, 2)
        assert target.shape == (1, )
# See the License for the specific language governing permissions and
# limitations under the License.
from torchmetrics.classification import Accuracy, Precision, Recall

import flash
from flash.data.utils import download_data
from flash.tabular import TabularClassifier, TabularData

# 1. Download the data
download_data("https://pl-flash-data.s3.amazonaws.com/titanic.zip", "data/")

# 2. Load the data
datamodule = TabularData.from_csv(
    ["Sex", "Age", "SibSp", "Parch", "Ticket", "Cabin", "Embarked"],
    ["Fare"],
    target_field="Survived",
    train_file="./data/titanic/titanic.csv",
    test_file="./data/titanic/test.csv",
    val_split=0.25,
)

# 3. Build the model
model = TabularClassifier.from_data(
    datamodule, metrics=[Accuracy(), Precision(),
                         Recall()])

# 4. Create the trainer
trainer = flash.Trainer(fast_dev_run=True)

# 5. Train the model
trainer.fit(model, datamodule=datamodule)
# limitations under the License.
from torchmetrics.classification import Accuracy, Precision, Recall

import flash
from flash.data.utils import download_data
from flash.tabular import TabularClassifier, TabularData

# 1. Download the data
download_data("https://pl-flash-data.s3.amazonaws.com/titanic.zip", "data/")

# 2. Load the data
datamodule = TabularData.from_csv(
    target_col="Survived",
    train_csv="./data/titanic/titanic.csv",
    test_csv="./data/titanic/test.csv",
    categorical_cols=[
        "Sex", "Age", "SibSp", "Parch", "Ticket", "Cabin", "Embarked"
    ],
    numerical_cols=["Fare"],
    val_size=0.25,
)

# 3. Build the model
model = TabularClassifier.from_data(
    datamodule, metrics=[Accuracy(), Precision(),
                         Recall()])

# 4. Create the trainer
trainer = flash.Trainer(fast_dev_run=True)

# 5. Train the model
trainer.fit(model, datamodule=datamodule)