making a simple model does only dot product

Viewed 30

Maybe this code is stupid, but I'd wanted check whether it allows to make a model that only dot-product the inputs.

This is my code.

from tensorflow.keras.models import Model
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint
import tensorflow as tf
import numpy as np

x = np.array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1]).reshape(1, 10)
y = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]).reshape(1, 10)

x1 = [x for j in range(30)]
x2 = [y for j in range(30)]
y1 = [np.array([55]).reshape(1, 1) for j in range(30)]

input1 = Input(shape=(10,))
input2 = Input(shape=(10,))
dotted = Dot(axes=(1))([input1, input2])

model = Model(inputs = [input1, input2], outputs = [dotted])

model.compile(optimizer='rmsprop', loss='sparse_categorical_crossentropy')

history = model.fit(x=[x1, x2], y=np.array(y1))

Well, this gave me a ValueError as below.

ValueError: Data cardinality is ambiguous:
  x sizes: 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
  y sizes: 30
Make sure all arrays contain the same number of samples.

I want to know two things from here.

First : Will this model runs well if I fix this ValueError?

Second : How can I fix this ValueError?

1 Answers

Ok, I got it...

from tensorflow.keras.layers import Input, LSTM, Embedding, Dense, Concatenate, Dot
from tensorflow.keras.models import Model
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint
import tensorflow as tf
import numpy as np

x = np.array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1]).reshape(1, 10)
y = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]).reshape(1, 10)
z = np.array([55]).reshape(1, 1)

x1 = np.array([x for j in range(30)]).reshape(30, 10, 1)
x2 = np.array([y for j in range(30)]).reshape(30, 10, 1)
y1 = np.array([z for j in range(30)]).reshape(30, 1, 1)

input1 = Input(shape=(10,))
input2 = Input(shape=(10,))
dotted = Dot(axes=(1))([input1, input2])

model = Model(inputs = [input1, input2], outputs = [dotted])

model.compile(optimizer='rmsprop', loss='mse')

history = model.fit(x=[x1, x2], y=np.array(y1))

Changing like this got worked, since it was a kind of regression.

And also, it was only using one same data, that never won't be fit to other vectors...

Well, it was good try for me. I'd test with more data for conclude.

Related