Clojure IllegalArgumentException:defprotocol中没有单个方法
我使用Clojure IllegalArgumentException:defprotocol中没有单个方法,clojure,Clojure,我使用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
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
我做错了什么?这很奇怪,因为
错误输出
与实际错误没有任何关联:算术错误
我应该为向后
函数提供初始向后梯度
值:
(neurals.core/backward neurals.core/sigaxcby 1.0)
知道了。。也应该给落后以最初的价值。少了一个参数。是的,这很奇怪,但“没有单一方法”通常意味着方法上的算术错误。。谢谢会将此错误添加到我的错误命名空间中。:)