def score(self, input_data, label): input_pad = self.prepare_input(input_data) label = self.prepare_label(label) prediction = self.model.predict(input_pad) get_score_senti(label, prediction) get_confusion_matrix(label, prediction) #implemented cofusion matrix for it
def train(self, train_input_data, train_label, valid_input_data, valid_label, class_weights=None): x_train = self.prepare_input(train_input_data) y_train = self.prepare_label(train_label) # np.set_printoptions(threshold=np.inf) x_valid = self.prepare_input(valid_input_data) y_valid = self.prepare_label(valid_label) print('start training...') history = self.model.fit(x=x_train, y=y_train, batch_size=self.config.batch_size, epochs=self.config.n_epochs, validation_data=(x_valid, y_valid), callbacks=self.callbacks, class_weight=class_weights) plt.plot(history.history['acc']) plt.plot(history.history['val_acc']) plt.title('Model accuracy') plt.ylabel('Accuracy') plt.xlabel('Epoch') plt.legend(['Train', 'Test'], loc='upper left') plt.savefig('model_acc.png') print('training end...') print('score over valid data:') valid_pred = self.model.predict(x_valid) get_score_senti(y_valid, valid_pred)
def score(self, input_data, label): input_pad = self.prepare_input(input_data) label = self.prepare_label(label) prediction = self.model.predict(input_pad) # no need to print them # print(prediction) get_score_senti(label, prediction)
def train(self, train_input_data, train_label, valid_input_data, valid_label): x_train = self.prepare_input(train_input_data) y_train = self.prepare_label(train_label) x_valid = self.prepare_input(valid_input_data) y_valid = self.prepare_label(valid_label) print('start training...') self.model.fit(x=x_train, y=y_train, batch_size=self.config.batch_size, epochs=self.config.n_epochs, validation_data=(x_valid, y_valid), callbacks=self.callbacks) print('training end...') print('score over valid data:') valid_pred = self.model.predict(x_valid) get_score_senti(y_valid, valid_pred)
def train(self, train_input_data, train_label, valid_input_data, valid_label,class_weights=None,imBalanced=False): print("LENGTH OF THIS STUPID input thing IS",len(train_input_data)) x_train = self.prepare_input(train_input_data) y_train = self.prepare_label(train_label) # np.set_printoptions(threshold=np.inf) x_valid = self.prepare_input(valid_input_data) y_valid = self.prepare_label(valid_label) # if imBalanced is True: # # print("Applying ImBalance Technique") # # training_generator, steps_per_epoch = balanced_batch_generator( # # x_train, y_train, sampler=NearMiss(), batch_size=10, random_state=42) if imBalanced is False: print('start training (without ImBalance)...') history = self.model.fit(x=x_train, y=y_train, batch_size=self.config.batch_size, epochs=self.config.n_epochs, validation_data=(x_valid, y_valid), callbacks=self.callbacks, class_weight=class_weights) else: print('start training (without ImBalance)...') # history = self.model.fit_generator(generator=training_generator,steps_per_epoch=steps_per_epoch, batch_size=self.config.batch_size, epochs=self.config.n_epochs, # validation_data=(x_valid, y_valid), callbacks=self.callbacks, class_weight=class_weights) history = self.model.fit(x=(x_train[0],x_train[1]), y=y_train, batch_size=self.config.batch_size, epochs=self.config.n_epochs, validation_data=(x_valid, y_valid), callbacks=self.callbacks, class_weight=class_weights) plt.plot(history.history['acc']) plt.plot(history.history['val_acc']) plt.title('Model accuracy') plt.ylabel('Accuracy') plt.xlabel('Epoch') plt.legend(['Train', 'Test'], loc='upper left') plt.savefig('model_acc.png') print('training end...') print('score over valid data:') valid_pred = self.model.predict(x_valid) get_score_senti(y_valid, valid_pred)
def on_epoch_end(self, epoch, logs={}): if len(self.validation_data[:-3]) == 1: x_valid = self.validation_data[0] else: x_valid = self.validation_data[:-3] y_valid = self.validation_data[-3] valid_results = self.model.predict(x_valid) _val_acc, _val_f1 = get_score_senti(y_valid, valid_results) logs['val_acc'] = _val_acc logs['val_f1'] = _val_f1 self.val_accs.append(_val_acc) self.val_f1s.append(_val_f1) print('val_acc: %f' % _val_acc) print('val_f1: %f' % _val_f1) return
def score(self, input_data, label): input_pad = self.prepare_input(input_data) label = self.prepare_label(label) prediction = self.model.predict(input_pad) get_score_senti(label, prediction)