亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 TensorFlow 中用一維內核卷積信號?

如何在 TensorFlow 中用一維內核卷積信號?

郎朗坤 2021-12-09 15:24:04
我正在嘗試過濾形狀為 TensorFlow 張量(N_batch, N_data),其中N_batch是批量大?。ɡ?32),N_data是(嘈雜的)時間序列數組的大小。我有一個高斯核(取自這里),它是一維的。然后我想tensorflow.nn.conv1d用我的信號來卷積這個內核。我早上大部分時間都在努力使輸入信號和內核的維度正確,但顯然沒有成功。從我從互聯網上收集的信息來看,輸入信號和內核的維度都需要以某種挑剔的方式對齊,而我就是不知道是哪種方式。TensorFlow 錯誤消息也不是特別有意義 ( Shape must be rank 4 but is rank 3 for 'conv1d/Conv2D' (op: 'Conv2D') with input shapes: [?,1,1000], [1,81])。下面我包含了一小段代碼來重現這種情況:import tensorflow as tfimport numpy as npimport matplotlib.pyplot as plt# Based on: https://stackoverflow.com/a/52012658/1510542# Credits to @zephyrusdef gaussian_kernel(size, mean, std):    d = tf.distributions.Normal(tf.cast(mean, tf.float32), tf.cast(std, tf.float32))    vals = d.prob(tf.range(start=-size, limit=size+1, dtype=tf.float32))    kernel = vals   # Some reshaping is required here    return kernel / tf.reduce_sum(kernel)def gaussian_filter(input, sigma):    size = int(4*sigma + 0.5)    x = input   # Some reshaping is required here    kernel = gaussian_kernel(size=size, mean=0.0, std=sigma)    conv = tf.nn.conv1d(x, kernel, stride=1, padding="SAME")    return convdef run_filter():    tf.reset_default_graph()    # Define size of data, batch sizes    N_batch = 32    N_data = 1000    noise = 0.2 * (np.random.rand(N_batch, N_data) - 0.5)    x = np.linspace(0, 2*np.pi, N_data)    y = np.tile(np.sin(x), N_batch).reshape(N_batch, N_data)    y_noisy = y + noise    input = tf.placeholder(tf.float32, shape=[None, N_data])    smooth_input = gaussian_filter(input, sigma=10)    with tf.Session() as sess:        sess.run(tf.global_variables_initializer())        y_smooth = smooth_input.eval(feed_dict={input: y_noisy})        plt.plot(y_noisy[0])        plt.plot(y_smooth[0])        plt.show()if __name__ == "__main__":    run_filter()有任何想法嗎?
查看完整描述

1 回答

?
繁星coding

TA貢獻1797條經驗 獲得超4個贊

您需要為輸入/內核添加通道維度,因為 TF 卷積通常用于多通道輸入/輸出。當您使用簡單的 1 通道輸入/輸出時,這相當于添加一些大小為 1 的“虛擬”軸。

由于默認情況下卷積期望通道最后出現,因此您的占位符應具有形狀,[None,  N_data, 1]并且您的輸入應修改為


y_noisy = y + noise

y_noisy = y_noisy[:, :, np.newaxis] 

同樣,您需要向過濾器添加輸入和輸出通道維度:


kernel = gaussian_kernel(size=size, mean=0.0, std=sigma)

kernel = kernel[:, tf.newaxis, tf.newaxis]

也就是說,過濾器應具有 shape [width, in_channels, out_cannels]。


查看完整回答
反對 回復 2021-12-09
  • 1 回答
  • 0 關注
  • 204 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號