LightFM recommendation: Inconsistent error with interaction data - python

I have the following basic code with the LightFM recommendation module:
# Interactions
A=[0,1,2,3,4,4] # users
B=[0,0,1,2,2,3] # items
C=[1,1,1,1,1,1] # weights
matrix = sparse.coo_matrix((C,(A,B)),shape=(max(A)+1,max(B)+1))
# Create model
model = LightFM(loss='warp')
# Train model
model.fit(matrix, epochs=30)
# Predict
scores = model.predict(1, np.array([0,1,2,3]))
print(scores)
This returns the following error:
> C:\Program
> Files\Python\Python36\lib\site-packages\numpy\core\_methods.py:32:
> RuntimeWarning: invalid value encountered in reduce return
> umr_sum(a, axis, dtype, out, keepdims) Traceback (most recent call
> last): File "run.py", line 15, in <module>
> model.fit(matrix, epochs=100) File "C:\Program Files\Python\Python36\lib\site-packages\lightfm\lightfm.py", line 476,
> in fit
> verbose=verbose) File "C:\Program Files\Python\Python36\lib\site-packages\lightfm\lightfm.py", line 580,
> in fit_partial
> self._check_finite() File "C:\Program Files\Python\Python36\lib\site-packages\lightfm\lightfm.py", line 410,
> in _check_finite
> raise ValueError("Not all estimated parameters are finite," ValueError: Not all estimated parameters are finite, your model may
> have diverged. Try decreasing the learning rate or normalising feature
> values and sample weights
Strangely enough, making some changes in the interaction data makes it work, as with:
# Interactions
A=[0,1,2,3,4,4]
B=[0,0,1,2,2,10] # notice the 10 here
C=[1,1,1,1,1,1]
Could anyone help me with that please?

#Predict
scores = model.predict(1, np.array([0,1,2,3]))
print(scores)
[-0.17697991 -0.55117112 -0.37800685 -0.57664376]
It works fine for me, update the lightFM version?

Related

How to solve this error? RuntimeError: indices should be either on cpu or on the same device as the indexed tensor (cpu)

This is WongKinYiu / PyTorch_YOLOv4 problem. I found many solutions of this problem in YOLOv7, and no solution in YOLOv4. Since, the community here is more active. Thus, I try to ask here also.
This is the link to WongKinYiu loss.py
Traceback (most recent call last):
File "/content/PyTorch_YOLOv4/train.py", line 537, in
train(hyp, opt, device, tb_writer, wandb)
File "/content/PyTorch_YOLOv4/train.py", line 288, in train
loss, loss_items = compute_loss(pred, targets.to(device), model) # loss scaled by batch_size
File "/content/PyTorch_YOLOv4/utils/loss.py", line 69, in compute_loss
tcls, tbox, indices, anchors = build_targets(p, targets, model) # targets
File "/content/PyTorch_YOLOv4/utils/loss.py", line 151, in build_targets
a, t = at[j], t.repeat(na, 1, 1)[j] # filter
RuntimeError: indices should be either on cpu or on the same device as the indexed tensor (cpu)

What is the ideal way, in tensorflow, of feeding the output of a model back into itself, for predicting data that changes over time?

I am working on a model that trains on simulation data, which should ideally be able to predict N timesteps forward from a given state in a simulation. I have attempted to model this by feeding the output of the model back into itself N times, where N is a hyperparameter of the model. I have done this in the call function of the tensorflow.keras.Model() class.
The relevant code:
def call(self, inputs):
x = inputs[0]
outputs = tf.TensorArray(
dtype=tf.float32, size=0, dynamic_size=True, infer_shape=False
)
window = inputs[1]
for i in tf.range(window):
x = self.model(x)
outputs = outputs.write(i, x)
outputs = tf.transpose(outputs.stack(), [1, 2, 3, 0, 4])
return outputs
This works, and the model trains, but i want to save the model using the tensorflow.keras.Model.save() function. Trying this leads to the following error:
Traceback (most recent call last):
File "/zhome/22/4/118839/Masters_Thesis/Model_files/Unet.py", line 562, in <module>
model_.save(savepath + "/saved_model/Model")
File "/zhome/22/4/118839/Masters_Thesis/Menv/lib/python3.9/site-packages/keras/utils/traceback_utils.py", line 70, in error_handler
raise e.with_traceback(filtered_tb) from None
File "/appl/python/3.9.11/lib/python3.9/contextlib.py", line 126, in __exit__
next(self.gen)
File "/zhome/22/4/118839/Masters_Thesis/Model_files/Unet.py", line 485, in call
for i in tf.range(4):
tensorflow.python.framework.errors_impl.OperatorNotAllowedInGraphError: Iterating over a symbolic `tf.Tensor` is not allowed: AutoGraph did convert this function. This might indicate you are trying to use an unsupported feature.
Is there a better way of doing what I'm trying to do? Any other threads I have found recommend using the tf.map_fn() function, but this does not work for me due to the sequential nature of the model. Any help is appreciated!

TypeError although same shape: if not (target.size() == input.size()): 'int' object is not callable

This is the error message I get. In the first line, I output the shapes of predicted and target. From my understanding, the error arises from those shapes not being the same but here they clearly are.
torch.Size([6890, 3]) torch.Size([6890, 3])
Traceback (most recent call last):
File "train.py", line 251, in <module>
main()
File "train.py", line 230, in main
train(net, training_dataset, targets, device, criterion, optimizer, epoch, args.epochs)
File "train.py", line 101, in train
loss = criterion(predicted, target.detach().cpu().numpy())
File "/home/hb119056/.local/lib/python3.6/site-packages/torch/nn/modules/module.py", line 493, in __call__
result = self.forward(*input, **kwargs)
File "/home/hb119056/.local/lib/python3.6/site-packages/torch/nn/modules/loss.py", line 443, in forward
return F.mse_loss(input, target, reduction=self.reduction)
File "/home/hb119056/.local/lib/python3.6/site-packages/torch/nn/functional.py", line 2244, in mse_loss
if not (target.size() == input.size()):
TypeError: 'int' object is not callable
I hope all the relevant context information is provided and if not, please let me know. Thanks for any suggestions!
EDIT: This is the part of the code where this error occurs:
target = torch.from_numpy(np.load(file_dir + '/points/points{:03}.npy'.format(i))).to(device)
rv = torch.zeros(12 * outputs.shape[0])
for j in [x for x in range(10) if x != i]:
source = torch.from_numpy(np.load(file_dir + '/points/points{:03}.npy'.format(j))).to(device)
rv = factor.ransac(source, target, prob, n_iter, tol, device) # some self-written RANSAC-like method
predicted = factor.predict(source, rv, outputs)
print(target.shape, predicted.shape)
loss = criterion(predicted, target.detach().cpu().numpy()) ## error occurs here
criterion is nn.MSELoss().
A little bit late but maybe it will help someone else. Just solved the same problem for myself.
As Alpha said in his answer we cannot call .size() for a numpy array.
But we can call .size() for a tensor.
Therefore, we need to make our target a tensor. You can do it like this:
target = torch.from_numpy(target)
I'm using GPU, so I also needed to send my target to GPU. You can do it like this:
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
target = target.to(device)
And then the loss function must work perfectly.
It probably means that you are trying to call a method when a property with the same name is available. If this is indeed the problem, the solution is easy. Simply change the method call into a property access.
If you are comparing in the following way:
compare = (X.method() == Y.method())
Change it to:
compare = (X.method == Y.method)
If this does not answer your question, kindly share the code which you have used to compare the shapes.
that's because your target is a numpy object
File "train.py", line 101, in train:
target.detach().cpu().numpy()
in your code change the target type to numpy.
TLDR try change
loss = criterion(predicted, target.detach().cpu().numpy()) ## error occurs here
to
loss = criterion(predicted, target) ## error occurs here
for example:
In [6]: b = np.ones(3)
In [7]: b.size
Out[7]: 3
In [8]: b.size()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-8-365705555409> in <module>
----> 1 b.size()
TypeError: 'int' object is not callable

AttributeError: 'ProgbarLogger' object has no attribute 'log_values'

I have tried to run this segmentation model using spyder.
When I run data.py, I obtain this message
File "C:/Users/achaire/Downloads/Compressed/ultrasound-nerve-segmentation-master/ultrasound-nerve-segmentation-master/data.py", line 19, in create_train_data
imgs = np.ndarray((total, image_rows, image_cols), dtype=np.uint8)
TypeError: 'float' object cannot be interpreted as an integer
The code is:
**14** def create_train_data():
**15** train_data_path = os.path.join(data_path, 'train')
**16** images = os.listdir(train_data_path)
**17** total = len(images) / 2
**18** imgs = np.ndarray((total, image_rows, image_cols), dtype=np.uint8)
**19** imgs_mask = np.ndarray((total, image_rows, image_cols), dtype=np.uint8)
I have replaced line 17 by total = int(len(images) / 2) and np.uint8 by float in lines 18 and 19. The problem is solved.
When I run train.py I have this message
runfile('C:/Users/achaire/Downloads/Compressed/ultrasound-nerve-segmentation-master/ultrasound-nerve-segmentation-master/train.py', wdir='C:/Users/achaire/Downloads/Compressed/ultrasound-nerve-segmentation-master/ultrasound-nerve-segmentation-master')
------------------------------
Loading and preprocessing train data...
------------------------------
------------------------------
Creating and compiling model...
------------------------------
C:\Users\achaire\Anaconda3\lib\site-packages\numpy\core\fromnumeric.py:3118: RuntimeWarning: Mean of empty slice.
out=out, **kwargs)
C:\Users\hamdi\Anaconda3\lib\site-packages\numpy\core\_methods.py:140: RuntimeWarning: Degrees of freedom <= 0 for slice
keepdims=keepdims)
------------------------------
Fitting model...
------------------------------
C:/Users/achaire/Downloads/Compressed/ultrasound-nerve-segmentation-master/ultrasound-nerve-segmentation-master/train.py:119: UserWarning: The `nb_epoch` argument in `fit` has been renamed `epochs`.
callbacks=[model_checkpoint])
Train on 0 samples, validate on 0 samples
Epoch 1/20
Traceback (most recent call last):
File "<ipython-input-15-f713d62eb4dc>", line 1, in <module>
runfile('C:/Users/achaire/Downloads/Compressed/ultrasound-nerve-segmentation-master/ultrasound-nerve-segmentation-master/train.py', wdir='C:/Users/achaire/Downloads/Compressed/ultrasound-nerve-segmentation-master/ultrasound-nerve-segmentation-master')
File "C:\Users\achaire\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
execfile(filename, namespace)
File "C:\Users\achaire\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/achaire/Downloads/Compressed/ultrasound-nerve-segmentation-master/ultrasound-nerve-segmentation-master/train.py", line 153, in <module>
train_and_predict()
File "C:/Users/achaire/Downloads/Compressed/ultrasound-nerve-segmentation-master/ultrasound-nerve-segmentation-master/train.py", line 119, in train_and_predict
callbacks=[model_checkpoint])
File "C:\Users\achaire\Anaconda3\lib\site-packages\keras\engine\training.py", line 1039, in fit
validation_steps=validation_steps)
File "C:\Users\achaire\Anaconda3\lib\site-packages\keras\engine\training_arrays.py", line 217, in fit_loop
callbacks.on_epoch_end(epoch, epoch_logs)
File "C:\Users\achaire\Anaconda3\lib\site-packages\keras\callbacks.py", line 79, in on_epoch_end
callback.on_epoch_end(epoch, logs)
File "C:\Users\achaire\Anaconda3\lib\site-packages\keras\callbacks.py", line 338, in on_epoch_end
self.progbar.update(self.seen, self.log_values)
AttributeError: 'ProgbarLogger' object has no attribute 'log_values'
I have the last version of anaconda, keras and python ...
Answer 1:
If the error only occurs when you use smaller datasets, you’re very likely using datasets small enough to not have a single sample in the validation set.
Thus it cannot calculate a validation loss.
Answer 2:
I up-voted the previous answer as it gave me the insight to verify the data and inputs to the fit_generator function and find out what the root cause of the issue actually was. In summary, in cases where my dataset was small, I calculated validation_steps and steps_per_epoch which turned out to be zero (0) which caused the error.
I suppose the better longer-term answer, perhaps for the Keras team, is to cause an error/exception in fit_generator when these values are zero, which would probably lead to a better understanding about how to address this issue.
Answer 3:
The error occurs to us because we forgot to set validation_data in fit() method, while used 'callbacks': [keras.callbacks.EarlyStopping(monitor='val_loss', patience=1)],
Code causing error is:
self.model.fit(
x=x_train,
y=y_train,
callbacks=[keras.callbacks.EarlyStopping(monitor='val_loss', patience=1)],
verbose=True)
Adding validation_data=(self.x_validate, self.y_validate), in fit() fixed:
self.model.fit(
x=x_train,
y=y_train,
callbacks=[keras.callbacks.EarlyStopping(monitor='val_loss', patience=1)],
validation_data=(x_validate, y_validate),
verbose=True)
Answer 4:
This error occurs due to the smaller dataset, to resolve this, increase the train times and split the train set in 80:20.
Reference: https://inneka.com/ml/kr/keras-early-stopping-callback-error-val_loss-metric-not-available/
I got the same error and I fixed it by setting verbose = 0 in while training.
I had this same problem. If the problem is during your fit_generator, then there is a high chance that you have some tiny bug in your data_generator code. Debug your fit_generator.

