<jupyter_start><jupyter_text>import previous models<jupyter_code>import pandas as pd
import ast,json
import numpy as np
from pymatgen.core import Composition
import CompositionDeformer as compd
from tensorflow.keras.utils import to_categorical
print("hello new materials")
from tensorflow.keras.utils import plot_model
from tensorflow.keras.models import load_model
from tensorflow.keras.models import clone_model
from IPython.display import Image
import tqdm
from tensorflow.keras.layers import Activation, Conv2D, Dense, Flatten, MaxPooling2D, Dropout
from tensorflow.keras.models import Sequential, Model
def reset_weights(model,weights=None):
    for layer in model.layers:
        print("---------------")
        print(layer)
        if isinstance(layer, tf.keras.Model): #if you're using a model as a layer
            reset_weights(layer) #apply function recursively
            continue
        if hasattr(layer, 'cell'):
            init_container = layer.cell
        else:
            init_container = layer

        for key, initializer in init_container.__dict__.items():
            if ("kernel_initializer" or "recurrent_initializer") not in key: #is this item an initializer?
                  continue #if no, skip it
            else:
                print("key:")
                print(key)
                print("initializer:")
                print(initializer)
                weights = layer.get_weights()
                weights = [initializer(w.shape, w.dtype) for w in weights] 
                layer.set_weights(weights)
#load models and visualize model layers
# loaded_model= load_model("./models/release/200827_ES_custom_50ep.h5")
loaded_model= load_model("./models/201231_ES_custom_50ep_50try.h5")
plot_model(loaded_model, to_file='./loaded_model.png', show_layer_names=True, show_shapes=True)
display(Image('loaded_model.png'))<jupyter_output>WARNING:tensorflow:Error in loading the saved optimizer state. As a result, your model is starting with a freshly initialized optimizer.<jupyter_text>import source data<jupyter_code># import materials project database
seeddata = pd.read_csv("./melting_points.csv")
# add 1d periodic table array and normalized magnifications
normalized_composition = []
melting_points = []
print(seeddata.columns)
normalized_compositions = []
for unit in tqdm.tqdm(seeddata["composition"]):
    composition_name = unit
    comp = Composition(composition_name)
    normalized_compositions.append(comp.fractional_composition.reduced_formula)
#拡張周期表表記の入力配列を作成
periodic_1d_arrays = []
for i,composition_name in enumerate(normalized_compositions):
    try:
        compd.generate_extended_PT_from_composition(composition_name).get_1d_array()
    except:
        del normalized_compositions[i]
        del seeddata['melting point(K)'][i]
#入力データを2次元マップに変換
print(len(seeddata['melting point(K)']))
for comp in normalized_compositions:
    periodic_1d_arrays.append(compd.generate_extended_PT_from_composition(comp).get_1d_array())
X = np.array(periodic_1d_arrays, dtype = np.float32)
X = X.reshape((len(X),7,32))
print(X.shape)

##融点をnumpy配列化
melting_point_array = np.array(seeddata['melting point(K)'],dtype=np.float32)
print(len(melting_point_array))
#訓練用データセットに分ける
from sklearn.model_selection import train_test_split
#新規データとしてband gapを取得し訓練用とテスト用に分ける
X_train,X_test,meltingpoint_train,meltingpoint_test = train_test_split(X,melting_point_array,test_size=0.2)
# X_train,X_test,energy_train,energy_test,symmetry_train,symmetry_test = train_test_split(X,dropped_seeddata["normalized_energy"],symmetry_category,test_size=0.1)<jupyter_output><empty_output><jupyter_text>transfer learning with new data<jupyter_code>for i in [-1, -2, -3, -4, -5, -6, -7,-8,-9,-10,-11,-12]:    
    layer = loaded_model.layers[i].output
    print(layer.name)
#model reconstruction
# すでにあるモデルの途中から新しいモデルを作る。

##レイヤー名の取得と切断レイヤーの選別
for i in [-1, -2, -3, -4, -5, -6, -7]:    
    layer = loaded_model.layers[i].output
    print(layer.name)
##残すベース部分のアウトプット層を取得
cut_output = loaded_model.layers[-6].output
##層を追加
fc1 = Dense(256, activation="relu")(cut_output)
fc2 = Dropout(rate=0.5)(fc1)
fc2._name = "dddd"
fc3 = Dense(100, activation="relu")(fc2)
fc4 = Dense(1, activation="linear")(fc3)
##転移モデルを作成
transfered_model = Model(inputs=loaded_model.inputs, outputs=fc4)
##ベースの層の訓練を禁止
for i in range(7):
    transfered_model.layers[i].trainable = False
