'tf.reshape(self.normalized_price(prce), (-1, 1)), ValueError: Shape must be rank 1 but is rank 2

I am getting the following error when I am calling the subclass of the model. My guess is that I am not passing the two parameters correctly or the reshape is not outputting the correct value.

ValueError: Shape must be rank 1 but is rank 2 for '{{node base_stock_model/concat}} = ConcatV2[N=3, T=DT_FLOAT, Tidx=DT_INT32](base_stock_model/sequential_2/embedding_2/embedding_lookup/Identity_1, base_stock_model/sequential_3/embedding_3/embedding_lookup/Identity_1, base_stock_model/Reshape, base_stock_model/concat/axis)' with input shapes: [32], [32], [1,1], [].

Here is the main class model

class StockModel(tfrs.models.Model):

  def __init__(self, rating_weight: float, retrieval_weight: float) -> None:

    super().__init__()

    embedding_dimension = 32

    self.user_model= UserModel()


    self.stock_model= base_stockModel()    

  
    self.rating_model = tf.keras.Sequential([
        tf.keras.layers.Dense(256, activation="relu"),
        tf.keras.layers.Dense(128, activation="relu"),
        tf.keras.layers.Dense(1),
    ])

    # The tasks.
    self.rating_task: tf.keras.layers.Layer = tfrs.tasks.Ranking(
        loss=tf.keras.losses.MeanSquaredError(),
        metrics=[tf.keras.metrics.RootMeanSquaredError()],
    )
    self.retrieval_task: tf.keras.layers.Layer = tfrs.tasks.Retrieval(
        metrics=tfrs.metrics.FactorizedTopK(
            candidates=stocks.batch(1).map(self.stock_model)
        )
    )

    # The loss weights.
    self.rating_weight = rating_weight
    self.retrieval_weight = retrieval_weight

  def call(self, features: Dict[Text, tf.Tensor]) -> tf.Tensor:

    user_embeddings = self.user_model(features['username'])

    # np.array([features["name"],features["price"]])
    
    price=tf.as_string(features["price"])
 
    stock_embeddings = self.stock_model([features["name"],price])
    
    return (
        user_embeddings,
        stock_embeddings,
        self.rating_model(
            tf.concat([user_embeddings, stock_embeddings], axis=1)
        ),
    )
  def compute_loss(self, features: Dict[Text, tf.Tensor], training=False) -> tf.Tensor:

    ratings = features.pop("Rating")

    print("features",features)

    user_embeddings, stock_embeddings, rating_predictions = self(features)

    # We compute the loss for each task.
    rating_loss = self.rating_task(
        labels=ratings,
        predictions=rating_predictions,
    )
    retrieval_loss = self.retrieval_task(user_embeddings, stock_embeddings)

    # And combine them using the loss weights.
    return (self.rating_weight * rating_loss
            + self.retrieval_weight * retrieval_loss)

The above main class model calls the base_stockModel class, which is causing errors.

class base_stockModel(tf.keras.Model):
  def __init__(self):
      super().__init__()
      embedding_dimension=32
      self.stock_embedding = tf.keras.Sequential([
      tf.keras.layers.StringLookup(
        vocabulary=unique_stock_titles, mask_token=None),
      tf.keras.layers.Embedding(len(unique_stock_titles) + 1, embedding_dimension)
      ])
      self.price_embedding=tf.keras.Sequential([
          tf.keras.layers.Discretization(prices_bucket.tolist()),
          tf.keras.layers.Embedding(len(prices_bucket)+2,32)
      ])
      self.normalized_price = tf.keras.layers.Normalization(axis=None)
      self.normalized_price.adapt(prices)

  
  def call(self,input,*args,**kwargs):
    print(input.get_shape(),kwargs)
    # print(tf.rank(input),[input[:]],input.get_shape(),input.dtype)
    # nme=input[3]
    nme=input[0]
    prce=input[1]

    prce=tf.strings.to_number(input[1],out_type=tf.dtypes.float32)
#print(tf.rank(self.stock_embedding(nme)),tf.rank(self.price_embedding(prce)),tf.rank(tf.reshape(sself.normalized_price(prce), (-1, 1))))
    return tf.concat([
        self.stock_embedding(nme),
        self.price_embedding(prce),
        tf.reshape(self.normalized_price(prce), (-1, 1)),
    ], axis=1)

This code is a variant of tensorflow recommender official page https://www.tensorflow.org/recommenders/examples/multitask/ https://www.tensorflow.org/recommenders/examples/context_features Any help is much appreciated.



Solution 1:[1]

First I analyzed all the ranks of tensor. If they were not the same rank I want or model wants then I used tf.reshape() command. Note that, tf.shape() gives you the shape most of the times only while running the model.

Here is documentation on it.

https://www.tensorflow.org/api_docs/python/tf/reshape https://www.tensorflow.org/api_docs/python/tf/shape

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Harsh Nagarkar