Tag Archives: neural network

TypeError(‘Keyword argument not understood:‘, ‘***‘) in keras.models load_model

TypeError(‘Keyword argument not understood:’, ‘***’) in keras.models load_ model

1.Problem description

    1. after training on Google colab, model.save (filepath)_ Save) and then use after saving
from keras.models import load_model
model = load_model(model_file)
# Error: TypeError: ('Keyword argument not understood:', 'step_dim')

2.Solutions

      1. method 1: confirm whether the versions of keras and TF are different twice. Someone’s solution: I only solved it by upgrading tensorflow and keras on the local computer at the same time

pip install --upgrade tensorflow
pip install --upgrade keras

What he means is the version problem. After training on Google’s colab, the model is saved locally. When it is called locally, the loading model will report an error due to the different versions of the packages in the two environments
then you can adjust the version of the local related package.

Similar to the following answer, the version when the model is saved is inconsistent with the version when the model is loaded, which may cause this problem
then unify the versions

import tensorflow as tf
import keras

print(keras.__version__)
print(tf.__version__)

But mine is still read on the colab, and the environment is the same, so this method can’t solve my specific problem.

      1. method 2. Model.load_ Weights() only reads weights

 

      1. the general idea is that we start with models.load_ Model () reads the network and weight. Now, because of the keyword argument not understood in the custom model, we first build the model structure, and then model. Load_ Weights () reads weights, which can achieve our original purpose

 

      1. at present, I use this method to solve the problem of re reading and importing the parameters of the network structure model of the user-defined model

I also have this problem I’ve tried a lot of methods and found that this method can be used

# first,build model
model = TextAttBiRNN(maxlen, max_features, embedding_dims).get_model()
model.compile(loss='binary_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])
              
# second, load weights: I solved the problem with this:
model_file = "/content/drive/My Drive/dga/output_data/model_lstm_att_test_v6.h5"
model.load_weights(model_file)
# then,we will find the modle can be use.
# in this way,I avoided the previous questions.

The function of flatten layer in deep learning

The official account of WeChat

Flatten layer is implemented in Keras.layers.core . flatten() class.

effect:

Flatten layer is used to “flatten” the input, that is, to make the multi-dimensional input one-dimensional. It is often used in the transition from convolution layer to fully connected layer. Flatten does not affect the size of the batch.

example:

from keras.models import Sequential
from keras.layers.core import Flatten
from keras.layers.convolutional import Convolution2D
from keras.utils.vis_utils import plot_model


model = Sequential()
model.add(Convolution2D(64,3,3,border_mode="same",input_shape=(3,32,32)))
# now:model.output_shape==(None,64,32,32)

model.add(Flatten())
# now: model.output_shape==(None,65536)

plot_model(model, to_file='Flatten.png', show_shapes=True)

In order to better understand the function of flatten layer, I visualize this neural network, as shown in the figure below:

Python: CUDA error: an illegal memory access was accounted for

Error in pytorch1.6 training:

RuntimeError: CUDA error: an illegal memory access was encountered

The reason for the error is the same as that of the lower version of python (such as version 1.1)

Runtimeerror: expected object of backend CUDA but get backend CPU for argument https://blog.csdn.net/weixin_ 44414948/article/details/109783988

Cause of error:

The essence of this kind of error reporting is model and input data_ image、input_ Label) is not all moved to GPU (CUDA).
* * tips: * * when debugging, you must carefully check whether every input variable and network model have been moved to the GPU. I usually report an error because I have missed one or two of them.

resolvent:

Model, input_ image、input_ The example code is as follows:

model = model.cuda()
input_image = input_iamge.cuda()
input_label = input_label.cuda()

or

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = model.to(device)
input_image = input_iamge.to(device)
input_label = input_label.to(device)