def postComment(self): reddit = self.reddit subreddit = reddit.subreddit(self.subreddit) with tf.Session() as sess: # Create model and load parameters. model = translate.create_model(sess, True) model.batch_size = 1 # We decode one sentence at a time. # Load vocabularies. en_vocab_path = os.path.join( FLAGS.data_dir, "vocab%d.from" % FLAGS.from_vocab_size) fr_vocab_path = os.path.join(FLAGS.data_dir, "vocab%d.to" % FLAGS.to_vocab_size) en_vocab, _ = data_utils.initialize_vocabulary(en_vocab_path) _, rev_fr_vocab = data_utils.initialize_vocabulary(fr_vocab_path) # replace this with logic for calling the chatbot for submission in subreddit.stream.submissions(): title = submission.title comments = submission.comments for c in comments: print("comment: " + c.body) comment_response = decode(c.body, en_vocab, model, sess, rev_fr_vocab) if "_UNK" not in comment_response: c.reply(comment_response) sleep(600) print("Title: " + title) response = decode(title, en_vocab, model, sess, rev_fr_vocab) if "_UNK" not in response: submission.reply(response) sleep(600)
def compare(): """Compare two sentences separated by a semi-colon""" # Load the data frame train, dev, test = loader.getData() with tf.Session() as sess: # Create model and load parameters. model = create_model(sess, True, train_dir=TRAIN_DIR) model.batch_size = 64 # We decode one sentence at a time. # Load vocabularies. en_vocab = get_english_vocab(DATA_DIR, VOCAB_SIZE) results = [] for i, row in train.iterrows(): try: context1 = get_context(sess, model, en_vocab, row["sentence1"])[0] context2 = get_context(sess, model, en_vocab, row["sentence2"])[0] except TypeError: print "Error on line %i" % i continue cosine_distance = cosine(context1, context2) euclid_distance = np.linalg.norm(context1 - context2) prediction = euclid_distance < 10 correctness = prediction == row["label"] results.append(correctness) print "%i, %i, %.3f" % (row["label"], prediction, euclid_distance) # Print the accuracy so far if i % 10 == 0: print "Correctness:", np.mean(results) results = np.array(results) print np.mean(results)
def encode(): """Encode all of the sentences to vector form""" train, dev, test = loader.getData() sentences = [] tokens = [] # Load the vocab en_vocab = get_english_vocab(DATA_DIR, VOCAB_SIZE) # Collect all the training sentences for i, row in pd.concat((train, test)).iterrows(): if isinstance(row["sentence1"], basestring) and isinstance( row["sentence2"], basestring): sentences.append(row["sentence1"]) sentences.append(row["sentence2"]) # Allocate the sentences to buckets bucketed = {} for sentence in sentences: bucket_id = get_bucket(en_vocab, sentence) bucketed.setdefault(bucket_id, []) bucketed[bucket_id].append(sentence) mapped = {} with tf.Session() as sess: # Create model and load parameters. model = create_model(sess, True, train_dir=TRAIN_DIR) model.batch_size = BATCH_SIZE # We decode 64 sentence at a time. # Iterate over each bucket for bucket_id, sentences in bucketed.iteritems(): for batch in chunker(sentences, BATCH_SIZE): data = [] for sentence in batch: token_ids = data_utils.sentence_to_token_ids( tf.compat.as_bytes(sentence), en_vocab) expected_output = [] data.append((token_ids, expected_output)) encoder_inputs, decoder_inputs, target_weights = model.get_batch( {bucket_id: data}, bucket_id) contexts = model.step_context(sess, encoder_inputs, decoder_inputs, target_weights, bucket_id) features = np.hstack(contexts) print 'Extracted another set of features with shape:', features.shape # Now we align sentences with their contexts for i, sentence in enumerate(batch): mapped[sentence] = features[i, :].tolist() print sentence print mapped[sentence] print "Saving sentences to %s" % JSON_NAME with open(JSON_NAME, 'w') as file: json.dump(mapped, file)
def get_sentence_to_context_map(sentences): """ Process all of the sentences with the model Return a map between sentence text and the context vectors The order of the map is undefined due to the bucketing process """ # Load the vocab en_vocab = get_english_vocab(DATA_DIR, VOCAB_SIZE) # Allocate the sentences to buckets bucketed = {} for sentence in sentences: bucket_id = get_bucket(en_vocab, sentence) bucketed.setdefault(bucket_id, []) bucketed[bucket_id].append(sentence) mapped = {} with tf.Session() as sess: # Create model and load parameters. model = create_model(sess, True, train_dir=TRAIN_DIR) model.batch_size = BATCH_SIZE # We decode 64 sentence at a time. # Iterate over each bucket for bucket_id, sentences in bucketed.iteritems(): for batch in chunker(sentences, BATCH_SIZE): data = [] # Tokenize each sentence for sentence in batch: token_ids = data_utils.sentence_to_token_ids( tf.compat.as_bytes(sentence), en_vocab) expected_output = [] data.append((token_ids, expected_output)) # Use the model to obtain contexts for each sentence in the batch encoder_inputs, decoder_inputs, target_weights = model.get_batch( {bucket_id: data}, bucket_id) contexts = model.step_context(sess, encoder_inputs, decoder_inputs, target_weights, bucket_id) features = np.hstack(contexts) print 'Encoded {0} sentences into {1} dimensional vectors'.format( *features.shape) # Now we align sentences with their contexts for i, sentence in enumerate(batch): mapped[sentence] = features[i, :].tolist() return mapped
def loadCoeffIdx(pathName, modelName): skipLinear = False device = 'cuda' if torch.cuda.is_available() else 'cpu' if modelName == "vgg16_c10": model = vgg16.VGG16() elif modelName == "vgg16_c10_old": model = vgg16_old.VGG16() elif modelName == "resnet50_c10": model = resnet.ResNet50() elif modelName == "inceptionv3_c10": model = inception_v3_c10.inception_v3() elif modelName == "alexnet_in": #model = torch.hub.load('pytorch/vision', 'alexnet', pretrained=True) model = alexnet.AlexNet() skipLinear = True elif modelName == "vgg16_in": model = vgg_in.vgg16() elif modelName == "resnet50_in": model = resnet_in.resnet50() elif modelName == "transformer_wmt": translator = transformer_translate.create_model( ) # returns Transformer() translator = translator.to(device) model = translator.model else: print("Model not supported") exit(1) model = model.to(device) if ("vgg16" in modelName or "alexnet" in modelName): model = replace_vgg16(model, skipLinear) elif "transformer" in modelName: #transformer_train.replace_with_pruned(model, "model", prune_attention=True, prune_only_attention=False) pass else: replace_with_pruned(model, "model", skipLinear) if "_in" in modelName: if not torch.distributed.is_initialized(): port = np.random.randint(10000, 65536) torch.distributed.init_process_group( backend='nccl', init_method='tcp://127.0.0.1:%d' % port, rank=0, world_size=1) model = torch.nn.parallel.DistributedDataParallel(model) if not ("transformer" in modelName): model.load_state_dict(torch.load(pathName)) model.eval() #prune(model, method="cascade", q=1.0) layers = [] widths = [3] numConv = 0 assert isinstance(model, nn.Module) for n, m in model.named_modules(): print(type(m)) if isinstance(m, torch.nn.Conv2d): numConv += 1 if isinstance(m, torch.nn.Linear): numConv += 1 if isinstance(m, PrunedConv): #print(m.mask) #layer = m.mask.view((m.out_channels, -1)).detach().cpu().numpy() layer = m.conv.weight.view( (m.out_channels, -1)).detach().cpu().numpy() elif isinstance(m, PrunedLinear) or isinstance( m, CSP.pruned_layers.PrunedLinear) and (not skipLinear): #print(m.mask) #layer = m.mask.view((m.out_features, -1)).detach().cpu().numpy() layer = m.linear.weight.view( (m.out_features, -1)).detach().cpu().numpy() else: continue #print(n) #print(layer.shape) #layer = layer > 0 widths.append(len(layer)) layers.append(layer) #layerMask = layer > 0 #layerMask = np.transpose(layerMask) #print("NUM CONV: {}".format(numConv)) #print("NUM EXTRACTED LAYERS: {}".format(len(layers))) if "transformer" in modelName: for i in range(11): for j in range(36, 97): layers.append(layers[j]) return layers
def exportData(pathName, modelName): global skipLinear device = 'cuda' if torch.cuda.is_available() else 'cpu' if modelName == "vgg16_c10": model = vgg16.VGG16() elif modelName == "vgg16_c10_old": model = vgg16_old.VGG16() elif modelName == "resnet50_c10": model = resnet.ResNet50() elif modelName == "inceptionv3_c10": model = inception_v3_c10.inception_v3() elif modelName == "alexnet_in": #model = torch.hub.load('pytorch/vision', 'alexnet', pretrained=True) model = alexnet.AlexNet() skipLinear = True elif modelName == "vgg16_in": model = vgg_in.vgg16() skipLinear = True elif modelName == "resnet50_in": model = resnet_in.resnet50() elif modelName == "transformer_wmt": translator = transformer_translate.create_model( ) # returns Transformer() translator = translator.to(device) model = translator.model else: print("Model not supported") exit(1) model = model.to(device) if ("vgg16" in modelName or "alexnet" in modelName): model = replace_vgg16(model, skipLinear) elif "transformer" in modelName: #transformer_train.replace_with_pruned(model, "model", prune_attention=True, prune_only_attention=False) pass else: replace_with_pruned(model, "model", skipLinear) if "_in" in modelName: if not torch.distributed.is_initialized(): port = np.random.randint(10000, 65536) torch.distributed.init_process_group( backend='nccl', init_method='tcp://127.0.0.1:%d' % port, rank=0, world_size=1) model = torch.nn.parallel.DistributedDataParallel(model, device_ids=[0], output_device=0) if not ("transformer" in modelName): model.load_state_dict(torch.load(pathName)) model.eval() #prune(model, method="cascade", q=1.0) #layer_num = 0 #save_dir = "/root/hostCurUser/reproduce/DNNsim/net_traces/ResNet50_ImageNet_CSP/" #lstModules = list( model.named_modules()) #for n, m in model.named_modules(): #for i in range(len(lstModules)): # if isinstance(lstModules[i], nn.Conv2d) or isinstance(lstModules[i], nn.Linear): # model[i] = DataExporter(lstModules[i], save_dir, layer_num) # layer_num += 1 f = open(MODEL_PATH + "model.csv", "w") fscale = open(SCALE_PATH, "w") fscale.write( "Layer name, IFMAP Height, IFMAP Width, Filter Height, Filter Width, Channels, Num Filter, Strides,\n" ) layer_idx = 0 models = [] # for SparTen layers = [] acts = [] weights = [] paddings = [] strides = [] idxs = [] def extract(module, input): #if module.extracted: # return if len(input[0].shape) < 4: if not ("transformer" in modelName): return try: a = input[0].detach().cpu().reshape(1, module.in_features, 1, 1) except: a = input[0].detach().cpu().reshape(-1, 1, 1) a = a[:module.in_features] a = a.reshape(1, module.in_features, 1, 1) else: a = input[0].detach().cpu() acts.append(a) if isinstance(module, torch.nn.Conv2d): layer = module.weight.view( (module.out_channels, -1)).detach().cpu().numpy() weight = module.weight.detach().cpu().numpy() tp = "conv" stride = str(max(module.stride[0], module.stride[1])) padding = str(max(module.padding[0], module.padding[1])) in_channels = module.in_channels out_channels = module.out_channels kernel_size = module.kernel_size padding_st = module.padding stride_st = module.stride elif isinstance(module, torch.nn.Linear) and (not skipLinearExport): layer = module.weight.view( (module.out_features, -1)).detach().cpu().numpy() weight = module.weight.detach().cpu().reshape( module.weight.shape[0], module.weight.shape[1], 1, 1).numpy() tp = "fc" stride = str(1) padding = str(0) in_channels = module.in_features out_channels = module.out_features kernel_size = (1, 1) padding_st = (0, 0) stride_st = (1, 1) else: print("{} does not exist".format(module)) exit(1) name = str(module.layer_idx) weights.append(weight) paddings.append(int(padding)) strides.append(int(stride)) f.write(name + "," + tp + "," + stride + "," + padding + ",\n") layers.append(layer) models.append({ 'in_channels': in_channels, 'out_channels': out_channels, 'kernel': kernel_size, 'name': tp + name, 'padding': padding_st, 'weights': weight, 'IFM': a.cpu().numpy(), 'stride': stride_st }) idxs.append(module.layer_idx) #module.extracted = True #replace_with_exporter(model, "model", f, fscale) for n, m in model.named_modules(): if isinstance(m, torch.nn.Conv2d): weight = m.weight.detach().cpu().numpy() if weight.shape[2] != weight.shape[3]: continue #weights.append(weight) #m.extracted = False m.register_forward_pre_hook(extract) #name = str(layer_idx) #tp = "conv" #stride = str(max(m.stride[0], m.stride[1])) #padding = str(max(m.padding[0], m.padding[1])) #paddings.append(int(padding)) #f.write(name+"," + tp+"," + stride+"," + padding + ",\n") m.layer_idx = layer_idx layer_idx += 1 elif isinstance(m, torch.nn.Linear): if not ("transformer" in modelName): continue #weight = m.weight.detach().cpu().reshape(m.weight.shape[0], m.weight.shape[1], 1, 1).numpy() #weights.append(weight) #m.extracted = False m.register_forward_pre_hook(extract) #name = str(layer_idx) #tp = "fc" #stride = str(1) #padding = str(0) #paddings.append(int(padding)) #f.write(name+"," + tp+"," + stride+"," + padding + ",\n") m.layer_idx = layer_idx layer_idx += 1 if "_in" in modelName: IFM = torch.rand(1, 3, 224, 224).cuda() model(IFM) elif "_c10" in modelName: IFM = torch.rand(1, 3, 32, 32).cuda() model(IFM) elif "_wmt" in modelName: src_seq = [4556, 4562, 4560, 4557, 4712, 1894, 15, 4564, 4620, 0, 5] pred_seq = translator.translate_sentence( torch.LongTensor([src_seq]).to(device)) print(pred_seq) else: print("Dataset not supported") exit(1) print(len(acts)) print(len(weights)) #layers = loadCoeffIdx(pathName, modelName) with open(ST_PATH + modelName + ".h5", "wb") as f: pickle.dump(models, f) sq_ptrs = [] out_channels = [] for layer in layers: sp, oc = squeezeCoeffIdxTotal(layer, len(layer)) out_channels.append(oc) #""" i = 0 for idx in range(len(acts)): x_save = acts[idx] weight_save = weights[idx] np.save(EXPORT_PATH + "act-" + str(idx) + "-0.npy", x_save) np.save(EXPORT_PATH + "wgt-" + str(idx) + ".npy", weight_save) x_save[x_save == 0] = int(0) x_save[x_save != 0] = int(1) np.savetxt(CX_PATH + "act" + str(idx) + ".csv", x_save.reshape(-1), delimiter=",", fmt="%d") weight_save[weight_save == 0] = int(0) weight_save[weight_save != 0] = int(1) np.savetxt(CX_PATH + "Conv2D_" + str(idx) + ".csv", weight_save.reshape(weight_save.shape[0], -1), delimiter=",", fmt="%d") if x_save.shape[2] > 1: name = "Conv" + str(idx) #str(idxs[idx]) IFM_height = str(x_save.shape[2] + (2 * paddings[idx])) IFM_width = str(x_save.shape[3] + (2 * paddings[idx])) filt_height = str(weight_save.shape[2]) filt_width = str(weight_save.shape[3]) else: name = "FC" + str(idx) #str(idxs[idx]) IFM_height = str(1) IFM_width = str(1) filt_height = str(1) filt_width = str(1) channels = str(weight_save.shape[1]) if ("resnet50" in modelName) and (idx == 4 or idx == 15 or idx == 29 or idx == 49): num_filt = str(weight_save.shape[0]) else: num_filt = str(out_channels[i]) i += 1 fscale.write(name + ',\t' + IFM_height + ',\t' + IFM_width + ',\t' + filt_height + ',\t' + filt_width + ',\t' + channels + ',\t' + num_filt + ',\t' + str(strides[idx]) + ',\n') fscale.close() f.close() #""" cxf = open(CX_PATH + "ModelShape.txt", "w") cxf.write("LayerName\tLayerID\tInputShape\tOutputShape\tKernelShape\n") cx_layers = [] layer_idx = 0 for n, m in model.named_modules(): if isinstance(m, torch.nn.Conv2d): if m.weight.shape[2] != m.weight.shape[3]: continue curr = "Conv\t" + str(layer_idx) + "\t" + str( tuple(acts[layer_idx].shape)).replace('(', '').replace( ')', '').replace(' ', '') + "\t" + str( tuple(acts[layer_idx].shape)).replace('(', '').replace( ')', '').replace(' ', '') + "\t" + str( tuple( m.weight.shape)).replace('(', '').replace( ')', '').replace(' ', '') + "\n" cxf.write(curr) cx_layers.append(curr) layer_idx += 1 if isinstance(m, torch.nn.Linear) and (not skipLinear): curr = "FC\t" + str(idxs[layer_idx]) + "\t" + str( tuple(acts[layer_idx].shape)).replace('(', '').replace( ')', '').replace(' ', '') + "\t" + str( tuple(acts[layer_idx].shape)).replace('(', '').replace( ')', '').replace(' ', '') + "\t" + str( tuple( m.weight.shape)).replace('(', '').replace( ')', '').replace(' ', '') + ",1,1\n" cxf.write(curr) cx_layers.append(curr) layer_idx += 1 if "transformer" in modelName: for i in range(11): for j in range(36, 97): cxf.write(cx_layers[j]) cxf.close() return
print EMOJIS # load the tf model with tf.Session() as sess: # Load slack metadata metadata = None with open("metadata.json", "r") as m: metadata = json.load(m) # Load vocabularies. vocab_file = FLAGS.data_dir + "/vocab.pkl" word2id = pkl.load(open(vocab_file, "rb")) id2word = {v: k for (k, v) in word2id.items()} embeddings = embedding.Embedding(None, word2id, id2word, word2id["UNK"], word2id["PAD"], word2id["</s>"], word2id["<s>"]) # Create model and load parameters. model = create_model(sess, True, len(word2id)) if slack_client.rtm_connect(): print "%s running: id %s, token %s" % (BOT_NAME, BOT_ID, TOKEN) while True: command, channel = parse_slack_output(slack_client.rtm_read()) if command and channel: handle_command(command, channel, model, embeddings, metadata) time.sleep(READ_WEBSOCKET_DELAY) else: print "%s failed" % BOT_NAME