commit 3e5c5fbb1658a47954160dc53f3b22170bcfa712
parent a8a6e19677c108647b0cb973d9c4181a0e5a931d
Author: David Freifeld <freifeld.david@gmail.com>
Date: Fri, 3 Jul 2020 11:04:54 -0700
Specific list_net() for CNN
Diffstat:
| M | cnn.cpp | | | 27 | +++++++++++++++++++++------ |
1 file changed, 21 insertions(+), 6 deletions(-)
diff --git a/cnn.cpp b/cnn.cpp
@@ -31,7 +31,7 @@ ConvLayer::ConvLayer(int x, int y, int stride, int kern_size)
void ConvLayer::convolute()
{
//std::cout << input->cols() << " " << input->cols() << "\n";
- std::cout << "Conv input:\n" << *input << "\nkernel:\n" << *kernel << "\n\n";
+ // std::cout << "Conv input:\n" << *input << "\nkernel:\n" << *kernel << "\n\n";
for (int i = 0; i < input->cols() - kernel->cols(); i+=stride_len) {
for (int j = 0; j < input->rows() - kernel->rows(); j+=stride_len) {
//std::cout << i << j << stride_len << "\n";
@@ -70,7 +70,7 @@ void PoolingLayer::pool()
{
// It doesn't look like anything better than O(n^4) is doable for this as kernel needs to go through matrix and you need to index kernel. LOOK INTO ME!!
float maxnum = -LARGE_NUM;
- std::cout << "Pool input:\n" << *input << "\n\n";
+ // std::cout << "Pool input:\n" << *input << "\n\n";
for (int i = 0; i < input->cols() - kernel->cols(); i+=stride_len) {
for (int j = 0; j < input->rows() - kernel->rows(); j+=stride_len) {
for (int k = 0; k < kernel->cols(); k++) {
@@ -94,6 +94,7 @@ public:
std::vector<PoolingLayer> pool_layers;
ConvNet(char* path, int batch_sz, float learn_rate, float bias_rate, float ratio);
+ void list_net();
void process(); // Runs the convolutional and pooling layers.
void next_batch();
void add_conv_layer(int x, int y, int stride, int kern_size);
@@ -120,7 +121,7 @@ void ConvNet::add_pool_layer(int x, int y, int stride, int kern_size)
// Needs a batch advancement function, 100% does not work.
void ConvNet::process()
{
- std::cout << preprocess_length << "\n";
+ // std::cout << preprocess_length << "\n";
// Assumes pooling is immediately after any conv layer.
for (int i = 0; i < preprocess_length-1; i++) {
conv_layers[i].convolute();
@@ -131,14 +132,27 @@ void ConvNet::process()
conv_layers[preprocess_length-1].convolute();
pool_layers[preprocess_length-1].input = conv_layers[preprocess_length-1].output;
pool_layers[preprocess_length-1].pool();
- std::cout << "Output:\n" << *pool_layers[preprocess_length-1].output << "\n\n";
+ // std::cout << "Output:\n" << *pool_layers[preprocess_length-1].output << "\n\n";
Eigen::Map<Eigen::RowVectorXd> flattened (pool_layers[preprocess_length-1].output->data(), pool_layers[preprocess_length-1].output->size());
- std::cout << "Flattened:\n" << flattened << "\n\n";
+ // std::cout << "Flattened:\n" << flattened << "\n\n";
for (int i = 0; i < flattened.cols(); i++) {
(*layers[0].contents)(0, i) = flattened[i];
}
}
+void ConvNet::list_net()
+{
+ for (int i = 0; i < preprocess_length; i++) {
+ std::cout << " CONVOLUTIONAL LAYER " << i << "\n\n" << *conv_layers[i].input << "\n\n WITH KERNEL\n" << *conv_layers[i].kernel << "\n\n AND OUTPUT \n" << *conv_layers[i].output << "\n\n\n";
+ std::cout << " POOLING LAYER " << i << "\n\n" << *pool_layers[i].input << "\n\n WITH KERNEL\n" << *pool_layers[i].kernel << "\n\n AND OUTPUT \n" << *pool_layers[i].output << "\n\n\n";
+ }
+ std::cout << " INPUT LAYER (LAYER 0)\n\n" << *layers[0].contents << "\n\n WITH BIAS\n" << *layers[0].bias << "\n\n AND WEIGHTS \n" << *layers[0].weights << "\n\n\n";
+ for (int i = 1; i < length-1; i++) {
+ std::cout << " LAYER " << i << "\n\n" << *layers[i].contents << "\n\n WITH BIAS\n" << *layers[i].bias << "\n\n AND WEIGHTS \n" << *layers[i].weights << "\n\n\n";
+ }
+ std::cout << " OUTPUT LAYER (LAYER " << length-1 << ")\n\n" << *layers[length-1].contents << "\n\n WITH BIAS\n" << *layers[length-1].bias << "\n\n\n";
+}
+
int main()
{
ConvNet net ("./data_banknote_authentication.txt", 1, 0.05, 0.01, 0.9);
@@ -149,7 +163,8 @@ int main()
net.add_layer(1, "resig");
net.initialize();
Eigen::MatrixXd* input = new Eigen::MatrixXd (8,8);
- *input << 0,0,0,0,0,0,0,0,
+ *input <<
+ 0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,1,1,1,1,0,0,
0,0,1,1,1,1,0,0,