簡體   English   中英

在python中組合兩個不同維度的數組

[英]Combine two arrays with different dimensions in python

我正在使用音頻和文本進行情緒分類的項目。 我將音頻和文本傳遞給 1D CNN 並得到以下輸出數組:

audio_features_shape = (396, 63, 64)
text_features_shape = (52, 1, 64)

現在我想將這兩個不同維度的數組堆疊成一個,這樣我就可以將一個數組傳遞給 LSTM。 我想要的形狀為:

expected_array_shape = (448, 64, 128)

我嘗試了以下方法,但沒有人給出我想要的輸出。

x = np.column_stack((audio_features, text_features))
x = np.concatenate((audio_features,text_features), axis=2)
x = np.append(audio_features, text_features)
x = np.transpose([np.tile(audio_features, len(text_features)), np.repeat(text_features, len(audio_features))])
x = np.array([np.append(text_features,x) for x in audio_features])

任何幫助,將不勝感激。 謝謝!

2 個數組的值應該如何分布在結果中?

audio_features_shape = (396, 63, 64)
text_features_shape = (52, 1, 64)

text_features應該“擴展”到 (52,63,64),或者通過在中間軸上重復值 63 次,或者將此數組放入 0 的目標數組中。 在任何一種情況下,它都會大 63 倍。

一旦數組在除第一個維度之外的所有維度上都匹配后,它們就可以被連接起來。

但真正的問題是,LSTM 的使用有何意義?

根據您到底想要什么以及您是否只對使用 Tensorflow 感興趣,您可以嘗試以下操作:

import tensorflow as tf

audio_features = tf.random.normal((396, 63, 64))
text_features = tf.random.normal((52, 1, 64))

text_features = tf.repeat(text_features, repeats=(audio_features.shape[1]-text_features.shape[1]) + 1, axis=1) 
repeat_features = tf.concat([audio_features, text_features], axis=0)
text_features = tf.random.normal((52, 1, 64))

paddings = tf.constant([[0, 0], [0, audio_features.shape[1]-text_features.shape[1]], [0, 0]])
pad_features = tf.concat([audio_features, tf.pad(text_features, paddings, "CONSTANT")], axis=0)

print('Using tf.repeat --> ', audio_features.shape, text_features.shape, repeat_features.shape)
print('Using tf.pad --> ', audio_features.shape, text_features.shape, pad_features.shape)
Using tf.repeat -->  (396, 63, 64) (52, 1, 64) (448, 63, 64)
Using tf.pad -->  (396, 63, 64) (52, 1, 64) (448, 63, 64)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM