title: PyTorch : predict single example
body: <p>Following the example from: </p>
  
  <p><a href="https://github.com/jcjohnson/pytorch-examples" rel="noreferrer">https://github.com/jcjohnson/pytorch-examples</a></p>
  
  <p>This code trains successfully: </p>
  
  <pre><code># Code in file tensor/two_layer_net_tensor.py
  import torch
  
  device = torch.device('cpu')
  # device = torch.device('cuda') # Uncomment this to run on GPU
  
  # N is batch size; D_in is input dimension;
  # H is hidden dimension; D_out is output dimension.
  N, D_in, H, D_out = 64, 1000, 100, 10
  
  # Create random input and output data
  x = torch.randn(N, D_in, device=device)
  y = torch.randn(N, D_out, device=device)
  
  # Randomly initialize weights
  w1 = torch.randn(D_in, H, device=device)
  w2 = torch.randn(H, D_out, device=device)
  
  learning_rate = 1e-6
  for t in range(500):
    # Forward pass: compute predicted y
    h = x.mm(w1)
    h_relu = h.clamp(min=0)
    y_pred = h_relu.mm(w2)
  
    # Compute and print loss; loss is a scalar, and is stored in a PyTorch Tensor
    # of shape (); we can get its value as a Python number with loss.item().
    loss = (y_pred - y).pow(2).sum()
    print(t, loss.item())
  
    # Backprop to compute gradients of w1 and w2 with respect to loss
    grad_y_pred = 2.0 * (y_pred - y)
    grad_w2 = h_relu.t().mm(grad_y_pred)
    grad_h_relu = grad_y_pred.mm(w2.t())
    grad_h = grad_h_relu.clone()
    grad_h[h &lt; 0] = 0
    grad_w1 = x.t().mm(grad_h)
  
    # Update weights using gradient descent
    w1 -= learning_rate * grad_w1
    w2 -= learning_rate * grad_w2
  </code></pre>
  
  <p>How can I predict a single example ? My experience thus far is utilising feedforward networks using just <code>numpy</code>. After training a model I utilise forward propagation but for a single example : </p>
  
  <p><code>numpy</code> code snippet where <code>new</code> is the output value I'm attempting to predict: </p>
  
  <pre><code>new = np.asarray(toclassify) 
  Z1 = np.dot(weight_layer_1, new.T) + bias_1 
  sigmoid_activation_1 = sigmoid(Z1) 
  Z2 = np.dot(weight_layer_2, sigmoid_activation_1) + bias_2 
  sigmoid_activation_2 = sigmoid(Z2)
  </code></pre>
  
  <p><code>sigmoid_activation_2</code> contains the predicted vector attributes</p>
  
  <p>Is the idiomatic PyTorch way same?  Use forward propagation in order to make a single prediction?</p>