PixelCNN on 1 Dimension

username: danielafrimi
cooked: <p>Hi,<br>
I’m trying to modify PixelCNN model to work on 1D as well.<br>
Let’s say i want the input of the network to be vector (size 20) and output a vector (size 60), I tried to play with the architecture to make it work but it didnt work.</p>
<p>First of all i changed to Conv2D to be 1D (the mask work on 1D now). but i dont understand how to make it train.</p>
<p>this is the architecture of the model:</p>
<pre><code>class PixelCNN(nn.Module):
def __init__(self, no_layers=8, kernel = 7, channels=64, device=None, e_dim=16, z_dim=64):
	super(PixelCNN, self).__init__()
	self.no_layers = no_layers
	self.kernel = kernel
	self.channels = channels
	self.layers = {}
	self.device = device

	self.Conv2d_1 = MaskedCNN('A',1,channels, kernel, 1, kernel//2, bias=False)
	self.BatchNorm2d_1 = nn.BatchNorm2d(channels)
	self.ReLU_1= nn.ReLU(True)

	self.Conv2d_2 = MaskedCNN('B',channels,channels, kernel, 1, kernel//2, bias=False)
	self.BatchNorm2d_2 = nn.BatchNorm2d(channels)
	self.ReLU_2= nn.ReLU(True)

	self.Conv2d_3 = MaskedCNN('B',channels,channels, kernel, 1, kernel//2, bias=False)
	self.BatchNorm2d_3 = nn.BatchNorm2d(channels)
	self.ReLU_3= nn.ReLU(True)

	self.Conv2d_4 = MaskedCNN('B',channels,channels, kernel, 1, kernel//2, bias=False)
	self.BatchNorm2d_4 = nn.BatchNorm2d(channels)
	self.ReLU_4 = nn.ReLU(True)

	self.Conv2d_5 = MaskedCNN('B',channels,channels, kernel, 1, kernel//2, bias=False)
	self.BatchNorm2d_5 = nn.BatchNorm2d(channels)
	self.ReLU_5= nn.ReLU(True)

	self.Conv2d_6 = MaskedCNN('B',channels,channels, kernel, 1, kernel//2, bias=False)
	self.BatchNorm2d_6 = nn.BatchNorm2d(channels)
	self.ReLU_6= nn.ReLU(True)

	self.Conv2d_7 = MaskedCNN('B',channels,channels, kernel, 1, kernel//2, bias=False)
	self.BatchNorm2d_7 = nn.BatchNorm2d(channels)
	self.ReLU_7= nn.ReLU(True)

	self.Conv2d_8 = MaskedCNN('B',channels,channels, kernel, 1, kernel//2, bias=False)
	self.BatchNorm2d_8 = nn.BatchNorm2d(channels)
	self.ReLU_8= nn.ReLU(True)

	self.out = nn.Conv2d(channels, 256, 1)
</code></pre>
<p>MaskedCNN defined as:</p>
<pre><code>class MaskedCNN(nn.Conv2d):
def __init__(self, mask_type, *args, **kwargs):
	self.mask_type = mask_type

	assert mask_type in ['A', 'B'], "Unknown Mask Type"
	super(MaskedCNN, self).__init__(*args, **kwargs)
	self.register_buffer('mask', self.weight.data.clone())

	_, depth, height, width = self.weight.size()
	self.mask.fill_(1)
	if mask_type =='A':
		self.mask[:,:,height//2,width//2:] = 0
		self.mask[:,:,height//2+1:,:] = 0
	else:
		self.mask[:,:,height//2,width//2+1:] = 0
		self.mask[:,:,height//2+1:,:] = 0


def forward(self, x):
	self.weight.data*=self.mask
	return super(MaskedCNN, self).forward(x)
</code></pre>
<p>Thanks <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

username: kkpytorch
cooked: <p>I just tested this network. But I didn’t try 1D version.<br>
I think you should prepare your data at first.<br>
For each iteration, feed the network data.<br>
The network above use conv2d, change it to con1d.<br>
The mask is also should be changed to a vector.<br>
1D is not more complex than 2D problem.</p>

username: danielafrimi
cooked: <p>Thanks.<br>
I did it but im still struggling to do this. when i tested this network on MNIST the input tensor  was [batch_size, 1, image_size, image_size] and the output tensor was [batch_size, 256, image_size, image_size].</p>
<p>if i want to pass tensor of vector of size 20 (i prepared the data) like [batch_size, 20] and then i want to use the same loss function (crossEntropy) i don’t understand how to do it.</p>

username: Intech
cooked: <p>Did you make it work? I need to do something similar with time series but I have the same problem.</p>