Torch: NN handling text and numeric input - neural-network

I have the following NN architecture:
Part 1:
nn.Sequential {
[input -> (1) -> (2) -> (3) -> (4) -> (5) -> (6) -> (7) -> output]
(1): nn.TemporalConvolution
(2): nn.TemporalMaxPooling
(3): nn.TemporalConvolution
(4): nn.TemporalMaxPooling
(5): nn.Reshape(14336)
(6): nn.Dropout(0.500000)
(7): nn.Linear(14336 -> 128)
}
Part 2:
nn.Sequential {
[input -> (1) -> (2) -> (3) -> output]
(1): nn.Linear(4 -> 8)
(2): nn.ReLU
(3): nn.Linear(8 -> 4)
}
What i would like to do is to use the output of these two parts as input to another part:
nn.Sequential {
[input -> (1) -> (2) -> (3) -> (4) -> (5) -> (6) -> output]
(1): nn.Linear(132 -> 32)
(2): nn.ReLU
(3): nn.Linear(32 -> 32)
(4): nn.ReLU
(5): nn.Linear(32 -> 2)
(6): nn.LogSoftMax
}
Notice how part 1 has 128 outputs and part 2 has 4 and finally part 3 has 132 inputs. So basically what i want is a network that takes two types of input (part 1 is for text, part 2 for a number vector) and to use both these information in the third layer for a 2 class classification.
I have looked at the various containers but nothing seems like it is what i need. Specifically i have looked at nn.Parallel but from the docs it looks like it does something completely different (same input for two different modules). The first problem is what should the input look like for the network (since each part takes a different type of Tensor, i thought that a simple table (array) would be fine, with its first element being a 2D tensor and second a 1D tensor) and how to plug its outputs into another network so i can use forward/backward calls as usual.
Is there any way how to do this?
Thanks!

What you need is nn.ParallelTable and nn.JoinTable.
local parallel = nn.ParallelTable()
parallel:add(part1)
parallel:add(part2)
local net = nn.Sequential()
net:add(parallel) -- (A)
net:add(nn.JoinTable(1)) -- (B)
net:add(part3) -- (C)
(A):
parallel will take a table of 2 tensors (in your case, text and numbers), forward first tensor to part1, second tensor to part2 and output both results in another table of 2 tensors.
(B):
The following nn.JoinTable takes this table as input and concatenates the 2 tensors in a single one. You may have to play with the parameter handling the concatenation dimension (1 in my example) depending on the shape of your tensors.
(C):
Finally you can add the third part of your network taking the concatenated tensor as input.

Related

What is the difference between (0:2):4 and 0:(2:4)?

What is the difference between (0:2):4 and 0:(2:4)?
Both neglects the 2nd part of the bracket thus printing values similar to writing (0:4) and (0:2) respectively.
I could generalize from this that the bracket's first element is only working in this vector. But I would like to know the actual reason why is it happening.
the colon operator has lower priority than (), so, matlab first evaluates the vector inside the parenthesis, then, if one of the operands is a vector, colon only takes the first value. here are the evaluation steps:
(0:2):4 -> (0:2)=[0 1 2] -> 0:4 -> [0,1,2,3,4]
0:(2:4) -> (2:4)=[2 3 4] -> 0:2 -> [0,1,2]

Filtering data using constraints in Datastage Transformer

In Datastage, I have an INTEGER field from a Seq File 0, in a transformer i wanted to write a constraint that if the Source data from the seq file is <> 0 or <> Numeric (numbers) then it should be written in seq file 2 and other Numeric data and 0 into Seq File 1 sequential file.
Please help me out.
Seq File 0 ->Transformer(Constraint) -> Seq File 1 (0 or Other Numerics) and
-> Seq File 2 (Blanks and Non-numeric)
Check out the num Function or alpha function to check wether it is a number or alphanumeric value. You may have to handle the empty case separately.

Evaluate Equation from String

