Multiple if-else conditions in tensorflow - tensorflow

I have a float tensor with shape (1) whose value lies between 0.0 and 1.0.
I want to 'bin' the range in this tensor, as in:
if 0.0 < x < 0.2:
return tf.Constant([0])
if 0.2 < x < 0.4:
return tf.Constant([1])
if 0.4 < x < 0.6:
return tf.Constant([2])
if 0.6 < x:
return tf.Constant([3])
No idea how to do it!

You have not explained what will happen in the border points (0.2, 0.4, ...) and have not shown what do you want to output for x > 0.6, so my assumptions are:
closed open interval; a < x <= b
the same bin procedure continues till 1 with a step 0.2
For such a simple case you do not need if else condition (also it will be slow). You can achieve it with math and casting:
import tensorflow as tf
x = tf.constant(0.25)
res = tf.cast(5 * x, tf.int32)
with tf.Session() as sess:
print sess.run(res)

try tg.logical_and
the following example might help
b = tf.constant([5,2,-3,1])
c1 = tf.greater(b,0) # b>0
c2 = tf.less(b,5) # b<5
c_f = tf.logical_and(c1, c2) # 0 < b < 5
sess=tf.Session()
sess.run(c_f)

Related

Z3 ArithRef type: is there a way to show value once model evaluated?

Using Z3Py, once a model has been checked for an optimization problem, is there a way to convert ArithRef expressions into values?
Such as
y = If(x > 5, 0, 0.5 * x)
Once values have been found for x, can I get the evaluated value for y, without having to calculate again based on the given values for x?
Many thanks.
You need to evaluate, but it can be done by the model for you automatically:
from z3 import *
x = Real('x')
y = If(x > 5, 0, 0.5 * x)
s = Solver()
r = s.check()
if r == sat:
m = s.model();
print("x =", m.eval(x, model_completion=True))
print("y =", m.eval(y, model_completion=True))
else:
print("Solver said:", r)
This prints:
x = 0
y = 0
Note that we used the parameter model_completion=True since there are no constraints to force x (and consequently y) to any value in this model. If you have sufficient constraints added, you wouldn't need that parameter. (Of course, having it does not hurt.)

probability of sample of distribution

I am trying to generate a sample of 100 scenarios (X, Y) where both X and Y are normally distributed X=N(50,5^2), Y=N(30,2^2) and X and Y are correlated Cov(X,Y)=0.4.
I have been able to generate 100 scenarios with the Cholesky decomposition:
# We do a Cholesky decomposition to generate correlated scenarios
nScenarios = 10
Σ = [25 0.4; 0.4 4]
μ = [50, 30]
L = cholesky(Σ)
v = [rand(Normal(0, 1), nScenarios), rand(Normal(0, 1), nScenarios)]
X = reshape(zeros(nScenarios),1,nScenarios)
Y = reshape(zeros(nScenarios),1,nScenarios)
for i = 1:nScenarios
X[1, i] = sum(L.U[1, j] *v[j][i] for j = 1:nBreadTypes) + μ[1]
Y[1, i] = sum(L.U[2, j] *v[j][i] for j = 1:nBreadTypes) + μ[2]
end
However I need the probability of each scenario, i.e P(X=k and Y=p). My question would be, how can we get a sample of a certain distribution with the probability of each scenario?
Following the BatWannaBe explanation, normally I would do it like this:
julia> using Distributions
julia> d = MvNormal([50.0, 30.0], [25.0 0.4; 0.4 4.0])
FullNormal(
dim: 2
μ: [50.0, 30.0]
Σ: [25.0 0.4; 0.4 4.0]
)
julia> point = rand(d)
2-element Vector{Float64}:
52.807189619051485
32.693811008760676
julia> pdf(d, point)
0.0056519503173830515

Why is the result of trigonometric function calculation different?

