示例#1
0
	num_filters = 16
	latent_dim = 16
	num_conv_layers = 3
	n_meta_test_episodes = 1000
	n_meta_test_way = args.n_meta_test_way
	k_meta_test_shot = args.k_meta_test_shot
	n_meta_test_query = args.n_meta_test_query

	x = tf.placeholder(tf.float32, [None, None, im_height, im_width, channels])
	q = tf.placeholder(tf.float32, [None, None, im_height, im_width, channels])
	x_shape = tf.shape(x)
	q_shape = tf.shape(q)
	num_classes, num_support = x_shape[0], x_shape[1]
	num_queries = q_shape[1]
	labels_ph = tf.placeholder(tf.float32, [None, None, None])
	model = ProtoNet([num_filters]*num_conv_layers, latent_dim)
	x_latent = model(tf.reshape(x, [-1, im_height, im_width, channels]))
	q_latent = model(tf.reshape(q, [-1, im_height, im_width, channels]))
	ce_loss, acc = ProtoLoss(x_latent, q_latent, labels_ph, num_classes, num_support, num_queries)
	train_op = tf.train.AdamOptimizer().minimize(ce_loss)
	tf_config = tf.ConfigProto()
	tf_config.gpu_options.allow_growth=True
	sess = tf.InteractiveSession(config=tf_config)
	init_op = tf.global_variables_initializer()
	sess.run(init_op)

    # call DataGenerator with k_shot+n_query samples per class
	data_generator = DataGenerator(n_way, k_shot+n_query, n_meta_test_way, k_meta_test_shot+n_meta_test_query, config={'data_folder': args.data_path})
	for ep in range(n_epochs):
		for epi in range(n_episodes):
			#############################
示例#2
0
state_dict = t.load(model_path_man.Model() + '_v%s.0' % version)
# state_dict = t.load(path_man.DatasetBase()+'models/ProtoNet_v105.0')
word_matrix = state_dict['Embedding.weight']

if model_name == 'IMP':
    model = IMP(word_matrix,
                **modelParams)
elif model_name == 'SIMPLE':
    model = SIMPLE(word_matrix,
                   **modelParams)
elif model_name == 'HybridIMP':
    model = HybridIMP(word_matrix,
                      **modelParams)
elif model_name == 'ProtoNet':
    model = ProtoNet(word_matrix,
                     **modelParams)
elif model_name == 'NnNet':
    model = NnNet(word_matrix, **modelParams)

model.load_state_dict(state_dict)
model = model.cuda()
model.eval()

print('task_seed:', task_seed)
print('sampling_seed', sampling_seed)

if plot_option == 'entire':
    dataloader = DataLoader(dataset, batch_size=N, collate_fn=batchSequenceWithoutPad)

    datas = []
    original_input = []
示例#3
0
else:
    word_matrix = None



################################################
#----------------------模型定义和初始化------------------
################################################

printState('init model...')

# model = CNNLstmProtoNet()
# model = IncepProtoNet(channels=[1, 32, 1],
#                       depth=3)
if model_type == 'ProtoNet':
    model = ProtoNet(pretrained_matrix=word_matrix,
                     **modelParams)
elif model_type == 'InductionNet':
    model = InductionNet(pretrained_matrix=word_matrix,
                         **modelParams)
elif model_type == 'MetaSGD':
    model = MetaSGD(n=n,
                    loss_fn=loss,
                    pretrained_matrix=word_matrix,
                    **modelParams
                    )
elif model_type == 'ATAML':
    model = ATAML(n=n,
                  loss_fn=loss,
                  pretrained_matrix=word_matrix,
                  **modelParams
                  )
示例#4
0
                                       emb,
                                       max_length=max_length,
                                       reprocess=False)
val_data_loader = JSONFileDataLoader('./data/' + args.dataset + '/test.json',
                                     emb,
                                     max_length=max_length,
                                     reprocess=False)
#deploy_data_loader = JSONFileDataLoader('./data/' + args.dataset+'/deploy.json', './data/health_meta/word_vec.json', max_length=max_length, reprocess=False)

framework = FewShotREFramework(train_data_loader, val_data_loader,
                               val_data_loader, val_data_loader)

model = ProtoNet(train_data_loader.word_vec_mat,
                 max_length,
                 word_embedding_dim=word_emb_size,
                 hidden_size=100,
                 args=args,
                 N=args.N_for_train,
                 sup_cost=bool(args.use_sup_cost))
model_name = args.model_name + str(seed)

print(model_name)

if args.train_or_test == 'train':
    pretrain = args.pretrain
    if args.pretrain:
        pretrain = './checkpoint/' + args.pretrain + '.pth.tar'
    framework.train(model,
                    model_name,
                    args.batch,
                    N_for_train=args.N_for_train,
示例#5
0
printState('init model...')
if not MODEL_RANDOM_STATE:
    state_dict = t.load(test_path_manager.Model(type=cfg.loadBest()))
    if model_type in ADAPTED_MODELS:
        word_matrix = state_dict['Learner.Embedding.weight']
    else:
        word_matrix = state_dict['Embedding.weight']
else:
    word_matrix = t.Tensor(
        np.load(test_path_manager.WordEmbedMatrix(), allow_pickle=True))
print("loading done...")

loss = t.nn.NLLLoss().cuda() if loss_func == 'nll' else t.nn.MSELoss().cuda()

if model_type == 'ProtoNet':
    model = ProtoNet(pretrained_matrix=word_matrix, **modelParams)
elif model_type == 'InductionNet':
    model = InductionNet(pretrained_matrix=word_matrix, **modelParams)
elif model_type == 'MetaSGD':
    model = MetaSGD(n=n,
                    loss_fn=loss,
                    pretrained_matrix=word_matrix,
                    **modelParams)
elif model_type == 'ATAML':
    model = ATAML(n=n,
                  loss_fn=loss,
                  pretrained_matrix=word_matrix,
                  **modelParams)
elif model_type == 'HybridAttentionNet':
    model = HAPNet(k=k, pretrained_matrix=word_matrix, **modelParams)
elif model_type == 'ConvProtoNet':