pythonnumpytensorflowkerasdeep-learning

Broadcasting multiple versions of X_data that pair with the same y_data


My deep learning architecture accepts an input vector with size 512 and an output vector with size 512 too.

The problem is I have X_data version that pairs with the same y_data.

I have these tensors:

(4, 8, 512) -> (batch_size, number of X_data version, input size to model architecture) (list of X_data)

(4, 512) -> (batch_size, output size to model architecture) (y_data)

This means:

X_data[0,0,:] is pairing with y_data[0,:]
X_data[0,1,:] is pairing with y_data[0,:]
...
X_data[0,7,:] is pairing with y_data[0,:]
X_data[1,0,:] is pairing with y_data[1,:]
X_data[1,1,:] is pairing with y_data[1,:]
...
X_data[1,7,:] is pairing with y_data[1,:]
...
X_data[3,7,:] is pairing with y_data[3,:]

What is the final tensor shape of X_data and y_data so that I can train the model?

Could you do that in NumPy?


Solution

  • To reshape the arrays into (32, 512) arrays with X_data and y_data matched the way you specify, you could do something like this:

    import numpy as np
    
    X_data = np.random.rand(4, 8, 512)
    y_data = np.random.rand(4, 512)
    
    X_data, y_data = np.broadcast_arrays(X_data, y_data[:, None, :])
    X_data = X_data.reshape(32, 512)
    y_data = y_data.reshape(32, 512)
    ``