PyTorch 1.5 : PyTorch の学習 : サンプルによる PyTorch の学習 (翻訳/解説)
翻訳 : (株)クラスキャット セールスインフォメーション
作成日時 : 04/29/2020 (1.5.0)
* 本ページは、PyTorch 1.5 Tutorials の以下のページを翻訳した上で適宜、補足説明したものです:
- Learning PyTorch : Learning PyTorch with Examples
* サンプルコードの動作確認はしておりますが、必要な場合には適宜、追加改変しています。
* ご自由にリンクを張って頂いてかまいませんが、sales-info@classcat.com までご一報いただけると嬉しいです。
- お住まいの地域に関係なく Web ブラウザからご参加頂けます。事前登録 が必要ですのでご注意ください。
- Windows PC のブラウザからご参加が可能です。スマートデバイスもご利用可能です。
◆ お問合せ : 本件に関するお問い合わせ先は下記までお願いいたします。
株式会社クラスキャット セールス・マーケティング本部 セールス・インフォメーション |
E-Mail:sales-info@classcat.com ; WebSite: https://www.classcat.com/ |
Facebook: https://www.facebook.com/ClassCatJP/ |
PyTorch の学習 : サンプルによる PyTorch の学習
このチュートリアルは自己充足的なサンプルを通して PyTorch の基礎的な概念を紹介します。
その中核として、PyTorch は2つの主要な特徴を提供します :
- n-次元 Tensor、numpy に類似していますが GPU 上で動作可能です。
- ニューラルネットワークを構築して訓練するための自動微分
実行サンプルとして完全結合 ReLU ネットワークを使用します。ネットワークは単一の隠れ層を持ち、そしてネットワーク出力と真の出力の間のユークリッド距離を最小化するすることによりランダムデータに適合するために勾配降下で訓練されます。
Tensor
Warm-up: numpy
PyTorch を紹介する前に、最初に numpy を使用してネットワークを実装します。
Numpy は n-次元配列オブジェクト、そしてこれらの配列を操作するための多くの関数を提供します。Numpy は科学計算のための一般的なフレームワークです ; それは計算グラフや、深層学習や、勾配については何も知りません。けれどもランダムデータに 2-層ネットワークを適合させるために、numpy 演算を使用してネットワークを通して forward と backward パスを手動で実装することにより、容易に numpy を利用できます :
# -*- coding: utf-8 -*- import numpy as np # 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 = np.random.randn(N, D_in) y = np.random.randn(N, D_out) # Randomly initialize weights w1 = np.random.randn(D_in, H) w2 = np.random.randn(H, D_out) learning_rate = 1e-6 for t in range(500): # Forward pass: compute predicted y h = x.dot(w1) h_relu = np.maximum(h, 0) y_pred = h_relu.dot(w2) # Compute and print loss loss = np.square(y_pred - y).sum() print(t, loss) # 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.dot(grad_y_pred) grad_h_relu = grad_y_pred.dot(w2.T) grad_h = grad_h_relu.copy() grad_h[h < 0] = 0 grad_w1 = x.T.dot(grad_h) # Update weights w1 -= learning_rate * grad_w1 w2 -= learning_rate * grad_w2
PyTorch: Tensor
Numpy は素晴らしいフレームワークですが、それは数値計算を加速するために GPU を利用することができません。現代的な深層ニューラルネットワークについては、GPU はしばしば 50x またはそれ以上 のスピードアップを提供しますので、不幸なことに numpy は現代的な深層学習のためには十分ではないでしょう。
ここで最も基礎的な PyTorch 概念: Tensor を紹介します。PyTorch Tensor は概念的には numpy 配列と等値です : Tensor は n-次元配列で、そして PyTorch はこれらの Tensor 上で演算するための多くの関数を提供します。裏では、Tensor は計算グラフと勾配を追跡することができますが、それらは科学計算のための一般的なツールとしても有用です。
また numpy とは違い、PyTorch Tensor はそれらの数値計算を加速するために GPU を活用することができます。PyTorch Tensor を GPU 上で実行するためには、それを新しいデータ型に単純にキャストする必要があります。
ここでは 2-層ネットワークをランダムデータに適合させるために PyTorch Tensor を使用します。上の numpy サンプルのようにネットワークを通して forward と backward パスを手動で実装する必要があります :
# -*- coding: utf-8 -*- import torch dtype = torch.float device = torch.device("cpu") # device = torch.device("cuda:0") # 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, dtype=dtype) y = torch.randn(N, D_out, device=device, dtype=dtype) # Randomly initialize weights w1 = torch.randn(D_in, H, device=device, dtype=dtype) w2 = torch.randn(H, D_out, device=device, dtype=dtype) 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 = (y_pred - y).pow(2).sum().item() if t % 100 == 99: print(t, loss) # 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 < 0] = 0 grad_w1 = x.t().mm(grad_h) # Update weights using gradient descent w1 -= learning_rate * grad_w1 w2 -= learning_rate * grad_w2
Autograd
PyTorch: Tensor と autograd
上のサンプルでは、ニューラルネットワークの forward と backward パスの両者を手動で実装しなければなりませんでした。
backward パスを手動で実装することは小さな 2-層ネットワークについては大したことはありませんが、巨大で複雑なネットワークに対しては非常に困難なものに直ちになり得るでしょう。
ありがたいことに、ニューラルネットワークの backward パスの計算を自動化する 自動微分 を利用できます。PyTorch の autograd パッケージは正確にこの機能を提供します。autograd を使用するとき、貴方のネットワークの forward パスは 計算グラフ を定義します ; グラフのノードは tensor で、エッジは入力 tensor から出力 tensor を生成する関数です。それからこのグラフを通して逆伝播すれば、貴方に簡単に勾配を計算することを可能にします。
これは複雑に聞こえますが、実際に使用することは非常に単純です。各 Tensor は計算グラフのノードを表します。もし x が "x.requires_grad=True" を持つ Tensor であれば、x.grad はあるスカラー値に関する x の勾配を保持するもう一つの Tensor です。
ここで 2-層ネットワークを実装するために PyTorch Tensor と autograd を使用します ; 今ではネットワークを通して backward パスを手動で実装する必要はもはやありません :
# -*- coding: utf-8 -*- import torch dtype = torch.float device = torch.device("cpu") # device = torch.device("cuda:0") # 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 Tensors to hold input and outputs. # Setting requires_grad=False indicates that we do not need to compute gradients # with respect to these Tensors during the backward pass. x = torch.randn(N, D_in, device=device, dtype=dtype) y = torch.randn(N, D_out, device=device, dtype=dtype) # Create random Tensors for weights. # Setting requires_grad=True indicates that we want to compute gradients with # respect to these Tensors during the backward pass. w1 = torch.randn(D_in, H, device=device, dtype=dtype, requires_grad=True) w2 = torch.randn(H, D_out, device=device, dtype=dtype, requires_grad=True) learning_rate = 1e-6 for t in range(500): # Forward pass: compute predicted y using operations on Tensors; these # are exactly the same operations we used to compute the forward pass using # Tensors, but we do not need to keep references to intermediate values since # we are not implementing the backward pass by hand. y_pred = x.mm(w1).clamp(min=0).mm(w2) # Compute and print loss using operations on Tensors. # Now loss is a Tensor of shape (1,) # loss.item() gets the scalar value held in the loss. loss = (y_pred - y).pow(2).sum() if t % 100 == 99: print(t, loss.item()) # Use autograd to compute the backward pass. This call will compute the # gradient of loss with respect to all Tensors with requires_grad=True. # After this call w1.grad and w2.grad will be Tensors holding the gradient # of the loss with respect to w1 and w2 respectively. loss.backward() # Manually update weights using gradient descent. Wrap in torch.no_grad() # because weights have requires_grad=True, but we don't need to track this # in autograd. # An alternative way is to operate on weight.data and weight.grad.data. # Recall that tensor.data gives a tensor that shares the storage with # tensor, but doesn't track history. # You can also use torch.optim.SGD to achieve this. with torch.no_grad(): w1 -= learning_rate * w1.grad w2 -= learning_rate * w2.grad # Manually zero the gradients after updating weights w1.grad.zero_() w2.grad.zero_()
PyTorch: 新しい autograd 関数を定義する
内部的には、各々のプリミティブな autograd 演算子は実際には Tensor 上で作用する2つの関数です。forward 関数は入力 Tensor から出力 Tensor を計算します。backward 関数はあるスカラー値に関する出力 Tensor の勾配を受け取り、同じスカラー値に関する入力 Tensor の勾配を計算します。
PyTorch では torch.autograd.Function のサブクラスを定義して forward と backward 関数を実装することにより私達自身の autograd 演算子を簡単に定義できます。そしてインスタンスを構築してそれを関数のように呼び出して、入力データを含む Tensor を渡すことにより新しい autograd 演算子を利用できます。
このサンプルでは ReLU 非線形を遂行するためのカスタム autograd 関数を定義し、そして 2-層ネットワークを実装するためにそれを使用します :
# -*- coding: utf-8 -*- import torch class MyReLU(torch.autograd.Function): """ We can implement our own custom autograd Functions by subclassing torch.autograd.Function and implementing the forward and backward passes which operate on Tensors. """ @staticmethod def forward(ctx, input): """ In the forward pass we receive a Tensor containing the input and return a Tensor containing the output. ctx is a context object that can be used to stash information for backward computation. You can cache arbitrary objects for use in the backward pass using the ctx.save_for_backward method. """ ctx.save_for_backward(input) return input.clamp(min=0) @staticmethod def backward(ctx, grad_output): """ In the backward pass we receive a Tensor containing the gradient of the loss with respect to the output, and we need to compute the gradient of the loss with respect to the input. """ input, = ctx.saved_tensors grad_input = grad_output.clone() grad_input[input < 0] = 0 return grad_input dtype = torch.float device = torch.device("cpu") # device = torch.device("cuda:0") # 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 Tensors to hold input and outputs. x = torch.randn(N, D_in, device=device, dtype=dtype) y = torch.randn(N, D_out, device=device, dtype=dtype) # Create random Tensors for weights. w1 = torch.randn(D_in, H, device=device, dtype=dtype, requires_grad=True) w2 = torch.randn(H, D_out, device=device, dtype=dtype, requires_grad=True) learning_rate = 1e-6 for t in range(500): # To apply our Function, we use Function.apply method. We alias this as 'relu'. relu = MyReLU.apply # Forward pass: compute predicted y using operations; we compute # ReLU using our custom autograd operation. y_pred = relu(x.mm(w1)).mm(w2) # Compute and print loss loss = (y_pred - y).pow(2).sum() if t % 100 == 99: print(t, loss.item()) # Use autograd to compute the backward pass. loss.backward() # Update weights using gradient descent with torch.no_grad(): w1 -= learning_rate * w1.grad w2 -= learning_rate * w2.grad # Manually zero the gradients after updating weights w1.grad.zero_() w2.grad.zero_()
TensorFlow: 静的グラフ
PyTorch autograd は TensorFlow にとても良く似ています : 両者のフレームワークで私達は計算グラフを定義して勾配を計算するために自動微分を使用します。2つの間の最大の違いは TensorFlow の計算グラフは 静的 で PyTorch は 動的 計算グラフを使用することです。
TensorFlow では、計算グラフを一度定義してそれから多分異なる入力データをグラフに供給しながら、同じグラフを何度も何度も実行します。PyTorch では、各 forward パスが新しい計算グラフを定義します。
静的グラフは素晴らしいです、何故ならばグラフを前もって最適化できるからです ; 例えば、フレームワークは幾つかのグラフ演算を効率性のために融合することを、あるいは多くの GPU や多くのマシンに渡りグラフを分散させるためのストラテジーを持ち出すことを決定するかもしれません。もし同じグラフを何度も繰り返し再利用しているのであれば、同じグラフが何度も実行されるときこの潜在的にコスト高な事前の最適化は償却できます。
静的と動的グラフが異なる一つの局面は制御フローです。 幾つかのモデルについては各データポイントに対して異なる計算を遂行することを望むかもしれません ; 例えばリカレント・ネットワークは各データポイントに対して時間ステップの異なる数について展開されるかもしれません ; この展開はループとして実装できます。静的グラフではループ構築はグラフの一部である必要があります ; この理由で TensorFlow はループをグラフに埋め込むために tf.scan のような演算を提供します。動的グラフでは状況はより単純です : 各サンプルに対してグラフを on-the-fly に構築しますから、各入力に対して異なる計算を遂行するために通常の命令型フロー制御を使用できます。
上の PyTorch autograd サンプルと対比するために、ここで単純な 2-層ネットを fit させるために TensorFlow を使用します :
# -*- coding: utf-8 -*- import tensorflow as tf import numpy as np # First we set up the computational graph: # 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 placeholders for the input and target data; these will be filled # with real data when we execute the graph. x = tf.placeholder(tf.float32, shape=(None, D_in)) y = tf.placeholder(tf.float32, shape=(None, D_out)) # Create Variables for the weights and initialize them with random data. # A TensorFlow Variable persists its value across executions of the graph. w1 = tf.Variable(tf.random_normal((D_in, H))) w2 = tf.Variable(tf.random_normal((H, D_out))) # Forward pass: Compute the predicted y using operations on TensorFlow Tensors. # Note that this code does not actually perform any numeric operations; it # merely sets up the computational graph that we will later execute. h = tf.matmul(x, w1) h_relu = tf.maximum(h, tf.zeros(1)) y_pred = tf.matmul(h_relu, w2) # Compute loss using operations on TensorFlow Tensors loss = tf.reduce_sum((y - y_pred) ** 2.0) # Compute gradient of the loss with respect to w1 and w2. grad_w1, grad_w2 = tf.gradients(loss, [w1, w2]) # Update the weights using gradient descent. To actually update the weights # we need to evaluate new_w1 and new_w2 when executing the graph. Note that # in TensorFlow the the act of updating the value of the weights is part of # the computational graph; in PyTorch this happens outside the computational # graph. learning_rate = 1e-6 new_w1 = w1.assign(w1 - learning_rate * grad_w1) new_w2 = w2.assign(w2 - learning_rate * grad_w2) # Now we have built our computational graph, so we enter a TensorFlow session to # actually execute the graph. with tf.Session() as sess: # Run the graph once to initialize the Variables w1 and w2. sess.run(tf.global_variables_initializer()) # Create numpy arrays holding the actual data for the inputs x and targets # y x_value = np.random.randn(N, D_in) y_value = np.random.randn(N, D_out) for _ in range(500): # Execute the graph many times. Each time it executes we want to bind # x_value to x and y_value to y, specified with the feed_dict argument. # Each time we execute the graph we want to compute the values for loss, # new_w1, and new_w2; the values of these Tensors are returned as numpy # arrays. loss_value, _, _ = sess.run([loss, new_w1, new_w2], feed_dict={x: x_value, y: y_value}) print(loss_value)
nn モジュール
PyTorch: nn
計算グラフと autograd は複雑な演算子を定義して導関数を自動的に取るための非常にパワフルなパラダイムです ; けれども巨大なニューラルネットワークのためには生の autograd は少し低位過ぎます。
ニューラルネットワークを構築するとき私達は計算を 層 内に配置することをしばしば考えます、層の幾つかは学習の間に最適化される 学習可能なパラメータ を持ちます。
TensorFlow では、Keras, TensorFlow-Slim と TFLearn のようなパッケージは生の計算グラフを越える高位な抽象を提供します、これらはニューラルネットワークを構築するために有用です。
PyTorch では、nn パッケージがこの同じ目的のために役立ちます。nn パッケージは モジュール のセットを定義し、これはニューラルネットワーク層におおよそ同値です。モジュールは入力 Tensor を受け取り出力 Tensor を計算しますが、学習可能なパラメータを含む Tensor のような内部状態もまた保持するかもしれません。nn パッケージはまた、ニューラルネットワークを訓練するとき一般に使用される有用な損失関数のセットも定義します。
このサンプルでは私達の 2-層ネットワークを実装するために nn パッケージを使用します :
# -*- coding: utf-8 -*- import torch # 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 Tensors to hold inputs and outputs x = torch.randn(N, D_in) y = torch.randn(N, D_out) # Use the nn package to define our model as a sequence of layers. nn.Sequential # is a Module which contains other Modules, and applies them in sequence to # produce its output. Each Linear Module computes output from input using a # linear function, and holds internal Tensors for its weight and bias. model = torch.nn.Sequential( torch.nn.Linear(D_in, H), torch.nn.ReLU(), torch.nn.Linear(H, D_out), ) # The nn package also contains definitions of popular loss functions; in this # case we will use Mean Squared Error (MSE) as our loss function. loss_fn = torch.nn.MSELoss(reduction='sum') learning_rate = 1e-4 for t in range(500): # Forward pass: compute predicted y by passing x to the model. Module objects # override the __call__ operator so you can call them like functions. When # doing so you pass a Tensor of input data to the Module and it produces # a Tensor of output data. y_pred = model(x) # Compute and print loss. We pass Tensors containing the predicted and true # values of y, and the loss function returns a Tensor containing the # loss. loss = loss_fn(y_pred, y) if t % 100 == 99: print(t, loss.item()) # Zero the gradients before running the backward pass. model.zero_grad() # Backward pass: compute gradient of the loss with respect to all the learnable # parameters of the model. Internally, the parameters of each Module are stored # in Tensors with requires_grad=True, so this call will compute gradients for # all learnable parameters in the model. loss.backward() # Update the weights using gradient descent. Each parameter is a Tensor, so # we can access its gradients like we did before. with torch.no_grad(): for param in model.parameters(): param -= learning_rate * param.grad
PyTorch: optim
ここまでで学習可能なパラメータを保持する Tensor を手動で変更することによりモデルの重みを更新してきました (autograd で履歴を追跡することを回避するために with torch.no_grad() か .data)。これは確率的勾配降下のような単純な最適化アルゴリズムのためには大きな重荷ではありませんが、実際には私達はしばしば AdaGrad, RMSProp, Adam, etc. のようなより洗練された optimizer を使用してニューラルネットワークを訓練します。
PyTorch の optim パッケージは最適化アルゴリズムの考えを抽象化して一般に使用される最適化アルゴリズムの実装を提供します。
このサンプルでは前のように私達のモデルを定義するために nn パッケージを使用しますが、optim パッケージで提供される Adam アルゴリズムを使用してモデルを最適化します :
# -*- coding: utf-8 -*- import torch # 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 Tensors to hold inputs and outputs x = torch.randn(N, D_in) y = torch.randn(N, D_out) # Use the nn package to define our model and loss function. model = torch.nn.Sequential( torch.nn.Linear(D_in, H), torch.nn.ReLU(), torch.nn.Linear(H, D_out), ) loss_fn = torch.nn.MSELoss(reduction='sum') # Use the optim package to define an Optimizer that will update the weights of # the model for us. Here we will use Adam; the optim package contains many other # optimization algoriths. The first argument to the Adam constructor tells the # optimizer which Tensors it should update. learning_rate = 1e-4 optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate) for t in range(500): # Forward pass: compute predicted y by passing x to the model. y_pred = model(x) # Compute and print loss. loss = loss_fn(y_pred, y) if t % 100 == 99: print(t, loss.item()) # Before the backward pass, use the optimizer object to zero all of the # gradients for the variables it will update (which are the learnable # weights of the model). This is because by default, gradients are # accumulated in buffers( i.e, not overwritten) whenever .backward() # is called. Checkout docs of torch.autograd.backward for more details. optimizer.zero_grad() # Backward pass: compute gradient of the loss with respect to model # parameters loss.backward() # Calling the step function on an Optimizer makes an update to its # parameters optimizer.step()
PyTorch: カスタム nn Module
時に既存の Module のシークエンスよりも複雑なモデルを指定することを望むでしょう ; それらのケースのために、nn.Module をサブクラス化して (他のモジュールや Tensor 上の他の autograd 演算を使用して入力 Tensor を受け取り出力 Tensor を生成する) forward を定義することにより貴方自身の Module を定義できます。
このサンプルでは私達の 2-層ネットワークをカスタム Module サブクラスとして実装します :
# -*- coding: utf-8 -*- import torch class TwoLayerNet(torch.nn.Module): def __init__(self, D_in, H, D_out): """ In the constructor we instantiate two nn.Linear modules and assign them as member variables. """ super(TwoLayerNet, self).__init__() self.linear1 = torch.nn.Linear(D_in, H) self.linear2 = torch.nn.Linear(H, D_out) def forward(self, x): """ In the forward function we accept a Tensor of input data and we must return a Tensor of output data. We can use Modules defined in the constructor as well as arbitrary operators on Tensors. """ h_relu = self.linear1(x).clamp(min=0) y_pred = self.linear2(h_relu) return y_pred # 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 Tensors to hold inputs and outputs x = torch.randn(N, D_in) y = torch.randn(N, D_out) # Construct our model by instantiating the class defined above model = TwoLayerNet(D_in, H, D_out) # Construct our loss function and an Optimizer. The call to model.parameters() # in the SGD constructor will contain the learnable parameters of the two # nn.Linear modules which are members of the model. criterion = torch.nn.MSELoss(reduction='sum') optimizer = torch.optim.SGD(model.parameters(), lr=1e-4) for t in range(500): # Forward pass: Compute predicted y by passing x to the model y_pred = model(x) # Compute and print loss loss = criterion(y_pred, y) print(t, loss.item()) # Zero gradients, perform a backward pass, and update the weights. optimizer.zero_grad() loss.backward() optimizer.step()
PyTorch: 制御フロー + 重み共有
動的グラフと重み共有のサンプルとして、非常に奇妙なモデルを実装します : 完全結合 ReLU ネットワークで、これは各 forward パス上で 1 と 4 の間のランダム数を選択してその数の多さの隠れ層を持ち、最も内側の隠れ層を計算するために同じ重みを複数回再利用します。
このモデルのために、このループを実装するために通常の Python フロー制御を使用します、そして forward パスを定義するとき同じ Module を単純に複数回再利用することにより最も内側の層の中で重み共有を実装できます。
このモデルを Module サブクラスとして容易に実装することができます :
# -*- coding: utf-8 -*- import random import torch class DynamicNet(torch.nn.Module): def __init__(self, D_in, H, D_out): """ In the constructor we construct three nn.Linear instances that we will use in the forward pass. """ super(DynamicNet, self).__init__() self.input_linear = torch.nn.Linear(D_in, H) self.middle_linear = torch.nn.Linear(H, H) self.output_linear = torch.nn.Linear(H, D_out) def forward(self, x): """ For the forward pass of the model, we randomly choose either 0, 1, 2, or 3 and reuse the middle_linear Module that many times to compute hidden layer representations. Since each forward pass builds a dynamic computation graph, we can use normal Python control-flow operators like loops or conditional statements when defining the forward pass of the model. Here we also see that it is perfectly safe to reuse the same Module many times when defining a computational graph. This is a big improvement from Lua Torch, where each Module could be used only once. """ h_relu = self.input_linear(x).clamp(min=0) for _ in range(random.randint(0, 3)): h_relu = self.middle_linear(h_relu).clamp(min=0) y_pred = self.output_linear(h_relu) return y_pred # 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 Tensors to hold inputs and outputs x = torch.randn(N, D_in) y = torch.randn(N, D_out) # Construct our model by instantiating the class defined above model = DynamicNet(D_in, H, D_out) # Construct our loss function and an Optimizer. Training this strange model with # vanilla stochastic gradient descent is tough, so we use momentum criterion = torch.nn.MSELoss(reduction='sum') optimizer = torch.optim.SGD(model.parameters(), lr=1e-4, momentum=0.9) for t in range(500): # Forward pass: Compute predicted y by passing x to the model y_pred = model(x) # Compute and print loss loss = criterion(y_pred, y) if t % 100 == 99: print(t, loss.item()) # Zero gradients, perform a backward pass, and update the weights. optimizer.zero_grad() loss.backward() optimizer.step()
以上