Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
deeplearningzerotoall
GitHub Repository: deeplearningzerotoall/PyTorch
Path: blob/master/RNN/1-basics.ipynb
618 views
Kernel: Python 3
import torch import numpy as np
# Random seed to make results deterministic and reproducible torch.manual_seed(0)
<torch._C.Generator at 0x110204b50>
# declare dimension input_size = 4 hidden_size = 2
# singleton example # shape : (1, 1, 4) # input_data_np = np.array([[[1, 0, 0, 0]]]) # sequential example # shape : (3, 5, 4) h = [1, 0, 0, 0] e = [0, 1, 0, 0] l = [0, 0, 1, 0] o = [0, 0, 0, 1] input_data_np = np.array([[h, e, l, l, o], [e, o, l, l, l], [l, l, e, e, l]], dtype=np.float32)
# transform as torch tensor input_data = torch.Tensor(input_data_np)
# declare RNN rnn = torch.nn.RNN(input_size, hidden_size)
# check output outputs, _status = rnn(input_data) print(outputs) print(outputs.size())
tensor([[[-0.7497, -0.6135], [-0.5282, -0.2473], [-0.9136, -0.4269], [-0.9136, -0.4269], [-0.9028, 0.1180]], [[-0.5753, -0.0070], [-0.9052, 0.2597], [-0.9173, -0.1989], [-0.9173, -0.1989], [-0.8996, -0.2725]], [[-0.9077, -0.3205], [-0.8944, -0.2902], [-0.5134, -0.0288], [-0.5134, -0.0288], [-0.9127, -0.2222]]], grad_fn=<StackBackward>) torch.Size([3, 5, 2])