我注意到Arrays的执行速度远远超过Haxe的链接列表(至少在cpp上).我得到的结果如下.
Main.hx:40: With 1 items, Array is 14% faster than List.
Main.hx:40: With 5 items, Array is 58% faster than List.
Main.hx:40: With 10 items, Array is 59% faster than List.
Main.hx:40: With 100 items, Array is 54% faster than List.
Main.hx:40: With 1000 items, Array is 56% faster than List.
Main.hx:40: With 10000 items, Array is 55% faster than List.
Main.hx:40: With 100000 items, Array is 52% faster than List.
Run Code Online (Sandbox Code Playgroud)
这让我感到尴尬.尽管Array必须不断复制项目,但Array怎么能这么快?为什么甚至使用Lists呢?
package tests;
import haxe.Timer;
class Main
{
static …Run Code Online (Sandbox Code Playgroud) 我是J的新手,我一直在尝试创建一个Fibonacci函数作为练习(总是我学习语言时创建的第二个函数).我无法弄清楚我的做法究竟出了什么问题.我试图将它定义为默认,但如果参数大于1则会挂起.
fib =: [ ` (($: (]-1)) + ($: (]-2))) @. (>&1)
Run Code Online (Sandbox Code Playgroud)
我也试图明确地创建它,并且工作正常.
fib =: 3 : 'if. y>1 do. (fib (y-1)) + (fib (y-2)) else. y end.'
Run Code Online (Sandbox Code Playgroud)
我试图通过用13代替3来创建一个默认,但它引发了一个错误.
fib =: 13 : 'if. y>1 do. (fib (y-1)) + (fib (y-2)) else. y end.'
|spelling error
| if. y>1 do. (fib (y-1)) + (fib (y-2)) else. y end.
| ^
| fib=: 13 :'if. y>1 do. (fib (y-1)) + (fib (y-2)) else. y end.'
Run Code Online (Sandbox Code Playgroud)
所以,我要求有人解释我在这里做错了什么.
我试图建立一个利用强化学习的神经网络.我选择了scikit-neuralnetwork作为库(因为它很简单).看起来,两次装配会使Theano崩溃.
这是导致崩溃的最简单的代码(注意,它与哪些层无关,学习率或n_iter也不重要):
import numpy as np
from sknn.mlp import Classifier, Layer
clf = Classifier(
layers=[
Layer("Softmax")
],
learning_rate=0.001,
n_iter=1)
clf.fit(np.array([[0.]]), np.array([[0.]])) # Initialize the network for learning
X = np.array([[-1.], [1.]])
Y = np.array([[1.], [0.]])
clf.fit(X, Y) # crash
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误:
ValueError: Input dimension mis-match. (input[0].shape[1] = 2, input[1].shape[1] = 1)
Apply node that caused the error: Elemwise{Mul}[(0, 1)](y, LogSoftmax.0)
Toposort index: 12
Inputs types: [TensorType(float64, matrix), TensorType(float64, matrix)]
Inputs shapes: [(1L, 2L), (1L, 1L)]
Inputs strides: [(16L, 8L), (8L, 8L)] …Run Code Online (Sandbox Code Playgroud) 好吧,标题并不完全是我想要的,但它必须在记录的成员函数访问速度中找到一个有趣的东西.我将用这个REPL会话来说明:
==> (defprotocol Add (add [_]))
Add
==> (defrecord R [x y] Add (add [_] (+ x y)))
=.R
==> (let [r (->R 1 2)] (time (dotimes [_ 100000] (add r)))) ; Pure functional style
"Elapsed time: 19.613694 msecs"
nil
==> (let [r (->R 1 2)] (time (dotimes [_ 100000] (.add r)))) ; Functional creation, but with method call
"Elapsed time: 477.29611 msecs"
nil
==> (let [r (R. 1 2)] (time (dotimes [_ 100000] (.add r)))) ; Java-style
"Elapsed time: …Run Code Online (Sandbox Code Playgroud) 我一直在尝试实现一个调用类型成员的泛型函数.我发现使用内联可以实现这一点.它没有帮助,所以我试图实现一个接口,如下所示:
type Wrappable<'a, 'b> =
interface
abstract Wrap : ('b -> 'b) -> 'a
end
type StateType =
State of Scene * Cash | Exit
interface Wrappable<StateType, Scene * Cash> with
member this.Wrap f =
match this with
| Exit -> Exit
| State (scene, cash) -> f (scene, cash) |> State
let inline wrap f (o:Wrappable<_, _>) = o.Wrap f
Run Code Online (Sandbox Code Playgroud)
这非常有效,给出了类型输出
type Wrappable<'a,'b> =
interface
abstract member Wrap : ('b -> 'b) -> 'a
end
type StateType =
| …Run Code Online (Sandbox Code Playgroud)