1.質問内容
詳細ディープラーニング第2版(マイナビ)で、RNNの実装を勉強中です。参考コードも
詳細ディープラーニング第2版の公開github中コードです。
githubは、以下です。
https://github.com/yusugomori/deeplearning-keras-tf2-torch
ディレクト5の「21_imdb_birnn_torch.py」が参考コードです。
参考コードは、BiRNN用いた時系列データ分析例です。最初は、github中コードでは
from callbacks import EarlyStoppingでしたが、importが、うまくいかないので、
from tensorflow.keras.callbacks import EarlyStoppingにしたら、importでき
ましたが、実行中に 'EarlyStopping' object is not callableとなります。
対処方法をお願いいたします。
2.参考コード
python
1import numpy as np 2from sklearn.metrics import accuracy_score 3from sklearn.model_selection import train_test_split 4from sklearn.utils import shuffle 5import torch 6import torch.nn as nn 7import torch.optim as optimizers 8from tensorflow.keras import datasets 9from tensorflow.keras.preprocessing.sequence import pad_sequences 10 11#from callbacks import EarlyStopping 12from tensorflow.keras.callbacks import EarlyStopping 13 14class BiRNN(nn.Module): 15 def __init__(self, num_words, hidden_dim): 16 super().__init__() 17 self.emb = nn.Embedding(num_words, hidden_dim, padding_idx=0) 18 self.lstm = nn.LSTM(hidden_dim, hidden_dim, 19 batch_first=True, 20 bidirectional=True) 21 self.linear = nn.Linear(hidden_dim*2, 1) 22 self.sigmoid = nn.Sigmoid() 23 24 nn.init.xavier_normal_(self.lstm.weight_ih_l0) 25 nn.init.orthogonal_(self.lstm.weight_hh_l0) 26 nn.init.xavier_normal_(self.linear.weight) 27 28 def forward(self, x): 29 h = self.emb(x) 30 h, _ = self.lstm(h) 31 h = self.linear(h[:, -1]) 32 y = self.sigmoid(h) 33 return y.squeeze() # (batch_size, 1) => (batch_size,) 34 35 36if __name__ == '__main__': 37 np.random.seed(123) 38 torch.manual_seed(123) 39 device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') 40 41 ''' 42 1. データの準備 43 ''' 44 num_words = 20000 45 maxlen = 80 46 47 imdb = datasets.imdb 48 word_index = imdb.get_word_index() 49 50 (x_train, t_train), (x_test, t_test) = imdb.load_data(num_words=num_words, 51 start_char=1, 52 oov_char=2, 53 index_from=3) 54 55 x_train, x_val, t_train, t_val = \ 56 train_test_split(x_train, t_train, test_size=0.2) 57 58 x_train = pad_sequences(x_train, maxlen=maxlen, padding='pre') 59 x_val = pad_sequences(x_val, maxlen=maxlen, padding='pre') 60 x_test = pad_sequences(x_test, maxlen=maxlen, padding='pre') 61 62 ''' 63 2. モデルの構築 64 ''' 65 model = BiRNN(num_words, 128).to(device) 66 67 ''' 68 3. モデルの学習 69 ''' 70 criterion = nn.BCELoss() 71 optimizer = optimizers.Adam(model.parameters(), 72 lr=0.001, 73 betas=(0.9, 0.999), amsgrad=True) 74 75 def compute_loss(t, y): 76 return criterion(y, t) 77 78 def train_step(x, t): 79 x = torch.LongTensor(x).to(device) 80 t = torch.Tensor(t).to(device) 81 model.train() 82 preds = model(x) 83 loss = compute_loss(t, preds) 84 optimizer.zero_grad() 85 loss.backward() 86 optimizer.step() 87 88 return loss, preds 89 90 def val_step(x, t): 91 x = torch.LongTensor(x).to(device) 92 t = torch.Tensor(t).to(device) 93 model.eval() 94 preds = model(x) 95 loss = criterion(preds, t) 96 97 return loss, preds 98 99 epochs = 1000 100 batch_size = 100 101 n_batches_train = x_train.shape[0] // batch_size 102 n_batches_val = x_val.shape[0] // batch_size 103 es = EarlyStopping(patience=5, verbose=1) 104 105 for epoch in range(epochs): 106 train_loss = 0. 107 train_acc = 0. 108 val_loss = 0. 109 val_acc = 0. 110 x_, t_ = shuffle(x_train, t_train) 111 112 for batch in range(n_batches_train): 113 start = batch * batch_size 114 end = start + batch_size 115 loss, preds = train_step(x_[start:end], t_[start:end]) 116 train_loss += loss.item() 117 train_acc += \ 118 accuracy_score(t_[start:end].tolist(), 119 preds.data.cpu().numpy() > 0.5) 120 121 train_loss /= n_batches_train 122 train_acc /= n_batches_train 123 124 for batch in range(n_batches_val): 125 start = batch * batch_size 126 end = start + batch_size 127 loss, preds = val_step(x_val[start:end], t_val[start:end]) 128 val_loss += loss.item() 129 val_acc += \ 130 accuracy_score(t_val[start:end].tolist(), 131 preds.data.cpu().numpy() > 0.5) 132 133 val_loss /= n_batches_val 134 val_acc /= n_batches_val 135 136 print('epoch: {}, loss: {:.3}, acc: {:.3f}' 137 ', val_loss: {:.3}, val_acc: {:.3f}'.format( 138 epoch+1, 139 train_loss, 140 train_acc, 141 val_loss, 142 val_acc 143 )) 144 145 if es(val_loss): 146 break 147 148 ''' 149 4. モデルの評価 150 ''' 151 def test_step(x, t): 152 return val_step(x, t) 153 154 loss, preds = test_step(x_test, t_test) 155 test_loss = loss.item() 156 preds = preds.data.cpu().numpy() > 0.5 157 test_acc = accuracy_score(t_test, preds) 158 159 print('test_loss: {:.3f}, test_acc: {:.3f}'.format( 160 test_loss, 161 test_acc 162 )) 163
3.補足情報(FW/ツールのバージョンなど)
開発環境:Google Colaboratory
プログラム言語:python3
OS:windows10 Home
CPU:Intel(R) Core(TM) i7-7500U CPU@2.70GHz 2.90GHz
回答1件
あなたの回答
tips
プレビュー