einops not equivalent to torch.chunk? - torch

I'm trying to replicate the following 2 lines in einops:
emb = emb[..., None, None]
cond_w, cond_b = th.chunk(emb, 2, dim=1)
So far, I've managed to get:
emb = rearrange(emb, "b (c h w) -> b c h w", w=1, h=1)
cond_w, cond_b = th.chunk(emb, 2, dim=1)
This works fine.
But, when I do:
emb = rearrange(emb, "b (c h w) -> b c h w", w=1, h=1)
cond_w, cond_b = rearrange(emb, "b (split c) ... -> b split c ...", split=2)
The output is not the same. (Even though the shapes are).
Does anyone know what's going on here?

Solution:
cond_w, cond_b = rearrange(b_t, "b (split c) ... -> split b c ...", split=2)

Related

Run multiple tasks in parallel

I know it is possible to run 1 set of tasks in parallel. For example, something like this:
begin >> [A, B, C, D,E] >> end
would run A, B, C, D, E all in parallel. However, I want to do something like this such that after begin, there are two workflows running in parallel. Something like:
A -> B -> C
begin -> -> end
D -> E -> F
What would be the correct syntax to achieve this?
Every split is like a starting point.
start_dag = start_task()
end_dag = end_task()
a = DummyOperator(task_id="a")
b = DummyOperator(task_id="b")
c = DummyOperator(task_id="c")
d = DummyOperator(task_id="d")
e = DummyOperator(task_id="e")
f = DummyOperator(task_id="f")
(start_dag >> [a, d])
(a >> b >> c >> end_dag)
(d >> e >> f >> end_dag)

Find equivalent value on different ranges

So...
I've been banging my head on the wall over this problem for a few days now, but still couldn't find a solution.
I have two ranges of numbers
A -> B
C -> D
A given number (x) is on the A -> B range.
I need to find it's equivalent in the C -> D range.
eg:
A -> B = (2 -> 4)
C -> D = (-148 -> -50)
x = 2.3
What is the equivalent value on the (-148 -> -50) range?
Your requirements are a bit loose, but I would be tempted to believe you want to find an affine transformation from the interval [2;4] to [-148;-50].
Calling f(x) = a.x + b this transformation, you have:
f(2) = 2.a + b = -148
f(4) = 4.a + b = -50
=> 2.f(2) = 4.a + 2.b = -296
=> 2.f(2) - f(4) = b = -246
=> a = (-148 - b)/2 = 49
=> f(x) = 49.x - 246
So the point you are looking for would be f(2.3) = -133.3
You can use ((X - A) * (D - C) / (B - A)) + C.
Size of first range is: B - A
Size of second range is: D - C
Ratio between (X - A) and (Y - C) should be proportional to that of (B - A) and (D - C).

Simplifying 5-var Boolean SOP Expression using the Laws and Properties

