0

My codes are as follow:

v = tf.Variable(initial_value=v, trainable=True)

v.shape is (1, 768)

In the model:

inputs_sents = keras.Input(shape=(50,3))
inputs_events = keras.Input(shape=(50,768))
x_1 = tf.matmul(v,tf.transpose(inputs_events))
x_2 = tf.matmul(x_1,inputs_sents)

But I got an error,

ValueError: Dimensions must be equal, but are 768 and 50 for 
'{{node BatchMatMulV2_3}} = 
    BatchMatMulV2[T=DT_FLOAT,
    adj_x=false,
    adj_y=false](BatchMatMulV2_3/ReadVariableOp,
    Transpose_3)' with input shapes: [1,768], [768,50,?]

I think it takes consideration of the batch? But how shall I deal with this? v is a trainable vector (or 2d array with first dimension being 1), I want it to be trained in the training process.

PS: This is the result I got using the codes provided by the first answer, I think it is incorrect cause keras already takes consideration of the first batch dimension. enter image description here

Plus, from the keras documentation,

shape: A shape tuple (integers), not including the batch size. For instance, shape=(32,) indicates that the expected input will be batches of 32-dimensional vectors. Elements of this tuple can be None; 'None' elements represent dimensions where the shape is not known.

https://keras.io/api/layers/core_layers/input/

Should I rewrite my codes without keras?

R__
  • 87
  • 5

1 Answers1

0

The shape of a batch is denoted by None:

import numpy as np
inputs_sents = keras.Input(shape=(None,1,3))
inputs_events = keras.Input(shape=(None,1,768))
v = np.ones(shape=(1,768), dtype=np.float32)
v = tf.Variable(initial_value=v, trainable=True)
x_1 = tf.matmul(v,tf.transpose(inputs_events))
x_2 = tf.matmul(x_1,inputs_sents)

YScharf
  • 1,638
  • 15
  • 20