Search code examples
pythonnumpytensorflowkerasdeep-learning

Broadcasting multiple version of X_data that pairing with same y_data


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

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

I have this 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 tensors 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)
    ``