I am trying to merge two networks. I can accomplish this by doing the following:
merged = Merge([CNN_Model, RNN_Model], mode='concat')
But I get a warning:
merged = Merge([CNN_Model, RNN_Model], mode='concat')
__main__:1: UserWarning: The `Merge` layer is deprecated and will be removed after 08/2017. Use instead layers from `keras.layers.merge`, e.g. `add`, `concatenate`, etc.
So I tried this:
merged = Concatenate([CNN_Model, RNN_Model])
model = Sequential()
model.add(merged)
and got this error:
ValueError: The first layer in a Sequential model must get an `input_shape` or `batch_input_shape` argument.
Can anyone give me the syntax as how I would get this to work?
Don't use sequential models for models with branches.
Use the Functional API:
from keras.models import Model
You're right in using the Concatenate layer, but you must pass "tensors" to it. And first you create it, then you call it with input tensors (that's why there are two parentheses):
concatOut = Concatenate()([CNN_Model.output,RNN_Model.output])
For creating a model out of that, you need to define the path from inputs to outputs:
model = Model([CNN_Model.input, RNN_Model.input], concatOut)
This answer assumes your existing models have only one input and output each.
Related
I trying to make a classification model with Keras. My data contains some numeric features and some text features. By text features I mean comments or something similar. Numeric features will be category, age so on.
I want to pass the text feature to a Embedding layer and then to a LSTM layer. The numeric feature needed to be passed to a series of Dense Layers. After that both layers needed to be concatenated. After that a Dense Layer to make the output.
How can I implement this type of model in Keras.?
Or is there any other way to use both numeric features and text based features in the model at the same time.?
It is fairly easy to implement such a network using keras functional API.
suppose you have defined two sequential models to process your textual and numerical features, you can then merge the output and continue with more layers:
txt_input = keras.layers.Input(shape=(n,))
txt_feat = text_network(txt_input)
num_input = keras.layers.Input(shape=(m,))
num_feat = num_network(input2)
concatinated = keras.layers.Concatenate()([txt_feat, num_feat])
out = keras.layers.Dense(nodes)(concatinated)
model = keras.models.Model(inputs=[input1, input2], outputs=out)
you can also use other types of merge using any merge layer from keras.
This a part of the code for a Deconvolutional-Convoltional Generative Adversarial Network (DC-GAN)
discriminator.trainable = False
ganInput = Input(shape=(100,))
# getting the output of the generator
# and then feeding it to the discriminator
# new model = D(G(input))
x = generator(ganInput)
ganOutput = discriminator(x)
gan = Model(input=ganInput, output=ganOutput)
gan.compile(loss='binary_crossentropy', optimizer=Adam())
I do not understand what the line ganInput = Input(shape=(100,)) does.
Clearly ganInput is a variable but what is Input? Is it a function ?
If Input is a function then what will ganInput contain ?
Then it is ganInput is fed into the generator since it is an empty variable (assuming) it will not matter. Next ganOutput catches the output of the discriminator.
Then comes the problem. I read about the Model API but I do not understand fully what it does.
To summarise these are my problems : What is the role of ganInput and what is Input in the second line. And what is Model doing and what is it?
Using Keras with TensorFlow backend
COMPLETE SOURCE CODE : https://github.com/yashk2810/DCGAN-Keras/blob/master/DCGAN.ipynb
Please ask for any more clarification / details required. If you know the answer to even one of my queries I will request you to please answer it will be a huge help. Thanks
What is input: Notice the wildcard import of keras.layers. In context, Input is keras.layers.Input. Generally, if you see a function or class that wasn't defined or explicitly imported in Python, it got there via a wildcard import, i.e.
from keras.layers import *
That means import everything from keras.layers directly into the workspace.
What is model: The model object is essentially the interface for making neural networks with Keras.
You can read about model and keras.layers.Input at the model docs or at this model guide since I'm not very familiar with Keras.
What's going on in the example is they define generator and discriminator as Sequentials. But the GAN model is a little more complex than a standard old Sequential. The authors deal with that by marking the data that needs fed in at every iteration (in this case, just the random noise for the generator - ganInput) as a keras.layers.Input. Then, like you said, ganOutput catches the output of the discriminator. Since we have two distinct Sequentials that need wrapped together, the authors use the model API.
When a Keras model accept multiple inputs, its layers behave like there is just one input. It might be a bug.
model = vgg19.VGG19(weights='imagenet', include_top=False, pooling='avg')
model(image1)
model(image2)
model.get_output_at(0)
model.get_output_at(1)
#no error here
outputs_0 = [layer.get_output_at(0) for layer in model.layers]
#no error here
outputs_1 = [layer.get_output_at(1) for layer in model.layers]
#error "Asked to get output at node 1, but the layer has only 1 inbound nodes."
I'm really not sure about what is outputs_0, since model have two inputs, image1 and image2, and when a layer return its output, what is its corresponding input?
In keras, If you have a model: .
print your model, you can know layer name;
wrap a new model;
get output;
from keras.models import Model
print(<your_model>.summary())
<new_model> = Model(inputs=<your_model>.input, outputs=<your_model>.get_layer('your layer_name').get_output_at(<index_number>))
<your_output> = <new_model>.predict(<your_input>)
Regardless of the model's inputs and outputs, there is no rule about how the layers behave inside a model. A model may have many internal branches and reuse (or not) the same layer with different inputs, yielding thus different outputs. A layer will only have "output at 1 (or more)" if that layer was used more than once.
The only certain things are:
the input layers will match the model's input (see 1),
and the output layers will match the model's output (see 1).
But anything is possible in between (see 2).
(1) - But, a model that has many inputs/outputs actually has many "input/output layers". Each output layer has a single output. If you check the "model" outputs, you have many, but if you check the "layers" outputs, then there are several output layers, each yealding a single output (output at 0 only). The same is valid for model's inputs vs input layers.
(2) - Even though, the most common option is to have layers being used only once, and thus having only "output at 0", without additional outputs.
I haven't used Keras and I'm thinking whether to use it or not.
I want to save a trained layer to use later. For example:
I train a model.
Then, I gain a trained layer t_layer.
I have another model to train which consists of layer1, layer2, layer3 .
I want to use t_layer as layer2 and not to update this layer(i.e. t_layer does not learn any more).
This may be an odd attempt, but I want to try this. Is this possible on Keras?
Yes, it is.
You will probably have to save the layer's weights and biases instead of saving the layer itself, but it's possible.
Keras also allows you to save entire models.
Suppose you have a model in the var model:
weightsAndBiases = model.layers[i].get_weights()
This is a list of numpy arrays, very probably with two arrays: weighs and biases. You can simply use numpy.save() to save these two arrays and later you can create a similar layer and give it the weights:
from keras.layers import *
from keras.models import Model
inp = Input(....)
out1 = SomeKerasLayer(...)(inp)
out2 = AnotherKerasLayer(....)(out1)
....
model = Model(inp,out2)
#above is the usual process of creating a model
#supposing layer 2 is the layer you want (you can also use names)
weights = numpy.load(...path to your saved weights)
biases = numpy.load(... path to your saved biases)
model.layers[2].set_weights([weights,biases])
You can make layers untrainable (must be done before the model compilation):
model.layers[2].trainable = False
Then you compile the model:
model.compile(.....)
And there you go, a model, whose one layer is untrainable and has weights and biases defined by you, taken from somewhere else.
Yes, it is a common practice in transfer learning, see here.
Thjs piece_to_share below can be one or more layers.
piece_to_share = tf.keras.Model(...)
full_model = tf.keras.Sequential([piece_to_share, ...])
full_model.fit(...)
piece_to_share.save(...)
I'm confusing about the mechanism of the merge layer in Keras.
For example, branch_left is a Sequential()model that contains two dense() layers while branch_right is another Sequential() model that has only one dense(). Then there is a merge() layer connect them in a concatenate way and I put the merge layer into a Sequential() model merged_model. Since I wanna get the name or weights saved in layers from those two branch model, I tried to call merged_model.layers and found the first layer is merge layer.
Considering len(erge.get_weights())=0, is there any way to get the layers existing in branch_left and branch_right? If that is possible, then I can modify the properties which are belonging to those layers.
I'm not sure it's a good idea to put the merge layer into a sequential...
(Sequential models do not support branches)
The best to do is to create a functional API model:
from keras.models import Model
merged_model = Model([branch_left.input,branch_right.input],concatLayer.output)