I'm using TF version 2.6.2. Using the Dataset API
I've a dictionary containing a list of labels and list of encodings.
Created a TF dataset using:
ds = tf.data.Dataset.from_tensor_slices(dictionary)
To do some custom batching I've used this line of code:
ds = ds.group_by_window(key_func=lambda elem: tf.cast(elem['label'], tf.int64) , reduce_func=lambda _, window: window.batch(batch_size), window_size=batch_size)
In essence it makes batches according to a modulo rule on the label column.
Next I create a very simple model to eat the encodings, which have length 30:
model = tf.keras.models.Sequential()
model.add(tf.keras.Input(shape=(30,)))
model.add(tf.keras.layers.Dense(16, activation='relu'))
model.add(tf.keras.layers.Dense(8, activation='relu'))
Compile using the TripletSemiHardLoss:
model.compile(
optimizer=tf.keras.optimizers.Adam(0.001),
loss=tfa.losses.TripletSemiHardLoss(y_true = "label", y_pred = "encodings"))
and fit:
history = model.fit(
ds,
epochs=5)
This gives me the error:
ValueError: Layer sequential_7 expects 1 input(s), but it received 2 input tensors. Inputs received: [<tf.Tensor 'ExpandDims:0' shape=(None, 1) dtype=int32>, <tf.Tensor 'IteratorGetNext:1' shape=(None, 30) dtype=float64>]
Any idea how to approach this? The model seems to not know to differentiate between the label and encodings part of the data set.
CodePudding user response:
Maybe switch x
and y
positions in ds
:
ds = ds.map(lambda x: (x['positives'], x['id_col']))
history = model.fit(
ds,
epochs=5)
Or depending on the structure of your data, maybe:
ds = ds.map(lambda x: (x[1], x[0]))
history = model.fit(
ds,
epochs=5)