Training isn't working, CNN classifier

username: jacobhm98
cooked: <p>Hello! I am trying to build a binary classifier using a convolutional neural network architecture. I have finished writing the model and dataset, and the training and validation loops. Everything seems fine when I train it, the parameters in the network are registered correctly, predictions are being generated etc… However, my validation accuracy does not budge from 50%, ie chance. This is strange, as I’m training and validating it on a cats and dogs training set that I know should work for this architecture. Any ideas what may be causing this error?</p>
<p>CNN architecture:</p>
<pre><code class="lang-auto">class ConvNet : public torch::nn::Module {
	public:
		ConvNet(){
			register_module("layer1", layer1);
			register_module("layer2", layer2);
			register_module("fc", fc);
		};
		torch::Tensor forward(torch::Tensor x){
			x = layer1-&gt;forward(x);
			x = layer2-&gt;forward(x);
			x = x.view({-1, x.size(1) * x.size(2) * x.size(3)});
			x = fc-&gt;forward(x);
			return x;
		};

	private:
		torch::nn::Sequential layer1{
			torch::nn::Conv2d(torch::nn::Conv2dOptions(3, 64, 3).stride(1).padding(1)),
			torch::nn::ReLU(),
			torch::nn::MaxPool2d(torch::nn::MaxPool2dOptions({2, 2}).stride(2))
			};

		torch::nn::Sequential layer2{
			torch::nn::Conv2d(torch::nn::Conv2dOptions(64, 64, 3).stride(1).padding(1)),
			torch::nn::ReLU(),
			torch::nn::MaxPool2d(torch::nn::MaxPool2dOptions({2, 2}).stride(2))
			};

		torch::nn::Sequential fc{
			torch::nn::Linear(torch::nn::LinearOptions(64 * 50 * 125, 64)),
			torch::nn::ReLU(),
			torch::nn::Linear(torch::nn::LinearOptions(64, 1)),
			torch::nn::Sigmoid()
			};
			
};
Training loop:

</code></pre>
<p>void trainModel(std::shared_ptr cnn, std::string folder1, std::string folder2){</p>
<pre><code>cout &lt;&lt; "training.." &lt;&lt; endl;
cnn-&gt;train();
double learning_rate = 0.01;
torch::optim::Adam optimizer(cnn-&gt;parameters(), torch::optim::AdamOptions(learning_rate));
auto lossFunction = torch::nn::BCELoss();

int batch_size = 1;
auto dataset = CustomDataset(folder1, folder2).map(torch::data::transforms::Stack&lt;&gt;());
cout &lt;&lt; "dataset made" &lt;&lt; endl;
auto data_loader = torch::data::make_data_loader&lt;torch::data::samplers::SequentialSampler&gt;(dataset, batch_size);
int counter = 0;
for (auto&amp; batch : *data_loader){
	optimizer.zero_grad();
	torch::Tensor outputs = cnn-&gt;forward(batch.data);
	auto loss = lossFunction(outputs, batch.target);
	loss.backward();
	optimizer.step();
	if (counter &lt; 20){
		counter++;
	}
	else{
		cout &lt;&lt; loss &lt;&lt; endl;
		counter = 0;
	}
}

torch::save(cnn, "cnn.pt");
</code></pre>
<p>}</p>
<p>Validation loop:</p>
<p>void testModel(std::shared_ptr cnn, std::string folder1, std::string folder2){</p>
<pre><code>cout &lt;&lt; "testing.." &lt;&lt; endl;
auto dataset = CustomDataset(folder1, folder2).map(torch::data::transforms::Stack&lt;&gt;());
auto data_loader = torch::data::make_data_loader&lt;torch::data::samplers::SequentialSampler&gt;(dataset, 1 /*batch size*/);
cout &lt;&lt; "dataset made" &lt;&lt; endl;
cnn-&gt;eval();
double correct = 0;
size_t total =  dataset.size().value();
for (auto &amp;batch : *data_loader){
	torch::Tensor outputs = cnn-&gt;forward(batch.data);
	correct += outputs.eq(batch.target).sum().template item&lt;int64_t&gt;();	
}
double percentage = correct/total;
cout &lt;&lt; "size of validation sample: " &lt;&lt; total &lt;&lt; endl;
cout &lt;&lt; "validation accuracy: " &lt;&lt; percentage &lt;&lt; endl;
</code></pre>
<p>}</p>
<pre><code class="lang-auto"></code></pre>

username: futscdav
cooked: <p>Is your training loss going down? LR of 0.01 is generally a bit high for Adam in my experience.</p>

username: jacobhm98
cooked: <p>I edited my loss metric to reflect the algorithm given in the “training a classifier” example, and changed LR to 0.001. Alas, the loss does not seem to be decreasing systematically.</p>