/// <summary>
    /// Build neural network based on the lsit of layer defs.
    /// </summary>
    /// <param name="layerDefs">layer definitions</param>
    /// <param name="input">input tensor</param>
    /// <param name="scope">name scope</param>
    /// <returns>value tuple of (output tensor, list of weights)</returns>
    public static ValueTuple <Tensor, List <Tensor> > BuildSequentialLayers(List <SimpleDenseLayerDef> layerDefs, Tensor input, string scope = null)
    {
        NameScope nameScppe = null;

        if (!string.IsNullOrEmpty(scope))
        {
            nameScppe = Current.K.name_scope(scope);
        }

        List <Tensor> weights = new List <Tensor>();
        Tensor        temp    = input;

        foreach (var l in layerDefs)
        {
            var result = l.Call(temp);
            temp = result.Item1;
            weights.AddRange(result.Item2);
        }

        if (nameScppe != null)
        {
            nameScppe.Dispose();
        }
        return(ValueTuple.Create(temp, weights));
    }
Пример #2
0
    /// <summary>
    /// Trainers will call this method to initialize the model. This method will call the InitializeInner()
    /// </summary>
    /// <param name="brainParameters">brain parameter of the MLagent brain</param>
    /// <param name="enableTraining">whether enable training</param>
    /// <param name="trainerParams">trainer parameters passed by the trainer. Training will not be enabled </param>
    public virtual void Initialize(BrainParameters brainParameters, bool enableTraining, TrainerParams trainerParams = null)
    {
        Debug.Assert(Initialized == false, "Model already Initalized");

        NameScope ns = null;

        if (!string.IsNullOrEmpty(modelName))
        {
            ns = Current.K.name_scope(modelName);
        }

        ActionSizes = brainParameters.vectorActionSize;
        StateSize   = brainParameters.vectorObservationSize * brainParameters.numStackedVectorObservations;
        ActionSpace = brainParameters.vectorActionSpaceType;

        Debug.Assert(ActionSizes[0] > 0, "Action size can not be zero");

        //create basic inputs
        var inputStateTensor = StateSize > 0 ? UnityTFUtils.Input(new int?[] { StateSize }, name: "InputStates")[0] : null;

        HasVectorObservation = inputStateTensor != null;
        var inputVisualTensors = CreateVisualInputs(brainParameters);

        HasVisualObservation = inputVisualTensors != null;

        //create inner intialization
        InitializeInner(brainParameters, inputStateTensor, inputVisualTensors, enableTraining ? trainerParams : null);

        //test
        //Debug.LogWarning("Tensorflow Graph is saved for test purpose at: SavedGraph/" + name + ".pb");
        //((UnityTFBackend)Current.K).ExportGraphDef("SavedGraph/" + name + ".pb");

        Current.K.try_initialize_variables(true);

        if (ns != null)
        {
            ns.Dispose();
        }

        if (checkpointToLoad != null)
        {
            RestoreCheckpoint(checkpointToLoad.bytes, true);
        }
        Initialized     = true;
        TrainingEnabled = enableTraining;
    }