小编Har*_*din的帖子

如何检索Julia宏的方法?

在Julia中,该methods函数可用于检索函数的方法.

julia> f(::Int) = 0
f (generic function with 1 method)

julia> f(::String) = ""
f (generic function with 2 methods)

julia> methods(f)
# 2 methods for generic function "f":
f(::String) in Main at REPL[1]:1
f(::Int64) in Main at REPL[0]:1
Run Code Online (Sandbox Code Playgroud)

宏也可以有多种方法.

julia> macro g(::Int)
           0
       end
@g (macro with 1 method)

julia> macro g(::String)
           ""
       end
@g (macro with 2 methods)

julia> @g 123
0

julia> @g "abc"
""
Run Code Online (Sandbox Code Playgroud)

但是,该methods函数似乎不适用于宏,因为Julia首先调用宏,因为它们不需要括号.

julia> methods(@g)
ERROR: MethodError: no method …
Run Code Online (Sandbox Code Playgroud)

methods macros julia

6
推荐指数
1
解决办法
151
查看次数

Python:Jupyter Notebook中的漂亮打印

我想在Jupyter Notebook中漂亮地打印字典。

我正在使用以下内容:

import pprint
stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni']
stuff.insert(0, stuff[:])
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(stuff)
Run Code Online (Sandbox Code Playgroud)

但是,按Shift + Enter时,没有任何[out]单元格出现(即,我看不到漂亮的打印输出)

在此处输入图片说明

知道为什么会这样吗/为了看到漂亮的打印输出,我应该改变什么?


编辑:实际上这是一个python 2.7问题-它在3.x中正常工作。有没有人在python 2.7上试用过它,并且看到它起作用了?

python pretty-print python-2.x python-2.7 jupyter-notebook

5
推荐指数
1
解决办法
1万
查看次数

如何将构造函数添加到 Julia 中的类型别名?

我有一个 type Milkshake,其中包含一个flavor字段。我想要另一种类型,Order,它只包含一个Milkshakes列表;因此,我使用了一个typealias.

julia> VERSION
v"0.5.1"

julia> type Milkshake
           flavor::String
       end

julia> typealias Order Array{Milkshake, 1}
Array{Milkshake,1}

julia> Order([Milkshake("Chocolate"), Milkshake("Vanilla")])
2-element Array{Milkshake,1}:
 Milkshake("Chocolate")
 Milkshake("Vanilla")  
Run Code Online (Sandbox Code Playgroud)

不过,我想向 中添加一个构造函数Order,以便我可以通过简单地使用flavor字符串来初始化订单。但是,当我尝试定义执行此操作的构造函数时,该定义奇怪地返回 type Array{Milkshake, 1}

julia> Order(milkshakes::String...) = Order(map(Milkshake, milkshakes))
Array{Milkshake,1}
Run Code Online (Sandbox Code Playgroud)

运行时,会产生以下错误。

julia> Order("chocolate", "vanilla")
ERROR: MethodError: Cannot `convert` an object of type Tuple{Milkshake,Milkshake} to an object of type Array{Milkshake,1}
This may have arisen from a call to the constructor …
Run Code Online (Sandbox Code Playgroud)

constructor type-alias julia

5
推荐指数
1
解决办法
489
查看次数

如何根据观察结果训练带有Keras的DDPG以回显点?

我正在尝试训练强化学习模型,以回应我使用的一个点keras-rl.

我的代码如下所示,使用20x20的世界形状.

import numpy as np
import rl.core as krl


class EchoEnv(krl.Env):

    def __init__(self):
        self.shape = np.array([20, 20])
        self.desired = np.random.rand(*self.shape.shape)

    def step(self, action):

        action = np.clip(action, 0, 1) * self.shape
        self.action = action

        observation = self.desired

        dx = action - (self.desired * self.shape)
        distance_to_goal = np.sqrt((dx**2).sum())

        done = distance_to_goal < 1

        reward = -distance_to_goal

        # observation, reward, done, info
        return observation, reward, done, {}

    def reset(self):
        self.desired = np.random.rand(*self.shape.shape)
        return self.desired
Run Code Online (Sandbox Code Playgroud)

