-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
setup Anaconda, Spyder, did first couple tutorials, pytorch working i…
…n environment
- Loading branch information
1 parent
40dc855
commit 6301ae2
Showing
3 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import torch, torchvision | ||
|
||
# Load a pretrained resnet18 model | ||
model = torchvision.models.resnet18(pretrained=True) | ||
|
||
# Random tensor data to represent an image with three channels and height/width of 64 | ||
data = torch.rand(1,3,64,64) | ||
# Image's label init'd to some random values | ||
labels = torch.rand(1,1000) | ||
|
||
# Run the input data through the model to make a prediction | ||
prediction = model(data) # forward pass | ||
|
||
# Calculate the error and backpropagate through the network | ||
loss = (prediction - labels).sum() | ||
loss.backward() # backward pass | ||
|
||
# Load an optimizer with a learning rate of 0.01 and momentum of 0.9 | ||
optim = torch.optim.SGD(model.parameters(), lr=1e-2, momentum=0.9) | ||
|
||
# Initiate gradient descent | ||
optim.step() # gradient descent |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import torch | ||
import numpy as np | ||
|
||
# Examples of Tensors being initialized | ||
data = [[1,2],[3,4]] | ||
|
||
# Directly from data, data type is inferred | ||
xData = torch.tensor(data) | ||
|
||
# From a NumPy array | ||
npArray = np.array(data) | ||
xNP = torch.from_numpy(npArray) | ||
|
||
# From another tensor, retaining properties of the original | ||
xOnes = torch.ones_like(xData) | ||
print(f"Ones Tensor: \n {xOnes} \n") | ||
|
||
# From another tensor, overriding the datatype | ||
xRand = torch.rand_like(xData, dtype=torch.float) | ||
print(f"Random Tensor: \n {xRand} \n") | ||
|
||
# shape defines the tensor dimensions | ||
shape = (2,3,) | ||
randomTensor = torch.rand(shape) | ||
onesTensor = torch.ones(shape) | ||
zerosTensor = torch.zeros(shape) | ||
|
||
print(f"Random Tensor: \n {randomTensor} \n") | ||
print(f"Ones Tensor: \n {onesTensor} \n") | ||
print(f"Zeros Tensor: \n {zerosTensor} \n") | ||
|
||
# Tensors have attributes for their shape, datatype, and device | ||
attrDemoTensor = torch.rand(3,4) | ||
print(f"Shape: {attrDemoTensor.shape}") | ||
print(f"Datatype: {attrDemoTensor.dtype}") | ||
print(f"Device: {attrDemoTensor.device} \n") | ||
|
||
# Torch has a ton of tensor operations buil-in, apparently similar to NumPy | ||
tensor = torch.ones(4,4) | ||
print(f"{tensor} \n") | ||
tensor[:,1] = 0 | ||
print(f"{tensor} \n") | ||
|
||
t1 = torch.cat([tensor, tensor, tensor], dim=1) | ||
print(f"{t1} \n") | ||
|
||
# Operations with a _ suffix are performed in-place | ||
tensor.add_(5) | ||
print(tensor) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import torch | ||
|
||
print("Hello World!") | ||
|
||
x = torch.rand(5,3) | ||
print(x) |