我想要做的是計算模型內的相關性并將相關性結果用作下一層的輸入。我可以預先計算相關性,但是我有很多輸入特征并且計算所有特征的相互相關性是不可行的。我的想法是將特征減少到可管理的大小,然后計算它們的相關性。這是我偶然發現一個問題的最小示例:
from tensorflow import keras
import tensorflow_probability as tfp
def the_corr(x):
return tfp.stats.correlation(x, sample_axis = 1)
input = keras.Input(shape=(100,3000,))
x = keras.layers.Conv1D(filters=64, kernel_size=1,activation='relu') (input)
x = keras.layers.Lambda(the_corr, output_shape=(64,64,)) (x)
#x = keras.layers.Dense(3) (x)
model = keras.Model(input, x)
model.summary()
但是,這是總結結果:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_9 (InputLayer) [(None, 100, 3000)] 0
conv1d_3 (Conv1D) (None, 100, 64) 192064
lambda_8 (Lambda) (None, None, None) 0
=================================================================
Total params: 192,064
Trainable params: 192,064
Non-trainable params: 0
_________________________________________________________________
lambda 層產生的輸出形狀不正確,完全忽略了 option output_shape=(64,64,)。所以很明顯,如果帶回注釋的行,下面的密集層會拋出錯誤:
ValueError: The last dimension of the inputs to a Dense layer should be defined. Found None. Full input shape received: (None, None, None)
我也可以洗掉 中的sample_axis=1選項tfp.stats.correlation(),但是,批處理軸 ( None) 被丟棄:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_12 (InputLayer) [(None, 100, 3000)] 0
conv1d_6 (Conv1D) (None, 100, 64) 192064
lambda_11 (Lambda) (100, 64, 64) 0
dense_5 (Dense) (100, 64, 3) 195
=================================================================
Total params: 192,259
Trainable params: 192,259
Non-trainable params: 0
_________________________________________________________________
這也不是我想要的,因為批次樣本是獨立的,不應該放在一起。:
我究竟做錯了什么?這甚至可能嗎?
uj5u.com熱心網友回復:
您可以嘗試keepdims=True設定tfp.stats.corr:
def the_corr(x):
x = tfp.stats.correlation(x,
sample_axis=1,
keepdims=True)
# Keepdims will give an extra dim.
x = tf.squeeze(x, axis = 1)
return x
input = keras.Input(shape=(100,3000,))
x = keras.layers.Conv1D(filters=64, kernel_size=1,activation='relu') (input)
x = keras.layers.Lambda(the_corr)(x)
x = keras.layers.Dense(3)(x)
model = keras.Model(input, x)
model.summary()
概括:
Model: "model"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) [(None, 100, 3000)] 0
conv1d (Conv1D) (None, 100, 64) 192064
lambda (Lambda) (None, 64, 64) 0
dense (Dense) (None, 64, 3) 195
=================================================================
Total params: 192,259
Trainable params: 192,259
Non-trainable params: 0
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/473128.html
