How to use Inherited Types in ML.Net Pipeline

Kevin Monaghan 6 Reputation points
2023-11-07T04:39:27.5633333+00:00

Is there a good way to use inherited types as data for ML pipeline?

I have tried reflection and many other methods but somewhere between the LoadFromEnumerable method and the training of the pipeline, the pipeline will not be able to see properties of the concrete class.

I have tried inheriting from a base class, and from an interface, and from a base class that implements and interface.

when i remove the inheritance my training method works but when I inherit an exception is thrown "Cannot find xxyz property in pipeline"

my feature columns are directly taken from the concrete type instance using a method where the type is passed in at run time from a sample of the data.

When I print the feature columns they are all there, but when I progress to training the training the columns are not found.

Really all I need is a simple way to pass many different kinds of 'BaseFood' to my pipelines, I can get it working with a simple switch statement and by passing only classes that do not inherit but I am curious if there is a fix that would allow me to dynamically pass in types that use inheritance so that I do not need to have many lists or dictionaries for each type in the rest of my code.

just to keep it simple I will give an outline rather than all the code.

//(BaseFood): implements the features that are commonly used by all food types

//(Food1 : BaseFood): has its own specific properties.

//(Food 2: BaseFood): has its own specific properties.

TrainingMethod(BaseFood basefood)

{

  var concreteType = baseFood[0].GetType();

//This method returns the string[0] of feature columns for the pipeline

 var featureColumnNames = FeatureColumnFiltering.GetColumnNames(concreteType);

//The concrete column names print correctly from the returning string[]

//split data sets etc

//proceed to train pipeline and this is where error is thrown as the pipeline only recognises BaseFood properties and not Food1 or Food2 even if they have been cast, converted or reflected. so Food1 or Food2 "Property xyx Could not be found"

}

Everything I have learned is trial and error to be honest, so I am open to using specific design principle or approach that I may not have thought of, if someone could be kind enough to point it out.

.NET Machine learning
.NET Machine learning
.NET: Microsoft Technologies based on the .NET software framework.Machine learning: A type of artificial intelligence focused on enabling computers to use observed data to evolve new behaviors that have not been explicitly programmed.
150 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Kevin Monaghan 6 Reputation points
    2023-11-07T06:50:53.3433333+00:00

    I think I might be able to do something like this

    namespace MachineLearning.Foods{    public class FoodStash    {        private Symbol Symbol { get; set; }        private Type Type { get; set; }        private Dictionary<int, dynamic> Active { get; } = new();        private Dictionary<int, dynamic> Completed { get; } = new();        public FoodStash(Symbol symbol, Type type)        {            this.Symbol = symbol;            this.Type = type;        }        public dynamic GetOrCreate(string bias, decimal entryPrice, int tradeId)        {            if (!Active.TryGetValue(tradeId, out var food))            {                
    
    
    public static Dictionary<Symbol, Dictionary<Type, FoodStash>> FoodStashes = new();
    
    
    0 comments No comments

  2. Kevin Monaghan 6 Reputation points
    2023-11-07T06:57:07.4433333+00:00

    nm, this website just does not allow properly formatted code to be added

    0 comments No comments