Name the output of an expression in Tensorflow - tensorflow

I wonder whether it's possible to give a name to the output of a certain expression to retrieve it from the graph at another part of the code.
For example
def A(a, b):
c = a + b
d = d * a
return d
For debug purposes it would be nice if I could pull out c at another position without returning it through the entire function hierarchy.
Any ideas?
Thanks in advance!

I'm assuming a and b are tensors.
Either you give a name to c using tf.identity
def A(a, b):
c = a + b
c = tf.identity(c, name = "c")
d = d * a
return d
Either you use the tf.add operation instead of +:
def A(a, b):
tf.add(a, b, name = "c")
d = d * a
return d
Either way, you get retrieve c with tf.get_variable('c:0') (You might need to precise the scope if any.)

Related

Posing a quadrating optimization problem for CVXOPT correctly

I am trying to minimize the function || Cx - d ||_2^2 with constraints Ax <= b. Some information about their sizes is as such:
* C is a (138, 22) matrix
* d is a (138,) vector
* A is a (138, 22) matrix
* b is a (138, ) vector of zeros
So I have 138 equation and 22 free variables that I'd like to optimize. I am currently coding this in Python and am using the transpose C.T*C to form a square matrix. The entire code looks like this
C = matrix(np.matmul(w, b).astype('double'))
b = matrix(np.matmul(w, np.log(dwi)).astype('double').reshape(-1))
P = C.T * C
q = -C.T * b
G = matrix(-constraints)
h = matrix(np.zeros(G.size[0]))
dt = np.array(solvers.qp(P, q, G, h, dims)['x']).reshape(-1)
where np.matmul(w, b) is C and np.matmul(w, np.log(dwi)) is d. Variables P and q are C and b multiplied by the transpose C.T to form a square multiplier matrix and constant vector, respectively. This works perfectly and I can find a solution.
I'd like to know whether this my approach makes mathematical sense. From my limited knowledge of linear algebra I know that a square matrix produces a unique solution, but is there is a way to run the same this to produce an overdetermined solution? I tried this but solver.qp said input Q needs to be a square matrix.
We can also parse in a dims argument to solver.qp, which I tried, but received the error:
use of function valued P, G, A requires a user-provided kktsolver.
How do I correctly setup dims?
Thanks a lot for any help. I'll try to clarify any questions as best as I can.

Power BI while loop

I'm trying to do a while loop in Power BI M language. But all of the logic are all over my head!
How would you translate a very simple loop like this into M language?
while X == True:
do abcdef
if Y == True:
end
Thanks very much!
Loops in M are probably best handled with the List.Generate function.
This article does a pretty good job at explaining how it works:
https://potyarkin.ml/posts/2017/loops-in-power-query-m-language/
Using this function, let's look at a more specific implementation of a while loop, say to find Fibonacci numbers less than 1000.
a = 1
b = 1
while b < 1000
b = a + b
a = b - a
would translate to M something like this:
let
data =
List.Generate(
() => [ a = 1, b = 1 ],
each [b] < 1000,
each [ b = [a] + [b], a = [b] ]
),
output = Table.FromRecords(data)[a]
in output
I'm not sure the best way to handle your break condition Y. It might depend on the specific problem.

Combinations using FOREACH in Pig

I would like to generate combinations in pig with the help of FOREACH. Is there any possible way to do this ?
My Input:
A
B
C
Objective:
A,B
A,C
B,C
Here's the sample which I have tried. This sample shows " Syntax error, unexpected symbol at or near '$0' ".
A = load '/test';
B = foreach A generate $0;
Combination = Cross A, B;
Combination_Filter = foreach Combination generate $0 < $1;
Please help me in resolving this. Thanks in Advance
Can you try the below options?
input:
A
B
C
Option1:
A = LOAD 'input' AS(f1:chararray);
B = LOAD 'input' AS(f2:chararray);
C = CROSS A,B;
D = FILTER C BY A::f1 < B::f2;
DUMP D;
Option2:
A = LOAD 'input' AS (f1:chararray);
B = FOREACH A GENERATE f1 AS (f2:chararray);
C = CROSS A,B;
D = FILTER C BY A::f1 < B::f2;
DUMP D;
Output:
(A,B)
(A,C)
(B,C)
It is not possible to do that using only foreach, the only way to achieve something like that would be with Sivasakthi's answer, or using a custom UDF. You can put all the registers in a bag with a group all, and then run the UDF.
The UDF is in this other question: How to turn (A, B, C) into (AB, AC, BC) with Pig?
The code would be something like:
A = load '/test';
A_grouped = group A all;
A_combinations = foreach A_grouped generate CombinationsUDF(A);

xlrd dynamic variables python

I can make it work like this:
book = xlrd.open_workbook(Path+'infile')
sheet = book.sheet_by_index(0)
A, B, C, D = ([] for i in range (4))
A = sheet.col_values(0)
B = sheet.col_values(1)
C = sheet.col_values(2)
D = sheet.col_values(3)
but what I want is to make it work like this:
dyn_var_list = [A, B, C, D]
assert(len(sheet.row_values(0))==len(dyn_var_list))
for index, col in enumerate(sheet.row_values(0)):
dyn_var_list[index].append(col)
however, so far I can only get one value in my lists, using the code above, which is due to the usage of "(0)" after the row_values I guess, but I don't know how to resolve this as of yet.
Try
for c in range(sheet.ncols):
for r in range(sheet.nrows):
dyn_var_list[c].append(sheet.cell(r,c).value)
Here sheet.nrows gives you the number of rows in the sheet.

Inconsistency in sql query output using OR after WHERE

I'm doing a query on a complex db:
SELECT *
FROM
table1,
table2,
table3,
table4,
table5,
table6,
table7,
table8
WHERE
a = b and
c = d and
e = d and
(
(strfldvar = 'BROKEN_ARROW' AND x = g)
OR (strfldvar = 'BROKEN_BOX' AND y = g)
) and
f = h and
i = j
Only works when strfldvar = 'BROKEN_BOX' and not when strfldvar = 'BROKEN_ARROW'. When I replace
(
(strfldvar = 'BROKEN_ARROW' AND x = g)
OR (strfldvar = 'BROKEN_BOX' AND y = g)
) and
with either x = g and or y = g and it works fine in two seperate queries runs like that. The error message for the case strfldvar = 'BROKEN_ARROW' is:
ORA-01013: user requested cancel of current operation
Before this error message comes the computer goes into deep thought for I guess 2 minutes.
What am I doing wrong here?
f.y.i. I looked at the names of the fields of the of the two seperate runs and they appear idendical. I mean the scema of the output looks the same for both. But I'm not 100% sure they are the same, if that matters i.e.
Thanks for your help
When strfldvar = 'BROKEN_ARROW' AND x = g (or if strfldvar is not BROKEN_ARROW or BROKEN_BOX), the y = g part is not evaluated, which seems to be causing the query to run for longer than you expect - until it's eventually killed by you, your client or resource limits. I suspect that's the only join condition for whichever table y is from, so you end up with a cartesian product.
When strfldvar = 'BROKEN_BOX' then both x = g and y = g will be evaluated, so you wouldn't get the same cartesian product, against either of the tables providing x and y.
If you are essentially deciding which table to include in the query based on that flag then you'll need to redesign this; possibly with a union of two queries, one which joins to x and the other on y; or with separate queries and you decide which to run; or maybe even with outer joins. But it depends on what you're really trying to do and what the data looks like. The code you have shown is a too generic to guess what will be appropriate.