View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.math4.neuralnet;
19  
20  import org.junit.Test;
21  import org.junit.Assert;
22  
23  /**
24   * Tests for {@link Neuron}.
25   */
26  public class NeuronTest {
27      @Test
28      public void testGetIdentifier() {
29          final long id = 1234567;
30          final Neuron n = new Neuron(id, new double[] {0 });
31  
32          Assert.assertEquals(id, n.getIdentifier());
33      }
34  
35      @Test
36      public void testGetSize() {
37          final double[] features = {-1, -1e-97, 0, 23.456, 9.01e203};
38          final Neuron n = new Neuron(1, features);
39          Assert.assertEquals(features.length, n.getSize());
40      }
41  
42      @Test
43      public void testGetFeatures() {
44          final double[] features = {-1, -1e-97, 0, 23.456, 9.01e203};
45          final Neuron n = new Neuron(1, features);
46  
47          final double[] f = n.getFeatures();
48          // Accessor returns a copy.
49          Assert.assertFalse(f == features);
50  
51          // Values are the same.
52          Assert.assertEquals(features.length, f.length);
53          for (int i = 0; i < features.length; i++) {
54              Assert.assertEquals(features[i], f[i], 0d);
55          }
56      }
57  
58      @Test
59      public void testCompareAndSetFeatures() {
60          final Neuron n = new Neuron(1, new double[] {0 });
61          double[] expect = n.getFeatures();
62          double[] update = new double[] {expect[0] + 1.23};
63  
64          // Test "success".
65          boolean ok = n.compareAndSetFeatures(expect, update);
66          // Check that the update is reported as successful.
67          Assert.assertTrue(ok);
68          // Check that the new value is correct.
69          Assert.assertEquals(update[0],  n.getFeatures()[0], 0d);
70  
71          // Test "failure".
72          double[] update1 = new double[] {update[0] + 4.56};
73          // Must return "false" because the neuron has been
74          // updated: a new update can only succeed if "expect"
75          // is set to the new features.
76          ok = n.compareAndSetFeatures(expect, update1);
77          // Check that the update is reported as failed.
78          Assert.assertFalse(ok);
79          // Check that the value was not changed.
80          Assert.assertEquals(update[0],  n.getFeatures()[0], 0d);
81      }
82  
83      @Test
84      public void testCopy() {
85          final Neuron n = new Neuron(1, new double[] {9.87 });
86  
87          // Update original.
88          double[] update = new double[] {n.getFeatures()[0] + 2.34};
89          n.compareAndSetFeatures(n.getFeatures(), update);
90  
91          // Create a copy.
92          final Neuron copy = n.copy();
93  
94          // Check that original and copy have the same value.
95          Assert.assertTrue(n.getFeatures()[0] == copy.getFeatures()[0]);
96          Assert.assertEquals(n.getNumberOfAttemptedUpdates(),
97                              copy.getNumberOfAttemptedUpdates());
98  
99          // Update original.
100         update = new double[] {1.23 * n.getFeatures()[0]};
101         n.compareAndSetFeatures(n.getFeatures(), update);
102 
103         // Check that original and copy differ.
104         Assert.assertFalse(n.getFeatures()[0] == copy.getFeatures()[0]);
105         Assert.assertNotEquals(n.getNumberOfSuccessfulUpdates(),
106                                copy.getNumberOfSuccessfulUpdates());
107     }
108 }