Questions tagged [keras-layer]

1512 questions
414
votes
3 answers

Keras input explanation: input_shape, units, batch_size, dim, etc

For any Keras layer (Layer class), can someone explain how to understand the difference between input_shape, units, dim, etc.? For example the doc says units specify the output shape of a layer. In the image of the neural net below hidden layer1…
scarecrow
  • 6,624
  • 5
  • 20
  • 39
101
votes
3 answers

Keras: Difference between Kernel and Activity regularizers

I have noticed that weight_regularizer is no more available in Keras and that, in its place, there are activity and kernel regularizer. I would like to know: What are the main differences between kernel and activity regularizers? Could I use…
Simone
  • 4,800
  • 12
  • 30
  • 46
97
votes
4 answers

How to stack multiple lstm in keras?

I am using deep learning library keras and trying to stack multiple LSTM with no luck. Below is my code model = Sequential() model.add(LSTM(100,input_shape =(time_steps,vector_size))) model.add(LSTM(100)) The above code returns error in the third…
Tamim Addari
  • 7,591
  • 9
  • 40
  • 59
82
votes
4 answers

How to add and remove new layers in keras after loading weights?

I am trying to do a transfer learning; for that purpose I want to remove the last two layers of the neural network and add another two layers. This is an example code which also output the same error. from keras.models import Sequential from…
Eka
  • 14,170
  • 38
  • 128
  • 212
73
votes
9 answers

Error when checking model input: expected convolution2d_input_1 to have 4 dimensions, but got array with shape (32, 32, 3)

I want to train a deep network starting with the following layer: model = Sequential() model.add(Conv2D(32, 3, 3, input_shape=(32, 32, 3))) using history = model.fit_generator(get_training_data(), samples_per_epoch=1,…
Oblomov
  • 8,953
  • 22
  • 60
  • 106
59
votes
10 answers

Tensorflow Allocation Memory: Allocation of 38535168 exceeds 10% of system memory

Using ResNet50 pre-trained Weights I am trying to build a classifier. The code base is fully implemented in Keras high-level Tensorflow API. The complete code is posted in the below GitHub Link. Source Code: Classification Using RestNet50…
Madhi
  • 1,206
  • 3
  • 16
  • 27
58
votes
4 answers

How do you create a custom activation function with Keras?

Sometimes the default standard activations like ReLU, tanh, softmax, ... and the advanced activations like LeakyReLU aren't enough. And it might also not be in keras-contrib. How do you create your own activation function?
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
58
votes
10 answers

Reset weights in Keras layer

I'd like to reset (randomize) the weights of all layers in my Keras (deep learning) model. The reason is that I want to be able to train the model several times with different data splits without having to do the (slow) model recompilation every…
Tor
  • 778
  • 1
  • 6
  • 9
57
votes
19 answers

How to fix "AttributeError: module 'tensorflow' has no attribute 'get_default_graph'"?

I am trying to run some code to create an LSTM model but i get an error: AttributeError: module 'tensorflow' has no attribute 'get_default_graph' My code is as follows: from keras.models import Sequential model = Sequential() model.add(Dense(32,…
Alice
  • 643
  • 1
  • 5
  • 7
56
votes
5 answers

When does keras reset an LSTM state?

I read all sorts of texts about it, and none seem to answer this very basic question. It's always ambiguous: In a stateful = False LSTM layer, does keras reset states after: Each sequence; or Each batch? Suppose I have X_train shaped as…
Daniel Möller
  • 84,878
  • 18
  • 192
  • 214
53
votes
3 answers

What is the difference between an Embedding Layer and a Dense Layer?

The docs for an Embedding Layer in Keras say: Turns positive integers (indexes) into dense vectors of fixed size. eg. [[4], [20]] -> [[0.25, 0.1], [0.6, -0.2]] I believe this could also be achieved by encoding the inputs as one-hot vectors of…
Imran
  • 12,950
  • 8
  • 64
  • 79
48
votes
4 answers

How do I get the weights of a layer in Keras?

I am using Windows 10, Python 3.5, and tensorflow 1.1.0. I have the following script: import tensorflow as tf import tensorflow.contrib.keras.api.keras.backend as K from tensorflow.contrib.keras.api.keras.layers import…
Toke Faurby
  • 5,788
  • 9
  • 41
  • 62
46
votes
5 answers

In Keras, how to get the layer name associated with a "Model" object contained in my model?

I built a Sequential model with the VGG16 network at the initial base, for example: from keras.applications import VGG16 conv_base = VGG16(weights='imagenet', # do not include the top, fully-connected Dense layers …
Ryan Chase
  • 2,384
  • 4
  • 24
  • 33
44
votes
5 answers

what is the difference between Flatten() and GlobalAveragePooling2D() in keras

I want to pass the output of ConvLSTM and Conv2D to a Dense Layer in Keras, what is the difference between using global average pooling and flatten Both is working in my…
user239457
  • 1,766
  • 1
  • 16
  • 26
43
votes
3 answers

Running the Tensorflow 2.0 code gives 'ValueError: tf.function-decorated function tried to create variables on non-first call'. What am I doing wrong?

error_giving_notebook non_problematic_notebook As it can be seen that I have used tf.function decorator in the 'error_giving_notebook' and it throws a ValueError while the same notebook without any changes except for removing the tf.function…
Gaurav Singh
  • 555
  • 1
  • 6
  • 9
1
2 3
99 100