作为玩具示例,我试图f(x) = 1/x从100个无噪声数据点拟合函数.matlab默认实现非常成功,均方差为~10 ^ -10,并且插值完美.
我实现了一个神经网络与一个隐藏的10个sigmoid神经元层.我是神经网络的初学者,所以请注意防范愚蠢的代码.
import tensorflow as tf
import numpy as np
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
#Can't make tensorflow consume ordinary lists unless they're parsed to ndarray
def toNd(lst):
lgt = len(lst)
x = np.zeros((1, lgt), dtype='float32')
for i in range(0, lgt):
x[0,i] = lst[i]
return x
xBasic = np.linspace(0.2, 0.8, 101)
xTrain = toNd(xBasic)
yTrain = toNd(map(lambda x: 1/x, xBasic))
x = tf.placeholder("float", …Run Code Online (Sandbox Code Playgroud) 我喜欢F#的一件事是真正的inline关键词.但是,虽然它允许编写执行与粘贴代码块相同的第一个顺序函数,但对于高阶函数来说,情况并不乐观.考虑
let inline add i = i+1
let inline check i = if (add i) = 0 then printfn ""
let inline iter runs f = for i = 0 to runs-1 do f i
let runs = 100000000
time(fun()->iter runs check) 1
time(fun()->for i = 0 to runs-1 do check i) 1
Run Code Online (Sandbox Code Playgroud)
结果是244 ms对于iter和61 ms用于手动检查.让我们深入研究ILSpy.要求直接呼叫的相关功能是:
internal static void func@22-12(Microsoft.FSharp.Core.Unit unitVar0)
{
for (int i = 0; i < 100000000; i++)
{ …Run Code Online (Sandbox Code Playgroud) 在编写我的穷人的FParsec版本时,我遇到了这种现象.考虑:
let add x = x+1
let fromLeft = add>>add>>add>>add>>add>>add>>add>>add>>add>>add
let fromRight = add<<add<<add<<add<<add<<add<<add<<add<<add<<add
let imperative x =
let mutable result = x
for i = 0 to 9 do
result <- add result
result
Run Code Online (Sandbox Code Playgroud)
并测试所有三个功能的性能:
let runs = 10000000
printf "From left\n"
time(fun()->fromLeft 0) runs
printf "\nFrom right\n"
time(fun()->fromRight 0) runs
printf "\nImperative\n"
time(fun()->imperative 0) runs
Run Code Online (Sandbox Code Playgroud)
结果是:59 fromLeft毫秒,658毫秒fromRight
和26毫秒Imperative.
测试在发布模式和VS外部完成.结果是稳定的,不依赖于我测试函数的顺序.如果Imperative运行时间被认为是add函数本身的开销并且从两个结果中减去,则两个组合物的性能不同的因素是11x或19x .
有谁知道这种差异的原因?
我的time组合是
let inline time func n = …Run Code Online (Sandbox Code Playgroud) 我正在学习类型提供程序,它看起来像是一个突破性的功能。但是,我无法设法使用 JsonProvider 反序列化 json,以便目标类型具有 Generic.Dictionary 属性。可以使用 Json.NET 来完成。这是代码:
type ByProv = JsonProvider<"""{"dict":{"A":1,"B":2}}""">
type ByHand(d:Dictionary<string,int>) =
member val dict = d with get,set
let text = """{"dict":{"C":3,"D":4}}"""
let newt = JsonConvert.DeserializeObject<ByHand> text
let prov = ByProv.Parse text
printfn "%A" newt.dict.["C"]
//Can only call statically inferred A or B (and it will throw at run-time)
printfn "%A" prov.Dict.A
Run Code Online (Sandbox Code Playgroud)
显然,dict被推断为记录类型,而不是Dictionary默认类型。这可以被覆盖吗?
我最近一直在用C#测试C5系列,我很喜欢它们的功能.对于大型馆藏,性能似乎与通用同行相当.对于小型集合,它们的速度要慢得多.我怀疑相对速度的急剧恶化来自于C5系列的恒定时间操作.我所知道的一项行动就是解雇事件.这可能是小型收藏品表现不佳的原因吗?可以通过关闭某些功能来解决这个问题吗?这是'性能测试:
//Two containers to be tested. 'Test' is a wrapper over decimal.
var arrayList = new C5.ArrayList<Test>();
var genericList = new System.Collections.Generic.List<Test>();
var toBeAdded = new List<Test>();
var watch = new Stopwatch();
//Fill both tested containers
for (int i = 10; i > 0; i--)
{
var test = new Test(i);
genericList.Add(test);
arrayList.Add(test);
}
//Fill the container the range of which will be inserted to the tested containers
for (int i = 5; i > 0; i--)
{
toBeAdded.Add(new Test(i+0.5m)); …Run Code Online (Sandbox Code Playgroud) Hopac允许同步give值到一个通道,该通道将在没有人正在侦听时阻塞,并且异步地send 将在没有读取时缓冲这些值.
我想在中间做一些事情:如果有一个监听器则给出一个值,或者在没有阻塞或缓冲值的情况下继续运行(如果没有).有办法吗?
f# ×4
performance ×3
asynchronous ×1
c# ×1
c5 ×1
collections ×1
containers ×1
f#-data ×1
hopac ×1
matlab ×1
python ×1
tensorflow ×1