Keras (& tensorflow) often used for machine learning with python.
I didn't have an article summarizing how to deal with errors when using keras, so I'll summarize it based on the solved questions of teratail.
ImportError,AttributeError -Resolved by lowering or raising the version (ImportError, AttributeError) -Sometimes it can be solved by reinstalling (Import Error) -The file to be imported is incorrect (Attribute Error) ・ Tensorflow.keras and keras are different ・ Typo (case sensitive)
In keras, the influence of various library versions is large. If it is too old or too new, it may cause an Inport Error.
For AttributeError, refer to the following article.
[python] 5 things to do when "Attribute Error: module (object) ‘xxx’ has no attribute ‘yyy’ ”
ValueError
-Check the model summary model.summary () ・ Check the shape of the data with print (data.shape)
Most of the ValueErrors are that the dimensions of the data are different from the model's expectations. Check the shape of the data, and if it is different, use reshape () to shape the data.
Shapes A and B are incompatible ・ Whether the dimensions of the model output and the output data match
For example, when the output of the model is 3 (Dense (3), etc.) even though it should be a binary classification. You need to analyze the model with model.summary ().
expected ndim=A, found ndim=B -Since the input of Dense is a basic one-dimensional array, reshape or Flatten to format it into one dimension. ・ Or you need to change the input_shape -Input_shape does not include batch size -Reshape the image data so that it becomes (number of samples, height, width, channel) -For LSTM, it is necessary to set [Batch number, time axis, number of channels]
expected layer_name to have shape A dimensions but got array with shape B ・ Isn't RGB and black and white mistaken (in the case of images)? ・ Whether the dimensions of the input data and the model input match -Reshape to a tensor with 1 x number of elements (occurs when there is only one test data)
Input arrays should have the same number of samples as target arrays. Found A input samples and B target samples
・ Is the number of input data and output data correct?
ResourceExhaustedError ・ OOM (Out Of Memory, Out of Memory, Resource Exhausted Error) ・ Reduce batch size ・ Restart your PC
An error that occurs when the memory of the GPU or PC is insufficient. A common method is to increase memory or reduce processing.
Is the program stored in memory for some things? Sometimes it happens, so resetting it with a reboot may work (for example, if it worked).
・ Pre-process data ・ There is a problem with the input data itself ・ Is the forecast data correct? ・ Change hyperparameters ・ There are some things that cannot be learned even with machine learning
If it is image data, is it normalized ([0,255] → [-1,1]), is the input data or prediction data damaged, etc.
Anyway, you need to make sure that the data is accurate. If the data is not accurate, neither learning nor prediction is of course accurate.
Also, if the types of data are insufficient, the learning accuracy will not increase. If the weather forecast can be made only by the temperature, the forecaster will not have any trouble.
And there are cases where machine learning cannot be used for learning. For example, the dice rolls are random and cannot be predicted using machine learning. There are some directions.
This is how to deal with the error.
I'm not that professional myself, so there may be mistakes in the content. In that case, please point out in the comments.
Recommended Posts