예제 #1
0
  def generate(self, length, alpha, beta):
    '''
    解を生成
    - @param {Integer} length 解の長さ
    - @param {Array} alpha
    - @param {Array} beta

    '''
    # 制約条件を満たす解を生成
    while True:
      x = "".join([str(random.randint(1, 6)) for i in range(length)])
      result = eval.evaluate(x_str=x, mode="constraint", bias_alpha=alpha, bias_beta=beta, valiables=len(x))
      if result.count(0) == len(result):
        break
    # 結果
    self.value = {
        "x": x,
        "objective": eval.evaluate(x_str=x, bias_alpha=alpha, bias_beta=beta, valiables=len(x))
    }
예제 #2
0
  def renew(self, length, origin_harmony_list, alpha, beta, change_count=1):
    '''
    解の作り変え
    - @param {Integer} length,
    - @param {Array} origin_harmony_list 変更前となるharmonyのリスト
    - @param {Array} alpha
    - @param {Array} beta
    - @param {Number} change_count
    '''

    # 対象となる解を決定
    r = random.randint(0, len(origin_harmony_list) - 1)
    while True:
      # 新規解
      newx = ""
      for i in range(length):
        if random.random() < self.R_A:
          if random.random() < self.R_P:
            # 既存の解を継承
            newx = newx + origin_harmony_list[r].value["x"][i]
          else:
            # 既存の解をすこし変更して継承
            add_num = (int(origin_harmony_list[r].value["x"][i]) - 1 + random.randint(-3, 3)) % 6 + 1
            if add_num < 0:
              add_num += 6
            newx = newx + str(add_num)
        else:
          # ランダムな継承
          newx = newx + str(random.randint(1, 6))

      result = eval.evaluate(x_str=newx, mode="constraint", bias_alpha=alpha, bias_beta=beta, valiables=len(newx))
      if result.count(0) == len(result):
        break

    self.value = {
        "x": newx,
        "objective": eval.evaluate(x_str=newx, bias_alpha=alpha, bias_beta=beta, valiables=len(newx))
    }
예제 #3
0
def createVirtualResult(x, vAlpha, vBeta, vGamma):
    '''
  仮設定から仮の結果を出力する
  - @param {String} x submitされた結果
  - @param {Array} vAlpha 仮置きα
  - @param {Array} vBeta 仮置きβ
  - @param {Array} vGamma 仮置きγ
  '''
    return {
        "objective":
        eval.evaluate(x_str=x,
                      bias_alpha=vAlpha,
                      bias_beta=vBeta,
                      bias_gamma=vGamma,
                      valiables=len(x),
                      mode="objective"),
        "constraint":
        eval.evaluate(x_str=x,
                      bias_alpha=vAlpha,
                      bias_beta=vBeta,
                      bias_gamma=vGamma,
                      valiables=len(x),
                      mode="constraint"),
    }
예제 #4
0
            images, labels = images.cuda(), labels.cuda()
        labels = labels.view(-1)

        predictions = model_deterministic(images)
        loss = loss_func(predictions, labels)
        model_deterministic.zero_grad()
        loss.backward()
        optimizer.step()

        if i % 50 == 0:
            loss = loss.data[0]
            print('[%d / %d] Loss: %f' % ((epoch - 1) * len(train_loader) + i,
                                          epochs * len(train_loader), loss))

    print('\nFinished Epoch %d' % epoch)
    acc = evaluate(model_deterministic, test_loader)
    print('Test Accuracy: %.2f\n' % (acc * 100))

model_deterministic.eval()
state = model_deterministic.state_dict()
torch.save(state, 'saved_model.pt')

model_stochastic = Model(True)
if torch.cuda.is_available():
    model_stochastic.cuda()
model_stochastic.eval()
model_stochastic.load_state_dict(state)

