<jupyter_start><jupyter_text>CKA based selection<jupyter_code># Full
start = time.time()

selection, sim_sequence=select_feature_CKA(temp_array,5)

end = time.time()
for i in range(len(selection)):
    print("Sensor %d to retain: %d, with similarity: %.4f"%(i,selection[i],sim_sequence[i]))
print("total time:",(end - start))
# Full
start = time.time()

selection, sim_sequence=select_feature_CKA(hum_array,5)

end = time.time()
for i in range(len(selection)):
    print("Sensor %d to retain: %d, with similarity: %.4f"%(i,selection[i],sim_sequence[i]))
print("total time:",(end - start))
sim_matrix=np.empty([16,16])
sim_matrix.fill(1)
for i in range(16):
    for j in range(i+1,16):
        sim=CKA(temp_array[:,i],temp_array[:,j],False)
        sim_matrix[i,j]=sim
        sim_matrix[j,i]=sim
end = time.time()
custom_cmap = cm.afmhot
#custom_cmap.set_bad(color='gray')
fig, ax = plt.subplots()
im=sns.heatmap(sim_matrix,cmap=custom_cmap,xticklabels=list(range(16)),yticklabels=list(range(16)),vmin=0.9, vmax=1)
#fig.colorbar(im, ax=ax)
ax.xaxis.tick_top()
plt.savefig("plots/temp_sim_heatmap.pdf")
sim_matrix=np.empty([16,16])
sim_matrix.fill(1)
for i in range(16):
    for j in range(i+1,16):
        sim=CKA(hum_array[:,i],hum_array[:,j],False)
        sim_matrix[i,j]=sim
        sim_matrix[j,i]=sim
end = time.time()
custom_cmap = sns.color_palette("mako", as_cmap=True)
#custom_cmap.set_bad(color='gray')
fig, ax = plt.subplots()
im=sns.heatmap(sim_matrix,cmap=custom_cmap,xticklabels=list(range(16)),yticklabels=list(range(16)),vmin=0.9, vmax=1)
#fig.colorbar(im, ax=ax)
ax.xaxis.tick_top()
plt.savefig("plots/hum_sim_heatmap.pdf")<jupyter_output><empty_output><jupyter_text>Concrete autoencoder<jupyter_code>def convert_data(data, step):
    X, Y = [], []
    scaler = StandardScaler()
    scaler.fit(data)
    for i in range(len(data)-step+1):
        d = i+step  
        X.append(scaler.transform(data[i:d,]))
        Y.append(data[d-1,])
    return np.array(X), np.array(Y)<jupyter_output><empty_output><jupyter_text>Temperature<jupyter_code>X,Y=convert_data(temp_array,15)
print(temp_array.shape,X.shape,Y.shape)
X_train, X_val, Y_train, Y_val= train_test_split(X,Y, test_size=0.1, random_state=2021)
X_val, X_test, Y_val, Y_test= train_test_split(X_val,Y_val, test_size=0.25, random_state=2021)
print(X_train.shape,X_val.shape,X_test.shape)
print(Y_train.std(),Y_val.std(),Y_test.std())
# LEAKY RELU IS ABSOLUTELY CRUCIAL HERE
tf.random.set_seed(
    2021
)

decoder3 = Sequential()
decoder3.add(SimpleRNN(10,activation=LeakyReLU(0.2)))
decoder3.add(Dropout(0.1))
decoder3.add(Dense(16))

selector3 = ConcreteAutoencoderFeatureSelector(k = 3, output_function = decoder3, num_epochs =2500, batch_size=4, tryout_limit=1, learning_rate=0.01)

tf.random.set_seed(
    2021
)
decoder2 = Sequential()
decoder2.add(SimpleRNN(10,activation=LeakyReLU(0.2)))
decoder2.add(Dropout(0.1))
decoder2.add(Dense(16))

selector2 = ConcreteAutoencoderFeatureSelector(k = 2, output_function = decoder2, num_epochs =2500, batch_size=4, tryout_limit=1, learning_rate=0.01)

tf.random.set_seed(
    2021
)
decoder1 = Sequential()
decoder1.add(SimpleRNN(10,activation=LeakyReLU(0.2)))
decoder1.add(Dropout(0.1))
decoder1.add(Dense(16))

