Bayesian networks using Encog Java
Code: "neural_net_bayesian.java". Programming language: Java DMelt Version 1. Last modified: 10/25/2015. License: Pro
https://datamelt.org/code/cache/neural_net_bayesian_7133.java
To run this script using the DMelt IDE, copy the above URL link to the menu [File]→[Read script from URL] of the DMelt IDE.



/*
 * Encog(tm) Java Examples v3.3
 * http://www.heatonresearch.com/encog/
 * https://github.com/encog/encog-java-examples
 *
 * Copyright 2008-2014 Heaton Research, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *   
 * For more information on Heaton Research copyrights, licenses 
 * and trademarks visit:
 * http://www.heatonresearch.com/copyright
 */

import org.encog.ml.bayesian.BayesianEvent;
import org.encog.ml.bayesian.BayesianNetwork;
import org.encog.ml.bayesian.EventType;
import org.encog.ml.bayesian.query.enumerate.EnumerationQuery;
import org.encog.ml.bayesian.query.sample.SamplingQuery;

public class neural_net_bayesian  {
	public static void main(String[] args) {
		
		// build the bayesian network structure
		BayesianNetwork network = new BayesianNetwork();
		BayesianEvent rained = network.createEvent("rained");
		BayesianEvent evenTemperatures = network.createEvent("temperature");
		BayesianEvent gardenGrew = network.createEvent("gardenGrew");
		BayesianEvent plentyOfCarrots = network.createEvent("carrots");
		BayesianEvent plentyOfTomatoes = network.createEvent("Tomatoes");
		network.createDependency(rained, gardenGrew);
		network.createDependency(evenTemperatures, gardenGrew);
		network.createDependency(gardenGrew, plentyOfCarrots);
		network.createDependency(gardenGrew, plentyOfTomatoes);
		network.finalizeStructure();
		
		// build the truth tales
		rained.getTable().addLine(0.2, true);
		evenTemperatures.getTable().addLine(0.5, true);
		gardenGrew.getTable().addLine(0.9, true, true, true);
		gardenGrew.getTable().addLine(0.7, true, false, true);
		gardenGrew.getTable().addLine(0.5, true, true, false);
		gardenGrew.getTable().addLine(0.1, true, false, false);
		plentyOfCarrots.getTable().addLine(0.8, true, true);
		plentyOfCarrots.getTable().addLine(0.2, true, false);
		plentyOfTomatoes.getTable().addLine(0.6, true, true);
		plentyOfTomatoes.getTable().addLine(0.1, true, false);
		
		// validate the network
		network.validate();
		
		// display basic stats
		System.out.println(network.toString());
		System.out.println("Parameter count: " + network.calculateParameterCount());
		
		EnumerationQuery query = new EnumerationQuery(network);
		//SamplingQuery query = new SamplingQuery(network);
		query.defineEventType(rained, EventType.Evidence);
		query.defineEventType(evenTemperatures, EventType.Evidence);
		query.defineEventType(plentyOfCarrots, EventType.Outcome);
		query.setEventValue(rained, true);
		query.setEventValue(evenTemperatures, true);
		query.setEventValue(plentyOfCarrots, true);
		query.execute();

		System.out.println("\n\n" + query.toString());
	}
}

Ads help maintain this website.