简体   繁体   English

在TensorFlow中手动初始化conv1d

[英]Manual initialization of conv1d in TensorFlow

How can I set custom coefficients to tf.layers.conv1d . 如何将自定义系数设置为tf.layers.conv1d I found out how to read current coefficients, but how can I write them? 我找到了如何读取当前系数的方法,但是如何写呢?

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))

Here is a link to notebook with code at google colab: https://colab.research.google.com/drive/1YNSzKmtC88b__LqYcfD-tFHFG3jOZIAz 这是Google colab上带有代码的笔记本的链接: https ://colab.research.google.com/drive/1YNSzKmtC88b__LqYcfD-tFHFG3jOZIAz

In general, I'm interested in how to make a FIR-filter in TensorFlow. 总的来说,我对如何在TensorFlow中制作FIR滤波器感兴趣。

I got it! 我知道了! There is kerner_initializer parameter. kerner_initializer参数。

And this is solution 这是解决方案

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM