def forward(self, x_data, y_data, train=True, models=None): VGG_mini = models["VGG_mini"] VGG_mini2 = models["VGG_mini2"] VGG_mini3 = models["VGG_mini3"] x = Variable(x_data, volatile=not train) t = Variable(y_data, volatile=not train) h = F.relu(self.conv1_1(x)) h = F.relu(self.conv1_2(h)) h = F.relu(self.conv1_3(h)) h = F.relu(self.conv1_4(h)) h = F.max_pooling_2d(h, 2, stride=2) h = F.dropout(h, ratio=0.25, train=train) h = F.relu(self.conv1_5(h)) h = F.max_pooling_2d(h, 2, stride=2) h = F.dropout(h, ratio=0.25, train=train) h = self.fc(h) if train: return F.softmax_cross_entropy(h, t), F.accuracy(h, t) else: # return F.softmax_cross_entropy(h, t), F.accuracy(h, t), h return F.softmax_cross_entropy(h, t), F.accuracy(h, t)
def __call__(self, x, test=False): # add gaussian noise #xp = cuda.get_array_module(x.data) #with cuda.get_device_from_id(self.device): # noise = xp.random.randn(*x.shape) * 0.0015 # x.data += noise h = self.conv00(x, test) h = self.conv01(h, test) h = self.conv02(h, test) h = F.max_pooling_2d(h, (2, 2)) # 32 -> 16 h = self.bn0(h, test) h = F.dropout(h, train=not test) h = self.conv10(h, test) h = self.conv11(h, test) h = self.conv12(h, test) h = F.max_pooling_2d(h, (2, 2)) # 16 -> 8 h = self.bn1(h, test) h = F.dropout(h, train=not test) h = self.conv20(h, test) # 8 -> 6 h = self.conv21(h, test) h = self.conv22(h, test) h = self.conv23(h, test) h = F.average_pooling_2d(h, (6, 6)) # 6 -> 1 h = self.bn2(h, test) h = F.reshape(h, (h.shape[0], np.prod(h.shape[1:]))) return h
def __call__(self, x, train): h_1 = self.l1(x) h_2 = self.l2(F.dropout(h_1, ratio=0.5, train = train)) h_3 = self.l3(F.dropout(h_2, ratio=0.5, train = train)) h_4 = self.l4(F.dropout(h_3, ratio=0.5, train = train)) out = self.l5(h_4) return out
def __call__(self, x, t): h = F.relu(self.conv1_1(x)) h = F.relu(self.conv1_2(h)) h = F.max_pooling_2d(h, 2, stride=2) h = F.relu(self.conv2_1(h)) h = F.relu(self.conv2_2(h)) h = F.max_pooling_2d(h, 2, stride=2) h = F.relu(self.conv3_1(h)) h = F.relu(self.conv3_2(h)) h = F.relu(self.conv3_3(h)) h = F.max_pooling_2d(h, 2, stride=2) h = F.relu(self.conv4_1(h)) h = F.relu(self.conv4_2(h)) h = F.relu(self.conv4_3(h)) h = F.max_pooling_2d(h, 2, stride=2) h = F.relu(self.conv5_1(h)) h = F.relu(self.conv5_2(h)) h = F.relu(self.conv5_3(h)) h = F.max_pooling_2d(h, 2, stride=2) h = F.dropout(F.relu(self.fc6(h)), train=self.train, ratio=0.5) h = F.dropout(F.relu(self.fc7(h)), train=self.train, ratio=0.5) h = self.fc8(h) if self.train: self.loss = F.softmax_cross_entropy(h, t) self.acc = F.accuracy(h, t) return self.loss else: self.pred = F.softmax(h) return self.pred
def __call__(self, s): accum_loss = None _, k = self.embed.W.data.shape h = Variable(np.zeros((1, k), dtype=np.float32)) c = Variable(np.zeros((1, k), dtype=np.float32)) s_length = len(s) for i in range(s_length): w1 = s[i] w2 = s[i + 1] if i < s_length - 1 else self.eos_id x_k = self.embed(Variable(np.array([w1], dtype=np.int32))) tx = Variable(np.array([w2], dtype=np.int32)) z0 = self.Wz(x_k) + self.Rz(F.dropout(h)) z1 = F.tanh(z0) i0 = self.Wi(x_k) + self.Ri(F.dropout(h)) i1 = F.sigmoid(i0) f0 = self.Wf(x_k) + self.Rf(F.dropout(h)) f1 = F.sigmoid(f0) c = i1 * z1 + f1 * c o0 = self.Wo(x_k) + self.Ro(F.dropout(h)) o1 = F.sigmoid(o0) y = o1 * F.tanh(c) h = y loss = F.softmax_cross_entropy(self.W(y), tx) accum_loss = loss if accum_loss is None else accum_loss + loss return accum_loss
def __call__(self, x, t): h = F.relu(self.bn1_1(self.conv1_1(x), test=not self.train)) h = F.relu(self.bn1_2(self.conv1_2(h), test=not self.train)) h = F.max_pooling_2d(h, 2, 2) h = F.dropout(h, ratio=0.25, train=self.train) h = F.relu(self.bn2_1(self.conv2_1(h), test=not self.train)) h = F.relu(self.bn2_2(self.conv2_2(h), test=not self.train)) h = F.max_pooling_2d(h, 2, 2) h = F.dropout(h, ratio=0.25, train=self.train) h = F.relu(self.bn3_1(self.conv3_1(h), test=not self.train)) h = F.relu(self.bn3_2(self.conv3_2(h), test=not self.train)) h = F.relu(self.bn3_3(self.conv3_3(h), test=not self.train)) h = F.relu(self.bn3_4(self.conv3_4(h), test=not self.train)) h = F.max_pooling_2d(h, 2, 2) h = F.dropout(h, ratio=0.25, train=self.train) h = F.dropout(F.relu(self.fc4(h)), ratio=0.5, train=self.train) h = F.dropout(F.relu(self.fc5(h)), ratio=0.5, train=self.train) h = self.fc6(h) self.pred = F.softmax(h) self.loss = F.softmax_cross_entropy(h, t) self.accuracy = F.accuracy(self.pred, t) if self.train: return self.loss else: return self.pred
def forward(x_data, y_data, model,train=True): # Neural net architecture #x, t = chainer.Variable(x_data), chainer.Variable(y_data) t = chainer.Variable(y_data) x = {} for n in range(500): x[n] = chainer.Variable(x_data[n]) h = {} initial_V = {} initial_V_relu = {} for nameint in range(len(l_name)-2): initial_V[nameint] = model[l_name[nameint]](x[nameint]) #initial_V_relu[nameint] = F.relu(initial_V[nameint]) #initial_V_relu[nameint] = F.sigmoid(initial_V[nameint]) initial_V_relu[nameint] = F.tanh(initial_V[nameint]) #h[nameint] = F.dropout(F.relu(initial_V[nameint]), train=train) #h[nameint] = F.dropout(F.sigmoid(initial_V[nameint]), train=train) h[nameint] = F.dropout(F.tanh(initial_V[nameint]), train=train) #h[nameint] = F.relu(model[l_name[nameint]](x[nameint])) #h6 = F.dropout(F.relu(model.l501(Returnharray(h))), train=train) #h6 = F.dropout(F.sigmoid(model.l501(Returnharray(h))), train=train) h6 = F.dropout(F.tanh(model.l501(Returnharray(h))), train=train) y = model.l502(h6) y_pre = (y.data.argmax(axis = 1)) return F.softmax_cross_entropy(y, t), F.accuracy(y, t),y_pre,initial_V,initial_V_relu
def forward(x_data, y_data, train=True): """ 順伝搬の処理を定義 """ # train ... 学習フラグ(Falseにすると学習しない) # 訓練時は,dropoutを実行 # テスト時は,dropoutを無効 # 入力と教師データ # データ配列は,Chainer.Variable型にしないといけない x, t = chainer.Variable(x_data), chainer.Variable(y_data) # 隠れ層1の出力 h1 = F.dropout(F.relu(model.l1(x)), train=train) # 隠れ層2の出力 h2 = F.dropout(F.relu(model.l2(h1)), train=train) # 出力層の出力 y = model.l3(h2) # 訓練時とテスト時で返す値を変える # y ... ネットワークの出力(仮説) # t ... 教師データ if train: # 訓練 # 誤差を返す # 多クラス分類なので,誤差関数としてソフトマックス関数の # クロスエントロピー関数を使う loss = F.softmax_cross_entropy(y, t) return loss else: # テスト # 精度を返す acc = F.accuracy(y, t) return acc
def forward(x_data, y_data, train=True): x_0 = chainer.Variable(cuda.to_gpu(x_data, 0), volatile=not train) x_1 = chainer.Variable(cuda.to_gpu(x_data, 1), volatile=not train) t = chainer.Variable(cuda.to_gpu(y_data, 0), volatile=not train) h1_0 = F.dropout(F.relu(model.gpu0.l1(x_0)), train=train) h1_1 = F.dropout(F.relu(model.gpu1.l1(x_1)), train=train) h2_0 = F.dropout(F.relu(model.gpu0.l2(h1_0)), train=train) h2_1 = F.dropout(F.relu(model.gpu1.l2(h1_1)), train=train) h3_0 = F.dropout(F.relu(model.gpu0.l3(h2_0)), train=train) h3_1 = F.dropout(F.relu(model.gpu1.l3(h2_1)), train=train) # Synchronize h3_0 += F.copy(h3_1, 0) h3_1 = F.copy(h3_0, 1) h4_0 = F.dropout(F.relu(model.gpu0.l4(h3_0)), train=train) h4_1 = F.dropout(F.relu(model.gpu1.l4(h3_1)), train=train) h5_0 = F.dropout(F.relu(model.gpu0.l5(h4_0)), train=train) h5_1 = F.dropout(F.relu(model.gpu1.l5(h4_1)), train=train) h6_0 = F.relu(model.gpu0.l6(h5_0)) h6_1 = F.relu(model.gpu1.l6(h5_1)) # Synchronize y = h6_0 + F.copy(h6_1, 0) return F.softmax_cross_entropy(y, t), F.accuracy(y, t)
def forward(x, is_train=True): h1 = F.max_pooling_2d(F.relu(model.conv1(x)), 3) h2 = F.max_pooling_2d(F.relu(model.conv2(h1)), 3) h3 = F.dropout(F.relu(model.l1(h2)), train=is_train) h4 = F.dropout(F.relu(model.l2(h3)), train=is_train) p = model.l3(h4) return p
def __call__(self, x, test=False): # add gaussian noise xp = cuda.get_array_module(x.data) with cuda.get_device(self.device): noise = xp.random.randn(*x.shape) * 0.15 x.data += noise # (conv -> act -> bn) x 3 -> maxpool -> dropout h = self.bn_conv0(self.act(self.conv0(x), 0.1), test) h = self.bn_conv1(self.act(self.conv1(h), 0.1), test) h = self.bn_conv2(self.act(self.conv2(h), 0.1), test) h = F.max_pooling_2d(h, (2, 2)) # 32 -> 16 h = F.dropout(h, 0.5, not test) # (conv -> act -> bn) x 3 -> maxpool -> dropout h = self.bn_conv3(self.act(self.conv3(h), 0.1), test) h = self.bn_conv4(self.act(self.conv4(h), 0.1), test) h = self.bn_conv5(self.act(self.conv5(h), 0.1), test) h = F.max_pooling_2d(h, (2, 2)) # 16 -> 8 h = F.dropout(h, 0.5, not test) # conv -> act -> bn -> (nin -> act -> bn) x 2 h = self.bn_conv6(self.act(self.conv6(h), 0.1), test) # 8 -> 6 h = self.bn_conv7(self.act(self.conv7(h), 0.1), test) h = self.bn_conv8(self.act(self.conv8(h), 0.1), test) h = F.average_pooling_2d(h, (6, 6)) h = self.linear(h) return h
def forward(x_data, y_data, train=True): # Neural net architecture x, t = chainer.Variable(x_data), chainer.Variable(y_data) h = F.max_pooling_2d(F.dropout(F.relu(model.bn2(model.cv1(x))), train=train),2) h = F.dropout(F.relu(model.ln3(h)), train=train) y = model.ln4(h) return F.softmax_cross_entropy(y, t), F.accuracy(y, t)
def predict(self, test_x: np.ndarray): test_x = Variable(test_x) self.h1 = F.dropout(F.relu(self.l1(test_x))) self.h2 = F.dropout(F.relu(self.l2(self.h1))) y = self.l3(self.h2) predict_list = list(map(np.argmax, F.softmax(y).data)) return predict_list
def __call__(self, x, t): h = F.relu(self.bn1_1(self.conv1_1(x), test=not self.train)) h = F.relu(self.bn1_2(self.conv1_2(h), test=not self.train)) h = F.max_pooling_2d(h, 2, stride=2) h = F.relu(self.bn2_1(self.conv2_1(h), test=not self.train)) h = F.relu(self.bn2_2(self.conv2_2(h), test=not self.train)) h = F.max_pooling_2d(h, 2, stride=2) h = F.relu(self.bn3_1(self.conv3_1(h), test=not self.train)) h = F.relu(self.bn3_2(self.conv3_2(h), test=not self.train)) h = F.relu(self.bn3_3(self.conv3_3(h), test=not self.train)) h = F.max_pooling_2d(h, 2, stride=2) h = F.relu(self.bn4_1(self.conv4_1(h), test=not self.train)) h = F.relu(self.bn4_2(self.conv4_2(h), test=not self.train)) h = F.relu(self.bn4_3(self.conv4_3(h), test=not self.train)) h = F.max_pooling_2d(h, 2, stride=2) h = F.relu(self.bn5_1(self.conv5_1(h), test=not self.train)) h = F.relu(self.bn5_2(self.conv5_2(h), test=not self.train)) h = F.relu(self.bn5_3(self.conv5_3(h), test=not self.train)) h = F.max_pooling_2d(h, 2, stride=2) h = F.dropout(F.relu(self.fc6(h)), train=self.train, ratio=0.6) h = F.dropout(F.relu(self.fc7(h)), train=self.train, ratio=0.6) self.pred = self.fc8(h) if t is not None: self.loss = F.mean_squared_error(self.pred, t) return self.loss else: return self.pred
def __call__(self,x,y,state,train=True,target=True): if train: h = Variable(x.reshape(self.batchsize,12), volatile=not train) else: h = Variable(x, volatile=not train) t = Variable(y.flatten(), volatile=not train) h0 = F.relu(self.l0(h)) if target == False: data = h0.data self.data_first.append(data) h1_in = self.l1_x(h0) + self.l1_h(state['h1']) h1_in = F.dropout(F.tanh(h1_in),train=train) c1, h1 = F.lstm(state['c1'], h1_in) h2_in = F.dropout(F.tanh(self.l2_x(h1)), train=train) + self.l2_h(state['h2']) c2, h2 = F.lstm(state['c2'], h2_in) if target == False: data = h1.data self.data_hidden.append(data) y = self.l3(h2) if target ==False: data = y.data self.data_output.append(data) state = {'c1': c1, 'h1': h1,'c2':c2,'h2':h2} self.loss = F.softmax_cross_entropy(y,t) return state,self.loss
def forward_one_step(self, x_data, y_data, state, train=True,dropout_ratio=0.0): x ,t = Variable(x_data,volatile=not train),Variable(y_data,volatile=not train) h1_in = self.l1_x(F.dropout(x, ratio=dropout_ratio, train=train)) + self.l1_h(state['h1']) c1, h1 = F.lstm(state['c1'], h1_in) y = self.l6(F.dropout(h1, ratio=dropout_ratio, train=train)) state = {'c1': c1, 'h1': h1} return state, F.mean_squared_error(y, t)
def forward(x_data, y_data, train=True): # Neural net architecture x, t = chainer.Variable(x_data), chainer.Variable(y_data) h1 = F.dropout(F.relu(model.l1(x)), train=train) h2 = F.dropout(F.relu(model.l2(h1)), train=train) y = model.l3(h2) return F.softmax_cross_entropy(y, t), F.accuracy(y, t)
def __call__(self, x, rois, train=False): h = F.relu(self.conv1_1(x)) h = F.relu(self.conv1_2(h)) h = F.max_pooling_2d(h, 2, stride=2) h = F.relu(self.conv2_1(h)) h = F.relu(self.conv2_2(h)) h = F.max_pooling_2d(h, 2, stride=2) h = F.relu(self.conv3_1(h)) h = F.relu(self.conv3_2(h)) h = F.relu(self.conv3_3(h)) h = F.max_pooling_2d(h, 2, stride=2) h = F.relu(self.conv4_1(h)) h = F.relu(self.conv4_2(h)) h = F.relu(self.conv4_3(h)) h = F.max_pooling_2d(h, 2, stride=2) h = F.relu(self.conv5_1(h)) h = F.relu(self.conv5_2(h)) h = F.relu(self.conv5_3(h)) h = roi_pooling_2d(h, rois, 7, 7, 0.0625) h = F.dropout(F.relu(self.fc6(h)), train=train, ratio=0.5) h = F.dropout(F.relu(self.fc7(h)), train=train, ratio=0.5) cls_score = F.softmax(self.cls_score(h)) bbox_pred = self.bbox_pred(h) return cls_score, bbox_pred
def solve(self, x_seq, pos, neg, train=True, variablize=False, onebyone=True): if variablize:# If arguments are just arrays (not variables), make them variables x_seq = [chainer.Variable(x, volatile=not train) for x in x_seq] x_seq = [F.dropout(x, ratio=self.dropout_ratio, train=train) for x in x_seq] pos = self.act1(self.W_candidate( F.dropout(chainer.Variable(pos, volatile=not train), ratio=self.dropout_ratio, train=train))) neg = self.act1(self.W_candidate( F.dropout(chainer.Variable(neg, volatile=not train), ratio=self.dropout_ratio, train=train))) if onebyone and train: target_x_seq = [self.act1(self.W_candidate(x)) for x in x_seq[:4]]# 1,2,3,4,5-th targets onebyone_loss = 0. self.LSTM.reset_state() for i, x in enumerate(x_seq): h = self.LSTM( F.dropout(x, ratio=self.dropout_ratio, train=train) ) if onebyone and train and target_x_seq[i+1:]: pos_score, neg_score = self.calculate_score(h, target_x_seq[i+1:], neg, multipos=True) onebyone_loss += F.relu( self.margin - pos_score + neg_score ) pos_score, neg_score = self.calculate_score(h, pos, neg) accum_loss = F.relu( self.margin - pos_score + neg_score ) TorFs = sum(accum_loss.data < self.margin) if onebyone and train: return F.sum(accum_loss) + F.sum(onebyone_loss), TorFs else: return F.sum(accum_loss), TorFs
def predict(self,x): h1 = F.dropout(F.relu(self.l1(x)),ratio=self.dropout1,train=False) h2 = F.dropout(F.relu(self.l2(h1)),ratio=self.dropout2,train=False) h3 = F.dropout(F.relu(self.l3(h2)),ratio=self.dropout3,train=False) h4 = self.l4(h3) return h4
def __call__(self, x): """Forward pass computation, without saving computation history. return: 4096 dimension image features (fc7 features) """ h = F.relu(self.conv1_1(x)) h = F.relu(self.conv1_2(h)) h = F.max_pooling_2d(h, 2, stride=2) h = F.relu(self.conv2_1(h)) h = F.relu(self.conv2_2(h)) h = F.max_pooling_2d(h, 2, stride=2) h = F.relu(self.conv3_1(h)) h = F.relu(self.conv3_2(h)) h = F.relu(self.conv3_3(h)) h = F.max_pooling_2d(h, 2, stride=2) h = F.relu(self.conv4_1(h)) h = F.relu(self.conv4_2(h)) h = F.relu(self.conv4_3(h)) h = F.max_pooling_2d(h, 2, stride=2) h = F.relu(self.conv5_1(h)) h = F.relu(self.conv5_2(h)) h = F.relu(self.conv5_3(h)) h = F.max_pooling_2d(h, 2, stride=2) h = F.dropout(F.relu(self.fc6(h)), train=self.train, ratio=0.5) h = F.dropout(F.relu(self.fc7(h)), train=self.train, ratio=0.5) # we only need the fc7 layer's output, i.e., 4096-D features return h
def __call__(self, x_batch,train = True): #h_concat = F.dropout(F.tanh(self.l_hidden(x_batch)), train=train) #h_hidden = F.dropout(F.tanh(self.l_hidden2(h_concat)), train=train) h_concat = F.dropout(F.relu(self.l_hidden(x_batch)), train=train) h_hidden = F.dropout(F.relu(self.l_hidden2(h_concat)), train=train) y = self.l_output(h_concat) return y, h_concat
def __call__(self, x, train=True): h1 = F.relu(self.conv1_1(x)) h2 = F.max_pooling_2d(h1, 2, stride=2) h3 = F.relu(self.conv2_1(h2)) h4 = F.max_pooling_2d(h3, 2, stride=2) h5 = F.relu(self.conv3_1(h4)) h6 = F.relu(self.conv3_2(h5)) h7 = F.max_pooling_2d(h6, 2, stride=2) h8 = F.relu(self.conv4_1(h7)) h9 = F.relu(self.conv4_2(h8)) h10 = F.max_pooling_2d(h9, 2, stride=2) h11 = F.relu(self.conv5_1(h10)) h12 = F.relu(self.conv5_2(h11)) h13 = F.max_pooling_2d(h12, 2, stride=2) h14 = F.dropout(F.relu(self.fc6(h13)), train=train, ratio=0.5) h15 = F.dropout(F.relu(self.fc7(h14)), train=train, ratio=0.5) h16 = self.fc8(h15) y = F.sigmoid(h16) return y
def discrimination(): # parameters input_size = 900 h_units1 = 1000 h_units2 = 300 output_size = 40 in_data = 'input.csv' in_data = np.loadtxt(in_data, delimiter=',', dtype=np.float32) in_data2d = in_data[np.newaxis, :] with open('model.pkl', 'rb') as i: model = pickle.load(i) in_data = in_data2d[0:1] h1 = F.dropout(F.relu(model.l1(Variable(in_data))), train=False) h2 = F.dropout(F.relu(model.l2(h1)), train=False) y = model.l3(h2) out_class = y.data out_class_max = max(out_class) #print 'Output: ' + str(out_class) #print 'Max index: ' + str(np.argmax(out_class)) f = open('output.txt', 'w') for i in range(output_size): if i == np.argmax(out_class): f.writelines('1') else: f.writelines('0') f.close()
def __call__(self, x, y, t): self.clear() hR = F.max_pooling_2d(F.relu( F.local_response_normalization(self.convR1(x))), 3, stride=2) hR = F.max_pooling_2d(F.relu( F.local_response_normalization(self.convR2(hR))), 3, stride=2) hR = F.relu(self.convR3(hR)) hR = F.relu(self.convR4(hR)) hR = F.max_pooling_2d(F.relu(self.convR5(hR)), 3, stride=2) hR = F.dropout(F.relu(self.fcR6(hR)), train=self.train) hR = F.dropout(F.relu(self.fcR7(hR)), train=self.train) hD = F.max_pooling_2d(F.relu( F.local_response_normalization(self.convD1(y))), 3, stride=2) hD = F.max_pooling_2d(F.relu( F.local_response_normalization(self.convD2(hD))), 3, stride=2) hD = F.relu(self.convD3(hD)) hD = F.relu(self.convD4(hD)) hD = F.max_pooling_2d(F.relu(self.convD5(hD)), 3, stride=2) hD = F.dropout(F.relu(self.fcD6(hD)), train=self.train) hD = F.dropout(F.relu(self.fcD7(hD)), train=self.train) h = F.dropout(F.relu(self.fc8(hR, hD)), train=self.train) h = self.fc9(h) self.loss = F.softmax_cross_entropy(h, t) self.accuracy = F.accuracy(h, t) return self.loss
def forward(self, x_img, x_doc, y_data, train=True): x_img = cuda.cupy.asarray(x_img) x_doc = cuda.cupy.asarray(x_doc) y_data = cuda.cupy.asarray(y_data) img, doc, t = Variable(x_img), Variable(x_doc), Variable(y_data) h = F.max_pooling_2d(F.relu(self.conv1(img)), ksize=3, stride=2, pad=0) h = F.local_response_normalization(h) h = F.max_pooling_2d(F.relu(self.conv2(h)), ksize=3, stride=2, pad=0) h = F.local_response_normalization(h) h = F.relu(self.conv3(h)) h = F.relu(self.conv4(h)) h = F.max_pooling_2d(F.relu(self.conv5(h)), ksize=3, stride=2, pad=0) h = F.dropout(F.relu(self.fc6(h)), train=train, ratio=0.5) h = F.dropout(F.relu(self.fc7(h)), train=train, ratio=0.5) h2 = F.relu(self.doc_fc1(doc)) h2 = F.relu(self.doc_fc2(h2)) b = F.relu(self.bi1(h, h2)) y = self.fc8(b) if train: return F.softmax_cross_entropy(y, t) else: return F.accuracy(y, t)
def forward(self, x_data, y_data, train=True): x = Variable(x_data, volatile=not train) t = Variable(y_data, volatile=not train) h = F.relu(self.bn1_1(self.conv1_1(x))) h = F.relu(self.bn1_2(self.conv1_2(h))) h = F.max_pooling_2d(h, 2, stride=2) h = F.dropout(h, ratio=0.25, train=train) h = F.relu(self.bn2_1(self.conv2_1(h))) h = F.relu(self.bn2_2(self.conv2_2(h))) h = F.max_pooling_2d(h, 2, stride=2) h = F.dropout(h, ratio=0.25, train=train) h = F.relu(self.bn3_1(self.conv3_1(h))) h = F.relu(self.bn3_2(self.conv3_2(h))) h = F.relu(self.bn3_3(self.conv3_3(h))) h = F.relu(self.bn3_4(self.conv3_4(h))) h = F.max_pooling_2d(h, 2, stride=2) h = F.dropout(h, ratio=0.25, train=train) h = F.dropout(F.relu(self.fc4(h)), train=train, ratio=0.5) h = F.dropout(F.relu(self.fc5(h)), train=train, ratio=0.5) h = self.fc6(h) if train: return F.softmax_cross_entropy(h, t), F.accuracy(h, t) else: return F.softmax_cross_entropy(h, t), F.accuracy(h, t), h
def forward(self, x_data, y_data, train=True): x = Variable(x_data, volatile=not train) t = Variable(y_data, volatile=not train) h = self.prelu1_1(self.bn1_1(self.conv1_1(x))) h = self.prelu1_2(self.bn1_2(self.conv1_2(h))) h = F.max_pooling_2d(h, 2, stride=2) h = self.prelu2_1(self.bn2_1(self.conv2_1(h))) h = self.prelu2_2(self.bn2_2(self.conv2_2(h))) h = F.max_pooling_2d(h, 2, stride=2) h = self.prelu3_1(self.conv3_1(h)) h = self.prelu3_2(self.conv3_2(h)) h = self.prelu3_3(self.conv3_3(h)) h = F.max_pooling_2d(h, 2, stride=1) h = self.prelu4_1(self.conv4_1(h)) h = self.prelu4_2(self.conv4_2(h)) h = self.prelu4_3(self.conv4_3(h)) h = F.max_pooling_2d(h, 2, stride=1) h = self.prelu5_1(self.conv5_1(h)) h = self.prelu5_2(self.conv5_2(h)) h = self.prelu5_3(self.conv5_3(h)) h = F.max_pooling_2d(h, 2, stride=1) h = F.dropout(self.prelu6(self.fc6(h)), train=train, ratio=0.5) h = F.dropout(self.prelu7(self.fc7(h)), train=train, ratio=0.5) h = self.fc8(h) if train: return F.softmax_cross_entropy(h, t), F.accuracy(h, t) else: return F.softmax_cross_entropy(h, t), F.accuracy(h, t), h
def __call__(self, x, model_params, test=False): # Initial convolution mp_filtered = self._filter_model_params(model_params, "/convunit") h = self.convunit(x, mp_filtered, test) # Residual convolution mp_filtered = self._filter_model_params(model_params, "/resconvunit0") h = self.resconvunit0(h, mp_filtered, test) mp_filtered = self._filter_model_params(model_params, "/resconvunit1") h = self.resconvunit1(h, mp_filtered, test) h = F.max_pooling_2d(h, (2, 2)) # 32 -> 16 h = F.dropout(h, train=not test) mp_filtered = self._filter_model_params(model_params, "/resconvunit2") h = self.resconvunit2(h, mp_filtered, test) mp_filtered = self._filter_model_params(model_params, "/resconvunit3") h = self.resconvunit3(h, mp_filtered, test) h = F.max_pooling_2d(h, (2, 2)) # 16 -> 8 h = F.dropout(h, train=not test) mp_filtered = self._filter_model_params(model_params, "/resconvunit4") h = self.resconvunit4(h, mp_filtered, test) mp_filtered = self._filter_model_params(model_params, "/resconvunit5") h = self.resconvunit5(h, mp_filtered, test) # Linear mp_filtered = self._filter_model_params(model_params, "/linear") y = self.linear(h, mp_filtered["/W"], mp_filtered["/b"]) return y
def forward(self, x_data, y_data, train=True): x = Variable(x_data, volatile=not train) t = Variable(y_data, volatile=not train) h = F.relu(self.conv1_1(x)) h = F.relu(self.conv1_2(h)) h = F.max_pooling_2d(h, 2, stride=2) h = F.relu(self.conv2_1(h)) h = F.relu(self.conv2_2(h)) h = F.max_pooling_2d(h, 2, stride=2) h = F.relu(self.conv3_1(h)) h = F.relu(self.conv3_2(h)) h = F.relu(self.conv3_3(h)) h = F.max_pooling_2d(h, 2, stride=2) h = F.relu(self.conv4_1(h)) h = F.relu(self.conv4_2(h)) h = F.relu(self.conv4_3(h)) h = F.max_pooling_2d(h, 2, stride=2) h = F.relu(self.conv5_1(h)) h = F.relu(self.conv5_2(h)) h = F.relu(self.conv5_3(h)) h = F.max_pooling_2d(h, 2, stride=2) h = F.dropout(F.relu(self.fc6(h)), train=train, ratio=0.6) h = F.dropout(F.relu(self.fc7(h)), train=train, ratio=0.6) h = self.fc8(h) loss = F.mean_squared_error(h, t) return loss, h
def __call__(self, x): x = F.transpose_sequence(x) for x_ in x: self.lstm(F.dropout(self.embed(x_), train=self.train)) h = self.out(F.dropout(self.lstm.h, train=self.train)) return h
def __call__(self, x): h = F.max_pooling_2d(F.relu(self.conv1(x)), 2) h = F.max_pooling_2d(F.relu(self.conv2(h)), 2) h = F.dropout(F.relu(self.l1(h)), 0.2) h = self.l2(h) return h
def __call__(self, x, t=None): # conv1 h = F.relu(self.conv1_1(x)) conv1_1 = h h = F.relu(self.conv1_2(conv1_1)) conv1_2 = h h = F.max_pooling_2d(conv1_2, 2, stride=2, pad=0) pool1 = h # 1/2 # conv2 h = F.relu(self.conv2_1(pool1)) conv2_1 = h h = F.relu(self.conv2_2(conv2_1)) conv2_2 = h h = F.max_pooling_2d(conv2_2, 2, stride=2, pad=0) pool2 = h # 1/4 # conv3 h = F.relu(self.conv3_1(pool2)) conv3_1 = h h = F.relu(self.conv3_2(conv3_1)) conv3_2 = h h = F.relu(self.conv3_3(conv3_2)) conv3_3 = h h = F.max_pooling_2d(conv3_3, 2, stride=2, pad=0) pool3 = h # 1/8 # conv4 h = F.relu(self.conv4_1(pool3)) h = F.relu(self.conv4_2(h)) h = F.relu(self.conv4_3(h)) h = F.max_pooling_2d(h, 2, stride=2, pad=0) pool4 = h # 1/16 # conv5 h = F.relu(self.conv5_1(pool4)) h = F.relu(self.conv5_2(h)) h = F.relu(self.conv5_3(h)) h = F.max_pooling_2d(h, 2, stride=2, pad=0) pool5 = h # 1/32 # fc6 h = F.relu(self.fc6(pool5)) h = F.dropout(h, ratio=.5) fc6 = h # 1/32 # fc7 h = F.relu(self.fc7(fc6)) h = F.dropout(h, ratio=.5) fc7 = h # 1/32 # score_fr h = self.score_fr(fc7) score_fr = h # 1/32 # score_pool3 h = self.score_pool3(pool3) score_pool3 = h # 1/8 # score_pool4 h = self.score_pool4(pool4) score_pool4 = h # 1/16 # upscore2 h = self.upscore2(score_fr) upscore2 = h # 1/16 # score_pool4c h = score_pool4[:, :, 5:5 + upscore2.data.shape[2], 5:5 + upscore2.data.shape[3]] score_pool4c = h # 1/16 # fuse_pool4 h = upscore2 + score_pool4c fuse_pool4 = h # 1/16 # upscore_pool4 h = self.upscore_pool4(fuse_pool4) upscore_pool4 = h # 1/8 # score_pool4c h = score_pool3[:, :, 9:9 + upscore_pool4.data.shape[2], 9:9 + upscore_pool4.data.shape[3]] score_pool3c = h # 1/8 # fuse_pool3 h = upscore_pool4 + score_pool3c fuse_pool3 = h # 1/8 # upscore8 h = self.upscore8(fuse_pool3) upscore8 = h # 1/1 # score h = upscore8[:, :, 31:31 + x.data.shape[2], 31:31 + x.data.shape[3]] score = h # 1/1 self.score = score if t is None: assert not chainer.config.train return loss = F.softmax_cross_entropy(score, t, normalize=False) if np.isnan(float(loss.data)): raise ValueError('Loss is nan.') return loss
def fwd(self, x): h1 = F.max_pooling_2d(F.relu(self.cn1(x)), 2) h2 = F.max_pooling_2d(F.relu(self.cn2(h1)), 2) h3 = F.dropout(F.relu(self.l1(h2))) return self.l2(h3)
def forward(self, x): h0 = self.embed(x) h1 = self.input_vec(F.dropout(h0)) h2 = self.output_vec(F.dropout(h1)) y = self.output_word(F.dropout(h2)) return y
def forward(self, x): y1 = F.dropout(x) return y1
def __forward(self, train, support_sets, support_lbls, x_set, x_lbl=None): model = self mod = self.__mod gpu = self.__gpu n_out = self.__n_out batch_size = support_sets[0].shape[0] N = len(support_sets) self.cleargrads() key_mems, x_keys = self.embed_key(train, support_sets, support_lbls, x_set) grad_mems = [] grad_mems1 = [] hs = [ Variable(h.data, volatile=False) for h in F.split_axis(key_mems, N, axis=0) ] for i in range(N): self.cleargrads() h = hs[i] h = F.dropout(h, ratio=0.0, train=train) h = F.relu(model.fc1(h)) h = F.relu(model.fc0(h)) h = F.dropout(h, ratio=0.0, train=train) if train or self.__n_out == self.__n_out_test: y = model.fc2(h) else: y = model.fc3(h) y_batch = mod.array(support_lbls[i], dtype=np.int32) lbl = Variable(y_batch, volatile=False) support_loss = F.softmax_cross_entropy(y, lbl) support_loss.backward(retain_grad=True) grads1 = [] grad_sections1 = [] grads1.append(model.fc1.W.grad.reshape(-1, 1)) grad_sections1.append(grads1[-1].shape[0]) grads1.append(model.fc0.W.grad.reshape(-1, 1)) grad_sections1.append(grad_sections1[-1] + grads1[-1].shape[0]) if train or self.__n_out == self.__n_out_test: grads1.append(model.fc2.W.grad.reshape(-1, 1)) else: grads1.append(model.fc3.W.grad.reshape(-1, 1)) meta_in = mod.concatenate(grads1, axis=0) meta_in = cuda.to_cpu(meta_in) meta_in = logAndSign(meta_in, k=7) meta_in = mod.array(meta_in) meta_in = Variable(meta_in, volatile=False) meta_outs = F.relu( model.mc_l1(F.dropout(meta_in, ratio=0.0, train=train))) meta_outs = F.relu( model.mc_ll1(F.dropout(meta_outs, ratio=0.0, train=train))) meta_outs = model.meta_g_lstm_l2( F.dropout(meta_outs, ratio=0.0, train=train)) grad_mems1.append(meta_outs) grad_mems1 = F.concat(grad_mems1, axis=1) x_keys = F.split_axis(x_keys, x_set.shape[0], axis=0) xs = x_keys x_loss = 0 preds = [] ys = [] for h, x_key, lbl in zip(xs, x_keys, x_lbl): x_key = F.reshape(x_key, (1, -1)) sc = F.softmax(cosine_similarity2d(key_mems, x_key)) meta_outs1 = F.matmul(grad_mems1, sc, transb=True) meta_outs1 = F.split_axis(meta_outs1, grad_sections1, axis=0) fc1_W = F.reshape(meta_outs1[0], model.fc1.W.data.shape) fc0_W = F.reshape(meta_outs1[1], model.fc0.W.data.shape) if train or self.__n_out == self.__n_out_test: fc2_W = F.reshape(meta_outs1[2], model.fc2.W.data.shape) else: fc2_W = F.reshape(meta_outs1[2], model.fc3.W.data.shape) h = F.dropout(h, ratio=0.0, train=train) h = F.relu(model.fc1(h)) + F.relu(F.matmul(h, fc1_W, transb=True)) h = F.dropout(h, ratio=0.0, train=train) h = F.relu(model.fc0(h)) + F.relu(F.matmul(h, fc0_W, transb=True)) h = F.dropout(h, ratio=0.0, train=train) if train or self.__n_out == self.__n_out_test: y = model.fc2(h) + F.matmul(h, fc2_W, transb=True) else: y = model.fc3(h) + F.matmul(h, fc2_W, transb=True) y_batch = mod.array(lbl, dtype=np.int32).reshape((1, )) lbl = Variable(y_batch, volatile=False) x_loss += F.softmax_cross_entropy(y, lbl) preds += mod.argmax(y.data, 1).tolist() return preds, x_loss
def __call__(self, h): return self.l2( F.dropout(F.tanh(self.l1(F.dropout(h, self.dropout_rate))), self.dropout_rate))
def __call__(self, sentences, transitions, y_batch=None, train=True): ratio = 1 - self.keep_rate # Get Embeddings sentences = self.initial_embeddings.take(sentences, axis=0).astype(np.float32) # Reshape sentences x_prem = sentences[:, :, 0] x_hyp = sentences[:, :, 1] x = np.concatenate([x_prem, x_hyp], axis=0) if self.__gpu >= 0: x = cuda.to_gpu(x) x = Variable(x, volatile=not train) batch_size, seq_length = x.shape[0], x.shape[1] x = F.dropout(x, ratio=ratio, train=train) # Pass embeddings through projection layer, so that they match # the dimensions in the output of the compose/reduce function. x = F.reshape(x, (batch_size * seq_length, self.word_embedding_dim)) x = self.projection(x) x = F.reshape(x, (batch_size, seq_length, self.model_dim)) # Extract Transitions t_prem = transitions[:, :, 0] t_hyp = transitions[:, :, 1] t = np.concatenate([t_prem, t_hyp], axis=0) # Pass through Sentence Encoders. self.x2h.reset_state(batch_size, train) h_both, transition_acc = self.x2h(x, t, train=train) h_premise, h_hypothesis = F.split_axis(h_both, 2, axis=0) # Pass through MLP Classifier. h = F.concat([h_premise, h_hypothesis], axis=1) # h = self.batch_norm_0(h, test=not train) # h = F.dropout(h, ratio, train) # h = F.relu(h) h = self.l0(h) # h = self.batch_norm_1(h, test=not train) # h = F.dropout(h, ratio, train) h = F.relu(h) h = self.l1(h) # h = self.batch_norm_2(h, test=not train) # h = F.dropout(h, ratio, train) h = F.relu(h) h = self.l2(h) y = h # Calculate Loss & Accuracy. accum_loss = self.classifier(y, Variable(y_batch, volatile=not train), train) self.accuracy = self.accFun(y, self.__mod.array(y_batch)) if hasattr(transition_acc, 'data'): transition_acc = transition_acc.data return y, accum_loss, self.accuracy.data, transition_acc
def embed_key(self, train, support_sets, support_lbls, x_set): mod = self.__mod model = self IT = 10 N = len(support_sets) model.meta_lstm_l1.reset_state() model.meta_g_lstm_l1.reset_state() x = mod.concatenate(support_sets, axis=0).reshape((-1, 3, 84, 84)) x = Variable(x, volatile=False) x = F.dropout(x, ratio=0.0, train=train) h = model.key_1(x, train) h = F.max_pooling_2d(h, ksize=2, stride=2) h = F.dropout(h, ratio=0.0, train=train) h = model.key_2(h, train) h = F.max_pooling_2d(h, ksize=2, stride=2) h = F.dropout(h, ratio=0.0, train=train) h = model.key_3(h, train) h = F.max_pooling_2d(h, ksize=2, stride=2) h = F.dropout(h, ratio=0.0, train=train) hs = F.split_axis(h, N, axis=0) inc = 1 if N < 10 else N / IT for i in xrange(0, N, inc): self.cleargrads() h = F.concat(hs[i:(i + inc)], axis=0) h = model.key_4(h, train) h = F.max_pooling_2d(h, ksize=2, stride=2) h = F.dropout(h, ratio=0.0, train=train) h = model.key_5(h, train) h = F.max_pooling_2d(h, ksize=2, stride=2) h = F.reshape(h, (-1, 288)) h = F.dropout(h, ratio=0.0, train=train) h = F.relu(model.key_fc1(h)) if train or self.__n_out == self.__n_out_test: y = model.key_fc2(h) else: y = model.key_fc3(h) y_batch = mod.array(support_lbls[i:(i + inc)], dtype=np.int32).reshape((-1, )) lbl = Variable(y_batch, volatile=False) loss = F.softmax_cross_entropy(y, lbl) loss.backward(retain_grad=True) grads = [] grad_sections = [] grads.append(model.key_4.conv.W.grad.reshape(-1, 1)) grad_sections.append(grads[-1].shape[0]) grads.append(model.key_5.conv.W.grad.reshape(-1, 1)) grads1 = [] grad_sections1 = [] grads1.append(model.key_fc1.W.grad.reshape(-1, 1)) grad_sections1.append(grads1[-1].shape[0]) if train or self.__n_out == self.__n_out_test: grads1.append(model.key_fc2.W.grad.reshape(-1, 1)) else: grads1.append(model.key_fc3.W.grad.reshape(-1, 1)) meta_in = mod.concatenate(grads, axis=0) meta_in = cuda.to_cpu(meta_in) meta_in = logAndSign(meta_in, k=7) meta_in = mod.array(meta_in) meta_in = Variable(meta_in, volatile=False) meta_outs = model.meta_lstm_l1( F.dropout(meta_in, ratio=0.0, train=train)) meta_outs = model.meta_lstm_ll1( F.dropout(meta_outs, ratio=0.0, train=train)) meta_in = mod.concatenate(grads1, axis=0) meta_in = cuda.to_cpu(meta_in) meta_in = logAndSign(meta_in, k=7) meta_in = mod.array(meta_in) meta_in = Variable(meta_in, volatile=False) meta_outs1 = model.meta_g_lstm_l1( F.dropout(meta_in, ratio=0.0, train=train)) meta_outs1 = model.meta_g_lstm_ll1( F.dropout(meta_outs1, ratio=0.0, train=train)) meta_outs = F.split_axis(meta_outs, grad_sections, axis=0) meta_outs1 = F.split_axis(meta_outs1, grad_sections1, axis=0) key_4_W = F.reshape(meta_outs[0], model.key_4.conv.W.data.shape) key_5_W = F.reshape(meta_outs[1], model.key_5.conv.W.data.shape) key_fc1_W = F.reshape(meta_outs1[0], model.key_fc1.W.data.shape) self.cleargrads() keys = [] for x in [support_sets, x_set]: x = mod.asarray(x, dtype=np.float32).reshape((-1, 3, 84, 84)) x = Variable(x, volatile=False) x = F.dropout(x, ratio=0.0, train=train) h = model.key_1(x, train) h = F.max_pooling_2d(h, ksize=2, stride=2) h = F.dropout(h, ratio=0.0, train=train) h = model.key_2(h, train) h = F.max_pooling_2d(h, ksize=2, stride=2) h = F.dropout(h, ratio=0.0, train=train) h = model.key_3(h, train) h = F.max_pooling_2d(h, ksize=2, stride=2) h = F.dropout(h, ratio=0.0, train=train) h = model.key_4(h, train) + model.key_4.call_on_W( h, key_4_W, train) h = F.max_pooling_2d(h, ksize=2, stride=2) h = F.dropout(h, ratio=0.0, train=train) h = model.key_5(h, train) + model.key_5.call_on_W( h, key_5_W, train) h = F.max_pooling_2d(h, ksize=2, stride=2) h = F.reshape(h, (-1, 288)) h = F.dropout(h, ratio=0.0, train=train) h = model.key_fc1(h) + F.matmul(h, key_fc1_W, transb=True) keys.append(h) return keys