tensorflow的1维卷积

前面找到了tensorflow的一维卷积、池化函数,但是官方API太简单,网上的例子也不多。

由于没时间研究源码,只能另寻他法了。

后面细细想来,tensorflow的二维卷积、池化函数,好像也能进行一维卷积、池化;也就是,利用对图像矩阵进行卷积、池化的函数,把第一个维度设置成1。

这样做确实可行,最简单的代码示例如下:

import tensorflow as tf
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

sess = tf.InteractiveSession()

def conv2d(x, W):
    return tf.nn.conv2d(x, W, strides=[1,1,1,1], padding='SAME')
def max_pool_1x2(x): return tf.nn.avg_pool(x, ksize=[1,1,2,1], strides=[1,1,2,1], padding='SAME')
'''
ksize = [x, pool_height, pool_width, x]
strides = [x, pool_height, pool_width, x]
''' x
= tf.Variable([[1,2,3,4]], dtype=tf.float32) x = tf.reshape(x, [1,1,4,1]) #这一步必不可少,否则会报错说维度不一致;
'''
[batch, in_height, in_width, in_channels] = [1,1,4,1]
''' W_conv1
= tf.Variable([1,2,1],dtype=tf.float32) W_conv1 = tf.reshape(W_conv1, [1,3,1,1]) # 这一步同样必不可少
'''
[filter_height, filter_width, in_channels, out_channels]
''' h_conv1
= conv2d(x, W_conv1) h_pool1 = max_pool_1x2(h_conv1) tf.global_variables_initializer().run() sess.run(h_pool1) # 结果array([6,11.5])
原文地址:https://www.cnblogs.com/HITSZ/p/8721414.html