Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is Tensorflow equivalent of pytorch's conv1d?

Just wondering how I can perform 1D convolution in tensorflow. Specifically, looking to replace this code to tensorflow:

inputs = F.pad(inputs, (kernel_size-1,0), 'constant', 0)

output = F.conv1d(inputs, weight, padding=0, groups=num_heads)
like image 894
user3086871 Avatar asked Jun 30 '19 01:06

user3086871


1 Answers

Tensorflow equivalent of PyTorch's

torch.nn.functional.conv1d() is tf.nn.conv1d() and torch.nn.functional.pad() is tf.pad().

For Example:

(PyTorch code)

import torch.nn as nn
import torch

inputs = torch.tensor([1, 0, 2, 3, 0, 1, 1], dtype=torch.float32)
filters = torch.tensor([2, 1, 3], dtype=torch.float32) 

inputs = inputs.unsqueeze(0).unsqueeze(0)                   # torch.Size([1, 1, 7])
filters = filters.unsqueeze(0).unsqueeze(0)                 # torch.Size([1, 1, 3])
conv_res = F.conv1d(inputs, filters, padding=0, groups=1)   # torch.Size([1, 1, 5])
pad_res = F.pad(conv_res, (1, 1), mode='constant', value=0) # torch.Size([1, 1, 7])

output:

tensor([[[ 0.,  8., 11.,  7.,  9.,  4.,  0.]]])

(Tensorflow code)

import tensorflow as tf
tf.enable_eager_execution()

i = tf.constant([1, 0, 2, 3, 0, 1, 1], dtype=tf.float32)
k = tf.constant([2, 1, 3], dtype=tf.float32, name='k')

data = tf.reshape(i, [1, int(i.shape[0]), 1], name='data')
kernel = tf.reshape(k, [int(k.shape[0]), 1, 1], name='kernel')

res = tf.nn.conv1d(data, kernel, 1, 'VALID')
res = tf.pad(res[0], [[1, 1], [0, 0]], "CONSTANT")

output:

<tf.Tensor: id=555, shape=(7, 1), dtype=float32, numpy=
array([[ 0.],
       [ 8.],
       [11.],
       [ 7.],
       [ 9.],
       [ 4.],
       [ 0.]], dtype=float32)>
like image 140
Anubhav Singh Avatar answered Nov 17 '22 03:11

Anubhav Singh