The dimensionality of the PyTorch inputs are not what the model expects, and I am not sure why.
To my understanding...
in_channels is first the number of 1D inputs we would like to pass to the model, and is the previous out_channel for all subsequent layers.
out_channels is the desired number of kernels (filters).
kernel_size is the number of parameters per filter.
Therefore, we would expect, as data passed to forward, a dataset with 7 1D channels (i.e. a 2D input).
However, the following code throws an error that is not consistent with what I expect, where this code:
import numpy
import torch
X = numpy.random.uniform(-10, 10, 70).reshape(-1, 7)
# Y = np.random.randint(0, 9, 10).reshape(-1, 1)
class Simple1DCNN(torch.nn.Module):
def __init__(self):
super(Simple1DCNN, self).__init__()
self.layer1 = torch.nn.Conv1d(in_channels=7, out_channels=20, kernel_size=5, stride=2)
self.act1 = torch.nn.ReLU()
self.layer2 = torch.nn.Conv1d(in_channels=20, out_channels=10, kernel_size=1)
def forward(self, x):
x = self.layer1(x)
x = self.act1(x)
x = self.layer2(x)
log_probs = torch.nn.functional.log_softmax(x, dim=1)
return log_probs
model = Simple1DCNN()
print(model(torch.tensor(X)).size)
Throws the following error:
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-5-eca5856a2314> in <module>()
21
22 model = Simple1DCNN()
---> 23 print(model(torch.tensor(X)).size)
~/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
487 result = self._slow_forward(*input, **kwargs)
488 else:
--> 489 result = self.forward(*input, **kwargs)
490 for hook in self._forward_hooks.values():
491 hook_result = hook(self, input, result)
<ipython-input-5-eca5856a2314> in forward(self, x)
12 self.layer2 = torch.nn.Conv1d(in_channels=20, out_channels=10, kernel_size=1)
13 def forward(self, x):
---> 14 x = self.layer1(x)
15 x = self.act1(x)
16 x = self.layer2(x)
~/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
487 result = self._slow_forward(*input, **kwargs)
488 else:
--> 489 result = self.forward(*input, **kwargs)
490 for hook in self._forward_hooks.values():
491 hook_result = hook(self, input, result)
~/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages/torch/nn/modules/conv.py in forward(self, input)
185 def forward(self, input):
186 return F.conv1d(input, self.weight, self.bias, self.stride,
--> 187 self.padding, self.dilation, self.groups)
188
189
RuntimeError: Expected 3-dimensional input for 3-dimensional weight [20, 7, 5], but got 2-dimensional input of size [10, 7] instead
Edit: See below for solution, motivated by Shai.
import numpy
import torch
X = numpy.random.uniform(-10, 10, 70).reshape(1, 7, -1)
# Y = np.random.randint(0, 9, 10).reshape(1, 1, -1)
class Simple1DCNN(torch.nn.Module):
def __init__(self):
super(Simple1DCNN, self).__init__()
self.layer1 = torch.nn.Conv1d(in_channels=7, out_channels=20, kernel_size=5, stride=2)
self.act1 = torch.nn.ReLU()
self.layer2 = torch.nn.Conv1d(in_channels=20, out_channels=10, kernel_size=1)
def forward(self, x):
x = self.layer1(x)
x = self.act1(x)
x = self.layer2(x)
log_probs = torch.nn.functional.log_softmax(x, dim=1)
return log_probs
model = Simple1DCNN().double()
print(model(torch.tensor(X)).shape)
You are forgetting the "minibatch dimension", each "1D" sample has indeed two dimensions: the number of channels (7 in your example) and length (10 in your case). However, pytorch expects as input not a single sample, but rather a minibatch of B samples stacked together along the "minibatch dimension".
So a "1D" CNN in pytorch expects a 3D tensor as input: BxCxT. If you only have one signal, you can add a singleton dimension:
out = model(torch.tensor(X)[None, ...])
Related
I'm new in pytorch geometric, and when running my model I obtain this error:
RuntimeError: mat1 dim 1 must match mat2 dim 0
The error occurs while running this code, and it happens at the z = model.encode(x, train_pos_edge_index) line
def train():
model.train()
optimizer.zero_grad()
z = model.encode(x, train_pos_edge_index)
loss = model.recon_loss(z, train_pos_edge_index)
loss = loss + (1 / data.num_nodes) * model.kl_loss() # new line
loss.backward()
optimizer.step()
return float(loss)
The class through which I generate my model is the following:
class VariationalGCNEncoder(torch.nn.Module):
def __init__(self, in_channels, out_channels):
super(VariationalGCNEncoder, self).__init__()
self.conv1 = GCNConv(in_channels, 2 * out_channels, cached=True) # cached only for transductive learning
self.conv_mu = GCNConv(2 * out_channels, out_channels, cached=True)
self.conv_logstd = GCNConv(2 * out_channels, out_channels, cached=True)
def forward(self, x, edge_index):
x = self.conv1(x, edge_index).relu()
return self.conv_mu(x, edge_index), self.conv_logstd(x, edge_index)
Using VGAE from torch_geometric.nn, I create the model as follows:
out_channels = 2 #dimension of the embedding
num_features = 128
model = VGAE(VariationalGCNEncoder(num_features, out_channels))
The dimensionality of the variables is the following:
test_neg_edge_index=[2, 68],
test_pos_edge_index=[2, 68],
train_neg_adj_mask=[132, 132],
train_pos_edge_index=[2, 1166],
val_neg_edge_index=[2, 34],
val_pos_edge_index=[2, 34],
x=[132, 132]
And the dimensionality of the layers in the model is the following:
VGAE(
(encoder): VariationalGCNEncoder(
(conv1): GCNConv(128, 4)
(conv_mu): GCNConv(4, 2)
(conv_logstd): GCNConv(4, 2)
)
(decoder): InnerProductDecoder()
)
The full error backtrace is the following:
Traceback (most recent call last)
<ipython-input-20-af87b3233297> in <module>
2
3 for epoch in range(1, epochs + 1):
----> 4 loss = train()
5 auc, ap = test(data.test_pos_edge_index, data.test_neg_edge_index)
6 print('Epoch: {:03d}, AUC: {:.4f}, AP: {:.4f}'.format(epoch, auc, ap))
<ipython-input-19-e42a4a22847f> in train()
2 model.train()
3 optimizer.zero_grad()
----> 4 z = model.encode(x, train_pos_edge_index)
5 loss = model.recon_loss(z, train_pos_edge_index)
6
~\anaconda3\lib\site-packages\torch_geometric\nn\models\autoencoder.py in encode(self, *args, **kwargs)
153 def encode(self, *args, **kwargs):
154 """"""
--> 155 self.__mu__, self.__logstd__ = self.encoder(*args, **kwargs)
156 self.__logstd__ = self.__logstd__.clamp(max=MAX_LOGSTD)
157 z = self.reparametrize(self.__mu__, self.__logstd__)
~\anaconda3\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)
720 result = self._slow_forward(*input, **kwargs)
721 else:
--> 722 result = self.forward(*input, **kwargs)
723 for hook in itertools.chain(
724 _global_forward_hooks.values(),
<ipython-input-16-d5c82de35dd4> in forward(self, x, edge_index)
7
8 def forward(self, x, edge_index):
----> 9 x = self.conv1(x, edge_index).relu()
10 return self.conv_mu(x, edge_index), self.conv_logstd(x, edge_index)
~\anaconda3\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)
720 result = self._slow_forward(*input, **kwargs)
721 else:
--> 722 result = self.forward(*input, **kwargs)
723 for hook in itertools.chain(
724 _global_forward_hooks.values(),
~\anaconda3\lib\site-packages\torch_geometric\nn\conv\gcn_conv.py in forward(self, x, edge_index, edge_weight)
177 edge_index = cache
178
--> 179 x = x # self.weight
180
181 # propagate_type: (x: Tensor, edge_weight: OptTensor)
RuntimeError: mat1 dim 1 must match mat2 dim 0
I have some MRI scans, where each scan is a set of 31 RGB images. The dimensions of the input data are (Channels, Depth, Height, Width). The images are png, and each scan is a folder containing its 31 images.
I created a custom Dataset class:
class TrainImages(Dataset):
def __init__(self, csv_file, root_dir, transform=None):
self.annotations = pd.read_csv(csv_file)
self.root_dir = root_dir
self.transform = transform
def __len__(self):
return len(self.annotations)
def __getitem__(self, index):
img_path = os.path.join(self.root_dir, str(self.annotations.iloc[index, 0]).zfill(5))
image = torch.from_numpy(np.array([np.array(Image.open(os.path.join(str(img_path),"rgb-"+str(i)+".png"))) for i in range(31)]).transpose(3,0,1,2).astype(np.float32))
y_label = torch.tensor(int(self.annotations.iloc[index, 1]))
return (image, y_label)
Then, I created a small 3D CNN class:
class CNN2(nn.Module):
def __init__(self):
super(CNN2, self).__init__()
self.conv_layer1 = self._conv_layer(3, 12)
def _conv_layer(self, in_c, out_c, conv_kernel_size=3, padding=0):
layer = nn.Sequential(
nn.Conv3d(in_c, out_c, conv_kernel_size, padding),
)
return layer
def forward(self, x):
out = self.conv_layer1(x)
return out
Then, I tried to feed one scan into the CNN2 object:
x=torch.unsqueeze(dataset[0][0], 0)
x.shape #torch.Size([1, 3, 31, 512, 512])
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
data = x.to(device)
model = CNN2().to(device)
model(x)
But it produces this error:
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-24-c89306854a22> in <module>
1 model = CNN_test().to(device)
----> 2 model(x)
/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
725 result = self._slow_forward(*input, **kwargs)
726 else:
--> 727 result = self.forward(*input, **kwargs)
728 for hook in itertools.chain(
729 _global_forward_hooks.values(),
<ipython-input-22-ac66ca7a2459> in forward(self, x)
14
15 def forward(self, x):
---> 16 out = self.conv_layer1(x)
17
18 return out
/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
725 result = self._slow_forward(*input, **kwargs)
726 else:
--> 727 result = self.forward(*input, **kwargs)
728 for hook in itertools.chain(
729 _global_forward_hooks.values(),
/opt/conda/lib/python3.7/site-packages/torch/nn/modules/container.py in forward(self, input)
115 def forward(self, input):
116 for module in self:
--> 117 input = module(input)
118 return input
119
/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
725 result = self._slow_forward(*input, **kwargs)
726 else:
--> 727 result = self.forward(*input, **kwargs)
728 for hook in itertools.chain(
729 _global_forward_hooks.values(),
/opt/conda/lib/python3.7/site-packages/torch/nn/modules/conv.py in forward(self, input)
571 self.dilation, self.groups)
572 return F.conv3d(input, self.weight, self.bias, self.stride,
--> 573 self.padding, self.dilation, self.groups)
574
575
RuntimeError: non-positive stride is not supported
However, when I just create a Conv3D object and pass the same scan in, no error results:
x=torch.unsqueeze(dataset[0][0], 0)
m=nn.Conv3d(3,12,3)
out=m(x)
I think the error might have to do with the dimensions of the input data, but I don't understand what "non-positive stride" means. I'm also confused why no error occurs when I just pass the data into a Conv3D object, but an error occurs when I pass the same data into an instance of the CNN class that does the same thing.
The issue is not with your input shape, it has to do with your layer initialization. You have essentially defined your 3D convolution with this line:
nn.Conv3d(in_c, out_c, conv_kernel_size, padding)
The issue is nn.Conv3d function head is the following:
torch.nn.Conv3d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, padding_mode='zeros', device=None, dtype=None)
Notice how stride is placed before padding. In your code, variable padding ends up being assigned to the stride argument.
To solve the issue, you can specify the argument name with a keyword argument, i.e. padding=padding. This disambiguates the issue with positional arguments stride and padding.
class CNN2(nn.Module):
def __init__(self):
super(CNN2, self).__init__()
self.conv_layer1 = self._conv_layer(3, 12)
def _conv_layer(self, in_c, out_c, conv_kernel_size=3, padding=0):
layer = nn.Sequential(
nn.Conv3d(in_c, out_c, conv_kernel_size, padding=padding))
return layer
def forward(self, x):
out = self.conv_layer1(x)
return out
I have some trouble with IndexError in my GCN code.
The error occurred on the GCNConv layer.
I cannot understand why self.node_dim is included in the size function, shown in the error code.
How can I solve this problem?
data.x is node_feature in which the number of features for each node is just one, and the number of nodes is 2058.
Thanks in advance.
'''
class GCNEncoder(torch.nn.Module):
def __init__(self, in_channels, out_channels):
super(GCNEncoder, self).__init__()
self.conv1 = GCNConv(in_channels, out_channels, cached=True)
self.conv2 = GCNConv(out_channels, out_channels, cached=True)
def forward(self, x, edge_index):
x = self.conv1(x, edge_index).relu()
return self.conv2(x, edge_index)
class OurGAE(torch.nn.Module):
def __init__(self, num_features, out_chennels, node):
super(OurGAE, self).__init__()
self.encoder = GCNEncoder(num_features, out_channels)
self.linear = nn.Linear(out_channels, len(node))
def encode(self, node_feature, edge_lst):
z = self.encoder(node_feature, edge_lst)
return z
def predict(self, z):
predicted_node_features = self.linear(z)
return predicted_node_features
############ train model #######
dataset = OurDataset(one_node_feature, super_edge_lst)
out_channels = 16
num_features = dataset.num_features
model = OurGAE(num_features, out_channels, node)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = model.to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=1e-3)
epochs = 100
criterion = nn.MSELoss()
#model.train()
for epoch in range(1, epochs + 1):
loss_ep = 0
for data in dataset:
optimizer.zero_grad()
x = data.x.to(device)
edge_lst = data.edge_index.to(device)
z = model.encode(x, edge_lst)
super_z = z[-1]
predicted_x = model.predict(super_z)
loss = criterion(x.reshape(-1), predicted_x)
loss.backward()
optimizer.step()
loss_ep += loss.cpu().detach().data.numpy()
if epoch % 1 == 0:
print(f"epoch: {epoch}, loss : {loss_ep}") '''
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-103-9dd243b81c8e> in <module>
71 edge_lst = data.edge_index.to(device)
72
---> 73 z = model.encode(x, edge_lst)
74
75 super_z = z[-1]
<ipython-input-103-9dd243b81c8e> in encode(self, node_feature, edge_lst)
36
37 def encode(self, node_feature, edge_lst):
---> 38 z = self.encoder(node_feature, edge_lst)
39 return z
40
~/anaconda3/lib/python3.8/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
725 result = self._slow_forward(*input, **kwargs)
726 else:
--> 727 result = self.forward(*input, **kwargs)
728 for hook in itertools.chain(
729 _global_forward_hooks.values(),
<ipython-input-103-9dd243b81c8e> in forward(self, x, edge_index)
15
16 def forward(self, x, edge_index):
---> 17 x = self.conv1(x, edge_index).relu()
18 return self.conv2(x, edge_index)
19
~/anaconda3/lib/python3.8/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
725 result = self._slow_forward(*input, **kwargs)
726 else:
--> 727 result = self.forward(*input, **kwargs)
728 for hook in itertools.chain(
729 _global_forward_hooks.values(),
~/anaconda3/lib/python3.8/site-packages/torch_geometric/nn/conv/gcn_conv.py in forward(self, x, edge_index, edge_weight)
159 if cache is None:
160 edge_index, edge_weight = gcn_norm( # yapf: disable
--> 161 edge_index, edge_weight, x.size(self.node_dim),
162 self.improved, self.add_self_loops)
163 if self.cached:
IndexError: Dimension out of range (expected to be in range of [-1, 0], but got -2)
TLDR: reshape your data-points to [num_nodes, num_node_features] =>[2058, 1] before constructing the dataset.
It looks like the OurDataset you created doesn't fit the assumptions of the graph dataset. I just assume this is because you have only one feature at each node, and you most probably didn't reshape the data before passing it to the Dataset.
Taken from the documentation: Data Handling of Graphs
data.x: Node feature matrix with shape [num_nodes, num_node_features]
from torch_geometric.data import Data
edge_index = torch.tensor([[0, 1, 1, 2], [1, 0, 2, 1]], dtype=torch.long)
x_wrong_dims = torch.tensor([-1, 0, 1], dtype=torch.float)
data_wrong_dims = Data(x=x_wrong_dims, edge_index=edge_index)
data_wrong_dims.x.size()
# torch.Size([3])
data_wrong_dims.x.size(-2)
# IndexError: Dimension out of range (expected to be in range of [-1, 0], but got -2)
x_correct_dims = torch.tensor([-1, 0, 1], dtype=torch.float).reshape([-1, 1])
data_correct_dims = Data(x=x_correct_dims, edge_index=edge_index)
data_correct_dims.x.size()
# torch.Size([3, 1])
data_correct_dims.x.size(-2)
# 3
As to the question why gcn_norm uses the x.size(self.node_dim) I would assume that the function has to infer along which dimension to aggregate for the normalization and node_dim is -2 by default which means it aggregates over the second to last dimensions which in the case of the correctly shaped dataset is the num_nodes dimension.
I m having some issues with pytorch after I train my model, it seems that the type i pass into my network changes, i.e when i try to do.a single prediction my forward function has an error as it has no ".dim" function even though I pass a torch.tensor object as input. this is my basic network architecture
class Net(nn.Module):
def __init__(self,input_shape):
super(Net,self).__init__()
self.fc1 = nn.Linear(input_shape,2)
self.fc2 = nn.Dropout(p=0.2)
self.fc3 = nn.Linear(2,2)
def forward(self,x):
x = torch.relu(self.fc1(x))
x = torch.softmax(self.fc3(x), 1)
return x
it trains and works as expected on the training batch but when i pass a single tensor
torch.Size([1, 100])
it gives me this error
x = torch.relu(self.fc1(x)) AttributeError: 'builtin_function_or_method' object has no attribute 'dim'
The network was trained with batches of
torch.Size([64, 100])
And it worked during training on the batches of 64 and output the results I expected when when I perform a single prediction using
prediction = model(X[0])
I encounter this error. I have tried a few things such as printing the type to verify that it was a valid object being passed in but it seems that somehow during the forward pass the types I have passed become invalid and I cannot understand why if my input is a singular element of the original training data . I also tried wrapping the X[0] inside a Variable() as Im unsure exactly what pytorch seems to expect as input given this error. I have also read other answers but they seem to address changes mostly in other layers I am not using as returning different values and I am unsure how to verify if the output of one of my layers is changing the type passed into the next layer.
Any help would be appreciated.
EDITED------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-602-1f45fd5f7727> in <module>
---> 12 y_pred = model(X[0])
/opt/conda/envs/pytorch-py35/lib/python3.5/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
487 result = self._slow_forward(*input, **kwargs)
488 else:
--> 489 result = self.forward(*input, **kwargs)
490 for hook in self._forward_hooks.values():
491 hook_result = hook(self, input, result)
<ipython-input-595-74840ffbc280> in forward(self, x)
13 self.fc3 = nn.Linear(2,2)
14 def forward(self,x):
---> 15 x = torch.relu(self.fc1(x))
16 x = torch.softmax(self.fc3(x), 1)
17 return x
/opt/conda/envs/pytorch-py35/lib/python3.5/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
487 result = self._slow_forward(*input, **kwargs)
488 else:
--> 489 result = self.forward(*input, **kwargs)
490 for hook in self._forward_hooks.values():
491 hook_result = hook(self, input, result)
/opt/conda/envs/pytorch-py35/lib/python3.5/site-packages/torch/nn/modules/linear.py in forward(self, input)
65 #weak_script_method
66 def forward(self, input):
---> 67 return F.linear(input, self.weight, self.bias)
68
69 def extra_repr(self):
/opt/conda/envs/pytorch-py35/lib/python3.5/site-packages/torch/nn/functional.py in linear(input, weight, bias)
1348 - Output: :math:`(N, *, out\_features)`
1349 """
-> 1350 if input.dim() == 2 and bias is not None:
1351 # fused op is marginally faster
1352 ret = torch.addmm(torch.jit._unwrap_optional(bias), input, weight.t())
AttributeError: 'builtin_function_or_method' object has no attribute 'dim'
Here is my training batching also incase this is useful.
from torch.utils.data import Dataset, DataLoader #Define a dataset and trainer for pytorch
class dataset(Dataset):
def __init__(self,x,y):
self.x = x.clone().detach().requires_grad_(True)
self.y = y.clone().detach().requires_grad_(False)
self.length = self.x.shape[0]
def __getitem__(self,idx):
return self.x[idx],self.y[idx]
def __len__(self):
return self.length
trainset = dataset(X,y)
trainloader = DataLoader(trainset,batch_size=64,shuffle=False)
EDITED------------------------------------------------------------------------
To add more details:
If i print(X)
tensor([[ 1.0430, -0.8477, 0.9932, ..., -0.8662, 0.6748, -0.4958],
[ 2.0728, -1.3987, 2.0863, ..., -1.2584, 0.9423, -1.1652],
[ 1.1594, -0.2600, 1.1863, ..., -0.2268, -0.0596, -1.2434],
...,
[ 1.2905, -2.1808, 1.2301, ..., -2.4004, 2.1426, 0.8747],
[-0.4722, -1.2714, -0.4748, ..., -1.3785, 1.6691, 1.8886],
[ 0.6362, -1.1497, 0.6117, ..., -1.1174, 1.0634, 0.2901]],
dtype=torch.float64)
and to actually train the network im using:
output = model(x_train)
loss = loss_fn(output,y_train)
predicted = model(X.clone().detach().requiresgrad(True).float())
optimizer.zero_grad()
loss.backward()
optimizer.step()
EDITED------------------------------------------------------------------------
If i add the type passed into forward i get this during training
def forward(self,x):
print(type(X))
x = torch.relu(self.fc1(x))
x = torch.softmax(self.fc3(x), 1)
return x
<class 'torch.Tensor'>
<class 'torch.Tensor'>
I am running a GAN model however it looks like in the generator forward area, I am getting a size mismatch. I am unsure on how to fix this.
The entire error message is below.
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-25-5498a3d56294> in <module>
190 print('{}. {}: {:.4f}'.format(idx+1, ccle_features.index[top], pval[top]))
191
--> 192 run_test_ccle(X_drug[:,ccle_selected],y_drug)
<ipython-input-25-5498a3d56294> in run_test_ccle(X, Y)
182 # now run test
183 #pval.append(GCIT(x,Y,z))
--> 184 pval.append(EBGCIT(x,Y,z))
185
186 ccle_features = features[ccle_selected]
<ipython-input-24-d7ac34907be1> in EBGCIT(x, y, z, n_iter)
66
67 G_loss=0
---> 68 d_input=torch.cat((G[j](g_input),z),axis=1)
69 Gen_Dis=D(d_input)
70 G_loss=loss_criteria(Gen_Dis.squeeze(),real)
~/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
720 result = self._slow_forward(*input, **kwargs)
721 else:
--> 722 result = self.forward(*input, **kwargs)
723 for hook in itertools.chain(
724 _global_forward_hooks.values(),
<ipython-input-23-6f461c71bb16> in forward(self, x)
32 # Linear function
33 #print(x.shape)
---> 34 out = self.fc1(x)
35 out = self.tanh(out)
36 # out = self.fc2(out)
~/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
720 result = self._slow_forward(*input, **kwargs)
721 else:
--> 722 result = self.forward(*input, **kwargs)
723 for hook in itertools.chain(
724 _global_forward_hooks.values(),
~/anaconda3/lib/python3.7/site-packages/torch/nn/modules/linear.py in forward(self, input)
89
90 def forward(self, input: Tensor) -> Tensor:
---> 91 return F.linear(input, self.weight, self.bias)
92
93 def extra_repr(self) -> str:
~/anaconda3/lib/python3.7/site-packages/torch/nn/functional.py in linear(input, weight, bias)
1672 if input.dim() == 2 and bias is not None:
1673 # fused op is marginally faster
-> 1674 ret = torch.addmm(bias, input, weight.t())
1675 else:
1676 output = input.matmul(weight.t())
RuntimeError: size mismatch, m1: [64 x 103], m2: [550 x 50] at /Users/distiller/project/conda/conda-bld/pytorch_1595629430416/work/aten/src/TH/generic/THTensorMath.cpp:41
This is a portion of my code for my EBGCIT function.
def EBGCIT(x, y, z, n_iter=1000):
J_g=10
J_d=1
# Generator and Discriminator
G=[None]*J_g
lr_g=0.005
noise_G=[None]*J_g
G_optimizer=[None]*J_g
for i in range(J_g):
G[i]=Generator(z_dim+v_dim,h_dim, x_dim)
G_optimizer[i]=torch.optim.SGD(G[i].parameters(),lr=lr_g)
D=Discriminator(x_dim+z_dim,h_dim, x_dim)
D_optimizer=torch.optim.SGD(D.parameters(),lr=lr_g)
m=0
for p in G[0].parameters():
m=m+1
M=[]
for j in range(J_g):
M.append([None]*m)
for j in range(J_g):
m=0
for par in G[0].parameters():
M[j][m]=torch.zeros(par.size())
m=m+1
n = len(z[:, 0])
# define training and testing subsets, training for learning the sampler and
# testing for computing test statistic. Set 2/3 and 1/3 as default
x_train, y_train, z_train = x[:int(2*n/3),], y[:int(2*n/3),], z[:int(2*n/3),]
x_test, y_test, z_test = x[int(2*n/3):,], y[int(2*n/3):,], z[int(2*n/3):,]
n = len(z_train[:, 0])
# 2. # of confounders
z_train=torch.Tensor(z_train)
x_train=torch.Tensor(x_train)
z_test=torch.Tensor(z_test)
check=x_test
x_test=torch.Tensor(x_test)
for iter in range(1000):
# Train Generator
for j in range(J_g):
v=np.sqrt(1./3)*torch.randn(mini_batch,v_dim)
perm = torch.randperm(n)
idx=perm[:mini_batch]
z=z_train[idx]
g_input=torch.cat((z,v),axis=1)
G_loss=0
d_input=torch.cat((G[j](g_input),z),axis=1)
Gen_Dis=D(d_input)
G_loss=loss_criteria(Gen_Dis.squeeze(),real)
l2=0.0
for p in G[j].parameters():
l2=l2+(p**2).sum()/200
G_loss+=l2/n
G_optimizer[j].zero_grad()
G_loss.backward()
G_optimizer[j].step()
m=0
for par in G[j].parameters():
par.data.sub_(a*M[j][m]*lr_g/n)
m=m+1
with torch.no_grad():
for param in G[j].parameters():
param.add_(torch.randn(param.size()) * np.sqrt(2*lr_g/n))
m=0
for par in G[j].parameters():
M[j][m]*=beta_1
M[j][m]+=(1-beta_1)*par.grad.data*n
m=m+1
And this is the code for my generator and discriminator classes.
J_g=10
np.random.seed(1)
torch.manual_seed(1)
class Generator(nn.Module):
def __init__(self, input_dim, hidden_dim, output_dim):
super(Generator, self).__init__()
# Linear function
self.fc1 = nn.Linear(input_dim, hidden_dim)
nn.init.xavier_normal_(self.fc1.weight)
self.tanh = nn.Tanh()
self.fc3 = nn.Linear(hidden_dim, output_dim)
nn.init.xavier_normal_(self.fc3.weight)
def forward(self, x):
# Linear function
#print(x.shape)
out = self.fc1(x)
out = self.tanh(out)
out = self.fc3(out)
return out
class Discriminator(nn.Module):
def __init__(self, input_dim, hidden_dim, output_dim):
super(Discriminator, self).__init__()
# Linear function
self.fc1 = nn.Linear(input_dim, hidden_dim)
nn.init.xavier_normal_(self.fc1.weight)
self.relu = nn.ReLU()
self.fc3 = nn.Linear(hidden_dim, output_dim)
nn.init.xavier_normal_(self.fc3.weight)
def forward(self, x):
# Linear function
out = self.fc1(x)
out = self.relu(out)
out = self.fc3(out)
return out
mini_batch=64
loss_criteria=nn.BCEWithLogitsLoss()
real=torch.ones(mini_batch)
fake=torch.zeros(mini_batch)
a=1
beta_1=0.9
A=1
B=1000
sig=nn.Sigmoid()
Any help would be much appreciated. Thanks!!
we assume m1: [a x b], m2: [c x d]\
[a x b] = [batch size x in_channels]
[c x d] = [in_channels x out_channels]
So all we need to do is to make:
[b = c]