Example #1
0
        /// <summary>
        /// Trains the model with a single minibatch of data
        /// </summary>
        /// <param name="features">Features for the batch</param>
        /// <param name="targets">Targets for the batch</param>
        public void TrainMinibatch(Dictionary <Input, Array> features, Dictionary <Layer, Array> targets)
        {
            if (_graph == null)
            {
                throw new InvalidOperationException("The model must be compiled first before you can train it.");
            }

            if (!features.Select(x => _inputMapping.ContainsKey(x.Key)).All(x => x))
            {
                throw new ArgumentException("Not all inputs have a value provided for them. Please make sure that you provide data for each of the inputs of the model.");
            }

            if (!targets.Select(x => _placeholderMapping.ContainsKey(x.Key)).All(x => x))
            {
                throw new ArgumentException("Not all targets have a value provided for them. Please make sure that you provide data for each of the targets of the model.");
            }

            EnsureSession();

            var modelInputs = new Dictionary <TFOutput, Array>();

            foreach (var keyValuePair in features)
            {
                modelInputs.Add(_inputMapping[keyValuePair.Key], keyValuePair.Value);
            }

            foreach (var keyValuePair in targets)
            {
                modelInputs.Add(_placeholderMapping[keyValuePair.Key], keyValuePair.Value);
            }

            _optimizer.Execute(_session, modelInputs);
        }