def main(path): train,test,wordVocab = loaddata(path) maxSentLen = 140 train_sent = pad_sequences(train["sent"],maxlen=maxSentLen,padding="post") train_gen = pad_sequences(train["gen"],maxlen=5,padding="post") train_disease = pad_sequences(train["disease"],maxlen=5,padding="post") test_sent = pad_sequences(test["sent"],maxlen=maxSentLen,padding="post") test_gen = pad_sequences(test["gen"],maxlen=5,padding="post") test_disease = pad_sequences(test["disease"],maxlen=5,padding="post") train_rel = np.array(train["rel"]) test_rel = np.array(test["rel"]) print(train_rel.shape) print(test_rel.shape) # load pre-embedding print("loading embedding...") embedding_size = 200 embeddingVocab_size = len(wordVocab) # w2v_dir_path = "/media/network/watching_dog/embedding/bio_nlp_vec/PubMed-shuffle-win-30.bin" w2v_dir_path = "/media/kazane/watching_dog/embedding/bio_nlp_vec/PubMed-shuffle-win-30.bin" word2vec = KeyedVectors.load_word2vec_format(w2v_dir_path, binary=True, unicode_errors='ignore') print("build embedding weights...") embedding_weights = np.zeros((embeddingVocab_size + 1, embedding_size)) unknow_words = [] know_words = [] for word, index in wordVocab.items(): try: embedding_weights[index, :] = word2vec[word.lower()] know_words.append(word) except KeyError as E: # print(E) unknow_words.append(word) embedding_weights[index, :] = np.random.uniform(-0.025, 0.025, embedding_size) print("unknow_per: ", len(unknow_words) / embeddingVocab_size, " unkownwords: ", len(unknow_words), " vocab_size: ", embeddingVocab_size) model = myModel(sent_lenth=maxSentLen,word_embedding=embedding_weights) model.attn(embedding=embedding_weights) model.train(inputs=[train_sent,train_gen,train_disease,], label=[train_rel,], save_path="./outputs/checkpoint", validation_split=0.1, batch_size=128, epochs=5, ) y_ = model.predict([test_sent,test_gen,test_disease]) # np.save(file=path+"/"+"outputs.npy",arr=y_) is_gate = False if is_gate is False: y_ = get_Result(y_,) get_F1Value(y_=y_,y=test_rel,path=path) else: get_ROC(y_,test_rel,path=path,gate=100)
def __init__(self): self._view = myView(self) self._model = myModel(self) self._server = myServer(self) self._view.connect('button-addChannel-clicked', self._addVideoButton) self._view.connect('button-startChannel-clicked', self._startVideo) self._view.connect('button-stopChannel-clicked', self._stopVideo) self._view.connect('combobox-input-changed', self._inputChanged) self._view.connect('destroy', self.on_destroy) self._view.connect('button-addClient-clicked', self._addClient)
import torch import torchvision.transforms as transforms from torch.autograd import Variable from model import myModel from PIL import Image import numpy as np from config import Config as cfg transform = transforms.Compose([ # transforms.Resize(128), transforms.ToTensor() ]) dic = {0: "萝莉", 1: "御姐", 2: "熟女"} model = myModel().eval() model.load_state_dict(torch.load(cfg.model_saved_path)) img = Image.open(cfg.test_img) img.show(img) img = img.resize((128, 128)) img = transform(img).unsqueeze(0) img = Variable(img) out = model(img) print(out) pred = np.argmax(out.data.numpy()) print("这个妹子是:", dic[pred])
def _inputChanged(self, combo, channelNum, inputType): print("input changed") # print(combo) print("channel", channelNum) print("input", inputType) self._model._setInput(channelNum, inputType) if __name__ == "__main__": # window = Gtk.ApplicationWindow() view = myView() model = myModel() # vbox = Gtk.VBox() # window # window.add(vbox) # model controller = myController(view, model) # Create a gstreamer pipeline with no sink. # A sink will be created inside the GstWidget. # widget = GstWidget('videotestsrc') # widget.set_size_request(200, 200) # vbox.add(widget)
unknow_words = [] know_words = [] for word, index in wordVocab.items(): try: embedding_weights[index, :] = word2vec[word.lower()] know_words.append(word) except KeyError as E: # print(E) unknow_words.append(word) embedding_weights[index, :] = np.random.uniform( -0.025, 0.025, embedding_size) print("unknow_per: ", len(unknow_words) / embeddingVocab_size, " unkownwords: ", len(unknow_words), " vocab_size: ", embeddingVocab_size) print('model...') model = myModel(sent_lenth=maxSentLen) model.attn(embedding_weights) # model = myModel(sent_lenth=maxSentLen) # model.attn() print('train...') model.train(inputs=train, batch_size=32, epochs=5, maxSentLen=maxSentLen) print('predict') y_ = model.predict([test_sent, test_gene, test_phenotype]) def get_Result(y): result = np.zeros(shape=y.shape) for i in range(y.shape[0]): if y[i, 0] >= y[i, 1]: result[i, 0] = 1 else: