Return-Path: X-Original-To: apmail-horn-dev-archive@minotaur.apache.org Delivered-To: apmail-horn-dev-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 247421728D for ; Tue, 17 Nov 2015 08:22:48 +0000 (UTC) Received: (qmail 87449 invoked by uid 500); 17 Nov 2015 08:22:48 -0000 Delivered-To: apmail-horn-dev-archive@horn.apache.org Received: (qmail 87413 invoked by uid 500); 17 Nov 2015 08:22:48 -0000 Mailing-List: contact dev-help@horn.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@horn.incubator.apache.org Delivered-To: mailing list dev@horn.incubator.apache.org Received: (qmail 87402 invoked by uid 99); 17 Nov 2015 08:22:48 -0000 Received: from Unknown (HELO spamd3-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 17 Nov 2015 08:22:48 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd3-us-west.apache.org (ASF Mail Server at spamd3-us-west.apache.org) with ESMTP id 9A48A180A63 for ; Tue, 17 Nov 2015 08:22:47 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd3-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 1.77 X-Spam-Level: * X-Spam-Status: No, score=1.77 tagged_above=-999 required=6.31 tests=[KAM_ASCII_DIVIDERS=0.8, KAM_LAZY_DOMAIN_SECURITY=1, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, T_RP_MATCHES_RCVD=-0.01] autolearn=disabled Received: from mx1-eu-west.apache.org ([10.40.0.8]) by localhost (spamd3-us-west.apache.org [10.40.0.10]) (amavisd-new, port 10024) with ESMTP id h7MhKi_7eqTG for ; Tue, 17 Nov 2015 08:22:45 +0000 (UTC) Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx1-eu-west.apache.org (ASF Mail Server at mx1-eu-west.apache.org) with SMTP id 28BC221270 for ; Tue, 17 Nov 2015 08:22:44 +0000 (UTC) Received: (qmail 87313 invoked by uid 99); 17 Nov 2015 08:22:43 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 17 Nov 2015 08:22:43 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 511B2DFE2E; Tue, 17 Nov 2015 08:22:43 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: edwardyoon@apache.org To: dev@horn.incubator.apache.org Date: Tue, 17 Nov 2015 08:22:43 -0000 Message-Id: <4532377f19ea4237be435f25609e4b83@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [1/6] incubator-horn git commit: HORN-7: Implementation of DistBelief package for model parallelism Repository: incubator-horn Updated Branches: refs/heads/master 0aeefcd77 -> acf976d2a HORN-7: Implementation of DistBelief package for model parallelism Project: http://git-wip-us.apache.org/repos/asf/incubator-horn/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-horn/commit/11871567 Tree: http://git-wip-us.apache.org/repos/asf/incubator-horn/tree/11871567 Diff: http://git-wip-us.apache.org/repos/asf/incubator-horn/diff/11871567 Branch: refs/heads/master Commit: 11871567bd0c3b2edb8f6991af95724839188cf1 Parents: 0aeefcd Author: Edward J. Yoon Authored: Mon Nov 9 10:48:22 2015 +0900 Committer: Edward J. Yoon Committed: Mon Nov 9 10:48:22 2015 +0900 ---------------------------------------------------------------------- CHANGES.txt | 11 +++ .../java/org/apache/horn/distbelief/Neuron.java | 42 ++++++++++ .../apache/horn/distbelief/NeuronInterface.java | 45 ++++++++++ .../org/apache/horn/distbelief/PropMessage.java | 60 ++++++++++++++ .../org/apache/horn/distbelief/TestNeuron.java | 87 ++++++++++++++++++++ 5 files changed, 245 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-horn/blob/11871567/CHANGES.txt ---------------------------------------------------------------------- diff --git a/CHANGES.txt b/CHANGES.txt new file mode 100644 index 0000000..0826b90 --- /dev/null +++ b/CHANGES.txt @@ -0,0 +1,11 @@ +Horn Change Log + +Release 0.1.0 (unreleased changes) + + NEW FEATURES + + HORN-1: Website for Apache Horn (Incubating) (Elmurod Talipov via edwardyoon) + + BUG FIXES + + IMPROVEMENTS http://git-wip-us.apache.org/repos/asf/incubator-horn/blob/11871567/src/main/java/org/apache/horn/distbelief/Neuron.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/horn/distbelief/Neuron.java b/src/main/java/org/apache/horn/distbelief/Neuron.java new file mode 100644 index 0000000..ce67cf2 --- /dev/null +++ b/src/main/java/org/apache/horn/distbelief/Neuron.java @@ -0,0 +1,42 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ +package org.apache.horn.distbelief; + +import org.apache.hadoop.io.Writable; + +public abstract class Neuron implements NeuronInterface { + double output; + double weight; + + public void setOutput(double output) { + this.output = output; + } + + public double getOutput() { + return output; + } + + public void push(double weight) { + this.weight = weight; + } + + public double getUpdate() { + return weight; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-horn/blob/11871567/src/main/java/org/apache/horn/distbelief/NeuronInterface.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/horn/distbelief/NeuronInterface.java b/src/main/java/org/apache/horn/distbelief/NeuronInterface.java new file mode 100644 index 0000000..8093b07 --- /dev/null +++ b/src/main/java/org/apache/horn/distbelief/NeuronInterface.java @@ -0,0 +1,45 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ +package org.apache.horn.distbelief; + +import java.io.IOException; + +import org.apache.hadoop.io.Writable; + +public interface NeuronInterface { + + /** + * This method is called when the messages are propagated from the lower + * layer. It can be used to determine if the neuron would activate, or fire. + * + * @param messages + * @throws IOException + */ + public void upward(Iterable messages) throws IOException; + + /** + * This method is called when the errors are propagated from the upper layer. + * It can be used to calculate the error of each neuron and change the + * weights. + * + * @param messages + * @throws IOException + */ + public void downward(Iterable messages) throws IOException; + +} http://git-wip-us.apache.org/repos/asf/incubator-horn/blob/11871567/src/main/java/org/apache/horn/distbelief/PropMessage.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/horn/distbelief/PropMessage.java b/src/main/java/org/apache/horn/distbelief/PropMessage.java new file mode 100644 index 0000000..dd6f2b1 --- /dev/null +++ b/src/main/java/org/apache/horn/distbelief/PropMessage.java @@ -0,0 +1,60 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ +package org.apache.horn.distbelief; + +import java.io.DataInput; +import java.io.DataOutput; +import java.io.IOException; + +import org.apache.hadoop.io.Writable; + +/** + * Message wrapper for a propagating message + */ +public class PropMessage implements + Writable { + + M message; + W weight; + + public PropMessage(M message, W weight) { + this.message = message; + this.weight = weight; + } + + public M getMessage() { + return message; + } + + public W getWeight() { + return weight; + } + + @Override + public void readFields(DataInput in) throws IOException { + message.readFields(in); + weight.readFields(in); + } + + @Override + public void write(DataOutput out) throws IOException { + message.write(out); + weight.write(out); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-horn/blob/11871567/src/test/java/org/apache/horn/distbelief/TestNeuron.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/horn/distbelief/TestNeuron.java b/src/test/java/org/apache/horn/distbelief/TestNeuron.java new file mode 100644 index 0000000..37e8fd6 --- /dev/null +++ b/src/test/java/org/apache/horn/distbelief/TestNeuron.java @@ -0,0 +1,87 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ +package org.apache.horn.distbelief; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import junit.framework.TestCase; + +import org.apache.hadoop.io.DoubleWritable; +import org.apache.hama.commons.math.Sigmoid; + +public class TestNeuron extends TestCase { + private static double learningRate = 0.1; + + public static class MyNeuron extends + Neuron> { + + @Override + public void upward( + Iterable> messages) + throws IOException { + double sum = 0; + for (PropMessage m : messages) { + sum += m.getMessage().get() * m.getWeight().get(); + } + sum += (-1 * 0.8); + + double output = new Sigmoid().apply(sum); + this.setOutput(output); + } + + @Override + public void downward( + Iterable> messages) + throws IOException { + + for (PropMessage m : messages) { + // Calculates error gradient for each neuron + double gradient = this.getOutput() * (1 - this.getOutput()) + * m.getMessage().get() * m.getWeight().get(); + + // Propagates to lower layer + System.out.println(gradient); + + // Weight corrections + double weight = learningRate * this.getOutput() * m.getMessage().get(); + this.push(weight); + } + } + + } + + public void testProp() throws IOException { + List> x = new ArrayList>(); + x.add(new PropMessage(new DoubleWritable( + 1.0), new DoubleWritable(0.5))); + x.add(new PropMessage(new DoubleWritable( + 1.0), new DoubleWritable(0.4))); + + MyNeuron n = new MyNeuron(); + n.upward(x); + assertEquals(0.5249791874789399, n.getOutput()); + + x.clear(); + x.add(new PropMessage(new DoubleWritable( + -0.1274), new DoubleWritable(-1.2))); + n.downward(x); + assertEquals(-0.006688234848481696, n.getUpdate()); + } +}