Exemple #1
0
    public override bool Read(GH_IReader reader)
    {
        var       nn = new BinaryFormatter();
        var       f  = new List <Owl.Learning.NeuronFunctions.NeuronFunctionBase>();
        int       fc = reader.GetInt32("FunctionCount");
        TensorSet w  = default;
        TensorSet b  = default;

        for (int i = 0, loopTo = fc - 1; i <= loopTo; i += 1)
        {
            using (var mstr = new MemoryStream(reader.GetByteArray("Function_" + i)))
            {
                f.Add(nn.Deserialize(mstr));
            }
        }

        using (var mstr = new MemoryStream(reader.GetByteArray("Biases")))
        {
            b = Owl.Core.IO.ReadTensors(mstr);
        }

        using (var mstr = new MemoryStream(reader.GetByteArray("Weigths")))
        {
            w = Owl.Core.IO.ReadTensors(mstr);
        }

        this.Value = new Owl.Learning.Networks.Network(w, b, f);
        return(true);
    }
    protected override void SolveInstance(IGH_DataAccess DA)
    {
        TensorSet  w = new TensorSet();
        TensorSet  b = new TensorSet();
        List <int> f = new List <int>();

        if (!DA.GetData(0, ref w))
        {
            return;
        }
        if (!DA.GetData(1, ref b))
        {
            return;
        }
        if (!DA.GetDataList(2, f))
        {
            return;
        }

        List <NeuronFunctionBase> fs = new List <NeuronFunctionBase>();

        foreach (int val in f)
        {
            switch (val)
            {
            case 0:
            {
                fs.Add(new Linear());
                break;
            }

            case 1:
            {
                fs.Add(new Relu());
                break;
            }

            case 2:
            {
                fs.Add(new Sigmoid());
                break;
            }

            case 3:
            {
                fs.Add(new Tanh());
                break;
            }
            }
        }

        Network nn = new Network(w, b, fs);

        DA.SetData(0, new GH_OwlNetwork(nn));
    }
Exemple #3
0
    protected override void SolveInstance(IGH_DataAccess DA)
    {
        GH_OwlNetwork   nn  = null /* TODO Change to default(_) if this is not a reference type */;
        GH_OwlTensorSet ins = new GH_OwlTensorSet();

        if (!DA.GetData(0, ref nn))
        {
            return;
        }
        if (!DA.GetData(1, ref ins))
        {
            return;
        }

        TensorSet outs = nn.Value.ComputeOptimized(ins.Value);

        // For i As Integer = 0 To ins.Value.Count - 1 Step 1
        // outs.Add(nn.Value.Compute(ins.Value(i)))
        // Next

        DA.SetData(0, outs);
    }
Exemple #4
0
    public override bool CastFrom(object source)
    {
        var switchExpr = source.GetType();

        switch (switchExpr)
        {
        case var @case when @case == typeof(Tensor):
        {
            this.Value = (Tensor)source;
            return(true);
        }

        case var case1 when case1 == typeof(TensorSet):
        {
            TensorSet ts = (TensorSet)source;
            if (ts.Count != 1)
            {
                return(false);
            }
            this.Value = ts[0];
            return(true);
        }

        case var case2 when case2 == typeof(Point3d):
        {
            Point3d asp = source;
            this.Value = new Tensor(new[] { asp.X, asp.Y, asp.Z });
            return(true);
        }

        case var case3 when case3 == typeof(GH_Point):
        {
            GH_Point asp = source;
            this.Value = new Tensor(new[] { asp.Value.X, asp.Value.Y, asp.Value.Z });
            return(true);
        }

        case var case4 when case4 == typeof(GH_Vector):
        {
            GH_Vector asp = source;
            this.Value = new Tensor(new[] { asp.Value.X, asp.Value.Y, asp.Value.Z });
            return(true);
        }

        case var case5 when case5 == typeof(Vector3d):
        {
            Vector3d asp = source;
            this.Value = new Tensor(new[] { asp.X, asp.Y, asp.Z });
            return(true);
        }

        case var case6 when case6 == typeof(Color):
        {
            Color asp = (Color)source;
            this.Value = new Tensor(new[] { Convert.ToDouble(asp.A), Convert.ToDouble(asp.R), Convert.ToDouble(asp.G), Convert.ToDouble(asp.B) });
            return(true);
        }

        case var case7 when case7 == typeof(GH_Colour):
        {
            GH_Colour asp = source;
            this.Value = new Tensor(new[] { Convert.ToDouble(asp.Value.A), Convert.ToDouble(asp.Value.R), Convert.ToDouble(asp.Value.G), Convert.ToDouble(asp.Value.B) });
            return(true);
        }

        case var case8 when case8 == typeof(Line):
        {
            Line asp = source;
            this.Value = new Tensor(new[] { asp.From.X, asp.From.Y, asp.From.Z, asp.To.X, asp.To.Y, asp.To.Z });
            return(true);
        }

        case var case9 when case9 == typeof(GH_Line):
        {
            GH_Line thisv = source;
            Line    asp   = thisv.Value;
            this.Value = new Tensor(new[] { asp.From.X, asp.From.Y, asp.From.Z, asp.To.X, asp.To.Y, asp.To.Z });
            return(true);
        }
        }

        return(false);
    }