2 views

I have an example of a neural network with two layers. The first layer takes two arguments and has one output. The second should take one argument as result of the first layer and one additional argument. It should looks like this:

x1  x2 x3

\  / /

y1   /

\  /

y2

So, I'd created a model with two layers and tried to merge them but it returns an error: The first layer in a Sequential model must get an "input_shape" or "batch_input_shape" argument. on the line result.add(merged).

Model:

first = Sequential()

second = Sequential()

result = Sequential()

merged = Concatenate([first, second])

by (140 points)
how to run model.fit when we have two models

by (33.1k points)

As I can understand your problem, the main reason for this error is the result defined as Sequential() is just a container for the model and you have not defined input for it.

It looks like you’re trying to build a set result to take the third input x3.

first = Sequential()

second = Sequential()

third = Sequential()

# provide the input to result with will be your x3

#then add a few more layers to first and second.

# concatenate them

merged = Concatenate([first, second])

# then concatenate the two outputs

result = Concatenate([merged,  third])

metrics=['accuracy'])

You can try another way of building a model that this type of input structure would be to use the functional API.

For example:

from keras.models import Model

from keras.layers import Concatenate, Dense, LSTM, Input, concatenate

first_input = Input(shape=(2, ))

first_dense = Dense(1, )(first_input)

second_input = Input(shape=(2, ))

second_dense = Dense(1, )(second_input)

merge_one = concatenate([first_dense, second_dense])

third_input = Input(shape=(1, ))

merge_two = concatenate([merge_one, third_input])

model = Model(inputs=[first_input, second_input, third_input], outputs=merge_two)

metrics=['accuracy'])

Concatenation works like this:

a        b         c

a b c   g h i a b c g h i

d e f   j k l d e f j k l

i.e rows are just joined.

2) You can say that x1 is input to first, x2 is input into second and x3 input into third.

I hope this solution solved your problem.

If you wish to learn more about Machine Learning, visit Machine Learning tutorial and Machine Learning course by Intellipaat.
{"ft_lang_label":"__label__en","ft_lang_prob":0.66456205,"math_prob":0.97770905,"size":2035,"snap":"2023-14-2023-23","text_gpt3_token_len":503,"char_repetition_ratio":0.12456918,"word_repetition_ratio":0.035971224,"special_character_ratio":0.26339066,"punctuation_ratio":0.15789473,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9957946,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-06-01T13:08:38Z\",\"WARC-Record-ID\":\"<urn:uuid:8260daf8-ced0-4180-a6a1-a92d07be8e9f>\",\"Content-Length\":\"124570\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:9e132970-28c0-4839-abe3-a783c875ba91>\",\"WARC-Concurrent-To\":\"<urn:uuid:4fcde366-0fcf-40ab-9ba7-64e303327c8d>\",\"WARC-IP-Address\":\"104.18.27.176\",\"WARC-Target-URI\":\"https://intellipaat.com/community/3409/how-to-concatenate-two-layers-in-keras\",\"WARC-Payload-Digest\":\"sha1:INFIVX6XGGH7AXLBEDMUN5VXINYR6FLZ\",\"WARC-Block-Digest\":\"sha1:FKKMOT4TXQ5UJTTM6YZR6TDALGBFNJFI\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-23/CC-MAIN-2023-23_segments_1685224647810.28_warc_CC-MAIN-20230601110845-20230601140845-00521.warc.gz\"}"}