transfered_model.summary()

##訓練用モデルを構築
transfered_model.compile(loss="mse", optimizer="adam", metrics=["mae"])
#visualization of transfer learning model
plot_model(transfered_model, to_file='./transfered_melting_model_210114.png', show_layer_names=True, show_shapes=True)
display(Image('transfered_melting_model_210114.png'))
#limit inputs
data_limit = 400
limited_X = X_train[:data_limit]
validation_X = X_train[data_limit:]

limited_meltingpoint = meltingpoint_train[:data_limit]
validation_meltingpoint = meltingpoint_train[data_limit:]

transfered_history = transfered_model.fit(limited_X, limited_meltingpoint, epochs=25, validation_split=0.1,batch_size=64)
transfered_model.evaluate(validation_X, validation_meltingpoint)
import matplotlib.pyplot as plt
# 損失値をグラフ表示
history = transfered_history
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()

# 正解率をグラフ表示
plt.plot(history.history['mae'],label="train")
plt.plot(history.history['val_mae'],label="test")
plt.title('model accuracy')
plt.ylabel('mean absolute error')
plt.xlabel('epochs')
# plt.legend(['train', 'test'], loc='upper right')
plt.legend(bbox_to_anchor=(1, 1), loc='upper right', borderaxespad=2, fontsize=10)
plt.savefig("./transfer_evaluation.png",dpi=200)
plt.show()
#予測値と実測値の描画
import matplotlib.pyplot as plt
#visualization of train and test prediction
## energy
predicted_bandgap_train = transfered_model.predict(limited_X)
predicted_bandgap_test = transfered_model.predict(validation_X)
plt.figure(figsize=(6,6))
plt.scatter(validation_meltingpoint,predicted_bandgap_test,alpha =0.5,s=10,label="test")
plt.scatter(limited_meltingpoint,predicted_bandgap_train,s=2,label="train")
plt.title('melting point predicted vs actual')
# plt.xlim(-12.5,0)
# plt.ylim(-12.5,0)
plt.ylabel('predicted melting point(K)')
plt.xlabel('actual melting point(K)')
plt.legend(loc='upper left')
plt.savefig("./mpoint_test_train.png",dpi=200)
plt.show()
#R^2値
from sklearn.metrics import r2_score
print("R2 test: {}".format(r2_score(validation_meltingpoint,predicted_bandgap_test)))
print("R2 train: {}".format(r2_score(limited_meltingpoint,predicted_bandgap_train)))
#test値の評価
import matplotlib.pyplot as plt
#visualization of train and test prediction
## energy
predicted_bandgap_test = transfered_model.predict(X_test)
plt.figure(figsize=(6,6))
plt.scatter(meltingpoint_test,predicted_bandgap_test,alpha =1,s=2)
plt.title('')
plt.xlim(0,4000)
plt.ylim(0,4000)
plt.ylabel('predicted melting point(K)')
plt.xlabel('actual melting point(K)')
plt.legend(['test'], loc='upper left')
plt.savefig("./mpoint_unknownsamples.png",dpi=200)
plt.show()

#R^2値
from sklearn.metrics import r2_score
print("R2: {}".format(r2_score(meltingpoint_test, predicted_bandgap_test)))
demo_1d_arrays = []
for i,composition_name in enumerate(["TiO2"]):
    demo_1d_arrays.append(compd.generate_extended_PT_from_composition(composition_name).get_1d_array())
#入力データを2次元マップに変換
demo_data = np.array(demo_1d_arrays, dtype = np.float32)
demo_data = demo_data.reshape((len(demo_data),7,32))
print(demo_data.shape)

result = transfered_model.predict(demo_data)
print(result)<jupyter_output>(1, 7, 32)
[[1937.0082]]
{'fraction_non_alphanumeric': 0.12501632919660352, 'fraction_numerical': 0.023383409536250815, 'mean_word_length': 6.141791044776119, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '197651', 'license': 'MIT', 'n_tokens_mistral': 2917, 'n_tokens_neox': 2764, 'n_words': 476, 'path': 'TransferLearning_meltingpoint.ipynb', 'repo_name': 'wak-shogo/MatSeed', 'size': '7655'}