selector1 = ConcreteAutoencoderFeatureSelector(k = 1, output_function = decoder1, num_epochs =3000, batch_size=4, tryout_limit=1, learning_rate=0.02)
tf.random.set_seed(
    2021
)
selector1.fit(X_train, Y_train, X_val, Y_val)
selector1.model.evaluate(X_train, Y_train)
selector1.model.evaluate(X_test, Y_test)
ax=sns.barplot(x=list(range(16)),y=selector1.get_mask(), color="white", edgecolor='black')
ax.set_yticklabels([0,None,None,None,1,None,None,None,2])
print(selector1.get_mask())
tf.random.set_seed(
    2021
)
selector2.fit(X_train, Y_train, X_val, Y_val)
selector2.model.evaluate(X_train, Y_train)
selector2.model.evaluate(X_test, Y_test)
ax=sns.barplot(x=list(range(16)),y=selector2.get_mask(), color="white", edgecolor='black')
ax.set_yticklabels([0,None,None,None,1,None,None,None,2])
print(selector2.get_mask())
tf.random.set_seed(
    2021
)
selector3.fit(X_train, Y_train, X_val, Y_val)
selector3.model.evaluate(X_train, Y_train)
selector3.model.evaluate(X_test, Y_test)
ax=sns.barplot(x=list(range(16)),y=selector3.get_mask(), color="white", edgecolor='black')
ax.set_yticklabels([0,None,None,None,1,None,None,None,2])
print(selector3.get_mask())<jupyter_output>[0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 1.]<jupyter_text>Humidity<jupyter_code>X,Y=convert_data(hum_array,15)
print(temp_array.shape,X.shape,Y.shape)
X_train, X_val, Y_train, Y_val= train_test_split(X,Y, test_size=0.1, random_state=2021)
X_val, X_test, Y_val, Y_test= train_test_split(X_val,Y_val, test_size=0.25, random_state=2021)
print(X_train.shape,X_val.shape,X_test.shape)
print(Y_train.std(),Y_val.std(),Y_test.std())
# LEAKY RELU IS ABSOLUTELY CRUCIAL HERE
tf.random.set_seed(
    2021
)

decoder3 = Sequential()
decoder3.add(SimpleRNN(10,activation=LeakyReLU(0.2)))
decoder3.add(Dropout(0.1))
decoder3.add(Dense(16))

selector3 = ConcreteAutoencoderFeatureSelector(k = 3, output_function = decoder3, num_epochs =2500, batch_size=4, tryout_limit=1, learning_rate=0.01)

tf.random.set_seed(
    2021
)
decoder2 = Sequential()
decoder2.add(SimpleRNN(10,activation=LeakyReLU(0.2)))
decoder2.add(Dropout(0.1))
decoder2.add(Dense(16))

selector2 = ConcreteAutoencoderFeatureSelector(k = 2, output_function = decoder2, num_epochs =2500, batch_size=4, tryout_limit=1, learning_rate=0.01)

tf.random.set_seed(
    2021
)
decoder1 = Sequential()
decoder1.add(SimpleRNN(10,activation=LeakyReLU(0.2)))
decoder1.add(Dropout(0.1))
decoder1.add(Dense(16))

selector1 = ConcreteAutoencoderFeatureSelector(k = 1, output_function = decoder1, num_epochs =3000, batch_size=4, tryout_limit=1, learning_rate=0.01)
tf.random.set_seed(
    2021
)
selector1.fit(X_train, Y_train, X_val, Y_val)
selector1.model.evaluate(X_train, Y_train)
selector1.model.evaluate(X_test, Y_test)
ax=sns.barplot(x=list(range(16)),y=selector1.get_mask(), color="white", edgecolor='black')
ax.set_yticklabels([0,None,None,None,1,None,None,None,2])
print(selector1.get_mask())
tf.random.set_seed(
    2021
)
selector2.fit(X_train, Y_train, X_val, Y_val)
selector2.model.evaluate(X_train, Y_train)
selector2.model.evaluate(X_test, Y_test)
ax=sns.barplot(x=list(range(16)),y=selector2.get_mask(), color="white", edgecolor='black')
ax.set_yticklabels([0,None,None,None,1,None,None,None,2])
print(selector2.get_mask())
tf.random.set_seed(
    2021
)
selector3.fit(X_train, Y_train, X_val, Y_val)
selector3.model.evaluate(X_train, Y_train)
selector3.model.evaluate(X_test, Y_test)
ax=sns.barplot(x=list(range(16)),y=selector3.get_mask(), color="white", edgecolor='black')
ax.set_yticklabels([0,None,None,None,1,None,None,None,2])
print(selector3.get_mask())<jupyter_output>[1. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 1. 0.]
{'fraction_non_alphanumeric': 0.16594139831737742, 'fraction_numerical': 0.05134899912967798, 'mean_word_length': 7.297232250300842, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '531044', 'license': 'Unlicense', 'n_tokens_mistral': 3061, 'n_tokens_neox': 2870, 'n_words': 389, 'path': '.ipynb_checkpoints/jetbot_data_example-checkpoint.ipynb', 'repo_name': 'BarathTamas/JetBot-Thesis', 'size': '6894'}