Keras: weighted binary crossentropy

Normally, the minority class will have a higher class weight. It’ll be better to use one_weight=0.89, zero_weight=0.11 (btw, you can use class_weight={0: 0.11, 1: 0.89}, as suggested in the comment). Under class imbalance, your model is seeing much more zeros than ones. It will also learn to predict more zeros than ones because the training … Read more

ValueError: Input 0 is incompatible with layer lstm_13: expected ndim=3, found ndim=4

I solved the problem by making input size: (95000,360,1) and output size: (95000,22) and changed the input shape to (360,1) in the code where model is defined: model = Sequential() model.add(LSTM(22, input_shape=(360,1))) model.add(Dense(22, activation=’softmax’)) model.compile(loss=”categorical_crossentropy”, optimizer=”adam”, metrics=[‘accuracy’]) print(model.summary()) model.fit(ml2_train_input, ml2_train_output_enc, epochs=2, batch_size=500)

keras BatchNormalization axis clarification

The confusion is due to the meaning of axis in np.mean versus in BatchNormalization. When we take the mean along an axis, we collapse that dimension and preserve all other dimensions. In your example data.mean(axis=0) collapses the 0-axis, which is the vertical dimension of data. When we compute a BatchNormalization along an axis, we preserve … Read more

How to convert keras(h5) file to a tflite file?

from tensorflow.contrib import lite converter = lite.TFLiteConverter.from_keras_model_file( ‘model.h5’) tfmodel = converter.convert() open (“model.tflite” , “wb”) .write(tfmodel) You can use the TFLiteConverter to directly convert .h5 files to .tflite file. This does not work on Windows. For Windows, use this Google Colab notebook to convert. Upload the .h5 file and it will convert it .tflite file. … Read more

How to save model.summary() to file in Keras?

If you want the formatting of summary you can pass a print function to model.summary() and output to file that way: def myprint(s): with open(‘modelsummary.txt’,’a’) as f: print(s, file=f) model.summary(print_fn=myprint) Alternatively, you can serialize it to a json or yaml string with model.to_json() or model.to_yaml() which can be imported back later. Edit An more pythonic … Read more

Keras: model.predict for a single image

Since you trained your model on mini-batches, your input is a tensor of shape [batch_size, image_width, image_height, number_of_channels]. When predicting, you have to respect this shape even if you have only one image. Your input should be of shape: [1, image_width, image_height, number_of_channels]. You can do this in numpy easily. Let’s say you have a … Read more

Keras uses way too much GPU memory when calling train_on_batch, fit, etc

It is a very common mistake to forget that the activations, gradients and optimizer moment tracking variables also take VRRAM, not just the parameters, increasing memory usage quite a bit. The backprob calculations themselves make it so the training phase takes almost double the VRAM of forward / inference use of the neural net, and … Read more

What are the differences between all these cross-entropy losses in Keras and TensorFlow?

There is just one cross (Shannon) entropy defined as: H(P||Q) = – SUM_i P(X=i) log Q(X=i) In machine learning usage, P is the actual (ground truth) distribution, and Q is the predicted distribution. All the functions you listed are just helper functions which accepts different ways to represent P and Q. There are basically 3 … Read more

Make a deep copy of a keras model in python

The issue is that model_copy is probably not compiled after cloning. There are in fact a few issues: Apparently cloning doesn’t copy over the loss function, optimizer info, etc. Before compiling you need to also build the model. Moreover, cloning doesn’t copy weight over So you need a couple extra lines after cloning. For example, … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)