Sean
Sean

Reputation: 3450

Zero-dimensional arrays cannot be concatenated, but my arrays aren't zero-dimensional

I'm facing an error when using np.concatenate. The code that I'm using is:

def get_angles(pos, i, d_model):
    angle_rates = 1 / np.power(10000, (2 * (i // 2)) / np.float32(d_model))
    return pos * angle_rates


def positional_encoding(position, d_model):
    angle_rads = get_angles(np.arange(position)[:, np.newaxis],
                            np.arange(d_model)[np.newaxis, :],
                            d_model)

    sines = tf.math.sin(angle_rads[:, 0::2])
    cosines = tf.math.cos(angle_rads[:, 1::2])

    pos_encoding = np.concatenate([sines, cosines], axis=-1)
    pos_encoding = pos_encoding[np.newaxis, ...]

    return tf.cast(pos_encoding, dtype=tf.float32)

The line of code that's causing problems is the np.concatenate part in function positional_encoding. When the program hits that part it spits out

ValueError: zero-dimensional arrays cannot be concatenated

but when I check the dimensions for sines and cosines, they each have dimensions (50, 25).

Is there something that I'm missing when performing these operations?

Thank you.

Edit

position = 50

d_model = 512

Upvotes: 1

Views: 639

Answers (1)

Olivier CAYROL
Olivier CAYROL

Reputation: 276

I don't know that well tensorflow but might it be because sines and cosines are Tensor objects that have not (yet) be evaluated and np.concatenate is a NumPy function that expects regular (ie evaluated) arrays?

Upvotes: 1

Related Questions