             // values with the new errors
             __syncthreads();
             
-            for (int x = 0;  x < N;  ++x)
+            for (int x = 0;  x < N && x < valid_examples;  ++x)
                 scratch[x * scratch_stride + tid] = total[x];
         }
 

        //float * d = (scratch + N * scratch_stride);
        float d[N];

        for (int x = 0;  x < N;  ++x) {
            float prev_output
                = (tid >= no
                   ? 0.0

            // A single thread per entry; no synchronization
            float bias = layer_biases[o];

            for (int x = 0;  x < N && x < valid_examples;  ++x) {
                accum[x] += bias;
                this_layer_outputs[x * total_neurons + o]
                    = scratch[x * scratch_stride + o]