I have this question that is messing me up because I am not getting that where should I start, which terms should I pick at the beginning? Because this confusing expression does not even let me take the common as it makes no sense. Also, it does not even let me remove compliments (using the Laws) as it also does not make any sense. Please help me in this, at least just guide me what should I do? From where should I start? I would be really grateful.
The Explanation of Symbols I used to write the expression:
! : NOT Gate
+ : OR Gate
. (dot) : AND Gate
Boolean Expression:
A.!B.E + !(B.C).D.!E + !(C.D).E+!A.D.!E + A.!(C.D).E + A.E + A.B.!E + !(A.C) + B.C.!D
I have used an online expression simplifier and that gave me the following answer:
!A + B + !C + D + E
But how the above long expression has been simplified in this short one? I know the Laws and Properties but I am not getting that how should I start simplifying the long one? Which terms should I see first? Kindly anyone please help me.
(This is a direct answer to your comment, and a side-ways answer to your main question. In short, use a different method to get the desired simplified expression.)
You have a complicated expression, but one that uses only 5 logical variables. In this problem it would be much easier to build a truth table, which would have just 2^5 = 32 rows. You could look at the results and use those to build a simplified, equivalent expression. This does not use "the laws and properties" that your original question requires, but it is a standard technique to simplify Boolean expressions.
You should have learned how to build a truth table in just about any Discrete Mathematics class. In short, you make a table where each element in each row is a T for True or F for False. The rows contain all possible combinations of Ts and Fs. For 5 variables this would use 2^5 = 32 rows. For each row, you assign the first value to A, the second to B, etc. You then evaluate the expression for those values and write the result at the end of the line.
This can be done by hand, but your expression is complicated enough that we could avoid that. Here is a Python 3 script that prints the desired table. Note that Python has the product() function which simplifies getting all possible combinations of Ts and Fs. This script used B[] to convert a Boolean value to a single character T or F.
from itertools import product
"""Make a truth table for the Boolean expression
A.!B.E + !(B.C).D.!E + !(C.D).E+!A.D.!E + A.!(C.D).E + A.E + A.B.!E + !(A.C) + B.C.!D
"""
B = ('F', 'T')
print('A B C D E : Result')
print('- - - - - : ------')
for a, b, c, d, e in product((True, False), repeat=5):
print(B[a], B[b], B[c], B[d], B[e], end=' : ')
print(B[
(a and not b and e)
or (not (b and c) and d and not e)
or (not (c and d) and e)
or (not a and d and not e)
or (a and not (c and d) and e)
or (a and e)
or (a and b and not e)
or (not (a and c))
or (b and c and not d)
])
Here are the results:
A B C D E : Result
- - - - - : ------
T T T T T : T
T T T T F : T
T T T F T : T
T T T F F : T
T T F T T : T
T T F T F : T
T T F F T : T
T T F F F : T
T F T T T : T
T F T T F : T
T F T F T : T
T F T F F : F
T F F T T : T
T F F T F : T
T F F F T : T
T F F F F : T
F T T T T : T
F T T T F : T
F T T F T : T
F T T F F : T
F T F T T : T
F T F T F : T
F T F F T : T
F T F F F : T
F F T T T : T
F F T T F : T
F F T F T : T
F F T F F : T
F F F T T : T
F F F T F : T
F F F F T : T
F F F F F : T
We see that the result is always T except for the single line T F T F F. This means your expression is true unless A is True, B is False, C is True, and D and E are False. So we can simplify your expression (using your notation) to
!(A.!B.C.!D.!E)
A simple use of DeMorgan's laws changes this to normal form:
!A + B + !C + D + E
which is what you wanted.

Maths Two Percentage Increases - Any Takers

Maths Puzzle
A = 10 (COST)
B = 20 (Sell Price)
C = 15% (Fee)
D = Tax at 20% or 1/6th if you are taking it away
E = Margin
B x C = 3.60
B / 6 in Reverse = 3.33
E = B - A - (B x C) - (B / 6) = 3.07
Ok The above works correct when i i provide B Sell Price
I want a formula that will Give me E, If i say E = 3.07 It says B = 20
How would i do that, the math
Any boffins can help
Thanks
First:
You did not use D: I suppose instead of B / 6, it should read B x D, where D can be either 20% or 16,666...%
There is a mistake: B x C is not 3.60 for the given values (B=20, C=15%), it is 3.
This is a matter of mathematical deduction:
Given the equation:
E = B - A - (B x C) - (B x D)
Add A to both sides:
E + A = B - (B x C) - (B x D)
Isolate B:
E + A = B x (1 - C - D)
Divide both sides by (1 - C - D). Condition: C + D cannot equal 100%
(E + A) / (1 - C - D) = B
So there is your formula for calculating B. Take note of the condition: this only holds true when C + D is not equal to 100%.

Expand an seq into individual scalars

I want to feed the members of a lazy seq produced by map as individual arguments to another function. Is there a function that splices a (lazy) seq?
Use apply.
(defn f [a b c d e]
(str "a = " a " b = " b " c = " c " d = " d " e = " e))
(println (apply f (range 5)))
;; prints: a = 0 b = 1 c = 2 d = 3 e = 4
As you can see, function f takes 5 arguments and (range 5) returns a lazy seq of 5 arguments.
Just make sure the size of the seq is the same as the amount of arguments expected by the function, or you will get an exception at runtime.

Resources