每次迭代,observation提供的是当前的随机self.desired值.(代理应该 …

python machine-learning reinforcement-learning keras keras-layer

5
推荐指数
0
解决办法
563
查看次数

如何将自定义类型调用函数概括为抽象类型?

我有以下模拟设置,抽象类型,具体类型作为子类型,以及一个f带两个参数的函数,第一个是a Letter.

abstract type Letter end

struct A <: Letter end
struct B <: Letter end

f(::A, x) = ('a', x)
f(::B, x) = ('b', x)

a = A()
b = B()
Run Code Online (Sandbox Code Playgroud)

我想为Letter只需调用的子类型定义一个自定义调用函数f.

(t::A)(x) = f(t, x)
(t::B)(x) = f(t, x)
Run Code Online (Sandbox Code Playgroud)

虽然这有效,但似乎相当多余,特别是考虑到可能有更多的亚型Letter.我的尝试如下,但似乎都没有效果.

julia> (t::Letter)(x) = f(t, x)
ERROR: cannot add methods to an abstract type

julia> (t::T)(x) where T <: Letter = f(t, x)
ERROR: function type in method definition is …
Run Code Online (Sandbox Code Playgroud)

types abstract julia

5
推荐指数
1
解决办法
129
查看次数

SymEngine.jl 中的简化表达式

在 中SymPy.jl,可以使用该simplify函数轻松简化表达式。

julia> using SymPy

julia> expr = x * (3 - 4/x)
  ?    4?
x??3 - ??
  ?    x?

julia> simplify(expr)
3?x - 4
Run Code Online (Sandbox Code Playgroud)

但是,似乎没有类似的功能SymEngine.jl

julia> using SymEngine

julia> expr = x * (3 - 4/x)
x*(3 - 4*x^(-1))
Run Code Online (Sandbox Code Playgroud)

据我所知,SymEngine绝不是完整的。不过,我仍然希望能够简化我的表达方式。有没有办法通过现有或自定义函数来实现这一点?

sympy julia

3
推荐指数
1
解决办法
778
查看次数

将方法内容检索为"Expr"

我有一个f定义如下的函数.

f(x, y) = 3x^2 + x*y - 2y + 1
Run Code Online (Sandbox Code Playgroud)

如何检索quote此方法的以下块,其中包括函数内容?

quote  # REPL[0], line 2:
    ((3 * x ^ 2 + x * y) - 2y) + 1
end
Run Code Online (Sandbox Code Playgroud)

expression julia

3
推荐指数
1
解决办法
116
查看次数

Julian替代属性继承是什么?

在许多编程语言中,父类可以要求任何子类包含特定字段.

如果该字段是静态的,则可以通过以下方式在Julia中实现相同的效果.

julia> abstract Fruit

julia> type Apple <: Fruit end

julia> type Orange <: Fruit end

julia> type Banana <: Fruit end

julia> color(::Apple) = :red
color (generic function with 1 method)

julia> color(::Orange) = :orange
color (generic function with 2 methods)

julia> color(::Banana) = :yellow
color (generic function with 3 methods)
Run Code Online (Sandbox Code Playgroud)

但是,如果该字段是动态的,则不起作用.在以下示例中,我想要求Pet包含该字段的任何子类型name.

julia> abstract Pet

julia> type Cat <: Pet
           name::String
           hairless::Bool
       end

julia> type Dog <: Pet
           name::String
       end

julia> abstract Bird <: …
Run Code Online (Sandbox Code Playgroud)

abstract-class types abstract julia

3
推荐指数
1
解决办法
186
查看次数

如何改进Julia中嵌套数组的速度?

以下函数nested_arrays(令人惊讶地)生成"深度"的嵌套数组n.但是,当运行甚至很小的n(2,3等等)时,运行和显示输出需要相当长的时间.

julia> nested_arrays(n) = n == 1 ? [1] : [nested_arrays(n - 1)]
nested_arrays (generic function with 1 method)

julia> nested_arrays(1)
1-element Array{Int64,1}:
 1

julia> nested_arrays(2)
1-element Array{Array{Int64,1},1}:
 [1]

julia> nested_arrays(3)
1-element Array{Array{Array{Int64,1},1},1}:
 Array{Int64,1}[[1]]

julia> nested_arrays(10)
1-element Array{Array{Array{Array{Array{Array{Array{Array{Array{Array{Int64,1},1},1},1},1},1},1},1},1},1}:
 Array{Array{Array{Array{Array{Array{Array{Array{Int64,1},1},1},1},1},1},1},1}[Array{Array{Array{Array{Array{Array{Array{Int64,1},1},1},1},1},1},1}[Array{Array{Array{Array{Array{Array{Int64,1},1},1},1},1},1}[Array{Array{Array{Array{Array{Int64,1},1},1},1},1}[Array{Array{Array{Array{Int64,1},1},1},1}[Array{Array{Array{Int64,1},1},1}[Array{Array{Int64,1},1}[Array{Int64,1}[[1]]]]]]]]]
Run Code Online (Sandbox Code Playgroud)

有趣的是,当使用@time宏或;在行的末尾时,结果花费的时间相对较少.相反,在REPL中实际显示结果占用了大部分时间.

这种奇怪的行为没有在例如Python中显示.

In [1]: def nested_lists(n):
   ...:     if n == 1:
   ...:         return [1]
   ...:     return [nested_lists(n - 1)]
   ...: 

In [2]: nested_lists(10)
Out[2]: [[[[[[[[[[1]]]]]]]]]] …
Run Code Online (Sandbox Code Playgroud)

arrays nested julia

3
推荐指数
1
解决办法
129
查看次数

对“sqlcxt”的未定义引用

尝试通过该命令编译一些 C 代码(select * from dual在 Linux 上使用 oracle (11g) db 连接的简单代码,通过命令从 .pc 生成procgcc,但我总是收到错误。

undefined reference to 'sqlcxt'
Run Code Online (Sandbox Code Playgroud)

我尝试链接到我在网络上不同论坛上看到的库,但什么也没得到(错误仍然发生):

gcc -o proc_test proc_test.c -L$ORACLE_HOME/lib/libclntsh.so
Run Code Online (Sandbox Code Playgroud)

我仍然得到

/tmp/ccAW4Imj.o: In function `main':
proc_test.c:(.text+0x3ea): undefined reference to `sqlcxt'
proc_test.c:(.text+0x57c): undefined reference to `sqlcxt'
proc_test.c:(.text+0x60d): undefined reference to `sqlcxt'
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)

有人可以帮忙吗?

c linux gcc

2
推荐指数
1
解决办法
6302
查看次数

Julia中的力积分型参数

我想定义一个简单的类型来表示一个n包含类型参数的维度形状n.

julia> struct Shape{n}
           name::String
       end

julia> square = Shape{2}("square")
Shape{2}("square")

julia> cube = Shape{3}("cube")
Shape{3}("cube")

julia> dim(::Shape{n}) where n = n
dim (generic function with 1 method)

julia> dim(cube)
3
Run Code Online (Sandbox Code Playgroud)

虽然此解决方案确实有效,但它接受非整数值而n没有问题.

julia> Shape{'?'}("invalid")
Shape{'?'}("invalid")
Run Code Online (Sandbox Code Playgroud)

我最初的想法是nstruct声明中使用约束.然而,我认为应该完成的这两种方式似乎都不起作用.

julia> struct Shape{n} where n <: Int
           name::String
       end
ERROR: syntax: invalid type signature

julia> struct Shape{n<:Int}
           name::String
       end

julia> Shape{2}("circle")
ERROR: TypeError: Shape: in n, expected n<:Int64, got Int64
Run Code Online (Sandbox Code Playgroud)

我也尝试使用内部构造函数,但这似乎也没有用.

julia> struct Shape{n} …
Run Code Online (Sandbox Code Playgroud)

types julia

2
推荐指数
1
解决办法
105
查看次数

Julia数组理解内存使用情况

我正在使用数组解析来定义自定义类型数组的元素的感兴趣值.

sum([value.interest for value in ArrayofMyType if condition])
Run Code Online (Sandbox Code Playgroud)

该表达式本身处于迭代循环中.这意味着每个周期,这种理解都使用新的记忆.此外,每次条件可能对所得到的阵列施加不同的长度,并且在循环外定义具有固定长度的预定义阵列可能不是最佳方式.

每次循环运行时都会产生开销,我不知道如何提高效率并更好地利用内存.有没有人可以用这个指导我?理解在风格上很方便但我觉得在我的情况下并不是最有效的.

代码风格是:

for i in 1:MAX_ITER
    ### Some code above
    sum([value.interest for value in ArrayofMyType if condition])     
    ### Some code below
end
Run Code Online (Sandbox Code Playgroud)

arrays memory-management list-comprehension julia

1
推荐指数
1
解决办法
207
查看次数