Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (17.6k points)

I am new to keras, and got some problems understanding the keras.layers.Dot() layer.

I am trying to calculate a dot product of two vectors.

from keras.layers import Input, Dot

from keras.models import Model

import numpy as np

x1 = Input(shape=(4,))

x2 = Input(shape=(4,))

y1 = Dot(axes=1)([x1,x2])

model = Model(inputs=[x1, x2], outputs=y1)

a1 = np.arange(4)

a2=np.arange(4)

model.predict([a1,a2])

I expect the output to be 14=0+1^2+2^2+3^2. However, I got error message like below:

ValueError: Error when checking input: expected input_46 to have shape (4,) but got array with shape (1,)

I tried to run model.get_config(), and below is the corresponding information about the graph of the model. As you can see input_46 is x1, and input_47 is x2.

{'name': 'model_19',

 'layers': [{'name': 'input_46',

   'class_name': 'InputLayer',

   'config': {'batch_input_shape': (None, 4),

    'dtype': 'float32',

    'sparse': False,

    'name': 'input_46'},

   'inbound_nodes': []},

  {'name': 'input_47',

   'class_name': 'InputLayer',

   'config': {'batch_input_shape': (None, 4),

    'dtype': 'float32',

    'sparse': False,

    'name': 'input_47'},

   'inbound_nodes': []},

  {'name': 'dot_20',

   'class_name': 'Dot',

   'config': {'name': 'dot_20',

    'trainable': True,

    'axes': 1,

    'normalize': False},

   'inbound_nodes': [[['input_46', 0, 0, {}], ['input_47', 0, 0, {}]]]}],

 'input_layers': [['input_46', 0, 0], ['input_47', 0, 0]],

 'output_layers': [['dot_20', 0, 0]]}

Is there anything I didn't do right? Thanks!

UPDATE

The following code worked:

x1 = Input(shape=(4,))

x2 = Input(shape=(4,))

y1 = Dot(axes=1)([x1,x2])

model = Model(inputs=[x1, x2], outputs=y1)

a1 = np.arange(4).reshape(1,4)

a2=np.arange(4).reshape(1,4)

model.predict([a1,a2])

or

from keras.layers import Input, Dot

from keras.models import Model

import numpy as np

x1 = Input(shape=(4,))

x2 = Input(shape=(4,))

y1 = Dot(axes=1)([x1,x2])

model = Model(inputs=[x1, x2], outputs=y1)

a1 = np.arange(4)

a2=np.arange(4)

model.predict([[a1],[a2]]) 

1 Answer

0 votes
by (41.4k points)

Here you have two choices

Change this

a1 = np.arange(4) a2=np.arange(4)

To

a1 = np.arange(1,4)

a2=np.arange(1,4)

Or you can try model.predict([[a1],[a2]]).

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...