我想在这里重新实现嵌入这个词
这是原始张量流代码(版本:0.12.1)
导入张量流为tfclass Network(object): def __init __( self,user_length,…
代码的张量流部分的pytorch等价物,用代码本身的注释解释,你必须从scipy导入truncnorm。
from scipy.stats import truncnorm #extra import equivalent to tf.trunc initialise pooled_outputs_u = [] for i, filter_size in enumerate(filter_sizes): filter_shape = [filter_size, embedding_size, 1, num_filters] #W is just a tensor now that will act as weight to the conv layer W=torch.tensor(truncnorm.rvs(-1,1,size=[10,10])) #bias initialized with 0.1 initial values b=torch.zeros([num_filters])+0.1 #conv layer with the same parameters as the tensorflow layer more on this in the link conv=torch.nn.functional.conv2d(self.embedded_users,W,bias=b,strides=[1,1,1,1],padding=0) #can use torch sequential to include it all in a single line but did it like this for better understanding. h=torch.relu(conv) #look at link2 for what a max pool layer does, basically it is a kind of feature extraction pooled=torch.nn.functional.max_pool(h,kernal_size=[1,user_length-filter_size+1,1,1],strides=[1,1,1,1],padding=0) pooled_outputs_u.append(pooled) num_filters_total = num_filters * len(filter_sizes) self.h_pool_u=torch.concat(3,pooled_outputs_u) self.h_pool_flat_u=torch.reshape(self.h_pool_u,[-1,num_filters_total])
参考:
链接1
链接2