signed integer is greater than maximum in scikit-learn in python

I am working on sentiment analysis of around 30,000 tweets. python version is 2.7 on linux. In the training phase I am using nltk as a wrapper for sklearn library to apply different Classifiers such as Naive Bayes, LinearSVC, Logistic regression , etc.
It works fine when the number of tweets are like 10,000 but now I received error for 30,000 tweets on classifying Bigrams with Multinomial naive bayes in sklearn. Here is part of the implementation code after pre-processing and dividing to train and test sets :
import nltk
from nltk.classify.scikitlearn import SklearnClassifier
from sklearn.naive_bayes import MultinomialNB,
training_set = nltk.classify.util.apply_features(extractFeatures, trainTweets)
testing_set = nltk.classify.util.apply_features(extractFeatures, testTweets)
MNB_classifier = SklearnClassifier(MultinomialNB())
MNB_classifier.train(training_set)
MNBAccuracy = nltk.classify.accuracy(MNB_classifier, testing_set)*100
print "-------- MultinomialNB --------"
print "RESULT : Matches " + str(int((testSize*MNBAccuracy)/100)) + ":"+ str(testSize)
print "MNB accuracy percentage:" + str(MNBAccuracy)
print ""
here the Error:
Traceback (most recent call last):
File "/home/sb402747/Desktop/Sentiment/sentiment140API/analysing/Classifier.py", line 83, in <module>
MNB_classifier.train(training_set)
File "/home/sb402747/.local/lib/python2.7/site-packages/nltk/classify/scikitlearn.py", line 115, in train
X = self._vectorizer.fit_transform(X)
File "/home/sb402747/.local/lib/python2.7/site-packages/sklearn/feature_extraction/dict_vectorizer.py", line 226, in fit_transform
return self._transform(X, fitting=True)
File "/home/sb402747/.local/lib/python2.7/site-packages/sklearn/feature_extraction/dict_vectorizer.py", line 176, in _transform
indptr.append(len(indices))
OverflowError: signed integer is greater than maximum
I guess the reason is because the number of indices in array is more that the maximum allowed for it on dict_vectore.py. I even tried to change the type of indices in dict_vectorizer.py from i to l but it didn't solve my problem and received this error:
Traceback (most recent call last):
File "/home/sb402747/Desktop/Sentiment/ServerBackup26-02-2016/analysing/Classifier.py", line 84, in <module>
MNB_classifier.train(training_set)
File "/home/sb402747/.local/lib/python2.7/site-packages/nltk/classify/scikitlearn.py", line 115, in train
X = self._vectorizer.fit_transform(X)
File "/home/sb402747/.local/lib/python2.7/site-packages/sklearn/feature_extraction/dict_vectorizer.py", line 226, in fit_transform
return self._transform(X, fitting=True)
File "/home/sb402747/.local/lib/python2.7/site-packages/sklearn/feature_extraction/dict_vectorizer.py", line 186, in _transform
shape=shape, dtype=dtype)
File "/rwthfs/rz/SW/UTIL.common/Python/2.7.9/x86_64/lib/python2.7/site-packages/scipy/sparse/compressed.py", line 88, in __init__
self.check_format(full_check=False)
File "/rwthfs/rz/SW/UTIL.common/Python/2.7.9/x86_64/lib/python2.7/site-packages/scipy/sparse/compressed.py", line 167, in check_format
raise ValueError("indices and data should have the same size")
ValueError: indices and data should have the same size
then discarded it and changed it back to i again. How can I solve this problem?
Hmm, looks like here:
File "/home/sb402747/.local/lib/python2.7/site-packages/nltk/classify/scikitlearn.py", line 115, in train
X = self._vectorizer.fit_transform(X)
nltk demands too big matrix as a result.
Maybe you can change it somehow, for example minimize number of features (words) in your text, or request for this result in two passes?
Also, are you trying to do this on latest numpy/scipy/scikit-learn stable releases?
Read this too: https://sourceforge.net/p/scikit-learn/mailman/message/31340515/

Categories

Resources