keras-layer

How to Implement the Conv1DTranspose in keras?

◇◆丶佛笑我妖孽 提交于 2020-05-09 20:20:33
问题 I Know there is the Conv2DTranspose in keras which can be used in Image. We need to use it in NLP, so the 1D deconvolution is needed. How do we implement the Conv1DTranspose in keras? 回答1: Use keras backend to fit the input tensor to 2D transpose convolution. Do not always use transpose operation for it will consume a lot of time. import keras.backend as K from keras.layers import Conv2DTranspose, Lambda def Conv1DTranspose(input_tensor, filters, kernel_size, strides=2, padding='same'): """

How to include normalization of features in Keras regression model?

本秂侑毒 提交于 2020-05-09 06:35:06
问题 I have a data for a regression task. The independent features( X_train ) are scaled with a standard scaler. Built a Keras sequential model adding hidden layers. Compiled the model. Then fitting the model with model.fit(X_train_scaled, y_train ) Then I saved the model in a .hdf5 file. Now how to include the scaling part inside the saved model, so that the same scaling parameters can be applied to unseen test data. #imported all the libraries for training and evaluating the model X_train, X

Persistent Variable in keras Custom Layer

﹥>﹥吖頭↗ 提交于 2020-05-07 19:59:17
问题 I want write a custom layer, where I can keep a variable in memory between runs. For example, class MyLayer(Layer): def __init__(self, out_dim = 51, **kwargs): self.out_dim = out_dim super(MyLayer, self).__init__(**kwargs) def build(self, input_shape): a = 0.0 self.persistent_variable = K.variable(a) self.built = True def get_output_shape_for(self, input_shape): return (input_shape[0], 1) def call(self, x, mask=None): a = K.eval(self.persistent_variable) + 1 K.set_value(self.persistent

Persistent Variable in keras Custom Layer

陌路散爱 提交于 2020-05-07 19:57:47
问题 I want write a custom layer, where I can keep a variable in memory between runs. For example, class MyLayer(Layer): def __init__(self, out_dim = 51, **kwargs): self.out_dim = out_dim super(MyLayer, self).__init__(**kwargs) def build(self, input_shape): a = 0.0 self.persistent_variable = K.variable(a) self.built = True def get_output_shape_for(self, input_shape): return (input_shape[0], 1) def call(self, x, mask=None): a = K.eval(self.persistent_variable) + 1 K.set_value(self.persistent

If then inside custom non-trainable keras layer

送分小仙女□ 提交于 2020-04-17 22:06:02
问题 I have a custom Keras layer that I want to return specific output from specific inputs. I don't want it to be trainable. The layer should do the following if input = [1,0] then output = 1 if input = [0,1] then output = 0 Instead, it always outputs -1, the value I set if there's problem. I think the line that is not behaving as I expect it would is: if(test_mask_1_result_count == 2) Here's the custom layer: class my_custom_layer(layers.Layer): def __init__(self, **kwargs): super(my_custom

How to disable dropout while prediction in keras?

早过忘川 提交于 2020-04-10 07:07:48
问题 I am using dropout in neural network model in keras. Little bit code is like model.add(Dropout(0.5)) model.add(Dense(classes)) For testing, I am using preds = model_1.predict_proba(image) . But while testing Dropout is also participating to predict the score which should not be happen. I search a lot to disable the dropout but didn't get any hint yet. Do anyone have solution to disable the Dropout while testing in keras?? 回答1: Keras does this by default. In Keras dropout is disabled in test

How to disable dropout while prediction in keras?

家住魔仙堡 提交于 2020-04-10 07:06:50
问题 I am using dropout in neural network model in keras. Little bit code is like model.add(Dropout(0.5)) model.add(Dense(classes)) For testing, I am using preds = model_1.predict_proba(image) . But while testing Dropout is also participating to predict the score which should not be happen. I search a lot to disable the dropout but didn't get any hint yet. Do anyone have solution to disable the Dropout while testing in keras?? 回答1: Keras does this by default. In Keras dropout is disabled in test

How to disable dropout while prediction in keras?

笑着哭i 提交于 2020-04-10 07:06:11
问题 I am using dropout in neural network model in keras. Little bit code is like model.add(Dropout(0.5)) model.add(Dense(classes)) For testing, I am using preds = model_1.predict_proba(image) . But while testing Dropout is also participating to predict the score which should not be happen. I search a lot to disable the dropout but didn't get any hint yet. Do anyone have solution to disable the Dropout while testing in keras?? 回答1: Keras does this by default. In Keras dropout is disabled in test

How to load the Keras model with custom layers from .h5 file correctly?

孤街浪徒 提交于 2020-03-03 04:49:37
问题 I built a Keras model with a custom layers, and it was saved to a .h5 file by the callback ModelCheckPoint . When I tried to load this model after the training, the error message below showed up: __init__() missing 1 required positional argument: 'pool_size' This is the definition of the custom layer and its __init__ method: class MyMeanPooling(Layer): def __init__(self, pool_size, axis=1, **kwargs): self.supports_masking = True self.pool_size = pool_size self.axis = axis self.y_shape = None

How to load the Keras model with custom layers from .h5 file correctly?

北城余情 提交于 2020-03-03 04:47:13
问题 I built a Keras model with a custom layers, and it was saved to a .h5 file by the callback ModelCheckPoint . When I tried to load this model after the training, the error message below showed up: __init__() missing 1 required positional argument: 'pool_size' This is the definition of the custom layer and its __init__ method: class MyMeanPooling(Layer): def __init__(self, pool_size, axis=1, **kwargs): self.supports_masking = True self.pool_size = pool_size self.axis = axis self.y_shape = None