TensorFlow incompatible function arguments in making Elbo - python

Pardon me for being lazy about this, but I'm not getting the answer anywhere.
I'm trying to replicate the exercise here in my own computer: https://github.com/tensorflow/probability/blob/main/tensorflow_probability/examples/jupyter_notebooks/Structural_Time_Series_Modeling_Case_Studies_Atmospheric_CO2_and_Electricity_Demand.ipynb
However, I'm getting stuck at this part:
# Build and optimize the variational loss function.
elbo_loss_curve = tfp.vi.fit_surrogate_posterior(
target_log_prob_fn=co2_model.joint_distribution(
observed_time_series=co2_by_month_training_data).log_prob,
surrogate_posterior=variational_posteriors,
optimizer=tf.optimizers.Adam(learning_rate=0.1),
num_steps=num_variational_steps,
jit_compile=True)
plt.plot(elbo_loss_curve)
plt.show()
I get:
TypeError: TF_TryEvaluateConstant_wrapper(): incompatible function arguments. The following argument types are supported:
1. (arg0: tensorflow.python.client._pywrap_tf_session.TF_Graph, arg1: tensorflow.python.client._pywrap_tf_session.TF_Output) -> object
Invoked with: <tensorflow.python.framework.c_api_util.ScopedTFGraph object at 0x0000024EDCF90C70>, <tensorflow.python.client._pywrap_tf_session.TF_Output object at 0x0000024EDD26DBB0>
Anyone knows what the cause is? I get that there's a problem with my input but i'm looking for a fix.

Related

`TypeError: missing 1 required positional argument: 'self'` Whitebox tools

I am attempting to use whitebox geospatial tools to analyze .tif files. Any whitebox tool I run however, raises the error: TypeError: missing 1 required positional argument: 'self'. I understand that this is a well-documented error within the stack overflow community, however, the way I understand the self argument, it is used in the creation of a class, which I am not doing as far as I can tell.
Additionally, upon the addition of the argument in an attempt to resolve the issue as various other stack answers have suggested, I receive a name error, stating that 'self' is not defined. Both cases are exemplified bellow.
Code:
from whitebox_tools import WhiteboxTools as wbt
print(wbt.list_tools())
Result:
TypeError: list_tools() missing 1 required positional argument: 'self'
Code (self argument added):
print(wbt.list_tools(self))
Result:
NameError: name 'self' is not defined
Please excuse my lack of understanding of the self argument. It stems from a further lack of understanding of Python classes. Either way, any resolution to this problem I can find has been to add the self argument which does not seem to work in this case.
Thank you for considering.
"TypeError: missing 1 required positional argument: 'self'" generally tends to mean you're attempting to call an instance method directly on a class.
WhiteboxTools looks like a class. You're importing it with an alias that makes it look like an instance, and attempting to use it as one.
Don't do that, but just instantiate an object of that class:
from whitebox_tools import WhiteboxTools
wbt = WhiteboxTools()
print(wbt.list_tools())

Can't specify initialization method in Exponential smoothing

When going through the exponential smoothing tutorial provided by statsmodels, I had an issue with specifying an initialization method in anaconda notebook. The error would return:
TypeError: init() got an unexpected keyword argument 'initialization_method'
It would reference the first line in the cell:
fit1 = Holt(air, initialization_method="estimated").fit(smoothing_level=0.8, smoothing_trend=0.2, optimized=False)
When looking for the solution, I downgraded my prompt-toolkit but that didn't do anything. I can't find anyone else having this problem, any ideas?

Type error: fit() takes 2 positional arguments but 3 were given

I am getting this error while working on question 4 (Chapter 2) from the "Hands on Machine learning" book. This is the question "Try creating a single pipeline that does the full data preparation plus the final prediction". The solution is available in Github link, but the solution is giving me the error mentioned in the title. I used the housing data for my example. Please help me.
I wrote this command and it fired the following error:
prepare_select_and_predict_pipeline = Pipeline([
('preparation', full_pipeline),
('feature_selection', TopFeatureSelector(feature_importances, k)),
('svm_reg', SVR(**rnd_search.best_params_))
])
prepare_select_and_predict_pipeline.fit(housing,housing_labels)
Error:
TypeError: fit() takes 2 positional arguments but 3 were given
I would like to attach the solution from Github
Solution of Question 2 from Github
But this isn't working for me. :(
Warning, I'm going to grossly simplify here, but I hope this will help.
In Python, if you call a function on an object, the object itself is always passed as the first argument (unless it is a static or class method). This is usually captured by a parameter we call self.
So, if you call object.function(), you are passing an argument to function, namely object itself.
class C:
def f(self):
print(self)
o = C()
o.f() # <__main__.C object at 0x7f1049993f28>
o.f('hello') # TypeError: f() takes 1 positional argument but 2 were given
In your case, you are calling prepare_select_and_predict_pipeline.fit(housing, housing_labels), so you are passing the function fit three arguments: prepare_select_and_predict_pipeline, housing and housing_labels.
If you check the definition of the fit method, you will probably find that it indeed only takes two arguments. I'm guessing the first one will be called self.

Running ode, dopri5 method, error: unsupported operand type 'ode'

I'm a beginning programmer and I would like to integrate a function using ode 'dopri5', but I don't think I'm doing it correctly. The reference wasn't much help and I'm having an error that I don't recognize. So, originally I was using odeint, and it was working fine. Here is that chunk of code:
Itmp = odeint(te.rhs, Itmp, [xLim[i], xLim[i+1]], mxstep=10000,
atol=1e-11, rtol=1e-11, args=(f,))[1]
And my attempt to integrate using dopri5 is this:
Itmp = ode(te.rhs).set_integrator('dopri5', max_step=10000,atol=1e-11, rtol=1e-11)
The error I get is saying that Itmp is type 'ode' while I need it to be a float, like the odeint gives me.
Here is the specific error, (I try to subtract Itmp from a float):
unsupported operand type(s) for -: 'ode' and 'float'
And when I use the python debugger and try to print out Itmp, it gives me
<scipy.integrate._ode.ode object at 0x10d6ab410>
And after I continue it stops with the above error. I'm guessing I don't have the ode command written out correctly. Any help would be greatly appreciated!
The return value of the constructor of the ode class is an instance object of type ode. At this point, no integration has taken place. For that you need to call the step functions of the integrator. After the step, the new state is in the y field of the ode object.
Consult the documentation of the ode class for further details.
You should have noted that you did not pass neither the initial conditions nor the end of the integration interval to the integrator.

How to use user defined metric in KernelDensity from scikit-learn (python)

I'm using scikit-learn (0.14) and trying to implement a user defined metric for my KernelDensity estimation.
Following code is an example how my code is structured:
def myDistance(x,y):
return np.sqrt(sum((x - y)**2))
dt=DistanceMetric.get_metric("pyfunc",func=myDistance)
kernelModel=KernelDensity(algorithm='ball_tree',metric='pyfunc')
kernelModel.fit(X)
According to the documentation, the BallTree algorithm should accept user defined metrics.
If I run this code the way given here, I get following error:
TypeError: __init__() takes exactly 1 positional argument (0 given)
The error seems to come from:
sklearn.neighbors.dist_metrics.PyFuncDistance.__init__
I don't understand this. If i check what 'dt' in the code above gives me, I get what I expect. dt.pairwise(X) returns the correct values.
What am I doing wrong?
Thanks in advance.
Solution is
kernelModel=KernelDensity(...,metric='pyfunc',metric_params={"func":myDistance})
The call to Distancemetric.get_metric is not necessary.
M

Categories

Resources