How to check neural network structure in keras model?

I am new to Keras and Neural Networks. I am writing a dissertation and am trying to create SimpleRNN in Keras, as shown below:

enter image description here

As shown in the figure, I need to create a model with 4 inputs + 2 outputs and with any number of neurons in a hidden layer.

This is my code:

model = Sequential() model.add(SimpleRNN(4, input_shape=(1, 4), activation='sigmoid', return_sequences=True)) model.add(Dense(2)) model.compile(loss='mean_absolute_error', optimizer='adam') model.fit(data, target, epochs=5000, batch_size=1, verbose=2) predict = model.predict(data) 

1) Does my model use a chart?
2) Is it possible to specify the connections between the layers of neurons Input and Hidden or Output and Input?

Explanation:

I am going to use backpropagation to train my network. I have input and destination values

The input is a 10 * 4 array, and the target is a 10 * 2 array, which I then modify:

 input = input.reshape((10, 1, 4)) target = target.reshape((10, 1, 2)) 

It is very important to be able to indicate the connections between neurons, since they can be different. For example, here you can have an example:

enter image description here

+5
source share
1 answer

1) Not really. But I'm not sure what exactly you want on this chart. (Let's see how the repeating Keras layers work)

2) Yes, it is possible to connect each layer to each layer, but you cannot use Sequential for this, you must use Model .

This answer may not be what you are looking for. What exactly do you want to achieve? What data do you have, what result do you expect, what should the model do? etc...


1 - How does the repeating layer work?

Documentation

Repeating layers in keras work with an input sequence and can output one result or the result of a sequence. The revaluation is completely contained in it and does not interact with other layers.

You must have inputs with the form (NumberOrExamples, TimeStepsInSequence, DimensionOfEachStep) . This means input_shape=(TimeSteps,Dimension) .

The recurrence layer will work inside each step. Cycles occur from step to step, and this behavior is completely invisible. A layer seems to work just like any other layer.

This is not what you want. If you do not have a "sequence" for input. The only way I know to use repeating layers in Keras, which is similar to your chart, is when you have a sequence segment and you want to predict the next step. If this is the case, see some examples by searching for โ€œnext element forecastingโ€ on Google.

2 - How to connect layers using the model:

Instead of adding layers to a sequential model (which will always follow a straight line), start using layers independently, starting with the input tensor:

 from keras.layers import * from keras.models import Model inputTensor = Input(shapeOfYourInput) #it seems the shape is "(2,)", but we must see your data. #A dense layer with 2 outputs: myDense = Dense(2, activation=ItsAGoodIdeaToUseAnActivation) #The output tensor of that layer when you give it the input: denseOut1 = myDense(inputTensor) #You can do as many cycles as you want here: denseOut2 = myDense(denseOut1) #you can even make a loop: denseOut = Activation(ItsAGoodIdeaToUseAnActivation)(inputTensor) #you may create a layer and call it with the input tensor in just one line if you're not going to reuse the layer #I'm applying this activation layer here because since we defined an activation for the dense layer and we're going to cycle it, it not going to behave very well receiving huge values in the first pass and small values the next passes.... for i in range(n): denseOut = myDense(denseOut) 

This type of use allows you to create any model with branches, alternative methods, connections from anywhere to anywhere, if you follow the rules of the form. For such a cycle, the inputs and outputs must have the same shape.

In the end, you must define a model from one or more inputs to one or more outputs (you must have training data to match all selected inputs and outputs):

 model = Model(inputTensor,denseOut) 

But note that this model is static. If you want to change the number of cycles, you will need to create a new model.

In this case, it would be as simple as repeating the loop step denseOut = myDense(denseOut) and creating another model2=Model(inputTensor,denseOut) .


3 - Trying to create something like the image below:

I assume that C and F will participate in all iterations. If not,

Since there are four actual inputs, and we will consider them separately, let us instead create 4 inputs, all like (1). Your input array should be divided into 4 arrays, all of which (10.1).

 from keras.models import Model from keras.layers import * inputA = Input((1,)) inputB = Input((1,)) inputC = Input((1,)) inputF = Input((1,)) 

Now layers N2 and N3, which will be used only once, since C and F are constant:

 outN2 = Dense(1)(inputC) outN3 = Dense(1)(inputF) 

Now the repeating layer N1, without giving it tensors:

 layN1 = Dense(1) 

For the loop, create outA and outB. They start as actual inputs and will be assigned to layer N1, but in a cycle they will be replaced

 outA = inputA outB = inputB 

Now in the loop do "pass":

 for i in range(n): #unite A and B in one inputAB = Concatenate()([outA,outB]) #pass through N1 outN1 = layN1(inputAB) #sum results of N1 and N2 into A outA = Add()([outN1,outN2]) #this is constant for all the passes except the first outB = outN3 #looks like B is never changing in your image.... 

Now the model:

 finalOut = Concatenate()([outA,outB]) model = Model([inputA,inputB,inputC,inputF], finalOut) 
+5
source

Source: https://habr.com/ru/post/1271237/


All Articles