Julia/Flux creating a model correctly - using Chain Embedding layer reshaping & Dense layers - julia

I am trying to follow an old article about word embedding in Julia language with Flux: https://spcman.github.io/getting-to-know-julia/deep-learning/nlp/flux-embeddings-tutorial-1/
As it is an old outdated article I managed to handle the embedding layer correctly using the new embedding layer of Flux (0.13.11). The Chain looks like this:
embedding = Flux.Embedding(vocab_size => max_features, init=Flux.glorot_normal)
model = Chain(embedding(Flux.onehotbatch(reshape(x, pad_size*N), 0:vocab_size-1)),
x -> reshape(x, max_features, pad_size, N),
x -> mean(x, dims=2),
x -> reshape(x, 8, 10),
Dense(8, 1),
)
When I try to check the model my calling model(x) I get:
ERROR: MethodError: objects of type Matrix{Float32} are not callable
Use square brackets [] for indexing an Array.
My guess that the problem somehow connected to the reshaping.
I am new to both Julia & Flux so sorry if the question is basic.
Any clue what should I do?

Related

Creating a custom TreeModel using Gtk.jl

Background: I am trying to build a mini spreadsheet application in Julia that could hold potentially thousands of columns/rows. I found that GTK seems to be fairly mature graphics library to this task (though no spreadsheet application that I know of exists in Julia). GTK's default TreeModel/ListStore works but writing data to it would almost certaintly be a huge pain and very slow as it requires tuples to pass each row to it...
My attempt based on the documentation from Gtk.jl (does not work as intended, I also created the various methods I want to override):
mutable struct CustomTreeModel <: ListStore
handle::Ptr{Gtk.GObject}
_num_rows::Int
_n_columns::Int
cell_matrix::Matrix{Cell}
function CustomTreeModel(cell_matrix)
ls = ListStore(String)
tm_handle = Base.unsafe_convert(Ptr{Gtk.GObject}, ls)
new_obj = new(tm_handle, size(cell_matrix, 1), size(cell_matrix, 2), cell_matrix)
return gobject_move_ref(new_obj, ls)
end
#Methods to override
get_value(self::CustomTreeModel, iter_, column) = string(self.cell_matrix[iter_.user_data][column])
get_n_columns(self::CustomTreeModel) = self._n_columns
get_column_type(self::CustomTreeModel, column) = String
get_flags(self::CustomTreeModel) = Gtk.GtkTreeModelFlags.GTK_TREE_MODEL_ITERS_PERSIST
...
#I want to override the tree model though
tv = GtkTreeView(GtkTreeModel(CustomTreeModel(cells)))
Based on the GTK documentation, it appears I have two choices: override the TreeModel class's virtual methods (cant from Julia) or register the type. Unfortunately I have no idea how to do the latter.
Any thoughts on how to implement a custom model using Gtk.jl?

Get branch and bound node count in JuMP/Gurobi

I am trying to get the branch and bound node count using the JuMP interface and the Gurobi solver in Julia.
I tried getnodecount(m) as suggested on the JuMP website, but this came back as undefined. After doing more research, I read to try:
MathProgBase.getnodecount(m)
A simple example:
using Gurobi
using JuMP
using MathProgBase
m = Model(with_optimizer(Gurobi.Optimizer))
#variable(m, x, Bin)
#variable(m, y >=0)
#objective(m, Min, x*y)
optimize!(m)
println(value(x))
# getnodecount(m)
MathProgBase.getnodecount(m)
I expected to get a node count of 0, but got this error:
LoadError: MethodError: no method matching getnodecount(::Model)
Closest candidates are:
getnodecount(!Matched::Gurobi.GurobiMathProgModel) at /uliapro/JuliaPro_v1.1.1.1/packages/Gurobi/dlJep/src/MPB_wrapper.jl:759
You seem to be using the new MathOptInterface rather than MathProgBase. That is why you do not get a GurobiMathProgModel and hence the error. In MathOptInterface, you can do the following to get the node count.
MOI.get(model, MOI.NodeCount())
which will call the Gurobi.get_node_count(model::Model) method implemented here. For other attributes, please refer to the MOI API Reference.

What does the "Base" keyword mean in Julia?

I saw this example in the Julia language documentation. It uses something called Base. What is this Base?
immutable Squares
count::Int
end
Base.start(::Squares) = 1
Base.next(S::Squares, state) = (state*state, state+1)
Base.done(S::Squares, s) = s > S.count;
Base.eltype(::Type{Squares}) = Int # Note that this is defined for the type
Base.length(S::Squares) = S.count;
Base is a module which defines many of the functions, types and macros used in the Julia language. You can view the files for everything it contains here or call whos(Base) to print a list.
In fact, these functions and types (which include things like sum and Int) are so fundamental to the language that they are included in Julia's top-level scope by default.
This means that we can just use sum instead of Base.sum every time we want to use that particular function. Both names refer to the same thing:
Julia> sum === Base.sum
true
Julia> #which sum # show where the name is defined
Base
So why, you might ask, is it necessary is write things like Base.start instead of simply start?
The point is that start is just a name. We are free to rebind names in the top-level scope to anything we like. For instance start = 0 will rebind the name 'start' to the integer 0 (so that it no longer refers to Base.start).
Concentrating now on the specific example in docs, if we simply wrote start(::Squares) = 1, then we find that we have created a new function with 1 method:
Julia> start
start (generic function with 1 method)
But Julia's iterator interface (invoked using the for loop) requires us to add the new method to Base.start! We haven't done this and so we get an error if we try to iterate:
julia> for i in Squares(7)
println(i)
end
ERROR: MethodError: no method matching start(::Squares)
By updating the Base.start function instead by writing Base.start(::Squares) = 1, the iterator interface can use the method for the Squares type and iteration will work as we expect (as long as Base.done and Base.next are also extended for this type).
I'll grant that for something so fundamental, the explanation is buried a bit far down in the documentation, but http://docs.julialang.org/en/release-0.4/manual/modules/#standard-modules describes this:
There are three important standard modules: Main, Core, and Base.
Base is the standard library (the contents of base/). All modules
implicitly contain using Base, since this is needed in the vast
majority of cases.