print('Stochastic Model Evaluation')
acc = evaluate(model_stochastic, test_loader)
print('Test Accuracy: %.2f\n' % (acc * 100))
예제 #5
0
def train():

    ### CONFIGS ###

    batch_size = 25
    lr = 0.0002
    weight_decay = 1e-5

    iter_i = 0
    train_loss = 0.
    losses = []
    accuracies = []
    best_eval = 1.0e9
    best_iter = 0
    threshold = 1e-4

    ################

    train_data = list(sst_reader("data/sst/train.txt"))
    dev_data = list(sst_reader("data/sst/dev.txt"))
    test_data = list(sst_reader("data/sst/test.txt"))

    iters_per_epoch = len(train_data) // batch_size

    vocabulary = Vocabulary()
    glove = load_glove('data/sst/glove.840B.300d.sst.txt', vocabulary)
    t2i = OrderedDict({
        p: i
        for p, i in zip([
            "very negative", "negative", "neutral", "positive", "very positive"
        ], range(5))
    })

    # Build model
    model = build_model(vocabulary, t2i)
    initialize_model_(model)

    with torch.no_grad():
        model.embed.weight.data.copy_(torch.from_numpy(glove))
        model.embed.weight.requires_grad = False
        model.embed.weight[1] = 0.0

    optimizer = Adam(model.parameters(), lr=lr, weight_decay=weight_decay)

    scheduler = ReduceLROnPlateau(optimizer,
                                  mode="min",
                                  factor=0.5,
                                  patience=5,
                                  verbose=True,
                                  cooldown=5,
                                  threshold=1e-4,
                                  min_lr=1e-5)

    model = model.to(device)

    while True:  # when we run out of examples, shuffle and continue
        for batch in get_minibatch(train_data,
                                   batch_size=batch_size,
                                   shuffle=True):
            epoch = iter_i // iters_per_epoch

            model.train()
            x, targets, _ = prepare_minibatch(batch,
                                              model.vocab,
                                              device=device)
            mask = (x != 1)

            logits = model(x)

            loss, loss_optional = model.get_loss(logits, targets, mask=mask)
            model.zero_grad()

            train_loss += loss.item()
            loss.backward()

            torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=5.)
            optimizer.step()
            iter_i += 1

            if iter_i % 100 == 0:

                train_loss = train_loss / 100
                print(f"Epoch {epoch}, Iter {iter_i}, loss={train_loss}")
                losses.append(train_loss)
                train_loss = 0.

            if iter_i % iters_per_epoch == 0:
                dev_eval = evaluate(model,
                                    dev_data,
                                    batch_size=batch_size,
                                    device=device)

                print(f"Epoch {epoch} iter {iter_i}: dev {dev_eval['acc']}")

                # save best model parameters
                compare_score = dev_eval["loss"]
                if "obj" in dev_eval:
                    compare_score = dev_eval["obj"]

                scheduler.step(compare_score)  # adjust learning rate

                if (compare_score <
                    (best_eval *
                     (1 - threshold))) and iter_i > (3 * iters_per_epoch):

                    best_eval = compare_score
                    best_iter = iter_i

                    if not os.path.exists('sst_results/default'):
                        os.makedirs('sst_results/default')

                    ckpt = {
                        "state_dict": model.state_dict(),
                        "optimizer_state_dict": optimizer.state_dict(),
                        "best_eval": best_eval,
                        "best_iter": best_iter
                    }
                    path = os.path.join('sst_results/default', "model.pt")
                    torch.save(ckpt, path)

            if iter_i == (iters_per_epoch * 25):

                path = os.path.join('sst_results/model', "model.pt")
                model.load_state_dict(torch.load(path)["state_dict"])

                print("# Evaluating")
                dev_eval = evaluate(model,
                                    dev_data,
                                    batch_size=25,
                                    device=device)
                test_eval = evaluate(model,
                                     test_data,
                                     batch_size=25,
                                     device=device)

                print("best model iter {:d}: "
                      "dev {} test {}".format(best_iter, dev_eval['acc'],
                                              test_eval['acc']))

                return losses, accuracies