I have a String stored in a table with two logic gates which looks like 1*((CUBL>1)*((DIFL>1)*1)).
I have been able to replace the text with values from a different table i.e. 1*((1500>1)*((0>1)*1)). The issue i am having is being able to evaluate the equations between the brackets. The following is a list of steps that i believe are needed.
1*((CUBL>1)*((DIFL>1)*1)) -> 1*((1500>1)*((0>1)*1))
1*((1500>1)*((0>1)*1)) -> 1*(1*(0*1))
1*(1*(0*1)) -> 1*(1*0)
1*(1*0) -> 1*0 = 0

append if element in list

I'm trying to create a parser for program. For example,
I entered (what I want)
"(2+3)-4" it will become something like this "(minus, (plus, num 2, num 3),num 4)"
What I've done so far..
"(2+3)-4" I then split it and it becomes list Z = ["(","2","+","3",")","-","4"] then I compared if "-" is a member of Z, if true I append the element "-" into a new list ["-"]
I'm not sure if the way I'm doing is correct, I'm new to Er-lang and struggling quite a lot. If anyone is able to offer me some insight, thanks.
Consider the following, which returns a tuple-based representation of its input:
parse(Expr) ->
Elems = re:split(Expr, "([-+)(])", [{return,list}]),
parse(lists:filter(fun(E) -> E /= [] end, Elems), []).
parse([], [Result]) ->
Result;
parse([], [V2,{op,Op},V1|Tacc]) ->
parse([], [{Op,V1,V2}|Tacc]);
parse(["("|Tail], Acc) ->
parse(Tail, [open|Acc]);
parse([")"|Tail], [Op,open|TAcc]) ->
parse(Tail, [Op|TAcc]);
parse(["+"|Tail], Acc) ->
parse(Tail, [{op,plus}|Acc]);
parse(["-"|Tail], Acc) ->
parse(Tail, [{op,minus}|Acc]);
parse([V2|Tail], [{op,Op},V1|Tacc]) ->
parse(Tail, [{Op,V1,{num,list_to_integer(V2)}}|Tacc]);
parse([Val|Tail], Acc) ->
parse(Tail, [{num,list_to_integer(Val)}|Acc]).
The first function, parse/1, splits the expression along the + and - operators and parentheses, preserving these in the resulting list. It then filters that list to remove empty elements, and passes it with an empty accumulator to parse/2.
The parse/2 function has eight clauses, described below:
The first two handle the case when the parsed input list has been exhausted. The second of these handles the case where multiple elements in the accumulator need to be collapsed into a single tuple consisting of operator and operands.
The next two handle clauses parentheses. When we see an open parenthesis, we push an atom open into the accumulator. Upon seeing the matching close parenthesis, we expect to see an operation tuple and the atom open in the accumulator, and we replace them with just the tuple.
Clauses 5 and 6 handle + and - respectively. Each just pushes a {op,Operator} tuple into the accumulator, where Operator is either the atom plus or the atom minus.
The final two clauses handle values. The first one handles the case where the accumulator holds a value and an op tuple, which gets replaced with a full operation tuple consisting of the atom plus or minus followed by two num tuples each holding integer operands. The last clause just handles plain values.
Putting this in a module p, compiling it, and running it in an Erlang shell yields the following:
1> p:parse("2+3").
{plus,{num,2},{num,3}}
2> p:parse("(2+3)-4").
{minus,{plus,{num,2},{num,3}},{num,4}}

How can I know the line equivalence of two similar files?

When I add a line to the middle of a file, all following lines have their number incremented.
Is there a utility that generates the list of equivalent line numbers between two files?
The output would be something like:
1 1
2 2
3 4 (line added)
4 5
One can probably create such utility by using dynamic programming in a way similar to the diff algorithm. Seems useful, hasn't already been done?
I found out it is pretty easy to do with python's difflib:
import difflib
def seq_equivs(s1, s2):
equiv = []
s = difflib.SequenceMatcher(a=s1, b=s2)
for m in s.get_matching_blocks():
if m[2] == 0:
break
for n in range(1, 1+m[2]):
equiv.append((m[0]+n, m[1]+n))
return equiv
Example usage:
f1 = open('file1.txt').read().split('\n')
f2 = open('file2.txt').read().split('\n')
for equivs in seq_equivs(f1, f2):
print('%d %d' % equivs)