Skip to content

Commit 1817f7d

Browse files
committed
Added Basic Autograd Example and Linter
1 parent 0debfc0 commit 1817f7d

File tree

7 files changed

+47
-12
lines changed

7 files changed

+47
-12
lines changed

CMakeLists.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ target_link_libraries(pytorch-cpp "${TORCH_LIBRARIES}")
99

1010
add_executable(pytorch_basics tutorials/basics/pytorch_basics/main.cpp)
1111
target_link_libraries(pytorch_basics "${TORCH_LIBRARIES}")
12-
add_executable(linear_regression tutorials/basics/linear_regression/main.cpp)
13-
target_link_libraries(linear_regression "${TORCH_LIBRARIES}")
14-
add_executable(logistic_regression tutorials/basics/logistic_regression/main.cpp)
15-
target_link_libraries(logistic_regression "${TORCH_LIBRARIES}")
16-
add_executable(feedforward_neural_network tutorials/basics/feedforward_neural_network/main.cpp)
17-
target_link_libraries(feedforward_neural_network "${TORCH_LIBRARIES}")
12+
# add_executable(linear_regression tutorials/basics/linear_regression/main.cpp)
13+
# target_link_libraries(linear_regression "${TORCH_LIBRARIES}")
14+
# add_executable(logistic_regression tutorials/basics/logistic_regression/main.cpp)
15+
# target_link_libraries(logistic_regression "${TORCH_LIBRARIES}")
16+
# add_executable(feedforward_neural_network tutorials/basics/feedforward_neural_network/main.cpp)
17+
# target_link_libraries(feedforward_neural_network "${TORCH_LIBRARIES}")
1818

1919
set_property(TARGET pytorch-cpp PROPERTY CXX_STANDARD 11)

main.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
// Copyright 2019 Omkar Prabhu
12
#include <torch/torch.h>
23
#include <iostream>
34

45
int main() {
56
std::cout << "Welcome to PyTorch Tutorial in C++ for Deep Learning Researchers" << std::endl;
6-
}
7+
}

scripts.sh

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,17 @@ function build() {
1414
make
1515
}
1616

17+
function lint() {
18+
cpplint --linelength=120 main.cpp tutorials/*/*/**
19+
}
20+
1721
if [ $1 = "install" ]
1822
then
1923
install
20-
else
24+
elif [ $1 = "build" ]
25+
then
2126
build
27+
elif [ $1 = "lint" ]
28+
then
29+
lint
2230
fi
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
// Copyright 2019 Omkar Prabhu
12
#include <torch/torch.h>
23
#include <iostream>
34

45
int main() {
56
std::cout << "FeedForward Neural Network" << std::endl;
6-
}
7+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
// Copyright 2019 Omkar Prabhu
12
#include <torch/torch.h>
23
#include <iostream>
34

45
int main() {
56
std::cout << "Linear Regression" << std::endl;
6-
}
7+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
// Copyright 2019 Omkar Prabhu
12
#include <torch/torch.h>
23
#include <iostream>
34

45
int main() {
56
std::cout << "Logistic Regression" << std::endl;
6-
}
7+
}
Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,29 @@
1+
// Copyright 2019 Omkar Prabhu
12
#include <torch/torch.h>
3+
#include <torch/csrc/autograd/variable.h>
4+
#include <torch/csrc/autograd/function.h>
25
#include <iostream>
36

47
int main() {
58
std::cout << "PyTorch Basics" << std::endl;
6-
}
9+
10+
///////////////////////////////////////////////////////////
11+
// BASIC AUTOGRAD EXAMPLE 1 //
12+
///////////////////////////////////////////////////////////
13+
14+
// Create Tensors
15+
torch::Tensor x = torch::tensor(1.0, torch::requires_grad());
16+
torch::Tensor w = torch::tensor(2.0, torch::requires_grad());
17+
torch::Tensor b = torch::tensor(3.0, torch::requires_grad());
18+
19+
// Build a computational graph
20+
auto y = w * x + b; // y = 2 * x + 3
21+
22+
// Compute the gradients
23+
y.backward();
24+
25+
// Print out the gradients
26+
std::cout << x.grad() << std::endl; // x.grad() = 2
27+
std::cout << w.grad() << std::endl; // w.grad() = 1
28+
std::cout << b.grad() << std::endl; // b.grad() = 1
29+
}

0 commit comments

Comments
 (0)