value error happens when using GridSearchCV - python

I am using GridSearchCV to do classification and my codes are:
parameter_grid_SVM = {'dual':[True,False],
'loss':["squared_hinge","hinge"],
'penalty':["l1","l2"]
}
clf = GridSearchCV(LinearSVC(),param_grid=parameter_grid_SVM,verbose=2)
clf.fit(trian_data, labels)
And then, I meet the error
ValueError: Unsupported set of arguments: penalty='l1' is only supported when dual='false'., Parameters: penalty='l1', loss='hinge', dual=False
later on I change my code to :
clf = GridSearchCV(LinearSVC(penalty='l1',dual=False),verbose=2)
And I meet the error
TypeError: init() takes at least 3 arguments (3 given)
I also tried:
parameter_grid_SVM = {
'loss':["squared_hinge"]
}
clf = GridSearchCV(LinearSVC(penalty='l1',dual=False),param_grid=parameter_grid_SVM,verbose=2)
clf.fit(trian_data, labels)
However, I still have the error
ValueError: Unsupported set of arguments: penalty='l1' is only supported when dual='false'., Parameters: penalty='l1', loss='squared_hinge', dual=False
Anyone has idea what I should do to deal with that?

I also met this problem when doing sparse SVM. I find one piece of working demo code in this page SVM module explanation. Hope it might help.
clf = LinearSVC(loss='l2', penalty='l1', dual=False)

One option is to instruct GridSearchCV to set the score manually if model throws an exception using the error_score parameter. See my answer here.

Had a similar issue and in my case, it was writing twelve 12 instead of 'el two' l2 in some instances.

The code that produces this error message is here. I don't see what would cause this to only happen occasionally, but the bare else means that presumably it's something else other than just the penalty='l1', dual='false' combination.

Related

How to run a non-linear autoregression with exogenous inputs with sysidentpy?

I am trying to run a nonlinear autoregression with exogenous inputs (NARX) in Python.
This is my code
Step 1: import the libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sysidentpy.model_structure_selection import FROLS
from sysidentpy.basis_function import Polynomial, Fourier
from sysidentpy.metrics import root_relative_squared_error
from sysidentpy.utils.generate_data import get_siso_data
from sysidentpy.utils.display_results import results
from sysidentpy.utils.plotting import plot_residues_correlation, plot_results
from sysidentpy.residues.residues_correlation import compute_residues_autocorrelation, compute_cross_correlation
from sklearn.model_selection import train_test_split
Step 2: import the data
df=pd.read_excel(r"C:\Users\Action\Downloads\Python\Practice_Data\sorted_data v2.xlsx")
Step 3: Organize the data
target_column = ['public health care services']
predictors = list(set(list(df.columns))-set(target_column))
df[predictors] = df[predictors]/df[predictors].max()
Step 4: Step up the training and testing data
X = df[predictors].values
y = df[target_column].values
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30, random_state=40)
print(X_train.shape); print(X_test.shape)
Step 5: Create the NARX Neural Network Model
basis_function = Polynomial(degree=2)
model = FROLS(
basis_function=basis_function,
order_selection=True,
n_info_values=10,
extended_least_squares=False,
ylag=2, xlag=2,
info_criteria='aic',
estimator='least_squares',
)
Step 6: Apply fit the model
model.fit(X_train, y_train)
From step 6 I am experiencing an error
TypeError: fit() takes 1 positional argument but 3 were given
Step 7: Prediction
yhat = model.predict(X_test, y_test)
I am also experiencing an error
AttributeError: 'FROLS' object has no attribute 'final_model'
Step 8: Compute the RRSE
rrse = root_relative_squared_error(y_test, yhat)
print(rrse)
I am experiencing the following error
NameError: name 'yhat' is not defined
Well, I realise that this error is due to the error before it, so 'yhat' is not defined.
I would be grateful for any assistance.
I'm the developer of SysIdentPy and just found this question.
I hope you already solved it, but if not, here is the solution:
The first error you got
model.fit(X_train, y_train)
TypeError: fit() takes 1 positional argument but 3 were given
is due the fact you have to use keyword arguments instead of positional arguments. To fix it, just use:
model.fit(X=X_train, y=y_train)
All the other problems are consequences of the first one: without fiting the model you cannot predict and you will not have a final_model to access, for example.
I'll add a "check_fitted" method to give the users a more detailed message about this kind of error.
The use of keyword arguments instead of positional arguments was described in update v0.17.0 and the examples were adapted to follow this change in this same update, but this can be a common mistake and hard to understand without a propor error message if you havent read the docs.
Note: Its not related to your question, but you used the train_test_split method from sklearn to split your data. In a time series scenario this is usually (to not say always) wrong. I don't know what you were trying to do, but its worth checking this part too (take a look at https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.TimeSeriesSplit.html)
Hope it helps you.

"AssertionError: Cannot handle batch sizes > 1 if no padding token is > defined" and pad_token = eos_token

