如何将自定义系数设置为tf.layers.conv1d
。我发现了如何读取当前系数,但我怎么写它们?
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
sess = tf.Session()
order = 5
x = np.zeros(30)
x[10] = 1
y = tf.layers.conv1d(inputs=tf.reshape(x,[1, len(x), 1]),
filters=1,
kernel_size=order,
padding='same')
sess.run(tf.global_variables_initializer())
y_out = sess.run(y)
# get coef
coef = sess.run(tf.all_variables()[-2].value())
print(coef.reshape(order))
这是一个链接到笔记本的代码在谷歌colab:https://colab.research.google.com/drive/1YNSzKmtC88b__LqYcfD-tFHFG3jOZIAz
一般来说,我对如何在TensorFlow中制作FIR滤波器感兴趣。
我知道了!有kerner_initializer
参数。
这是解决方案
init_coef = np.array([1,2,3,4,5])[::-1]
init_coef = tf.initializers.constant(init_coef)
y = tf.layers.conv1d(inputs=tf.reshape(x,[1, len(x), 1]),
filters=1,
kernel_size=order,
padding='same',
kernel_initializer=init_coef)