Beispiel #1
0
        void run_optimization(OptimizerV2 optimizer, Tensor x, Tensor y)
        {
            using var g = tf.GradientTape();
            var pred = conv_net(x);
            var loss = cross_entropy(pred, y);

            // Compute gradients.
            var trainable_variables = new IVariableV1[] { wc1, wc2, wd1, wout, bc1, bc2, bd1, bout };
            var gradients           = g.gradient(loss, trainable_variables);

            // Update W and b following gradients.
            optimizer.apply_gradients(zip(gradients, trainable_variables.Select(x => x as ResourceVariable)));
        }