Set axis origin using PyPlot plot in Julia

I am completely new to Julia. I am using the PyPlot package in Julia and am just trying to set my x and y axis origin to 0 and 0 respectively. At the moment it just chooses where the origin will be based on the values of the points I'm plotting.
plot(x1,y1,".")
xlabel("X1")
ylabel("Y1")
title("First Line")
grid("on")
I have tried the following but it doesn't work.
change matplotlib axis settings
Using PyPlot in Julia is different than matplotlib in Python. You are looking for the Julia equivalent of setting limits on axis. I've found this useful github repo that might be of use to you.
Here is how you can add custom limits to the y axis:
using PyPlot
x1 = rand(50, 1) .* 30 .+ 50
y1 = rand(50, 1) .* 30 .+ 100
plot(x1, y1)
# get the current axis argument of the plot
ax = gca()
# add new limits from 0 - 100
ax[:set_ylim]([0,100])
The syntax for using PyPlot in Julia is a bit different from Python.
This is a result of the fact that (currently) you cannot use obj.f() to access the method (function) f() inside an object obj in Julia, which PyPlot uses heavily.
To get around this, obj.f() in Python is replaced by obj[:f]() in Julia; note the :.
So ax.spines in Python becomes
ax[:spines]
in Julia.
As noted by the other poster, you must first do ax = gca() to store the current axis object in the variable ax.
If you do this at the Julia REPL or in a Jupyter notebook, you will see
julia> ax = gca()
PyObject <matplotlib.axes._subplots.AxesSubplot object at 0x31f854550>
julia> ax[:spines]
Dict{Any,Any} with 4 entries:
"left" => PyObject <matplotlib.spines.Spine object at 0x31f854c10>
"bottom" => PyObject <matplotlib.spines.Spine object at 0x31f854f50>
"right" => PyObject <matplotlib.spines.Spine object at 0x31f854dd0>
"top" => PyObject <matplotlib.spines.Spine object at 0x31f86e110>
showing that ax[:spines] is a dictionary. (I did not previously know about this. This is the advantage of having an interactive session -- you can just ask Julia what you need to know. This is called "introspection".)
Following the Python answer linked to in the original question, you then need to replace ax.spines['left'].set_position('zero') in Python by the following in Julia:
ax[:spines]["left"][:set_position]("zero")
Again, the set_position method inside the object ax[:spines]["left"] is called. Note also that strings in Julia must have ", not '.

Abstracting over Collection Types

Is there a possibility of writing functions which are generic in respect to collection types they support other than using the seq module?
The goal is, not having to resort to copy and paste when adding new collection functions.
Generic programming with collections can be handled the same way generic programming is done in general: Using generics.
let f (map_fun : ('T1 -> 'T2) -> 'T1s -> 'T2s) (iter_fun : ('T2 -> unit) -> 'T2s -> unit) (ts : 'T1s) (g : 'T1 -> 'T2) (h : 'T2 -> unit)=
ts
|> map_fun g
|> iter_fun h
type A =
static member F(ts, g, h) = f (Array.map) (Array.iter) ts g h
static member F(ts, g, h) = f (List.map) (List.iter) ts g h
A bit ugly and verbose, but it's possible. I'm using a class and static members to take advantage of overloading. In your code, you can just use A.F and the correct specialization will be called.
For a prettier solution, see https://stackoverflow.com/questions/979084/what-features-would-you-add-remove-or-change-in-f/987569#987569 Although this feature is enabled only for the core library, it should not be a problem to modify the compiler to allow it in your code. That's possible because the source code of the compiler is open.
The seq<'T> type is the primary way of writing computations that work for any collections in F#. There are a few ways you can work with the type:
You can use functions from the Seq module (such as Seq.filter, Seq.windowed etc.)
You can use sequence comprehensions (e.g. seq { for x in col -> x * 2 })
You can use the underlying (imperative) IEnumerator<'T> type, which is sometimes needed e.g. if you want to implement your own zipping of collections (this is returned by calling GetEnumerator)
This is relatively simple type and it can be used only for reading data from collections. As the result, you'll always get a value of type seq<'T> which is essentially a lazy sequence.
F# doesn't have any mechanism for transforming collections (e.g. generic function taking collection C to collection C with new values) or any mechanism for creating collections (which is available in Haskell or Scala).
In most of the practical cases, I don't find that a problem - most of the work can be done using seq<'T> and when you need a specialized collection (e.g. array for performance), you typically need a slightly different implementation anyway.

Resources