예제 #1
0
  /**
   * Feeds the {@link Network with the final phrase consisting of the first
   * two words of the final question "fox, eats, ...".
   *
   * @param network   the current {@link Network} object
   * @param it        an {@link Iterator} over the input source file lines
   * @return
   */
  Term feedQuestion(Network network, String[] phrase) {
    for (int i = 0; i < 2; i++) {
      int[] sdr = getFingerprintSDR(phrase[i]);
      network.compute(sdr);
    }
    int[] sdr = new int[128 * 128];
    Arrays.fill(sdr, 1);

    Layer<?> layer = network.lookup("Region 1").lookup("Layer 2/3");
    Set<Cell> predictiveCells = layer.getConnections().getPredictiveCells();
    System.out.println("predictiveCells " + predictiveCells.toString());
    int[] prediction =
        SDR.cellsAsColumnIndices(predictiveCells, layer.getConnections().getCellsPerColumn());

    network.compute(sdr);
    Set<Cell> ffActive = layer.getActiveCells();
    System.out.println("activeCells " + ffActive.toString());
    int[] paPrediction =
        SDR.cellsAsColumnIndices(ffActive, layer.getConnections().getCellsPerColumn());

    Term term = getClosestTerm(prediction);
    Term paterm = getClosestTerm(paPrediction);

    System.out.println(paterm.toString());

    cache.put(term.getTerm(), term);

    return term;
  }
예제 #2
0
  ///////////////////////////////////////////
  //             HELPER METHODS            //
  ///////////////////////////////////////////
  private Network getLoadedDayOfWeekNetwork() {
    Parameters p = NetworkTestHarness.getParameters().copy();
    p = p.union(NetworkTestHarness.getDayDemoTestEncoderParams());
    p.set(KEY.RANDOM, new FastRandom(42));

    Sensor<ObservableSensor<String[]>> sensor =
        Sensor.create(
            ObservableSensor::create,
            SensorParams.create(
                Keys::obs,
                new Object[] {
                  "name",
                  PublisherSupplier.builder()
                      .addHeader("dayOfWeek")
                      .addHeader("number")
                      .addHeader("B")
                      .build()
                }));

    Network network =
        Network.create("test network", p)
            .add(
                Network.createRegion("r1")
                    .add(
                        Network.createLayer("1", p)
                            .alterParameter(KEY.AUTO_CLASSIFY, true)
                            .add(Anomaly.create())
                            .add(new TemporalMemory())
                            .add(new SpatialPooler())
                            .add(sensor)));

    return network;
  }
예제 #3
0
  /**
   * Feeds a single array of phrase words into the {@link Network}.
   *
   * @param network
   * @param phrase
   */
  void feedLine(Network network, String[] phrase) {
    for (String term : phrase) {
      int[] sdr = getFingerprintSDR(term);
      // System.out.println(String.format("SDR for '%s'\t%d",term,sdr.length));
      network.compute(sdr);
    }

    network.reset();
  }
  /**
   * Creates and returns a {@link Network} for demo processing
   *
   * @return
   */
  Network createNetwork() {
    org.numenta.nupic.Parameters temporalParams = createParameters();
    PALayer<?> l = new PALayer("Layer 2/3", null, temporalParams);
    l.setPADepolarize(0.0); // 0.25
    l.setVerbosity(1);
    PASpatialPooler sp = new PASpatialPooler();

    network =
        Network.create("Cortical.io API Demo", temporalParams)
            .add(Network.createRegion("Region 1").add(l.add(new TemporalMemory()).add(sp)));
    return network;
  }
  /**
   * Feeds the {@link Network with the final phrase consisting of the first
   * two words of the final question "fox, eats, ...".
   *
   * @param network   the current {@link Network} object
   * @param it        an {@link Iterator} over the input source file lines
   * @return
   */
  Term feedQuestion(Network network, String[] phrase) {
    for (int i = 0; i < 2; i++) {
      int[] sdr = getFingerprintSDR(phrase[i]);
      network.compute(sdr);
    }

    Layer<?> layer = network.lookup("Region 1").lookup("Layer 2/3");
    Set<Cell> predictiveCells = layer.getPredictiveCells();
    int[] prediction =
        SDR.cellsAsColumnIndices(predictiveCells, layer.getConnections().getCellsPerColumn());
    Term term = getClosestTerm(prediction);
    cache.put(term.getTerm(), term);

    return term;
  }
예제 #6
0
  @Ignore
  public void testPlayground() {
    final int NUM_CYCLES = 600;
    final int INPUT_GROUP_COUNT = 7; // Days of Week

    ///////////////////////////////////////
    //          Load a Network           //
    ///////////////////////////////////////
    Network network = getLoadedDayOfWeekNetwork();

    int cellsPerCol = (int) network.getParameters().get(KEY.CELLS_PER_COLUMN);

    network
        .observe()
        .subscribe(
            new Observer<Inference>() {
              @Override
              public void onCompleted() {}

              @Override
              public void onError(Throwable e) {
                e.printStackTrace();
              }

              @SuppressWarnings("unused")
              @Override
              public void onNext(Inference inf) {
                /** see {@link #createDayOfWeekInferencePrintout()} */
                int cycle = dayOfWeekPrintout.apply(inf, cellsPerCol);
              }
            });

    Publisher pub = network.getPublisher();

    network.start();

    int cycleCount = 0;
    for (; cycleCount < NUM_CYCLES; cycleCount++) {
      for (double j = 0; j < INPUT_GROUP_COUNT; j++) {
        pub.onNext("" + j);
      }

      network.reset();
    }

    // Test network output
    try {
      Region r1 = network.lookup("r1");
      r1.lookup("1").getLayerThread().join(2000);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
예제 #7
0
파일: Network.java 프로젝트: sawie/htm.java
 /**
  * Creates a {@link Layer} to hold algorithmic components and returns it.
  *
  * @param name the String identifier for the specified {@link Layer}
  * @param p the {@link Parameters} to use for the specified {@link Layer}
  * @return
  */
 @SuppressWarnings("rawtypes")
 public static Layer<?> createLayer(String name, Parameters p) {
   Network.checkName(name);
   return new Layer(name, null, p);
 }
예제 #8
0
파일: Network.java 프로젝트: sawie/htm.java
  /**
   * Creates and returns a child {@link Region} of this {@code Network}
   *
   * @param name The String identifier for the specified {@link Region}
   * @return
   */
  public static Region createRegion(String name) {
    Network.checkName(name);

    Region r = new Region(name, null);
    return r;
  }