Skip to content
Snippets Groups Projects
Commit 0638cd5f authored by hl00586's avatar hl00586
Browse files

first commit

parent 96d5aeb8
No related branches found
No related tags found
No related merge requests found
Showing with 13 additions and 208 deletions
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
...@@ -8,8 +8,12 @@ ...@@ -8,8 +8,12 @@
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" /> <option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" /> <option name="LAST_RESOLUTION" value="IGNORE" />
</component> </component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component>
<component name="ProjectId" id="1UWaMsw6zBMWl6bWHnCTXed0kq5" /> <component name="ProjectId" id="1UWaMsw6zBMWl6bWHnCTXed0kq5" />
<component name="PropertiesComponent"> <component name="PropertiesComponent">
<property name="SHARE_PROJECT_CONFIGURATION_FILES" value="true" />
<property name="settings.editor.selected.configurable" value="com.jetbrains.python.configuration.PyActiveSdkModuleConfigurable" /> <property name="settings.editor.selected.configurable" value="com.jetbrains.python.configuration.PyActiveSdkModuleConfigurable" />
</component> </component>
<component name="RunDashboard"> <component name="RunDashboard">
...@@ -47,6 +51,9 @@ ...@@ -47,6 +51,9 @@
<module name="Task_likelihood" /> <module name="Task_likelihood" />
<option name="INTERPRETER_OPTIONS" value="" /> <option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" /> <option name="PARENT_ENVS" value="true" />
<envs>
<env name="PYTHONUNBUFFERED" value="1" />
</envs>
<option name="SDK_HOME" value="" /> <option name="SDK_HOME" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" /> <option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" />
<option name="IS_MODULE_SDK" value="true" /> <option name="IS_MODULE_SDK" value="true" />
......
No preview for this file type
No preview for this file type
No preview for this file type
from keras.models import Model
from keras.layers import Dense, Input, Conv2D, Flatten, MaxPooling2D
from configuration import conf
from utils.dataloader import Sequential_loader
import numpy as np
from utils.model_utils import mask_layer_by_task
from utils.layers import Probability_CLF_Mul_by_task
from utils.train_utils import train_with_task
from utils.predict_utils import get_task_likelihood, get_test_acc
PATH = './results/%s/' % conf.dataset_name
epochs = 50
latent_dim = 250
output_dim = 10
verbose = 0
data_loader = Sequential_loader()
inputs = Input(shape=(784,))
task_input = Input(shape=(5,))
archi = Dense(1000, activation='relu')(inputs)
archi = mask_layer_by_task(task_input, archi)
archi = Dense(1000, activation='relu')(archi)
archi = mask_layer_by_task(task_input, archi)
task_output = Probability_CLF_Mul_by_task(conf.num_tasks, num_centers=output_dim // conf.num_tasks)(
[task_input, archi])
task_output = mask_layer_by_task(task_input, task_output, 'task_out')
clf = Dense(output_dim, activation='softmax')(archi)
clf = mask_layer_by_task(task_input, clf, 'clf_out')
model = Model(inputs=[inputs, task_input], outputs=[clf, task_output])
model.compile(loss=['categorical_crossentropy', 'mse'], optimizer='adam', metrics=['accuracy', 'mse'],
loss_weights=[1, 4])
tlh = [] # Task Likelihood
tlh_std = [] # Standard Deviation of Task Likelihood
test_acc = []
for task_idx in range(conf.num_tasks):
# Learn a new task
train_with_task(model, task_idx=task_idx, data_loader=data_loader)
# Get the likelihood of the current task
mean, std = get_task_likelihood(model, learned_task=task_idx, test_task=task_idx, data_loader=data_loader)
tlh.append(mean)
tlh_std.append(std)
# Get the likelihood of the next task
if task_idx < conf.num_tasks - 1:
mean, std = get_task_likelihood(model, learned_task=task_idx, test_task=task_idx+1, data_loader=data_loader)
tlh.append(mean)
tlh_std.append(std)
# Run 200 times to get the test accuracy (for drawing the figure)
for _ in range(conf.num_runs):
test_acc.append(get_test_acc(model,data_loader,test_on_whole_set=False))
# Print the average test accuracy across all the tasks
print('Learned %dth Task, Average test accuracy on all the task : %.3f'%(task_idx,get_test_acc(model, data_loader, test_on_whole_set=True)))
def paa(sample, w=None):
w = sample.shape[0] // 20 if w is None else w
l = len(sample)
stepfloat = l / w
step = int(np.ceil(stepfloat))
start = 0
j = 1
paa = []
while start <= (l - step):
section = sample[start:start + step]
paa.append(np.mean(section))
start = int(j * stepfloat)
j += 1
return paa
tlh_s = []
for i in tlh:
tlh_s += i.tolist()
tlh_s = np.array(tlh_s)
tlh_std_s = []
for i in tlh_std:
tlh_std_s += i.tolist()
tlh_std_s = np.array(tlh_std_s)
test_acc_s = np.array(test_acc).reshape(-1)
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
tlh = np.array(paa(tlh_s))
tlh_std = np.array(paa(tlh_std_s))
test_acc = np.array(paa(test_acc_s, tlh.shape[0]))
fig = sns.lineplot(np.arange(len(tlh)), tlh, label='Task Likelihood')
fig.fill_between(np.arange(len(tlh)), tlh - tlh_std, tlh + tlh_std, alpha=0.3)
fig = sns.lineplot(np.arange(len(tlh)), test_acc, label='Test Accuracy')
a = [10, 30, 50, 70]
for i in a:
fig.fill_between(np.arange(i, i + 10 + 1), 0, 0.1, alpha=0.1, color='red')
fig.fill_between(np.arange(i - 10, i + 1), 0, 0.1, alpha=0.1, color='green')
fig.fill_between(np.arange(90 - 10, 90), 0, 0.1, alpha=0.1, color='green')
# a = fig.get_xticklabels()
fig.set_xticklabels(['', 'Task 1', 'Task 2', 'Task 3', 'Task 4', 'Task 5'])
plt.legend(loc='center right')
plt.savefig(PATH + 'result')
plt.show()
import tensorflow as tf
from keras.models import Model
from keras.layers import Dense, Input, Lambda, Dropout
from configuration import conf
from utils.dataloader import Sequential_loader
import numpy as np
from utils.layers import Probability_CLF_Mul_by_task
def mask_layer_by_task(task_input, input_tensor, name=None, return_mask=False):
mask = tf.expand_dims(task_input, axis=-1)
mask = tf.tile(mask, multiples=[1, 1, input_tensor.shape[1] // conf.num_tasks])
mask = tf.keras.layers.Flatten()(mask)
if name is None:
out = Lambda(lambda x: x * mask)(input_tensor)
else:
out = Lambda(lambda x: x * mask, name=name)(input_tensor)
if return_mask:
return out, mask
else:
return out
def get_model_keras_mask(output_dim, label=None):
inputs = Input(shape=(784,))
task_input = Input(shape=(5,))
archi = Dense(1000, activation='relu')(inputs)
archi = mask_layer_by_task(task_input, archi)
archi = Dense(1000, activation='relu')(archi)
archi = mask_layer_by_task(task_input, archi)
task_output = Probability_CLF_Mul_by_task(conf.num_tasks, num_centers=output_dim // conf.num_tasks)(
[task_input, archi])
task_output = mask_layer_by_task(task_input, task_output, 'task_out')
clf = Dense(output_dim, activation='softmax')(archi)
clf = mask_layer_by_task(task_input, clf, 'clf_out')
model = Model(inputs=[inputs, task_input], outputs=[clf, task_output])
model_latent = Model(inputs=inputs, outputs=archi)
model.compile(loss=['categorical_crossentropy', 'mse'], optimizer='adam', metrics=['accuracy', 'mse'],
loss_weights=[1, 4])
return model, model_latent
data_loader = Sequential_loader()
model, model_latent = get_model_keras_mask(10)
for task_idx in range(conf.num_tasks):
x, y = data_loader.sample(task_idx=task_idx, whole_set=True)
task_input = np.zeros([y.shape[0], conf.num_tasks])
task_input[:, task_idx] = 1
model.fit([x, task_input], [y, task_input], epochs=10, batch_size=conf.batch_size, verbose=0)
if task_idx == 0:
model.layers[1].trainable = False
model.compile(loss=['categorical_crossentropy', 'mse'], optimizer='adam', metrics=['accuracy', 'mse'],
loss_weights=[1, 4])
for task_idx in range(conf.num_tasks):
x, y = data_loader.sample(task_idx, whole_set=True, dataset='test')
for test_idx in range(conf.num_tasks):
task_input = np.zeros([y.shape[0], conf.num_tasks])
task_input[:, test_idx] = 1
res = np.max(model.predict([x, task_input])[1], axis=1)
block_size = conf.test_batch_size
def block_likelihood(res):
block_likelihood = []
for r in res:
extra_index = r.shape[0] % block_size
extra_values = r[-extra_index:]
resize_values = r[:-extra_index]
r = resize_values.reshape(-1, block_size)
r = np.mean(r, axis=1, keepdims=True)
r = np.repeat(r, block_size, axis=1).reshape(-1, )
extra = np.repeat(np.mean(extra_values), len(extra_values))
final = np.append(r, extra)
block_likelihood.append(final)
return block_likelihood
test_acc = []
for task_idx in range(conf.num_tasks):
x, y = data_loader.sample(task_idx, whole_set=True, dataset='test')
res = []
pred = []
for test_idx in range(conf.num_tasks):
task_input = np.zeros([y.shape[0], conf.num_tasks])
task_input[:, test_idx] = 1
prediction = model.predict([x, task_input])
res.append(np.max(prediction[1], axis=1))
pred.append(np.argmax(prediction[0], axis=1))
res = block_likelihood(res)
pred = np.array(pred)
acc = np.sum(pred[np.argmax(res, axis=0), np.arange(pred.shape[1])] == np.argmax(y, axis=1)) / y.shape[0]
print('Task %d, Accuracy %.3f' % (task_idx, acc))
test_acc.append(acc)
print('Average of Test Accuracy : %.3f' % np.mean(test_acc))
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment