我正在使用defprotocol
来实现多态性。我有一个简单的逻辑门。
forward
和backward
是每个门实现的两个函数。
代码:
;; a single unit has forward value and backward gradient.
(defrecord Unit
[value gradient])
(defrecord Gate
[^:Unit input-a ^:Unit input-b])
(defprotocol GateOps
(forward [this])
(backward [this back-grad]))
(extend-protocol GateOps
Unit
(forward [this]
(-> this :value))
(backward [this back-grad]
({this back-grad})))
(defrecord MultiplyGate [input-a input-b]
GateOps
(forward [this]
(* (forward (-> this :input-a)) (forward (:input-b this))))
(backward [this back-grad]
(let [val-a (forward (-> this :input-a))
val-b (forward (-> this :input-b))
input-a (:input-a this)
input-b (:input-b this)]
(merge-with + (backward input-a (* val-b back-grad))
(backward input-b (* val-a back-grad))))))
(defrecord AddGate [input-a input-b]
GateOps
(forward [this]
(+ (forward (:input-a this)) (forward (:input-b this))))
(backward [this back-grad]
(let [val-a (forward (-> this :input-a))
val-b (forward (-> this :input-b))
input-a (:input-a this)
input-b (:input-b this)]
(merge-with + (backward input-a (* 1.0 back-grad))
(backward input-b (* 1.0 back-grad))))))
(defn sig [x]
(/ 1 (+ 1 (Math/pow Math/E (- x)))))
(defrecord SigmoidGate [gate]
GateOps
(forward [this]
(sig (forward (:gate this))))
(backward [this back-grad]
(let [s (forward this)
ds (* s (- 1 s))]
(backward (:gate this) ds))))
虽然forward
工作正常,但backward
我正在异常:
user> (neurals.core/forward neurals.core/sigaxcby)
0.8807970779778823
user> (neurals.core/backward neurals.core/sigaxcby)
CompilerException java.lang.IllegalArgumentException:
No single method: backward of interface: neurals.core.GateOps
found for function: backward of protocol: GateOps,
compiling:(C:\xxyyzz\AppData\Local\Temp\form-init8132866244624247216.clj:1:1)
user>
版本信息:Clojure 1.6.0
我做错了什么?
答案 0 :(得分:1)
这很奇怪,因为error-output
与实际错误没有任何关联:Arity error
。
我应该为back-grad
函数提供初始backward
值:
(neurals.core/backward neurals.core/sigaxcby 1.0)