예제 #6
0
파일: hw4.py 프로젝트: seanfrischmann/Repl
def repl():
	print (
		'\nInterpreter\n'
		'Author: Sean Frischmann\n'
		'Class: Cse 305\n'
	)
	env = dict()
	env['stack'] = list()
	env['global'] = dict()
	env['active'] = list()
	env['global']['bindings'] = dict()
	env['global']['closures'] = list()
	env['global']['binded closures'] = dict()
	env['inactive'] = list()
	mode = 'default'
	buf = ''
	isSpace = False
	isNested = False
	apply_count = 0
	temp_buf = []
	temp_position = []
	active_env = 0
	closure_count = 0
	user_input = ''
	while True:
		position=0
		if apply_count > 0:
			if isNested:
				isNested = False
				env['inactive'] = list()
			if len(temp_buf) != 0:
				buf = temp_buf[0]
				temp_buf.pop(0)
				position = temp_position[0]
				temp_position.pop(0)
				position += 1
			apply_count += -1
			active_env += -1
			if len(env['active']) != 0:
				env['active'].pop(0)
		elif mode == 'default':
			buf = input('repl> ')
		else:
			buf = input(' ')
		while position < len(buf):
			if buf[position] == '"':
				if mode == 'closure':
					mode = 'closure-string'
				elif mode == 'closure-string':
					mode = 'closure'
				elif mode == 'string':
					mode = 'default'
				else:
					mode = 'string'
			if buf[position] == '{':
				closure_count = closure_count + 1
				mode = 'closure'
			if (buf[position] == ' ') and (mode == 'default'):
				isSpace = True
			else:
				user_input = user_input + buf[position]
			if isSpace or position == len(buf)-1:
				if user_input == 'apply':
					topValue = env['stack'][0]
					if topValue[0] == '<':
						topValue = primitives.get_not_local(topValue)
					if topValue == ':closure:':
						temp_buf.insert(0, buf)
						temp_position.insert(0, position)
						closureValue = env['global']['closures'][0]
						env['active'].insert(0,{})
						env['active'][0]['bindings'] = dict()
						env['active'][0]['binded closures'] = dict()
						if isinstance(closureValue, dict):
							isNested = True
							linked_env = []
							for val in closureValue.values():
								linked_env = linked_env + val
							env['inactive'] = linked_env
							env['inactive'].insert(0,env['active'][0])
							for val in closureValue.keys():
								closureValue = val
						buf = closureValue
						env['global']['closures'].pop(0)
						env['stack'].pop(0)
						active_env += 1
					else:
						buf = ':error:' + buf[position+1:len(buf)]
					user_input = ''
					apply_count += 1
					isSpace = False
					position = 0
					continue
				elif user_input == 'load':
					if len(env['stack']) > 0 and (env['stack'][0])[0] == '"':
						try:
							inputFile = env['stack'][0]
							inputFile = inputFile.replace('"','')
							if position == len(buf)-1:
								buf = buf[position+1:len(buf)]
							else:
								buf = buf[position:len(buf)]
							buf = load(inputFile)+' '+':true:'+buf
							env['stack'].pop(0)
							user_input = ''
							position = 0
							continue
						except:
							user_input = ':false:'
							continue
				elif mode == 'string':
					user_input = user_input + '\n'
				elif mode == 'default':
					evaluater.evaluate(user_input,env, active_env, isNested)
					user_input = ''
					isSpace = False
			if buf[position] == '}' and mode == 'closure':
				closure_count = closure_count - 1
				if closure_count == 0:
					isFrontValid = True
					isEndValid = True
					isMore = True
					if user_input[1] != ' ':
						isFrontValid = False
						temp = ':error:'+user_input[2:len(user_input)]
						user_input = temp
					if user_input[len(user_input)-2] != ' ':
						isEndValid = False
						if isFrontValid:
							temp = ':error:'+user_input[1:len(user_input)]
							user_input = temp
					if (position+1) < len(buf):
						if buf[position+1] != ' ':
							if isFrontValid:
								isFrontValid = False
								temp = ':error:'+user_input[1:len(user_input)]
								user_input = temp
						else:
							isMore = True
					if isFrontValid and isEndValid:
						closure.createClosure(user_input, env, active_env)
						if isMore:
							position = position+1
					else:
						buf = user_input + buf[position+1:len(buf)]
						position = -1
					mode = 'default'
					user_input = ''
			position = position+1
		if mode == 'default' and (apply_count == 0):
			print_list(env['stack'])