I calculated three methods of the following with Numpy.
Avoiding the circle periodicity, I given the range is 0 to +180.
The calculation results of the three methods should match.
However, all calculation results are different.
Why is this?
degAry = []
sumDeg = 0
cosRad = 0
sinRad = 0
LEN = 300
RAD2DEG = 180.0 / PI # 57.2957795
for i in range(LEN):
deg = random.uniform(0,180)
rad = np.deg2rad(deg)
degAry.append(deg)
sumDeg += deg
cosRad += np.cos(rad)
sinRad += np.sin(rad)
print(np.arctan2( sinRad/LEN, cosRad/LEN ) * RAD2DEG) # 88.39325364335279
print(np.sum(degAry)/LEN) # 88.75448888951954
print(sumDeg/LEN) # 88.75448888951951
What makes you think that the mean angle and the angle of the mean vector should be the same? This is correct only for n = 1,2, for n = 3 degAry = [0, 90, 90] is easily verified to be a counter example: mean of the angles is 60 with tan = sqrt(3), mean vector is (1/3 2/3) corresponding to tan = 2.
EDIT
Mean of circular quantities
suggesting that the sin, cos approach is best.
Refactoring your code to use numpy exclusively. The two methods are different, however, the first two using RAD2DEG or the np.degrees yield the same results. The latter which used the sum of degrees divided by sample size differs.
It doesn't appear to be a summation issue (N=3000, sum in normal order, ascending then descending). They yield the same results
np.sum(deg) # 134364.25172174018
np.sum(np.sort(deg)) # 134364.25172174018
np.sum(np.sort(deg)[::-1]) # 134364.25172174018
I didn't carry it out with the summation for the cos and sin in radian form. I will leave that for others.
PI = np.pi
sumDeg = 0.
cosRad = 0.
sinRad = 0.
N = 30
RAD2DEG = 180.0 / PI # 57.2957795
deg = np.random.uniform(0, 90.0, N)
rad = np.deg2rad(deg)
sumDeg = np.sum(deg)
cosRad = np.sum(np.cos(rad))
sinRad = np.sum(np.sin(rad))
print(np.arctan2(sinRad/N, cosRad/N) * RAD2DEG)
print(np.degrees(np.arctan2(sinRad/N, cosRad/N)))
print(sumDeg/N)
Results for
> N = 1
> 22.746571717879792
> 22.746571717879792
> 22.746571717879792
>
> N= 30
> 48.99636699165551
> 48.99636699165551
> 49.000295118106884
>
> N = 300
> 44.39333460088003
> 44.39333460088003
> 44.44513528547155
>
> N = 3000
> 44.984167020219175
> 44.984167020219175
> 44.97574462726241

Scipy.optimize.minimize on trajectory optimization (Cost Functional Minimization)