I am trying to finetune a pre-trained GPT2-model. When applying the respective tokenizer, I originally got the error message:
Using pad_token, but it is not set yet.
Thus, I changed my code to:
GPT2_tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
GPT2_tokenizer.pad_token = GPT2_tokenizer.eos_token
When calling the trainer.train() later, I end up with the following error:
AssertionError: Cannot handle batch sizes > 1 if no padding token is
defined.
Since I specifically defined the pad_token above, I expect these errors (or rather my fix of the original error and this new error) to be related - although I could be wrong. Is this a known problem that eos_token and pad_token somehow interfer? Is there an easy work-around?
Thanks a lot!
I've been running into a similar problem, producing the same error message you were receiving. I can't be sure if your problem and my problem were caused by the same issue, since I can't see your full stack trace, but I'll post my solution in case it can help you or someone else who comes along.
You were totally correct to fix the first issue you described with your tokenizer by setting its pad token with the code provided. However, I also had to set the pad_token_id of my model's configuration to get my GPT2 model to function properly. I did this in the following way:
# instantiate the configuration for your model, this can be imported from transformers
configuration = GPT2Config()
# set up your tokenizer, just like you described, and set the pad token
GPT2_tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
GPT2_tokenizer.pad_token = GPT2_tokenizer.eos_token
# instantiate the model
model = GPT2ForSequenceClassification(configuration).from_pretrained(model_name).to(device)
# set the pad token of the model's configuration
model.config.pad_token_id = model.config.eos_token_id
I suppose this is because the tokenizer and the model function separately, and both need knowledge of the ID being used for the pad token. I can't tell if this will fix your problem (since this post is 6 months old, it may not matter anyway), but hopefully my answer may be able to help someone else.

AttributeError: module 'tensorflow_core.python.keras.api._v2.keras.losses' has no attribute 'softmax_cross_entropy'

I have an AttributeError: module 'tensorflow_core.python.keras.api._v2.keras.losses' has no attribute 'softmax_cross_entropy' error when using tf.losses.softmax_cross_entropy. Could someone help me?
The tf.losses now point to tf.keras.losses. You can get identical behavior by using
tf.losses.categorical_crossentropy with from_logits set to True
Sometimes we encounter this error, especially when running on online binders like jupyter notebook. Instead of writing
tf.losses.softmax_cross_entropy
try
loss = 'softmax_cross_entropy'
or either of the below
tf.keras.losses.CategoricalCrossentropy()
loss = 'categorical_crossentropy'
You may also want to use from_logits=True as an argument - which shall look like
tf.keras.losses.CategoricalCrossentropy(from_logits=True)
while keep metrics something like
metrics=['accuracy']

Explain features from my keras object with lime R package

After obtaining accuracy metric from my keras binary classification model, I need know what the model made the predictions. So, I'm interested in variable importance. I use lime package.
library(lime)
explainer <- lime (
x = x_train,
model = model_keras,
bin_continuous = FALSE)
explanation <- explain (
x_test[1:20,], # Show first 20 samples
explainer = explainer,
n_labels = 1,
n_features = 5)
Explain function gives me the following error in py_get_attr_impl function: AttributeError: 'function' object has no attribute 'func_name'.
I have compiled keras model with R, but this Issue seems to be that error comes from Python version. Problems with Reticulate package?
It works with python 2.7 but generates error with python 3+.
Actually function attribute func_name was renamed in python 3+ to __name__.
lime package (models.R) has a line:
if (keras::get_layer(x, index = -1)$activation$func_name == 'linear')
I removed $func_name and the code worked for me.
I suppose this is not the best workaround, however the possible solution that comes to mind:
if (keras::get_layer(x, index = -1)$activation$__name__ == 'linear')
did not work with R.

Issues integrating Keras with Spearmint

When i am using spearmint to optimize the hyperparamaters for Keras models, for the first time it runs fine. But the second job onwards it always throws the following error.
<type 'exceptions.TypeError'>, TypeError('An update must have the same type as the original shared variable (shared_var=<TensorType(float32, matrix)>, shared_var.type=TensorType(float32, matrix), update_val=Elemwise{add,no_inplace}.0, update_val.type=TensorType(float64, matrix)).', 'If the difference is related to the broadcast pattern, you can call the tensor.unbroadcast(var, axis_to_unbroadcast[, ...]) function to remove broadcastable dimensions.'), <traceback object at 0x18a5c5710>)
I am using the following code to load the pre-created numpy array of the train data and test data. The following params are passed by the optimization python script. But the set of parameters work fine if run without spearmint.
def load_train_data(arg_type, params=None):
X_train1 = pickle.load(open(arg_type+"_train1","rb"))
X_train2 = pickle.load(open(arg_type+"_train2","rb"))
Y_train = pickle.load(open(arg_type+"_train_labels","rb"))
model=combined_model(X_train1,X_train2,Y_train,params)
X_test1 = pickle.load(open(arg_type+"_test1","rb"))
X_test2 = pickle.load(open(arg_type+"_test2","rb"))
Y_test = pickle.load(open(arg_type+"_test_labels","rb"))
loss = model.evaluate({'input1': X_test1,'input2': X_test2,'output':Y_test},batch_size=450)
return loss
The variables that I was setting with the spearmint, they had to be explicitly converted to basic python datatypes, using float(),int(). This helped in solving this issue.

Categories

Resources