一个文本卷积模块
def cnn(): import numpy as np result = [] n, dim = 10, 30 kernels = [np.random.randint(0,2,(i, dim)) for i in range(3,6)] # 生成3个长度不同的一维卷积核 data = np.random.random((n, dim)) # 生成数据,np.random.random生成随机数方法 for each in kernels: size = len(each) end = n - size + 1 feat = [] for i in range(end): convolution = np.sum(data[i:i+size] * each) # 对应位置点积再求和 feat.append(convolution) result.append(max(feat)) # 最大池化 return result print(cnn())
标签:python,random,卷积,result,np,手写,feat,size From: https://www.cnblogs.com/demo-deng/p/16594915.html