I've posted a question asking about how to go about this problem in general here:
Trajectory Optimization for "Rocket" using scipy.optimize.minimize
Ideally I would like to just minimize the final time, but I could not get the optimizer to append the time to the variable that can be adjusted properly, so I decided that I would just try and minimize u^2 instead for now.
Here is the code:
# Code
t_f = 1.0
t = np.linspace(0., t_f, num = 10) # Time array for 1 second into the future with 0.01 increment
u = np.zeros(t.size) + 650
print(u)
g = -650
initial_position = 0
initial_velocity = 0
final_position = 100
final_velocity = 100
def car_dynamics(x):
# Create time vector
# t = np.linspace(0., t_f, num = 100) # Time array for 1 second into the future with 0.01 increment
# Integrate over entire time to find v as a function of t
a = x + g
v = int.cumtrapz(a, t, initial = 0) + initial_velocity
# Integrate v(t) to get s(t)
s = int.cumtrapz(v, t, initial = 0) + initial_position
return s, v
def constraint1(x): # Final state constraints (Boundary conditions)
s, v = car_dynamics(x)
print('c1', s[0] - initial_position)
return s[0] - initial_position
def constraint2(x): # Initial state constraints (initial conditions)
s, v = car_dynamics(x)
print('c2', v[0] - initial_velocity)
return v[0] - initial_velocity
def constraint3(x):
s, v = car_dynamics(x)
print('c3', s[-1] - final_position)
return s[-1] - final_position
def constraint4(x):
s, v = car_dynamics(x)
print('c4', v[-1] - final_velocity)
return v[-1] - final_velocity
def constraint5(x):
return x - 1000
def objective(x):
u2 = np.square(x)
return np.sum(u2)
cons = [{'type':'eq', 'fun':constraint1},
{'type':'eq', 'fun':constraint2},
{'type':'eq', 'fun':constraint3},
{'type':'eq', 'fun':constraint4}]
# {'type':'ineq', 'fun':constraint5}]
result = minimize(objective, u, constraints = cons, method = 'SLSQP', options={'eps':500, 'maxiter':1000, 'ftol':0.001, 'disp':True})
print(result)
The code runs but the optimizer fails. Here is the error from the output.
c1 0.0
c2 0.0
c3 -100.0
c4 -100.0
c1 0.0
c2 0.0
c3 -100.0
c4 -100.0
c1 0.0
c1 0.0
c1 0.0
c1 0.0
c1 0.0
c1 0.0
c1 0.0
c1 0.0
c1 0.0
c1 0.0
c1 0.0
c2 0.0
c2 0.0
c2 0.0
c2 0.0
c2 0.0
c2 0.0
c2 0.0
c2 0.0
c2 0.0
c2 0.0
c2 0.0
c3 -100.0
c3 -73.76543209876543
c3 -50.617283950617285
c3 -56.79012345679013
c3 -62.96296296296296
c3 -69.1358024691358
c3 -75.30864197530863
c3 -81.4814814814815
c3 -87.65432098765432
c3 -93.82716049382715
c3 -98.45679012345678
c4 -100.0
c4 -72.22222222222223
c4 -44.44444444444445
c4 -44.44444444444445
c4 -44.44444444444445
c4 -44.444444444444436
c4 -44.44444444444445
c4 -44.44444444444448
c4 -44.44444444444445
c4 -44.44444444444442
c4 -72.22222222222221
Singular matrix C in LSQ subproblem (Exit mode 6)
Current function value: 4225000.0
Iterations: 1
Function evaluations: 12
Gradient evaluations: 1
fun: 4225000.0
jac: array([1800., 1800., 1800., 1800., 1800., 1800., 1800., 1800., 1800.,
1800.])
message: 'Singular matrix C in LSQ subproblem'
nfev: 12
nit: 1
njev: 1
status: 6
success: False
x: array([650., 650., 650., 650., 650., 650., 650., 650., 650., 650.])
It seems the constraints aren't being met in some amount of iterations. Should I switch my objective function to contain the final velocity and final position? I've tried different step sizes and what not with the same exit code.
Is there a better way to use this function for what I am trying to get? I am trying to get the control vector u(t) over the entire interval from t0 to t_f, that way I can send these commands to a rocket for optimal control. For now I've simplified the optimization to a single axis, just to learn how to use the function. But as you can see I have not succeeded.
Similar examples would be extremely helpful, and I am open to other methods of optimization, as long as they are numerical, and relatively fast as I plan to implement this as a Model Predictive Controller in real-time eventually.
Your model has both algebraic and differential equations. You need a DAE solver to solve the above implicit ODE functions. One such package that I am aware is gekko. (https://github.com/BYU-PRISM/GEKKO)
Gekko specializes on dynamic optimization of linear, mixed integer and non linear optimization problems.
Below is an example rocket launch problem that minimizes the final time. Available at http://apmonitor.com/wiki/index.php/Apps/RocketLaunch
import numpy as np
import matplotlib.pyplot as plt
from gekko import GEKKO
# create GEKKO model
m = GEKKO()
# scale 0-1 time with tf
m.time = np.linspace(0,1,101)
# options
m.options.NODES = 6
m.options.SOLVER = 3
m.options.IMODE = 6
m.options.MAX_ITER = 500
m.options.MV_TYPE = 0
m.options.DIAGLEVEL = 0
# final time
tf = m.FV(value=1.0,lb=0.1,ub=100)
tf.STATUS = 1
# force
u = m.MV(value=0,lb=-1.1,ub=1.1)
u.STATUS = 1
u.DCOST = 1e-5
# variables
s = m.Var(value=0)
v = m.Var(value=0,lb=0,ub=1.7)
mass = m.Var(value=1,lb=0.2)
# differential equations scaled by tf
m.Equation(s.dt()==tf*v)
m.Equation(mass*v.dt()==tf*(u-0.2*v**2))
m.Equation(mass.dt()==tf*(-0.01*u**2))
# specify endpoint conditions
m.fix(s, pos=len(m.time)-1,val=10.0)
m.fix(v, pos=len(m.time)-1,val=0.0)
# minimize final time
m.Obj(tf)
# Optimize launch
m.solve()
print('Optimal Solution (final time): ' + str(tf.value[0]))
# scaled time
ts = m.time * tf.value[0]
# plot results
plt.figure(1)
plt.subplot(4,1,1)
plt.plot(ts,s.value,'r-',linewidth=2)
plt.ylabel('Position')
plt.legend(['s (Position)'])
plt.subplot(4,1,2)
plt.plot(ts,v.value,'b-',linewidth=2)
plt.ylabel('Velocity')
plt.legend(['v (Velocity)'])
plt.subplot(4,1,3)
plt.plot(ts,mass.value,'k-',linewidth=2)
plt.ylabel('Mass')
plt.legend(['m (Mass)'])
plt.subplot(4,1,4)
plt.plot(ts,u.value,'g-',linewidth=2)
plt.ylabel('Force')
plt.legend(['u (Force)'])
plt.xlabel('Time')
plt.show()

How to properly apply a lambda function into a pandas data frame column

I have a pandas data frame, sample, with one of the columns called PR to which am applying a lambda function as follows:
sample['PR'] = sample['PR'].apply(lambda x: NaN if x < 90)
I then get the following syntax error message:
sample['PR'] = sample['PR'].apply(lambda x: NaN if x < 90)
^
SyntaxError: invalid syntax
What am I doing wrong?
You need mask:
sample['PR'] = sample['PR'].mask(sample['PR'] < 90, np.nan)
Another solution with loc and boolean indexing:
sample.loc[sample['PR'] < 90, 'PR'] = np.nan
Sample:
import pandas as pd
import numpy as np
sample = pd.DataFrame({'PR':[10,100,40] })
print (sample)
PR
0 10
1 100
2 40
sample['PR'] = sample['PR'].mask(sample['PR'] < 90, np.nan)
print (sample)
PR
0 NaN
1 100.0
2 NaN
sample.loc[sample['PR'] < 90, 'PR'] = np.nan
print (sample)
PR
0 NaN
1 100.0
2 NaN
EDIT:
Solution with apply:
sample['PR'] = sample['PR'].apply(lambda x: np.nan if x < 90 else x)
Timings len(df)=300k:
sample = pd.concat([sample]*100000).reset_index(drop=True)
In [853]: %timeit sample['PR'].apply(lambda x: np.nan if x < 90 else x)
10 loops, best of 3: 102 ms per loop
In [854]: %timeit sample['PR'].mask(sample['PR'] < 90, np.nan)
The slowest run took 4.28 times longer than the fastest. This could mean that an intermediate result is being cached.
100 loops, best of 3: 3.71 ms per loop
You need to add else in your lambda function. Because you are telling what to do in case your condition(here x < 90) is met, but you are not telling what to do in case the condition is not met.
sample['PR'] = sample['PR'].apply(lambda x: 'NaN' if x < 90 else x)